12 12 import { type KlausQueryHandle, startSdkQuery }
from "./agent-sdk.js";
15 + KlausCacheCorruptionError,
15 16 type KlausSessionStore,
17 18 MemorySessionStore,
44 45 toolCallIds:
Set<string
>;
45 46 tools: KlausRequest[
"tools"];
47 + boundaryFingerprint
?: string;
48 + boundaryMessageCount
?: number;
47 50 closePromise
?: Promise<void>;
48 51 timer
?: ReturnType
<typeof setTimeout
>;
49 53 idleLease
?: ReturnType
<typeof setTimeout
>;
50 54 abort
?: ()
=> void;
51 55 signal
?: AbortSignal;
56 60 thinking: SimpleStreamOptions[
"reasoning"];
61 + thinkingBudget
?: number;
62 + systemPrompt: string;
57 63 headers: SimpleStreamOptions[
"headers"];
64 + env: SimpleStreamOptions[
"env"];
58 65 metadata
?: Record
<string, string
>;
59 66 emittedContent: boolean;
245 - dbg?.(
"coordinator.sink.toolUse", { finished: this.finished });
256 + cacheWrite: number;
258 + responseId
?: string,
259 + responseModel
?: string,
261 + dbg?.(
"coordinator.sink.toolUse", { finished: this.finished, usage });
246 262 if (this.finished)
return;
247 263 this.finished
= true;
264 + this.
applyUsage(usage, responseId, responseModel);
248 265 this.output.stopReason
= "toolUse";
249 266 this.stream.
push({ type:
"done", reason:
"toolUse", message: this.output });
250 267 this.stream.
end();
271 288 if (this.finished)
return;
272 289 this.finished
= true;
290 + this.
applyUsage(usage, responseId, responseModel);
291 + this.output.stopReason
= stopReason;
294 + reason: stopReason,
295 + message: this.output,
300 + private
applyUsage(
305 + cacheWrite: number;
307 + responseId
?: string,
308 + responseModel
?: string,
273 310 Object.
assign(this.output.usage, usage);
274 311 this.output.usage.totalTokens
= 275 312 usage.
input + usage.output
+ usage.cacheRead
+ usage.cacheWrite;
292 329 this.output.responseId
= responseId;
293 330 this.output.responseModel
= responseModel;
294 - this.output.stopReason
= stopReason;
297 - reason: stopReason,
298 - message: this.output,
303 333 error(error: unknown, aborted
= false):
void {
440 470 hasHandle:
Boolean(continuation?.handle),
442 472 if (continuation?.handle) {
473 + const resultCount
= batch.results.
length;
474 + const boundaryMessageCount
= context.messages.
length - resultCount;
475 + const boundaryFingerprint
= contextFingerprint(
477 + systemPrompt: context.systemPrompt,
478 + messages: context.messages.
slice(
0, boundaryMessageCount),
479 + tools: context.tools,
481 + `${model.id}\0${this.currentCwd()}`,
443 483 const incompatible
= 444 484 batch.userAfter
|| 445 - continuation.handle.bridge.
hasSchemaChange(tools)
|| 485 + continuation.boundaryMessageCount
!== boundaryMessageCount
|| 486 + continuation.boundaryFingerprint
!== boundaryFingerprint
|| 487 + JSON.
stringify(continuation.tools)
!== JSON.
stringify(tools)
|| 446 488 continuation.modelId
!== model.
id || 489 + continuation.cwd
!== this.
currentCwd()
|| 490 + continuation.systemPrompt
!== (context.systemPrompt
?? "")
|| 447 491 continuation.thinking
!== options.reasoning
|| 448 - JSON.
stringify(continuation.
headers ?? {})
!== 449 - JSON.
stringify(options.
headers ?? {})
|| 450 - options.onPayload
!== undefined;
492 + continuation.thinkingBudget
!== thinkingBudget(options)
|| 493 + !sameStringRecord(continuation.
headers, options.
headers)
|| 494 + !sameStringRecord(continuation.env, options.env);
451 495 dbg?.(
"coordinator.invoke.compatibility", { incompatible });
452 496 if (incompatible) {
453 497 await this.
closeState(
460 504 if (continuation.idleLease)
clearTimeout(continuation.idleLease);
461 505 continuation.idleLease
= undefined;
462 506 this.
replaceAbort(continuation, options.signal);
463 - this.
armTimeout(continuation, options.timeoutMs);
507 + continuation.timeoutMs
= options.timeoutMs;
508 + this.
armTimeout(continuation, continuation.timeoutMs);
509 + if (options.signal?.aborted) {
510 + await this.
closeState(
512 + "Pi aborted the Klaus query.",
464 517 if (options.onResponse
&& continuation.metadata) {
465 518 await options.
onResponse(
466 519 { status:
200, headers: continuation.metadata },
523 + if (options.signal?.aborted) {
524 + await this.
closeState(
526 + "Pi aborted the Klaus query.",
470 531 for (
const result
of batch.results) {
471 532 await continuation.handle.bridge.
waitForPending(
536 + if (options.signal?.aborted) {
537 + await this.
closeState(
539 + "Pi aborted the Klaus query.",
475 544 if (
!continuation.handle.bridge.
settle(result)) {
476 545 throw new Error(
`Unknown Klaus tool result ${result.id}.`);
513 582 !forceCold
&& options.sessionId
514 583 ? this.completed.
get(options.sessionId)
516 - if (memory) candidates.
push(memory);
585 + if (memory?.position) candidates.
push(memory);
517 586 dbg?.(
"coordinator.start.memoryCandidate", { found:
Boolean(memory) });
518 587 const checkpointRoot
= this.
checkpointRoot(options.sessionId);
519 588 if (
!forceCold
&& options.sessionId
&& checkpointRoot) {
523 592 options.sessionId,
527 - (item)
=> item.fingerprint
=== checkpoint.fingerprint,
530 - candidates.
push({
...checkpoint, store });
594 + if (checkpoint.position) candidates.
push({
...checkpoint, store });
533 596 }
catch (error) {
534 597 dbg?.(
"coordinator.start.checkpointLoadError", {
535 598 error: error
instanceof Error ? error.message
: String(error),
537 - candidates.
length = memory
? 1 : 0;
600 + candidates.
length = memory?.position
? 1 : 0;
540 603 dbg?.(
"coordinator.start.candidates", { count: candidates.
length });
541 - const completed
= candidates.
toReversed().
find(
543 - checkpoint.messageCount
< context.messages.
length && 544 - checkpoint.fingerprint
=== 545 - contextFingerprint(
547 - systemPrompt: context.systemPrompt,
548 - messages: context.messages.
slice(
0, checkpoint.messageCount),
549 - tools: context.tools,
604 + const prefixFingerprints
= new Map<
number,
string>();
605 + let completed
: CompletedQuery | undefined;
606 + for (
const checkpoint
of candidates) {
607 + if (checkpoint.messageCount
>= context.messages.
length)
continue;
608 + let fingerprint
= prefixFingerprints.
get(checkpoint.messageCount);
609 + if (
!fingerprint) {
610 + fingerprint
= contextFingerprint(
612 + systemPrompt: context.systemPrompt,
613 + messages: context.messages.
slice(
0, checkpoint.messageCount),
614 + tools: context.tools,
618 + prefixFingerprints.
set(checkpoint.messageCount, fingerprint);
621 + checkpoint.fingerprint
=== fingerprint
&& 622 + (
!completed
|| checkpoint.messageCount
> completed.messageCount)
624 + completed
= checkpoint;
554 627 dbg?.(
"coordinator.start.completed", {
555 628 found:
Boolean(completed),
556 629 messageCount: completed?.messageCount,
591 664 timeoutMs: options.timeoutMs,
592 665 sessionStore: staging,
593 666 resume: canResume
? completed?.sdkSessionId
: undefined,
594 - resumeAt: canResume
&& !linearResume
? completed?.position
: undefined,
595 - forkSession: canResume
&& !linearResume,
667 + resumeAt: canResume
? completed?.position
: undefined,
668 + forkSession: canResume,
597 670 dbg?.(
"coordinator.start.request", {
598 671 modelId: request.modelId,
644 717 transformed.cwd
!== request.cwd
|| 645 718 JSON.
stringify(transformed.tools)
!== JSON.
stringify(request.tools))
647 - transformed.resume
= undefined;
648 - transformed.resumeAt
= undefined;
649 - transformed.forkSession
= undefined;
650 - transformed.
prompt = replayPrompt(context);
651 - transformed.
images = replayImages(context);
720 + await this.
start(sink, model, context, options, tools,
true);
653 723 const scope
= this.
currentSession();
654 724 dbg?.(
"coordinator.start.scope", {
661 731 sessionId: options.sessionId,
663 733 toolCallIds:
new Set(),
664 - tools: transformed.tools,
736 + timeoutMs: transformed.timeoutMs,
666 737 secondary:
Boolean(options.sessionId
&& options.sessionId
!== scope?.
id),
667 738 committable:
true,
668 739 modelId: model.
id,
670 741 thinking: options.reasoning,
742 + thinkingBudget:
thinkingBudget(options),
743 + systemPrompt: context.systemPrompt
?? "",
671 744 headers: options.
headers,
672 746 emittedContent:
false,
674 748 store: completed?.store
?? store,
696 770 onActivity: ()
=> {
697 771 dbg?.(
"coordinator.callback.activity", { closed: state.
closed });
698 - this.
touchState(state, transformed.timeoutMs);
772 + this.
touchState(state, state.timeoutMs);
700 774 onContent: (event)
=> {
701 775 dbg?.(
"coordinator.callback.content", {
708 782 if (
event.
type === "tool-start") state.toolCallIds.
add(
event.
id);
709 783 state.sink?.
content(
event);
711 - onToolBoundary: ()
=> {
785 + onToolBoundary: (result)
=> {
712 786 dbg?.(
"coordinator.callback.toolBoundary", {
713 787 closed: state.
closed,
714 788 sessionId: state.sessionId,
728 802 if (state.timer)
clearTimeout(state.timer);
729 803 state.timer
= undefined;
730 - state.sink?.
toolUse();
804 + state.sink?.
toolUse(
807 + result.responseModel,
810 + state.boundaryMessageCount
= state.context.messages.
length + 1;
811 + state.boundaryFingerprint
= contextFingerprint(
813 + systemPrompt: state.context.systemPrompt,
814 + messages: [
...state.context.messages, state.sink.output],
815 + tools: state.context.tools,
817 + `${state.modelId}\0${state.cwd}`,
731 820 state.sink
= undefined;
732 821 if (state.secondary) {
733 822 state.idleLease
= setTimeout(
746 835 closed: state.
closed,
748 837 if (state.
closed)
return;
752 - result.responseModel,
838 + const resultSink
= state.sink;
755 839 void (
async ()
=> {
757 841 state.committable
&& 758 842 state.sessionId
&& 759 843 result.sdkSessionId
&& 762 - await state.staging.
commit();
763 - const completed
: CompletedQuery = {
764 - sdkSessionId: result.sdkSessionId,
765 - position: result.position,
766 - messageCount: state.context.messages.
length + 1,
768 - fingerprint:
contextFingerprint(
770 - systemPrompt: state.context.systemPrompt,
771 - messages: [
...state.context.messages, state.sink.output],
772 - tools: state.context.tools,
774 - `${state.modelId}\0${state.cwd}`,
776 - store: state.store,
778 - this.completed.
set(state.sessionId, completed);
779 - if (this.
checkpointRoot(state.sessionId)) {
780 - this.pendingCheckpoints.
set(state.sessionId, completed);
847 + await state.staging.
commit();
848 + if (
!state.
closed && state.committable) {
849 + const completed
: CompletedQuery = {
850 + sdkSessionId: result.sdkSessionId,
851 + position: result.position,
852 + messageCount: state.context.messages.
length + 1,
854 + fingerprint:
contextFingerprint(
856 + systemPrompt: state.context.systemPrompt,
857 + messages: [
...state.context.messages, resultSink.output],
858 + tools: state.context.tools,
860 + `${state.modelId}\0${state.cwd}`,
862 + store: state.store,
864 + this.completed.
set(state.sessionId, completed);
865 + if (this.
checkpointRoot(state.sessionId)) {
866 + this.pendingCheckpoints.
set(state.sessionId, completed);
871 + `Klaus could not cache the completed session: ${error instanceof Error ? error.message : String(error)}`,
875 + if (
!state.
closed) {
879 + result.responseModel,
783 883 await this.
closeState(state,
"Klaus query completed.");
784 884 })().
catch((error)
=> {
785 - state.sink?.
error(error);
885 + resultSink?.
error(error);
786 886 void this.
closeState(state,
"Klaus checkpoint failed.");
794 894 emittedContent: state.emittedContent,
796 896 if (state.
closed)
return;
797 - if (transformed.resume
&& !state.emittedContent
&& state.sink) {
898 + transformed.resume
&& 899 + !state.emittedContent
&& 901 + isReplayableResumeError(error)
798 903 const retrySink
= state.sink;
799 904 state.sink
= undefined;
800 905 if (state.sessionId) {
1107 +function sameStringRecord(
1108 + left
: Record<
string,
string | null>
| undefined,
1109 + right
: Record<
string,
string | null>
| undefined,
1111 + const entries = (value
: Record<
string,
string | null>
| undefined)
=> 1112 + Object.
entries(value
?? {}).
sort(([leftKey], [rightKey])
=> 1113 + leftKey
< rightKey
? -1 : leftKey
> rightKey
? 1 : 0,
1115 + return JSON.
stringify(
entries(left))
=== JSON.
stringify(
entries(right));
1118 +export function isReplayableResumeError(error
: Error)
: boolean {
1120 + error
instanceof KlausCacheCorruptionError || 1121 + /^Klaus cache corruption:/i.
test(error.message)
|| 1122 + /^Resume rejected by --resume-drops-turn:/i.
test(error.message)
1002 1126 function thinkingBudget(options
: SimpleStreamOptions)
: number | undefined {
1003 1127 dbg?.(
"coordinator.thinkingBudget", { reasoning: options.reasoning });
1004 1128 switch (options.reasoning) {