Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(privacy): let the YAML parse decide a private request, scope the guard to content writes, keep ..-prefixed corpus files (#7841, #7805)

garrytancc6a72685043
diff --git a/src/core/cycle/synthesize-source-routing.ts b/src/core/cycle/synthesize-source-routing.ts
index 8041303fec..fda0d81f57 100644
--- a/src/core/cycle/synthesize-source-routing.ts
+++ b/src/core/cycle/synthesize-source-routing.ts
@@ -14,7 +14,7 @@
* transcripts dir, an `--input` file elsewhere) and imported conversation pages
* (read from the cycle's source) are not session captures and pass unchanged.
*/
-import { basename, dirname, isAbsolute, relative, resolve } from 'node:path';
+import { basename, dirname, isAbsolute, relative, resolve, sep } from 'node:path';
import type { BrainEngine } from '../engine.ts';
import type { PhaseResult } from '../cycle.ts';
import { corpusSpoolDir } from '../context/corpus-segments.ts';
@@ -34,7 +34,7 @@ function corpusLocation(filePath: string, corpusDir: string): 'spool' | 'legacy'
const root = resolve(corpusDir);
const file = resolve(filePath);
const rel = relative(root, file);
- if (!rel || rel.startsWith('..') || isAbsolute(rel)) return null;
+ if (!rel || rel === '..' || rel.startsWith('..' + sep) || isAbsolute(rel)) return null;
return dirname(file) === resolve(corpusSpoolDir(root)) ? 'spool' : 'legacy';
}
diff --git a/src/core/persistence/page-prepare.ts b/src/core/persistence/page-prepare.ts
index 41a5cbeb68..d6263890f2 100644
--- a/src/core/persistence/page-prepare.ts
+++ b/src/core/persistence/page-prepare.ts
@@ -833,7 +833,7 @@ async function prepareInlineOrSegmentedMutation(unbounded: BrainEngine, row: Wri
const { timeline, materialized } = await materializeTimeline(engine,parsed,row.slug,snapshot,writer,timelinePolicy);
if (materialized) content = serializePageToMarkdown({...snapshot.page,...parsed,timeline,type:parsed.typeExplicit ? parsed.type : snapshot.page.type},parsed.tags);
}
- const privateRequest = typeof content === 'string' ? readPrivateRequest(content, () => parseMarkdown(content as string, row.slug, { activePack }).frontmatter) : null;
+ const privateRequest = typeof content === 'string' && (row.operation === 'put_page' || row.operation === 'edit_page' || row.operation === 'capture') && p.kind !== 'managed_maintenance_page' ? readPrivateRequest(content, () => parseMarkdown(content as string, row.slug, { activePack }).frontmatter) : null;
if (privateRequest) assertPrivateRequestKept(row, privateRequest.frontmatter);
const storedTier = snapshot ? await storedPageTier(engine, row.source_id, row.slug) : null;
// Detect an exact canonical no-op before ingestion can invoke any provider.
diff --git a/src/core/persistence/visibility-request.ts b/src/core/persistence/visibility-request.ts
index 1a68d8d9c7..d0fa5b778e 100644
--- a/src/core/persistence/visibility-request.ts
+++ b/src/core/persistence/visibility-request.ts
@@ -39,14 +39,14 @@ function visibilityValues(frontmatter: Record<string, unknown> | null | undefine
/**
* The private request `content` carries, with the frontmatter it parses to, or null.
- * A visibility line asks for private when its own text says so or when the parse
- * reads it as private (a value on the next line, a block scalar, a flow list), so a
- * spelling the line scan cannot read is still seen. Parsing waits for a visibility key.
+ * The content asks for private when a visibility line says so or when the parse
+ * reads a visibility key (any case) as private, so a spelling the line scan cannot
+ * read is still seen: a value on the next line, a block scalar, a list, a flow
+ * mapping, an explicit or merged key. The parse decides; the line scan is only the
+ * fallback when the parse throws.
*/
export function readPrivateRequest(content: string, parse: () => Record<string, unknown> | null | undefined): { frontmatter: Record<string, unknown> | null | undefined } | null {
- const declared = declaredVisibilities(content);
- if (!declared.length) return null;
- const inline = declared.includes('private');
+ const inline = declaredVisibilities(content).includes('private');
let frontmatter: Record<string, unknown> | null | undefined;
try { frontmatter = parse(); } catch (error) { if (inline) throw error; return null; }
return inline || visibilityValues(frontmatter).includes('private') ? { frontmatter } : null;
diff --git a/test/put-visibility-private-7841.test.ts b/test/put-visibility-private-7841.test.ts
index 59705d82c1..cca5d7df7b 100644
--- a/test/put-visibility-private-7841.test.ts
+++ b/test/put-visibility-private-7841.test.ts
@@ -18,6 +18,7 @@ import { join } from 'node:path';
import type { BrainEngine } from '../src/core/engine.ts';
import type { OperationContext } from '../src/core/operations.ts';
import { operations } from '../src/core/operations.ts';
+import { importFromContent } from '../src/core/import-file.ts';
import { PGLiteEngine } from '../src/core/pglite-engine.ts';
import { claimWorktree } from '../src/core/persistence/ownership.ts';
import { disposePersistenceConsumer } from '../src/core/persistence/service.ts';
@@ -118,7 +119,8 @@ for (const managed of [false, true]) describe(`${managed ? 'managed brain with a
}, 120_000);
for (const [label, extra] of [['visibility: Private', 'visibility: Private\n'], ['Visibility: private', 'Visibility: private\n'], ['VISIBILITY: "PRIVATE"', 'VISIBILITY: "PRIVATE"\n'],
- ['visibility with Private on the next line', 'visibility:\n Private\n'], ['visibility as a block scalar', 'visibility: |\n private\n'], ['visibility as a flow list', 'visibility: [private]\n']] as const) {
+ ['visibility with Private on the next line', 'visibility:\n Private\n'], ['visibility as a block scalar', 'visibility: |\n private\n'], ['visibility as a flow list', 'visibility: [private]\n'],
+ ['an explicit key', '? visibility\n: Private\n'], ['a merge key', 'base: &base\n Visibility: Private\n<<: *base\n']] as const) {
test(`${label} on an existing world page is refused noncanonical_value, and the page and its revision are unchanged`, async () => {
await brain(managed, async (ctx, engine) => {
await put(ctx, 'notes/example', page());
@@ -133,6 +135,16 @@ for (const managed of [false, true]) describe(`${managed ? 'managed brain with a
}, 120_000);
}
+ test('a flow-mapping frontmatter that spells Visibility: Private is refused, and the page is unchanged', async () => {
+ await brain(managed, async (ctx, engine) => {
+ await put(ctx, 'notes/example', page());
+ const before = (await snapshot(engine, ctx, 'notes/example'))!;
+ const result = await put(ctx, 'notes/example', '---\n{title: Example, type: note, Visibility: Private}\n---\nA page about acme-example.\n', before.revision);
+ expect(result.thrown).toBe('visibility_not_applied');
+ expect((await snapshot(engine, ctx, 'notes/example'))!.revision).toBe(before.revision);
+ });
+ }, 120_000);
+
test('a quoted key with a trailing comment still publishes private, with CRLF line endings too', async () => {
await brain(managed, async (ctx, engine) => {
for (const [slug, content] of [['notes/quoted', page('"visibility": private # keep\n')], ['notes/crlf', page('visibility: private\n').replace(/\n/g, '\r\n')]] as const) {
@@ -171,3 +183,17 @@ for (const managed of [false, true]) describe(`${managed ? 'managed brain with a
});
}, 120_000);
});
+
+test('a derived write (add_tag) on a page that already stores Visibility: Private is not refused: the guard governs authored content only', async () => {
+ await brain(false, async (ctx, engine) => {
+ const [{ local_path: root }] = await engine.executeRaw<{ local_path: string }>('SELECT local_path FROM sources WHERE id=$1', [ctx.sourceId]);
+ mkdirSync(join(root, 'notes'));
+ writeFileSync(join(root, 'notes/legacy.md'), page('Visibility: Private\n'));
+ await importFromContent(engine, 'notes/legacy', page('Visibility: Private\n'), { sourceId: ctx.sourceId, noEmbed: true });
+ const addTag = operations.find(op => op.name === 'add_tag')!;
+ const result = await (addTag.handler(ctx, { slug: 'notes/legacy', tag: 'alice-example', request_id: randomUUID(), wait_ms: 25_000 }) as Promise<Outcome>)
+ .catch((e: { code?: string }) => ({ thrown: e.code }));
+ expect(result.thrown).toBeUndefined();
+ expect((await engine.getTags('notes/legacy', { sourceId: ctx.sourceId })).includes('alice-example')).toBe(true);
+ });
+}, 120_000);
diff --git a/test/synthesize-session-source-7805.test.ts b/test/synthesize-session-source-7805.test.ts
index 3bef42b5d7..4fe1d3cf0f 100644
--- a/test/synthesize-session-source-7805.test.ts
+++ b/test/synthesize-session-source-7805.test.ts
@@ -188,6 +188,15 @@ for (const managed of [false, true]) describe(`${managed ? 'managed' : 'unmanage
});
}, 120_000);
+ test('a top-level corpus file whose name starts with two dots is a corpus file, held like any legacy file', async () => {
+ await fixture(managed, async ({ engine, cycle, other, corpus }) => {
+ writeTranscript(join(corpus, '..stray.txt'), 'the office move');
+ const result = await runPhaseSynthesize(engine, { brainDir: cycle.root, sourceId: cycle.id, dryRun: false });
+ expect(result.details.source_routing).toMatchObject({ held: { corpus_legacy_unstamped: 1 } });
+ expect(await synthesizedSources(engine, { cycle, other })).toEqual({});
+ });
+ }, 120_000);
+
test('an --input file outside the corpus is kept for the cycle source, in no routing bucket', async () => {
await fixture(managed, async ({ engine, cycle, other, corpus }) => {
const outside = join(dirname(corpus), 'meeting-notes.txt');