mirror of
https://github.com/garrytan/gbrain.git
synced 2026-07-30 11:22:34 +00:00
Fixes the "toggle enabled true, run, toggle back to false" workaround that gbrain doctor's extract_atoms_backlog message implicitly recommends and that #2860's reporter had to script around: with an external orchestrator running `gbrain dream --phase patterns` on a cadence outside the autopilot, the only way to run patterns once was `config set dream.patterns.enabled true` -> run -> `config set ... false`. A crash between steps left the flag stuck true, and the autopilot (which polls the same flag) re-enqueued patterns every cycle -- 119 LLM jobs / ~$400 over 24h before it was caught. Root cause: `--phase X` only controls which phase FUNCTION cycle.ts calls; it does not bypass that phase's own `dream.<phase>.enabled` / `cycle.<phase>.enabled` config read. Each gated phase (patterns, synthesize, conversation_facts_backfill, enrich_thin, skillopt) reads its enabled flag internally and skips regardless of how the phase was selected -- confirmed by reading each phase module, not assumed. extract_atoms/synthesize_concepts are a DIFFERENT mechanism entirely (pack-declaration via packDeclaresPhase, not a config .enabled read) and already have a working one-shot escape hatch: `--drain`. The existing doctor message for extract_atoms already says `--phase extract_atoms --drain --window 120`, so no doctor text needed updating there -- verified by reading src/commands/doctor.ts directly rather than assuming the paraphrase in the issue was literal. Design: `gbrain dream --phase <name> --once`. Requires an explicit --phase (bare --once is a usage error, exit 2) so it can never force-enable every disabled phase at once in a full/default cycle -- that would recreate the same unbounded-spend risk the flag exists to prevent. Threaded through CycleOpts as `onceForPhase?: CyclePhase` (the literal phase name, not a boolean) so the bypass can never leak to a phase other than the one named, even if a future programmatic caller passes a wider `phases` array than the CLI does. Never reads or writes config -- the phase still evaluates its .enabled gate every call; --once only overrides the boolean OUTCOME for that one invocation, mirroring the existing --unsafe-bypass-dream-guard / --input precedents (stderr warning at the bypass point, no new config-touching code path). Rejected alternatives (documented per task instructions): - Making explicit --phase X always bypass .enabled: breaking change for existing crons that rely on the disabled flag as a cheap no-op; an upgrade would silently start running LLM/write phases. - A new subcommand: adds a whole dispatch/help/arg surface that internally routes through the same override anyway. - Extending --once to also bypass packDeclaresPhase for extract_atoms/synthesize_concepts: conflates two different gating mechanisms (config toggle vs. pack membership) under one flag; extract_atoms already has --drain, which is purpose-built for its batched/windowed execution model. Design was cross-validated by an independent second-model review (external design consultation) before implementation; its recommendation to also update the extract_atoms doctor message to `--once` was NOT adopted because that phase has no .enabled gate to bypass -- doing so would be a documented no-op, contradicted by reading src/commands/doctor.ts:3264 directly. Tests: 9 new (structural CLI-flag wiring in dream-cli-flags.test.ts; a real PGLite E2E test in dream-patterns-pglite.test.ts proving the bypass fires AND that dream.patterns.enabled is never written; 4 runCycle-level tests in cycle.serial.test.ts proving onceForPhase does not leak across phases). Verified 6 of 9 fail against the pre-fix source (via git stash of source-only changes) to confirm they're meaningful regressions, not tautologies. Closes #2860 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
163 lines
6.3 KiB
TypeScript
163 lines
6.3 KiB
TypeScript
/**
|
|
* Structural tests for `gbrain dream` argv parsing (v0.21).
|
|
*
|
|
* Verifies the help text + parser source contains the new flags
|
|
* (--input, --date, --from, --to) and that conflict detection is wired.
|
|
* The actual parseArgs is internal; we exercise it via the source file
|
|
* structure to avoid spinning up a process per test.
|
|
*/
|
|
|
|
import { describe, test, expect } from 'bun:test';
|
|
import { readFileSync } from 'fs';
|
|
|
|
const dreamSrc = readFileSync(new URL('../src/commands/dream.ts', import.meta.url), 'utf-8');
|
|
|
|
describe('dream CLI flag wiring', () => {
|
|
test('declares --input flag with file argument', () => {
|
|
expect(dreamSrc).toContain("'--input'");
|
|
expect(dreamSrc).toContain('inputFile');
|
|
});
|
|
|
|
test('declares --date / --from / --to flags', () => {
|
|
expect(dreamSrc).toContain("'--date'");
|
|
expect(dreamSrc).toContain("'--from'");
|
|
expect(dreamSrc).toContain("'--to'");
|
|
});
|
|
|
|
test('validates ISO date format', () => {
|
|
expect(dreamSrc).toMatch(/ISO_DATE_RE/);
|
|
expect(dreamSrc).toContain('YYYY-MM-DD');
|
|
});
|
|
|
|
test('--input + --date conflict detection', () => {
|
|
expect(dreamSrc).toContain('--input cannot be combined with --date');
|
|
});
|
|
|
|
test('--input implies --phase synthesize', () => {
|
|
expect(dreamSrc).toContain("phase = 'synthesize'");
|
|
});
|
|
|
|
test('--from > --to range validation', () => {
|
|
expect(dreamSrc).toContain('empty range');
|
|
});
|
|
|
|
test('forwards synth fields to runCycle', () => {
|
|
expect(dreamSrc).toContain('synthInputFile');
|
|
expect(dreamSrc).toContain('synthDate');
|
|
expect(dreamSrc).toContain('synthFrom');
|
|
expect(dreamSrc).toContain('synthTo');
|
|
});
|
|
|
|
test('totals line includes synth + patterns counters', () => {
|
|
expect(dreamSrc).toContain('synth_transcripts');
|
|
expect(dreamSrc).toContain('synth_pages');
|
|
expect(dreamSrc).toContain('patterns=');
|
|
});
|
|
|
|
test('help text documents dry-run synthesis semantics (Codex finding #8)', () => {
|
|
expect(dreamSrc).toContain('skips the Sonnet');
|
|
expect(dreamSrc.toLowerCase()).toContain('zero llm calls');
|
|
});
|
|
|
|
// v0.41.13: --source / --source-id flag wiring (supersedes PR #1559).
|
|
// Structural-only tests; behavioral tests live in test/dream.test.ts.
|
|
describe('--source / --source-id wiring (v0.41.13)', () => {
|
|
test('declares --source flag in argv parsing', () => {
|
|
expect(dreamSrc).toContain("'--source'");
|
|
});
|
|
|
|
test('declares --source-id alias in argv parsing', () => {
|
|
expect(dreamSrc).toContain("'--source-id'");
|
|
});
|
|
|
|
test('forwards resolved sourceId to runCycle', () => {
|
|
// The runCycle call must pass sourceId; gate name "sourceId"
|
|
// not "source" because CycleOpts.sourceId is the contract.
|
|
expect(dreamSrc).toMatch(/sourceId:\s*resolvedSourceId/);
|
|
});
|
|
|
|
test('imports resolveSourceId from canonical source-resolver helper', () => {
|
|
expect(dreamSrc).toContain("from '../core/source-resolver.ts'");
|
|
expect(dreamSrc).toContain('resolveSourceId');
|
|
});
|
|
|
|
test('declares isResolverUserError predicate for typed-error catch (T3 from eng review)', () => {
|
|
expect(dreamSrc).toContain('function isResolverUserError');
|
|
});
|
|
|
|
test('documents --source in --help output', () => {
|
|
expect(dreamSrc).toContain('--source <id>');
|
|
expect(dreamSrc).toContain('--source-id <id>');
|
|
});
|
|
|
|
test('preserves --help short-circuit ordering comment (IRON RULE)', () => {
|
|
// The comment lives in runDream BEFORE the engine-null gate.
|
|
// Future refactors that reorder these blocks will trip this guard.
|
|
expect(dreamSrc).toContain('IRON RULE: --help short-circuits BEFORE');
|
|
});
|
|
|
|
test('declares engine-null guard for --source', () => {
|
|
expect(dreamSrc).toContain('requires a connected brain');
|
|
});
|
|
|
|
test('declares archived-source guard', () => {
|
|
expect(dreamSrc).toMatch(/source.*is archived/);
|
|
expect(dreamSrc).toContain('gbrain sources restore');
|
|
});
|
|
});
|
|
|
|
// issue #1678 — --drain bounded backlog drain wiring (structural).
|
|
describe('--drain wiring', () => {
|
|
test('declares --drain and --window flags', () => {
|
|
expect(dreamSrc).toContain("'--drain'");
|
|
expect(dreamSrc).toContain("'--window'");
|
|
expect(dreamSrc).toContain('windowSeconds');
|
|
});
|
|
|
|
test('--drain defaults to extract_atoms and rejects other phases', () => {
|
|
expect(dreamSrc).toContain("phase = 'extract_atoms'");
|
|
expect(dreamSrc).toContain('--drain currently supports only --phase extract_atoms');
|
|
});
|
|
|
|
test('drain routes through the shared helper with the resolved source (5A)', () => {
|
|
// v0.42.10.0 (#1685 GAP D / 5A): the lock+batch+count wiring moved into
|
|
// runExtractAtomsDrainForSource so the CLI, the Minion handler, and
|
|
// autopilot share ONE drain path. dream threads resolvedSourceId so the
|
|
// helper picks cycleLockIdFor(resolvedSourceId) — the same lock the routine
|
|
// cycle holds for that source. The lock-id contract is now pinned in
|
|
// test/extract-atoms-drain.test.ts ("shared wiring helper holds the cycle lock").
|
|
expect(dreamSrc).toContain('runExtractAtomsDrainForSource');
|
|
expect(dreamSrc).toContain('sourceId: resolvedSourceId');
|
|
});
|
|
|
|
test('drain reports remaining + exits non-zero when incomplete', () => {
|
|
expect(dreamSrc).toContain('EXIT_DRAIN_INCOMPLETE');
|
|
expect(dreamSrc).toContain('cycle_already_running');
|
|
});
|
|
});
|
|
|
|
// issue #2860 — --once one-shot phase-enabled-gate bypass (structural).
|
|
// Behavioral coverage: test/e2e/dream-patterns-pglite.test.ts (bypass +
|
|
// config-untouched) and test/core/cycle.serial.test.ts (non-leak across
|
|
// phases via CycleOpts.onceForPhase).
|
|
describe('--once wiring (issue #2860)', () => {
|
|
test('declares --once flag', () => {
|
|
expect(dreamSrc).toContain("'--once'");
|
|
});
|
|
|
|
test('rejects bare --once with no --phase (exit 2)', () => {
|
|
expect(dreamSrc).toContain('--once requires --phase <name>');
|
|
expect(dreamSrc).toContain('if (once && !phase)');
|
|
});
|
|
|
|
test('threads onceForPhase to runCycle, gated on opts.once', () => {
|
|
expect(dreamSrc).toMatch(/onceForPhase:\s*opts\.once\s*\?\s*opts\.phase!\s*:\s*undefined/);
|
|
});
|
|
|
|
test('documents --once in --help output', () => {
|
|
expect(dreamSrc).toContain('--once');
|
|
expect(dreamSrc).toContain('Never reads or writes config');
|
|
});
|
|
});
|
|
});
|