Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

Merge remote-tracking branch 'origin/capy/belief-w1-purge' into capy/belief-ledger-w1

garrytan8b8dadb3b577
diff --cc src/commands/beliefs.ts
index a5428252f9,6c1fb61ea1..bf4106392b
--- a/src/commands/beliefs.ts
+++ b/src/commands/beliefs.ts
@@@ -16,10 -16,10 +16,11 @@@ import { coverageIntervals, coveredSour
import { backfillBeliefHistory } from '../core/belief/backfill.ts';
import { readLedgerSource } from '../core/belief/ledger.ts';
import { readManagedMode } from '../core/persistence/managed-mode.ts';
+import { resolveSourceId, SourceTargetError } from '../core/source-resolver.ts';
+ import { replayPurgesCli } from './beliefs-replay-purges.ts';
export const BELIEFS_USAGE = [
- 'Usage: gbrain beliefs <enable|disable|status|reconcile|backfill> [--source <id>] [--json]',
+ 'Usage: gbrain beliefs <enable|disable|status|reconcile|backfill|replay-purges> [--source <id>] [--json]',
' enable Start recording the belief ledger on a source: baselines every page (resumable) and sets the watermark.',
' Only a managed source\'s coverage is certified; an unmanaged source is recorded but uncertified.',
' disable Stop recording (appends coverage.stopped); enabling again starts a new interval with a new baseline.',
@@@ -27,10 -27,12 +28,12 @@@
' reconcile Appends reconcile events for slugs whose state drifted from their newest event, and fills missing commit marks.',
' A drifted slug\'s interval stays uncertified on a managed source.',
' backfill Seeds best-effort history before the coverage start from retained page versions (never certified).',
+ ' replay-purges --archived-at <iso> (--purge-mirror <abs path> | --from-url-env <VAR>) [--apply]',
+ ' After a Postgres restore, replays the purges the restored backup does not hold (dry run unless --apply).',
- ' --source <id> The source (default: every covered source for status and reconcile, "default" for the others).',
+ ' --source <id> The source (default: every covered source for status and reconcile; the resolved source, as for other commands, for the rest).',
].join('\n');
- const SUBCOMMANDS = ['enable', 'disable', 'status', 'reconcile', 'backfill'] as const;
+ const SUBCOMMANDS = ['enable', 'disable', 'status', 'reconcile', 'backfill', 'replay-purges'] as const;
function sourceArg(args: string[]): string | undefined {
const at = args.indexOf('--source');
diff --cc src/core/belief/ledger.ts
index 73a9383070,6dd5f29029..6e2d22e9c1
--- a/src/core/belief/ledger.ts
+++ b/src/core/belief/ledger.ts
@@@ -471,17 -470,21 +471,30 @@@ export async function currentStateHash(
return { hash: stateHashOf(page, page ? lifetimes.get(page.id)! : null, subjects), state, lifetimes };
}
+/** resolveLifetimes without writes (a read-only drift scan): a page with no fitting open lifetime gets a placeholder, so its hash cannot match a recorded one. */
+async function peekLifetimes(tx: BrainEngine, needs: ReturnType<typeof pageNeeds>): Promise<Map<number, string>> {
+ const open = await readOpenLifetimes(tx, needs);
+ return new Map(needs.map(n => {
+ const row = open.get(`${n.incarnation}\u0000${n.slug}`);
+ return [n.pageId, row && (row.page_id == null || row.page_id === n.pageId) ? row.page_lifetime : `unminted:${n.pageId}`];
+ }));
+}
+
export { stateHashOf };
+ /**
+ * The journaled operations that are a direct edit of a page's facts or takes (design 6.5: a trusted local write that
+ * sets the field). Only these, from a local caller and with no internal intent `kind` (the `put_page` rows of
+ * import, file repair, quarantine clear, reconcile and managed maintenance carry one), can release a purge
+ * redaction; sync (`submit_job`), gate release, held accept, backfill, revert, restore and retention never do.
+ * append.ts still requires the writer tier at or above OWNER_TIER_FLOOR, and the trust channel records these verbs
+ * as agent_written even from the local CLI, so today `trust confirm` is the release that reaches it.
+ */
+ const OWNER_EDIT_OPERATIONS: ReadonlySet<string> = new Set(['put_page', 'edit_page', 'takes_update', 'takes_resolve']);
+ let allowlistDropped = false;
+ /** Tests: the forced probe that drops the allowlist, so every local publication counts as an owner edit. */
+ export function __dropOwnerEditAllowlistForTests(dropped: boolean): void { allowlistDropped = dropped; }
+
/** The batch options of one journaled page publication (single or group member). */
export function publicationBeliefOptions(row: { id: string; source_id: string; slug: string; operation: string; authority: { remote?: boolean }; intent?: Record<string, unknown> | null },
additionalKeys: readonly { sourceId: string; slug: string }[] = [], report?: { appended: number }): BeliefBatchOptions {