@ycodium-ai/agent-claude 0.2.4408 → 0.2.4477
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/index.js +500 -186
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -3,7 +3,7 @@ import * as NodePath20 from "node:path";
|
|
|
3
3
|
import { definePlugin } from "@ycodium-ai/plugin-api";
|
|
4
4
|
|
|
5
5
|
// ../../packages/plugin-api/src/version.ts
|
|
6
|
-
var PLUGIN_API_VERSION =
|
|
6
|
+
var PLUGIN_API_VERSION = 118;
|
|
7
7
|
|
|
8
8
|
// src/index.ts
|
|
9
9
|
import {
|
|
@@ -416,12 +416,12 @@ async function* historyLines(path, start, end, backwards, signal) {
|
|
|
416
416
|
}
|
|
417
417
|
}
|
|
418
418
|
var historyIdentity = async (path) => {
|
|
419
|
-
const
|
|
419
|
+
const stat5 = await NodeFSP2.stat(path);
|
|
420
420
|
return {
|
|
421
|
-
size:
|
|
422
|
-
mtimeMs:
|
|
423
|
-
ctimeMs:
|
|
424
|
-
identity: NodeCrypto.createHash("sha256").update(`${path}/${
|
|
421
|
+
size: stat5.size,
|
|
422
|
+
mtimeMs: stat5.mtimeMs,
|
|
423
|
+
ctimeMs: stat5.ctimeMs,
|
|
424
|
+
identity: NodeCrypto.createHash("sha256").update(`${path}/${stat5.dev}/${stat5.ino}/${stat5.birthtimeMs}`).digest("hex")
|
|
425
425
|
};
|
|
426
426
|
};
|
|
427
427
|
var historyCommittedFingerprint = async (path, sequence, signal) => {
|
|
@@ -1598,17 +1598,17 @@ function listTranscriptFiles(transcriptDir, directory) {
|
|
|
1598
1598
|
continue;
|
|
1599
1599
|
}
|
|
1600
1600
|
for (const name of names) {
|
|
1601
|
-
let
|
|
1601
|
+
let stat5;
|
|
1602
1602
|
try {
|
|
1603
|
-
|
|
1603
|
+
stat5 = NodeFS3.statSync(NodePath3.join(projectDir, name));
|
|
1604
1604
|
} catch {
|
|
1605
1605
|
continue;
|
|
1606
1606
|
}
|
|
1607
1607
|
found.push({
|
|
1608
1608
|
path: NodePath3.join(projectDir, name),
|
|
1609
1609
|
sessionId: name.slice(0, -TRANSCRIPT_SUFFIX.length),
|
|
1610
|
-
size:
|
|
1611
|
-
mtimeMs: Number.isFinite(
|
|
1610
|
+
size: stat5.size,
|
|
1611
|
+
mtimeMs: Number.isFinite(stat5.mtimeMs) ? stat5.mtimeMs : 0
|
|
1612
1612
|
});
|
|
1613
1613
|
}
|
|
1614
1614
|
}
|
|
@@ -22647,10 +22647,10 @@ function inputDiffStat(input) {
|
|
|
22647
22647
|
let deletions = 0;
|
|
22648
22648
|
for (const edit of input.edits) {
|
|
22649
22649
|
if (edit === null || typeof edit !== "object" || Array.isArray(edit)) return void 0;
|
|
22650
|
-
const
|
|
22651
|
-
if (!
|
|
22652
|
-
additions +=
|
|
22653
|
-
deletions +=
|
|
22650
|
+
const stat5 = replacementDiffStat(edit, budget);
|
|
22651
|
+
if (!stat5) return void 0;
|
|
22652
|
+
additions += stat5.additions;
|
|
22653
|
+
deletions += stat5.deletions;
|
|
22654
22654
|
}
|
|
22655
22655
|
return { additions, deletions };
|
|
22656
22656
|
}
|
|
@@ -23224,11 +23224,13 @@ function readClaudeResumeState(resumeCursor) {
|
|
|
23224
23224
|
const resumeCandidate = typeof cursor.resume === "string" ? cursor.resume : typeof cursor.sessionId === "string" ? cursor.sessionId : void 0;
|
|
23225
23225
|
const resume = resumeCandidate && isUuid(resumeCandidate) ? resumeCandidate : void 0;
|
|
23226
23226
|
const resumeSessionAt = typeof cursor.resumeSessionAt === "string" ? cursor.resumeSessionAt : void 0;
|
|
23227
|
+
const transcriptPath = typeof cursor.transcriptPath === "string" && cursor.transcriptPath.length > 0 ? cursor.transcriptPath : void 0;
|
|
23227
23228
|
const turnCountValue = typeof cursor.turnCount === "number" ? cursor.turnCount : void 0;
|
|
23228
23229
|
return {
|
|
23229
23230
|
...threadId ? { threadId } : {},
|
|
23230
23231
|
...resume ? { resume } : {},
|
|
23231
23232
|
...resumeSessionAt ? { resumeSessionAt } : {},
|
|
23233
|
+
...transcriptPath ? { transcriptPath } : {},
|
|
23232
23234
|
...turnCountValue !== void 0 && Number.isInteger(turnCountValue) && turnCountValue >= 0 ? { turnCount: turnCountValue } : {}
|
|
23233
23235
|
};
|
|
23234
23236
|
}
|
|
@@ -23759,6 +23761,7 @@ var makeEmitters = (emitter) => {
|
|
|
23759
23761
|
threadId,
|
|
23760
23762
|
...context.resumeSessionId ? { resume: context.resumeSessionId } : {},
|
|
23761
23763
|
...context.lastAssistantUuid ? { resumeSessionAt: context.lastAssistantUuid } : {},
|
|
23764
|
+
...context.reportedTranscriptPath ? { transcriptPath: context.reportedTranscriptPath } : {},
|
|
23762
23765
|
turnCount: context.turns.length
|
|
23763
23766
|
};
|
|
23764
23767
|
context.session = {
|
|
@@ -24187,6 +24190,7 @@ var ClaudeProcess = class extends NodeEvents.EventEmitter {
|
|
|
24187
24190
|
killed = false;
|
|
24188
24191
|
exitCode = null;
|
|
24189
24192
|
handle;
|
|
24193
|
+
exited;
|
|
24190
24194
|
ended = false;
|
|
24191
24195
|
constructor(host, options, stderr, cleanup) {
|
|
24192
24196
|
super();
|
|
@@ -24226,7 +24230,7 @@ var ClaudeProcess = class extends NodeEvents.EventEmitter {
|
|
|
24226
24230
|
};
|
|
24227
24231
|
options.signal.addEventListener("abort", abort, { once: true });
|
|
24228
24232
|
if (options.signal.aborted) abort();
|
|
24229
|
-
|
|
24233
|
+
this.exited = ready.then(async (handle) => {
|
|
24230
24234
|
this.handle = handle;
|
|
24231
24235
|
const drainStderr = (async () => {
|
|
24232
24236
|
const decoder = new TextDecoder();
|
|
@@ -24240,6 +24244,7 @@ var ClaudeProcess = class extends NodeEvents.EventEmitter {
|
|
|
24240
24244
|
this.ended = true;
|
|
24241
24245
|
this.handle = void 0;
|
|
24242
24246
|
this.exitCode = code;
|
|
24247
|
+
await drainStderr;
|
|
24243
24248
|
await cleanup();
|
|
24244
24249
|
this.emit("exit", code, null);
|
|
24245
24250
|
} finally {
|
|
@@ -24266,6 +24271,7 @@ function makeClaudeProcessSpawner(host, stderr, config = {}) {
|
|
|
24266
24271
|
});
|
|
24267
24272
|
let process2;
|
|
24268
24273
|
let launch;
|
|
24274
|
+
let exited;
|
|
24269
24275
|
return {
|
|
24270
24276
|
spawn: (options) => {
|
|
24271
24277
|
const args = [...options.args, ...config.mcpConfigArgs ?? []];
|
|
@@ -24282,6 +24288,7 @@ function makeClaudeProcessSpawner(host, stderr, config = {}) {
|
|
|
24282
24288
|
}
|
|
24283
24289
|
launch = { ...options, args };
|
|
24284
24290
|
process2 = new ClaudeProcess(host, launch, stderr, cleanup);
|
|
24291
|
+
exited = process2.exited;
|
|
24285
24292
|
return process2;
|
|
24286
24293
|
},
|
|
24287
24294
|
current: () => process2?.handle && launch ? { handle: process2.handle, command: launch.command, args: launch.args } : void 0,
|
|
@@ -24289,6 +24296,9 @@ function makeClaudeProcessSpawner(host, stderr, config = {}) {
|
|
|
24289
24296
|
process2 = void 0;
|
|
24290
24297
|
launch = void 0;
|
|
24291
24298
|
return cleanup();
|
|
24299
|
+
},
|
|
24300
|
+
waitForExit: async () => {
|
|
24301
|
+
await exited;
|
|
24292
24302
|
}
|
|
24293
24303
|
};
|
|
24294
24304
|
}
|
|
@@ -24538,6 +24548,42 @@ Claude stderr (last lines):
|
|
|
24538
24548
|
${excerpt}`;
|
|
24539
24549
|
}
|
|
24540
24550
|
|
|
24551
|
+
// src/adapter/sessionStartup.ts
|
|
24552
|
+
async function emitResumeForked(context, warn) {
|
|
24553
|
+
const notice = context.startup?.forkNotice;
|
|
24554
|
+
if (!notice?.ready || notice.sent || !context.turnState || context.stopped) return;
|
|
24555
|
+
notice.sent = true;
|
|
24556
|
+
await warn(
|
|
24557
|
+
context,
|
|
24558
|
+
"Claude Code continued this thread in a fork of its earlier transcript.",
|
|
24559
|
+
{
|
|
24560
|
+
previousSessionId: notice.previousSessionId,
|
|
24561
|
+
sessionId: context.resumeSessionId,
|
|
24562
|
+
transcriptPath: notice.transcriptPath
|
|
24563
|
+
},
|
|
24564
|
+
"resume-thread-forked"
|
|
24565
|
+
);
|
|
24566
|
+
}
|
|
24567
|
+
var settleStartup = (context, outcome) => {
|
|
24568
|
+
const startup = context.startup;
|
|
24569
|
+
if (startup === void 0 || startup.settled) return;
|
|
24570
|
+
startup.settled = true;
|
|
24571
|
+
startup.outcome.resolve(outcome);
|
|
24572
|
+
};
|
|
24573
|
+
var noteStartupEstablished = (context) => {
|
|
24574
|
+
settleStartup(context, "established");
|
|
24575
|
+
};
|
|
24576
|
+
var suppressStartupFailure = (context, missingConversation) => {
|
|
24577
|
+
const startup = context.startup;
|
|
24578
|
+
if (startup?.failureHandled) return true;
|
|
24579
|
+
if (startup === void 0 || startup.settled || context.launchConfirmed) return false;
|
|
24580
|
+
if (startup.kind === "fork" && context.sessionEstablished) return false;
|
|
24581
|
+
if (!missingConversation && startup.kind !== "fork") return false;
|
|
24582
|
+
startup.failureHandled = true;
|
|
24583
|
+
settleStartup(context, startup.kind === "fork" ? "exited" : "missing");
|
|
24584
|
+
return true;
|
|
24585
|
+
};
|
|
24586
|
+
|
|
24541
24587
|
// src/adapter/sdkStream.ts
|
|
24542
24588
|
var makeSdkStream = (deps) => {
|
|
24543
24589
|
const {
|
|
@@ -24628,6 +24674,7 @@ var makeSdkStream = (deps) => {
|
|
|
24628
24674
|
};
|
|
24629
24675
|
const handleStreamExit = async (context, cause, streamBroke) => {
|
|
24630
24676
|
if (context.stopped) {
|
|
24677
|
+
settleStartup(context, "exited");
|
|
24631
24678
|
return;
|
|
24632
24679
|
}
|
|
24633
24680
|
unresumableSessions.noteStreamExit(context);
|
|
@@ -24640,6 +24687,8 @@ var makeSdkStream = (deps) => {
|
|
|
24640
24687
|
const reason = cause instanceof Error ? cause.message : "Claude runtime stream failed.";
|
|
24641
24688
|
const stderrTail = context.stderrTail.read();
|
|
24642
24689
|
unresumableSessions.noteStderr(context, stderrTail);
|
|
24690
|
+
const missingConversation = unresumableSessions.reasonFor(context.launch.sessionId) === "missing";
|
|
24691
|
+
if (suppressStartupFailure(context, missingConversation)) return;
|
|
24643
24692
|
const message = claudeStreamExitMessage(reason, stderrTail);
|
|
24644
24693
|
const category = claudeFailureCategory({
|
|
24645
24694
|
establishedStreamExit: streamBroke && context.sessionEstablished
|
|
@@ -24652,7 +24701,8 @@ var makeSdkStream = (deps) => {
|
|
|
24652
24701
|
} else if (context.turnState) {
|
|
24653
24702
|
await completeTurn(context, "interrupted", "Claude runtime stream ended.");
|
|
24654
24703
|
}
|
|
24655
|
-
await stopSessionInternal(context);
|
|
24704
|
+
await stopSessionInternal(context, { naturalExit: true });
|
|
24705
|
+
settleStartup(context, "exited");
|
|
24656
24706
|
};
|
|
24657
24707
|
return { runSdkStream };
|
|
24658
24708
|
};
|
|
@@ -25120,138 +25170,193 @@ var makeSendTurn = (deps) => {
|
|
|
25120
25170
|
emitRuntimeWarning
|
|
25121
25171
|
} = deps;
|
|
25122
25172
|
const { nowIso, randomUUIDv4, makeEventStamp, eventHeader, offerRuntimeEvent } = emitter;
|
|
25123
|
-
const
|
|
25124
|
-
|
|
25125
|
-
const
|
|
25126
|
-
|
|
25127
|
-
|
|
25128
|
-
const
|
|
25129
|
-
|
|
25130
|
-
|
|
25131
|
-
|
|
25132
|
-
|
|
25133
|
-
|
|
25134
|
-
|
|
25135
|
-
|
|
25136
|
-
|
|
25137
|
-
|
|
25138
|
-
refreshCatalog: refreshModelCatalogForSend,
|
|
25139
|
-
refreshSucceeded: modelCatalogRefreshSucceeded
|
|
25140
|
-
}) : void 0;
|
|
25141
|
-
let effortFallback;
|
|
25173
|
+
const preparations = /* @__PURE__ */ new Map();
|
|
25174
|
+
const sendTurn = async (input, signal) => {
|
|
25175
|
+
const previous = preparations.get(input.threadId);
|
|
25176
|
+
const preparation = Promise.withResolvers();
|
|
25177
|
+
preparations.set(input.threadId, preparation.promise);
|
|
25178
|
+
const release = () => {
|
|
25179
|
+
preparation.resolve();
|
|
25180
|
+
if (preparations.get(input.threadId) === preparation.promise)
|
|
25181
|
+
preparations.delete(input.threadId);
|
|
25182
|
+
};
|
|
25183
|
+
let context;
|
|
25184
|
+
const abort = () => {
|
|
25185
|
+
context?.startup?.controller.abort();
|
|
25186
|
+
if (context?.startup) void deps.stop(context).catch(() => void 0);
|
|
25187
|
+
};
|
|
25142
25188
|
try {
|
|
25143
|
-
|
|
25144
|
-
|
|
25145
|
-
|
|
25146
|
-
|
|
25147
|
-
|
|
25148
|
-
|
|
25149
|
-
|
|
25150
|
-
|
|
25151
|
-
|
|
25152
|
-
|
|
25153
|
-
|
|
25154
|
-
|
|
25155
|
-
|
|
25156
|
-
|
|
25157
|
-
|
|
25158
|
-
|
|
25159
|
-
|
|
25160
|
-
|
|
25189
|
+
await previous;
|
|
25190
|
+
signal?.throwIfAborted();
|
|
25191
|
+
const candidate = deps.findSession(input.threadId);
|
|
25192
|
+
context = candidate?.startup?.replacement ? await candidate.startup.replacement : requireSession(input.threadId);
|
|
25193
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
25194
|
+
const validateSteeringTarget = () => assertSteeringTarget(input.expectedTurnId, context.turnState);
|
|
25195
|
+
validateSteeringTarget();
|
|
25196
|
+
const modelSelection = boundModelSelection(input.modelSelection, boundInstanceId);
|
|
25197
|
+
const steeringTurnState = context.turnState && context.turnState.synthetic !== true ? context.turnState : null;
|
|
25198
|
+
if (context.turnState && steeringTurnState === null) {
|
|
25199
|
+
await completeTurn(context, "completed");
|
|
25200
|
+
}
|
|
25201
|
+
const apiModelId = modelSelection?.model && steeringTurnState === null ? await resolveClaudeCliModelId({
|
|
25202
|
+
requestedModelId: modelSelection.model,
|
|
25203
|
+
placeholder: CLAUDE_CARD.defaultModel,
|
|
25204
|
+
catalog: currentModelCatalog,
|
|
25205
|
+
declared: declaredModels,
|
|
25206
|
+
customModels: recipe.customModels,
|
|
25207
|
+
refreshCatalog: refreshModelCatalogForSend,
|
|
25208
|
+
refreshSucceeded: modelCatalogRefreshSucceeded
|
|
25209
|
+
}) : void 0;
|
|
25210
|
+
let effortFallback;
|
|
25211
|
+
const configure = async () => {
|
|
25212
|
+
for (; ; ) {
|
|
25213
|
+
signal?.throwIfAborted();
|
|
25214
|
+
if (context.startup?.failureHandled) {
|
|
25215
|
+
context = await context.startup.replace(input);
|
|
25216
|
+
continue;
|
|
25217
|
+
}
|
|
25218
|
+
try {
|
|
25219
|
+
if (modelSelection?.model && apiModelId !== void 0) {
|
|
25220
|
+
const turnOptions = resolveClaudeSessionOptions({
|
|
25221
|
+
modelSelection,
|
|
25222
|
+
catalog: currentModelCatalog(),
|
|
25223
|
+
recipe
|
|
25224
|
+
});
|
|
25225
|
+
await applyClaudeSessionOptions(
|
|
25226
|
+
context,
|
|
25227
|
+
{ apiModelId, effort: turnOptions.effort, settings: turnOptions.settings },
|
|
25228
|
+
"turn",
|
|
25229
|
+
log
|
|
25230
|
+
);
|
|
25231
|
+
if (turnOptions.unsupportedEffort !== void 0) {
|
|
25232
|
+
effortFallback = {
|
|
25233
|
+
model: modelSelection.model,
|
|
25234
|
+
requestedEffort: turnOptions.unsupportedEffort,
|
|
25235
|
+
appliedEffort: turnOptions.effort
|
|
25236
|
+
};
|
|
25237
|
+
}
|
|
25238
|
+
context.session = {
|
|
25239
|
+
...context.session,
|
|
25240
|
+
model: modelSelection.model
|
|
25241
|
+
};
|
|
25242
|
+
}
|
|
25243
|
+
if (input.interactionMode === "plan") {
|
|
25244
|
+
await applyClaudePermissionMode(context, "plan", {
|
|
25245
|
+
method: "turn/setPermissionMode"
|
|
25246
|
+
});
|
|
25247
|
+
} else if (input.interactionMode === "default") {
|
|
25248
|
+
await applyClaudePermissionMode(context, context.basePermissionMode ?? "default", {
|
|
25249
|
+
method: "turn/setPermissionMode"
|
|
25250
|
+
});
|
|
25251
|
+
}
|
|
25252
|
+
if (steeringTurnState === null && context.turnState?.synthetic === true) {
|
|
25253
|
+
await completeTurn(context, "completed");
|
|
25254
|
+
}
|
|
25255
|
+
if (requireSession(input.threadId) !== context) {
|
|
25256
|
+
throw new Error(`Claude session replaced: ${input.threadId}`);
|
|
25257
|
+
}
|
|
25258
|
+
return;
|
|
25259
|
+
} catch (cause) {
|
|
25260
|
+
signal?.throwIfAborted();
|
|
25261
|
+
deps.unresumableSessions.noteStderr(context, context.stderrTail.read());
|
|
25262
|
+
const missing = deps.unresumableSessions.reasonFor(context.launch.sessionId) === "missing";
|
|
25263
|
+
if (missing) suppressStartupFailure(context, true);
|
|
25264
|
+
if (context.startup?.failureHandled) {
|
|
25265
|
+
context = await context.startup.replace(input);
|
|
25266
|
+
continue;
|
|
25267
|
+
}
|
|
25268
|
+
const rejected = Object.assign(
|
|
25269
|
+
cause instanceof Error ? cause : new Error(String(cause)),
|
|
25270
|
+
{ code: "provider-rejected" }
|
|
25271
|
+
);
|
|
25272
|
+
throw rejected;
|
|
25273
|
+
}
|
|
25161
25274
|
}
|
|
25275
|
+
};
|
|
25276
|
+
await configure();
|
|
25277
|
+
const turnCapsForPrompt = claudeModelCapabilities(
|
|
25278
|
+
modelSelection?.model,
|
|
25279
|
+
currentModelCatalog(),
|
|
25280
|
+
recipe.customModels
|
|
25281
|
+
);
|
|
25282
|
+
const turnId = steeringTurnState?.turnId ?? randomUUIDv4();
|
|
25283
|
+
if (steeringTurnState === null) {
|
|
25284
|
+
const turnState = {
|
|
25285
|
+
turnId,
|
|
25286
|
+
startedAt: nowIso(),
|
|
25287
|
+
assistantTextBlocks: /* @__PURE__ */ new Map(),
|
|
25288
|
+
assistantTextBlockOrder: [],
|
|
25289
|
+
capturedProposedPlanKeys: /* @__PURE__ */ new Set(),
|
|
25290
|
+
latestAssistantUsage: void 0,
|
|
25291
|
+
compactedSinceLatestAssistantUsage: false,
|
|
25292
|
+
retrying: false,
|
|
25293
|
+
authenticationFailed: false,
|
|
25294
|
+
nextSyntheticAssistantBlockIndex: -1,
|
|
25295
|
+
rejectedRateLimits: /* @__PURE__ */ new Map(),
|
|
25296
|
+
announcedUsageLimits: /* @__PURE__ */ new Set(),
|
|
25297
|
+
latestAssistantError: void 0,
|
|
25298
|
+
lastRetryErrorStatus: void 0
|
|
25299
|
+
};
|
|
25300
|
+
const updatedAt = nowIso();
|
|
25301
|
+
context.turnState = turnState;
|
|
25162
25302
|
context.session = {
|
|
25163
25303
|
...context.session,
|
|
25164
|
-
|
|
25304
|
+
status: "running",
|
|
25305
|
+
activeTurnId: turnId,
|
|
25306
|
+
updatedAt
|
|
25165
25307
|
};
|
|
25166
|
-
|
|
25167
|
-
|
|
25168
|
-
|
|
25169
|
-
|
|
25170
|
-
|
|
25171
|
-
|
|
25172
|
-
|
|
25173
|
-
method: "turn/setPermissionMode"
|
|
25308
|
+
const turnStartedStamp = await makeEventStamp();
|
|
25309
|
+
offerRuntimeEvent(context, {
|
|
25310
|
+
type: "turn.started",
|
|
25311
|
+
...eventHeader(context, turnStartedStamp),
|
|
25312
|
+
turnId,
|
|
25313
|
+
payload: modelSelection?.model ? { model: modelSelection.model } : {},
|
|
25314
|
+
providerRefs: {}
|
|
25174
25315
|
});
|
|
25175
25316
|
}
|
|
25176
|
-
|
|
25177
|
-
|
|
25178
|
-
|
|
25179
|
-
|
|
25180
|
-
|
|
25317
|
+
const turnText = await prepareClaudeTurnText({
|
|
25318
|
+
prompt: buildPromptText(turnCapsForPrompt, input, boundInstanceId),
|
|
25319
|
+
preamble: input.inputPreamble,
|
|
25320
|
+
recipe,
|
|
25321
|
+
context
|
|
25322
|
+
});
|
|
25323
|
+
for (const warning of turnText.warnings) await emitRuntimeWarning(context, warning);
|
|
25324
|
+
if (effortFallback !== void 0) {
|
|
25325
|
+
const warning = reasoningLevelFallbackWarning(effortFallback);
|
|
25326
|
+
await emitRuntimeWarning(context, warning.message, warning.detail);
|
|
25327
|
+
}
|
|
25328
|
+
const message = await buildUserMessageFromTurn({
|
|
25329
|
+
text: turnText,
|
|
25330
|
+
attachments: input.attachments,
|
|
25331
|
+
attachmentsApi: deps.attachments
|
|
25332
|
+
});
|
|
25333
|
+
await emitResumeForked(context, emitRuntimeWarning);
|
|
25334
|
+
while (context.startup?.failureHandled || deps.findSession(input.threadId) !== context) {
|
|
25335
|
+
context = context.startup?.replacement ? await context.startup.replacement : requireSession(input.threadId);
|
|
25336
|
+
await configure();
|
|
25337
|
+
await emitResumeForked(context, emitRuntimeWarning);
|
|
25181
25338
|
}
|
|
25182
|
-
|
|
25183
|
-
|
|
25184
|
-
|
|
25185
|
-
|
|
25186
|
-
|
|
25187
|
-
|
|
25188
|
-
|
|
25189
|
-
|
|
25190
|
-
|
|
25191
|
-
|
|
25192
|
-
|
|
25193
|
-
|
|
25194
|
-
|
|
25195
|
-
|
|
25196
|
-
const turnState = {
|
|
25339
|
+
signal?.throwIfAborted();
|
|
25340
|
+
validateSteeringTarget();
|
|
25341
|
+
if (context.startup && !context.startup.settled) context.startup.messages.push(message);
|
|
25342
|
+
context.promptQueue.push({
|
|
25343
|
+
type: "message",
|
|
25344
|
+
message
|
|
25345
|
+
});
|
|
25346
|
+
const startup = context.startup;
|
|
25347
|
+
if (startup !== void 0 && startup.work === void 0)
|
|
25348
|
+
startup.work = startup.recover(input);
|
|
25349
|
+
release();
|
|
25350
|
+
if (startup?.work !== void 0 && steeringTurnState === null) context = await startup.work;
|
|
25351
|
+
return {
|
|
25352
|
+
threadId: context.session.threadId,
|
|
25197
25353
|
turnId,
|
|
25198
|
-
|
|
25199
|
-
assistantTextBlocks: /* @__PURE__ */ new Map(),
|
|
25200
|
-
assistantTextBlockOrder: [],
|
|
25201
|
-
capturedProposedPlanKeys: /* @__PURE__ */ new Set(),
|
|
25202
|
-
latestAssistantUsage: void 0,
|
|
25203
|
-
compactedSinceLatestAssistantUsage: false,
|
|
25204
|
-
retrying: false,
|
|
25205
|
-
authenticationFailed: false,
|
|
25206
|
-
nextSyntheticAssistantBlockIndex: -1,
|
|
25207
|
-
rejectedRateLimits: /* @__PURE__ */ new Map(),
|
|
25208
|
-
announcedUsageLimits: /* @__PURE__ */ new Set(),
|
|
25209
|
-
latestAssistantError: void 0,
|
|
25210
|
-
lastRetryErrorStatus: void 0
|
|
25211
|
-
};
|
|
25212
|
-
const updatedAt = nowIso();
|
|
25213
|
-
context.turnState = turnState;
|
|
25214
|
-
context.session = {
|
|
25215
|
-
...context.session,
|
|
25216
|
-
status: "running",
|
|
25217
|
-
activeTurnId: turnId,
|
|
25218
|
-
updatedAt
|
|
25354
|
+
...context.session.resumeCursor !== void 0 ? { resumeCursor: context.session.resumeCursor } : {}
|
|
25219
25355
|
};
|
|
25220
|
-
|
|
25221
|
-
|
|
25222
|
-
|
|
25223
|
-
...eventHeader(context, turnStartedStamp),
|
|
25224
|
-
turnId,
|
|
25225
|
-
payload: modelSelection?.model ? { model: modelSelection.model } : {},
|
|
25226
|
-
providerRefs: {}
|
|
25227
|
-
});
|
|
25356
|
+
} finally {
|
|
25357
|
+
signal?.removeEventListener("abort", abort);
|
|
25358
|
+
release();
|
|
25228
25359
|
}
|
|
25229
|
-
const turnText = await prepareClaudeTurnText({
|
|
25230
|
-
prompt: buildPromptText(turnCapsForPrompt, input, boundInstanceId),
|
|
25231
|
-
preamble: input.inputPreamble,
|
|
25232
|
-
recipe,
|
|
25233
|
-
context
|
|
25234
|
-
});
|
|
25235
|
-
for (const warning of turnText.warnings) await emitRuntimeWarning(context, warning);
|
|
25236
|
-
if (effortFallback !== void 0) {
|
|
25237
|
-
const warning = reasoningLevelFallbackWarning(effortFallback);
|
|
25238
|
-
await emitRuntimeWarning(context, warning.message, warning.detail);
|
|
25239
|
-
}
|
|
25240
|
-
const message = await buildUserMessageFromTurn({
|
|
25241
|
-
text: turnText,
|
|
25242
|
-
attachments: input.attachments,
|
|
25243
|
-
attachmentsApi: deps.attachments
|
|
25244
|
-
});
|
|
25245
|
-
validateSteeringTarget();
|
|
25246
|
-
context.promptQueue.push({
|
|
25247
|
-
type: "message",
|
|
25248
|
-
message
|
|
25249
|
-
});
|
|
25250
|
-
return {
|
|
25251
|
-
threadId: context.session.threadId,
|
|
25252
|
-
turnId,
|
|
25253
|
-
...context.session.resumeCursor !== void 0 ? { resumeCursor: context.session.resumeCursor } : {}
|
|
25254
|
-
};
|
|
25255
25360
|
};
|
|
25256
25361
|
return { sendTurn };
|
|
25257
25362
|
};
|
|
@@ -25395,7 +25500,7 @@ function compactBoundaryTokenUsageSnapshot(message, contextWindow, totalProcesse
|
|
|
25395
25500
|
// src/adapter/sessionCallbacks.ts
|
|
25396
25501
|
var CONTEXT_AFTER_COMPACTION_TIMEOUT_MS = 5e3;
|
|
25397
25502
|
var makeSessionCallbacks = (deps) => {
|
|
25398
|
-
const { emitter, ensureSyntheticTurn, emitProposedPlanCompleted } = deps;
|
|
25503
|
+
const { emitter, ensureSyntheticTurn, emitProposedPlanCompleted, updateResumeCursor } = deps;
|
|
25399
25504
|
const { randomUUIDv4, makeEventStamp, eventHeader, offerRuntimeEvent } = emitter;
|
|
25400
25505
|
const forSession = (session) => {
|
|
25401
25506
|
const { waitSessionContext, pendingApprovals, pendingUserInputs } = session;
|
|
@@ -25411,6 +25516,7 @@ var makeSessionCallbacks = (deps) => {
|
|
|
25411
25516
|
const isNewMainTranscript = (context, hookInput) => hookInput.agent_id === void 0 && hookInput.session_id === context.resumeSessionId && typeof hookInput.transcript_path === "string" && hookInput.transcript_path.length > 0 && hookInput.transcript_path !== context.reportedTranscriptPath;
|
|
25412
25517
|
const reportTranscriptPath = async (context, hookInput) => {
|
|
25413
25518
|
context.reportedTranscriptPath = hookInput.transcript_path;
|
|
25519
|
+
await updateResumeCursor(context);
|
|
25414
25520
|
const stamp = await makeEventStamp();
|
|
25415
25521
|
if (context.stopped || context.stopping) return;
|
|
25416
25522
|
offerRuntimeEvent(context, {
|
|
@@ -25930,13 +26036,33 @@ var makeSessionLifecycle = (deps) => {
|
|
|
25930
26036
|
clearTimeout(timer);
|
|
25931
26037
|
}
|
|
25932
26038
|
};
|
|
25933
|
-
const stopSessionInternal = async (context) => {
|
|
26039
|
+
const stopSessionInternal = async (context, options) => {
|
|
26040
|
+
if (!options?.recovering && !options?.naturalExit) {
|
|
26041
|
+
context.startup?.controller.abort();
|
|
26042
|
+
context.recoveryController?.abort();
|
|
26043
|
+
settleStartup(context, "exited");
|
|
26044
|
+
}
|
|
25934
26045
|
if (context.stopWork) {
|
|
25935
26046
|
await context.stopWork;
|
|
26047
|
+
if (context.recoveryStopped && !options?.recovering) {
|
|
26048
|
+
context.recoveryStopped = false;
|
|
26049
|
+
await completeTurn(context, "interrupted", "Session stopped.");
|
|
26050
|
+
if (sessions.get(context.session.threadId) === context) {
|
|
26051
|
+
const stamp = await makeEventStamp();
|
|
26052
|
+
offerRuntimeEvent(context, {
|
|
26053
|
+
type: "session.exited",
|
|
26054
|
+
...eventHeader(context, stamp),
|
|
26055
|
+
payload: { reason: "Session stopped", exitKind: "graceful" },
|
|
26056
|
+
providerRefs: {}
|
|
26057
|
+
});
|
|
26058
|
+
sessions.delete(context.session.threadId);
|
|
26059
|
+
}
|
|
26060
|
+
}
|
|
25936
26061
|
return;
|
|
25937
26062
|
}
|
|
25938
26063
|
if (context.stopped) return;
|
|
25939
26064
|
context.stopping = true;
|
|
26065
|
+
context.recoveryStopped = options?.recovering === true;
|
|
25940
26066
|
const work = (async () => {
|
|
25941
26067
|
context.stopped = true;
|
|
25942
26068
|
let closeFailure;
|
|
@@ -25981,7 +26107,7 @@ var makeSessionLifecycle = (deps) => {
|
|
|
25981
26107
|
for (const pending2 of Array.from(context.pendingUserInputs.values())) {
|
|
25982
26108
|
pending2.cancel();
|
|
25983
26109
|
}
|
|
25984
|
-
if (context.turnState) {
|
|
26110
|
+
if (context.turnState && !options?.recovering) {
|
|
25985
26111
|
await awaitClaimedResult(context);
|
|
25986
26112
|
if (context.turnState) {
|
|
25987
26113
|
await completeTurn(context, "interrupted", "Session stopped.");
|
|
@@ -26005,7 +26131,7 @@ var makeSessionLifecycle = (deps) => {
|
|
|
26005
26131
|
status: "closed",
|
|
26006
26132
|
updatedAt
|
|
26007
26133
|
};
|
|
26008
|
-
if (sessions.get(context.session.threadId) === context) {
|
|
26134
|
+
if (options?.emitExit !== false && sessions.get(context.session.threadId) === context) {
|
|
26009
26135
|
const stamp = await makeEventStamp();
|
|
26010
26136
|
offerRuntimeEvent(context, {
|
|
26011
26137
|
type: "session.exited",
|
|
@@ -26017,7 +26143,7 @@ var makeSessionLifecycle = (deps) => {
|
|
|
26017
26143
|
providerRefs: {}
|
|
26018
26144
|
});
|
|
26019
26145
|
}
|
|
26020
|
-
if (sessions.get(context.session.threadId) === context) {
|
|
26146
|
+
if (!options?.recovering && sessions.get(context.session.threadId) === context) {
|
|
26021
26147
|
sessions.delete(context.session.threadId);
|
|
26022
26148
|
}
|
|
26023
26149
|
})();
|
|
@@ -26100,6 +26226,70 @@ var makeSessionLifecycle = (deps) => {
|
|
|
26100
26226
|
};
|
|
26101
26227
|
};
|
|
26102
26228
|
|
|
26229
|
+
// src/adapter/resumeRecovery.ts
|
|
26230
|
+
var makeResumeRecovery = (input) => ({
|
|
26231
|
+
replace: (turn) => {
|
|
26232
|
+
const recovery = input(turn);
|
|
26233
|
+
const startup = recovery.context.startup;
|
|
26234
|
+
return startup.replacement ??= replaceResumeProcess(recovery);
|
|
26235
|
+
},
|
|
26236
|
+
recover: (turn) => recoverFirstPrompt(input(turn))
|
|
26237
|
+
});
|
|
26238
|
+
async function replaceResumeProcess(input) {
|
|
26239
|
+
const { context, restart, stop } = input;
|
|
26240
|
+
const startup = context.startup;
|
|
26241
|
+
if (startup === void 0) return context;
|
|
26242
|
+
const controller = startup.controller;
|
|
26243
|
+
await stop(context, { emitExit: false, recovering: true });
|
|
26244
|
+
await context.cliProcess.waitForExit();
|
|
26245
|
+
controller.signal.throwIfAborted();
|
|
26246
|
+
const next = await restart({
|
|
26247
|
+
controller,
|
|
26248
|
+
previous: context,
|
|
26249
|
+
...startup.kind === "fork" ? { forkFailed: true, resetReason: "fork-start-failed" } : {}
|
|
26250
|
+
});
|
|
26251
|
+
context.turnState = void 0;
|
|
26252
|
+
if (controller.signal.aborted) {
|
|
26253
|
+
await stop(next);
|
|
26254
|
+
await next.cliProcess.waitForExit();
|
|
26255
|
+
controller.signal.throwIfAborted();
|
|
26256
|
+
}
|
|
26257
|
+
for (const message of startup.messages) next.promptQueue.push({ type: "message", message });
|
|
26258
|
+
if (startup.messages.length > 0 && next.startup !== void 0)
|
|
26259
|
+
next.startup.work = next.startup.recover(input.turnInput);
|
|
26260
|
+
return next;
|
|
26261
|
+
}
|
|
26262
|
+
async function recoverFirstPrompt(input) {
|
|
26263
|
+
const { context, stop } = input;
|
|
26264
|
+
const startup = context.startup;
|
|
26265
|
+
if (startup === void 0) return context;
|
|
26266
|
+
const controller = startup.controller;
|
|
26267
|
+
const abort = () => {
|
|
26268
|
+
void stop(context).catch(() => void 0);
|
|
26269
|
+
};
|
|
26270
|
+
controller.signal.addEventListener("abort", abort, { once: true });
|
|
26271
|
+
if (controller.signal.aborted) abort();
|
|
26272
|
+
try {
|
|
26273
|
+
const outcome = await startup.outcome.promise;
|
|
26274
|
+
if (controller.signal.aborted) {
|
|
26275
|
+
await stop(context);
|
|
26276
|
+
controller.signal.throwIfAborted();
|
|
26277
|
+
}
|
|
26278
|
+
if (startup.failureHandled && (outcome === "missing" || outcome === "exited")) {
|
|
26279
|
+
const next = await startup.replace(input.turnInput);
|
|
26280
|
+
if (next.startup?.work !== void 0) return await next.startup.work;
|
|
26281
|
+
return next;
|
|
26282
|
+
}
|
|
26283
|
+
return context;
|
|
26284
|
+
} finally {
|
|
26285
|
+
controller.signal.removeEventListener("abort", abort);
|
|
26286
|
+
if (controller.signal.aborted) await context.cliProcess.waitForExit();
|
|
26287
|
+
}
|
|
26288
|
+
}
|
|
26289
|
+
|
|
26290
|
+
// src/adapter/startSession.ts
|
|
26291
|
+
import * as NodeFSP6 from "node:fs/promises";
|
|
26292
|
+
|
|
26103
26293
|
// src/claudeMcpConfig.ts
|
|
26104
26294
|
import * as NodeFSP5 from "node:fs/promises";
|
|
26105
26295
|
import * as NodeOS7 from "node:os";
|
|
@@ -26180,6 +26370,16 @@ function toClaudeMcpServers(entries) {
|
|
|
26180
26370
|
}
|
|
26181
26371
|
return servers;
|
|
26182
26372
|
}
|
|
26373
|
+
var unreadableTranscript = async (transcriptPath) => {
|
|
26374
|
+
try {
|
|
26375
|
+
if (!(await NodeFSP6.stat(transcriptPath)).isFile()) return "transcript-unreadable";
|
|
26376
|
+
await NodeFSP6.access(transcriptPath, NodeFSP6.constants.R_OK);
|
|
26377
|
+
return void 0;
|
|
26378
|
+
} catch (cause) {
|
|
26379
|
+
const code = cause.code;
|
|
26380
|
+
return code === "ENOENT" || code === "ENOTDIR" ? "transcript-missing" : "transcript-unreadable";
|
|
26381
|
+
}
|
|
26382
|
+
};
|
|
26183
26383
|
var makeStartSession = (deps) => {
|
|
26184
26384
|
const {
|
|
26185
26385
|
emitter,
|
|
@@ -26202,7 +26402,9 @@ var makeStartSession = (deps) => {
|
|
|
26202
26402
|
const claudeSdkExecutablePath = deps.executablePath;
|
|
26203
26403
|
const currentModelCatalog = modelCatalog.current;
|
|
26204
26404
|
const { nowIso, randomUUIDv4, makeEventStamp, offerRuntimeEvent } = emitter;
|
|
26205
|
-
const
|
|
26405
|
+
const startSessionWithRecovery = async (input, recovery, signal) => {
|
|
26406
|
+
signal?.throwIfAborted();
|
|
26407
|
+
recovery?.controller.signal.throwIfAborted();
|
|
26206
26408
|
if (recipe.binaryPath.length === 0) throw new Error(CLAUDE_PROGRAM_NOT_CONFIGURED);
|
|
26207
26409
|
const { flags, variadicArgs } = parseCliArgs(recipe.launchArgs, {
|
|
26208
26410
|
booleanFlags: ["dangerously-skip-permissions"],
|
|
@@ -26255,7 +26457,7 @@ var makeStartSession = (deps) => {
|
|
|
26255
26457
|
processEnv: { ...compactEnv(buildLaunchEnvironment()), ...workingDirectories.env }
|
|
26256
26458
|
};
|
|
26257
26459
|
const existingContext = sessions.get(input.threadId);
|
|
26258
|
-
if (existingContext) {
|
|
26460
|
+
if (existingContext && recovery === void 0) {
|
|
26259
26461
|
const reconfigured = await reconfigureSession(existingContext, input, {
|
|
26260
26462
|
permissionMode,
|
|
26261
26463
|
...apiModelId ? { apiModelId } : {},
|
|
@@ -26276,14 +26478,63 @@ var makeStartSession = (deps) => {
|
|
|
26276
26478
|
await stopSessionInternal(existingContext);
|
|
26277
26479
|
}
|
|
26278
26480
|
const startedAt = nowIso();
|
|
26279
|
-
const
|
|
26481
|
+
const cursorState = input.discardResumeCursor === true ? void 0 : readClaudeResumeState(input.resumeCursor);
|
|
26482
|
+
const persistedResumeState = cursorState?.transcriptPath === void 0 && input.nativeHistory !== void 0 && cursorState?.resume === input.nativeHistory.nativeSessionId ? readClaudeResumeState({
|
|
26483
|
+
...cursorState,
|
|
26484
|
+
transcriptPath: input.nativeHistory.transcriptPath
|
|
26485
|
+
}) : cursorState;
|
|
26280
26486
|
const persistedResumeId = persistedResumeState?.resume;
|
|
26281
|
-
const unresumable = persistedResumeId === void 0 ? void 0 : unresumableSessions.reasonFor(persistedResumeId);
|
|
26282
|
-
|
|
26487
|
+
const unresumable = recovery !== void 0 ? "missing" : persistedResumeId === void 0 ? void 0 : unresumableSessions.reasonFor(persistedResumeId);
|
|
26488
|
+
let resetReason = recovery?.resetReason;
|
|
26489
|
+
if (unresumable === "missing" && persistedResumeState?.transcriptPath === void 0)
|
|
26490
|
+
resetReason ??= "transcript-path-unrecorded";
|
|
26491
|
+
let forkTranscriptPath;
|
|
26492
|
+
if (unresumable === "missing" && recovery?.forkFailed !== true && persistedResumeState?.transcriptPath !== void 0) {
|
|
26493
|
+
const failure = await unreadableTranscript(persistedResumeState.transcriptPath);
|
|
26494
|
+
if (failure === void 0) forkTranscriptPath = persistedResumeState.transcriptPath;
|
|
26495
|
+
else resetReason = failure;
|
|
26496
|
+
}
|
|
26497
|
+
const resumeState = unresumable === void 0 || forkTranscriptPath !== void 0 ? persistedResumeState : void 0;
|
|
26283
26498
|
const threadId = input.threadId;
|
|
26284
|
-
const existingResumeSessionId = resumeState?.resume;
|
|
26499
|
+
const existingResumeSessionId = unresumable === void 0 ? resumeState?.resume : void 0;
|
|
26285
26500
|
const sessionId = existingResumeSessionId ?? randomUUIDv4();
|
|
26286
26501
|
const newSessionId = existingResumeSessionId === void 0 ? sessionId : void 0;
|
|
26502
|
+
const controller = recovery?.controller ?? new AbortController();
|
|
26503
|
+
const startup = existingResumeSessionId === void 0 && forkTranscriptPath === void 0 ? void 0 : {
|
|
26504
|
+
kind: forkTranscriptPath === void 0 ? "resume" : "fork",
|
|
26505
|
+
outcome: Promise.withResolvers(),
|
|
26506
|
+
settled: false,
|
|
26507
|
+
controller,
|
|
26508
|
+
messages: recovery?.previous?.startup?.messages ?? [],
|
|
26509
|
+
...forkTranscriptPath === void 0 ? {} : {
|
|
26510
|
+
forkNotice: {
|
|
26511
|
+
previousSessionId: persistedResumeId,
|
|
26512
|
+
transcriptPath: forkTranscriptPath,
|
|
26513
|
+
ready: false,
|
|
26514
|
+
sent: false
|
|
26515
|
+
}
|
|
26516
|
+
},
|
|
26517
|
+
...makeResumeRecovery((turnInput) => ({
|
|
26518
|
+
context,
|
|
26519
|
+
turnInput,
|
|
26520
|
+
stop: stopSessionInternal,
|
|
26521
|
+
restart: async (nextRecovery) => {
|
|
26522
|
+
await startSessionWithRecovery(
|
|
26523
|
+
{
|
|
26524
|
+
...input,
|
|
26525
|
+
...turnInput.modelSelection === void 0 ? {} : { modelSelection: turnInput.modelSelection }
|
|
26526
|
+
},
|
|
26527
|
+
nextRecovery
|
|
26528
|
+
);
|
|
26529
|
+
const restarted = sessions.get(input.threadId);
|
|
26530
|
+
if (restarted === void 0) {
|
|
26531
|
+
nextRecovery.controller.signal.throwIfAborted();
|
|
26532
|
+
throw new Error(`Claude session replaced: ${input.threadId}`);
|
|
26533
|
+
}
|
|
26534
|
+
return restarted;
|
|
26535
|
+
}
|
|
26536
|
+
}))
|
|
26537
|
+
};
|
|
26287
26538
|
const promptQueue = new PromptQueue();
|
|
26288
26539
|
const pendingApprovals = /* @__PURE__ */ new Map();
|
|
26289
26540
|
const pendingUserInputs = /* @__PURE__ */ new Map();
|
|
@@ -26331,7 +26582,10 @@ var makeStartSession = (deps) => {
|
|
|
26331
26582
|
workspaceWrite: claudeWorkspaceWriteAllowRules(additionalDirectories),
|
|
26332
26583
|
officialChannel: claudeUsesOfficialChannel(spawnEnvironment.processEnv)
|
|
26333
26584
|
};
|
|
26334
|
-
const sdkPermission = claudeSdkPermission(
|
|
26585
|
+
const sdkPermission = claudeSdkPermission(
|
|
26586
|
+
permissionRules,
|
|
26587
|
+
recovery?.previous?.currentPermissionMode ?? permissionMode
|
|
26588
|
+
);
|
|
26335
26589
|
const permissionAllow = sdkPermission.allow;
|
|
26336
26590
|
if (sdkPermission.mode !== permissionMode) {
|
|
26337
26591
|
log.warn("claude.permission.allowlistMode", {
|
|
@@ -26346,6 +26600,11 @@ var makeStartSession = (deps) => {
|
|
|
26346
26600
|
}
|
|
26347
26601
|
const stderrTail = createOutputTail(CLAUDE_STDERR_TAIL_CHARS);
|
|
26348
26602
|
const mcpConfig = await createClaudeMcpConfig(mcpServers, log);
|
|
26603
|
+
if (signal?.aborted || controller.signal.aborted) {
|
|
26604
|
+
await mcpConfig.cleanup();
|
|
26605
|
+
signal?.throwIfAborted();
|
|
26606
|
+
controller.signal.throwIfAborted();
|
|
26607
|
+
}
|
|
26349
26608
|
const cliProcess = makeClaudeProcessSpawner(deps.host, stderrTail.append, {
|
|
26350
26609
|
cleanup: mcpConfig.cleanup,
|
|
26351
26610
|
...mcpConfig.path === void 0 ? {} : { mcpConfigPath: mcpConfig.path },
|
|
@@ -26365,7 +26624,7 @@ var makeStartSession = (deps) => {
|
|
|
26365
26624
|
// SDK 要在启动时允许未来切入 Bypass;当前权限仍由 permissionMode 决定。
|
|
26366
26625
|
allowDangerouslySkipPermissions: true,
|
|
26367
26626
|
...Object.keys(querySettings).length > 0 ? { settings: querySettings } : {},
|
|
26368
|
-
...existingResumeSessionId ? { resume: existingResumeSessionId } : {},
|
|
26627
|
+
...forkTranscriptPath !== void 0 ? { resume: forkTranscriptPath, forkSession: true } : existingResumeSessionId ? { resume: existingResumeSessionId } : {},
|
|
26369
26628
|
...newSessionId ? { sessionId: newSessionId } : {},
|
|
26370
26629
|
includePartialMessages: true,
|
|
26371
26630
|
canUseTool,
|
|
@@ -26407,6 +26666,8 @@ var makeStartSession = (deps) => {
|
|
|
26407
26666
|
...Object.keys(extraArgs).length > 0 ? { extraArgs } : {},
|
|
26408
26667
|
...claudeSessionPlugins(input.skills)
|
|
26409
26668
|
};
|
|
26669
|
+
signal?.throwIfAborted();
|
|
26670
|
+
controller.signal.throwIfAborted();
|
|
26410
26671
|
let queryRuntime;
|
|
26411
26672
|
try {
|
|
26412
26673
|
queryRuntime = createQuery({
|
|
@@ -26415,6 +26676,14 @@ var makeStartSession = (deps) => {
|
|
|
26415
26676
|
});
|
|
26416
26677
|
} catch (cause) {
|
|
26417
26678
|
await cliProcess.clear();
|
|
26679
|
+
if (forkTranscriptPath !== void 0 && !controller.signal.aborted) {
|
|
26680
|
+
return startSessionWithRecovery(input, {
|
|
26681
|
+
...recovery,
|
|
26682
|
+
controller,
|
|
26683
|
+
forkFailed: true,
|
|
26684
|
+
resetReason: "fork-start-failed"
|
|
26685
|
+
});
|
|
26686
|
+
}
|
|
26418
26687
|
throw new Error("Failed to start Claude runtime session.", { cause });
|
|
26419
26688
|
}
|
|
26420
26689
|
const session = {
|
|
@@ -26430,6 +26699,7 @@ var makeStartSession = (deps) => {
|
|
|
26430
26699
|
threadId,
|
|
26431
26700
|
resume: sessionId,
|
|
26432
26701
|
...resumeState?.resumeSessionAt ? { resumeSessionAt: resumeState.resumeSessionAt } : {},
|
|
26702
|
+
...resumeState?.transcriptPath ? { transcriptPath: resumeState.transcriptPath } : {},
|
|
26433
26703
|
turnCount: resumeState?.turnCount ?? 0
|
|
26434
26704
|
},
|
|
26435
26705
|
createdAt: startedAt,
|
|
@@ -26439,11 +26709,16 @@ var makeStartSession = (deps) => {
|
|
|
26439
26709
|
session,
|
|
26440
26710
|
events: input.events,
|
|
26441
26711
|
bindingGeneration: input.bindingGeneration,
|
|
26442
|
-
//
|
|
26443
|
-
origin: input.resumeCursor === void 0 ? {} : {
|
|
26444
|
-
originResumeCursor: unresumable === void 0 ? input.resumeCursor : session.resumeCursor
|
|
26712
|
+
// 绑定内恢复沿用宿主通道的代次身份;游标会换号,不再把旧游标冻结成交互身份。
|
|
26713
|
+
origin: input.resumeCursor === void 0 || recovery !== void 0 && input.bindingGeneration !== void 0 ? {} : {
|
|
26714
|
+
originResumeCursor: recovery?.previous?.origin.originResumeCursor ?? (unresumable === void 0 ? input.resumeCursor : session.resumeCursor)
|
|
26445
26715
|
},
|
|
26446
|
-
launch: {
|
|
26716
|
+
launch: {
|
|
26717
|
+
sessionId,
|
|
26718
|
+
resumed: existingResumeSessionId !== void 0 || forkTranscriptPath !== void 0
|
|
26719
|
+
},
|
|
26720
|
+
...startup === void 0 ? {} : { startup },
|
|
26721
|
+
...recovery === void 0 ? {} : { recoveryController: controller },
|
|
26447
26722
|
promptQueue,
|
|
26448
26723
|
query: queryRuntime,
|
|
26449
26724
|
cliProcess,
|
|
@@ -26451,7 +26726,7 @@ var makeStartSession = (deps) => {
|
|
|
26451
26726
|
streamFiber: void 0,
|
|
26452
26727
|
startedAt,
|
|
26453
26728
|
basePermissionMode: permissionMode,
|
|
26454
|
-
currentPermissionMode: permissionMode,
|
|
26729
|
+
currentPermissionMode: recovery?.previous?.currentPermissionMode ?? permissionMode,
|
|
26455
26730
|
currentApiModelId: apiModelId,
|
|
26456
26731
|
// 上下文长度只信 SDK 结果里报的那个数,起步时不猜。
|
|
26457
26732
|
lastKnownContextWindow: void 0,
|
|
@@ -26464,6 +26739,7 @@ var makeStartSession = (deps) => {
|
|
|
26464
26739
|
currentPermissionAllow: permissionAllow,
|
|
26465
26740
|
interruptSettled: void 0,
|
|
26466
26741
|
resumeSessionId: sessionId,
|
|
26742
|
+
...resumeState?.transcriptPath ? { reportedTranscriptPath: resumeState.transcriptPath } : {},
|
|
26467
26743
|
pendingApprovals,
|
|
26468
26744
|
pendingUserInputs,
|
|
26469
26745
|
turns: [],
|
|
@@ -26475,7 +26751,7 @@ var makeStartSession = (deps) => {
|
|
|
26475
26751
|
workflowMemberFingerprints,
|
|
26476
26752
|
liveTaskIds,
|
|
26477
26753
|
responseMessageIds: /* @__PURE__ */ new Set(),
|
|
26478
|
-
turnState:
|
|
26754
|
+
turnState: recovery?.previous?.turnState,
|
|
26479
26755
|
compaction: void 0,
|
|
26480
26756
|
compactionInProgress: false,
|
|
26481
26757
|
pendingManualCompaction: false,
|
|
@@ -26489,6 +26765,13 @@ var makeStartSession = (deps) => {
|
|
|
26489
26765
|
launchConfirmed: false,
|
|
26490
26766
|
stopWork: void 0
|
|
26491
26767
|
};
|
|
26768
|
+
if (recovery?.previous?.turnState) {
|
|
26769
|
+
context.session = {
|
|
26770
|
+
...session,
|
|
26771
|
+
status: "running",
|
|
26772
|
+
activeTurnId: recovery.previous.turnState.turnId
|
|
26773
|
+
};
|
|
26774
|
+
}
|
|
26492
26775
|
contextDeferred.resolve(context);
|
|
26493
26776
|
sessions.set(threadId, context);
|
|
26494
26777
|
const sessionStartedStamp = await makeEventStamp();
|
|
@@ -26519,11 +26802,11 @@ var makeStartSession = (deps) => {
|
|
|
26519
26802
|
},
|
|
26520
26803
|
providerRefs: {}
|
|
26521
26804
|
});
|
|
26522
|
-
if (unresumable === "missing") {
|
|
26805
|
+
if (unresumable === "missing" && forkTranscriptPath === void 0) {
|
|
26523
26806
|
await emitRuntimeWarning(
|
|
26524
26807
|
context,
|
|
26525
26808
|
`Claude Code has no record of this thread's earlier conversation (session ${String(persistedResumeId)}), so the thread continues in a new Claude Code session. The messages here are kept, but the agent no longer has their context.`,
|
|
26526
|
-
void 0,
|
|
26809
|
+
resetReason === void 0 ? void 0 : { reason: resetReason },
|
|
26527
26810
|
"resume-thread-reset"
|
|
26528
26811
|
);
|
|
26529
26812
|
}
|
|
@@ -26535,7 +26818,7 @@ var makeStartSession = (deps) => {
|
|
|
26535
26818
|
createdAt: readyStamp.createdAt,
|
|
26536
26819
|
threadId,
|
|
26537
26820
|
payload: {
|
|
26538
|
-
state: "ready"
|
|
26821
|
+
state: recovery?.previous?.turnState ? "running" : "ready"
|
|
26539
26822
|
},
|
|
26540
26823
|
providerRefs: {}
|
|
26541
26824
|
});
|
|
@@ -26549,9 +26832,11 @@ var makeStartSession = (deps) => {
|
|
|
26549
26832
|
});
|
|
26550
26833
|
if (unresumable !== void 0 && persistedResumeId !== void 0)
|
|
26551
26834
|
unresumableSessions.forget(persistedResumeId);
|
|
26552
|
-
return withProcessGeneration(session, context);
|
|
26835
|
+
return withProcessGeneration(context.session, context);
|
|
26836
|
+
};
|
|
26837
|
+
return {
|
|
26838
|
+
startSession: (input, signal) => startSessionWithRecovery(input, void 0, signal)
|
|
26553
26839
|
};
|
|
26554
|
-
return { startSession };
|
|
26555
26840
|
};
|
|
26556
26841
|
|
|
26557
26842
|
// src/unresumableSessions.ts
|
|
@@ -26834,7 +27119,7 @@ var makeStreamEventHandler = (deps) => {
|
|
|
26834
27119
|
};
|
|
26835
27120
|
|
|
26836
27121
|
// src/claudeWorkspaceInventory.ts
|
|
26837
|
-
import * as
|
|
27122
|
+
import * as NodeFSP7 from "node:fs/promises";
|
|
26838
27123
|
import * as NodePath13 from "node:path";
|
|
26839
27124
|
var MAX_COMMAND_DEPTH = 5;
|
|
26840
27125
|
var MAX_COMMAND_FILES = 500;
|
|
@@ -26861,7 +27146,7 @@ var scanCommands = async (claudeDir) => {
|
|
|
26861
27146
|
for (let depth = 0; depth < MAX_COMMAND_DEPTH && level.length > 0; depth += 1) {
|
|
26862
27147
|
const next = [];
|
|
26863
27148
|
for (const directory of level) {
|
|
26864
|
-
const entries = await
|
|
27149
|
+
const entries = await NodeFSP7.readdir(directory, { withFileTypes: true }).catch(() => []);
|
|
26865
27150
|
for (const entry of entries.toSorted((left, right) => left.name.localeCompare(right.name))) {
|
|
26866
27151
|
const entryPath = NodePath13.join(directory, entry.name);
|
|
26867
27152
|
if (entry.isDirectory()) {
|
|
@@ -26875,7 +27160,7 @@ var scanCommands = async (claudeDir) => {
|
|
|
26875
27160
|
files += 1;
|
|
26876
27161
|
const name = entry.name.slice(0, -".md".length).trim();
|
|
26877
27162
|
if (name.length === 0 || commands.has(name)) continue;
|
|
26878
|
-
const contents = await
|
|
27163
|
+
const contents = await NodeFSP7.readFile(entryPath, "utf8").catch(() => void 0);
|
|
26879
27164
|
if (contents === void 0) continue;
|
|
26880
27165
|
const frontMatter = parseSkillFrontMatter(contents);
|
|
26881
27166
|
const description = frontMatter.description ?? firstBodyLine(contents);
|
|
@@ -26892,7 +27177,7 @@ var scanCommands = async (claudeDir) => {
|
|
|
26892
27177
|
};
|
|
26893
27178
|
var watchSkillsRoot = async (claudeDir) => {
|
|
26894
27179
|
const root = NodePath13.join(claudeDir, "skills");
|
|
26895
|
-
const entries = await
|
|
27180
|
+
const entries = await NodeFSP7.readdir(root).catch(() => []);
|
|
26896
27181
|
return [
|
|
26897
27182
|
root,
|
|
26898
27183
|
...entries.flatMap((entry) => [
|
|
@@ -27052,6 +27337,11 @@ var makeSystemMessageHandler = (deps) => {
|
|
|
27052
27337
|
return;
|
|
27053
27338
|
}
|
|
27054
27339
|
context.sessionEstablished = true;
|
|
27340
|
+
if (message.subtype === "init" && context.startup?.forkNotice) {
|
|
27341
|
+
context.startup.forkNotice.ready = true;
|
|
27342
|
+
await emitResumeForked(context, emitRuntimeWarning);
|
|
27343
|
+
}
|
|
27344
|
+
if (context.startup?.work === void 0) noteStartupEstablished(context);
|
|
27055
27345
|
const stamp = await makeEventStamp();
|
|
27056
27346
|
const base = {
|
|
27057
27347
|
...eventHeader(context, stamp),
|
|
@@ -27994,9 +28284,13 @@ var makeTurnMessageHandlers = (deps) => {
|
|
|
27994
28284
|
if (status === "failed") {
|
|
27995
28285
|
if ("errors" in message && Array.isArray(message.errors))
|
|
27996
28286
|
unresumableSessions.noteResultErrors(context, message.errors);
|
|
28287
|
+
const missingConversation = unresumableSessions.reasonFor(context.launch.sessionId) === "missing";
|
|
28288
|
+
if (suppressStartupFailure(context, missingConversation)) return;
|
|
27997
28289
|
await emitRuntimeError(context, errorMessage ?? "Claude turn failed.", void 0, category);
|
|
27998
28290
|
}
|
|
27999
28291
|
await completeTurn(context, status, errorMessage, message, category);
|
|
28292
|
+
settleStartup(context, "established");
|
|
28293
|
+
if (context.startup) context.startup.messages.length = 0;
|
|
28000
28294
|
};
|
|
28001
28295
|
return { handleUserMessage, handleAssistantMessage, handleResultMessage };
|
|
28002
28296
|
};
|
|
@@ -28022,7 +28316,7 @@ function claudeSignInCommand(credentialsPath, platform4) {
|
|
|
28022
28316
|
}
|
|
28023
28317
|
|
|
28024
28318
|
// src/textGeneration.ts
|
|
28025
|
-
import * as
|
|
28319
|
+
import * as NodeFSP8 from "node:fs/promises";
|
|
28026
28320
|
import * as NodeOS8 from "node:os";
|
|
28027
28321
|
import * as NodePath15 from "node:path";
|
|
28028
28322
|
import { mergeLaunchEnv as mergeLaunchEnv2 } from "@ycodium-ai/plugin-api/executor";
|
|
@@ -28484,14 +28778,14 @@ function createClaudeTextGeneration(recipe, host, modelCatalog) {
|
|
|
28484
28778
|
}
|
|
28485
28779
|
};
|
|
28486
28780
|
if (input.operation !== "generateThreadTitle") return runIn(input.cwd);
|
|
28487
|
-
const scratchDirectory = await
|
|
28781
|
+
const scratchDirectory = await NodeFSP8.mkdtemp(
|
|
28488
28782
|
NodePath15.join(NodeOS8.tmpdir(), "ycodium-claude-title-")
|
|
28489
28783
|
);
|
|
28490
28784
|
try {
|
|
28491
28785
|
return await runIn(scratchDirectory);
|
|
28492
28786
|
} finally {
|
|
28493
28787
|
if (safeToRemove) {
|
|
28494
|
-
await
|
|
28788
|
+
await NodeFSP8.rm(scratchDirectory, { recursive: true, force: true }).catch(() => void 0);
|
|
28495
28789
|
}
|
|
28496
28790
|
}
|
|
28497
28791
|
};
|
|
@@ -28834,14 +29128,14 @@ async function listTranscriptFiles2(root, sinceMs) {
|
|
|
28834
29128
|
continue;
|
|
28835
29129
|
}
|
|
28836
29130
|
if (!entry.isFile() || !entry.name.endsWith(TRANSCRIPT_SUFFIX2)) continue;
|
|
28837
|
-
let
|
|
29131
|
+
let stat5;
|
|
28838
29132
|
try {
|
|
28839
|
-
|
|
29133
|
+
stat5 = await NodeFS7.promises.stat(child);
|
|
28840
29134
|
} catch {
|
|
28841
29135
|
continue;
|
|
28842
29136
|
}
|
|
28843
|
-
if (
|
|
28844
|
-
found.push({ path: child, size:
|
|
29137
|
+
if (stat5.mtimeMs < sinceMs) continue;
|
|
29138
|
+
found.push({ path: child, size: stat5.size, mtimeMs: stat5.mtimeMs });
|
|
28845
29139
|
}
|
|
28846
29140
|
};
|
|
28847
29141
|
await walk(root);
|
|
@@ -28849,9 +29143,9 @@ async function listTranscriptFiles2(root, sinceMs) {
|
|
|
28849
29143
|
}
|
|
28850
29144
|
async function statTranscriptRoot(root) {
|
|
28851
29145
|
try {
|
|
28852
|
-
const
|
|
28853
|
-
if (!
|
|
28854
|
-
return { volumeId:
|
|
29146
|
+
const stat5 = await NodeFS7.promises.stat(root);
|
|
29147
|
+
if (!stat5.isDirectory()) return null;
|
|
29148
|
+
return { volumeId: stat5.ino === 0 ? "" : `${String(stat5.dev)}:${String(stat5.ino)}` };
|
|
28855
29149
|
} catch {
|
|
28856
29150
|
return null;
|
|
28857
29151
|
}
|
|
@@ -29419,7 +29713,8 @@ function createClaudeAdapter(recipe, host, options) {
|
|
|
29419
29713
|
const sessionCallbacks = makeSessionCallbacks({
|
|
29420
29714
|
emitter,
|
|
29421
29715
|
ensureSyntheticTurn,
|
|
29422
|
-
emitProposedPlanCompleted
|
|
29716
|
+
emitProposedPlanCompleted,
|
|
29717
|
+
updateResumeCursor
|
|
29423
29718
|
});
|
|
29424
29719
|
const { startSession } = makeStartSession({
|
|
29425
29720
|
host,
|
|
@@ -29441,9 +29736,23 @@ function createClaudeAdapter(recipe, host, options) {
|
|
|
29441
29736
|
unresumableSessions,
|
|
29442
29737
|
emitRuntimeWarning
|
|
29443
29738
|
});
|
|
29739
|
+
const stopSession = async (threadId) => {
|
|
29740
|
+
const context = sessions.get(threadId);
|
|
29741
|
+
if (!context) return;
|
|
29742
|
+
await stopSessionInternal(context);
|
|
29743
|
+
await context.startup?.work?.catch((cause) => {
|
|
29744
|
+
if (!context.startup?.controller.signal.aborted) throw cause;
|
|
29745
|
+
});
|
|
29746
|
+
await context.startup?.replacement?.catch((cause) => {
|
|
29747
|
+
if (!context.startup?.controller.signal.aborted) throw cause;
|
|
29748
|
+
});
|
|
29749
|
+
if (context.recoveryController) await context.cliProcess.waitForExit();
|
|
29750
|
+
const current = sessions.get(threadId);
|
|
29751
|
+
if (current && current !== context) await stopSessionInternal(current);
|
|
29752
|
+
};
|
|
29444
29753
|
const {
|
|
29445
29754
|
compactContext,
|
|
29446
|
-
interruptTurn,
|
|
29755
|
+
interruptTurn: interruptRunningTurn,
|
|
29447
29756
|
readThread,
|
|
29448
29757
|
respondToRequest,
|
|
29449
29758
|
setRuntimeMode,
|
|
@@ -29453,10 +29762,22 @@ function createClaudeAdapter(recipe, host, options) {
|
|
|
29453
29762
|
nowIso,
|
|
29454
29763
|
log: options?.log ?? host.log,
|
|
29455
29764
|
requireSession,
|
|
29456
|
-
stopSessionInternal
|
|
29765
|
+
stopSessionInternal: (context) => stopSession(context.session.threadId)
|
|
29457
29766
|
});
|
|
29767
|
+
const interruptTurn = async (threadId, turnId) => {
|
|
29768
|
+
const context = sessions.get(threadId);
|
|
29769
|
+
if ((context?.startup || context?.recoveryController) && !context.launchConfirmed) {
|
|
29770
|
+
if (turnId !== void 0 && context.turnState?.turnId !== turnId) return;
|
|
29771
|
+
await stopSession(threadId);
|
|
29772
|
+
return;
|
|
29773
|
+
}
|
|
29774
|
+
await interruptRunningTurn(threadId, turnId);
|
|
29775
|
+
};
|
|
29458
29776
|
const { sendTurn } = makeSendTurn({
|
|
29459
29777
|
emitter,
|
|
29778
|
+
findSession: (threadId) => sessions.get(threadId),
|
|
29779
|
+
stop: stopSessionInternal,
|
|
29780
|
+
unresumableSessions,
|
|
29460
29781
|
log: options?.log ?? host.log,
|
|
29461
29782
|
recipe,
|
|
29462
29783
|
boundInstanceId,
|
|
@@ -29469,11 +29790,6 @@ function createClaudeAdapter(recipe, host, options) {
|
|
|
29469
29790
|
completeTurn,
|
|
29470
29791
|
emitRuntimeWarning
|
|
29471
29792
|
});
|
|
29472
|
-
const stopSession = async (threadId) => {
|
|
29473
|
-
const context = sessions.get(threadId);
|
|
29474
|
-
if (!context) return;
|
|
29475
|
-
await stopSessionInternal(context);
|
|
29476
|
-
};
|
|
29477
29793
|
return {
|
|
29478
29794
|
/** 型号目录:探测用它读并拿当前这份;会话内部自己走同一份。 */
|
|
29479
29795
|
modelCatalog: {
|
|
@@ -29497,9 +29813,7 @@ function createClaudeAdapter(recipe, host, options) {
|
|
|
29497
29813
|
stopSession,
|
|
29498
29814
|
openCommandProcesses: async (threadId) => openCommandsOf(sessions.get(threadId)),
|
|
29499
29815
|
dispose: async () => {
|
|
29500
|
-
const results = await Promise.allSettled(
|
|
29501
|
-
[...sessions.values()].map((ctx) => stopSessionInternal(ctx))
|
|
29502
|
-
);
|
|
29816
|
+
const results = await Promise.allSettled([...sessions.keys()].map(stopSession));
|
|
29503
29817
|
const failures = results.flatMap(
|
|
29504
29818
|
(result) => result.status === "rejected" ? [result.reason] : []
|
|
29505
29819
|
);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ycodium-ai/agent-claude",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.4477",
|
|
4
4
|
"description": "Claude executor: Claude Code CLI via @anthropic-ai/claude-agent-sdk.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ycodium",
|
|
@@ -24,10 +24,10 @@
|
|
|
24
24
|
"@types/node": "24.12.4",
|
|
25
25
|
"esbuild": "0.28.1",
|
|
26
26
|
"vite-plus": "0.2.2",
|
|
27
|
+
"@ycodium-ai/plugin-api": "0.2.4477",
|
|
27
28
|
"ycodium-composer-tokens": "0.0.0",
|
|
28
|
-
"
|
|
29
|
-
"ycodium-executor-testing": "0.0.0"
|
|
30
|
-
"ycodium-jsonl-stream": "0.0.0"
|
|
29
|
+
"ycodium-jsonl-stream": "0.0.0",
|
|
30
|
+
"ycodium-executor-testing": "0.0.0"
|
|
31
31
|
},
|
|
32
32
|
"ycodium": {
|
|
33
33
|
"plugin": "./dist/index.js",
|