Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

Owner writes to private pages keep pinned questions, refuse soft-deleted pages and honor live slug fences (GST-2091)

garrytand6c5f0b27097
diff --git a/behavior-changes.d/owner_remote_private_writes_gst2091.md b/behavior-changes.d/owner_remote_private_writes_gst2091.md
index 7f15e86cb8..a197f7fd09 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 (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.
+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, and through put_pages and staged uploads (begin_page_upload and commit_page_upload), which write the same pages; before, these writes answered page_not_found. A put_page that leaves out visibility or readable_by keeps the stored ones, an empty visibility counts as left out, and a page private only through a privacy alias, a pinned question, a derived type or a private origin is saved as visibility: private (a pinned question keeps its marker) by any of these writes that states no visibility; 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, and any write to a soft-deleted private page, stays refused, and a credential fenced to slug prefixes, even after the write was submitted, gets no owner widening. 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.md
index 834fe5b958..bce1be2053 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 (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.
+- 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`, or with `put_pages` and staged uploads (`begin_page_upload` and `commit_page_upload`), and relabel it (for example to `visibility: world`). A `put_page` that leaves out `visibility` or `readable_by` keeps the stored ones, an empty `visibility` counts as left out, and a page private only through a privacy alias, a pinned question, a derived type or a private origin is saved as `visibility: private` (keeping its `pinned_question`) by any of these writes that states no visibility; 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, and so does any write to a soft-deleted private page and a credential fenced to slug prefixes. A staged upload over the single-string ceiling is now held to the same `readable_by` check as a smaller one.
<!-- behavior-change: owner_remote_private_writes_gst2091 -->
diff --git a/docs/guides/access-designations.md b/docs/guides/access-designations.md
index 698f61c950..87ac14c379 100644
--- a/docs/guides/access-designations.md
+++ b/docs/guides/access-designations.md
@@ -186,12 +186,15 @@ when it speaks for itself (no audience, or an audience of the owner alone); neve
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 an
-attested 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 (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's
-credential. Readers lists and folder rules that leave the owner out still refuse it, and every other caller is refused
-as before; to keep an owner agent from changing private pages, give it a credential without write scope.
+attested call, not a subagent job, no slug fence, checked again when the write is published), may change a private page
+it reads with `put_page`, `edit_page` or `append_page`, or with `put_pages` and staged uploads (`begin_page_upload`
+and `commit_page_upload`), which write the same pages, and relabel it (for example `visibility: world`). A `put_page`
+that leaves out `visibility` or `readable_by` keeps the stored ones, and an empty `visibility` counts as left out. A
+page private only by a privacy alias, a pinned question, a derived type or a private origin is stored as
+`visibility: private` by any of these writes that states no visibility, and a pinned question keeps its
+`pinned_question`. The revision and the journaled request (`ownerWrite`) name the owner's credential. Readers lists and
+folder rules that leave the owner out still refuse it, a soft-deleted private page stays refused, and every other caller
+is refused as before; to keep an owner agent from changing private pages, give it a credential without write scope.
**The owner opt-in.** `gbrain access owner reads-restricted on` (config `access.owner_reads_restricted`, off by
default and unchanged on upgrade) lets the owner's credentials read the pages that `readable_by` lists, folder rules
diff --git a/src/core/persistence/page-prepare.ts b/src/core/persistence/page-prepare.ts
index 06f737c635..367a346c9f 100644
--- a/src/core/persistence/page-prepare.ts
+++ b/src/core/persistence/page-prepare.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 ownerRewriteOf(engine, row.authority, row.source_id, row.slug) : null;
+ const defaultSourceWriter = row.operation === 'restore_page' || row.operation === 'revert_version' ? null : await defaultSourceMemberWriter(engine, row.authority), defaultSourceOwner = await defaultSourceWriterIsOwner(engine, defaultSourceWriter), ownerRewrite = ['put_page', 'edit_page', 'append_page'].includes(row.operation) ? await ownerRewriteOf(engine, row.authority, row.source_id, row.slug, row.operation === 'put_page') : 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.ts
index 5e117df309..62d0b33d32 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, privacyPartSql, privatePagesFilterForCaller, readPolicyFragment, readRemotePolicyNow, resolveExcludePrivatePages, withCallerAccess } from '../search/private-visibility.ts';
+import { callerAccessAllowedSql, callerIdentities, currentCallerAccess, labelledOnlyForSql, OWNER_MEMBER_KEY, privacyPartSql, privatePagesFilterForCaller, readHonorAliases, 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';
@@ -55,16 +55,19 @@ export async function authorizePageVisibility(engine: SqlEngine, authority: Writ
}
const anyLabelled = excludePrivate ? probe?.any_labelled ?? true : await anyLabelledOnlyMember(engine);
const labelled = anyLabelled && await writerLabelledOnly(engine, authority, await writerIdentities(engine, authority));
- // GST-2091: the owner's own page write (storedOwnerWrite) is held to member access without the privacy part. It is
- // judged only for a page the privacy part hides, so every other write costs no extra lookup.
+ // GST-2091: the owner's own page write (storedOwnerWrite) to a live page is held to member access without the privacy
+ // part. It is judged only for a page the privacy part hides, so every other write costs no extra lookup; a soft-deleted
+ // private page stays refused as before, so a write there never republishes it or opens its version history.
let owner: boolean | undefined;
+ const ownerOnLivePage = async () => await storedOwnerWrite(engine, authority)
+ && (await engine.executeRaw('SELECT 1 FROM pages WHERE source_id=$1 AND slug=$2 AND deleted_at IS NULL LIMIT 1', [authority.sourceId, slug])).length > 0;
if (!labelled && excludePrivate) {
// Privacy is hidden whatever the policy says, and an empty identity is the strictest reading (an identity only
// opens a page that names readers or sits under a folder rule), so a page visible with none needs no policy read
// or member lookup.
probe ??= (await engine.executeRaw<{ identity_may_open: boolean | null }>(`SELECT (${hiddenSql}) AS identity_may_open`, [authority.sourceId, slug]))[0];
if (probe?.identity_may_open == null) return;
- if (!probe.identity_may_open && !(owner = await storedOwnerWrite(engine, authority))) throw pageNotVisibleError();
+ if (!probe.identity_may_open && !(owner = await ownerOnLivePage())) throw pageNotVisibleError();
}
const policy = await readRemotePolicyNow(engine);
const hidePrivate = excludePrivate || !policy.privacyRelaxed;
@@ -72,7 +75,7 @@ export async function authorizePageVisibility(engine: SqlEngine, authority: Writ
const hiddenUnder = async (hide: boolean) => (await engine.executeRaw(`SELECT 1 FROM pages WHERE source_id=$1 AND slug=$2
AND NOT (${await writerReadPolicySql(engine, authority, 'pages', hide, policy.restricted)}) LIMIT 1`, [authority.sourceId, slug])).length > 0;
if (!await hiddenUnder(hidePrivate && owner !== true)) return;
- if (hidePrivate && owner === undefined && await storedOwnerWrite(engine, authority) && !await hiddenUnder(false)) return;
+ if (hidePrivate && owner === undefined && await ownerOnLivePage() && !await hiddenUnder(false)) return;
throw pageNotVisibleError();
}
@@ -290,11 +293,16 @@ export async function defaultSourceWriterIsOwner(engine: SqlEngine, writer: stri
/**
* GST-2346 D1: whether a stored remote write came from the brain owner's own credential speaking for itself, judged
* live from the stored authority (the request-time twin is ownerSpeaksForItself): no attested actor, no audience, no
- * subagent and no slug fence, and the principal's member is the enabled owner now. A credential unbound, a member
- * disabled or an owner changed after submission is not the owner here.
+ * subagent and no slug fence, stored or live (an OAuth client's bound_slug_prefixes, a local writer's grant ceiling), and the
+ * principal's member is the enabled owner now. A credential unbound or fenced, a member disabled or an owner changed after
+ * submission is not the owner here.
*/
export async function storedOwnerSpeaksForItself(engine: SqlEngine, authority: WriteAuthority, member?: string): Promise<boolean> {
if (!authority.remote || authority.actingFor || authority.restrictedNamespace || authority.slugPrefixes != null) return false;
+ const { principal } = authority;
+ const fence = principal.kind === 'oauth_client' ? 'SELECT bound_slug_prefixes AS prefixes FROM oauth_clients WHERE client_id=$1'
+ : principal.kind === 'local_cli' || principal.kind === 'local_stdio' ? "SELECT grant_ceiling->'slugPrefixes' AS prefixes FROM persistence_local_writers WHERE id=$1::uuid" : null;
+ if (fence && (await engine.executeRaw<{ prefixes: unknown }>(fence, [principal.id]))[0]?.prefixes != null) return false;
const bound = (await memberIdentityFor(engine, authority)).find(p => p.startsWith('member:')) ?? null;
return (member === undefined || bound === member) && defaultSourceWriterIsOwner(engine, bound);
}
@@ -309,16 +317,19 @@ export async function storedOwnerWrite(engine: SqlEngine, authority: WriteAuthor
}
/**
- * 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.
+ * GST-2091: for a page write 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, with visibility.honor_aliases read first as remote reads do); null when the write is not the owner's.
+ * `wholePage` is a put_page, which replaces the whole frontmatter. ownerWriteKeepsLabels keeps such a page private.
*/
-export async function ownerRewriteOf(engine: SqlEngine, authority: WriteAuthority, sourceId: string, slug: string): Promise<{ privacyPrivate: boolean } | null> {
+export async function ownerRewriteOf(engine: BrainEngine, authority: WriteAuthority, sourceId: string, slug: string, wholePage: boolean): Promise<OwnerRewrite | null> {
if (!await storedOwnerWrite(engine, authority)) return null;
+ await readHonorAliases(engine);
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 };
+ return { privacyPrivate: row?.hidden === true, wholePage };
}
+export interface OwnerRewrite { privacyPrivate: boolean; wholePage: boolean }
/**
* GST-2051: the readers a page written by a default-source member writer (defaultSourceMemberWriter) keeps when its content
@@ -328,8 +339,8 @@ export async function ownerRewriteOf(engine: SqlEngine, authority: WriteAuthorit
* 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: { privacyPrivate: boolean } | null = null): void {
- if (ownerRewrite) ownerWriteKeepsLabels(frontmatter, stored, ownerRewrite.privacyPrivate);
+ ownerRewrite: OwnerRewrite | null = null): void {
+ if (ownerRewrite) ownerWriteKeepsLabels(frontmatter, stored, ownerRewrite);
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;
@@ -337,19 +348,25 @@ 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.
+ * GST-2091: a page write (put_page, edit_page, append_page) by the brain owner's own credential (storedOwnerWrite) to a
+ * page private by the privacy rules (`privacyPrivate`: visibility, an alias, a pinned question, a derived type or a
+ * private origin) is stored as `visibility: private` when its result states no visibility (an empty or null value
+ * states none), so removing an alias, a derived type or a lineage key never publishes it; a stored pinned_question the
+ * result leaves out is kept, since granted_sources credentials read every other private page of their sources. A
+ * whole-page rewrite (put_page) also keeps the stored visibility and readable_by its content leaves out. A value the
+ * result states replaces the stored one.
*/
-function ownerWriteKeepsLabels(frontmatter: Record<string, unknown>, stored: { frontmatter?: unknown; deleted_at?: unknown } | null | undefined, privacyPrivate: boolean): void {
+function ownerWriteKeepsLabels(frontmatter: Record<string, unknown>, stored: { frontmatter?: unknown; deleted_at?: unknown } | null | undefined, rewrite: OwnerRewrite): 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')) {
- if (privacyPrivate) frontmatter.visibility = 'private';
- else if (live.visibility !== undefined) frontmatter.visibility = live.visibility;
+ const visibility = frontmatter.visibility;
+ if (visibility == null || (typeof visibility === 'string' && visibility.trim() === '')) {
+ delete frontmatter.visibility;
+ if (rewrite.privacyPrivate) frontmatter.visibility = 'private';
+ else if (rewrite.wholePage && live.visibility != null) frontmatter.visibility = live.visibility;
}
- if (!hasReaders(frontmatter) && hasReaders(live)) frontmatter.readable_by = live.readable_by;
+ if (frontmatter.pinned_question == null && live.pinned_question != null) frontmatter.pinned_question = live.pinned_question;
+ if (rewrite.wholePage && !hasReaders(frontmatter) && hasReaders(live)) frontmatter.readable_by = live.readable_by;
}
/** The one refusal a remote writer gets for a target it may not see; other visibility refusals reuse it verbatim. */
diff --git a/src/core/persistence/upload-streamed.ts b/src/core/persistence/upload-streamed.ts
index 2b3aec4867..f69914471f 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 './page
import type { PreparedMutation } from './coordinator.ts';
import type { WriteRequest } from './model.ts';
import { assertPrivateRequestKept, privateRequestGuarded, readPrivateRequest } from './visibility-request.ts';
-import { assertLabelledOnlyCreate, defaultSourceMemberWriter, defaultSourceWriterIsOwner, defaultSourceWriterReaders, ownerRewriteOf } from './page-visibility.ts';
+import { defaultSourceMemberWriter, defaultSourceWriterIsOwner, defaultSourceWriterReaders, ownerRewriteOf, remoteLabelsChecked } from './page-visibility.ts';
const text = (value: unknown) => (typeof value === 'string' ? value : null);
@@ -95,7 +95,7 @@ export async function prepareStreamedUpload(engine: BrainEngine, row: WriteReque
const 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 ownerRewriteOf(engine, row.authority, row.source_id, row.slug);
+ const ownerRewrite = await ownerRewriteOf(engine, row.authority, row.source_id, row.slug, row.operation === 'put_page');
let trust: WriteTrust | undefined;
const dir = await mkdtemp(join(tmpdir(), 'gbrain-upload-'));
let ready: StreamedPreparedImport | undefined;
@@ -138,7 +138,7 @@ export async function prepareStreamedUpload(engine: BrainEngine, row: WriteReque
throw opError('invalid_params', result.error ?? 'The uploaded page could not be prepared.', `Page ${row.slug} was not written. Fix what the message names, then upload it again with a new request_id.`);
}
const prepared: StreamedPreparedImport = ready;
- await assertLabelledOnlyCreate(engine, row.authority, row.slug, snapshot?.page, prepared.frontmatter);
+ await remoteLabelsChecked(engine, row.authority, row.slug, snapshot?.page, { parsedPage: { frontmatter: prepared.frontmatter } });
const privateRequest = privateRequestGuarded(row.operation, p.kind) ? readPrivateRequest('', () => prepared.frontmatter) : null;
if (privateRequest) assertPrivateRequestKept(row, privateRequest.frontmatter);
if (coreMarking(snapshot?.page.frontmatter).core || coreMarking(prepared.frontmatter).core) {
diff --git a/test/owner-remote-private-writes-gst2091.test.ts b/test/owner-remote-private-writes-gst2091.test.ts
index f2b857a412..44bf5d6cb5 100644
--- a/test/owner-remote-private-writes-gst2091.test.ts
+++ b/test/owner-remote-private-writes-gst2091.test.ts
@@ -20,7 +20,7 @@
* Postgres) and of a managed repo brain (Git commit).
*/
import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
-import { randomUUID } from 'node:crypto';
+import { createHash, randomUUID } from 'node:crypto';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import type { BrainEngine } from '../src/core/engine.ts';
@@ -31,6 +31,10 @@ import { sqlQueryForEngine } from '../src/core/sql-query.ts';
import { mintLegacyToken } from '../src/core/token-mint.ts';
import { dispatchToolCall } from '../src/mcp/dispatch.ts';
import { __resetPrivateVisibilityCacheForTests } from '../src/core/search/private-visibility.ts';
+import { storedOwnerWrite } from '../src/core/persistence/page-visibility.ts';
+import type { WriteAuthority } from '../src/core/persistence/model.ts';
+import { parseRescopeTokenArgs, rescopeLegacyToken } from '../src/core/grants/legacy-token.ts';
+import { readsUnscopedOnPurpose } from './helpers/reads-unscoped.ts';
import { addMember, bindCredential } from '../src/core/access/members.ts';
import { setOwner } from '../src/core/access/owner.ts';
import { linkMember } from '../src/core/access/actor.ts';
@@ -45,7 +49,7 @@ const WRITERS = ['put_page', 'edit_page', 'append_page'] as const;
type Writer = typeof WRITERS[number];
type Shape = 'private' | 'shared' | 'alice_only' | 'vault';
type Channel = { actor?: string; audience?: string };
-type Call = { isError: boolean; code: string | null; text: string };
+type Call = { isError: boolean; code: string | null; text: string; json: Record<string, any> };
type PageRow = { frontmatter: Record<string, unknown>; page_readers: string[] | null; revision: string; principal_kind: string | null; principal_id: string | null; body: string };
const tag = () => randomUUID().slice(0, 8);
@@ -83,17 +87,22 @@ async function seedBrain(engine: BrainEngine, sourceId: string, writePage: (slug
}
tokens.unbound_token = (await mintLegacyToken(engine, { name: `gst2091-unbound-${tag()}`, scopes: ['read', 'write'], takesHolders: ['world'], sourceGrant: [sourceId] })).token;
await linkMember(engine, OWNER, 'qm', 'U0OWNER');
+ const grantedName = `gst2091-granted-${tag()}`;
+ const granted = await mintLegacyToken(engine, { name: grantedName, scopes: ['read'], takesHolders: ['world'], sourceGrant: [sourceId] });
+ await rescopeLegacyToken(engine, parseRescopeTokenArgs([grantedName, '--private-pages', 'granted-sources']));
+ await readsUnscopedOnPurpose(engine, granted.id);
+ tokens.granted = granted.token;
async function call(who: string, op: string, params: Record<string, unknown>, channel: Channel = {}): Promise<Call> {
const auth = await provider().verifyAccessToken(tokens[who]!) as unknown as AuthInfo;
const out = await dispatchToolCall(engine, op, { ...params }, {
- remote: true, transport: 'http', auth, sourceId, takesHoldersAllowList: ['world'],
+ remote: true, transport: 'http', auth, sourceId, takesHoldersAllowList: ['world'], inboundFrameBytes: 4 * 1024 ** 2,
...(channel.actor ? { actor: { header: channel.actor } } : {}), ...(channel.audience ? { onBehalfOf: { header: channel.audience } } : {}),
});
const text = out.content.map(c => c.text).join('\n');
let json: Record<string, any> = {};
try { json = JSON.parse(out.content[0]?.text ?? ''); } catch { /* prose */ }
- return { isError: out.isError === true, code: out.isError === true ? json.error ?? json.code ?? null : null, text };
+ return { isError: out.isError === true, code: out.isError === true ? json.error ?? json.code ?? null : null, text, json };
}
async function local(slug: string): Promise<{ revision: string; content: string }> {
const out = await dispatchToolCall(engine, 'get_page', { slug, include_content: true }, { remote: false, sourceId });
@@ -125,7 +134,7 @@ async function seedBrain(engine: BrainEngine, sourceId: string, writePage: (slug
}
return call(who, 'append_page', { slug: page.slug, text: marker, request_id: randomUUID() }, channel);
}
- return { call, row, revision, seed, write, clients };
+ return { call, row, revision, seed, write, clients, local };
}
for (const kind of testBackends()) {
@@ -228,6 +237,169 @@ for (const kind of testBackends()) {
expect(await h.row(page.slug)).toEqual(before);
}, 120_000);
+ test('an owner put_page that leaves out labels keeps a pinned question hidden from a granted_sources credential', async () => {
+ const plain = await h.seed('private');
+ expect((await h.call('granted', 'get_page', { slug: plain.slug })).text).toContain(plain.canary);
+ const t = tag();
+ const page = { slug: `notes/pinned-${t}`, canary: `gst2091pinned${t}`, title: `Note ${t}` };
+ await importFromContent(engine, page.slug, pageText(page.title, `First line ${page.canary}.`, ['pinned_question: true']), { sourceId: SRC });
+ expect((await h.call('granted', '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: '' });
+ const after = await h.row(page.slug);
+ expect(after.body).toContain(`Owner change ${page.canary}.`);
+ expect(after.frontmatter.pinned_question).toBe(true);
+ for (const who of ['granted', 'alice', 'bob']) expect((await h.call(who, 'get_page', { slug: page.slug })).text).not.toContain(page.canary);
+ }, 120_000);
+
+ const anyRow = async (slug: string) => (await engine.executeRaw<Record<string, unknown>>(`SELECT frontmatter, deleted_at::text AS deleted_at, knowledge_revision::text AS revision,
+ revision_principal_id AS principal_id FROM pages WHERE source_id = $1 AND slug = $2`, [SRC, slug]))[0];
+ const versionsText = async (who: string, slug: string) => (await h.call(who, 'get_versions', { slug, include_body: true })).text;
+
+ for (const lines of [['visibility: private'], ['pinned_question: true']]) {
+ test(`an owner write to a soft-deleted private page (${lines[0]}) is refused as on master, changes nothing and opens no version history`, async () => {
+ const t = tag();
+ const page = { slug: `notes/deleted-${t}`, canary: `gst2091deleted${t}`, title: `Note ${t}` };
+ await importFromContent(engine, page.slug, pageText(page.title, `First line ${page.canary}.`, lines), { sourceId: SRC });
+ await h.write('owner', 'put_page', page);
+ const deleted = await dispatchToolCall(engine, 'delete_page', { slug: page.slug, expected_revision: await h.revision(page.slug) }, { remote: false, sourceId: SRC });
+ expect({ isError: deleted.isError === true, text: deleted.isError ? deleted.content.map(c => c.text).join('\n') : '' }).toEqual({ isError: false, text: '' });
+ const before = await anyRow(page.slug);
+ expect(before?.deleted_at).not.toBeNull();
+ const readers = ['bob', 'alice', 'granted'];
+ const seen = async () => Object.fromEntries(await Promise.all(readers.map(async who => [who, await versionsText(who, page.slug)])));
+ const seenBefore = await seen();
+ for (const who of ['bob', 'alice', ...(lines[0] === 'pinned_question: true' ? ['granted'] : [])]) expect(seenBefore[who]).not.toContain(page.canary);
+ const attempts: Array<[string, Record<string, unknown>]> = [
+ ['put_page', { slug: page.slug, content: pageText(page.title, `First line ${page.canary}.`, []), request_id: randomUUID() }],
+ ['put_page', { slug: page.slug, content: pageText(page.title, `First line ${page.canary}.`, ['visibility: world']), request_id: randomUUID() }],
+ ['edit_page', { slug: page.slug, expected_revision: before!.revision, request_id: randomUUID(), edits: [{ old_text: `First line ${page.canary}.`, new_text: `Edited ${page.canary}.` }] }],
+ ['append_page', { slug: page.slug, text: `Appended ${page.canary}.`, request_id: randomUUID() }],
+ ];
+ const codes: string[] = [];
+ for (const [op, params] of attempts) {
+ const out = await h.call('owner', op, params);
+ codes.push(`${op}:${out.isError ? out.code : 'ok'}`);
+ }
+ expect(codes).toEqual(SOFT_DELETED_CODES);
+ expect(await anyRow(page.slug)).toEqual(before);
+ expect(await seen()).toEqual(seenBefore);
+ }, 120_000);
+ }
+
+ test('a streamed owner upload over the single-string ceiling is held to the same readers check as a small one', async () => {
+ await engine.executeRaw("INSERT INTO config (key, value) VALUES ('write_gate.agent_mode', 'off') ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value");
+ const sha = (s: string) => createHash('sha256').update(Buffer.from(s, 'utf8')).digest('hex');
+ const PART_CHARS = 3_900_000;
+ const filler = 'We agreed to ship the review checklist first and revisit pricing next week. '.repeat(64) + '\n\n';
+ async function upload(slug: string, mode: 'create' | 'replace', content: string): Promise<Call> {
+ const begin = await h.call('owner', 'begin_page_upload', { slug, mode, total_bytes: Buffer.byteLength(content), sha256: sha(content), request_id: randomUUID(),
+ ...(mode === 'replace' ? { expected_revision: await h.revision(slug) } : {}) });
+ if (begin.isError) return begin;
+ for (let part = 0, at = 0; at < content.length; part++, at += PART_CHARS) {
+ const piece = content.slice(at, at + PART_CHARS);
+ const sent = await h.call('owner', 'upload_page_part', { upload_id: begin.json.upload_id, part, text: piece, part_sha256: sha(piece) });
+ if (sent.isError) return sent;
+ }
+ let commit = await h.call('owner', 'commit_page_upload', { upload_id: begin.json.upload_id, wait_ms: 30_000 });
+ for (let attempt = 0; commit.json.code === 'write_pending' && attempt < 40; attempt++) commit = await h.call('owner', 'commit_page_upload', { upload_id: begin.json.upload_id, wait_ms: 30_000 });
+ return commit;
+ }
+ try {
+ const shared = await h.seed('shared');
+ const created = `notes/upload-${tag()}`;
+ const cases: Array<[string, 'create' | 'replace', string[]]> = [
+ [shared.slug, 'replace', ['visibility: private', 'readable_by: [alice-example]']],
+ [created, 'create', ['readable_by: []']],
+ ];
+ const outcomes: string[] = [];
+ for (const big of [false, true]) {
+ for (const [slug, mode, lines] of cases) {
+ const before = await anyRow(slug);
+ const body = `First line ${shared.canary}.\n\n` + (big ? filler.repeat(Math.ceil(25 * 1024 ** 2 / filler.length)) : 'Short body.');
+ const out = await upload(slug, mode, pageText(shared.title, body, lines));
+ outcomes.push(`${big ? 'streamed' : 'inline'} ${mode}: ${out.isError ? out.code : out.json.state}`);
+ expect(await anyRow(slug)).toEqual(before);
+ }
+ }
+ expect(outcomes).toEqual(['inline replace: readable_by_invalid', 'inline create: readable_by_invalid', 'streamed replace: readable_by_invalid', 'streamed create: readable_by_invalid']);
+ } finally {
+ await engine.executeRaw("DELETE FROM config WHERE key = 'write_gate.agent_mode'");
+ }
+ }, 900_000);
+
+ test("the owner widening reads the client's slug fence live: a client fenced after submission is no longer the owner's own write", async () => {
+ const page = await h.seed('private');
+ const out = await h.write('owner', 'put_page', page);
+ expect({ isError: out.isError, text: out.isError ? out.text : '' }).toEqual({ isError: false, text: '' });
+ const [req] = await engine.executeRaw<{ authority: WriteAuthority | string }>(
+ "SELECT authority FROM persistence_requests WHERE source_id = $1 AND slug = $2 AND operation = 'put_page' ORDER BY created_at DESC LIMIT 1", [SRC, page.slug]);
+ const authority = (typeof req!.authority === 'string' ? JSON.parse(req!.authority) : req!.authority) as WriteAuthority;
+ expect(await storedOwnerWrite(engine, authority)).toBe(true);
+ await engine.executeRaw('UPDATE oauth_clients SET bound_slug_prefixes = $2::text[] WHERE client_id = $1', [h.clients.owner!, ['notes/']]);
+ try {
+ expect(await storedOwnerWrite(engine, authority)).toBe(false);
+ } finally {
+ await engine.executeRaw('UPDATE oauth_clients SET bound_slug_prefixes = NULL WHERE client_id = $1', [h.clients.owner!]);
+ }
+ expect(await storedOwnerWrite(engine, authority)).toBe(true);
+ }, 120_000);
+
+ for (const line of ['visibility:', 'visibility: ""']) {
+ test(`an owner put_page with an empty visibility (${line}) keeps a private page private`, async () => {
+ const page = await h.seed('private');
+ const out = await h.write('owner', 'put_page', page, {}, pageText(page.title, `First line ${page.canary}.\n\nOwner change ${page.canary}.`, [line]));
+ expect({ isError: out.isError, text: out.isError ? out.text : '' }).toEqual({ isError: false, text: '' });
+ const after = await h.row(page.slug);
+ expect(after.body).toContain(`Owner change ${page.canary}.`);
+ expect(after.frontmatter.visibility).toBe('private');
+ for (const who of ['alice', 'bob']) expect((await h.call(who, 'get_page', { slug: page.slug })).text).not.toContain(page.canary);
+ }, 120_000);
+ }
+
+ const editedAway: Array<[string, (origin: string) => string[], string, string, string]> = [
+ ['a privacy alias', () => ['access: confidential'], 'note', 'access: confidential', 'status: draft'],
+ ['derived_from a private page', origin => [`derived_from: [${origin}]`], 'note', 'derived_from:', 'related_to:'],
+ ['a derived atom type', () => [], 'atom', 'type: atom', 'type: note'],
+ ];
+ for (const [label, lines, type, oldText, newText] of editedAway) {
+ test(`an owner edit_page that removes ${label} without stating visibility keeps the page private`, async () => {
+ const origin = (await h.seed('private')).slug;
+ const t = tag();
+ const page = { slug: `notes/edited-${t}`, canary: `gst2091edited${t}`, title: `Note ${t}` };
+ await importFromContent(engine, page.slug, pageText(page.title, `First line ${page.canary}.`, lines(origin)).replace('type: note', `type: ${type}`), { sourceId: SRC });
+ expect((await h.call('bob', 'get_page', { slug: page.slug })).text).not.toContain(page.canary);
+ expect((await h.local(page.slug)).content).toContain(oldText);
+ const out = await h.call('owner', 'edit_page', { slug: page.slug, expected_revision: await h.revision(page.slug), request_id: randomUUID(),
+ edits: [{ old_text: oldText, new_text: newText }] });
+ expect({ isError: out.isError, text: out.isError ? out.text : '' }).toEqual({ isError: false, text: '' });
+ const after = await h.row(page.slug);
+ expect(after.body).not.toContain(oldText);
+ expect(after.frontmatter.visibility).toBe('private');
+ for (const who of ['alice', 'bob']) expect((await h.call(who, 'get_page', { slug: page.slug })).text).not.toContain(page.canary);
+ }, 120_000);
+ }
+
+ test('an owner put_page reads visibility.honor_aliases live before deciding a page is private by an alias', async () => {
+ const t = tag();
+ const page = { slug: `notes/alias-${t}`, canary: `gst2091alias${t}`, title: `Note ${t}` };
+ await importFromContent(engine, page.slug, pageText(page.title, `First line ${page.canary}.`, ['access: confidential']), { sourceId: SRC });
+ const setHonor = (value: string) => engine.executeRaw("INSERT INTO config (key, value) VALUES ('visibility.honor_aliases', $1) ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value", [value]);
+ try {
+ await setHonor('off');
+ __resetPrivateVisibilityCacheForTests();
+ await h.call('bob', 'get_page', { slug: page.slug });
+ await setHonor('on');
+ 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)).frontmatter.visibility).toBe('private');
+ } finally {
+ await engine.executeRaw("DELETE FROM config WHERE key = 'visibility.honor_aliases'");
+ __resetPrivateVisibilityCacheForTests();
+ }
+ for (const who of ['alice', 'bob']) expect((await h.call(who, 'get_page', { slug: page.slug })).text).not.toContain(page.canary);
+ }, 120_000);
+
const refused: Array<[string, string, Shape, Channel]> = [
['another member', 'bob', 'private', {}],
['a member the page lists', 'alice', 'shared', {}],
@@ -279,3 +451,6 @@ for (const kind of testBackends()) {
* attested call (put_page, edit_page and append_page are not attestable) and a credential without write scope.
*/
const REFUSED_CODE: Record<string, string> = { attested: 'actor_not_attested', owner_read_only: 'permission_denied' };
+
+/** The owner's writes to a soft-deleted private page, as master 503e7b65 answered them (recorded before the fix). */
+const SOFT_DELETED_CODES = ['put_page:page_not_found', 'put_page:page_not_found', 'edit_page:page_not_found', 'append_page:page_not_found'];