Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
An owner put_page that leaves out labels keeps a page private by an alias, pinned question or private origin private (GST-2091)
garrytand68427c2571a
diff --git a/behavior-changes.d/owner_remote_private_writes_gst2091.md b/behavior-changes.d/owner_remote_private_writes_gst2091.mdindex e29d9a1a77..7f15e86cb8 100644--- a/behavior-changes.d/owner_remote_private_writes_gst2091.md+++ b/behavior-changes.d/owner_remote_private_writes_gst2091.md@@ -1 +1 @@-The brain owner's own remote agents can now change private pages. A credential bound to the brain owner (gbrain access owner set) that has write scope and speaks for itself (no audience, not an attested call, not a subagent job, no slug fence) may rewrite, edit, append to and relabel a page marked visibility: private that it already reads, through put_page, edit_page and append_page; before, these writes answered page_not_found. A put_page that leaves out visibility or readable_by keeps the stored ones, and the change is recorded under the owner's credential. Everyone else is refused exactly as before: other members, unbound tokens and OAuth clients, attested calls, the owner's credential asserting a shared room, the owner's credential without write scope, and any write to a page whose readable_by or folder rule leaves the owner out. delete_page on a private page stays refused. An owner who does not want an agent to change private pages gives that agent a credential without write scope (gbrain auth create <name> --member <owner> with read scope only), or binds it to its own member instead of the owner.+The brain owner's own remote agents can now change private pages. A credential bound to the brain owner (gbrain access owner set) that has write scope and speaks for itself (no audience, not an attested call, not a subagent job, no slug fence) may rewrite, edit, append to and relabel a page marked visibility: private that it already reads, through put_page, edit_page and append_page; before, these writes answered page_not_found. A put_page that leaves out visibility or readable_by keeps the stored ones (a page private only through a privacy alias, a pinned question or a private origin is saved as visibility: private), and the change is recorded under the owner's credential. Everyone else is refused exactly as before: other members, unbound tokens and OAuth clients, attested calls, the owner's credential asserting a shared room, the owner's credential without write scope, and any write to a page whose readable_by or folder rule leaves the owner out. delete_page on a private page stays refused. An owner who does not want an agent to change private pages gives that agent a credential without write scope (gbrain auth create <name> --member <owner> with read scope only), or binds it to its own member instead of the owner.diff --git a/changelog.d/capy-owner-remote-private-writes.md b/changelog.d/capy-owner-remote-private-writes.mdindex 8a99fc151d..834fe5b958 100644--- a/changelog.d/capy-owner-remote-private-writes.md+++ b/changelog.d/capy-owner-remote-private-writes.md@@ -2,6 +2,6 @@### Changed-- A credential bound to the brain owner, with write scope and speaking for itself, may change a `visibility: private` page it reads with `put_page`, `edit_page` or `append_page`, and relabel it (for example to `visibility: world`). A `put_page` that leaves out `visibility` or `readable_by` keeps the stored ones, and the page revision and the journaled request name the owner's credential. Other members, unbound tokens and OAuth clients, attested calls, the owner's credential without write scope and writes to pages whose readers or folder rule leave the owner out get `page_not_found` (or their earlier refusal) as before, on repo-less and repo brains alike.+- A credential bound to the brain owner, with write scope and speaking for itself, may change a `visibility: private` page it reads with `put_page`, `edit_page` or `append_page`, and relabel it (for example to `visibility: world`). A `put_page` that leaves out `visibility` or `readable_by` keeps the stored ones (a page private only through a privacy alias, a pinned question or a private origin is saved as `visibility: private`), and the page revision and the journaled request name the owner's credential. Other members, unbound tokens and OAuth clients, attested calls, the owner's credential without write scope and writes to pages whose readers or folder rule leave the owner out get `page_not_found` (or their earlier refusal) as before, on repo-less and repo brains alike.<!-- behavior-change: owner_remote_private_writes_gst2091 -->diff --git a/docs/guides/access-designations.md b/docs/guides/access-designations.mdindex 0c7bd7df67..698f61c950 100644--- a/docs/guides/access-designations.md+++ b/docs/guides/access-designations.md@@ -188,7 +188,8 @@ attested call. `gbrain access owner clear` turns it off.**The owner's writes.** The owner's own credential with write scope, speaking for itself (no audience, not anattested call, not a subagent job, no slug fence), may change a private page it reads with `put_page`, `edit_page` or`append_page`, and relabel it (for example `visibility: world`); a `put_page` that leaves out `visibility` or-`readable_by` keeps the stored ones, and the revision and the journaled request (`ownerWrite`) name the owner's+`readable_by` keeps the stored ones (a page private only by a privacy alias, a pinned question, a derived type or a+private origin is stored as `visibility: private`), and the revision and the journaled request (`ownerWrite`) name the owner'scredential. Readers lists and folder rules that leave the owner out still refuse it, and every other caller is refusedas before; to keep an owner agent from changing private pages, give it a credential without write scope.diff --git a/src/core/persistence/page-prepare.ts b/src/core/persistence/page-prepare.tsindex b914059562..06f737c635 100644--- a/src/core/persistence/page-prepare.ts+++ b/src/core/persistence/page-prepare.ts@@ -38,7 +38,7 @@ import { recordedPathFromFileUri, scannerSlugRootMode, scannerSourcePath } fromimport { engineMutationPrecondition, parseMutationPrecondition } from './preconditions.ts';import { assertPurgeParams } from './purge-params.ts';import { authorizeWrite, slugDisclosable } from './authority.ts';-import { remoteLabelsChecked, defaultSourceMemberWriter, defaultSourceWriterIsOwner, defaultSourceWriterReaders, storedOwnerWrite } from './page-visibility.ts';+import { remoteLabelsChecked, defaultSourceMemberWriter, defaultSourceWriterIsOwner, defaultSourceWriterReaders, ownerRewriteOf } from './page-visibility.ts';import { findLiveFileClaimants, type FileClaimant, type FileClaimTarget } from './file-claims.ts';import { digest, sha256 } from './digest.ts';import { replaceableFile } from '../facts/file-body-guard.ts';@@ -1125,7 +1125,7 @@ async function prepareInlineOrSegmentedMutation(unbounded: BrainEngine, row: Wri// GST-2518: a lower-tier local write keeps the page's privacy labels it would widen (GST-2521: and the owner as a reader); its proposal carries the request.const labelGuard = await privacyLabelGuard(engine, row, trust, snapshot?.page ?? null, storedTier, incoming);let heldLabels: HeldPrivacyLabels | null = null;- const defaultSourceWriter = row.operation === 'restore_page' || row.operation === 'revert_version' ? null : await defaultSourceMemberWriter(engine, row.authority), defaultSourceOwner = await defaultSourceWriterIsOwner(engine, defaultSourceWriter), ownerRewrite = row.operation === 'put_page' && await storedOwnerWrite(engine, row.authority);+ const defaultSourceWriter = row.operation === 'restore_page' || row.operation === 'revert_version' ? null : await defaultSourceMemberWriter(engine, row.authority), defaultSourceOwner = await defaultSourceWriterIsOwner(engine, defaultSourceWriter), ownerRewrite = row.operation === 'put_page' ? await ownerRewriteOf(engine, row.authority, row.source_id, row.slug) : null;// The apply diet: the publisher's locked read of row.slug is the import base, and one read after the// last page write serves the read-back check, the seal, the core notice and the receipt (as sync does).const lean = options.coordinated === true && !targetDeleted && row.operation !== 'restore_page' && !versionTags;diff --git a/src/core/persistence/page-visibility.ts b/src/core/persistence/page-visibility.tsindex 2b3701382c..5e117df309 100644--- a/src/core/persistence/page-visibility.ts+++ b/src/core/persistence/page-visibility.ts@@ -1,5 +1,5 @@import { OperationError, opError } from '../ops/contract.ts';-import { callerAccessAllowedSql, callerIdentities, currentCallerAccess, labelledOnlyForSql, OWNER_MEMBER_KEY, privatePagesFilterForCaller, readPolicyFragment, readRemotePolicyNow, resolveExcludePrivatePages, withCallerAccess } from '../search/private-visibility.ts';+import { callerAccessAllowedSql, callerIdentities, currentCallerAccess, labelledOnlyForSql, OWNER_MEMBER_KEY, privacyPartSql, privatePagesFilterForCaller, readPolicyFragment, readRemotePolicyNow, resolveExcludePrivatePages, withCallerAccess } from '../search/private-visibility.ts';import { anyLabelledOnlyMember, labelledOnlyFor } from '../access/labelled-only.ts';import { hostFix } from '../ops/op-fix.ts';import { parseMarkdown } from '../markdown.ts';@@ -308,16 +308,28 @@ export async function storedOwnerWrite(engine: SqlEngine, authority: WriteAuthorreturn !!authority.ownerWrite && hasScope(authority.scopes, 'write') && await storedOwnerSpeaksForItself(engine, authority, authority.ownerWrite);}+/**+ * GST-2091: for a whole-page rewrite that is the owner's own (storedOwnerWrite), whether the live stored page is private by+ * the privacy rules remote reads apply (privacyPartSql: visibility, privacy aliases, pinned questions, derived types and+ * lineage); null when the write is not the owner's. ownerWriteKeepsLabels keeps such a page private.+ */+export async function ownerRewriteOf(engine: SqlEngine, authority: WriteAuthority, sourceId: string, slug: string): Promise<{ privacyPrivate: boolean } | null> {+ if (!await storedOwnerWrite(engine, authority)) return null;+ const [row] = await engine.executeRaw<{ hidden: boolean }>(`SELECT NOT (${privacyPartSql('p')}) AS hidden FROM pages p+ WHERE p.source_id = $1 AND p.slug = $2 AND p.deleted_at IS NULL`, [sourceId, slug]);+ return { privacyPrivate: row?.hidden === true };+}+/*** GST-2051: the readers a page written by a default-source member writer (defaultSourceMemberWriter) keeps when its content* names none: the stored page's readers when it has them, else the writer alone for a page this write creates, unless* the writer is the brain owner (GST-2059), whose new pages stay shared. An existing page with no readers stays as it- * is. Only ever narrows: a page naming readers is left to the caller. `ownerRewrite` (GST-2091): first keep the stored labels the- * owner's own whole-page rewrite leaves out (ownerWriteKeepsLabels).+ * is. Only ever narrows: a page naming readers is left to the caller. `ownerRewrite` (GST-2091, ownerRewriteOf): first keep the+ * stored labels the owner's own whole-page rewrite leaves out (ownerWriteKeepsLabels).*/export function defaultSourceWriterReaders(writer: string | null, frontmatter: Record<string, unknown>, stored: { frontmatter?: unknown; deleted_at?: unknown } | null | undefined, owner = false,- ownerRewrite = false): void {- if (ownerRewrite) ownerWriteKeepsLabels(frontmatter, stored);+ ownerRewrite: { privacyPrivate: boolean } | null = null): void {+ if (ownerRewrite) ownerWriteKeepsLabels(frontmatter, stored, ownerRewrite.privacyPrivate);if (!writer || hasReaders(frontmatter)) return;const live = stored && stored.deleted_at == null ? stored : null;if (live && hasReaders(live.frontmatter)) frontmatter.readable_by = (live.frontmatter as Record<string, unknown>).readable_by;@@ -327,11 +339,16 @@ export function defaultSourceWriterReaders(writer: string | null, frontmatter: R/*** GST-2091: a whole-page rewrite (put_page) by the brain owner's own credential (storedOwnerWrite) keeps the live stored* page's visibility and readable_by when its content leaves them out; a value the content states replaces the stored one.+ * A page private by another privacy rule (`privacyPrivate`: an alias, a pinned question, a derived type or a private+ * origin) becomes `visibility: private` when the content states no visibility, so dropping that key never publishes it.*/-function ownerWriteKeepsLabels(frontmatter: Record<string, unknown>, stored: { frontmatter?: unknown; deleted_at?: unknown } | null | undefined): void {+function ownerWriteKeepsLabels(frontmatter: Record<string, unknown>, stored: { frontmatter?: unknown; deleted_at?: unknown } | null | undefined, privacyPrivate: boolean): void {const live = stored && stored.deleted_at == null && typeof stored.frontmatter === 'object' && stored.frontmatter !== null ? stored.frontmatter as Record<string, unknown> : null;if (!live) return;- if (!Object.prototype.hasOwnProperty.call(frontmatter, 'visibility') && live.visibility !== undefined) frontmatter.visibility = live.visibility;+ if (!Object.prototype.hasOwnProperty.call(frontmatter, 'visibility')) {+ if (privacyPrivate) frontmatter.visibility = 'private';+ else if (live.visibility !== undefined) frontmatter.visibility = live.visibility;+ }if (!hasReaders(frontmatter) && hasReaders(live)) frontmatter.readable_by = live.readable_by;}diff --git a/src/core/persistence/upload-streamed.ts b/src/core/persistence/upload-streamed.tsindex 02679a5d56..2b3aec4867 100644--- a/src/core/persistence/upload-streamed.ts+++ b/src/core/persistence/upload-streamed.ts@@ -41,7 +41,7 @@ import { stagedUploadParts, stagedUploadWork, type StagingBodyRef } from './pageimport type { PreparedMutation } from './coordinator.ts';import type { WriteRequest } from './model.ts';import { assertPrivateRequestKept, privateRequestGuarded, readPrivateRequest } from './visibility-request.ts';-import { assertLabelledOnlyCreate, defaultSourceMemberWriter, defaultSourceWriterIsOwner, defaultSourceWriterReaders, storedOwnerWrite } from './page-visibility.ts';+import { assertLabelledOnlyCreate, defaultSourceMemberWriter, defaultSourceWriterIsOwner, defaultSourceWriterReaders, ownerRewriteOf } from './page-visibility.ts';const text = (value: unknown) => (typeof value === 'string' ? value : null);@@ -95,7 +95,7 @@ export async function prepareStreamedUpload(engine: BrainEngine, row: WriteRequeconst storedTier = remote && snapshot ? await storedPageTier(engine, row.source_id, row.slug) : null;const defaultSourceWriter = await defaultSourceMemberWriter(engine, row.authority);const defaultSourceOwner = await defaultSourceWriterIsOwner(engine, defaultSourceWriter);- const ownerRewrite = await storedOwnerWrite(engine, row.authority);+ const ownerRewrite = await ownerRewriteOf(engine, row.authority, row.source_id, row.slug);let trust: WriteTrust | undefined;const dir = await mkdtemp(join(tmpdir(), 'gbrain-upload-'));let ready: StreamedPreparedImport | undefined;diff --git a/test/owner-remote-private-writes-gst2091.test.ts b/test/owner-remote-private-writes-gst2091.test.tsindex c8cffc9288..f2b857a412 100644--- a/test/owner-remote-private-writes-gst2091.test.ts+++ b/test/owner-remote-private-writes-gst2091.test.ts@@ -200,6 +200,26 @@ for (const kind of testBackends()) {expect((await h.call('bob', 'get_page', { slug: derived })).text).not.toContain(page.canary);}, 120_000);+ const privateWithoutVisibility: Array<[string, (t: string, origin: string) => string[], string]> = [+ ['a privacy alias', () => ['access: confidential'], 'note'],+ ['a pinned_question marker', () => ['pinned_question: true'], 'note'],+ ['derived_from a private page', (_t, origin) => [`derived_from: [${origin}]`], 'note'],+ ['a derived atom type', () => [], 'atom'],+ ];+ for (const [label, lines, type] of privateWithoutVisibility) {+ test(`an owner put_page that leaves out labels keeps a page private by ${label} private`, async () => {+ const origin = (await h.seed('private')).slug;+ const t = tag();+ const page = { slug: `notes/implicit-${t}`, canary: `gst2091implicit${t}`, title: `Note ${t}` };+ await importFromContent(engine, page.slug, pageText(page.title, `First line ${page.canary}.`, lines(t, origin)).replace('type: note', `type: ${type}`), { sourceId: SRC });+ expect((await h.call('bob', 'get_page', { slug: page.slug })).text).not.toContain(page.canary);+ const out = await h.write('owner', 'put_page', page);+ expect({ isError: out.isError, text: out.isError ? out.text : '' }).toEqual({ isError: false, text: '' });+ expect((await h.row(page.slug)).body).toContain(`Owner change ${page.canary}.`);+ for (const member of ['alice', 'bob']) expect((await h.call(member, 'get_page', { slug: page.slug })).text).not.toContain(page.canary);+ }, 120_000);+ }+test('the owner still cannot delete a private page remotely: delete_page answers as before', async () => {const page = await h.seed('private');const before = await h.row(page.slug);