Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e6f78bed1e | ||
|
|
50906db339 | ||
|
|
dd987f5474 | ||
|
|
8c572b038d |
No files matched your search
@@ -54,12 +54,11 @@ class FakeClaudeQuery implements AsyncIterable<SDKMessage> {
|
||||
private done = false;
|
||||
private failure: unknown | undefined;
|
||||
|
||||
public readonly interruptCalls: Array<void> = [];
|
||||
public readonly stopTaskCalls: Array<string> = [];
|
||||
public readonly setModelCalls: Array<string | undefined> = [];
|
||||
public readonly setPermissionModeCalls: Array<string> = [];
|
||||
public readonly setMaxThinkingTokensCalls: Array<number | null> = [];
|
||||
public closeCalls = 0;
|
||||
public closeError: unknown | undefined;
|
||||
|
||||
emit(message: SDKMessage): void {
|
||||
if (this.done) {
|
||||
@@ -95,14 +94,6 @@ class FakeClaudeQuery implements AsyncIterable<SDKMessage> {
|
||||
}
|
||||
}
|
||||
|
||||
readonly interrupt = async (): Promise<void> => {
|
||||
this.interruptCalls.push(undefined);
|
||||
};
|
||||
|
||||
readonly stopTask = async (taskId: string): Promise<void> => {
|
||||
this.stopTaskCalls.push(taskId);
|
||||
};
|
||||
|
||||
readonly setModel = async (model?: string): Promise<void> => {
|
||||
this.setModelCalls.push(model);
|
||||
};
|
||||
@@ -117,6 +108,9 @@ class FakeClaudeQuery implements AsyncIterable<SDKMessage> {
|
||||
|
||||
readonly close = (): void => {
|
||||
this.closeCalls += 1;
|
||||
if (this.closeError !== undefined) {
|
||||
throw this.closeError;
|
||||
}
|
||||
this.finish();
|
||||
};
|
||||
|
||||
@@ -1580,7 +1574,7 @@ describe("ClaudeAdapterLive", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it.effect("interruptTurn settles every acknowledged live task before interrupting", () => {
|
||||
it.effect("interruptTurn settles live tasks and closes the provider session", () => {
|
||||
const harness = makeHarness();
|
||||
return Effect.gen(function* () {
|
||||
const adapter = yield* ClaudeAdapter;
|
||||
@@ -1645,9 +1639,12 @@ describe("ClaudeAdapterLive", () => {
|
||||
);
|
||||
yield* adapter.interruptTurn(session.threadId);
|
||||
|
||||
// Only the still-live task is stopped; interrupt always fires after.
|
||||
assert.deepEqual(harness.query.stopTaskCalls, ["task-live"]);
|
||||
assert.equal(harness.query.interruptCalls.length, 1);
|
||||
// Closing the session is the hard stop because SDK interrupt can leave
|
||||
// resumed background work alive.
|
||||
assert.equal(harness.query.closeCalls, 1);
|
||||
|
||||
const sessions = yield* adapter.listSessions();
|
||||
assert.equal(sessions.length, 0);
|
||||
|
||||
const stoppedTaskEvents = Array.from(yield* Fiber.join(stoppedTaskEventFiber));
|
||||
assert.equal(stoppedTaskEvents.length, 1);
|
||||
@@ -1665,6 +1662,172 @@ describe("ClaudeAdapterLive", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it.effect("keeps the session available when process close fails", () => {
|
||||
const harness = makeHarness();
|
||||
return Effect.gen(function* () {
|
||||
const adapter = yield* ClaudeAdapter;
|
||||
const session = yield* adapter.startSession({
|
||||
threadId: THREAD_ID,
|
||||
provider: ProviderDriverKind.make("claudeAgent"),
|
||||
runtimeMode: "full-access",
|
||||
});
|
||||
harness.query.closeError = new Error("close failed");
|
||||
|
||||
const result = yield* adapter.interruptTurn(session.threadId).pipe(Effect.result);
|
||||
|
||||
assert.equal(result._tag, "Failure");
|
||||
if (result._tag === "Failure") {
|
||||
assert.equal(result.failure._tag, "ProviderAdapterProcessError");
|
||||
}
|
||||
assert.equal(harness.query.closeCalls, 1);
|
||||
assert.equal(yield* adapter.hasSession(session.threadId), true);
|
||||
assert.equal((yield* adapter.listSessions())[0]?.status, "ready");
|
||||
}).pipe(
|
||||
Effect.provideService(Random.Random, makeDeterministicRandomService()),
|
||||
Effect.provide(harness.layer),
|
||||
);
|
||||
});
|
||||
|
||||
it.effect("stopAll attempts every session when one process close fails", () => {
|
||||
const queries: FakeClaudeQuery[] = [];
|
||||
const layer = Layer.effect(
|
||||
ClaudeAdapter,
|
||||
Effect.gen(function* () {
|
||||
const claudeConfig = decodeClaudeSettings({});
|
||||
return yield* makeClaudeAdapter(claudeConfig, {
|
||||
createQuery: () => {
|
||||
const query = new FakeClaudeQuery();
|
||||
queries.push(query);
|
||||
return query;
|
||||
},
|
||||
});
|
||||
}),
|
||||
).pipe(
|
||||
Layer.provideMerge(ServerConfig.layerTest("/tmp/claude-adapter-test", "/tmp")),
|
||||
Layer.provideMerge(ServerSettingsService.layerTest()),
|
||||
Layer.provideMerge(NodeServices.layer),
|
||||
);
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const adapter = yield* ClaudeAdapter;
|
||||
yield* adapter.startSession({
|
||||
threadId: THREAD_ID,
|
||||
provider: ProviderDriverKind.make("claudeAgent"),
|
||||
runtimeMode: "full-access",
|
||||
});
|
||||
yield* adapter.startSession({
|
||||
threadId: RESUME_THREAD_ID,
|
||||
provider: ProviderDriverKind.make("claudeAgent"),
|
||||
runtimeMode: "full-access",
|
||||
});
|
||||
const firstQuery = queries[0];
|
||||
if (!firstQuery) {
|
||||
return;
|
||||
}
|
||||
firstQuery.closeError = new Error("close failed");
|
||||
|
||||
const result = yield* adapter.stopAll().pipe(Effect.result);
|
||||
|
||||
assert.equal(result._tag, "Failure");
|
||||
assert.equal(queries[0]?.closeCalls, 1);
|
||||
assert.equal(queries[1]?.closeCalls, 1);
|
||||
assert.equal(yield* adapter.hasSession(THREAD_ID), true);
|
||||
assert.equal(yield* adapter.hasSession(RESUME_THREAD_ID), false);
|
||||
}).pipe(
|
||||
Effect.provideService(Random.Random, makeDeterministicRandomService()),
|
||||
Effect.provide(layer),
|
||||
);
|
||||
});
|
||||
|
||||
it.effect("keeps a resumed replacement session during slow stop cleanup", () => {
|
||||
const queries: FakeClaudeQuery[] = [];
|
||||
let signalUsageStarted: () => void = () => undefined;
|
||||
const usageStarted = new Promise<void>((resolve) => {
|
||||
signalUsageStarted = resolve;
|
||||
});
|
||||
const layer = Layer.effect(
|
||||
ClaudeAdapter,
|
||||
Effect.gen(function* () {
|
||||
const claudeConfig = decodeClaudeSettings({});
|
||||
return yield* makeClaudeAdapter(claudeConfig, {
|
||||
createQuery: () => {
|
||||
const query = new FakeClaudeQuery();
|
||||
if (queries.length === 0) {
|
||||
Object.assign(query, {
|
||||
getContextUsage: async () => {
|
||||
signalUsageStarted();
|
||||
return await new Promise<never>(() => undefined);
|
||||
},
|
||||
});
|
||||
}
|
||||
queries.push(query);
|
||||
return query;
|
||||
},
|
||||
});
|
||||
}),
|
||||
).pipe(
|
||||
Layer.provideMerge(ServerConfig.layerTest("/tmp/claude-adapter-test", "/tmp")),
|
||||
Layer.provideMerge(ServerSettingsService.layerTest()),
|
||||
Layer.provideMerge(NodeServices.layer),
|
||||
);
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const adapter = yield* ClaudeAdapter;
|
||||
const runtimeEventsFiber = yield* Stream.take(adapter.streamEvents, 8).pipe(
|
||||
Stream.runCollect,
|
||||
Effect.forkChild,
|
||||
);
|
||||
const firstSession = yield* adapter.startSession({
|
||||
threadId: THREAD_ID,
|
||||
provider: ProviderDriverKind.make("claudeAgent"),
|
||||
runtimeMode: "full-access",
|
||||
});
|
||||
yield* adapter.sendTurn({
|
||||
threadId: firstSession.threadId,
|
||||
input: "hello",
|
||||
attachments: [],
|
||||
});
|
||||
|
||||
const interruptFiber = yield* adapter
|
||||
.interruptTurn(firstSession.threadId)
|
||||
.pipe(Effect.forkChild);
|
||||
yield* Effect.promise(() => usageStarted);
|
||||
assert.equal(queries[0]?.closeCalls, 1);
|
||||
|
||||
const replacement = yield* adapter.startSession({
|
||||
threadId: THREAD_ID,
|
||||
provider: ProviderDriverKind.make("claudeAgent"),
|
||||
runtimeMode: "full-access",
|
||||
resumeCursor: firstSession.resumeCursor,
|
||||
});
|
||||
yield* TestClock.adjust("1 second");
|
||||
yield* Fiber.join(interruptFiber);
|
||||
|
||||
const activeSessions = yield* adapter.listSessions();
|
||||
const runtimeEvents = Array.from(yield* Fiber.join(runtimeEventsFiber));
|
||||
assert.equal(queries.length, 2);
|
||||
assert.equal(queries[1]?.closeCalls, 0);
|
||||
assert.equal(activeSessions.length, 1);
|
||||
assert.deepEqual(activeSessions[0]?.resumeCursor, replacement.resumeCursor);
|
||||
assert.deepEqual(
|
||||
runtimeEvents
|
||||
.filter((event) => event.type.startsWith("session."))
|
||||
.map((event) => event.type),
|
||||
[
|
||||
"session.started",
|
||||
"session.configured",
|
||||
"session.state.changed",
|
||||
"session.started",
|
||||
"session.configured",
|
||||
"session.state.changed",
|
||||
],
|
||||
);
|
||||
}).pipe(
|
||||
Effect.provideService(Random.Random, makeDeterministicRandomService()),
|
||||
Effect.provide(layer),
|
||||
);
|
||||
});
|
||||
|
||||
it.effect("workflow member coalescing: identical snapshots suppress, changes emit", () => {
|
||||
const harness = makeHarness();
|
||||
return Effect.gen(function* () {
|
||||
|
||||
@@ -249,9 +249,6 @@ interface ClaudeSessionContext {
|
||||
}
|
||||
|
||||
interface ClaudeQueryRuntime extends AsyncIterable<SDKMessage> {
|
||||
readonly interrupt: () => Promise<void>;
|
||||
/** SDK Query.stopTask — present on real queries; optional for test doubles. */
|
||||
readonly stopTask?: (taskId: string) => Promise<void>;
|
||||
readonly setModel: (model?: string) => Promise<void>;
|
||||
readonly setPermissionMode: (mode: PermissionMode) => Promise<void>;
|
||||
readonly setMaxThinkingTokens: (maxThinkingTokens: number | null) => Promise<void>;
|
||||
@@ -2073,13 +2070,13 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
});
|
||||
if (!usage) {
|
||||
}).pipe(Effect.timeoutOption("1 second"));
|
||||
if (Option.isNone(usage) || !usage.value) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
context.lastKnownContextWindow = usage.maxTokens;
|
||||
return normalizeClaudeContextUsageApiSnapshot(usage, totalProcessedTokens);
|
||||
context.lastKnownContextWindow = usage.value.maxTokens;
|
||||
return normalizeClaudeContextUsageApiSnapshot(usage.value, totalProcessedTokens);
|
||||
});
|
||||
|
||||
const emitProposedPlanCompleted = Effect.fn("emitProposedPlanCompleted")(function* (
|
||||
@@ -3598,8 +3595,42 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
|
||||
) {
|
||||
if (context.stopped) return;
|
||||
|
||||
// Schedule process termination before any cleanup that can wait on the
|
||||
// provider. The SDK closes stdin, then escalates from SIGTERM to SIGKILL.
|
||||
yield* Effect.try({
|
||||
try: () => context.query.close(),
|
||||
catch: (cause) =>
|
||||
new ProviderAdapterProcessError({
|
||||
provider: PROVIDER,
|
||||
threadId: context.session.threadId,
|
||||
detail: "Failed to close Claude runtime query.",
|
||||
cause,
|
||||
}),
|
||||
});
|
||||
|
||||
context.stopped = true;
|
||||
|
||||
for (const taskId of Array.from(context.liveTaskIds)) {
|
||||
if (!context.liveTaskIds.delete(taskId)) {
|
||||
continue;
|
||||
}
|
||||
const stamp = yield* makeEventStamp();
|
||||
yield* offerRuntimeEvent({
|
||||
type: "task.completed",
|
||||
eventId: stamp.eventId,
|
||||
provider: PROVIDER,
|
||||
createdAt: stamp.createdAt,
|
||||
threadId: context.session.threadId,
|
||||
...(context.turnState ? { turnId: asCanonicalTurnId(context.turnState.turnId) } : {}),
|
||||
payload: {
|
||||
taskId: RuntimeTaskId.make(taskId),
|
||||
status: "stopped",
|
||||
...taskLinkageFor(context.taskAgents, taskId),
|
||||
},
|
||||
providerRefs: nativeProviderRefs(context),
|
||||
});
|
||||
}
|
||||
|
||||
for (const [requestId, pending] of context.pendingApprovals) {
|
||||
yield* Deferred.succeed(pending.decision, "cancel");
|
||||
const stamp = yield* makeEventStamp();
|
||||
@@ -3632,26 +3663,6 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
|
||||
yield* Fiber.interrupt(streamFiber);
|
||||
}
|
||||
|
||||
yield* Effect.try({
|
||||
try: () => context.query.close(),
|
||||
catch: (cause) =>
|
||||
new ProviderAdapterProcessError({
|
||||
provider: PROVIDER,
|
||||
threadId: context.session.threadId,
|
||||
detail: "Failed to close Claude runtime query.",
|
||||
cause,
|
||||
}),
|
||||
}).pipe(
|
||||
Effect.catch((error) =>
|
||||
emitRuntimeError(context, "Failed to close Claude runtime query.", {
|
||||
errorTag: error._tag,
|
||||
provider: error.provider,
|
||||
threadId: error.threadId,
|
||||
detail: error.detail,
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
const updatedAt = yield* nowIso;
|
||||
context.session = {
|
||||
...context.session,
|
||||
@@ -3660,7 +3671,7 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
|
||||
updatedAt,
|
||||
};
|
||||
|
||||
if (options?.emitExitEvent !== false) {
|
||||
if (options?.emitExitEvent !== false && sessions.get(context.session.threadId) === context) {
|
||||
const stamp = yield* makeEventStamp();
|
||||
yield* offerRuntimeEvent({
|
||||
type: "session.exited",
|
||||
@@ -3676,7 +3687,9 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
|
||||
});
|
||||
}
|
||||
|
||||
sessions.delete(context.session.threadId);
|
||||
if (sessions.get(context.session.threadId) === context) {
|
||||
sessions.delete(context.session.threadId);
|
||||
}
|
||||
});
|
||||
|
||||
const requireSession = (
|
||||
@@ -3721,16 +3734,7 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
|
||||
});
|
||||
yield* stopSessionInternal(existingContext, {
|
||||
emitExitEvent: false,
|
||||
}).pipe(
|
||||
// Replacement cleanup is best-effort: never block the new session on
|
||||
// either typed failures or unexpected defects from tearing down the old one.
|
||||
Effect.catchCause((cause) =>
|
||||
Effect.logWarning("claude.session.replace.stop-failed", {
|
||||
threadId: input.threadId,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
const startedAt = yield* nowIso;
|
||||
@@ -4413,62 +4417,10 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
|
||||
const interruptTurn: ClaudeAdapterShape["interruptTurn"] = Effect.fn("interruptTurn")(
|
||||
function* (threadId, _turnId) {
|
||||
const context = yield* requireSession(threadId);
|
||||
// Stop-everything semantics: users reach for Stop precisely when a
|
||||
// fleet ran away. interrupt() alone only ends the parent turn —
|
||||
// background subagents/shells keep running and keep burning tokens.
|
||||
// Stop every live task first (best-effort per task: one refusal must
|
||||
// not strand the rest or block the turn interrupt), then interrupt.
|
||||
if (context.query.stopTask && context.liveTaskIds.size > 0) {
|
||||
const liveIds = Array.from(context.liveTaskIds);
|
||||
// Bounded: a wedged child's stopTask promise may never settle
|
||||
// (Effect.ignore handles rejection, not non-resolution), and the
|
||||
// parent interrupt below MUST still run — Stop matters most during
|
||||
// runaway fleets (review finding). Per-task timeout keeps one hung
|
||||
// child from consuming the whole budget.
|
||||
yield* Effect.forEach(
|
||||
liveIds,
|
||||
(taskId) =>
|
||||
Effect.gen(function* () {
|
||||
const stopAcknowledged = yield* Effect.tryPromise({
|
||||
// Invoke through the query object: SDK methods rely on `this`.
|
||||
try: () => context.query.stopTask!(taskId),
|
||||
catch: () => undefined,
|
||||
}).pipe(
|
||||
Effect.timeoutOption("3 seconds"),
|
||||
Effect.orElseSucceed(() => Option.none()),
|
||||
);
|
||||
if (Option.isNone(stopAcknowledged) || !context.liveTaskIds.delete(taskId)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// stopTask only acknowledges the control request. Its separate
|
||||
// task_notification can lose the race with interrupt(), so make
|
||||
// the acknowledged stop authoritative for the durable UI state.
|
||||
const stamp = yield* makeEventStamp();
|
||||
yield* offerRuntimeEvent({
|
||||
type: "task.completed",
|
||||
eventId: stamp.eventId,
|
||||
provider: PROVIDER,
|
||||
createdAt: stamp.createdAt,
|
||||
threadId: context.session.threadId,
|
||||
...(context.turnState
|
||||
? { turnId: asCanonicalTurnId(context.turnState.turnId) }
|
||||
: {}),
|
||||
payload: {
|
||||
taskId: RuntimeTaskId.make(taskId),
|
||||
status: "stopped",
|
||||
...taskLinkageFor(context.taskAgents, taskId),
|
||||
},
|
||||
providerRefs: nativeProviderRefs(context),
|
||||
});
|
||||
}).pipe(Effect.ignore),
|
||||
{ concurrency: 8, discard: true },
|
||||
).pipe(Effect.timeoutOption("10 seconds"), Effect.ignore);
|
||||
}
|
||||
yield* Effect.tryPromise({
|
||||
try: () => context.query.interrupt(),
|
||||
catch: (cause) => toRequestError(threadId, "turn/interrupt", cause),
|
||||
});
|
||||
// interrupt() can acknowledge while resumed background tasks keep the
|
||||
// CLI alive. Stop is a hard session boundary for Claude, so close the
|
||||
// query and let the SDK escalate to SIGKILL when graceful exit fails.
|
||||
yield* stopSessionInternal(context);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -4541,25 +4493,26 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
|
||||
return context !== undefined && !context.stopped;
|
||||
});
|
||||
|
||||
const stopAll: ClaudeAdapterShape["stopAll"] = () =>
|
||||
Effect.forEach(
|
||||
sessions,
|
||||
([, context]) =>
|
||||
stopSessionInternal(context, {
|
||||
emitExitEvent: true,
|
||||
}),
|
||||
{ discard: true },
|
||||
const stopSessions = Effect.fn("stopSessions")(function* (
|
||||
contexts: ReadonlyArray<ClaudeSessionContext>,
|
||||
emitExitEvent: boolean,
|
||||
) {
|
||||
const results = yield* Effect.forEach(contexts, (context) =>
|
||||
stopSessionInternal(context, { emitExitEvent }).pipe(Effect.result),
|
||||
);
|
||||
|
||||
for (const result of results) {
|
||||
if (result._tag === "Failure") {
|
||||
return yield* Effect.fail(result.failure);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const stopAll: ClaudeAdapterShape["stopAll"] = () =>
|
||||
stopSessions(Array.from(sessions.values()), true);
|
||||
|
||||
yield* Effect.addFinalizer(() =>
|
||||
Effect.forEach(
|
||||
sessions,
|
||||
([, context]) =>
|
||||
stopSessionInternal(context, {
|
||||
emitExitEvent: false,
|
||||
}),
|
||||
{ discard: true },
|
||||
).pipe(
|
||||
stopSessions(Array.from(sessions.values()), false).pipe(
|
||||
Effect.catch((cause) =>
|
||||
Effect.logError("Failed to emit Claude session shutdown event.", { cause }),
|
||||
),
|
||||
|
||||
Reference in new issue
Block a user