Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

test(persistence): guard completion true-up and the ghost exemptions (#6696 A1, A12)

garrytand503895989b8
diff --git a/src/core/persistence/prepared-maintenance.ts b/src/core/persistence/prepared-maintenance.ts
index 9bb57c1191..96447e2815 100644
--- a/src/core/persistence/prepared-maintenance.ts
+++ b/src/core/persistence/prepared-maintenance.ts
@@ -199,7 +199,7 @@ export function maintenanceDriftRefusal(sourceId: string, slug: string): Operati
}
/** #6696 (A12): maintenance kinds whose preparer passes `allowMissingFile` (chronicle, stale-atom retirement): a missing canonical file publishes, so it is no ghost. */
-const PUBLISHES_WITHOUT_FILE = new Set(['managed_maintenance_chronicle_event', 'managed_maintenance_chronicle_retire', 'managed_maintenance_retire_stale_atoms']);
+export const PUBLISHES_WITHOUT_FILE: ReadonlySet<string> = new Set(['managed_maintenance_chronicle_event', 'managed_maintenance_chronicle_retire', 'managed_maintenance_retire_stale_atoms']);
/** #6696 (A12): `source_changed` / `canonical_file_missing` before admission: the page's recorded file is gone; never a new request. */
export function maintenanceGhostRefusal(sourceId: string, slug: string): OperationError {
diff --git a/test/maintenance-ghost-exemption-guard.test.ts b/test/maintenance-ghost-exemption-guard.test.ts
new file mode 100644
index 0000000000..bb8eadd3e1
--- /dev/null
+++ b/test/maintenance-ghost-exemption-guard.test.ts
@@ -0,0 +1,54 @@
+/**
+ * #6696 (A12) guard: maintenance refuses a write before admission when its
+ * page's canonical file is gone, except for the kinds whose preparer passes
+ * `allowMissingFile` (a missing file publishes for them). The exemption set in
+ * prepared-maintenance.ts must equal the set of dispatched kinds whose
+ * preparer, or a same-file helper it calls, passes that option; a new kind
+ * that tolerates a missing file, or an exemption whose preparer stopped
+ * tolerating it, fails here.
+ */
+import { expect, test } from 'bun:test';
+import { readFileSync } from 'node:fs';
+import { join } from 'node:path';
+import { PUBLISHES_WITHOUT_FILE } from '../src/core/persistence/prepared-maintenance.ts';
+
+const ROOT = join(import.meta.dir, '..');
+const DISPATCH_FILE = join(ROOT, 'src/core/persistence/prepared-maintenance.ts');
+const ALLOWS_MISSING = /\ballowMissing(?:File)?\s*:\s*true\b/;
+
+function functionBody(text: string, name: string): string | null {
+ const start = text.search(new RegExp(`(?:export\\s+)?async\\s+function\\s+${name}\\s*\\(`));
+ if (start < 0) return null;
+ const end = text.indexOf('\n}\n', start);
+ return text.slice(start, end < 0 ? undefined : end);
+}
+
+/** The preparer body plus the bodies of same-file functions it calls (one level). */
+function preparerText(file: string, name: string): string {
+ const text = readFileSync(file, 'utf8');
+ const body = functionBody(text, name);
+ if (body === null) throw new Error(`preparer ${name} not found in ${file}`);
+ const helpers = [...new Set([...body.matchAll(/\b(\w+)\(/g)].map(m => m[1]!))].filter(callee => callee !== name)
+ .map(callee => functionBody(text, callee) ?? '');
+ return [body, ...helpers].join('\n');
+}
+
+test('the ghost-refusal exemptions are exactly the maintenance kinds whose preparer passes allowMissingFile', () => {
+ const dispatcher = functionBody(readFileSync(DISPATCH_FILE, 'utf8'), 'prepareMaintenanceKind')!;
+ const branch = /row\.intent\?\.kind === '([a-z_]+)'(?:\s*\|\|\s*row\.intent\?\.kind === '([a-z_]+)')?\)\s*(?:\{\s*)?return\s+(?:\(await import\('([^']+)'\)\)\.)?(\w+)\(/g;
+ const dispatched = new Map<string, boolean>();
+ for (const m of dispatcher.matchAll(branch)) {
+ const file = m[3] ? join(ROOT, 'src/core/persistence', m[3]) : DISPATCH_FILE;
+ const tolerates = ALLOWS_MISSING.test(preparerText(file, m[4]!));
+ for (const kind of [m[1], m[2]].filter((k): k is string => !!k)) dispatched.set(kind, tolerates);
+ }
+ // Every `kind === '...'` branch is a delegating return, so none escapes the parse; the inline kinds (page, consolidate) never pass the option.
+ const every = new Set([...dispatcher.matchAll(/row\.intent\?\.kind === '([a-z_]+)'/g)].map(m => m[1]!));
+ const inline = [...every].filter(kind => !dispatched.has(kind)).sort();
+ expect(inline).toEqual(['managed_maintenance_page']);
+ const inlineText = dispatcher.replace(branch, '');
+ expect(ALLOWS_MISSING.test(inlineText)).toBe(false);
+ expect(dispatched.size).toBeGreaterThanOrEqual(15);
+ const tolerant = [...dispatched].filter(([, tolerates]) => tolerates).map(([kind]) => kind).sort();
+ expect([...PUBLISHES_WITHOUT_FILE].sort()).toEqual(tolerant);
+});
diff --git a/test/persistence-true-up-sites.test.ts b/test/persistence-true-up-sites.test.ts
index 7100647f85..edc200e1aa 100644
--- a/test/persistence-true-up-sites.test.ts
+++ b/test/persistence-true-up-sites.test.ts
@@ -3,6 +3,11 @@
* (`committed`, `failed`, or a computed state) must reach `trueUpRequests`
* after its transaction, or the request keeps its admission reservation
* forever. A new site fails here until it is wired and listed.
+ *
+ * A request with no effects never settles one, so its completion trues it up
+ * in the completing statement itself (counters are already locked first
+ * there): every statement that can move a request to a terminal state must be
+ * a listed completion and carry that true-up.
*/
import { expect, test } from 'bun:test';
import { readdirSync, readFileSync, statSync } from 'node:fs';
@@ -48,3 +53,29 @@ test('the effect-journal settle helpers are called only under the effect runner'
const callers = sources.filter(file => /\b(?:completeEffect|failEffect|parkEffect)\(/.test(file.text)).map(file => file.path);
expect(callers.filter(path => !RUNNER_FILES.has(path))).toEqual([]);
});
+
+/** The statements that complete a request: file -> how many. Each sizes terminal_reservation down when no effect is unfinished. */
+const COMPLETIONS: Record<string, number> = {
+ 'src/core/persistence/journal.ts': 1, // completeWrite: committed, conflict, failed, cancelled
+ 'src/core/persistence/group-publish.ts': 1, // completeGroup: committed
+};
+/** An UPDATE of persistence_requests assigning `state` in its SET list, with the statement text up to the closing backtick. */
+const REQUEST_STATE = /UPDATE\s+persistence_requests\b(?:(?!\bWHERE\b)[^`;])*?\bSET\b(?:(?!\bWHERE\b)[^`;])*?(?<![.\w])state\s*=\s*([^,\n]+)[^`]*/g;
+const NON_TERMINAL = /^'(?:queued|running|recovering)'$/;
+/** A CASE that only keeps the current state or moves it to a non-terminal one. */
+const NON_TERMINAL_CASE = (statement: string) => [...statement.matchAll(/\bTHEN\s+('[a-z_]+'|state)\b/g)].every(m => m[1] === 'state' || NON_TERMINAL.test(m[1]!));
+
+test('every statement that completes a request is a listed completion and trues up its reservation', () => {
+ const found: Record<string, number> = {};
+ const missing: string[] = [];
+ for (const file of sources) {
+ for (const match of file.text.matchAll(REQUEST_STATE)) {
+ const value = match[1]!.trim();
+ if (NON_TERMINAL.test(value) || (value.startsWith('CASE') && NON_TERMINAL_CASE(match[0]))) continue;
+ found[file.path] = (found[file.path] ?? 0) + 1;
+ if (!/terminal_reservation\s*=\s*CASE\s+WHEN\b[\s\S]*?\bunfinished\b[\s\S]*?\bLEAST\(/.test(match[0])) missing.push(file.path);
+ }
+ }
+ expect(found).toEqual(COMPLETIONS);
+ expect(missing).toEqual([]);
+});