Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
Label and rule previews list same-slug pages other sources leave open
garrytan4b6048f6d528
diff --git a/src/commands/access-rules.ts b/src/commands/access-rules.tsindex f7f484471d..e59b5e6906 100644--- a/src/commands/access-rules.ts+++ b/src/commands/access-rules.ts@@ -23,6 +23,7 @@ import { usageError } from '../cli/cli-error.ts';import { opError } from '../core/ops/contract.ts';import { classifyRulebook, coversDerived, folderRulesAllow, normalizeRule, ruleCoversSql, parsePolicyYaml, serializePolicyYaml, sortRules, type FolderRule, type RuleChange } from '../core/access/folder-rules.ts';import { identityFor } from '../core/access/principals.ts';+import { openTwins, openTwinsLines, OPEN_TWINS_PAGES, type OpenTwins } from '../core/access/open-twins.ts';import { boundCredentials, listGroups, listMembers } from '../core/access/members.ts';import {appliedRules, applyApproved, getRevision, listRevisions, newOperationId, policyStatus, previewApply, proposeRevision, readAccessStore, resumePolicy,@@ -112,6 +113,8 @@ export interface RulePreview {changes: RuleChange[];held: RuleChange[];matched_pages: Record<string, number>;+ /** GST-2603: same-slug pages in other sources the narrowed rules leave readable (listed, never approved). */+ open_twins: OpenTwins;unknown_readers: string[];members_losing: string[];credentials_losing: Array<{ member: string; kind: string; name: string }>;@@ -140,8 +143,15 @@ async function previewRules(engine: BrainEngine, sourceId: string, store: Access}const credentials: RulePreview['credentials_losing'] = [];for (const m of losing) for (const c of await boundCredentials(engine, m)) credentials.push({ member: m, kind: c.kind, name: c.name });+ const twins: OpenTwins = { count: 0, twins: [], commands: [] };+ for (const rule of cls.changes.filter(c => c.narrowing).map(c => cls.target.find(r => r.id === c.rule)).filter(r => r !== undefined)) {+ const covered = await engine.executeRaw<{ source_id: string; slug: string }>(`SELECT source_id, slug FROM pages WHERE source_id = $1 AND deleted_at IS NULL AND ${ruleCoversSql('slug', '$2::text')} ORDER BY slug LIMIT ${OPEN_TWINS_PAGES}`, [sourceId, rule.prefix]);+ const found = await openTwins(engine, covered, rule.readable_by,+ source => `gbrain access rule add ${rule.prefix.endsWith('/') ? '--prefix' : '--slug'} ${rule.prefix} --readers ${rule.readable_by.map(reader).join(',')} --source ${source}`);+ Object.assign(twins, { count: twins.count + found.count, twins: [...twins.twins, ...found.twins], commands: [...twins.commands, ...found.commands] });+ }return { source_id: sourceId, store, store_persisted: persisted, expect, desired, changes: cls.changes, held: cls.held, matched_pages: matched,- unknown_readers: unknown, members_losing: losing, credentials_losing: credentials };+ unknown_readers: unknown, members_losing: losing, credentials_losing: credentials, open_twins: twins };}function previewLines(p: RulePreview, rerun: string): string[] {@@ -152,6 +162,7 @@ function previewLines(p: RulePreview, rerun: string): string[] {...(p.unknown_readers.length ? [` Unknown readers (no such member or group; they match nobody): ${p.unknown_readers.join(', ')}`] : []),...(p.members_losing.length ? [` Members losing access: ${p.members_losing.join(', ')}`] : []),...(p.credentials_losing.length ? [` Credentials losing access: ${p.credentials_losing.map(c => `${c.kind} ${c.name} (${c.member})`).join(', ')}`] : []),+ ...openTwinsLines(p.open_twins),'Pages under no rule are unchanged; unbound credentials and stdio read no page under a rule.',`Nothing written. To approve, add --yes --expect ${p.expect} (${rerun}).`,];diff --git a/src/core/access/open-twins.ts b/src/core/access/open-twins.tsnew file mode 100644index 0000000000..178b7a2e79--- /dev/null+++ b/src/core/access/open-twins.ts@@ -0,0 +1,51 @@+/**+ * GST-2603: labels and folder rules apply per source, so a page labelled or ruled in one source leaves a live row with the+ * same slug in another source as it was, and a reader with no source filter still finds that row. `openTwins` lists, for+ * the pages a preview changes, those rows a caller outside the change could still read: a row that is not private by its+ * own labels (privateSnapshotFilterFragment; lineage is not read, so a plan that reads no links still reads none) and that the empty identity or an enabled member outside `readers` passes by its own readers and+ * folder rules (readersAllowedSql, folderRulesAllowedSql). Read-only and bounded; it lists and never approves, so no+ * preview hash covers it.+ */+import type { BrainEngine } from '../engine.ts';+import { folderAccessFor } from './caller.ts';+import { listMembers } from './members.ts';+import { identityFor, normalizePrincipal } from './principals.ts';+import { folderRulesAllowedSql, privateSnapshotFilterFragment, readersAllowedSql } from '../search/private-visibility.ts';++/** The pages of one preview checked for twins, and the twins listed. */+export const OPEN_TWINS_PAGES = 1000;+export const OPEN_TWINS_LISTED = 50;+export interface OpenTwins { count: number; twins: Array<{ source_id: string; slug: string; twin_of: string }>; commands: string[] }++/** `readers`: who the change leaves reading (bare member ids or principals); `cover(source, slugs)`: the command giving one source's listed twins the same change. */+export async function openTwins(engine: BrainEngine, pages: ReadonlyArray<{ source_id: string; slug: string }>, readers: readonly string[],+ cover: (sourceId: string, slugs: string[]) => string): Promise<OpenTwins> {+ const window = pages.slice(0, OPEN_TWINS_PAGES);+ if (!window.length) return { count: 0, twins: [], commands: [] };+ const inside = new Set<string | null>(readers.map(normalizePrincipal));+ const outside = [[], ...(await listMembers(engine)).filter(member => member.status === 'enabled')+ .map(member => identityFor(member.id, member.groups)).filter(identity => !identity.some(principal => inside.has(principal)))];+ const readable = await Promise.all(outside.map(async identity =>+ `(${readersAllowedSql('t', identity)} AND ${folderRulesAllowedSql('t', identity.length ? (await folderAccessFor(engine, identity)).satisfiedRuleIds : [])})`));+ const rows = await engine.executeRaw<{ source_id: string; slug: string; twin_of: string; n: number }>(`SELECT t.source_id, t.slug, min(c.source_id) AS twin_of,+ count(*) OVER ()::int AS n FROM unnest($1::text[], $2::text[]) AS c(source_id, slug)+ JOIN pages t ON t.slug = c.slug AND t.source_id <> c.source_id AND t.deleted_at IS NULL+ WHERE NOT EXISTS (SELECT 1 FROM unnest($1::text[], $2::text[]) AS w(source_id, slug) WHERE w.source_id = t.source_id AND w.slug = t.slug)+ AND ${privateSnapshotFilterFragment('t')} AND (${readable.join(' OR ')})+ GROUP BY t.source_id, t.slug ORDER BY t.source_id, t.slug LIMIT ${OPEN_TWINS_LISTED}`, [window.map(page => page.source_id), window.map(page => page.slug)]);+ const twins = rows.map(({ source_id, slug, twin_of }) => ({ source_id, slug, twin_of: `${twin_of}:${slug}` }));+ const sources = [...new Set(twins.map(twin => twin.source_id))];+ return { count: Number(rows[0]?.n ?? 0), twins, commands: sources.map(source => cover(source, twins.filter(twin => twin.source_id === source).map(twin => twin.slug))) };+}++/** The preview warning for open twins; none when there are none. */+export function openTwinsWarning(found: OpenTwins): string[] {+ return found.count ? [`${found.count} live page(s) in other sources share a slug with a page this changes and stay readable by callers outside the change, `+ + `because labels and rules apply per source. details.open_twins lists them; to cover them, run: ${found.commands.join('; ')}`] : [];+}++/** The text lines of a preview's open twins. */+export function openTwinsLines(found: OpenTwins | undefined): string[] {+ return found?.count ? [` open twins: ${found.count}`, ...found.twins.map(twin => ` open twin ${twin.source_id}:${twin.slug} (twin of ${twin.twin_of})`),+ ...found.commands.map(command => ` cover: ${command}`)] : [];+}diff --git a/src/core/repair/private-labels.ts b/src/core/repair/private-labels.tsindex fd5c7c953b..d66ccae113 100644--- a/src/core/repair/private-labels.ts+++ b/src/core/repair/private-labels.ts@@ -49,6 +49,8 @@*/import type { BrainEngine } from '../engine.ts';import { serializePageToMarkdown } from '../markdown.ts';+import { openTwins, openTwinsLines, openTwinsWarning, type OpenTwins } from '../access/open-twins.ts';+import { ownerMemberId } from '../access/owner.ts';import { opError, type OperationContext } from '../ops/contract.ts';import { maintenancePreflight, publishMaintenancePages, type MaintenanceAuthority } from '../persistence/prepared-maintenance.ts';import { isPlainLabelValue, type LabelEdit } from '../persistence/label-edit.ts';@@ -463,6 +465,9 @@ export const privateLabelsRepair: RepairHandler = {const dependents = found?.counts ?? null;const exposed = dependents ? Object.values(dependents).reduce((sum, n) => sum + n, 0) : 0;const fileBacked = (await filesInCheckout(engine, window.map(entry => entry.page_id))).size;+ const owner = await ownerMemberId(engine);+ const twins = await openTwins(engine, restore ? window : [], owner ? [owner] : [],+ (source, slugs) => `gbrain repair visibility --source ${source}${slugs.map(slug => ` --slug ${slug}`).join('')}`);const warnings = [...(pastWindow ? [`This preview lists one window of ${window.length} item(s); ${pastWindow} more found past it. Apply this window (or decide not to), then preview again for the next one: ${command}`] : []),restore ? 'Restoring re-adds each recorded label, so remote callers that are not the brain owner stop reading these pages again.'@@ -481,6 +486,7 @@ export const privateLabelsRepair: RepairHandler = {...(retried ? [`${retried} listed page(s) were tried by an earlier apply and did not change (the ledger has their intent line and nothing after it); `+ 'they are listed after every page not tried yet. Each is tried again and, if its page still declines, left unchanged with its reason; '+ 'such a decline does not count toward the run\'s systemic-failure stop.'] : []),+ ...openTwinsWarning(twins),...(planned.unreadable ? [`${planned.unreadable} ledger line(s) could not be read and are not planned.`] : []),...(restore && before.removed_after_restore ? [`${before.removed_after_restore} page(s) had their label removed after a restore landed (the page changed since the revision the restore wrote), `+ 'so they are not listed; re-add the label by hand if that edit was a mistake.'] : []),@@ -492,7 +498,7 @@ export const privateLabelsRepair: RepairHandler = {listing: window.map(entry => ({ item: `${entry.source_id}:${entry.slug}`, class: entry.class,detail: entry.mode === 'restore' ? `re-add ${describeLabels(entry.labels)}` : entry.labels.visibility ? 'remove access: private, visibility: private' : 'remove access: private' })),details: { counts: { before, after: projected }, window: { size, listed: window.length, pending_past_window: pastWindow, next_preview: command, file_backed: fileBacked, tried_before: retried },- ...(dependents ? { dependents } : {}),+ ...(dependents ? { dependents } : {}), ...(twins.count ? { open_twins: twins } : {}),ledger: ledger?.path ?? null, class: selection.class } };},async apply(ctx, raw, opts): Promise<RepairItemOutcome> {@@ -596,6 +602,7 @@ export const privateLabelsRepair: RepairHandler = {const rows = details.ledger_rows as { intent: number; committed: number } | undefined;return [...(counts ? [` before: ${line(counts.before)}`, ` after (this window applied): ${line(counts.after)}`] : []),...(dependents ? [` also exposed by this window: ${line(dependents)}`] : []),+ ...openTwinsLines(details.open_twins as OpenTwins | undefined),...(details.ledger ? [` ledger: ${String(details.ledger)}${rows ? ` (this run appended ${rows.intent + rows.committed} line(s): ${rows.intent} intent, ${rows.committed} committed)` : ''}`] : [])];},};diff --git a/src/core/repair/readers.ts b/src/core/repair/readers.tsindex 16c8cbf317..a80850d89d 100644--- a/src/core/repair/readers.ts+++ b/src/core/repair/readers.ts@@ -30,6 +30,7 @@ import type { PageSnapshot } from '../page-state/types.ts';import { isSegmentedPage } from '../page-body/page-shape.ts';import { maintenanceTransaction } from '../persistence/attribution.ts';import { raisePageLabelGuard } from '../trust/page-write.ts';+import { openTwins, openTwinsWarning } from '../access/open-twins.ts';import { afterCursor, type RepairCursor, type RepairItem, type RepairItemOutcome, type RepairPlan, type RepairPlanOptions, type RepairScope } from './core.ts';/** The operator's request: `--readers` as typed (`none` removes), the owner flag, and the `--slugs-file` path with its slugs. */@@ -181,11 +182,14 @@ export async function planReaders(engine: BrainEngine, scope: RepairScope, afterif (opts.apply) return { items: await approvedItems(engine, scope, selection, opts.expect!, command, after), preview_hash: opts.expect, residuals: {} };const { items, residuals } = await planPages(engine, selection);const hash = previewHash(hashParts(scope, selection, items));+ const twins = await openTwins(engine, readers?.length ? items : [], readers ?? [],+ (source, named) => `gbrain repair visibility --source ${source}${named.map(slug => ` --slug ${slug}`).join('')} --readers ${request.readers}`);const selectionDigest = digest(selection);if (items.length) await saveApprovedSet<ApprovedReaders>(engine, { command: 'visibility', hash },items.map(item => ({ target: item.target, source_id: item.source_id, slug: item.slug, chars: item.chars, action: item.action, readers: item.readers, selection_digest: selectionDigest })));- return { items: items.map((item, index) => ({ ...item, cursor: { phase: 0, id: index } })), preview_hash: hash, residuals,- ...(residuals.named_not_found ? { warnings: [residuals.named_not_found + ' named page(s) were not found in ' + scope.source_ids.join(', ') + ' and are not listed.'] } : {}),+ const warnings = [...(residuals.named_not_found ? [residuals.named_not_found + ' named page(s) were not found in ' + scope.source_ids.join(', ') + ' and are not listed.'] : []), ...openTwinsWarning(twins)];+ return { items: items.map((item, index) => ({ ...item, cursor: { phase: 0, id: index } })), preview_hash: hash, residuals: { ...residuals, ...(twins.count ? { open_twins: twins.count } : {}) },+ ...(warnings.length ? { warnings } : {}), ...(twins.count ? { details: { open_twins: twins } } : {}),listing: items.map(item => ({ item: item.source_id + ':' + item.slug, class: 'readers', detail: item.action })) };}diff --git a/src/core/repair/visibility.ts b/src/core/repair/visibility.tsindex b693ee128f..13816f55a2 100644--- a/src/core/repair/visibility.ts+++ b/src/core/repair/visibility.ts@@ -53,6 +53,8 @@ import { maintenancePreflight, publishMaintenancePages, submitDatabaseMaintenancimport { CANONICAL_FILE_MISSING } from '../persistence/publication-failure.ts';import { isSegmentedPage } from '../page-body/page-shape.ts';import { afterCursor, repairRequestId, type RepairHandler, type RepairItem, type RepairItemOutcome, type RepairPlan, type RepairPlanOptions, type RepairScope, type RepairSettlement, type RepairCursor } from './core.ts';+import { openTwins, openTwinsLines, openTwinsWarning, type OpenTwins } from '../access/open-twins.ts';+import { ownerMemberId } from '../access/owner.ts';import { filesInCheckout, labelFileRefusal, writeLabelFile, writeLabelPage, type LabelFileRefusal } from './label-write.ts';import { applyReaders, planReaders, raiseGuardOnly, raiseReadersGuard, readersBatch, readersGitCounts, readersGuardOnly, readersOutcome, readersWrite, retireReaders, READERS_PHASE, type ReadersItem, type ReadersWrite } from './readers.ts';import type { LabelEdit } from '../persistence/label-edit.ts';@@ -483,6 +485,10 @@ export const visibilityRepair: RepairHandler = {progress?.finish();if (held.length) Object.assign(residuals, { held_file_drift: held.filter(hold => hold.reason === 'file_database_drift').length, held_label_edit: held.filter(hold => hold.reason === 'label_edit_refused').length });const hash = previewHash(hashParts(scope, selection, items));+ const owner = await ownerMemberId(engine);+ const twins = await openTwins(engine, items.filter(item => item.target.phase !== 4 && item.change?.to === 'private'), owner ? [owner] : [],+ (source, slugs) => `gbrain repair visibility --source ${source}${slugs.map(slug => ` --slug ${slug}`).join('')}`);+ if (twins.count) residuals.open_twins = twins.count;if (items.length) await saveApprovedSet<ApprovedVisibility>(engine, { command: 'visibility', hash },items.map(item => ({ target: item.target, source_id: item.source_id, slug: item.slug, chars: item.chars, action: item.action, change: item.change!, selection })));const next = visibilityPreviewCommand(scope, selection, true);@@ -491,12 +497,13 @@ export const visibilityRepair: RepairHandler = {+ `Apply this window (or decide not to), then preview again for the next one: ${next}`] : []),...timedOut.map(cls => `Class ${cls} did not finish within the ${PLAN_STATEMENT_MS} ms statement bound and is not listed; the other classes are. `+ `Preview the others with --class, or run gbrain doctor --only derived_visibility for the counts.`),+ ...openTwinsWarning(twins),...(held.length ? [`${held.length} page(s) are held and not in this preview's set: their files would refuse the label write now. details.held names what differs for each and the step that clears it; preview again after it.`] : []),];return { warnings, items: items.map((item, index) => ({ ...item, cursor: { phase: 0, id: index } })), preview_hash: hash,residuals,listing: items.map(item => ({ item: `${item.source_id}:${item.slug}`, class: CLASS_OF_PHASE[item.target.phase]!, detail: item.action })),- details: { window: { size, listed: items.length, pending, pending_past_window: pendingPastWindow, not_read: notRead, timed_out: timedOut, next_preview: next }, ...(held.length ? { held } : {}) } };+ details: { window: { size, listed: items.length, pending, pending_past_window: pendingPastWindow, not_read: notRead, timed_out: timedOut, next_preview: next }, ...(held.length ? { held } : {}), ...(twins.count ? { open_twins: twins } : {}) } };},/** A --readers apply: how its managed writes reached Git (`details.git`, effect states; readers.ts readersGitCounts). */async report(ctx, scope, result, opts) {@@ -506,6 +513,7 @@ export const visibilityRepair: RepairHandler = {const git = details.git as Record<string, number> | undefined;const held = (details.held as HeldVisibility[] | undefined) ?? [];return [...(git ? [` git: ${Object.entries(git).map(([key, value]) => `${key}=${value}`).join(', ')} (file(s) by Git effect state; queued files commit together, up to 100 per commit)`] : []),+ ...openTwinsLines(details.open_twins as OpenTwins | undefined),...held.map(hold => ` held ${hold.item} (${hold.reason}${hold.keys.length ? `: ${hold.keys.join(', ')}` : ''}); next: ${hold.next}`)];},/**