@demicodes/agent 0.25.0 → 0.25.2
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/client-entry.d.mts +2 -2
- package/dist/index.d.mts +10 -14
- package/dist/index.mjs +110 -52
- package/dist/stdio-transport.d.mts +1 -1
- package/dist/{transport-vzA9vMxT.d.mts → transport-Qj50JTjx.d.mts} +3 -2
- package/dist/{websocket-transport-CyoM_NS1.d.mts → websocket-transport-Ax8tieib.d.mts} +1 -1
- package/package.json +5 -5
package/dist/client-entry.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { f as TranscriptPatch, l as ServerFrame, n as AgentServerTransport, o as ClientFrame, r as AgentTransport, s as ClientSessionEvent, t as AgentClientTransport, u as ShellCommandStatusLike, x as AgentMetadata } from "./transport-
|
|
2
|
-
import { a as AgentClient, i as AgentActionOptions, n as createWebSocketClientTransport, o as AgentClientListener, r as createWebSocketServerTransport, t as JsonWebSocket } from "./websocket-transport-
|
|
1
|
+
import { f as TranscriptPatch, l as ServerFrame, n as AgentServerTransport, o as ClientFrame, r as AgentTransport, s as ClientSessionEvent, t as AgentClientTransport, u as ShellCommandStatusLike, x as AgentMetadata } from "./transport-Qj50JTjx.mjs";
|
|
2
|
+
import { a as AgentClient, i as AgentActionOptions, n as createWebSocketClientTransport, o as AgentClientListener, r as createWebSocketServerTransport, t as JsonWebSocket } from "./websocket-transport-Ax8tieib.mjs";
|
|
3
3
|
import { ProviderSelection } from "@demicodes/provider";
|
|
4
4
|
export { type AgentActionOptions, AgentClient, type AgentClientListener, type AgentClientTransport, type AgentMetadata, type AgentServerTransport, type AgentTransport, type ClientFrame, type ClientSessionEvent, type JsonWebSocket, type ProviderSelection, type ServerFrame, type ShellCommandStatusLike, type TranscriptPatch, createWebSocketClientTransport, createWebSocketServerTransport };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { A as AgentSystemPromptContext, B as CompactionWindow, C as AgentReferenceResolveContext, D as AgentSessionParams, E as AgentSessionOptions, F as ExternalMutationReservation, G as DEFAULT_TURN_RETRY_POLICY, H as TranscriptLog, I as ModelSwitchApply, J as resolveRetryPolicy, K as TurnRetryPolicy, L as SessionEvent, M as AgentToolContext, N as AgentToolInvokeContext, O as AgentSessionRestoreParams, P as AgentToolInvokeResult, R as SessionEventListener, S as AgentPromptContext, T as AgentSessionCloneParams, U as TranscriptOptions, V as DrainedTranscriptPatches, W as estimateTranscriptBlockTokens, Y as retryDelayMs, _ as AgentHarnessContext, a as createInProcessTransportPair, b as AgentLifecycleEvent, c as ConversationSummary, d as SubagentJob, f as TranscriptPatch, g as AgentHarness, h as AgentDisposeContext, i as InProcessTransportPair, j as AgentTool, k as AgentSessionStore, l as ServerFrame, m as AbortTarget, n as AgentServerTransport, o as ClientFrame, p as AbortResult, q as isRetryableCode, r as AgentTransport, s as ClientSessionEvent, t as AgentClientTransport, u as ShellCommandStatusLike, v as AgentHarnessRuntime, w as AgentSessionCheckpoint, x as AgentMetadata, y as AgentHostContext, z as SubagentProfile } from "./transport-
|
|
2
|
-
import { a as AgentClient, i as AgentActionOptions, n as createWebSocketClientTransport, o as AgentClientListener, r as createWebSocketServerTransport, t as JsonWebSocket } from "./websocket-transport-
|
|
1
|
+
import { A as AgentSystemPromptContext, B as CompactionWindow, C as AgentReferenceResolveContext, D as AgentSessionParams, E as AgentSessionOptions, F as ExternalMutationReservation, G as DEFAULT_TURN_RETRY_POLICY, H as TranscriptLog, I as ModelSwitchApply, J as resolveRetryPolicy, K as TurnRetryPolicy, L as SessionEvent, M as AgentToolContext, N as AgentToolInvokeContext, O as AgentSessionRestoreParams, P as AgentToolInvokeResult, R as SessionEventListener, S as AgentPromptContext, T as AgentSessionCloneParams, U as TranscriptOptions, V as DrainedTranscriptPatches, W as estimateTranscriptBlockTokens, Y as retryDelayMs, _ as AgentHarnessContext, a as createInProcessTransportPair, b as AgentLifecycleEvent, c as ConversationSummary, d as SubagentJob, f as TranscriptPatch, g as AgentHarness, h as AgentDisposeContext, i as InProcessTransportPair, j as AgentTool, k as AgentSessionStore, l as ServerFrame, m as AbortTarget, n as AgentServerTransport, o as ClientFrame, p as AbortResult, q as isRetryableCode, r as AgentTransport, s as ClientSessionEvent, t as AgentClientTransport, u as ShellCommandStatusLike, v as AgentHarnessRuntime, w as AgentSessionCheckpoint, x as AgentMetadata, y as AgentHostContext, z as SubagentProfile } from "./transport-Qj50JTjx.mjs";
|
|
2
|
+
import { a as AgentClient, i as AgentActionOptions, n as createWebSocketClientTransport, o as AgentClientListener, r as createWebSocketServerTransport, t as JsonWebSocket } from "./websocket-transport-Ax8tieib.mjs";
|
|
3
3
|
import { BashAuditEvent, BashEnvironment, BashEnvironmentOptions, Command, CommandMetadataRecord, CommandRegistry, Host, HostStore, ShellCommandStatus, ShellOutputChunk } from "@demicodes/shell";
|
|
4
|
-
import { AgentProvider, Provider } from "@demicodes/provider";
|
|
4
|
+
import { AgentProvider, Provider, ProviderSelection } from "@demicodes/provider";
|
|
5
5
|
import { Block, Model, ModelMediaKind, ModelSelection, ProviderErrorDiagnostics, QueuedMessage, SessionPhase, UserContentBlock } from "@demicodes/core";
|
|
6
6
|
//#region src/recovery.d.ts
|
|
7
7
|
/**
|
|
@@ -40,6 +40,7 @@ type ActiveTurnPhase = 'provider_streaming' | 'tool_executing' | 'compacting' |
|
|
|
40
40
|
declare class AgentSession<State> {
|
|
41
41
|
private provider;
|
|
42
42
|
private model;
|
|
43
|
+
private modelNeedsCompaction;
|
|
43
44
|
private pendingModelSwitch;
|
|
44
45
|
private readonly cwd;
|
|
45
46
|
private readonly runtime;
|
|
@@ -132,8 +133,8 @@ declare class AgentSession<State> {
|
|
|
132
133
|
* start of the next queued action, so a running turn finishes entirely on the old model;
|
|
133
134
|
* 'immediate' also applies mid-turn at the next sampling/tool continuation, so the very
|
|
134
135
|
* next request already runs on the new model (the same "as soon as possible" semantics
|
|
135
|
-
* steering has).
|
|
136
|
-
*
|
|
136
|
+
* steering has). The selection is applied before compaction, so recovery never
|
|
137
|
+
* depends on the previous provider remaining available. Recording is cheap
|
|
137
138
|
* and non-blocking so it never holds up an in-flight turn or other frames.
|
|
138
139
|
*/
|
|
139
140
|
updateModel(provider: AgentProvider | null, model: ModelSelection, apply?: ModelSwitchApply): void;
|
|
@@ -180,14 +181,8 @@ declare class AgentSession<State> {
|
|
|
180
181
|
private kickWorker;
|
|
181
182
|
private runWorker;
|
|
182
183
|
private executeAction;
|
|
183
|
-
/**
|
|
184
|
-
|
|
185
|
-
* new model's context window can't hold the current history, compaction runs FIRST with
|
|
186
|
-
* the current (pre-switch) model + provider — which can still load it to summarize — and
|
|
187
|
-
* only then do we swap. Doing it the other way would ask the smaller model to summarize
|
|
188
|
-
* a history it may not be able to load. Returns whether that compaction actually ran
|
|
189
|
-
* (false when no switch was pending).
|
|
190
|
-
*/
|
|
184
|
+
/** Apply selection independently of whether the selected model can summarize. */
|
|
185
|
+
private applyModelSelection;
|
|
191
186
|
private applyPendingModelSwitch;
|
|
192
187
|
/**
|
|
193
188
|
* Turn-loop variant for mid-turn continuation boundaries: only switches recorded with
|
|
@@ -521,6 +516,7 @@ interface ChildSupervisorOptions<State> {
|
|
|
521
516
|
shellOptions: Omit<BashEnvironmentOptions, 'host' | 'commands'>;
|
|
522
517
|
prepareShell: PrepareShell | null;
|
|
523
518
|
sessionOptions: AgentServerSessionOptions;
|
|
519
|
+
createRuntime(selection: ProviderSelection): Promise<AgentProvider>;
|
|
524
520
|
/** When false, a child closing never wakes an idle parent; the host app orchestrates the wakeup from the `subagent closed` frame. */
|
|
525
521
|
notifyParentOnIdle: boolean;
|
|
526
522
|
/** Backing store for child checkpoints and job metadata. */
|
|
@@ -621,6 +617,7 @@ declare class ChildSupervisor<State = unknown> {
|
|
|
621
617
|
/** Aborts every live child (each with its subtree); the archive is untouched. */
|
|
622
618
|
abortAll(): Promise<void>;
|
|
623
619
|
private spawn;
|
|
620
|
+
private createChildProvider;
|
|
624
621
|
/**
|
|
625
622
|
* Everything spawn and restore share: the command tree (identical to the
|
|
626
623
|
* owner's — children spawn, message, and observe exactly like any session),
|
|
@@ -631,7 +628,6 @@ declare class ChildSupervisor<State = unknown> {
|
|
|
631
628
|
private attachSession;
|
|
632
629
|
private childStoreKey;
|
|
633
630
|
private childSessionStore;
|
|
634
|
-
private deletePersistedJob;
|
|
635
631
|
/** Shared spawn/resume foreground behaviour: announce the id, wire abort and stdin steers, wait for close, report the outcome. */
|
|
636
632
|
private attendChild;
|
|
637
633
|
/**
|
package/dist/index.mjs
CHANGED
|
@@ -833,25 +833,31 @@ const COMPACTION_SUMMARY_INSTRUCTION = "Summarize the conversation above into a
|
|
|
833
833
|
*/
|
|
834
834
|
var CompactionController = class {
|
|
835
835
|
host;
|
|
836
|
+
revision = 0;
|
|
837
|
+
summaryAbort = null;
|
|
836
838
|
constructor(host) {
|
|
837
839
|
this.host = host;
|
|
838
840
|
}
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
if (contextWindow <= 0) return false;
|
|
846
|
-
const threshold = resolveCompactionThreshold(contextWindow, this.host.thresholdRatio, this.host.thresholdTokens);
|
|
847
|
-
if (this.host.transcript.estimateContextTokens(contextWindow) < threshold) return false;
|
|
841
|
+
interrupt() {
|
|
842
|
+
this.revision += 1;
|
|
843
|
+
this.summaryAbort?.abort();
|
|
844
|
+
}
|
|
845
|
+
/** Compact against the live selection, retaining it even if summarization fails. */
|
|
846
|
+
async compactToFit() {
|
|
848
847
|
return this.host.runWithCompactingPhase(async () => {
|
|
849
848
|
let compacted = false;
|
|
850
|
-
for (let attempt = 0; attempt < 8
|
|
851
|
-
|
|
849
|
+
for (let attempt = 0; attempt < 8; attempt += 1) {
|
|
850
|
+
await this.host.applyModelSwitch();
|
|
851
|
+
const contextWindow = this.host.model.model.contextWindow;
|
|
852
|
+
if (contextWindow <= 0) return compacted;
|
|
853
|
+
const threshold = resolveCompactionThreshold(contextWindow, this.host.thresholdRatio, this.host.thresholdTokens);
|
|
854
|
+
const before = this.host.transcript.estimateContextTokens(contextWindow);
|
|
855
|
+
if (before < threshold) return compacted;
|
|
856
|
+
if (!await this.run()) throw new Error("Compaction cannot fit the selected model: no reducible history remains");
|
|
852
857
|
compacted = true;
|
|
858
|
+
if (this.host.model.model.contextWindow === contextWindow && this.host.transcript.estimateContextTokens(contextWindow) >= before) throw new Error("Compaction cannot fit the selected model: summary made no progress");
|
|
853
859
|
}
|
|
854
|
-
|
|
860
|
+
throw new Error("Compaction exceeded its pass limit; retry with the selected model or select a larger context window");
|
|
855
861
|
});
|
|
856
862
|
}
|
|
857
863
|
/** Runs one compaction pass before a turn when the current model is over threshold. */
|
|
@@ -864,9 +870,20 @@ var CompactionController = class {
|
|
|
864
870
|
}
|
|
865
871
|
/** Runs one compaction pass; returns whether it compacted anything. */
|
|
866
872
|
async run() {
|
|
873
|
+
while (true) {
|
|
874
|
+
throwIfAborted(this.host.currentSignal());
|
|
875
|
+
await this.host.applyModelSwitch();
|
|
876
|
+
try {
|
|
877
|
+
return await this.runPass();
|
|
878
|
+
} catch (error) {
|
|
879
|
+
if (!(error instanceof SummarySuperseded)) throw error;
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
async runPass() {
|
|
867
884
|
const transcript = this.host.transcript;
|
|
868
885
|
if (transcript.pendingToolCalls().length > 0) return false;
|
|
869
|
-
const window = transcript.findCompactionWindow(this.host.keepRecentTokens);
|
|
886
|
+
const window = transcript.findCompactionWindow(Math.min(this.host.keepRecentTokens, Math.max(1, Math.floor(this.host.model.model.contextWindow / 4))));
|
|
870
887
|
if (window === null) return false;
|
|
871
888
|
let minCutPoint = window.startIndex;
|
|
872
889
|
while (minCutPoint < window.cutPoint) {
|
|
@@ -880,7 +897,10 @@ var CompactionController = class {
|
|
|
880
897
|
const compactedBlocks = transcript.blocks.slice(window.startIndex, cutPoint);
|
|
881
898
|
const compactedTokens = compactedBlocks.reduce((total, block) => total + estimateTranscriptBlockTokens(block), 0);
|
|
882
899
|
try {
|
|
900
|
+
const revision = this.revision;
|
|
883
901
|
const summary = await this.generateSummary(compactedBlocks);
|
|
902
|
+
throwIfAborted(this.host.currentSignal());
|
|
903
|
+
if (revision !== this.revision) throw new SummarySuperseded();
|
|
884
904
|
if (!summary) return false;
|
|
885
905
|
const boundary = transcript.insertCompactionBoundary(cutPoint, this.host.model, summary, estimateTokens(summary));
|
|
886
906
|
transcript.appendCompactionMarker(this.host.model, boundary.id, compactedTokens);
|
|
@@ -888,7 +908,12 @@ var CompactionController = class {
|
|
|
888
908
|
return true;
|
|
889
909
|
} catch (error) {
|
|
890
910
|
if (!isContextLengthExceeded(error)) throw error;
|
|
891
|
-
const
|
|
911
|
+
const midpoint = nextSmallerCompactionCutPoint(window.startIndex, cutPoint);
|
|
912
|
+
let nextCutPoint = null;
|
|
913
|
+
for (let index = minCutPoint + 1; index < cutPoint; index += 1) {
|
|
914
|
+
if (transcript.blocks[index - 1]?.type !== "response") continue;
|
|
915
|
+
if (nextCutPoint === null || midpoint !== null && index <= midpoint) nextCutPoint = index;
|
|
916
|
+
}
|
|
892
917
|
if (nextCutPoint === null) throw error;
|
|
893
918
|
cutPoint = nextCutPoint;
|
|
894
919
|
}
|
|
@@ -896,6 +921,8 @@ var CompactionController = class {
|
|
|
896
921
|
return false;
|
|
897
922
|
}
|
|
898
923
|
async generateSummary(blocks) {
|
|
924
|
+
const controller = new AbortController();
|
|
925
|
+
this.summaryAbort = controller;
|
|
899
926
|
const clone = this.host.clone({ blocks });
|
|
900
927
|
const baseBlockCount = clone.transcript().blocks.length;
|
|
901
928
|
const parentSignal = this.host.currentSignal();
|
|
@@ -906,21 +933,30 @@ var CompactionController = class {
|
|
|
906
933
|
if (event.type === "retry_scheduled") this.host.emit(event);
|
|
907
934
|
});
|
|
908
935
|
parentSignal.addEventListener("abort", abortClone, { once: true });
|
|
936
|
+
controller.signal.addEventListener("abort", abortClone, { once: true });
|
|
909
937
|
try {
|
|
910
938
|
throwIfAborted(parentSignal);
|
|
911
|
-
await clone.send([{
|
|
939
|
+
await abortable(clone.send([{
|
|
912
940
|
type: "text",
|
|
913
941
|
text: COMPACTION_SUMMARY_INSTRUCTION
|
|
914
|
-
}]);
|
|
942
|
+
}]), controller.signal);
|
|
915
943
|
throwIfAborted(parentSignal);
|
|
944
|
+
if (controller.signal.aborted) throw new SummarySuperseded();
|
|
916
945
|
return lastAssistantText$1(clone.transcript(), baseBlockCount).trim();
|
|
946
|
+
} catch (error) {
|
|
947
|
+
throwIfAborted(parentSignal);
|
|
948
|
+
if (controller.signal.aborted) throw new SummarySuperseded();
|
|
949
|
+
throw error;
|
|
917
950
|
} finally {
|
|
951
|
+
this.summaryAbort = null;
|
|
952
|
+
controller.signal.removeEventListener("abort", abortClone);
|
|
918
953
|
parentSignal.removeEventListener("abort", abortClone);
|
|
919
954
|
unsubscribe();
|
|
920
955
|
await clone.dispose();
|
|
921
956
|
}
|
|
922
957
|
}
|
|
923
958
|
};
|
|
959
|
+
var SummarySuperseded = class extends Error {};
|
|
924
960
|
function lastAssistantText$1(transcript, startIndex) {
|
|
925
961
|
for (let index = transcript.blocks.length - 1; index >= startIndex; index -= 1) {
|
|
926
962
|
const block = transcript.blocks[index];
|
|
@@ -1191,6 +1227,7 @@ const DEFAULT_PERSIST_INTERVAL_MS = 1e3;
|
|
|
1191
1227
|
var AgentSession = class AgentSession {
|
|
1192
1228
|
provider;
|
|
1193
1229
|
model;
|
|
1230
|
+
modelNeedsCompaction = false;
|
|
1194
1231
|
pendingModelSwitch = null;
|
|
1195
1232
|
cwd;
|
|
1196
1233
|
runtime;
|
|
@@ -1290,6 +1327,7 @@ var AgentSession = class AgentSession {
|
|
|
1290
1327
|
return self.compactionThresholdTokens;
|
|
1291
1328
|
},
|
|
1292
1329
|
currentSignal: () => self.currentSignal(),
|
|
1330
|
+
applyModelSwitch: () => self.applyModelSelection(true),
|
|
1293
1331
|
clone: (transcript) => self.clone({
|
|
1294
1332
|
transcript,
|
|
1295
1333
|
options: {
|
|
@@ -1494,16 +1532,23 @@ var AgentSession = class AgentSession {
|
|
|
1494
1532
|
* start of the next queued action, so a running turn finishes entirely on the old model;
|
|
1495
1533
|
* 'immediate' also applies mid-turn at the next sampling/tool continuation, so the very
|
|
1496
1534
|
* next request already runs on the new model (the same "as soon as possible" semantics
|
|
1497
|
-
* steering has).
|
|
1498
|
-
*
|
|
1535
|
+
* steering has). The selection is applied before compaction, so recovery never
|
|
1536
|
+
* depends on the previous provider remaining available. Recording is cheap
|
|
1499
1537
|
* and non-blocking so it never holds up an in-flight turn or other frames.
|
|
1500
1538
|
*/
|
|
1501
1539
|
updateModel(provider, model, apply = "next_turn") {
|
|
1540
|
+
const previous = this.pendingModelSwitch?.provider;
|
|
1541
|
+
const next = provider ?? previous ?? null;
|
|
1502
1542
|
this.pendingModelSwitch = {
|
|
1503
|
-
provider,
|
|
1543
|
+
provider: next,
|
|
1504
1544
|
model,
|
|
1505
1545
|
apply
|
|
1506
1546
|
};
|
|
1547
|
+
if (previous && previous !== next && previous !== this.provider) Promise.resolve().then(() => previous.dispose?.()).catch((error) => this.emit({
|
|
1548
|
+
type: "error",
|
|
1549
|
+
error: asError(error)
|
|
1550
|
+
}));
|
|
1551
|
+
if (apply === "immediate") this.compaction.interrupt();
|
|
1507
1552
|
}
|
|
1508
1553
|
async abort() {
|
|
1509
1554
|
if (this.currentAbortController && !this.currentAbortController.signal.aborted) {
|
|
@@ -1892,25 +1937,23 @@ var AgentSession = class AgentSession {
|
|
|
1892
1937
|
return;
|
|
1893
1938
|
}
|
|
1894
1939
|
}
|
|
1895
|
-
/**
|
|
1896
|
-
|
|
1897
|
-
* new model's context window can't hold the current history, compaction runs FIRST with
|
|
1898
|
-
* the current (pre-switch) model + provider — which can still load it to summarize — and
|
|
1899
|
-
* only then do we swap. Doing it the other way would ask the smaller model to summarize
|
|
1900
|
-
* a history it may not be able to load. Returns whether that compaction actually ran
|
|
1901
|
-
* (false when no switch was pending).
|
|
1902
|
-
*/
|
|
1903
|
-
async applyPendingModelSwitch() {
|
|
1940
|
+
/** Apply selection independently of whether the selected model can summarize. */
|
|
1941
|
+
async applyModelSelection(immediateOnly = false) {
|
|
1904
1942
|
const pending = this.pendingModelSwitch;
|
|
1905
|
-
if (!pending) return false;
|
|
1943
|
+
if (!pending || immediateOnly && pending.apply !== "immediate") return false;
|
|
1906
1944
|
this.pendingModelSwitch = null;
|
|
1907
|
-
const
|
|
1908
|
-
|
|
1909
|
-
const previous = this.provider;
|
|
1910
|
-
this.provider = pending.provider;
|
|
1911
|
-
await previous.dispose?.();
|
|
1912
|
-
}
|
|
1945
|
+
const previous = this.provider;
|
|
1946
|
+
this.provider = pending.provider ?? previous;
|
|
1913
1947
|
this.model = pending.model;
|
|
1948
|
+
this.modelNeedsCompaction = true;
|
|
1949
|
+
if (previous !== this.provider) await previous.dispose?.();
|
|
1950
|
+
return true;
|
|
1951
|
+
}
|
|
1952
|
+
async applyPendingModelSwitch(immediateOnly = false) {
|
|
1953
|
+
await this.applyModelSelection(immediateOnly);
|
|
1954
|
+
if (!this.modelNeedsCompaction) return false;
|
|
1955
|
+
const compacted = await this.compaction.compactToFit();
|
|
1956
|
+
this.modelNeedsCompaction = false;
|
|
1914
1957
|
return compacted;
|
|
1915
1958
|
}
|
|
1916
1959
|
/**
|
|
@@ -1919,8 +1962,7 @@ var AgentSession = class AgentSession {
|
|
|
1919
1962
|
* finishes and the next action starts.
|
|
1920
1963
|
*/
|
|
1921
1964
|
async applyImmediateModelSwitch() {
|
|
1922
|
-
|
|
1923
|
-
return this.applyPendingModelSwitch();
|
|
1965
|
+
return this.applyPendingModelSwitch(true);
|
|
1924
1966
|
}
|
|
1925
1967
|
async runWithCompactingPhase(fn) {
|
|
1926
1968
|
const previousPhase = this.currentPhase;
|
|
@@ -3080,8 +3122,11 @@ var ChildSupervisor = class ChildSupervisor {
|
|
|
3080
3122
|
if (this.jobs.has(id)) continue;
|
|
3081
3123
|
try {
|
|
3082
3124
|
await this.restoreJob(id);
|
|
3083
|
-
} catch {
|
|
3084
|
-
|
|
3125
|
+
} catch (error) {
|
|
3126
|
+
this.options.emit({
|
|
3127
|
+
type: "error",
|
|
3128
|
+
message: `Failed to restore subagent "${id}": ${errorMessage(error)}`
|
|
3129
|
+
});
|
|
3085
3130
|
}
|
|
3086
3131
|
}
|
|
3087
3132
|
}
|
|
@@ -3091,15 +3136,15 @@ var ChildSupervisor = class ChildSupervisor {
|
|
|
3091
3136
|
if (meta?.closedPhase) return;
|
|
3092
3137
|
const checkpoint = await this.childSessionStore(id).loadCheckpoint();
|
|
3093
3138
|
if (!meta || !checkpoint) throw new Error("incomplete persisted subagent");
|
|
3094
|
-
const
|
|
3139
|
+
const provider = await this.createChildProvider(checkpoint.model);
|
|
3140
|
+
const job = this.reassembleJob(id, meta, checkpoint, provider);
|
|
3095
3141
|
this.trackTurn(job, job.session.resume(meta.metadata ? { metadata: meta.metadata } : {}));
|
|
3096
3142
|
this.settleJob(job);
|
|
3097
3143
|
await job.ownSupervisor.restore();
|
|
3098
3144
|
}
|
|
3099
3145
|
/** Shared by restore and resume: rebuild a persisted child's job and session from its checkpoint. */
|
|
3100
|
-
reassembleJob(id, meta, checkpoint) {
|
|
3101
|
-
|
|
3102
|
-
if (!parent) throw new Error("subagent supervisor has no owner session");
|
|
3146
|
+
reassembleJob(id, meta, checkpoint, provider) {
|
|
3147
|
+
if (!this.parentSession) throw new Error("subagent supervisor has no owner session");
|
|
3103
3148
|
const profile = this.resolveProfile(meta.profileName ?? void 0);
|
|
3104
3149
|
const { job, runtime } = this.assembleJob({
|
|
3105
3150
|
id,
|
|
@@ -3111,7 +3156,7 @@ var ChildSupervisor = class ChildSupervisor {
|
|
|
3111
3156
|
canSpawnSubagents: meta.canSpawnSubagents !== false
|
|
3112
3157
|
});
|
|
3113
3158
|
const session = AgentSession.fromCheckpoint({
|
|
3114
|
-
provider
|
|
3159
|
+
provider,
|
|
3115
3160
|
runtime,
|
|
3116
3161
|
checkpoint
|
|
3117
3162
|
}, {
|
|
@@ -3146,8 +3191,9 @@ var ChildSupervisor = class ChildSupervisor {
|
|
|
3146
3191
|
spawnedAt: Date.now(),
|
|
3147
3192
|
...meta.canSpawnSubagents === false ? { canSpawnSubagents: false } : {}
|
|
3148
3193
|
};
|
|
3194
|
+
const provider = await this.createChildProvider(checkpoint.model);
|
|
3149
3195
|
await this.options.store.writeJson(this.childStoreKey(id, "job.json"), liveMeta);
|
|
3150
|
-
const job = this.reassembleJob(id, liveMeta, checkpoint);
|
|
3196
|
+
const job = this.reassembleJob(id, liveMeta, checkpoint, provider);
|
|
3151
3197
|
this.trackTurn(job, job.session.send([{
|
|
3152
3198
|
type: "text",
|
|
3153
3199
|
text: message
|
|
@@ -3188,8 +3234,10 @@ var ChildSupervisor = class ChildSupervisor {
|
|
|
3188
3234
|
if (this.isDisposed) throw new Error("owner session is closing");
|
|
3189
3235
|
if (this.jobs.size >= this.options.maxLiveSubagents) throw new Error(`at most ${this.options.maxLiveSubagents} running subagents per session; abort one or wait for a result`);
|
|
3190
3236
|
const profile = this.resolveProfile(input.profileName);
|
|
3191
|
-
const
|
|
3237
|
+
const model = structuredClone(profile.model ?? parent.modelSelection);
|
|
3192
3238
|
const metadata = parent.actionMetadata();
|
|
3239
|
+
const provider = await this.createChildProvider(model);
|
|
3240
|
+
const id = createId();
|
|
3193
3241
|
const profileName = input.profileName ?? null;
|
|
3194
3242
|
const spawnedAt = Date.now();
|
|
3195
3243
|
const canSpawnSubagents = !input.isSpawnForbidden && profile.canSpawnSubagents !== false;
|
|
@@ -3210,8 +3258,8 @@ var ChildSupervisor = class ChildSupervisor {
|
|
|
3210
3258
|
...canSpawnSubagents ? {} : { canSpawnSubagents }
|
|
3211
3259
|
});
|
|
3212
3260
|
const session = new AgentSession({
|
|
3213
|
-
provider
|
|
3214
|
-
model
|
|
3261
|
+
provider,
|
|
3262
|
+
model,
|
|
3215
3263
|
cwd: this.options.cwd,
|
|
3216
3264
|
runtime,
|
|
3217
3265
|
state: this.options.agent.initialState()
|
|
@@ -3228,6 +3276,19 @@ var ChildSupervisor = class ChildSupervisor {
|
|
|
3228
3276
|
this.settleJob(job);
|
|
3229
3277
|
return job;
|
|
3230
3278
|
}
|
|
3279
|
+
async createChildProvider(model) {
|
|
3280
|
+
const parent = this.parentSession;
|
|
3281
|
+
if (!parent || this.isDisposed) throw new Error("owner session is closing");
|
|
3282
|
+
const provider = model.providerId === parent.modelSelection.providerId ? parent.cloneProviderRuntime() : await this.options.createRuntime({
|
|
3283
|
+
providerId: model.providerId,
|
|
3284
|
+
model
|
|
3285
|
+
});
|
|
3286
|
+
if (this.isDisposed) {
|
|
3287
|
+
await provider.dispose?.();
|
|
3288
|
+
throw new Error("owner session is closing");
|
|
3289
|
+
}
|
|
3290
|
+
return provider;
|
|
3291
|
+
}
|
|
3231
3292
|
/**
|
|
3232
3293
|
* Everything spawn and restore share: the command tree (identical to the
|
|
3233
3294
|
* owner's — children spawn, message, and observe exactly like any session),
|
|
@@ -3343,10 +3404,6 @@ var ChildSupervisor = class ChildSupervisor {
|
|
|
3343
3404
|
loadCheckpoint: () => this.options.store.readJson(this.childStoreKey(childId, "checkpoint.json"))
|
|
3344
3405
|
};
|
|
3345
3406
|
}
|
|
3346
|
-
async deletePersistedJob(id) {
|
|
3347
|
-
await this.options.store.delete(this.childStoreKey(id, "checkpoint.json")).catch(noop);
|
|
3348
|
-
await this.options.store.delete(this.childStoreKey(id, "job.json")).catch(noop);
|
|
3349
|
-
}
|
|
3350
3407
|
/** Shared spawn/resume foreground behaviour: announce the id, wire abort and stdin steers, wait for close, report the outcome. */
|
|
3351
3408
|
async attendChild(job, ctx) {
|
|
3352
3409
|
const { io, isJson, signal, stdinStream } = ctx;
|
|
@@ -4257,6 +4314,7 @@ var AgentTransportBindingImpl = class {
|
|
|
4257
4314
|
shellOptions: this.shellOptions,
|
|
4258
4315
|
prepareShell: this.prepareShell,
|
|
4259
4316
|
sessionOptions: this.sessionOptions,
|
|
4317
|
+
createRuntime: (selection) => this.createRuntime(selection),
|
|
4260
4318
|
notifyParentOnIdle: this.notifyParentOnIdle,
|
|
4261
4319
|
store: provisionalHost.store,
|
|
4262
4320
|
storePrefix: `agent-sessions/${agentSessionId}`,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { n as AgentServerTransport, t as AgentClientTransport } from "./transport-
|
|
1
|
+
import { n as AgentServerTransport, t as AgentClientTransport } from "./transport-Qj50JTjx.mjs";
|
|
2
2
|
import { Readable, Writable } from "node:stream";
|
|
3
3
|
//#region src/stdio-transport.d.ts
|
|
4
4
|
declare function createStdioClientTransport(readable: Readable, writable: Writable): AgentClientTransport;
|
|
@@ -196,6 +196,7 @@ interface SubagentProfile<State = unknown> {
|
|
|
196
196
|
readonly?: boolean;
|
|
197
197
|
/** When false, the child cannot spawn subagents of its own (communication and reads remain). */
|
|
198
198
|
canSpawnSubagents?: boolean;
|
|
199
|
+
/** Selects a model from any provider registered on AgentServer; omitted inherits the parent. */
|
|
199
200
|
model?: ModelSelection;
|
|
200
201
|
}
|
|
201
202
|
interface AgentHarness<State = unknown> {
|
|
@@ -205,8 +206,8 @@ interface AgentHarness<State = unknown> {
|
|
|
205
206
|
host(ctx: AgentHarnessContext<State> | AgentHostContext<State>): Host | Promise<Host>;
|
|
206
207
|
commands?(ctx: AgentHarnessContext<State>): Promise<Command[]> | Command[];
|
|
207
208
|
/**
|
|
208
|
-
* Named subagent profiles for `demi agent`.
|
|
209
|
-
*
|
|
209
|
+
* Named subagent profiles for `demi agent`. Omitting --profile always
|
|
210
|
+
* inherits the parent, independently of this list.
|
|
210
211
|
*/
|
|
211
212
|
agents?(ctx: AgentHarnessContext<State>): Promise<SubagentProfile<State>[]> | SubagentProfile<State>[];
|
|
212
213
|
systemPrompt(ctx: AgentSystemPromptContext<State>): Promise<string> | string;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { I as ModelSwitchApply, c as ConversationSummary, n as AgentServerTransport, p as AbortResult, s as ClientSessionEvent, t as AgentClientTransport, x as AgentMetadata } from "./transport-
|
|
1
|
+
import { I as ModelSwitchApply, c as ConversationSummary, n as AgentServerTransport, p as AbortResult, s as ClientSessionEvent, t as AgentClientTransport, x as AgentMetadata } from "./transport-Qj50JTjx.mjs";
|
|
2
2
|
import { ProviderSelection } from "@demicodes/provider";
|
|
3
3
|
import { Block, UserContentBlock } from "@demicodes/core";
|
|
4
4
|
//#region src/client.d.ts
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@demicodes/agent",
|
|
3
3
|
"description": "Session runtime and transport-neutral client and server protocol for Demi.",
|
|
4
|
-
"version": "0.25.
|
|
4
|
+
"version": "0.25.2",
|
|
5
5
|
"private": false,
|
|
6
6
|
"type": "module",
|
|
7
7
|
"exports": {
|
|
@@ -19,10 +19,10 @@
|
|
|
19
19
|
}
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@demicodes/core": "^0.25.
|
|
23
|
-
"@demicodes/provider": "^0.25.
|
|
24
|
-
"@demicodes/shell": "^0.25.
|
|
25
|
-
"@demicodes/utils": "^0.25.
|
|
22
|
+
"@demicodes/core": "^0.25.2",
|
|
23
|
+
"@demicodes/provider": "^0.25.2",
|
|
24
|
+
"@demicodes/shell": "^0.25.2",
|
|
25
|
+
"@demicodes/utils": "^0.25.2",
|
|
26
26
|
"zod": "^4.0.0"
|
|
27
27
|
},
|
|
28
28
|
"license": "Apache-2.0",
|