Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Refuse Wakeshift watches for an attested person, and classify the new watch ops
garrytan0ab88f4cef85
diff --git a/docs/guides/wakeshift.md b/docs/guides/wakeshift.mdindex e7739b9966..f4386a77d8 100644--- a/docs/guides/wakeshift.md+++ b/docs/guides/wakeshift.md@@ -74,6 +74,8 @@ Each person then sees one agreement step before their first watch: the first `waThe drainer re-checks this before every wake: on a shared brain a watch whose person hasn't accepted the current terms ends with `last_error: terms_not_accepted` instead of waking. A member added after enable, on a brain whose links were never set, makes every watch refuse with `wakeshift_shared_brain` and `gbrain doctor` warn until you run `enable` with the links.+A watch and its terms acceptance belong to the credential (or the member it is bound to), not to a person a trusted system names with `x-qm-actor`. So a call that names one is refused with `actor_not_attested` (reason `op_not_attested`) on every watch tool, `list_watches`, `unwatch` and `accept_wakeshift_terms`, even when the operator listed them in `--attest-ops`: one person would otherwise see or end another's watches.+## Removing someone`gbrain access member remove`, `gbrain auth revoke` (by name or `--id`; also a token revoked from the admin API or replaced by a harness token rotation) and `gbrain auth revoke-client` end that person's or credential's open watches (`last_error: principal_revoked`), drop its terms acceptance, and queue a Wakeshift Forget User call for the hashed user it watched under, which deletes everything Wakeshift holds for it. The command never waits on the network and works with reporting back off: `gbrain serve` or the job worker sends queued forgets on their next tick (at most 10 a tick, 5 attempts on the delivery schedule), so while reporting back is off they wait. A credential bound to a member watched under the member's user, which outlives that one credential: its watches end, and the member's user is forgotten when the member is removed. A credential deleted some other way is caught when its watch would wake: the watch ends and its forget is queued. `gbrain doctor` warns about a forget waiting more than a day. A member added again later is a new member id with its own acceptance.diff --git a/src/core/ops/wakeshift.ts b/src/core/ops/wakeshift.tsindex 98370a926c..48654f3f0b 100644--- a/src/core/ops/wakeshift.ts+++ b/src/core/ops/wakeshift.ts@@ -18,6 +18,8 @@ import { readFix } from './op-fix.ts';import { agentOwnerFence, assertJobOwned, jobNotFound } from './jobs.ts';import { receiptAccess, receiptCallerSource, receiptNotFound, receiptVisible } from './persistence.ts';import { memberIdentityFor } from '../access/caller.ts';+import { ACTOR_HEADER } from '../access/actor-model.ts';+import { currentCallerAccess } from '../search/private-visibility.ts';import { parseWriteRequestId } from '../persistence/preconditions.ts';import { WakeshiftError, type WakeshiftCall } from '../wakeshift/client.ts';import { wakeshiftBrainKey, wakeshiftRuntime, wakeshiftSecret, wakeshiftUserId, type WakeshiftRuntime } from '../wakeshift/config.ts';@@ -63,6 +65,14 @@ function termsUnset(): OperationError {export interface WatchPrincipal { kind: 'oauth_client' | 'legacy_token' | 'local_stdio'; id: string; member: string | null }+/** A watch and its terms acceptance are keyed to the credential (principal, bound member), so an attested person (x-qm-actor) never runs one. */+function refuseAttested(): void {+ if (currentCallerAccess().actor) {+ throw opError('actor_not_attested', `Wakeshift watches cannot run as an attested person yet: a watch and its terms acceptance are kept to the credential, not the person ${ACTOR_HEADER} names, so one person would see or end another's. Nothing was read or written.`,+ `Call it without ${ACTOR_HEADER}; for a write, call get_write_request instead.`, { reason: 'op_not_attested' });+ }+}+/*** Who is watching: the verified credential (bound member when it has one), else the local stdio pipe. A stdio server* started with --stdio-member watches as principal id 'registration', so the drain re-checks that member, never config.@@ -172,6 +182,7 @@ function unrecognized(tool: string) {/** The live runtime and the watching principal, after the checks every watch shares. */async function watchContext(ctx: OperationContext, tool: string, cliSuggestion: string, fix?: Fix): Promise<{ runtime: WakeshiftRuntime; principal: WatchPrincipal }> {if (ctx.remote === false) throw needsConversation(tool, cliSuggestion, fix);+ refuseAttested();const runtime = wakeshiftRuntime();if (!runtime) throw notEnabled();const principal = await watchPrincipal(ctx);@@ -365,6 +376,7 @@ const list_watches: Operation = {publishGateKey: 'mcp.publish_wakeshift',cliHints: { hidden: true },handler: async (ctx) => {+ refuseAttested();if (!wakeshiftRuntime()) throw notEnabled();const rows = await ownRows(ctx, "AND (state = 'open' OR updated_at > now() - interval '1 day')", []);return { watches: rows.map(publicWatch) };@@ -412,6 +424,7 @@ const unwatch: Operation = {publishGateKey: 'mcp.publish_wakeshift',cliHints: { hidden: true },handler: async (ctx, p) => {+ refuseAttested();const runtime = wakeshiftRuntime();if (!runtime) throw notEnabled();const { key, extra, params } = unwatchFilter(p);diff --git a/test/access-member-facts.test.ts b/test/access-member-facts.test.tsindex a5db49a97d..11da96ec46 100644--- a/test/access-member-facts.test.ts+++ b/test/access-member-facts.test.ts@@ -339,6 +339,21 @@ for (const kind of testBackends()) {expect(await engine.executeRaw('SELECT 1 FROM tags t JOIN pages p ON p.id = t.page_id WHERE p.source_id = $1 AND p.slug = $2', [SRC, 'notes/' + shared])).toHaveLength(0);}, 60_000);+ test('Wakeshift watches are kept to the credential, so an attested person never runs one', async () => {+ const reg = await provider().registerClientManual('c2-watch-' + randomUUID().slice(0, 8), ['client_credentials'], 'read write', [], SRC);+ const ops = ['watch_write_request', 'list_watches', 'unwatch', 'accept_wakeshift_terms'];+ await bindCredential(engine, { kind: 'client', id: reg.clientId }, { attestActor: 'qm', attestOps: ['get_page', ...ops] });+ await readsUnscopedOnPurpose(engine, reg.clientId, 'client');+ tokens.watch = (await provider().exchangeClientCredentials(reg.clientId, reg.clientSecret!)).access_token;+ const calls: Array<[string, Record<string, unknown>]> = [['watch_write_request', { request_id: 'req-alice-example' }], ['list_watches', {}], ['unwatch', { job: 1 }], ['accept_wakeshift_terms', { terms_version: 'v1' }]];+ for (const [op, params] of calls) {+ const out = await call({ token: 'watch', actor: ALICE_ID }, op, params);+ expect({ op, isError: out.isError, code: out.json.code ?? out.json.error?.code ?? out.json.error, reason: out.json.reason ?? out.json.error?.reason }).toEqual({ op, isError: true, code: 'actor_not_attested', reason: 'op_not_attested' });+ }+ const [row] = await engine.executeRaw<{ n: number }>('SELECT count(*)::int AS n FROM wake_subscriptions');+ expect(row!.n).toBe(0);+ });+test('agent jobs: Alice\'s job is hers alone, and two people\'s identical submissions never share a job', async () => {const reg = await provider().registerClientManual('c2-agent-' + randomUUID().slice(0, 8), ['client_credentials'], 'read write', [], SRC);await engine.executeRaw("UPDATE oauth_clients SET scope = 'read write agent', bound_tools = $2, bound_source_id = source_id, federated_read = ARRAY[source_id], delegated_namespace = 'job', delegated_slug_prefixes = NULL, bound_max_concurrent = 5 WHERE client_id = $1", [reg.clientId, ['get_page', 'search']]);diff --git a/test/helpers/segmented-write-sweep.ts b/test/helpers/segmented-write-sweep.tsindex e2908db554..4e0a6051ab 100644--- a/test/helpers/segmented-write-sweep.ts+++ b/test/helpers/segmented-write-sweep.ts@@ -147,6 +147,7 @@ export const NO_PAGE_TARGET: Record<string, string> = {file_register: 'files row keyed by page slug; the page id is read by SQL, no Page object',submit_job: 'job queue', cancel_job: 'job queue', retry_job: 'job queue', pause_job: 'job queue', resume_job: 'job queue',replay_job: 'job queue', send_job_message: 'job queue', submit_agent: 'job queue', watch_job: 'job queue', unwatch: 'job queue',+ watch_write_request: 'wake subscription on a write receipt', watch_cron: 'wake subscription on a cron job', accept_wakeshift_terms: 'Wakeshift terms acceptance row',think: 'creates its own answer page; it reads pages as search results, not snapshots',sources_add: 'source registry', sources_remove: 'source registry',request_tools: 'tool surface request', connector_sync: 'connector pages it creates; its segmented holds are pinned by connector-segmented-page-9062',diff --git a/test/qm-attest-write-boundary.test.ts b/test/qm-attest-write-boundary.test.tsindex 40d71be222..60f41cc393 100644--- a/test/qm-attest-write-boundary.test.ts+++ b/test/qm-attest-write-boundary.test.ts@@ -50,6 +50,9 @@ const NOT_PAGE_WRITES: Record<string, string> = {request_tools: "the calling client's own MCP tool surface",think: 'read-only for remote callers; only a local --save writes',submit_agent: "queues a job; every tool call it makes is re-resolved against the deferred operations",+ watch_write_request: 'a Wakeshift watch row, no page; an attested call is refused (ops/wakeshift.ts refuseAttested)',+ unwatch: 'ends Wakeshift watch rows, no page; an attested call is refused (ops/wakeshift.ts refuseAttested)',+ accept_wakeshift_terms: 'a Wakeshift terms acceptance row, no page; an attested call is refused (ops/wakeshift.ts refuseAttested)',};describe('W1: the attested write boundary is built from the registry', () => {