clanka 0.22.2 → 0.22.4
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/README.md +10 -4
- package/dist/Acp.d.ts.map +1 -1
- package/dist/Acp.js +12 -0
- package/dist/Acp.js.map +1 -1
- package/dist/Agent.d.ts.map +1 -1
- package/dist/Agent.js +197 -31
- package/dist/Agent.js.map +1 -1
- package/dist/Agent.test.js +114 -18
- package/dist/Agent.test.js.map +1 -1
- package/dist/Compaction.d.ts +16 -18
- package/dist/Compaction.d.ts.map +1 -1
- package/dist/Compaction.js +18 -20
- package/dist/Compaction.js.map +1 -1
- package/dist/ModelLayer.d.ts +3 -2
- package/dist/ModelLayer.d.ts.map +1 -1
- package/dist/ModelLayer.js +6 -5
- package/dist/ModelLayer.js.map +1 -1
- package/dist/bin/cli.mjs +156 -41
- package/dist/bin/cli.mjs.map +1 -1
- package/package.json +1 -1
- package/src/Acp.ts +14 -0
- package/src/Agent.test.ts +184 -28
- package/src/Agent.ts +314 -74
- package/src/Compaction.ts +25 -37
- package/src/ModelLayer.ts +8 -5
package/package.json
CHANGED
package/src/Acp.ts
CHANGED
|
@@ -905,6 +905,20 @@ export const make = Effect.fnUntraced(function* <RAgent, RModel>(
|
|
|
905
905
|
`Retrying after error: ${part.error.message}\n`,
|
|
906
906
|
),
|
|
907
907
|
})
|
|
908
|
+
case "CompactionStarted":
|
|
909
|
+
return update(session.id, {
|
|
910
|
+
sessionUpdate: "agent_thought_chunk",
|
|
911
|
+
content: textContent(`Compacting context (${part.reason})...\n`),
|
|
912
|
+
})
|
|
913
|
+
case "CompactionEnded":
|
|
914
|
+
return update(session.id, {
|
|
915
|
+
sessionUpdate: "agent_thought_chunk",
|
|
916
|
+
content: textContent(
|
|
917
|
+
part.tokensAfter < part.tokensBefore
|
|
918
|
+
? `Context compacted (${part.reason}): ~${part.tokensBefore} tokens to ~${part.tokensAfter}\n`
|
|
919
|
+
: `Context not compacted (${part.reason}), continuing with ~${part.tokensBefore} tokens\n`,
|
|
920
|
+
),
|
|
921
|
+
})
|
|
908
922
|
case "SessionRestarted":
|
|
909
923
|
return update(session.id, {
|
|
910
924
|
sessionUpdate: "agent_thought_chunk",
|
package/src/Agent.test.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { assert, describe, it } from "@effect/vitest"
|
|
|
2
2
|
import * as NodeServices from "@effect/platform-node/NodeServices"
|
|
3
3
|
import * as FileSystem from "effect/FileSystem"
|
|
4
4
|
import * as FetchHttpClient from "effect/http/FetchHttpClient"
|
|
5
|
+
import * as Deferred from "effect/Deferred"
|
|
5
6
|
import * as Effect from "effect/Effect"
|
|
6
7
|
import * as Duration from "effect/Duration"
|
|
7
8
|
import * as Exit from "effect/Exit"
|
|
@@ -758,38 +759,57 @@ describe("Agent execute output cap", () => {
|
|
|
758
759
|
})
|
|
759
760
|
|
|
760
761
|
describe("Agent auto-compaction", () => {
|
|
761
|
-
it.
|
|
762
|
-
"streams
|
|
762
|
+
it.live(
|
|
763
|
+
"streams the first call while compacting and applies the summary at the next call",
|
|
763
764
|
() =>
|
|
764
765
|
Effect.gen(function* () {
|
|
765
|
-
const
|
|
766
|
+
const started = yield* Deferred.make<void>()
|
|
767
|
+
const summarized = yield* Deferred.make<void>()
|
|
768
|
+
let agentCalls = 0
|
|
769
|
+
const { exit } = yield* runAgentCollect({
|
|
766
770
|
history: fatHistory,
|
|
767
771
|
compaction: {
|
|
768
772
|
contextWindow: 12_000,
|
|
769
773
|
reserveTokens: 2_000,
|
|
770
774
|
keepRecentTokens: 9_000,
|
|
771
775
|
},
|
|
772
|
-
|
|
773
|
-
Stream.
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
776
|
+
executor: makeExecutor(() =>
|
|
777
|
+
Stream.fromEffect(Deferred.await(summarized).pipe(Effect.as("ok"))),
|
|
778
|
+
),
|
|
779
|
+
respond: (call) => {
|
|
780
|
+
assert.strictEqual(call.method, "streamText")
|
|
781
|
+
if (call.isSummarizer) {
|
|
782
|
+
assertSummarizerCall(call)
|
|
783
|
+
return Stream.fromEffect(Deferred.await(started)).pipe(
|
|
784
|
+
Stream.flatMap(() =>
|
|
785
|
+
Stream.fromIterable(text("BACKGROUND-SUMMARY")),
|
|
786
|
+
),
|
|
787
|
+
Stream.ensuring(Deferred.done(summarized, Exit.void)),
|
|
788
|
+
)
|
|
789
|
+
}
|
|
790
|
+
if (agentCalls++ === 0) {
|
|
791
|
+
assert.include(promptJson(call.prompt), "SENTINEL-ONE")
|
|
792
|
+
Deferred.doneUnsafe(started, Exit.void)
|
|
793
|
+
return Stream.fromIterable([
|
|
794
|
+
toolCall("call-3", "console.log('ok')"),
|
|
795
|
+
])
|
|
796
|
+
}
|
|
797
|
+
assertCompactedShape(call.prompt, "BACKGROUND-SUMMARY")
|
|
798
|
+
assert.include(promptJson(call.prompt), "console.log('ok')")
|
|
799
|
+
return Stream.fromIterable(text("answered"))
|
|
800
|
+
},
|
|
801
|
+
}).pipe(Effect.timeout("2 seconds"))
|
|
802
|
+
assert.deepStrictEqual(exit, Exit.succeed("answered"))
|
|
784
803
|
}),
|
|
785
804
|
)
|
|
786
805
|
|
|
787
806
|
it.live(
|
|
788
|
-
"
|
|
807
|
+
"backs off after a threshold timeout and continues with the original prompt",
|
|
789
808
|
() =>
|
|
790
809
|
Effect.gen(function* () {
|
|
791
810
|
let interrupted = false
|
|
792
|
-
|
|
811
|
+
let agentCalls = 0
|
|
812
|
+
const { exit, calls, outputs } = yield* runAgentCollect({
|
|
793
813
|
history: fatHistory,
|
|
794
814
|
turnTimeout: Duration.millis(100),
|
|
795
815
|
compaction: {
|
|
@@ -797,8 +817,13 @@ describe("Agent auto-compaction", () => {
|
|
|
797
817
|
reserveTokens: 2_000,
|
|
798
818
|
keepRecentTokens: 9_000,
|
|
799
819
|
},
|
|
800
|
-
|
|
801
|
-
|
|
820
|
+
// The script outlasts the timeout, so the summary has timed out
|
|
821
|
+
// by the next model call.
|
|
822
|
+
executor: makeExecutor(() =>
|
|
823
|
+
Stream.fromEffect(Effect.as(Effect.sleep("300 millis"), "ok")),
|
|
824
|
+
),
|
|
825
|
+
respond: (call) => {
|
|
826
|
+
if (call.isSummarizer) {
|
|
802
827
|
assertSummarizerCall(call)
|
|
803
828
|
return Stream.fromEffect(Effect.never).pipe(
|
|
804
829
|
Stream.ensuring(
|
|
@@ -808,22 +833,21 @@ describe("Agent auto-compaction", () => {
|
|
|
808
833
|
),
|
|
809
834
|
)
|
|
810
835
|
}
|
|
811
|
-
assert.isFalse(call.isSummarizer)
|
|
812
836
|
assert.include(promptJson(call.prompt), "SENTINEL-ONE")
|
|
813
837
|
assert.isTrue(
|
|
814
838
|
Option.isNone(Compaction.findPreviousSummary(call.prompt)),
|
|
815
839
|
)
|
|
816
|
-
return
|
|
840
|
+
return agentCalls++ < 2
|
|
841
|
+
? Stream.fromIterable([
|
|
842
|
+
toolCall(`call-${agentCalls + 2}`, "console.log('ok')"),
|
|
843
|
+
finish(17_000),
|
|
844
|
+
])
|
|
845
|
+
: Stream.fromIterable(text("continued after timeout"))
|
|
817
846
|
},
|
|
818
|
-
}).pipe(Effect.
|
|
819
|
-
assert.isTrue(
|
|
820
|
-
Option.isSome(result),
|
|
821
|
-
"threshold summarization must not hang the turn",
|
|
822
|
-
)
|
|
823
|
-
const { exit, calls, outputs } = Option.getOrThrow(result)
|
|
847
|
+
}).pipe(Effect.timeout("2 seconds"))
|
|
824
848
|
assert.deepStrictEqual(exit, Exit.succeed("continued after timeout"))
|
|
825
849
|
assert.isTrue(interrupted)
|
|
826
|
-
assert.strictEqual(calls.length,
|
|
850
|
+
assert.strictEqual(calls.filter((call) => call.isSummarizer).length, 1)
|
|
827
851
|
const ended = outputsOfTag(outputs, "CompactionEnded")
|
|
828
852
|
assert.strictEqual(ended.length, 1)
|
|
829
853
|
assert.strictEqual(ended[0]!.tokensAfter, ended[0]!.tokensBefore)
|
|
@@ -1131,6 +1155,138 @@ describe("Agent auto-compaction", () => {
|
|
|
1131
1155
|
assert.strictEqual(ended[0]!.tokensAfter, ended[0]!.tokensBefore)
|
|
1132
1156
|
}),
|
|
1133
1157
|
)
|
|
1158
|
+
|
|
1159
|
+
it.live(
|
|
1160
|
+
"does not finish a summary on the model of a send that has ended",
|
|
1161
|
+
() =>
|
|
1162
|
+
Effect.scoped(
|
|
1163
|
+
Effect.gen(function* () {
|
|
1164
|
+
const firstSendDone = yield* Deferred.make<void>()
|
|
1165
|
+
const agentPrompts: Array<Prompt.Prompt> = []
|
|
1166
|
+
// Built for each send, like the ACP model. Its client stops
|
|
1167
|
+
// working once the send that built it has ended.
|
|
1168
|
+
const modelLayer = Layer.mergeAll(
|
|
1169
|
+
Layer.effect(
|
|
1170
|
+
LanguageModel.LanguageModel,
|
|
1171
|
+
Effect.gen(function* () {
|
|
1172
|
+
let closed = false
|
|
1173
|
+
yield* Effect.addFinalizer(() =>
|
|
1174
|
+
Effect.sync(() => {
|
|
1175
|
+
closed = true
|
|
1176
|
+
}),
|
|
1177
|
+
)
|
|
1178
|
+
return yield* LanguageModel.make({
|
|
1179
|
+
generateText: () => Effect.succeed([]),
|
|
1180
|
+
streamText: (options) => {
|
|
1181
|
+
if (options.tools.length > 0) {
|
|
1182
|
+
agentPrompts.push(options.prompt)
|
|
1183
|
+
return Stream.fromIterable(text("answered"))
|
|
1184
|
+
}
|
|
1185
|
+
return Stream.fromEffect(
|
|
1186
|
+
Deferred.await(firstSendDone),
|
|
1187
|
+
).pipe(
|
|
1188
|
+
Stream.flatMap(() =>
|
|
1189
|
+
closed
|
|
1190
|
+
? Stream.fail(providerError)
|
|
1191
|
+
: Stream.fromIterable(text("SECOND-SEND-SUMMARY")),
|
|
1192
|
+
),
|
|
1193
|
+
)
|
|
1194
|
+
},
|
|
1195
|
+
})
|
|
1196
|
+
}),
|
|
1197
|
+
),
|
|
1198
|
+
Layer.succeed(Model.ProviderName, "test-provider"),
|
|
1199
|
+
Layer.succeed(Model.ModelName, "test-model"),
|
|
1200
|
+
)
|
|
1201
|
+
const agent = yield* Agent.make.pipe(
|
|
1202
|
+
Effect.provideService(AgentExecutor.AgentExecutor, makeExecutor()),
|
|
1203
|
+
)
|
|
1204
|
+
MutableRef.set(agent.history, fatHistory)
|
|
1205
|
+
const send = (prompt: string) =>
|
|
1206
|
+
agent.send({ prompt }).pipe(
|
|
1207
|
+
Effect.flatMap((stream) =>
|
|
1208
|
+
stream.pipe(
|
|
1209
|
+
Stream.runDrain,
|
|
1210
|
+
Effect.as(""),
|
|
1211
|
+
Effect.catchTag("AgentFinished", (finished) =>
|
|
1212
|
+
Effect.succeed(finished.summary),
|
|
1213
|
+
),
|
|
1214
|
+
),
|
|
1215
|
+
),
|
|
1216
|
+
Effect.provide(
|
|
1217
|
+
Layer.mergeAll(
|
|
1218
|
+
modelLayer,
|
|
1219
|
+
Agent.ConversationMode.layer(true),
|
|
1220
|
+
Compaction.CompactionConfig.layer({
|
|
1221
|
+
contextWindow: 12_000,
|
|
1222
|
+
reserveTokens: 2_000,
|
|
1223
|
+
keepRecentTokens: 9_000,
|
|
1224
|
+
}),
|
|
1225
|
+
),
|
|
1226
|
+
),
|
|
1227
|
+
)
|
|
1228
|
+
|
|
1229
|
+
assert.strictEqual(yield* send("first"), "answered")
|
|
1230
|
+
// The summary is still in flight when the first send ends.
|
|
1231
|
+
yield* Deferred.done(firstSendDone, Exit.void)
|
|
1232
|
+
assert.strictEqual(yield* send("second"), "answered")
|
|
1233
|
+
|
|
1234
|
+
assert.strictEqual(agentPrompts.length, 2)
|
|
1235
|
+
assertCompactedShape(agentPrompts[1]!, "SECOND-SEND-SUMMARY")
|
|
1236
|
+
}),
|
|
1237
|
+
).pipe(Effect.timeout("2 seconds")),
|
|
1238
|
+
)
|
|
1239
|
+
|
|
1240
|
+
it.live("waits for a running threshold summary when the call overflows", () =>
|
|
1241
|
+
Effect.gen(function* () {
|
|
1242
|
+
const callStarted = yield* Deferred.make<void>()
|
|
1243
|
+
let summarizerCalls = 0
|
|
1244
|
+
let agentCalls = 0
|
|
1245
|
+
const { exit, calls } = yield* runAgentCollect({
|
|
1246
|
+
history: fatHistory,
|
|
1247
|
+
compaction: {
|
|
1248
|
+
contextWindow: 12_000,
|
|
1249
|
+
reserveTokens: 2_000,
|
|
1250
|
+
keepRecentTokens: 9_000,
|
|
1251
|
+
},
|
|
1252
|
+
respond: (call) => {
|
|
1253
|
+
if (call.isSummarizer) {
|
|
1254
|
+
if (summarizerCalls++ > 0) {
|
|
1255
|
+
return Stream.fromIterable(text("OVERFLOW-SUMMARY"))
|
|
1256
|
+
}
|
|
1257
|
+
// Still running when the call overflows.
|
|
1258
|
+
return Stream.fromEffect(
|
|
1259
|
+
Deferred.await(callStarted).pipe(
|
|
1260
|
+
Effect.andThen(Effect.sleep("50 millis")),
|
|
1261
|
+
),
|
|
1262
|
+
).pipe(
|
|
1263
|
+
Stream.flatMap(() =>
|
|
1264
|
+
Stream.fromIterable(text("BACKGROUND-SUMMARY")),
|
|
1265
|
+
),
|
|
1266
|
+
)
|
|
1267
|
+
}
|
|
1268
|
+
switch (agentCalls++) {
|
|
1269
|
+
case 0:
|
|
1270
|
+
Deferred.doneUnsafe(callStarted, Exit.void)
|
|
1271
|
+
return Stream.fail(contextLengthError)
|
|
1272
|
+
case 1:
|
|
1273
|
+
return Stream.fromIterable(text("recovered"))
|
|
1274
|
+
default:
|
|
1275
|
+
return Stream.die(`unexpected model call #${agentCalls}`)
|
|
1276
|
+
}
|
|
1277
|
+
},
|
|
1278
|
+
}).pipe(Effect.timeout("2 seconds"))
|
|
1279
|
+
|
|
1280
|
+
assert.deepStrictEqual(exit, Exit.succeed("recovered"))
|
|
1281
|
+
assert.strictEqual(
|
|
1282
|
+
summarizerCalls,
|
|
1283
|
+
1,
|
|
1284
|
+
"the running summary must be awaited, not started over",
|
|
1285
|
+
)
|
|
1286
|
+
const last = calls.findLast((call) => !call.isSummarizer)!
|
|
1287
|
+
assertCompactedShape(last.prompt, "BACKGROUND-SUMMARY")
|
|
1288
|
+
}),
|
|
1289
|
+
)
|
|
1134
1290
|
})
|
|
1135
1291
|
|
|
1136
1292
|
describe("Agent overflow compaction", () => {
|