clanka 0.17.1 → 0.17.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Agent.d.ts +4 -0
- package/dist/Agent.d.ts.map +1 -1
- package/dist/Agent.js +5 -1
- package/dist/Agent.js.map +1 -1
- package/dist/Agent.test.js +35 -0
- package/dist/Agent.test.js.map +1 -1
- package/dist/ClaudeAgent.d.ts.map +1 -1
- package/dist/ClaudeAgent.js +93 -26
- package/dist/ClaudeAgent.js.map +1 -1
- package/dist/ClaudeAgent.test.js +191 -19
- package/dist/ClaudeAgent.test.js.map +1 -1
- package/dist/bin/cli.mjs +51 -20
- package/dist/bin/cli.mjs.map +1 -1
- package/package.json +1 -1
- package/src/Agent.test.ts +69 -0
- package/src/Agent.ts +5 -1
- package/src/ClaudeAgent.test.ts +277 -24
- package/src/ClaudeAgent.ts +122 -34
package/package.json
CHANGED
package/src/Agent.test.ts
CHANGED
|
@@ -144,6 +144,75 @@ describe("Agent", () => {
|
|
|
144
144
|
}),
|
|
145
145
|
),
|
|
146
146
|
)
|
|
147
|
+
|
|
148
|
+
it.effect("gives a sub-agent its own model instance", () =>
|
|
149
|
+
Effect.gen(function* () {
|
|
150
|
+
// Which model instance served each call, by the agent that made it.
|
|
151
|
+
const calls: Array<{
|
|
152
|
+
readonly child: boolean
|
|
153
|
+
readonly instance: number
|
|
154
|
+
}> = []
|
|
155
|
+
let instances = 0
|
|
156
|
+
const modelLayer = Layer.mergeAll(
|
|
157
|
+
Layer.effect(
|
|
158
|
+
LanguageModel.LanguageModel,
|
|
159
|
+
Effect.suspend(() => {
|
|
160
|
+
const instance = ++instances
|
|
161
|
+
return LanguageModel.make({
|
|
162
|
+
generateText: () => Effect.succeed([]),
|
|
163
|
+
streamText: ({ prompt }) => {
|
|
164
|
+
const child = JSON.stringify(prompt).includes("TASK-CHILD")
|
|
165
|
+
const index = calls.push({ child, instance }) - 1
|
|
166
|
+
return Stream.make(
|
|
167
|
+
toolCall(`call-${index}`, child || index > 0 ? "done" : ""),
|
|
168
|
+
)
|
|
169
|
+
},
|
|
170
|
+
})
|
|
171
|
+
}),
|
|
172
|
+
),
|
|
173
|
+
Layer.succeed(Model.ProviderName, "test-provider"),
|
|
174
|
+
Layer.succeed(Model.ModelName, "test-model"),
|
|
175
|
+
)
|
|
176
|
+
// The root delegates once in the foreground, then every agent finishes.
|
|
177
|
+
const executor = makeExecutor(
|
|
178
|
+
({ script, environment, subagents, onTaskComplete }) =>
|
|
179
|
+
Stream.fromEffect(
|
|
180
|
+
(script === "done"
|
|
181
|
+
? onTaskComplete("done")
|
|
182
|
+
: subagents.delegate({
|
|
183
|
+
prompt: "TASK-CHILD",
|
|
184
|
+
background: false,
|
|
185
|
+
environment: new AgentExecutor.EnvironmentUpdate({
|
|
186
|
+
environment,
|
|
187
|
+
update: Option.none(),
|
|
188
|
+
}),
|
|
189
|
+
})
|
|
190
|
+
).pipe(Effect.as(""), Effect.orDie),
|
|
191
|
+
),
|
|
192
|
+
)
|
|
193
|
+
const agent = yield* Agent.make.pipe(
|
|
194
|
+
Effect.provideService(AgentExecutor.AgentExecutor, executor),
|
|
195
|
+
)
|
|
196
|
+
const exit = yield* agent.send({ prompt: "hello" }).pipe(
|
|
197
|
+
Effect.flatMap(Stream.runDrain),
|
|
198
|
+
Effect.catchTag("AgentFinished", () => Effect.void),
|
|
199
|
+
Effect.provide(
|
|
200
|
+
Layer.mergeAll(
|
|
201
|
+
modelLayer,
|
|
202
|
+
Agent.ConversationMode.layer(false),
|
|
203
|
+
Agent.layerSubagentModel(modelLayer),
|
|
204
|
+
),
|
|
205
|
+
),
|
|
206
|
+
Effect.exit,
|
|
207
|
+
)
|
|
208
|
+
assert.deepStrictEqual(exit, Exit.void)
|
|
209
|
+
const root = calls.filter((call) => !call.child)
|
|
210
|
+
const child = calls.filter((call) => call.child)
|
|
211
|
+
assert.lengthOf(root, 2)
|
|
212
|
+
assert.lengthOf(child, 1)
|
|
213
|
+
assert.notStrictEqual(child[0]!.instance, root[0]!.instance)
|
|
214
|
+
}).pipe(Effect.scoped),
|
|
215
|
+
)
|
|
147
216
|
})
|
|
148
217
|
|
|
149
218
|
// =============================================================================
|
package/src/Agent.ts
CHANGED
|
@@ -1680,6 +1680,10 @@ export class SubagentModel extends Context.Service<
|
|
|
1680
1680
|
>()("clanka/Agent/SubagentModel") {}
|
|
1681
1681
|
|
|
1682
1682
|
/**
|
|
1683
|
+
* The model every sub-agent runs on. Each sub-agent builds its own instance,
|
|
1684
|
+
* even when `layer` is also this agent's model layer, so a stateful model
|
|
1685
|
+
* such as Claude Agent never shares its session with another agent.
|
|
1686
|
+
*
|
|
1683
1687
|
* @since 1.0.0
|
|
1684
1688
|
* @category Subagent model
|
|
1685
1689
|
*/
|
|
@@ -1694,7 +1698,7 @@ export const layerSubagentModel = <E, R>(
|
|
|
1694
1698
|
SubagentModel,
|
|
1695
1699
|
Effect.gen(function* () {
|
|
1696
1700
|
const services = yield* Effect.context<R>()
|
|
1697
|
-
return Layer.orDie(layer).pipe(
|
|
1701
|
+
return Layer.fresh(Layer.orDie(layer)).pipe(
|
|
1698
1702
|
Layer.provide(Layer.succeedContext(services)),
|
|
1699
1703
|
)
|
|
1700
1704
|
}),
|
package/src/ClaudeAgent.test.ts
CHANGED
|
@@ -3,13 +3,18 @@ import type {
|
|
|
3
3
|
query as SdkQuery,
|
|
4
4
|
SDKUserMessage,
|
|
5
5
|
} from "@anthropic-ai/claude-agent-sdk"
|
|
6
|
-
import { assert, describe, it } from "@effect/vitest"
|
|
6
|
+
import { assert, beforeEach, describe, it } from "@effect/vitest"
|
|
7
7
|
import { Client } from "@modelcontextprotocol/sdk/client/index.js"
|
|
8
8
|
import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js"
|
|
9
|
+
import * as Context from "effect/Context"
|
|
10
|
+
import * as Deferred from "effect/Deferred"
|
|
9
11
|
import * as Effect from "effect/Effect"
|
|
12
|
+
import * as Exit from "effect/Exit"
|
|
13
|
+
import * as Fiber from "effect/Fiber"
|
|
10
14
|
import * as Layer from "effect/Layer"
|
|
11
15
|
import * as Base64 from "effect/encoding/Base64"
|
|
12
16
|
import * as Schema from "effect/Schema"
|
|
17
|
+
import * as Scope from "effect/Scope"
|
|
13
18
|
import * as Stream from "effect/Stream"
|
|
14
19
|
import * as LanguageModel from "effect/ai/LanguageModel"
|
|
15
20
|
import * as Prompt from "effect/ai/Prompt"
|
|
@@ -302,39 +307,72 @@ const fakeClaude = (run: (session: FakeSession) => AsyncGenerator<object>) => {
|
|
|
302
307
|
}
|
|
303
308
|
|
|
304
309
|
// Claude Code calling clanka's `execute` tool over MCP, each call tagged with
|
|
305
|
-
// its tool use id unless `tagged` is false.
|
|
310
|
+
// its tool use id unless `tagged` is false. Like Claude Code, it runs the calls
|
|
311
|
+
// together only when `tools/list` marks the tool read-only, and otherwise
|
|
312
|
+
// requests each call after the previous one returns. `onRequest` runs once
|
|
313
|
+
// clanka's handler has taken each request.
|
|
306
314
|
const callExecute = async (
|
|
307
315
|
session: FakeSession,
|
|
308
316
|
calls: ReadonlyArray<readonly [id: string, script: string]>,
|
|
309
317
|
tagged = true,
|
|
318
|
+
onRequest?: () => void,
|
|
310
319
|
) => {
|
|
311
|
-
const server = session.options.mcpServers?.clanka as {
|
|
320
|
+
const server = session.options.mcpServers?.clanka as unknown as {
|
|
312
321
|
readonly instance: {
|
|
313
322
|
connect: (transport: InMemoryTransport) => Promise<void>
|
|
323
|
+
// The MCP SDK's handlers by method. Not public, but the SDK has no
|
|
324
|
+
// other way to see a request reach a handler that never returns.
|
|
325
|
+
readonly _requestHandlers: Map<
|
|
326
|
+
string,
|
|
327
|
+
(request: unknown, extra: unknown) => Promise<unknown>
|
|
328
|
+
>
|
|
314
329
|
}
|
|
315
330
|
}
|
|
331
|
+
if (onRequest !== undefined) {
|
|
332
|
+
const handlers = server.instance._requestHandlers
|
|
333
|
+
const handler = handlers.get("tools/call")
|
|
334
|
+
assert.isDefined(handler)
|
|
335
|
+
// The SDK's wrapper calls clanka's handler before its first await.
|
|
336
|
+
handlers.set("tools/call", (request, extra) => {
|
|
337
|
+
const result = handler(request, extra)
|
|
338
|
+
onRequest()
|
|
339
|
+
return result
|
|
340
|
+
})
|
|
341
|
+
}
|
|
316
342
|
const [clientTransport, serverTransport] =
|
|
317
343
|
InMemoryTransport.createLinkedPair()
|
|
318
344
|
await server.instance.connect(serverTransport)
|
|
319
345
|
const client = new Client({ name: "claude-code", version: "0.0.0" })
|
|
320
346
|
await client.connect(clientTransport)
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
347
|
+
const { tools } = await client.listTools()
|
|
348
|
+
const readOnly =
|
|
349
|
+
tools.find((tool) => tool.name === "execute")?.annotations?.readOnlyHint ===
|
|
350
|
+
true
|
|
351
|
+
return Effect.runPromise(
|
|
352
|
+
Effect.forEach(
|
|
353
|
+
calls,
|
|
354
|
+
([id, script]) =>
|
|
355
|
+
Effect.promise(() =>
|
|
356
|
+
client.callTool({
|
|
357
|
+
name: "execute",
|
|
358
|
+
arguments: { script },
|
|
359
|
+
...(tagged ? { _meta: { "claudecode/toolUseId": id } } : {}),
|
|
360
|
+
}),
|
|
361
|
+
),
|
|
362
|
+
{ concurrency: readOnly ? "unbounded" : 1 },
|
|
328
363
|
),
|
|
329
364
|
)
|
|
330
365
|
}
|
|
331
366
|
|
|
332
367
|
// One API message calling `execute` once per call.
|
|
333
|
-
const executeStep = (
|
|
368
|
+
const executeStep = (
|
|
369
|
+
calls: ReadonlyArray<readonly [string, string]>,
|
|
370
|
+
id = "msg_tools",
|
|
371
|
+
) => [
|
|
334
372
|
event({
|
|
335
373
|
type: "message_start",
|
|
336
374
|
message: {
|
|
337
|
-
id
|
|
375
|
+
id,
|
|
338
376
|
type: "message",
|
|
339
377
|
role: "assistant",
|
|
340
378
|
model: "claude-opus-5-5",
|
|
@@ -360,6 +398,22 @@ const executeStep = (calls: ReadonlyArray<readonly [string, string]>) => [
|
|
|
360
398
|
event({ type: "message_stop" }),
|
|
361
399
|
]
|
|
362
400
|
|
|
401
|
+
// Claude Code answering a call itself instead of calling clanka, as it does
|
|
402
|
+
// for input it cannot validate or a call it aborts.
|
|
403
|
+
const answeredByClaude = (id: string, content: string) => ({
|
|
404
|
+
...frame,
|
|
405
|
+
type: "user",
|
|
406
|
+
message: {
|
|
407
|
+
role: "user",
|
|
408
|
+
content: [
|
|
409
|
+
{ type: "tool_result", tool_use_id: id, content, is_error: true },
|
|
410
|
+
],
|
|
411
|
+
},
|
|
412
|
+
})
|
|
413
|
+
|
|
414
|
+
const invalidInput =
|
|
415
|
+
"<tool_use_error>InputValidationError: JSON parse failed</tool_use_error>"
|
|
416
|
+
|
|
363
417
|
const finalStep = [
|
|
364
418
|
assistant({ type: "text", text: "Done." }),
|
|
365
419
|
{
|
|
@@ -373,6 +427,21 @@ const finalStep = [
|
|
|
373
427
|
},
|
|
374
428
|
]
|
|
375
429
|
|
|
430
|
+
// The start of a step that is still writing its reply.
|
|
431
|
+
const replying = [
|
|
432
|
+
...executeStep([]).slice(0, 1),
|
|
433
|
+
event({
|
|
434
|
+
type: "content_block_start",
|
|
435
|
+
index: 0,
|
|
436
|
+
content_block: { type: "text", text: "" },
|
|
437
|
+
}),
|
|
438
|
+
event({
|
|
439
|
+
type: "content_block_delta",
|
|
440
|
+
index: 0,
|
|
441
|
+
delta: { type: "text_delta", text: "Working" },
|
|
442
|
+
}),
|
|
443
|
+
]
|
|
444
|
+
|
|
376
445
|
const Execute = Toolkit.make(
|
|
377
446
|
Tool.make("execute", {
|
|
378
447
|
parameters: Schema.Struct({ script: Schema.String }),
|
|
@@ -383,6 +452,10 @@ const Execute = Toolkit.make(
|
|
|
383
452
|
// The scripts clanka's `execute` handler ran.
|
|
384
453
|
const executed: Array<string> = []
|
|
385
454
|
|
|
455
|
+
beforeEach(() => {
|
|
456
|
+
executed.length = 0
|
|
457
|
+
})
|
|
458
|
+
|
|
386
459
|
const ExecuteHandlers = Execute.toLayer({
|
|
387
460
|
execute: ({ script }) =>
|
|
388
461
|
Effect.sync(() => {
|
|
@@ -438,6 +511,7 @@ describe("ClaudeAgent.model", () => {
|
|
|
438
511
|
results.map((result: any) => result.content),
|
|
439
512
|
[[{ type: "text", text: "ran a" }], [{ type: "text", text: "ran b" }]],
|
|
440
513
|
)
|
|
514
|
+
assert.deepStrictEqual(executed.toSorted(), ["a", "b"])
|
|
441
515
|
assert.lengthOf(fake.sessions, 1)
|
|
442
516
|
}),
|
|
443
517
|
)
|
|
@@ -451,7 +525,6 @@ describe("ClaudeAgent.model", () => {
|
|
|
451
525
|
["toolu_b", "b"],
|
|
452
526
|
] as const
|
|
453
527
|
let results: ReadonlyArray<unknown> = []
|
|
454
|
-
executed.length = 0
|
|
455
528
|
const fake = fakeClaude(async function* (session) {
|
|
456
529
|
await session.input.next()
|
|
457
530
|
yield* executeStep(calls)
|
|
@@ -470,6 +543,122 @@ describe("ClaudeAgent.model", () => {
|
|
|
470
543
|
}),
|
|
471
544
|
)
|
|
472
545
|
|
|
546
|
+
it.effect(
|
|
547
|
+
"drops a call Claude Code answered itself and runs the parked one",
|
|
548
|
+
() =>
|
|
549
|
+
Effect.gen(function* () {
|
|
550
|
+
let results: ReadonlyArray<unknown> = []
|
|
551
|
+
const fake = fakeClaude(async function* (session) {
|
|
552
|
+
await session.input.next()
|
|
553
|
+
yield* executeStep([
|
|
554
|
+
["toolu_a", "a"],
|
|
555
|
+
["toolu_b", "b"],
|
|
556
|
+
])
|
|
557
|
+
yield answeredByClaude("toolu_a", invalidInput)
|
|
558
|
+
results = await callExecute(session, [["toolu_b", "b"]])
|
|
559
|
+
yield* finalStep
|
|
560
|
+
})
|
|
561
|
+
yield* runSteps(fake, (first, response) =>
|
|
562
|
+
Prompt.concat(first, response),
|
|
563
|
+
)
|
|
564
|
+
assert.deepStrictEqual(
|
|
565
|
+
results.map((result: any) => result.content),
|
|
566
|
+
[[{ type: "text", text: "ran b" }]],
|
|
567
|
+
)
|
|
568
|
+
assert.deepStrictEqual(executed, ["b"])
|
|
569
|
+
assert.lengthOf(fake.sessions, 1)
|
|
570
|
+
}),
|
|
571
|
+
)
|
|
572
|
+
|
|
573
|
+
it.effect(
|
|
574
|
+
"keeps reading when Claude Code answers the only call and Claude retries",
|
|
575
|
+
() =>
|
|
576
|
+
Effect.gen(function* () {
|
|
577
|
+
let results: ReadonlyArray<unknown> = []
|
|
578
|
+
const fake = fakeClaude(async function* (session) {
|
|
579
|
+
await session.input.next()
|
|
580
|
+
yield* executeStep([["toolu_a", "a"]])
|
|
581
|
+
yield answeredByClaude("toolu_a", invalidInput)
|
|
582
|
+
yield* executeStep([["toolu_b", "b"]], "msg_retry")
|
|
583
|
+
results = await callExecute(session, [["toolu_b", "b"]])
|
|
584
|
+
yield* finalStep
|
|
585
|
+
})
|
|
586
|
+
yield* runSteps(fake, (first, response) =>
|
|
587
|
+
Prompt.concat(first, response),
|
|
588
|
+
)
|
|
589
|
+
assert.deepStrictEqual(
|
|
590
|
+
results.map((result: any) => result.content),
|
|
591
|
+
[[{ type: "text", text: "ran b" }]],
|
|
592
|
+
)
|
|
593
|
+
assert.deepStrictEqual(executed, ["b"])
|
|
594
|
+
assert.lengthOf(fake.sessions, 1)
|
|
595
|
+
}),
|
|
596
|
+
)
|
|
597
|
+
|
|
598
|
+
it.effect(
|
|
599
|
+
"holds a retry's calls until its message stops, even when they park first",
|
|
600
|
+
() =>
|
|
601
|
+
Effect.gen(function* () {
|
|
602
|
+
const retry = [
|
|
603
|
+
["toolu_b", "b"],
|
|
604
|
+
["toolu_c", "c"],
|
|
605
|
+
] as const
|
|
606
|
+
let results: ReadonlyArray<unknown> = []
|
|
607
|
+
const fake = fakeClaude(async function* (session) {
|
|
608
|
+
await session.input.next()
|
|
609
|
+
yield* executeStep([["toolu_a", "a"]])
|
|
610
|
+
yield answeredByClaude("toolu_a", invalidInput)
|
|
611
|
+
const retryStep = executeStep(retry, "msg_retry")
|
|
612
|
+
yield* retryStep.slice(0, 1)
|
|
613
|
+
// Claude Code runs a call as soon as its block streams, so the
|
|
614
|
+
// retry's requests park before its assistant events and its
|
|
615
|
+
// message_stop.
|
|
616
|
+
const parked = Promise.withResolvers<void>()
|
|
617
|
+
let requests = 0
|
|
618
|
+
const parking = callExecute(session, retry, true, () => {
|
|
619
|
+
if (++requests === retry.length) parked.resolve()
|
|
620
|
+
})
|
|
621
|
+
await parked.promise
|
|
622
|
+
yield* retryStep.slice(1)
|
|
623
|
+
results = await parking
|
|
624
|
+
yield* finalStep
|
|
625
|
+
})
|
|
626
|
+
yield* runSteps(fake, (first, response) =>
|
|
627
|
+
Prompt.concat(first, response),
|
|
628
|
+
)
|
|
629
|
+
assert.deepStrictEqual(
|
|
630
|
+
results.map((result: any) => result.content),
|
|
631
|
+
[
|
|
632
|
+
[{ type: "text", text: "ran b" }],
|
|
633
|
+
[{ type: "text", text: "ran c" }],
|
|
634
|
+
],
|
|
635
|
+
)
|
|
636
|
+
assert.deepStrictEqual(executed.toSorted(), ["b", "c"])
|
|
637
|
+
assert.lengthOf(fake.sessions, 1)
|
|
638
|
+
}),
|
|
639
|
+
)
|
|
640
|
+
|
|
641
|
+
it.effect("fails an aborted call with the result Claude Code gave it", () =>
|
|
642
|
+
Effect.gen(function* () {
|
|
643
|
+
const fake = fakeClaude(async function* (session) {
|
|
644
|
+
await session.input.next()
|
|
645
|
+
yield* executeStep([["toolu_a", "a"]])
|
|
646
|
+
yield answeredByClaude(
|
|
647
|
+
"toolu_a",
|
|
648
|
+
"[Request interrupted by user for tool use]",
|
|
649
|
+
)
|
|
650
|
+
})
|
|
651
|
+
const error = yield* Effect.flip(
|
|
652
|
+
runSteps(fake, (first, response) => Prompt.concat(first, response)),
|
|
653
|
+
)
|
|
654
|
+
assert.include(
|
|
655
|
+
error.message,
|
|
656
|
+
"[Request interrupted by user for tool use]",
|
|
657
|
+
)
|
|
658
|
+
assert.deepStrictEqual(executed, [])
|
|
659
|
+
}),
|
|
660
|
+
)
|
|
661
|
+
|
|
473
662
|
it.effect(
|
|
474
663
|
"starts over when the prompt's answer is not the one Claude gave",
|
|
475
664
|
() =>
|
|
@@ -498,17 +687,7 @@ describe("ClaudeAgent.model", () => {
|
|
|
498
687
|
Effect.gen(function* () {
|
|
499
688
|
const fake = fakeClaude(async function* (session) {
|
|
500
689
|
await session.input.next()
|
|
501
|
-
yield*
|
|
502
|
-
yield event({
|
|
503
|
-
type: "content_block_start",
|
|
504
|
-
index: 0,
|
|
505
|
-
content_block: { type: "text", text: "" },
|
|
506
|
-
})
|
|
507
|
-
yield event({
|
|
508
|
-
type: "content_block_delta",
|
|
509
|
-
index: 0,
|
|
510
|
-
delta: { type: "text_delta", text: "Working" },
|
|
511
|
-
})
|
|
690
|
+
yield* replying
|
|
512
691
|
await new Promise(() => {})
|
|
513
692
|
})
|
|
514
693
|
const prompt = Prompt.fromMessages([system, user("go")])
|
|
@@ -523,4 +702,78 @@ describe("ClaudeAgent.model", () => {
|
|
|
523
702
|
}).pipe(Effect.provide(modelWith(fake)))
|
|
524
703
|
}),
|
|
525
704
|
)
|
|
705
|
+
|
|
706
|
+
it.effect(
|
|
707
|
+
"refuses a second step while one is read, keeping the first session",
|
|
708
|
+
() =>
|
|
709
|
+
Effect.gen(function* () {
|
|
710
|
+
const released = Promise.withResolvers<void>()
|
|
711
|
+
const fake = fakeClaude(async function* (session) {
|
|
712
|
+
await session.input.next()
|
|
713
|
+
if (session === fake.sessions[0]) {
|
|
714
|
+
yield* replying
|
|
715
|
+
await released.promise
|
|
716
|
+
}
|
|
717
|
+
yield* finalStep
|
|
718
|
+
})
|
|
719
|
+
yield* Effect.gen(function* () {
|
|
720
|
+
const ai = yield* LanguageModel.LanguageModel
|
|
721
|
+
const reading = yield* Deferred.make<void>()
|
|
722
|
+
const first = yield* ai
|
|
723
|
+
.streamText({
|
|
724
|
+
prompt: Prompt.fromMessages([system, user("go")]),
|
|
725
|
+
toolkit: Execute,
|
|
726
|
+
})
|
|
727
|
+
.pipe(
|
|
728
|
+
Stream.tap(() => Deferred.succeed(reading, undefined)),
|
|
729
|
+
Stream.runDrain,
|
|
730
|
+
Effect.forkChild,
|
|
731
|
+
)
|
|
732
|
+
yield* Deferred.await(reading)
|
|
733
|
+
const second = yield* ai
|
|
734
|
+
.streamText({
|
|
735
|
+
prompt: Prompt.fromMessages([system, user("something else")]),
|
|
736
|
+
toolkit: Execute,
|
|
737
|
+
})
|
|
738
|
+
.pipe(Stream.runDrain, Effect.exit)
|
|
739
|
+
assert.isTrue(Exit.isFailure(second))
|
|
740
|
+
assert.lengthOf(fake.sessions, 1)
|
|
741
|
+
assert.isFalse(fake.sessions[0]!.closed)
|
|
742
|
+
released.resolve()
|
|
743
|
+
yield* Fiber.join(first)
|
|
744
|
+
}).pipe(Effect.provide(modelWith(fake)))
|
|
745
|
+
}),
|
|
746
|
+
)
|
|
747
|
+
|
|
748
|
+
it.live("fails a reader whose session was closed instead of hanging", () =>
|
|
749
|
+
Effect.gen(function* () {
|
|
750
|
+
const fake = fakeClaude(async function* (session) {
|
|
751
|
+
await session.input.next()
|
|
752
|
+
yield* replying
|
|
753
|
+
await new Promise(() => {})
|
|
754
|
+
})
|
|
755
|
+
const scope = yield* Scope.make()
|
|
756
|
+
const context = yield* Layer.buildWithScope(modelWith(fake), scope)
|
|
757
|
+
const reading = yield* Deferred.make<void>()
|
|
758
|
+
const reader = yield* Context.get(context, LanguageModel.LanguageModel)
|
|
759
|
+
.streamText({
|
|
760
|
+
prompt: Prompt.fromMessages([system, user("go")]),
|
|
761
|
+
toolkit: Execute,
|
|
762
|
+
})
|
|
763
|
+
.pipe(
|
|
764
|
+
Stream.tap(() => Deferred.succeed(reading, undefined)),
|
|
765
|
+
Stream.runDrain,
|
|
766
|
+
Effect.provideContext(context),
|
|
767
|
+
Effect.forkChild,
|
|
768
|
+
)
|
|
769
|
+
yield* Deferred.await(reading)
|
|
770
|
+
// Closing the model closes Claude Code under the step being read.
|
|
771
|
+
yield* Scope.close(scope, Exit.void)
|
|
772
|
+
const error = yield* Fiber.join(reader).pipe(
|
|
773
|
+
Effect.flip,
|
|
774
|
+
Effect.timeout("1 second"),
|
|
775
|
+
)
|
|
776
|
+
assert.strictEqual(error._tag, "AiError")
|
|
777
|
+
}),
|
|
778
|
+
)
|
|
526
779
|
})
|