Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix: pre-landing review fixes
garrytan55ec0ab5de88
diff --git a/src/core/access/actor-model.ts b/src/core/access/actor-model.tsindex 40b2627289..0365e3add1 100644--- a/src/core/access/actor-model.ts+++ b/src/core/access/actor-model.ts@@ -11,6 +11,8 @@ export const QM_AUDIENCE_HEADER = 'x-qm-audience';export const ACTOR_ARGUMENT = 'acting_user';export const ATTEST_SYSTEMS: readonly string[] = ['qm'];export const DEFAULT_ATTEST_OPS: readonly string[] = ['get_write_request', 'memory_recall', 'memory_write'];+/** GST-1113: the attested operations a credential with attest_sources may run: the memory pair, receipts and own-page withdrawal. */+export const SCOPE_ATTEST_OPS: ReadonlySet<string> = new Set(['forget', 'forget_fact', 'get_write_request', 'memory_recall', 'memory_write']);/*** Operations no attested call may run yet: what they read or write is not keyed to the person, so one person could* change or see another's (a conflict list across writers) or write facts and takes every reader sees. GST-359 C2@@ -34,7 +36,8 @@ export interface AttestedActor { system: string; external_id: string; member: st/*** The attest right a credential row carries, given the grant that issued the token (`legacy` for a legacy token,* else the OAuth grant type) and its member binding. Null unless the system is known, the credential is bound to no- * member, and the token is a legacy token or a client-credentials token.+ * member, and the token is a legacy token or a client-credentials token. A credential listing scope sources keeps only+ * SCOPE_ATTEST_OPS, at every call, so a later default never widens it.*/export function attestRightFrom(row: Record<string, unknown> | undefined, grant: unknown, member: unknown): AttestRight | null {const system = row?.attest_actor;@@ -42,7 +45,8 @@ export function attestRightFrom(row: Record<string, unknown> | undefined, grant:if (grant !== 'legacy' && grant !== 'client_credentials') return null;const ops = Array.isArray(row!.attest_ops) ? (row!.attest_ops as unknown[]).filter((v): v is string => typeof v === 'string') : [...DEFAULT_ATTEST_OPS];const sources = Array.isArray(row!.attest_sources) ? (row!.attest_sources as unknown[]).filter((v): v is string => typeof v === 'string') : null;- return { system, ops, sources: sources?.length ? sources : null };+ if (!sources?.length) return { system, ops, sources: null };+ return { system, ops: ops.filter(op => SCOPE_ATTEST_OPS.has(op)), sources };}/** The verify join plus the live attest right, for AuthInfo (a verifier spreads the result). */diff --git a/src/core/access/attest-sources.ts b/src/core/access/attest-sources.tsindex 933de5017a..37687f314f 100644--- a/src/core/access/attest-sources.ts+++ b/src/core/access/attest-sources.ts@@ -23,8 +23,7 @@ import { GrantError, grantFromRow, grantFromTokenRow, type ClientGrant, type Graimport { isScopeSourceConfig, normalizeSourceConfig, parseSourceConfig } from '../sources-load.ts';import { SCOPE_PAGE_SLUG } from './principals.ts';-/** The attested operations a credential with attest_sources may run: the memory pair, receipts and own-page withdrawal. */-export const SCOPE_ATTEST_OPS: ReadonlySet<string> = new Set(['forget', 'forget_fact', 'get_write_request', 'memory_recall', 'memory_write']);+export { SCOPE_ATTEST_OPS } from './actor-model.ts';/*** Whether this live write may target `sourceId` instead of the credential's own source: an attested memory_write that@@ -131,14 +130,15 @@ function attestInvalid(message: string, suggestion: string): OperationError {/*** Checks an attest_sources change for one credential (gbrain auth rescope --attest-sources) and marks each newly listed- * source, inside the caller's transaction. The mark is never cleared here.+ * source, and each listed source that lost its mark (a restore), inside the caller's transaction. The mark is never+ * cleared here. Returns whether anything was checked.*/-export async function applyAttestSources(db: Db, self: CredentialRef, next: readonly string[] | null, before: readonly string[] | null, opts: { dryRun?: boolean } = {}): Promise<void> {- const added = (next ?? []).filter(s => !(before ?? []).includes(s));- const dropped = (before ?? []).filter(s => !(next ?? []).includes(s));- if (!added.length && !dropped.length) return;+export async function applyAttestSources(db: Db, self: CredentialRef, next: readonly string[] | null, before: readonly string[] | null, opts: { dryRun?: boolean } = {}): Promise<boolean> {const sql = sqlOf(db);- await markedScopeSources(sql, added, true);+ const marked = await markedScopeSources(sql, next ?? [], true);+ const added = (next ?? []).filter(s => !(before ?? []).includes(s) || !marked.has(s));+ const dropped = (before ?? []).filter(s => !(next ?? []).includes(s));+ if (!added.length && !dropped.length) return false;const all = await credentialSources(sql);const mine = all.find(c => same(c.ref, self));const stillRead = dropped.filter(s => mine?.reads.includes(s));@@ -160,11 +160,12 @@ export async function applyAttestSources(db: Db, self: CredentialRef, next: read`Remove ${s} from those credentials' read grants first, or use a new source for the scope.`);}}- if (opts.dryRun) return;+ if (opts.dryRun) return true;for (const s of added) {const [row] = await sql`SELECT config FROM sources WHERE id = ${s}`;await sql`UPDATE sources SET config = ${JSON.stringify({ ...normalizeSourceConfig(row!.config), qm_scope: true, federated: false })}::text::jsonb WHERE id = ${s}`;}+ return true;}/** A remote memory_recall that names no source reads no scope source: a scope is read only by naming it. */diff --git a/src/core/access/members.ts b/src/core/access/members.tsindex 5d9fccf774..5e076e59e3 100644--- a/src/core/access/members.ts+++ b/src/core/access/members.ts@@ -334,13 +334,14 @@ export async function bindCredential(engine: BrainEngine, target: { kind: 'token'Register one for the trusted system: gbrain auth register-client <name> --grant-types client_credentials, then grant it --attest-actor qm.');}}- const changed = member !== before.member_id || JSON.stringify(mayActFor ?? null) !== JSON.stringify(before.may_act_for ?? null)+ let changed = member !== before.member_id || JSON.stringify(mayActFor ?? null) !== JSON.stringify(before.may_act_for ?? null)|| attestActor !== before.attest_actor || JSON.stringify(attestOps ?? null) !== JSON.stringify(before.attest_ops ?? null)|| JSON.stringify(attestSources ?? null) !== JSON.stringify(before.attest_sources ?? null);- if (changed) {+ if (changed || patch.attestSources != null) {// GST-1113: the scope-source checks, the marks and the binding commit together, under the scope sources' row locks.+ // A restated list also re-marks a listed source that lost its mark.await engine.transaction(async tx => {- await applyAttestSources(tx, target, attestSources ?? null, before.attest_sources ?? null, opts);+ if (await applyAttestSources(tx, target, attestSources ?? null, before.attest_sources ?? null, opts)) changed = true;if (opts.dryRun) return;await tx.executeRaw(`UPDATE ${table} SET member_id = $2, may_act_for = $3::text[], attest_actor = $4, attest_ops = $5::text[], attest_sources = $6::text[] WHERE ${key} = $1`,[target.id, member, mayActFor, attestActor, attestOps, attestSources]);diff --git a/src/core/backup/quarantine.ts b/src/core/backup/quarantine.tsindex f31e09b94e..c2c76e6f6b 100644--- a/src/core/backup/quarantine.ts+++ b/src/core/backup/quarantine.ts@@ -55,6 +55,7 @@ export async function quarantineRestoredExecution(tx: BrainEngine, options: {const detached = {...(typeof config.federated === 'boolean' ? { federated: config.federated } : {}),...(typeof config.strategy === 'string' ? { strategy: config.strategy } : {}),+ ...(config.qm_scope === true ? { qm_scope: true } : {}),syncEnabled: false, restore_detached: options.restoreId,};await tx.executeRaw('UPDATE sources SET local_path = NULL, config = $1::text::jsonb WHERE id = $2', [JSON.stringify(detached), row.id]);diff --git a/src/core/ops/qm-memory.ts b/src/core/ops/qm-memory.tsindex bc4e414cc3..cf1723714a 100644--- a/src/core/ops/qm-memory.ts+++ b/src/core/ops/qm-memory.ts@@ -27,6 +27,7 @@ import { currentCallerAccess, resolveExcludePrivatePages, withCallerAccess, typeimport { callerMember, memberPageSlug, SCOPE_PAGE_SLUG } from '../access/member-facts.ts';import { markedScopeSources, sourceNotAttested, withoutScopeSources } from '../access/attest-sources.ts';import { ACTOR_ARGUMENT, ACTOR_HEADER } from '../access/actor-model.ts';+import { folderAccessFor } from '../access/caller.ts';import { uuidV5 } from '../persistence/preconditions.ts';import { isStrictIsoDateTime } from '../utils.ts';@@ -122,10 +123,10 @@ async function ownRecentFacts(ctx: OperationContext, sources: string[], read: Re/*** GST-1094: the caller record of a recall that names sources. It is read as the scope, never as the person: no member* identity, actor or audience, so no member-page fact is admitted and every restricted page is hidden, and privacy is- * never relaxed.+ * never relaxed. Folder coverage (pending prefixes, an unreadable rulebook) is read for the call, so it stays fail-closed.*/const SCOPE_READ_ACCESS: Partial<CallerAccess> = {- identity: [], audienceIdentities: [], audience: null, actor: null, satisfiedRuleIds: [], folderCoverage: [], privateReadableSources: [],+ identity: [], audienceIdentities: [], audience: null, actor: null, satisfiedRuleIds: [], privateReadableSources: [],hidePrivate: true, applyRemoteReadPolicy: false, restricted: true, remotePolicy: { privacyRelaxed: false, restricted: true },};@@ -181,7 +182,8 @@ const memory_recall: Operation = {const { query, maxChars } = recallArguments(p);const { sources, named } = await recallSources(ctx, p);if (!named || ctx.remote === false) return recallFacts(ctx, query, maxChars, sources, named);- return withCallerAccess(SCOPE_READ_ACCESS, () => recallFacts(ctx, query, maxChars, sources, true));+ const { folderCoverage } = await folderAccessFor(ctx.engine, []);+ return withCallerAccess({ ...SCOPE_READ_ACCESS, folderCoverage }, () => recallFacts(ctx, query, maxChars, sources, true));},};diff --git a/test/qm-memory-tools.test.ts b/test/qm-memory-tools.test.tsindex b81979318e..265366625d 100644--- a/test/qm-memory-tools.test.ts+++ b/test/qm-memory-tools.test.ts@@ -432,6 +432,30 @@ for (const kind of testBackends()) {expect(unlinked.structured).toEqual({ facts: [], truncated: false, code: 'actor_unlinked' });}, 120_000);+ test('a recall that names sources keeps folder fail-closed coverage: facts under a pending prefix stay hidden', async () => {+ expect(tokens.scoped).toBeDefined();+ const aliceScoped: Who = { token: 'scoped', actor: ALICE_ID };+ const held = 'pending-hr/scope-note';+ await importFromContent(engine, held, '---\ntype: note\ntitle: Held note\n---\n# Held note\n', { sourceId: SRC });+ await engine.insertFact({ fact: 'Payroll budget ledger draft canaryscopepending', kind: 'fact', entity_slug: ACME, source: 'test', visibility: 'world' }, { source_id: SRC });+ await engine.executeRaw('UPDATE facts SET source_markdown_slug = $1 WHERE fact LIKE $2', [held, '%canaryscopepending%']);+ __resetPrivateVisibilityCacheForTests();+ expect((await recall(aliceScoped, 'payroll budget ledger', { max_chars: 12_000, source_id: SRC })).text).toContain('canaryscopepending');+ await engine.executeRaw('INSERT INTO access_policy_state (source_id) VALUES ($1) ON CONFLICT (source_id) DO NOTHING', [SRC]);+ await engine.executeRaw("INSERT INTO access_pending_coverage (source_id, prefix, reason) VALUES ($1, 'pending-hr/', 'invalid')", [SRC]);+ try {+ __resetPrivateVisibilityCacheForTests();+ for (const args of [{ source_id: SRC }, { source_ids: [SRC] }]) {+ const asScope = await recall(aliceScoped, 'payroll budget ledger', { max_chars: 12_000, ...args });+ expect(asScope.isError).toBe(false);+ expect(asScope.all).not.toContain('canaryscopepending');+ }+ } finally {+ await engine.executeRaw("DELETE FROM access_pending_coverage WHERE source_id = $1 AND prefix = 'pending-hr/'", [SRC]);+ __resetPrivateVisibilityCacheForTests();+ }+ }, 120_000);+test('generic recall takes max_chars: whole facts in order, no marker, structured truncated, invalid values refused; without it recall is unchanged (HARN-15)', async () => {const ENTITY = 'companies/harn-recall-example';for (let i = 0; i < 12; i++) {diff --git a/test/qm-scope-writes.test.ts b/test/qm-scope-writes.test.tsindex 5112b84b88..25a8fcbff9 100644--- a/test/qm-scope-writes.test.ts+++ b/test/qm-scope-writes.test.ts@@ -37,7 +37,9 @@ import { rescopeLegacyToken } from '../src/core/grants/legacy-token.ts';import { isSourceFederated, loadAllSources, parseSourceConfig } from '../src/core/sources-load.ts';import { restoreSource, softDeleteSource } from '../src/core/destructive-guard.ts';import { proposeRevision } from '../src/core/access/policy.ts';+import { quarantineRestoredExecution } from '../src/core/backup/quarantine.ts';import type { AuthInfo } from '../src/core/ops/contract.ts';+import { attestRightFrom } from '../src/core/access/actor-model.ts';import { testBackends } from './helpers/test-backends.ts';import { managedBrain, settleManagedGitEffects } from './helpers/managed-brain.ts';import { durableGitRepo, git } from './helpers/git-publication.ts';@@ -228,6 +230,13 @@ for (const kind of testBackends()) {expect(await factRows('canaryrecheckafter')).toHaveLength(0);}, 60_000);+ test('restating --attest-sources re-marks a listed scope source that lost its mark', async () => {+ await engine.executeRaw(`UPDATE sources SET config = config - 'qm_scope' WHERE id = $1`, [SCOPE_B]);+ expect((await markedScopeSources(engine, [SCOPE_B])).has(SCOPE_B)).toBe(false);+ await bindCredential(engine, { kind: 'client', id: clients.scopes!.id }, { attestSources: [SCOPE_A, SCOPE_B] });+ expect((await markedScopeSources(engine, [SCOPE_A, SCOPE_B])).size).toBe(2);+ }, 60_000);+test('a squatted scope page takes no scope write; no other remote writer may write memory/scope', async () => {await importFromContent(engine, 'memory/scope', '---\ntype: note\ntitle: Scope memory\nreadable_by:\n - member:alice-example\n - member:bob-example\n---\n# Scope memory\n', { sourceId: SCOPE_B });const squat = await write(alice, [{ fact: 'Squatted scope canarysquatscope', provenance: 'x' }], { source_id: SCOPE_B });@@ -331,3 +340,31 @@ describe('scope writes (pglite, repo-backed)', () => {await claimWorktree(engine, 'scope-repo', scopeRoot);} }), 120_000);});++describe('attest right of a scope credential (pure)', () => {+ test('a credential listing scope sources runs only the scope operations, whatever its stored or default attest_ops say', () => {+ const scoped = attestRightFrom({ attest_actor: 'qm', attest_ops: ['memory_recall', 'recall', 'search', 'memory_write'], attest_sources: ['scope-a'] }, 'client_credentials', null);+ expect(scoped).toEqual({ system: 'qm', ops: ['memory_recall', 'memory_write'], sources: ['scope-a'] });+ const defaults = attestRightFrom({ attest_actor: 'qm', attest_ops: null, attest_sources: ['scope-a'] }, 'legacy', null);+ expect(defaults?.ops.every(op => ['forget', 'forget_fact', 'get_write_request', 'memory_recall', 'memory_write'].includes(op))).toBe(true);+ const plain = attestRightFrom({ attest_actor: 'qm', attest_ops: ['memory_recall', 'recall'], attest_sources: null }, 'legacy', null);+ expect(plain?.ops).toEqual(['memory_recall', 'recall']);+ });+});++describe('backup restore quarantine keeps the scope mark', () => {+ test('an external scope source detached by a restore stays a QM scope source', async () => {+ const engine = new PGLiteEngine();+ await engine.connect({ engine: 'pglite' } as never);+ await engine.initSchema();+ try {+ await engine.executeRaw(`INSERT INTO sources (id, name, config) VALUES ('scope-q', 'scope-q', '{"qm_scope": true, "federated": false, "remote_url": "https://git.example.com/scope.git"}'::jsonb)`);+ const rows = await engine.executeRaw<{ id: string; local_path: string | null }>('SELECT id, local_path FROM sources ORDER BY id');+ const out = await quarantineRestoredExecution(engine, { sources: rows.map(r => ({ id: r.id, local_path: r.local_path, managed_relative_path: null })), originalRoot: '/old', root: '/new', managedPaths: [], restoreId: 'r1' });+ expect(out.sources.map(s => s.id)).toContain('scope-q');+ expect((await markedScopeSources(engine, ['scope-q'])).has('scope-q')).toBe(true);+ } finally {+ await engine.disconnect();+ }+ }, 120_000);+});