@ai-sdk/harness-claude-code 1.0.176 → 1.0.178
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/CHANGELOG.md +37 -0
- package/dist/bridge/index.mjs +69 -9
- package/dist/bridge/index.mjs.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +134 -37
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
- package/src/bridge/index.ts +69 -5
- package/src/claude-code-bridge-protocol.ts +4 -1
- package/src/claude-code-harness.ts +158 -16
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/harness-claude-code",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.178",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -26,9 +26,9 @@
|
|
|
26
26
|
}
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@ai-sdk/harness": "1.0.
|
|
30
|
-
"@ai-sdk/provider": "4.0.
|
|
31
|
-
"@ai-sdk/provider-utils": "5.0.
|
|
29
|
+
"@ai-sdk/harness": "1.0.174",
|
|
30
|
+
"@ai-sdk/provider": "4.0.36",
|
|
31
|
+
"@ai-sdk/provider-utils": "5.0.75",
|
|
32
32
|
"ws": "^8.21.0"
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|
package/src/bridge/index.ts
CHANGED
|
@@ -153,7 +153,41 @@ let lastClaudeSessionId: string | undefined;
|
|
|
153
153
|
await runBridge<StartMessage>({
|
|
154
154
|
bridgeType: 'claude-code',
|
|
155
155
|
bridgeStateDir,
|
|
156
|
-
onStart:
|
|
156
|
+
onStart: async (start, turn) => {
|
|
157
|
+
const emit: BridgeTurn['emit'] = event => {
|
|
158
|
+
if (turn.abortSignal.aborted) return;
|
|
159
|
+
turn.emit({
|
|
160
|
+
...event,
|
|
161
|
+
...(start.startId == null ? {} : { startId: start.startId }),
|
|
162
|
+
});
|
|
163
|
+
};
|
|
164
|
+
/*
|
|
165
|
+
* The shared emitError closes over its own emitter, so wrapping turn.emit
|
|
166
|
+
* cannot attach startId to errors or suppress them after abort. Duplicating
|
|
167
|
+
* its stderr logging and error serialization here is an adapter-local
|
|
168
|
+
* workaround to keep errors scoped to their turn without changing the
|
|
169
|
+
* shared bridge. Keep this duplication aligned with shared error handling.
|
|
170
|
+
*/
|
|
171
|
+
const emitError: BridgeTurn['emitError'] = ({ error, message }) => {
|
|
172
|
+
if (turn.abortSignal.aborted) return;
|
|
173
|
+
console.error(
|
|
174
|
+
`[harness:claude-code:error] ${message ?? 'bridge error'}:`,
|
|
175
|
+
error,
|
|
176
|
+
);
|
|
177
|
+
emit({
|
|
178
|
+
type: 'error',
|
|
179
|
+
error:
|
|
180
|
+
error instanceof Error
|
|
181
|
+
? { name: error.name, message: error.message, stack: error.stack }
|
|
182
|
+
: error,
|
|
183
|
+
});
|
|
184
|
+
};
|
|
185
|
+
try {
|
|
186
|
+
await runTurn(start, { ...turn, emit, emitError });
|
|
187
|
+
} catch (error) {
|
|
188
|
+
emitError({ error, message: 'claude-code turn failed' });
|
|
189
|
+
}
|
|
190
|
+
},
|
|
157
191
|
// Claude Code's conversation state lives in the runtime's own store, keyed
|
|
158
192
|
// by working directory. The resume payload names the exact conversation so a
|
|
159
193
|
// later resume does not have to fall back to "most recent in this workdir".
|
|
@@ -390,7 +424,7 @@ function createPermissionSettings(input: {
|
|
|
390
424
|
* the id Claude Code expects (`claude-opus-5-5`) to the gateway id, which
|
|
391
425
|
* stays the id sent to the gateway.
|
|
392
426
|
*/
|
|
393
|
-
function
|
|
427
|
+
function toGatewayModelOverride(
|
|
394
428
|
model: string | undefined,
|
|
395
429
|
): Record<string, string> | undefined {
|
|
396
430
|
if (!model?.startsWith('anthropic/')) return undefined;
|
|
@@ -400,6 +434,26 @@ function createGatewayModelOverrides(
|
|
|
400
434
|
return id.includes('.') ? { [id.replace(/\./g, '-')]: model } : undefined;
|
|
401
435
|
}
|
|
402
436
|
|
|
437
|
+
/**
|
|
438
|
+
* The `ANTHROPIC_DEFAULT_*_MODEL` variables point Claude Code's model aliases
|
|
439
|
+
* at other ids and `CLAUDE_CODE_SUBAGENT_MODEL` sets the model of subagents,
|
|
440
|
+
* which it misreads the same way. `model` comes last so that its entry wins a
|
|
441
|
+
* key it shares with one of them.
|
|
442
|
+
*/
|
|
443
|
+
function createGatewayModelOverrides(
|
|
444
|
+
model: string | undefined,
|
|
445
|
+
env: Record<string, string | undefined>,
|
|
446
|
+
): Record<string, string> | undefined {
|
|
447
|
+
const overrides = {
|
|
448
|
+
...toGatewayModelOverride(env.ANTHROPIC_DEFAULT_OPUS_MODEL),
|
|
449
|
+
...toGatewayModelOverride(env.ANTHROPIC_DEFAULT_SONNET_MODEL),
|
|
450
|
+
...toGatewayModelOverride(env.ANTHROPIC_DEFAULT_HAIKU_MODEL),
|
|
451
|
+
...toGatewayModelOverride(env.CLAUDE_CODE_SUBAGENT_MODEL),
|
|
452
|
+
...toGatewayModelOverride(model),
|
|
453
|
+
};
|
|
454
|
+
return Object.keys(overrides).length > 0 ? overrides : undefined;
|
|
455
|
+
}
|
|
456
|
+
|
|
403
457
|
async function needsHostApproval(input: {
|
|
404
458
|
toolName: string;
|
|
405
459
|
toolInput: unknown;
|
|
@@ -552,7 +606,16 @@ function nativeToolRequiresApproval(input: {
|
|
|
552
606
|
}
|
|
553
607
|
|
|
554
608
|
async function runTurn(start: StartMessage, turn: BridgeTurn): Promise<void> {
|
|
555
|
-
|
|
609
|
+
/*
|
|
610
|
+
* The host settles an aborted turn before its query finishes interrupting.
|
|
611
|
+
* Keep draining the query for graceful shutdown, but do not send its remaining
|
|
612
|
+
* events to listeners that may already belong to the next turn.
|
|
613
|
+
*/
|
|
614
|
+
const emit: Emit = msg => {
|
|
615
|
+
if (!turn.abortSignal.aborted) {
|
|
616
|
+
turn.emit(msg as BridgeEvent);
|
|
617
|
+
}
|
|
618
|
+
};
|
|
556
619
|
|
|
557
620
|
// Local controller for the Claude query. Aborted either by the host (via the
|
|
558
621
|
// shared runtime's `turn.abortSignal`) or by us on a terminal error.
|
|
@@ -745,7 +808,8 @@ async function runTurn(start: StartMessage, turn: BridgeTurn): Promise<void> {
|
|
|
745
808
|
approvalRequestedToolUseIds: streamEventState.approvalRequestedToolUseIds,
|
|
746
809
|
emittedToolCallIds: streamEventState.emittedToolCallIds,
|
|
747
810
|
});
|
|
748
|
-
const
|
|
811
|
+
const sdkEnv = { ...procEnv, ...start.env };
|
|
812
|
+
const modelOverrides = createGatewayModelOverrides(start.model, sdkEnv);
|
|
749
813
|
const deferredToolUseIds = new Set<string>();
|
|
750
814
|
const approvalPreToolUseHook = createApprovalPreToolUseHook({
|
|
751
815
|
toolCwds,
|
|
@@ -790,7 +854,7 @@ async function runTurn(start: StartMessage, turn: BridgeTurn): Promise<void> {
|
|
|
790
854
|
...(start.forwardSubagentText !== undefined
|
|
791
855
|
? { forwardSubagentText: start.forwardSubagentText }
|
|
792
856
|
: {}),
|
|
793
|
-
...(start.env !== undefined ? { env:
|
|
857
|
+
...(start.env !== undefined ? { env: sdkEnv } : {}),
|
|
794
858
|
...(skillsOption ? { skills: skillsOption } : {}),
|
|
795
859
|
...(nativeTools !== undefined ? { tools: nativeTools } : {}),
|
|
796
860
|
...(inactiveNativeTools.length > 0
|
|
@@ -14,7 +14,10 @@ import { z } from 'zod/v4';
|
|
|
14
14
|
* payload, which carries Claude SDK configuration.
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
export const outboundMessageSchema =
|
|
17
|
+
export const outboundMessageSchema = z.intersection(
|
|
18
|
+
harnessV1BridgeOutboundMessageSchema,
|
|
19
|
+
z.object({ startId: z.string().optional() }),
|
|
20
|
+
);
|
|
18
21
|
export type OutboundMessage = z.infer<typeof outboundMessageSchema>;
|
|
19
22
|
|
|
20
23
|
const thinkingDisplaySchema = z.enum(['summarized', 'omitted']).optional();
|
|
@@ -50,6 +50,7 @@ import {
|
|
|
50
50
|
type SandboxChannelReconnectOptions,
|
|
51
51
|
} from '@ai-sdk/harness/utils';
|
|
52
52
|
import {
|
|
53
|
+
generateId,
|
|
53
54
|
tool,
|
|
54
55
|
type Experimental_SandboxSession as SandboxSession,
|
|
55
56
|
type Experimental_SandboxProcess,
|
|
@@ -188,6 +189,8 @@ export type ClaudeCodeHarnessSettings = {
|
|
|
188
189
|
readonly portEndpoint?: HarnessV1PortEndpoint;
|
|
189
190
|
/** Maximum milliseconds to wait for the bridge to advertise its port. Defaults to 120000. */
|
|
190
191
|
readonly startupTimeoutMs?: number;
|
|
192
|
+
/** Maximum milliseconds to wait for the first turn event. Defaults to 120000. */
|
|
193
|
+
readonly turnStartTimeoutMs?: number;
|
|
191
194
|
/**
|
|
192
195
|
* Configures reconnection attempts after an established bridge connection
|
|
193
196
|
* drops. The reconnect window includes connection establishment and
|
|
@@ -858,6 +861,7 @@ const claudeCodeBridgeCoordsSchema = z.object({
|
|
|
858
861
|
port: z.number(),
|
|
859
862
|
token: z.string(),
|
|
860
863
|
lastSeenEventId: z.number(),
|
|
864
|
+
startId: z.string().optional(),
|
|
861
865
|
sandboxId: z.string().optional(),
|
|
862
866
|
});
|
|
863
867
|
|
|
@@ -1050,7 +1054,9 @@ export function createClaudeCode(
|
|
|
1050
1054
|
sessionId: startOpts.sessionId,
|
|
1051
1055
|
});
|
|
1052
1056
|
let supportsUserMessageResponses = false;
|
|
1057
|
+
let bridgeHello: BridgeHello | undefined;
|
|
1053
1058
|
const onHello = (hello: BridgeHello) => {
|
|
1059
|
+
bridgeHello = hello;
|
|
1054
1060
|
supportsUserMessageResponses =
|
|
1055
1061
|
hello.capabilities?.experimental_userMessageResponses === true;
|
|
1056
1062
|
};
|
|
@@ -1096,6 +1102,7 @@ export function createClaudeCode(
|
|
|
1096
1102
|
return createSession({
|
|
1097
1103
|
sessionId: startOpts.sessionId,
|
|
1098
1104
|
channel: attachChannel,
|
|
1105
|
+
startId: coords.startId,
|
|
1099
1106
|
finishListenerAttachment,
|
|
1100
1107
|
...(resumeSessionId ? { resumeSessionId } : {}),
|
|
1101
1108
|
// The live bridge was spawned by another process; this one owns no
|
|
@@ -1128,6 +1135,8 @@ export function createClaudeCode(
|
|
|
1128
1135
|
mcpServers: settings.mcpServers,
|
|
1129
1136
|
toolHints: settings.toolHints,
|
|
1130
1137
|
supportsUserMessageResponses: () => supportsUserMessageResponses,
|
|
1138
|
+
getBridgeHello: () => bridgeHello,
|
|
1139
|
+
turnStartTimeoutMs: settings.turnStartTimeoutMs ?? 120_000,
|
|
1131
1140
|
});
|
|
1132
1141
|
} catch {
|
|
1133
1142
|
// Bridge no longer reachable — recover by respawning below.
|
|
@@ -1277,6 +1286,7 @@ export function createClaudeCode(
|
|
|
1277
1286
|
sessionId: startOpts.sessionId,
|
|
1278
1287
|
channel,
|
|
1279
1288
|
finishListenerAttachment,
|
|
1289
|
+
startId: respawnStrategy === 'replay' ? coords?.startId : undefined,
|
|
1280
1290
|
proc,
|
|
1281
1291
|
maxTurns: settings.maxTurns,
|
|
1282
1292
|
skillOverrides: settings.skillOverrides,
|
|
@@ -1314,6 +1324,8 @@ export function createClaudeCode(
|
|
|
1314
1324
|
mcpServers: settings.mcpServers,
|
|
1315
1325
|
toolHints: settings.toolHints,
|
|
1316
1326
|
supportsUserMessageResponses: () => supportsUserMessageResponses,
|
|
1327
|
+
getBridgeHello: () => bridgeHello,
|
|
1328
|
+
turnStartTimeoutMs: settings.turnStartTimeoutMs ?? 120_000,
|
|
1317
1329
|
});
|
|
1318
1330
|
},
|
|
1319
1331
|
};
|
|
@@ -1329,6 +1341,7 @@ export function createClaudeCode(
|
|
|
1329
1341
|
function createSession({
|
|
1330
1342
|
sessionId,
|
|
1331
1343
|
channel,
|
|
1344
|
+
startId,
|
|
1332
1345
|
finishListenerAttachment,
|
|
1333
1346
|
proc,
|
|
1334
1347
|
maxTurns,
|
|
@@ -1359,9 +1372,12 @@ function createSession({
|
|
|
1359
1372
|
mcpServers,
|
|
1360
1373
|
toolHints,
|
|
1361
1374
|
supportsUserMessageResponses,
|
|
1375
|
+
getBridgeHello,
|
|
1376
|
+
turnStartTimeoutMs,
|
|
1362
1377
|
}: {
|
|
1363
1378
|
sessionId: string;
|
|
1364
1379
|
channel: ClaudeCodeChannel;
|
|
1380
|
+
startId?: string;
|
|
1365
1381
|
finishListenerAttachment: (() => void) | undefined;
|
|
1366
1382
|
/** Undefined on `attach` — the live bridge was spawned by another process. */
|
|
1367
1383
|
proc: Experimental_SandboxProcess | undefined;
|
|
@@ -1403,6 +1419,8 @@ function createSession({
|
|
|
1403
1419
|
mcpServers: Record<string, unknown> | undefined;
|
|
1404
1420
|
toolHints: ClaudeCodeHarnessSettings['toolHints'];
|
|
1405
1421
|
supportsUserMessageResponses: () => boolean;
|
|
1422
|
+
getBridgeHello: () => BridgeHello | undefined;
|
|
1423
|
+
turnStartTimeoutMs: number;
|
|
1406
1424
|
}): HarnessV1Session {
|
|
1407
1425
|
let stopped = false;
|
|
1408
1426
|
let stopPromise: Promise<void> | undefined;
|
|
@@ -1422,6 +1440,7 @@ function createSession({
|
|
|
1422
1440
|
* stop/detach must record for exact resume.
|
|
1423
1441
|
*/
|
|
1424
1442
|
let lastClaudeSessionId = resumeSessionId;
|
|
1443
|
+
let activeStartId = startId;
|
|
1425
1444
|
|
|
1426
1445
|
/*
|
|
1427
1446
|
* Wire the channel into one turn's worth of events and return the control
|
|
@@ -1433,31 +1452,55 @@ function createSession({
|
|
|
1433
1452
|
const wireTurn = (turnOpts: {
|
|
1434
1453
|
emit: (event: HarnessV1StreamPart) => void;
|
|
1435
1454
|
abortSignal?: AbortSignal;
|
|
1436
|
-
|
|
1455
|
+
startId?: string;
|
|
1456
|
+
}): {
|
|
1457
|
+
control: HarnessV1PromptControl;
|
|
1458
|
+
sendStart: (message: Extract<InboundMessage, { type: 'start' }>) => void;
|
|
1459
|
+
} => {
|
|
1460
|
+
activeStartId = turnOpts.startId;
|
|
1461
|
+
let expectedStartId = turnOpts.startId;
|
|
1462
|
+
let isSettled = false;
|
|
1463
|
+
const belongsToTurn = (event: { startId?: string }): boolean => {
|
|
1464
|
+
if (isSettled) return false;
|
|
1465
|
+
if (event.startId == null) return true;
|
|
1466
|
+
if (expectedStartId == null) {
|
|
1467
|
+
expectedStartId = event.startId;
|
|
1468
|
+
activeStartId = event.startId;
|
|
1469
|
+
}
|
|
1470
|
+
return event.startId === expectedStartId;
|
|
1471
|
+
};
|
|
1437
1472
|
let pendingResolve: (() => void) | undefined;
|
|
1438
1473
|
let pendingReject: ((err: unknown) => void) | undefined;
|
|
1439
1474
|
const done = new Promise<void>((resolve, reject) => {
|
|
1440
1475
|
pendingResolve = resolve;
|
|
1441
1476
|
pendingReject = reject;
|
|
1442
1477
|
});
|
|
1443
|
-
const userMessageSubmitter = supportsUserMessageResponses()
|
|
1444
|
-
? experimental_createBridgeUserMessageSubmitter({
|
|
1445
|
-
send: message => channel.send(message),
|
|
1446
|
-
onResponse: listener => channel.on('user-message-response', listener),
|
|
1447
|
-
onReconnect: listener => channel.onReconnect(listener),
|
|
1448
|
-
})
|
|
1449
|
-
: undefined;
|
|
1450
1478
|
const unsubs: Array<() => void> = [];
|
|
1451
|
-
|
|
1479
|
+
let pendingStart: Extract<InboundMessage, { type: 'start' }> | undefined;
|
|
1480
|
+
let awaitingStartSend = false;
|
|
1481
|
+
let startCursor = 0;
|
|
1482
|
+
let startTimer: ReturnType<typeof setTimeout> | undefined;
|
|
1483
|
+
const clearPendingStart = () => {
|
|
1484
|
+
pendingStart = undefined;
|
|
1485
|
+
clearTimeout(startTimer);
|
|
1486
|
+
};
|
|
1487
|
+
const forward = (event: HarnessV1StreamPart & { startId?: string }) => {
|
|
1488
|
+
clearPendingStart();
|
|
1489
|
+
/*
|
|
1490
|
+
* Keep the event object: the channel attaches a non-enumerable replay
|
|
1491
|
+
* checkpoint that must reach the consumer for step-boundary suspension.
|
|
1492
|
+
* Only the adapter's wire-level turn identifier is removed.
|
|
1493
|
+
*/
|
|
1494
|
+
delete event.startId;
|
|
1452
1495
|
try {
|
|
1453
1496
|
turnOpts.emit(event);
|
|
1454
1497
|
} catch {}
|
|
1455
1498
|
};
|
|
1456
1499
|
|
|
1457
|
-
let isSettled = false;
|
|
1458
1500
|
const settleSuccess = () => {
|
|
1459
1501
|
if (isSettled) return;
|
|
1460
1502
|
isSettled = true;
|
|
1503
|
+
clearPendingStart();
|
|
1461
1504
|
userMessageSubmitter?.close();
|
|
1462
1505
|
for (const u of unsubs) u();
|
|
1463
1506
|
pendingResolve!();
|
|
@@ -1465,11 +1508,68 @@ function createSession({
|
|
|
1465
1508
|
const settleError = (err: unknown) => {
|
|
1466
1509
|
if (isSettled) return;
|
|
1467
1510
|
isSettled = true;
|
|
1511
|
+
clearPendingStart();
|
|
1468
1512
|
userMessageSubmitter?.close(err);
|
|
1469
1513
|
for (const u of unsubs) u();
|
|
1470
1514
|
pendingReject!(err);
|
|
1471
1515
|
};
|
|
1472
1516
|
|
|
1517
|
+
const sendPendingStart = () => {
|
|
1518
|
+
if (pendingStart == null) return;
|
|
1519
|
+
awaitingStartSend = true;
|
|
1520
|
+
try {
|
|
1521
|
+
channel.send(pendingStart, {
|
|
1522
|
+
onSent: () => {
|
|
1523
|
+
awaitingStartSend = false;
|
|
1524
|
+
},
|
|
1525
|
+
});
|
|
1526
|
+
} catch (error) {
|
|
1527
|
+
awaitingStartSend = false;
|
|
1528
|
+
throw error;
|
|
1529
|
+
}
|
|
1530
|
+
};
|
|
1531
|
+
const failStart = (error: Error) => {
|
|
1532
|
+
settleError(error);
|
|
1533
|
+
channel.beginClose();
|
|
1534
|
+
try {
|
|
1535
|
+
channel.send({ type: 'abort' });
|
|
1536
|
+
} catch {}
|
|
1537
|
+
channel.close();
|
|
1538
|
+
};
|
|
1539
|
+
unsubs.push(
|
|
1540
|
+
channel.onReconnect(() => {
|
|
1541
|
+
if (pendingStart == null || isSettled || awaitingStartSend) return;
|
|
1542
|
+
const hello = getBridgeHello();
|
|
1543
|
+
if (hello?.capabilities?.idempotentStart === true) {
|
|
1544
|
+
sendPendingStart();
|
|
1545
|
+
return;
|
|
1546
|
+
}
|
|
1547
|
+
if (
|
|
1548
|
+
hello?.state !== 'waiting' ||
|
|
1549
|
+
hello.lastSeq == null ||
|
|
1550
|
+
hello.lastSeq > startCursor
|
|
1551
|
+
) {
|
|
1552
|
+
return;
|
|
1553
|
+
}
|
|
1554
|
+
failStart(
|
|
1555
|
+
new Error(
|
|
1556
|
+
'claude-code bridge is waiting for a start after reconnect and does not support safe start retries.',
|
|
1557
|
+
),
|
|
1558
|
+
);
|
|
1559
|
+
}),
|
|
1560
|
+
);
|
|
1561
|
+
|
|
1562
|
+
// Retry the turn start before the submitter replays pending steering.
|
|
1563
|
+
const userMessageSubmitter = supportsUserMessageResponses()
|
|
1564
|
+
? experimental_createBridgeUserMessageSubmitter({
|
|
1565
|
+
// The submitter owns replay, after the turn start has been retried.
|
|
1566
|
+
send: message =>
|
|
1567
|
+
channel.send(message, { queueIfDisconnected: false }),
|
|
1568
|
+
onResponse: listener => channel.on('user-message-response', listener),
|
|
1569
|
+
onReconnect: listener => channel.onReconnect(listener),
|
|
1570
|
+
})
|
|
1571
|
+
: undefined;
|
|
1572
|
+
|
|
1473
1573
|
const eventTypes = [
|
|
1474
1574
|
'stream-start',
|
|
1475
1575
|
'text-start',
|
|
@@ -1491,12 +1591,14 @@ function createSession({
|
|
|
1491
1591
|
for (const type of eventTypes) {
|
|
1492
1592
|
unsubs.push(
|
|
1493
1593
|
channel.on(type, msg => {
|
|
1594
|
+
if (!belongsToTurn(msg)) return;
|
|
1494
1595
|
forward(msg);
|
|
1495
1596
|
}),
|
|
1496
1597
|
);
|
|
1497
1598
|
}
|
|
1498
1599
|
unsubs.push(
|
|
1499
1600
|
channel.on('finish', msg => {
|
|
1601
|
+
if (!belongsToTurn(msg)) return;
|
|
1500
1602
|
const metadata = (
|
|
1501
1603
|
msg as {
|
|
1502
1604
|
harnessMetadata?: { 'claude-code'?: { sessionId?: unknown } };
|
|
@@ -1511,6 +1613,7 @@ function createSession({
|
|
|
1511
1613
|
);
|
|
1512
1614
|
unsubs.push(
|
|
1513
1615
|
channel.on('error', msg => {
|
|
1616
|
+
if (!belongsToTurn(msg)) return;
|
|
1514
1617
|
forward(msg);
|
|
1515
1618
|
settleError(msg.error);
|
|
1516
1619
|
}),
|
|
@@ -1556,7 +1659,7 @@ function createSession({
|
|
|
1556
1659
|
}
|
|
1557
1660
|
}
|
|
1558
1661
|
|
|
1559
|
-
|
|
1662
|
+
const control: HarnessV1PromptControl = {
|
|
1560
1663
|
submitToolResult: async input => {
|
|
1561
1664
|
channel.send({
|
|
1562
1665
|
type: 'tool-result',
|
|
@@ -1585,6 +1688,35 @@ function createSession({
|
|
|
1585
1688
|
}),
|
|
1586
1689
|
done,
|
|
1587
1690
|
};
|
|
1691
|
+
return {
|
|
1692
|
+
control,
|
|
1693
|
+
sendStart: message => {
|
|
1694
|
+
if (isSettled) return;
|
|
1695
|
+
startCursor = Math.max(
|
|
1696
|
+
channel.lastSeenEventId ?? 0,
|
|
1697
|
+
getBridgeHello()?.lastSeq ?? 0,
|
|
1698
|
+
);
|
|
1699
|
+
pendingStart = { ...message, startId: expectedStartId };
|
|
1700
|
+
startTimer = setTimeout(() => {
|
|
1701
|
+
if (isSettled || pendingStart == null) return;
|
|
1702
|
+
failStart(
|
|
1703
|
+
new Error(
|
|
1704
|
+
`claude-code bridge did not emit a turn event within ${turnStartTimeoutMs}ms.`,
|
|
1705
|
+
),
|
|
1706
|
+
);
|
|
1707
|
+
}, turnStartTimeoutMs);
|
|
1708
|
+
startTimer.unref?.();
|
|
1709
|
+
try {
|
|
1710
|
+
sendPendingStart();
|
|
1711
|
+
} catch (error) {
|
|
1712
|
+
// The caller never receives control when the initial send throws.
|
|
1713
|
+
// Consume its rejection and remove the timer and turn listeners.
|
|
1714
|
+
void done.catch(() => {});
|
|
1715
|
+
settleError(error);
|
|
1716
|
+
throw error;
|
|
1717
|
+
}
|
|
1718
|
+
},
|
|
1719
|
+
};
|
|
1588
1720
|
};
|
|
1589
1721
|
|
|
1590
1722
|
const prepareTurn = async (turnOpts: {
|
|
@@ -1592,7 +1724,8 @@ function createSession({
|
|
|
1592
1724
|
skills: ReadonlyArray<HarnessV1Skill>;
|
|
1593
1725
|
emit: (event: HarnessV1StreamPart) => void;
|
|
1594
1726
|
abortSignal?: AbortSignal;
|
|
1595
|
-
|
|
1727
|
+
startId?: string;
|
|
1728
|
+
}): Promise<ReturnType<typeof wireTurn>> => {
|
|
1596
1729
|
if (
|
|
1597
1730
|
turnOpts.responseFormat?.type === 'json' &&
|
|
1598
1731
|
turnOpts.responseFormat.schema == null
|
|
@@ -1618,6 +1751,7 @@ function createSession({
|
|
|
1618
1751
|
return wireTurn({
|
|
1619
1752
|
emit: turnOpts.emit,
|
|
1620
1753
|
abortSignal: turnOpts.abortSignal,
|
|
1754
|
+
startId: turnOpts.startId,
|
|
1621
1755
|
});
|
|
1622
1756
|
};
|
|
1623
1757
|
|
|
@@ -1625,7 +1759,10 @@ function createSession({
|
|
|
1625
1759
|
sessionId,
|
|
1626
1760
|
isResume,
|
|
1627
1761
|
doPromptTurn: async promptOpts => {
|
|
1628
|
-
const control = await prepareTurn(
|
|
1762
|
+
const { control, sendStart } = await prepareTurn({
|
|
1763
|
+
...promptOpts,
|
|
1764
|
+
startId: generateId(),
|
|
1765
|
+
});
|
|
1629
1766
|
|
|
1630
1767
|
/*
|
|
1631
1768
|
* A signal that was already aborted has settled the turn inside
|
|
@@ -1691,12 +1828,15 @@ function createSession({
|
|
|
1691
1828
|
pendingResumeFlag = false;
|
|
1692
1829
|
// Recovery starts a native turn once. Later continuations attach to it.
|
|
1693
1830
|
rerunContinue = false;
|
|
1694
|
-
|
|
1831
|
+
sendStart(startMessage);
|
|
1695
1832
|
|
|
1696
1833
|
return control;
|
|
1697
1834
|
},
|
|
1698
1835
|
doContinueTurn: async continueOpts => {
|
|
1699
|
-
const control = await prepareTurn(
|
|
1836
|
+
const { control, sendStart } = await prepareTurn({
|
|
1837
|
+
...continueOpts,
|
|
1838
|
+
startId: rerunContinue ? generateId() : activeStartId,
|
|
1839
|
+
});
|
|
1700
1840
|
|
|
1701
1841
|
/*
|
|
1702
1842
|
* attach / replay: the still-running (or disk-replayed) turn streams into
|
|
@@ -1716,7 +1856,7 @@ function createSession({
|
|
|
1716
1856
|
if (rerunContinue && !continueOpts.abortSignal?.aborted) {
|
|
1717
1857
|
rerunContinue = false;
|
|
1718
1858
|
pendingResumeFlag = false;
|
|
1719
|
-
|
|
1859
|
+
sendStart({
|
|
1720
1860
|
type: 'start' as const,
|
|
1721
1861
|
/*
|
|
1722
1862
|
* A continuation nudge rather than an empty prompt: `continue: true`
|
|
@@ -1809,6 +1949,7 @@ function createSession({
|
|
|
1809
1949
|
port: bridgePort,
|
|
1810
1950
|
token: bridgeToken,
|
|
1811
1951
|
lastSeenEventId,
|
|
1952
|
+
...(activeStartId == null ? {} : { startId: activeStartId }),
|
|
1812
1953
|
...(sandboxId == null ? {} : { sandboxId }),
|
|
1813
1954
|
},
|
|
1814
1955
|
...(lastClaudeSessionId
|
|
@@ -1968,6 +2109,7 @@ function createSession({
|
|
|
1968
2109
|
port: bridgePort,
|
|
1969
2110
|
token: bridgeToken,
|
|
1970
2111
|
lastSeenEventId,
|
|
2112
|
+
...(activeStartId == null ? {} : { startId: activeStartId }),
|
|
1971
2113
|
...(sandboxId == null ? {} : { sandboxId }),
|
|
1972
2114
|
},
|
|
1973
2115
|
...(lastClaudeSessionId
|