Files
openclaw/src/agents/cli-runner.binding-flush.test.ts
Brad Reaves b752384586 fix(agents): recover claude-cli warm-stdin continuity when no native transcript is written (reseed candidate not dropped) (#96841)
* fix(agents): recover claude-cli warm-stdin continuity when no native transcript is written

The headless warm-stdin claude-cli backend (liveSession: "claude-stdio")
never writes a native transcript, so the post-turn flush probe always
fails and the missing-transcript reuse path drops the bound session id.

Part 1 (cli-runner.ts): scope the non-destructive binding behavior to
warm-stdin sessions so they keep their binding instead of clearing it
every turn.

Part 2 (attempt-execution.ts): on a missing transcript, clear the stored
binding (no stale --resume) but still return the bound id as the reuse
candidate so prepare can re-detect the missing transcript and arm
raw-transcript reseed. Returning undefined starved reseed and lost
warm-stdin continuity.

Adds/updates regression coverage in attempt-execution.cli.test.ts and a
complementary reseed test in prepare.test.ts.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* test(agents): align warm-session continuity coverage

* test(agents): keep cli live-session mock complete

* fix(agents): respect stateless CLI session mode

* style(agents): keep session candidate guard focused

* test(agents): preserve minimal CLI runner fixtures

* fix(agents): preserve exact Claude warm sessions

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: Peter Steinberger <steipete@gmail.com>
2026-07-10 03:48:18 +01:00

134 lines
4.8 KiB
TypeScript

/** Tests bounded transcript-flush probing before reusing CLI bindings. */
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import {
isCliBindingFlushed,
restoreCliRunnerTestDeps,
setCliRunnerTestDeps,
} from "./cli-runner.js";
describe("isCliBindingFlushed", () => {
const workspaceDir = "/tmp/openclaw-workspace";
beforeEach(() => {
vi.useRealTimers();
restoreCliRunnerTestDeps();
});
afterEach(() => {
vi.clearAllTimers();
vi.useRealTimers();
restoreCliRunnerTestDeps();
});
it("returns false when no sessionId is provided", async () => {
const probe = vi.fn(async () => true);
setCliRunnerTestDeps({ claudeCliSessionTranscriptHasContent: probe });
expect(await isCliBindingFlushed(undefined, "claude-cli")).toBe(false);
expect(probe).not.toHaveBeenCalled();
});
it("returns true when the transcript has content on the first probe", async () => {
const probe = vi.fn(async () => true);
setCliRunnerTestDeps({ claudeCliSessionTranscriptHasContent: probe });
expect(await isCliBindingFlushed("sid-fresh", "claude-cli", workspaceDir)).toBe(true);
expect(probe).toHaveBeenCalledTimes(1);
expect(probe).toHaveBeenCalledWith({ sessionId: "sid-fresh", workspaceDir });
});
it("retries up to three times before giving up", async () => {
const delay = vi.fn(async () => undefined);
const probe = vi.fn(async () => false);
setCliRunnerTestDeps({ claudeCliSessionTranscriptHasContent: probe, delay });
expect(await isCliBindingFlushed("sid-cold", "claude-cli", workspaceDir)).toBe(false);
expect(probe).toHaveBeenCalledTimes(3);
expect(delay).toHaveBeenCalledTimes(2);
expect(delay).toHaveBeenNthCalledWith(1, 50);
expect(delay).toHaveBeenNthCalledWith(2, 150);
});
it("succeeds when the transcript becomes visible on a later retry", async () => {
const delay = vi.fn(async () => undefined);
let calls = 0;
const probe = vi.fn(async () => {
calls += 1;
return calls >= 2;
});
setCliRunnerTestDeps({ claudeCliSessionTranscriptHasContent: probe, delay });
expect(await isCliBindingFlushed("sid-late", "claude-cli", workspaceDir)).toBe(true);
expect(probe).toHaveBeenCalledTimes(2);
expect(delay).toHaveBeenCalledExactlyOnceWith(50);
});
it("schedules at most 0 + 50 + 150ms of delay across the bounded retry", async () => {
vi.useFakeTimers();
try {
// Fake timers enforce the retry contract without introducing wall-clock
// sleeps into this import-heavy agent test.
const probe = vi.fn(async () => false);
setCliRunnerTestDeps({ claudeCliSessionTranscriptHasContent: probe });
const settled = vi.fn();
const errored = vi.fn();
isCliBindingFlushed("sid-bounded", "claude-cli", workspaceDir).then(settled, errored);
await vi.advanceTimersByTimeAsync(0);
await vi.advanceTimersByTimeAsync(50);
await vi.advanceTimersByTimeAsync(150);
expect(settled).toHaveBeenCalledTimes(1);
expect(settled.mock.calls[0]?.[0]).toBe(false);
expect(errored).not.toHaveBeenCalled();
expect(probe).toHaveBeenCalledTimes(3);
} finally {
vi.clearAllTimers();
vi.useRealTimers();
}
});
it("returns true without probing for non-claude-cli providers", async () => {
const probe = vi.fn(async () => false);
setCliRunnerTestDeps({ claudeCliSessionTranscriptHasContent: probe });
expect(await isCliBindingFlushed("sid-codex", "codex-cli")).toBe(true);
expect(await isCliBindingFlushed("sid-anthropic", "anthropic")).toBe(true);
expect(await isCliBindingFlushed("sid-openai", "openai")).toBe(true);
expect(probe).not.toHaveBeenCalled();
});
it("returns true without probing when provider is undefined", async () => {
const probe = vi.fn(async () => false);
setCliRunnerTestDeps({ claudeCliSessionTranscriptHasContent: probe });
expect(await isCliBindingFlushed("sid-x", undefined)).toBe(true);
expect(probe).not.toHaveBeenCalled();
});
it("returns true without probing when the caller owns continuity outside native transcripts", async () => {
const probe = vi.fn(async () => false);
setCliRunnerTestDeps({ claudeCliSessionTranscriptHasContent: probe });
expect(
await isCliBindingFlushed("sid-warm", "claude-cli", workspaceDir, {
skipTranscriptProbe: true,
}),
).toBe(true);
expect(probe).not.toHaveBeenCalled();
});
it("still probes when transcript-probe skipping is disabled", async () => {
const probe = vi.fn(async () => true);
setCliRunnerTestDeps({ claudeCliSessionTranscriptHasContent: probe });
expect(
await isCliBindingFlushed("sid-probe", "claude-cli", workspaceDir, {
skipTranscriptProbe: false,
}),
).toBe(true);
expect(probe).toHaveBeenCalledTimes(1);
});
});