Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(dream): any failed phase exits 1; a missing embedding key warns instead of failing (E4)

garrytan8a77966f1200
diff --git a/src/commands/dream.ts b/src/commands/dream.ts
index bcad65f3e8..aef6337854 100644
--- a/src/commands/dream.ts
+++ b/src/commands/dream.ts
@@ -879,10 +879,15 @@ export async function runDream(engine: BrainEngine | null, args: string[]): Prom
printHuman(report);
}
- // Exit non-zero when the cycle failed overall (helps cron spot real problems).
- // 'partial' is not a failure — it means some phase warned but the cycle ran —
- // except when a phase threw and was contained so later phases could run.
- if (report.status === 'failed' || report.phases.some(p => p.details?.contained === true)) {
+ // Exit non-zero when the cycle failed overall or any phase failed (agent-first
+ // operator wave E4): a 'partial' cycle whose phase reports 'fail' — thrown and
+ // contained, or caught inside the runner — is never success. Warn-only
+ // 'partial' cycles still exit 0.
+ const failedPhases = report.phases.filter(p => p.status === 'fail').map(p => p.phase);
+ if (report.status === 'failed' || failedPhases.length > 0) {
+ if (!opts.json && failedPhases.length > 0) {
+ console.error(`Dream failed: ${failedPhases.length} phase(s) failed (${failedPhases.join(', ')}); see the phase errors above. After fixing the cause, re-run one with: gbrain dream --phase ${failedPhases[0]}`);
+ }
process.exit(1);
}
diff --git a/src/core/cycle.ts b/src/core/cycle.ts
index 1cd15dbb9c..3be393d1ea 100644
--- a/src/core/cycle.ts
+++ b/src/core/cycle.ts
@@ -1618,6 +1618,8 @@ async function runPhaseEmbed(engine: BrainEngine, dryRun: boolean, signal?: Abor
},
};
} catch (e) {
+ const { EmbeddingCredentialError } = await import('./embed-preflight.ts');
+ if (e instanceof EmbeddingCredentialError) return { phase: 'embed', status: 'warn', duration_ms: 0, summary: `embeddings not run: ${e.message.split('\n')[0]}`, details: { reason: 'embedding_unconfigured' } };
return {
phase: 'embed',
status: 'fail',
diff --git a/test/dream-env-source-scope.test.ts b/test/dream-env-source-scope.test.ts
index 392ff4cef2..161e6a9956 100644
--- a/test/dream-env-source-scope.test.ts
+++ b/test/dream-env-source-scope.test.ts
@@ -23,6 +23,7 @@ import { ALL_PHASES } from '../src/core/cycle.ts';
import { resetPgliteState } from './helpers/reset-pglite.ts';
import { runDream } from '../src/commands/dream.ts';
import { withEnv } from './helpers/with-env.ts';
+import { makeGitFixture } from './helpers/git-fixture.ts';
let engine: PGLiteEngine;
let dirA: string;
@@ -147,6 +148,8 @@ describe('gbrain dream honors GBRAIN_SOURCE like --source (#4778)', () => {
}, 60_000);
test('GBRAIN_SOURCE naming the sole non-default source keeps the full implicit cycle (#4700 no-regression)', async () => {
+ // A real checkout: the full cycle's sync phase must not fail (dream exits 1 on any failed phase).
+ await makeGitFixture(dirA);
await seedSource('source-a', dirA);
await withEnv({ GBRAIN_HOME: gbrainHome, GBRAIN_SOURCE: 'source-a' }, async () => {
const report = await runDream(engine, ['--dry-run', '--json']);
diff --git a/test/dream-failed-phase-exit.test.ts b/test/dream-failed-phase-exit.test.ts
new file mode 100644
index 0000000000..244abd2400
--- /dev/null
+++ b/test/dream-failed-phase-exit.test.ts
@@ -0,0 +1,50 @@
+/**
+ * Lane E4 (agent-first operator wave): `gbrain dream` propagates phase errors.
+ * A cycle where one phase reports `fail` and another succeeds derives
+ * `partial`; before this fix that exited 0 (success) unless the failure was a
+ * contained throw. Any failed phase now exits 1.
+ */
+import { afterAll, beforeAll, expect, spyOn, test } from 'bun:test';
+import { mkdtempSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { PGLiteEngine } from '../src/core/pglite-engine.ts';
+import { runDream } from '../src/commands/dream.ts';
+import { withEnv } from './helpers/with-env.ts';
+
+let engine: PGLiteEngine;
+let home: string;
+let notGit: string;
+
+beforeAll(async () => {
+ engine = new PGLiteEngine();
+ await engine.connect({});
+ await engine.initSchema();
+ home = mkdtempSync(join(tmpdir(), 'gbrain-dream-fail-home-'));
+ notGit = mkdtempSync(join(tmpdir(), 'gbrain-dream-fail-src-'));
+ await engine.executeRaw('INSERT INTO sources(id,name,local_path) VALUES($1,$1,$2)', ['user-notes-example', notGit]);
+}, 120_000);
+afterAll(async () => {
+ await engine.disconnect();
+ rmSync(home, { recursive: true, force: true });
+ rmSync(notGit, { recursive: true, force: true });
+});
+
+test('a partial cycle with a failed phase exits 1', () => withEnv({ GBRAIN_HOME: home }, async () => {
+ const exit = spyOn(process, 'exit').mockImplementation(((code?: number) => { throw new Error(`EXIT ${code}`); }) as never);
+ const log = spyOn(console, 'log').mockImplementation(() => {});
+ const err = spyOn(console, 'error').mockImplementation(() => {});
+ try {
+ await expect(runDream(engine, ['--dir', notGit, '--phase', 'sync', '--phase', 'backlinks', '--json'])).rejects.toThrow('EXIT 1');
+ const printed = log.mock.calls.map(c => String(c[0])).join('\n');
+ const report = JSON.parse(printed.slice(printed.indexOf('{')));
+ expect(report.status).toBe('partial');
+ expect(report.phases.find((p: { phase: string }) => p.phase === 'sync').status).toBe('fail');
+ expect(report.phases.find((p: { phase: string }) => p.phase === 'backlinks').status).toBe('ok');
+ expect(exit).toHaveBeenCalledWith(1);
+ } finally {
+ exit.mockRestore();
+ log.mockRestore();
+ err.mockRestore();
+ }
+}), 120_000);
diff --git a/test/dream-keyless-exit.test.ts b/test/dream-keyless-exit.test.ts
index 222ea8cea6..4ca1f33495 100644
--- a/test/dream-keyless-exit.test.ts
+++ b/test/dream-keyless-exit.test.ts
@@ -20,7 +20,7 @@
* 'embedding_disabled', no provider call and no phase error;
* - the report status is still 'partial' on this fixture because lint
* and orphans warn on the one-page seed brain, never 'failed', and
- * `gbrain dream` exits 1 only on 'failed' (src/commands/dream.ts).
+ * `gbrain dream` exits 1 only on 'failed' or a failed phase (src/commands/dream.ts).
*
* Test shape mirrors test/agent-scheduler-contract.serial.test.ts (keyless
* PGLite brain, real CLI spawn, exit-code assertions). Non-serial on