Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(trust): keep a synced quarantine_override only on an owner-tier write gate (CSO T1, W13 P1.13)
time-attack8b7493b53c5d
diff --git a/docs/architecture/key-files/quarantine.md b/docs/architecture/key-files/quarantine.mdindex b3ccc94b49..d6470eccab 100644--- a/docs/architecture/key-files/quarantine.md+++ b/docs/architecture/key-files/quarantine.md@@ -37,5 +37,5 @@ gets no body (fetch: empty `text`) unless it holds `admin` and passes`include_quarantined: true`.Test: `test/quarantine-read-write-surface.test.ts`.-- `src/core/quarantine-override.ts` — #6259 (fix wave 12): the `quarantine_override` frontmatter key `gbrain quarantine clear --force` records: `{ binding, cleared_at }`, where `binding` is a sha256 over the classifier's inputs (title, type, body) as the gate canonicalizes them (`quarantineOverrideFor`). `assessImportSanity` (`import-screen.ts`) wraps its verdict in `withQuarantineOverride`, which removes the junk-pattern, literal and markup-flag outcomes while the binding is current (size outcomes stay). `importFromContent` calls `stripGateOwnedMarkers` (`import-screen.ts`, `GATE_OWNED_FRONTMATTER_KEYS`): every writer loses `quarantine`, `content_flag`, `embed_skip`, `atoms_scan_hash` and `quarantine_override` unless an owner-tier path passes `preserveGateMarkers: true` (`importFromFile`, managed sync and file import, reindex, file repair, reconcile, the cycle derivers and their `managed_maintenance_page` intents, `quarantine clear/scan` and the owner-internal `managed_quarantine_clear` put_page kind); local put_page, connectors and ingest lanes do not. A preserving path keeps its own override only while current, and a current override drops classifier markers the content still carries. `opts.remote` keeps only its fence-merge/hidden-row meaning. Tests: `test/gate-marker-strip.test.ts`. For a stripped write the classifier would hide or markup-flag, `carryStoredQuarantineOverride` reads the stored override and keeps it when it still binds (so a remote tag edit does not re-hide the page); a clean remote write reads nothing extra (#6007 statement budget). Company-brain inspection refuses files carrying it (`hidden_input`). Doctor's quarantine row names `clear <slug> --force` and `content_sanity.disabled_patterns`.+- `src/core/quarantine-override.ts` — #6259 (fix wave 12): the `quarantine_override` frontmatter key `gbrain quarantine clear --force` records: `{ binding, cleared_at }`, where `binding` is a sha256 over the classifier's inputs (title, type, body) as the gate canonicalizes them (`quarantineOverrideFor`). `assessImportSanity` (`import-screen.ts`) wraps its verdict in `withQuarantineOverride`, which removes the junk-pattern, literal and markup-flag outcomes while the binding is current (size outcomes stay). `importFromContent` calls `stripGateOwnedMarkers` (`import-screen.ts`, `GATE_OWNED_FRONTMATTER_KEYS`): every writer loses `quarantine`, `content_flag`, `embed_skip`, `atoms_scan_hash` and `quarantine_override` unless an owner-tier path passes `preserveGateMarkers: true` (`importFromFile`, managed sync and file import, reindex, file repair, reconcile, the cycle derivers and their `managed_maintenance_page` intents, `quarantine clear/scan` and the owner-internal `managed_quarantine_clear` put_page kind); local put_page, connectors and ingest lanes do not. A preserving path keeps its own override only while current, and a current override drops classifier markers the content still carries. CSO T1 (fix wave 13): the binding is a public hash, so a preserving call that carries a `writeGate` (unmanaged sync/import via `ownerSourceGateInput`, managed sync and file import via `ownerGateInput`) keeps the override only when the gate's tier is owner-tier (`OWNER_TIER_FLOOR`); a lowered, mirror or connector source, or a remote authority, loses it and the gate decides; an unreadable tier fails closed. Paths that pass no gate (file repair, reconcile, the cycle derivers, `quarantine clear`) are unchanged. Tests: `test/quarantine-override-tier.test.ts`. `opts.remote` keeps only its fence-merge/hidden-row meaning. Tests: `test/gate-marker-strip.test.ts`. For a stripped write the classifier would hide or markup-flag, `carryStoredQuarantineOverride` reads the stored override and keeps it when it still binds (so a remote tag edit does not re-hide the page); a clean remote write reads nothing extra (#6007 statement budget). Company-brain inspection refuses files carrying it (`hidden_input`). Doctor's quarantine row names `clear <slug> --force` and `content_sanity.disabled_patterns`.- `src/core/ops/get-page-projection.ts` — `projectGetPage` shapes the get_page response from the reader-visible body (#2225 `content`, content_only round-trip fields, `timeline_entries`, `file_held`). #6259: `quarantinedView` decides whether a quarantined page's body is withheld (`body_omitted` for an untrusted reader without `admin` + `include_quarantined`); a withheld body empties compiled_truth and timeline and drops `content`.diff --git a/src/core/import-screen.ts b/src/core/import-screen.tsindex f2e5cb51ab..7f89d9db8d 100644--- a/src/core/import-screen.ts+++ b/src/core/import-screen.ts@@ -18,6 +18,7 @@ import { buildEmbedSkipMarker, EMBED_SKIP_KEY } from './embed-skip.ts';import { buildContentFlagMarker, buildQuarantineMarker, CONTENT_FLAG_KEY, QUARANTINE_KEY } from './quarantine.ts';import { ATOMS_SCAN_HASH_KEY } from './utils.ts';import { applyTrustAllowRules } from './trust/allow-rules.ts';+import { compareTrust, isTrustTier, OWNER_TIER_FLOOR } from './trust/tier.ts';import {assessPageForGate, DEFAULT_WRITE_GATE_CONFIG, parseWriteGateConfig, writeGateDetail, writeGateRejectedError,type WriteGateAssessment, type WriteGateConfig, type WriteGateInput,@@ -278,13 +279,20 @@ export const GATE_OWNED_FRONTMATTER_KEYS = [QUARANTINE_KEY, CONTENT_FLAG_KEY, EM* mining (`atoms_scan_hash`) or forge a cleared state (`quarantine_override`).* A preserving path keeps its own override only while it binds the content,* and a current override drops classifier markers the content still carries.+ * The override's binding is a public hash anyone can compute, so a preserving+ * file import that carries a write gate (owner sync and file import, managed+ * sync and import) keeps it only when that gate's tier is owner-tier; a+ * mirror, connector or lowered source can't clear its own page (CSO T1). An+ * unreadable tier fails closed.*/-export function stripGateOwnedMarkers(parsed: Pick<ParsedMarkdown, 'frontmatter' | 'title' | 'type' | 'compiled_truth' | 'timeline'>, opts: { preserveGateMarkers?: boolean }): void {+export function stripGateOwnedMarkers(parsed: Pick<ParsedMarkdown, 'frontmatter' | 'title' | 'type' | 'compiled_truth' | 'timeline'>,+ opts: { preserveGateMarkers?: boolean; writeGate?: { tier?: unknown } }): void {if (opts.preserveGateMarkers !== true) {for (const key of GATE_OWNED_FRONTMATTER_KEYS) delete parsed.frontmatter[key];return;}- if (Object.hasOwn(parsed.frontmatter, QUARANTINE_OVERRIDE_KEY) && !hasCurrentQuarantineOverride(parsed)) delete parsed.frontmatter[QUARANTINE_OVERRIDE_KEY];+ const ownerGate = !opts.writeGate || isTrustTier(opts.writeGate.tier) && compareTrust(opts.writeGate.tier, OWNER_TIER_FLOOR) >= 0;+ if (Object.hasOwn(parsed.frontmatter, QUARANTINE_OVERRIDE_KEY) && (!ownerGate || !hasCurrentQuarantineOverride(parsed))) delete parsed.frontmatter[QUARANTINE_OVERRIDE_KEY];dropClassifierMarkers(parsed as ParsedMarkdown);}diff --git a/test/quarantine-override-tier.test.ts b/test/quarantine-override-tier.test.tsnew file mode 100644index 0000000000..c6939c685d--- /dev/null+++ b/test/quarantine-override-tier.test.ts@@ -0,0 +1,80 @@+/**+ * CSO T1 (wave 13, Tier A): `quarantine_override` binds a public hash of title,+ * type and body, so a synced file's author can compute one for their own page.+ * Protects: a preserving import that carries a write gate keeps the override only+ * when the gate's tier is owner-tier. An unmanaged sync of an owner source keeps+ * it; a lowered (`trust_tier` below operator_curated) or connector source loses it+ * and the gate quarantines the page; the managed sync/import shape (preserve ++ * `ownerGateInput`) does the same, a remote authority included; an unreadable+ * tier fails closed (unit-level: the gate itself refuses such a tier). Internal owner paths that pass no gate are unchanged.+ * Seams: none; PGLite.+ */+import { afterAll, beforeAll, describe, expect, test } from 'bun:test';+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { PGLiteEngine } from '../src/core/pglite-engine.ts';+import { importFromContent, importFromFile } from '../src/core/import-file.ts';+import { stripGateOwnedMarkers } from '../src/core/import-screen.ts';+import { parseMarkdown } from '../src/core/markdown.ts';+import { QUARANTINE_OVERRIDE_KEY, quarantineOverrideFor } from '../src/core/quarantine-override.ts';+import { isQuarantined } from '../src/core/quarantine.ts';+import { ownerGateInput } from '../src/core/trust/channel.ts';+import { withEnv } from './helpers/with-env.ts';++const JUNK_BODY = 'Our scraper analysis: the headless browser stopped at a page that said "Cloudflare Ray ID: 8f2a" before any article text.';+const bare = (title: string) => `---\ntitle: ${title}\ntype: note\n---\n\n${JUNK_BODY}\n`;+function withOverride(title: string, path: string): string {+ const o = quarantineOverrideFor(bare(title), path);+ return `---\ntitle: ${title}\ntype: note\n${QUARANTINE_OVERRIDE_KEY}:\n binding: ${o.binding}\n cleared_at: '${o.cleared_at}'\n---\n\n${JUNK_BODY}\n`;+}++let engine: PGLiteEngine;+const home = mkdtempSync(join(tmpdir(), 'gbrain-override-tier-'));+const env = { GBRAIN_HOME: home, GBRAIN_AUDIT_DIR: home };+beforeAll(async () => {+ engine = new PGLiteEngine(); await engine.connect({}); await engine.initSchema();+ await engine.executeRaw(`INSERT INTO sources(id,name,config) VALUES ('mirror','mirror','{"trust_tier":"agent_written"}'::jsonb),+ ('gh','gh','{"kind":"github","gh_scope":"repos","gh_repos":"acme-example/app"}'::jsonb)`);+}, 120_000);+afterAll(async () => { await engine.disconnect(); rmSync(home, { recursive: true, force: true }); });++const page = async (slug: string, sourceId: string) => (await engine.getPage(slug, { sourceId }))!.frontmatter as Record<string, unknown>;++describe('unmanaged file sync', () => {+ for (const [sourceId, kept] of [['default', true], ['mirror', false], ['gh', false]] as const) {+ test(`${sourceId}: a synced file's own override is ${kept ? 'kept' : 'stripped and the gate decides'}`, () => withEnv(env, async () => {+ const dir = join(home, sourceId, 'notes'); mkdirSync(dir, { recursive: true });+ writeFileSync(join(dir, 'scraper.md'), withOverride('Scraper notes', 'notes/scraper.md'));+ await importFromFile(engine, join(dir, 'scraper.md'), 'notes/scraper.md', { noEmbed: true, sourceId });+ const fm = await page('notes/scraper', sourceId);+ expect({ sourceId, override: fm[QUARANTINE_OVERRIDE_KEY] !== undefined, quarantined: isQuarantined(fm) })+ .toEqual({ sourceId, override: kept, quarantined: !kept });+ }));+ }+});++describe('managed sync and import shape (preserveGateMarkers + ownerGateInput)', () => {+ const row = (source_id: string, remote: boolean) => ({ id: '00000000-0000-4000-8000-000000000013', source_id, operation: 'put_page' as const,+ authority: { remote } as never, intent: { kind: 'managed_sync_import' } as never });+ for (const [label, sourceId, remote, kept] of [['owner source', 'default', false, true], ['lowered source', 'mirror', false, false],+ ['connector source', 'gh', false, false], ['remote authority on the owner source', 'default', true, false]] as const) {+ test(`${label}: override ${kept ? 'kept' : 'stripped'}`, () => withEnv(env, async () => {+ const slug = `notes/managed-${label.replaceAll(' ', '-')}`;+ const content = withOverride(`Managed ${label}`, `${slug}.md`);+ const writeGate = await ownerGateInput(engine, row(sourceId, remote), null, `${slug}.md`);+ await importFromContent(engine, slug, content, { noEmbed: true, sourceId, preserveGateMarkers: true, writeGate });+ const fm = await page(slug, sourceId);+ expect({ override: fm[QUARANTINE_OVERRIDE_KEY] !== undefined, quarantined: isQuarantined(fm) }).toEqual({ override: kept, quarantined: !kept });+ }));+ }++ test('an unreadable gate tier fails closed; a preserving path with no gate keeps a current override', () => withEnv(env, async () => {+ const parsed = parseMarkdown(withOverride('Bad tier', 'notes/bad-tier.md'), 'notes/bad-tier.md', { validate: true });+ stripGateOwnedMarkers(parsed, { preserveGateMarkers: true, writeGate: { tier: 'not-a-tier' } });+ expect(parsed.frontmatter[QUARANTINE_OVERRIDE_KEY]).toBeUndefined();+ await importFromContent(engine, 'notes/no-gate', withOverride('No gate', 'notes/no-gate.md'), { noEmbed: true, preserveGateMarkers: true });+ const fm = await page('notes/no-gate', 'default');+ expect({ override: fm[QUARANTINE_OVERRIDE_KEY] !== undefined, quarantined: isQuarantined(fm) }).toEqual({ override: true, quarantined: false });+ }));+});