Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

wip: runner outcomes (signal, timeout, load, assertion), usage and nothing-checked codes, recovery table, jar step

garrytanf11358eb2dd6
diff --git a/.github/workflows/tla-spec.yml b/.github/workflows/tla-spec.yml
index a38b826bd9..80969c7f9a 100644
--- a/.github/workflows/tla-spec.yml
+++ b/.github/workflows/tla-spec.yml
@@ -73,7 +73,9 @@ jobs:
run: bun scripts/ci-bun-install.ts --frozen-lockfile --ignore-scripts
# TLA_JAR_DOWNLOAD_FAILED is a network failure (rerun the job); TLA_JAR_SHA_MISMATCH is never rerun.
- name: Fetch TLC (pinned in scripts/spec/tla-tools.env, checked against its sha256)
- run: echo "TLA2TOOLS=$(bash scripts/spec/tla-jar.sh)" >> "$GITHUB_ENV"
+ run: |
+ jar="$(bash scripts/spec/tla-jar.sh)" || exit "$?"
+ printf 'TLA2TOOLS=%s\n' "$jar" >> "$GITHUB_ENV"
- name: The configs match the code
run: bun test test/spec-drain-lease-constants.test.ts
- name: Model-check every config
diff --git a/scripts/spec/mutants.ts b/scripts/spec/mutants.ts
index 662cfb0b60..2644541f8e 100644
--- a/scripts/spec/mutants.ts
+++ b/scripts/spec/mutants.ts
@@ -10,10 +10,12 @@
*
* bun scripts/spec/mutants.ts [<spec>] [--only <id>] [--spec-only | --code-only] [--require-postgres]
*
- * Failure codes: MUTANT_STALE (an edit's `before` text is not found exactly once: the spec or code moved; update the
- * mutant), MUTANT_SURVIVED (TLC found no violation, or the test passed under the code edit: a property or a test no
- * longer catches the guard), MUTANT_WRONG_PROPERTY, MUTANT_INCOMPLETE (TLC did not finish, or a test failed without
- * the edit, matched no test, or did not load with it).
+ * Failure codes: MUTANT_USAGE (unknown or contradictory flags), MUTANT_NOTHING_CHECKED (every selected check was skipped), MUTANT_STALE (an edit's `before` text is not found exactly
+ * once: the spec or code moved; update the mutant), MUTANT_SURVIVED (TLC found no violation, or the test passed under the
+ * code edit: a property or a test no longer catches the guard), MUTANT_WRONG_PROPERTY, MUTANT_INCOMPLETE (TLC did not
+ * finish, or a test failed without the edit, matched no test, or did not run to a clean assertion failure with it: a
+ * signal, timeout, load error or non-assertion failure proves nothing). Every child's full output is kept under TLC_OUT
+ * (default .context/tlc).
*/
import { spawnSync } from 'node:child_process';
import { cpSync, existsSync, mkdirSync, readFileSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
@@ -28,6 +30,10 @@ interface Catalog { module: string; config: string; mutants: Mutant[] }
const ROOT = resolve(import.meta.dir, '../..');
const args = process.argv.slice(2);
const flag = (name: string) => args.includes(name);
+const FLAGS = new Set(['--only', '--spec-only', '--code-only', '--require-postgres']);
+const unknown = args.filter(a => a.startsWith('--') && !FLAGS.has(a));
+if (unknown.length) { console.error(`MUTANT_USAGE: unknown flag ${unknown.join(', ')}. Usage: bun scripts/spec/mutants.ts [<spec>] [--only <id>] [--spec-only | --code-only] [--require-postgres]`); process.exit(2); }
+if (flag('--spec-only') && flag('--code-only')) { console.error('MUTANT_USAGE: --spec-only and --code-only together would check nothing; pass at most one.'); process.exit(2); }
const only = args.includes('--only') ? args[args.indexOf('--only') + 1] : undefined;
const specName = args.find((a, i) => !a.startsWith('--') && args[i - 1] !== '--only') ?? 'drain-lease';
const specDir = join(ROOT, 'spec/tla', specName);
@@ -35,6 +41,7 @@ const catalog = JSON.parse(readFileSync(join(specDir, 'mutants.json'), 'utf8'))
const out = process.env.TLC_OUT ?? join(ROOT, '.context/tlc');
const timeoutS = Number(process.env.TLC_TIMEOUT_S ?? 300);
const failures: string[] = [];
+let skipped = 0, checked = 0;
const fail = (code: string, id: string, message: string) => { failures.push(`${code} ${id}: ${message}`); console.error(`FAIL ${code} ${id}: ${message}`); };
function applyOnce(text: string, edit: Edit): string | null {
@@ -62,6 +69,8 @@ function runSpec(m: Mutant, tla2tools: string): void {
'-config', catalog.config, '-metadir', join(work, 'states'), `${catalog.module}.tla`], { cwd: work, encoding: 'utf8', timeout: timeoutS * 1000 });
const log = `${r.stdout ?? ''}${r.stderr ?? ''}`;
writeFileSync(join(work, 'tlc.log'), log);
+ if (r.error) return fail('MUTANT_INCOMPLETE', m.id, `cannot run java (${(r.error as NodeJS.ErrnoException).code ?? r.error.message}); TLC did not start. Install Java 11 or later on PATH and rerun: bun scripts/spec/mutants.ts --only ${m.id} --spec-only`);
+ checked++;
const violated = log.match(/(?:Invariant|Action property|Property) (\w+) is violated/)?.[1];
const clean = log.includes('Model checking completed. No error has been found.');
if (m.expect === 'survives') {
@@ -86,14 +95,33 @@ function scratchCopy(): string {
return dir;
}
-interface TestRun { passed: number; failed: number; clean: boolean; tail: string }
-function bunTest(cwd: string, t: CodeTest): TestRun {
+/**
+ * One run of one test file, its full output kept in `log`. `outcome` is `passed` (exit 0, every selected test passed),
+ * `assertion` (exit 1, a selected test failed on an expect() and nothing else went wrong) or `broken`: a signal, a
+ * timeout, a spawn error, a load or bootstrap error, no test selected, or a failure that is not an assertion. Only
+ * `assertion` counts as a caught guard; `broken` proves nothing either way.
+ */
+interface TestRun { outcome: 'passed' | 'assertion' | 'broken'; why: string; log: string }
+function bunTest(cwd: string, t: CodeTest, log: string): TestRun {
const r = spawnSync('bun', ['test', '--timeout=300000', t.test, '-t', t.filter], { cwd, encoding: 'utf8', env: process.env, timeout: 600_000 });
const text = `${r.stdout ?? ''}${r.stderr ?? ''}`;
- const count = (word: string) => Number(text.match(new RegExp(`^\\s*(\\d+) ${word}\\b`, 'm'))?.[1] ?? 0);
- // A load or bootstrap failure is not a caught guard: the test must have run and failed on its own assertions.
- const clean = !/Unhandled error between tests|SyntaxError|Cannot find module|error: Unexpected/.test(text);
- return { passed: count('pass'), failed: count('fail'), clean, tail: text.split('\n').slice(-12).join('\n') };
+ writeFileSync(log, `$ bun test ${t.test} -t ${JSON.stringify(t.filter)}\nexit ${r.status} signal ${r.signal} error ${r.error?.message ?? 'none'}\n\n${text}`);
+ const lines = text.split('\n').map(line => line.trim());
+ const count = (...words: string[]) => {
+ const line = lines.find(l => words.includes(l.split(' ')[1] ?? '') && l.split(' ').length === 2 && Number.isInteger(Number(l.split(' ')[0])));
+ return line ? Number(line.split(' ')[0]) : 0;
+ };
+ const passed = count('pass'), failed = count('fail'), errors = count('error', 'errors');
+ const broken = (why: string): TestRun => ({ outcome: 'broken', why, log });
+ if (r.error) return broken(`bun did not run (${(r.error as NodeJS.ErrnoException).code ?? r.error.message})`);
+ if (r.signal) return broken(`bun was terminated by ${r.signal}`);
+ if (text.includes('timed out after')) return broken('a test timed out');
+ if (!lines.some(l => l.startsWith('Ran ') && l.includes(' across '))) return broken('bun printed no test summary');
+ if (errors > 0 || ['Unhandled error between tests', 'SyntaxError', 'Cannot find module'].some(s => text.includes(s))) return broken('the file did not load, or an error escaped between tests');
+ if (passed + failed === 0) return broken(`-t "${t.filter}" matched no test`);
+ if (r.status === 0 && failed === 0) return { outcome: 'passed', why: `${passed} passed`, log };
+ if (r.status === 1 && failed > 0 && text.includes('expect(')) return { outcome: 'assertion', why: `${failed} failed on an assertion`, log };
+ return broken(`exit ${r.status} with ${failed} failed, and no failed expect() in the output`);
}
function runCode(m: Mutant, tree: string): void {
@@ -101,7 +129,7 @@ function runCode(m: Mutant, tree: string): void {
const tests = code.tests.filter(t => {
if (!t.postgres || process.env.DATABASE_URL) return true;
if (flag('--require-postgres')) fail('MUTANT_INCOMPLETE', m.id, `${t.test} needs DATABASE_URL (a test Postgres)`);
- else console.log(`skip ${m.id} (code): ${t.test} needs DATABASE_URL`);
+ else { skipped++; console.log(`skip ${m.id} (code): ${t.test} needs DATABASE_URL`); }
return false;
});
if (tests.length === 0) return;
@@ -114,15 +142,18 @@ function runCode(m: Mutant, tree: string): void {
if (next === null) return fail('MUTANT_STALE', m.id, `code text not found exactly once in ${edit.file}: ${JSON.stringify(edit.before)}`);
mutated.set(edit.file, next);
}
- for (const t of tests) {
- const base = bunTest(tree, t);
- if (!base.clean || base.failed > 0 || base.passed === 0) return fail('MUTANT_INCOMPLETE', m.id, `${t.test} -t "${t.filter}" does not pass (or matched no test) without the edit:\n${base.tail}`);
+ const logs = join(out, `mutant-${specName}-${m.id}`);
+ mkdirSync(logs, { recursive: true });
+ for (const [i, t] of tests.entries()) {
+ checked++;
+ const base = bunTest(tree, t, join(logs, `test-${i}-without-edit.log`));
+ if (base.outcome !== 'passed') return fail('MUTANT_INCOMPLETE', m.id, `${t.test} -t "${t.filter}" must pass without the edit, but ${base.why}. Fix the test first. Log: ${base.log}`);
try {
for (const [file, text] of mutated) writeFileSync(join(tree, file), text);
- const under = bunTest(tree, t);
- if (!under.clean) return fail('MUTANT_INCOMPLETE', m.id, `${t.test} did not load or run with the edit, so it proves nothing:\n${under.tail}`);
- if (under.failed === 0) return fail('MUTANT_SURVIVED', m.id, `${t.test} -t "${t.filter}" passes with the guard removed from ${[...mutated.keys()].join(', ')}; the test no longer pins it`);
- console.log(`ok ${m.id} (code): ${t.test} fails with the guard removed from ${[...mutated.keys()].join(', ')}`);
+ const under = bunTest(tree, t, join(logs, `test-${i}-with-edit.log`));
+ if (under.outcome === 'broken') return fail('MUTANT_INCOMPLETE', m.id, `with the edit, ${under.why}, so the run proves nothing. Rerun once: bun scripts/spec/mutants.ts --only ${m.id} --code-only. Log: ${under.log}`);
+ if (under.outcome === 'passed') return fail('MUTANT_SURVIVED', m.id, `${t.test} -t "${t.filter}" passes with the guard removed from ${[...mutated.keys()].join(', ')}; the test no longer pins it. Log: ${under.log}`);
+ console.log(`ok ${m.id} (code): ${t.test} fails on an assertion with the guard removed from ${[...mutated.keys()].join(', ')}`);
} finally {
for (const [file, text] of originals) writeFileSync(join(tree, file), text);
}
@@ -133,5 +164,6 @@ const selected = catalog.mutants.filter(m => !only || m.id === only);
if (selected.length === 0) { console.error(`no mutant ${only} in ${specName}`); process.exit(2); }
if (!flag('--code-only')) { const tla2tools = jar(); for (const m of selected) runSpec(m, tla2tools); }
if (!flag('--spec-only') && selected.some(m => m.code)) { const tree = scratchCopy(); for (const m of selected) if (m.code) runCode(m, tree); }
+if (failures.length === 0 && checked === 0) { console.error(`MUTANT_NOTHING_CHECKED: every check the selection asked for was skipped (${skipped} Postgres code check(s) without DATABASE_URL); nothing was verified.`); process.exit(1); }
if (failures.length) { console.error(`\n${failures.length} mutant check(s) failed; spec/tla/README.md says what each code means.`); process.exit(1); }
-console.log(`\nall ${selected.length} mutant(s) behave as catalogued`);
+console.log(`\nall ${selected.length} mutant(s) behave as catalogued${skipped ? `; partial: ${skipped} Postgres code check(s) skipped (set DATABASE_URL to a test database, or pass --require-postgres to fail instead)` : ''}`);
diff --git a/spec/tla/README.md b/spec/tla/README.md
index 57acf562fa..961ab5a833 100644
--- a/spec/tla/README.md
+++ b/spec/tla/README.md
@@ -27,17 +27,24 @@ spec, its runner, the code a spec models, or a test a mutant names.
### When it fails
+Nothing here writes product files or data: TLC runs on copies under `TLC_OUT`, and code mutants run in a scratch
+copy of the tree there. A failed or interrupted run leaves only partial logs in `TLC_OUT`.
+
| Code | Meaning | What to do |
| --- | --- | --- |
-| `TLC_VIOLATION` | A property is violated. | Read the trace in the log; the action table below maps each step to code. Fix the code, or fix the spec when it is wrong and say why in the PR. |
-| `TLC_TIMEOUT` | TLC did not finish within `TLC_TIMEOUT_S` (600 s). | The state space grew: shrink the config's bounds, or find the action that multiplies states. |
-| `TLC_ERROR` | TLC stopped: a parse or semantic error, or out of memory. | The log names the line. |
-| `TLA_JAR_DOWNLOAD_FAILED` | The jar did not download. | Rerun. |
-| `TLA_JAR_SHA_MISMATCH` | The jar is not the reviewed release. | Never rerun; a new pin needs review. |
-| `MUTANT_STALE` | A mutant's `before` text is not found exactly once. | The spec or code moved: update the mutant's text in `mutants.json`. |
+| `TLC_VIOLATION` | A property is violated. | Read the trace in the log; the action table below maps each step to code. Fix the code, or fix the spec when it is wrong and say why in the PR. Never rerun to make it pass. |
+| `TLC_TIMEOUT` | TLC did not finish within `TLC_TIMEOUT_S` (600 s). | If the spec or a config changed, find the action that multiplies states. Shrinking a config's bounds reduces what is checked: that needs review in the PR. If nothing changed, the runner was slow: rerun once, then report it. |
+| `TLC_ERROR` | TLC stopped without a result: a parse or semantic error (the log names the line), out of memory, or killed. | Fix the error the log names; for memory, raise `-Xmx` in `TLC_JAVA_OPTS`. A killed run may be rerun once. |
+| `TLA_JAVA_MISSING` | No `java` on `PATH`. | Install Java 11 or later. |
+| `TLA_JAR_MISSING` | `TLA2TOOLS` names a file that does not exist. | Point it at the jar, or unset it to download the pinned one. |
+| `TLA_JAR_DOWNLOAD_FAILED` | The jar did not download. | A network failure: rerun once. |
+| `TLA_JAR_SHA_MISMATCH` | The jar is not the reviewed release. | Never rerun or bypass it; a new pin in `tla-tools.env` needs review. |
+| `MUTANT_USAGE` | An unknown flag, or `--spec-only` with `--code-only`. | Fix the command. |
+| `MUTANT_STALE` | A mutant's `before` text is not found exactly once. | The spec or code moved: update the mutant's text in `mutants.json` to the new line that implements the same guard. |
| `MUTANT_SURVIVED` | No property caught a model mutant, or the named test passed with the guard removed from the code. | A property or a test stopped pinning the guard. Restore it; never delete the mutant to go green. |
| `MUTANT_WRONG_PROPERTY` | A different property caught it, or a survivor became load-bearing. | Read the trace; update `expect`, and give a new load-bearing guard a code edit and a test. |
-| `MUTANT_INCOMPLETE` | TLC did not finish, or a named test failed without the edit, matched no test, or did not load with it. | Fix the test or the filter first; a failure that is not the test's own assertion proves nothing. |
+| `MUTANT_INCOMPLETE` | TLC did not finish or did not start; a named test failed without the edit or matched no test; or with the edit the test did not end in a clean assertion failure (a signal, a timeout, a load error, an error between tests). | Read the log the message names. Fix the test or filter first; a run that did not fail on the test's own assertion proves nothing. An interrupted run may be rerun once with the printed command. |
+| `MUTANT_NOTHING_CHECKED` | Every check the selection asked for was skipped (a Postgres test without `DATABASE_URL`). | Point `DATABASE_URL` at a test database (`GBRAIN_TEST_ALLOW_DATABASE_URL=1`). |
## drain-lease: the managed-sync drain lease
@@ -120,8 +127,10 @@ Postgres only: PGLite is one connection and serializes everything. Also left out
`LEASE_BEACON_QUEUE` holder record older binaries see (`test/persistence-sync-lease.test.ts`,
`test/minions-lease-beacon-sweeps-7261.test.ts`); a follower's wait and `drain_stalled` (`test/persistence-sync-lease.test.ts`);
reentrant passes under one lease; older binaries that take no lock; side effects outside the database; how the
-local monotonic clock behaves across a machine suspend; and a coordinator in another process that keeps a pass-stamp
-transaction open after the holder crashed (its share lock delays a takeover; its token check still holds).
+local monotonic clock behaves across a machine suspend; a coordinator in another process that keeps a pass-stamp
+transaction open after the holder crashed (its share lock delays a takeover; its token check still holds); and table
+locks on `gbrain_cycle_locks` (writer activation, admin intents, skill activation), which hold every renewal for the
+length of their transaction, so they count against the renewal timeout like a long fenced transaction.
### Known gaps