pi-agy-provider 1.1.2 → 1.1.3
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/index.ts +22 -10
- package/lib/agy-acp-client.ts +191 -29
- package/lib/agy-acp-driver.ts +154 -85
- package/lib/agy-client.ts +21 -3
- package/lib/agy-driver.ts +2 -2
- package/lib/agy-thoughts.ts +64 -6
- package/lib/bridge.ts +113 -16
- package/lib/events.ts +1 -0
- package/lib/models.ts +43 -7
- package/lib/pi-tools-agent.ts +4 -0
- package/lib/prompt.ts +67 -13
- package/lib/reducer.ts +1 -0
- package/lib/turn.ts +1 -0
- package/package.json +1 -1
- package/src/provider.ts +92 -5
- package/src/runtime.ts +33 -2
package/lib/agy-acp-driver.ts
CHANGED
|
@@ -15,10 +15,12 @@ import {
|
|
|
15
15
|
AcpTurnNormalizer,
|
|
16
16
|
AgyAcpClient,
|
|
17
17
|
AgyAcpRpcError,
|
|
18
|
+
AgyAcpTimeoutError,
|
|
18
19
|
acpUsageToAgyUsage,
|
|
19
20
|
buildAcpAgyArgs,
|
|
20
21
|
buildAcpChildEnvironment,
|
|
21
22
|
acpEmptyTurnApiFailure,
|
|
23
|
+
detectAcpPlainTextQuotaExhaustion,
|
|
22
24
|
extractAcpQuotaSnapshot,
|
|
23
25
|
formatAcpStopError,
|
|
24
26
|
modelsFromAcpSessionState,
|
|
@@ -45,6 +47,7 @@ import {
|
|
|
45
47
|
canFallbackToLegacyEffortSuffix,
|
|
46
48
|
extractAgyStderrSummary,
|
|
47
49
|
isAgyModelEffortFallbackError,
|
|
50
|
+
redactAgySecrets,
|
|
48
51
|
rememberLegacyEffortSuffixModel,
|
|
49
52
|
type AgyTurnRequest,
|
|
50
53
|
} from "./agy-client.ts";
|
|
@@ -73,6 +76,7 @@ import {
|
|
|
73
76
|
type SlidingDeadline,
|
|
74
77
|
} from "./reducer.ts";
|
|
75
78
|
import { isAgySubagentTool } from "./subagents.ts";
|
|
79
|
+
import { neutralizeAgyInterceptedSlashCommands } from "./prompt.ts";
|
|
76
80
|
import {
|
|
77
81
|
agyHasRunningToolProcess,
|
|
78
82
|
agyProbeSubagents,
|
|
@@ -89,8 +93,8 @@ const LIFECYCLE_LIMIT = 24;
|
|
|
89
93
|
const CANCEL_GRACE_MS = 1_500;
|
|
90
94
|
const TERM_CLOSE_MS = 1_000;
|
|
91
95
|
const MAX_OPEN_SESSIONS = 8;
|
|
92
|
-
const MAX_MODEL_LIVENESS_GRACES =
|
|
93
|
-
const MAX_UPSTREAM_STREAM_GRACES =
|
|
96
|
+
const MAX_MODEL_LIVENESS_GRACES = 6;
|
|
97
|
+
const MAX_UPSTREAM_STREAM_GRACES = 6;
|
|
94
98
|
const MAX_TOOL_LIVENESS_GRACES = 12;
|
|
95
99
|
const PARKED_POLL_MS = 5_000;
|
|
96
100
|
|
|
@@ -127,7 +131,8 @@ interface ActiveAcpTurn {
|
|
|
127
131
|
lastSubagentUpdateMs?: number;
|
|
128
132
|
subagentSpawned?: boolean;
|
|
129
133
|
aborted: boolean;
|
|
130
|
-
|
|
134
|
+
promptInFlight?: boolean;
|
|
135
|
+
stallError?: AgyStallError | AgySpawnError;
|
|
131
136
|
cancelTimer?: NodeJS.Timeout;
|
|
132
137
|
abortHandler?: () => void;
|
|
133
138
|
settleError?: (err: unknown) => void;
|
|
@@ -166,6 +171,7 @@ export class AgyAcpDriverSession implements AgyTurnExecutor {
|
|
|
166
171
|
#config: AgyProcessConfigSnapshot | undefined;
|
|
167
172
|
#openSessions = new Map<string, OpenAcpSessionMeta>();
|
|
168
173
|
#lastConversationId: string | undefined;
|
|
174
|
+
#rawStderrTail = "";
|
|
169
175
|
#stderrTail = "";
|
|
170
176
|
#active: ActiveAcpTurn | undefined;
|
|
171
177
|
#queue: Promise<void> = Promise.resolve();
|
|
@@ -394,6 +400,18 @@ export class AgyAcpDriverSession implements AgyTurnExecutor {
|
|
|
394
400
|
bridgeRevision: request.bridgeRevision,
|
|
395
401
|
};
|
|
396
402
|
|
|
403
|
+
if (
|
|
404
|
+
this.#child &&
|
|
405
|
+
(!this.#client ||
|
|
406
|
+
this.#client.closed ||
|
|
407
|
+
this.#client.hasPendingRpc() ||
|
|
408
|
+
this.#child.exitCode !== null ||
|
|
409
|
+
this.#child.signalCode !== null)
|
|
410
|
+
) {
|
|
411
|
+
this.#log("recycle:closed-connection");
|
|
412
|
+
await this.#killProcess("idle");
|
|
413
|
+
}
|
|
414
|
+
|
|
397
415
|
const recycleCause = this.#processRecycleCause(desiredConfig);
|
|
398
416
|
if (this.#child && recycleCause) {
|
|
399
417
|
this.#log(`recycle:${recycleCause}`);
|
|
@@ -489,6 +507,7 @@ export class AgyAcpDriverSession implements AgyTurnExecutor {
|
|
|
489
507
|
onQuota: (quota) => {
|
|
490
508
|
this.#lastQuota = quota;
|
|
491
509
|
},
|
|
510
|
+
getStderrTail: () => this.#stderrTail,
|
|
492
511
|
},
|
|
493
512
|
);
|
|
494
513
|
this.#consecutiveStalls = 0;
|
|
@@ -553,6 +572,7 @@ export class AgyAcpDriverSession implements AgyTurnExecutor {
|
|
|
553
572
|
this.#child = child;
|
|
554
573
|
this.#config = config;
|
|
555
574
|
this.#openSessions.clear();
|
|
575
|
+
this.#rawStderrTail = "";
|
|
556
576
|
this.#stderrTail = "";
|
|
557
577
|
this.#spawnCount += 1;
|
|
558
578
|
this.#currentProcessTurns = 0;
|
|
@@ -582,16 +602,15 @@ export class AgyAcpDriverSession implements AgyTurnExecutor {
|
|
|
582
602
|
turn?.settleError?.(transportErr);
|
|
583
603
|
});
|
|
584
604
|
},
|
|
605
|
+
getStderrSummary: () => extractAgyStderrSummary(this.#stderrTail),
|
|
585
606
|
});
|
|
586
607
|
this.#client = client;
|
|
587
608
|
|
|
588
609
|
child.stderr.setEncoding("utf8");
|
|
589
610
|
child.stderr.on("data", (chunk: string) => {
|
|
590
611
|
if (generation !== this.#generation) return;
|
|
591
|
-
this.#
|
|
592
|
-
|
|
593
|
-
this.#stderrTail = this.#stderrTail.slice(-STDERR_LIMIT);
|
|
594
|
-
}
|
|
612
|
+
this.#rawStderrTail = (this.#rawStderrTail + chunk).slice(-STDERR_LIMIT);
|
|
613
|
+
this.#stderrTail = redactAgySecrets(this.#rawStderrTail);
|
|
595
614
|
if (this.#active && this.#active.generation === generation) {
|
|
596
615
|
this.#rearmStallTimer(this.#active);
|
|
597
616
|
}
|
|
@@ -746,10 +765,30 @@ export class AgyAcpDriverSession implements AgyTurnExecutor {
|
|
|
746
765
|
() => {
|
|
747
766
|
if (this.#active !== turn) return;
|
|
748
767
|
const secs = Math.round(renewMs / 1000);
|
|
749
|
-
|
|
750
|
-
|
|
768
|
+
const detail = extractAgyStderrSummary(this.#stderrTail);
|
|
769
|
+
const timeoutErr = new AgySpawnError(
|
|
770
|
+
`agy timed out after ${secs}s${detail ? `: ${detail}` : ""}`,
|
|
771
|
+
this.#stderrTail,
|
|
772
|
+
);
|
|
773
|
+
if (!turn.promptInFlight || !turn.sessionId || !this.#client) {
|
|
774
|
+
this.#recordRecycle("unspecified");
|
|
775
|
+
void this.#killProcess(this.#shutdown ? "dead" : "idle", timeoutErr).finally(() => {
|
|
776
|
+
turn.settleError?.(timeoutErr);
|
|
777
|
+
});
|
|
778
|
+
return;
|
|
779
|
+
}
|
|
780
|
+
turn.stallError = timeoutErr;
|
|
781
|
+
this.#client.cancel(turn.sessionId);
|
|
782
|
+
if (!turn.cancelTimer) {
|
|
783
|
+
turn.cancelTimer = setTimeout(() => {
|
|
784
|
+
if (this.#active !== turn) return;
|
|
785
|
+
this.#recordRecycle("unspecified");
|
|
786
|
+
void this.#killProcess(this.#shutdown ? "dead" : "idle", timeoutErr).finally(() => {
|
|
787
|
+
turn.settleError?.(timeoutErr);
|
|
788
|
+
});
|
|
789
|
+
}, this.#cancelGraceMs);
|
|
790
|
+
turn.cancelTimer.unref?.();
|
|
751
791
|
}
|
|
752
|
-
turn.settleError?.(new AgySpawnError(`agy timed out after ${secs}s`, this.#stderrTail));
|
|
753
792
|
},
|
|
754
793
|
renewMs,
|
|
755
794
|
);
|
|
@@ -1074,6 +1113,7 @@ async function awaitTurnExecution(
|
|
|
1074
1113
|
onForceKillOnCancelTimeout: () => Promise<void>;
|
|
1075
1114
|
onSessionState: (state: AcpSessionState) => void;
|
|
1076
1115
|
onQuota: (quota: AcpQuotaSnapshot) => void;
|
|
1116
|
+
getStderrTail: () => string;
|
|
1077
1117
|
},
|
|
1078
1118
|
): Promise<AgyTurnOutcome> {
|
|
1079
1119
|
const { request, outcome, normalizer, thoughtTracker } = turn;
|
|
@@ -1097,19 +1137,64 @@ async function awaitTurnExecution(
|
|
|
1097
1137
|
const mcpServers = readAcpMcpServersFromDirectory(request.mcpDirectory);
|
|
1098
1138
|
const mcpFingerprint = JSON.stringify(mcpServers);
|
|
1099
1139
|
|
|
1100
|
-
await Promise.race([client.initialize(request.signal), interrupted]);
|
|
1101
|
-
|
|
1102
|
-
const useNativeSystemPrompt = client.supportsSystemPromptAppend();
|
|
1103
|
-
const desiredSystemPrompt = useNativeSystemPrompt ? (request.systemPrompt ?? "") : "";
|
|
1104
|
-
|
|
1105
1140
|
let sessionId = request.conversationId;
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1141
|
+
try {
|
|
1142
|
+
await Promise.race([client.initialize(request.signal), interrupted]);
|
|
1143
|
+
|
|
1144
|
+
const useNativeSystemPromptSetup = client.supportsSystemPromptAppend();
|
|
1145
|
+
const desiredSystemPrompt = useNativeSystemPromptSetup ? (request.systemPrompt ?? "") : "";
|
|
1146
|
+
|
|
1147
|
+
if (sessionId) {
|
|
1148
|
+
const existing = hooks.openSessions.get(sessionId);
|
|
1149
|
+
if (!existing) {
|
|
1150
|
+
const resumedState = await Promise.race([
|
|
1151
|
+
client.resumeSession(
|
|
1152
|
+
{
|
|
1153
|
+
sessionId,
|
|
1154
|
+
cwd,
|
|
1155
|
+
additionalDirectories,
|
|
1156
|
+
mcpServers,
|
|
1157
|
+
...(desiredSystemPrompt ? { systemPromptAppend: desiredSystemPrompt } : {}),
|
|
1158
|
+
},
|
|
1159
|
+
request.signal,
|
|
1160
|
+
),
|
|
1161
|
+
interrupted,
|
|
1162
|
+
]);
|
|
1163
|
+
hooks.onSessionState(resumedState);
|
|
1164
|
+
rememberOpenSession(hooks.openSessions, client, sessionId, {
|
|
1165
|
+
systemPrompt: desiredSystemPrompt,
|
|
1166
|
+
mcpFingerprint,
|
|
1167
|
+
});
|
|
1168
|
+
} else if (
|
|
1169
|
+
existing.systemPrompt !== desiredSystemPrompt ||
|
|
1170
|
+
existing.mcpFingerprint !== mcpFingerprint
|
|
1171
|
+
) {
|
|
1172
|
+
// Refresh in-memory ACP session handle when system instructions or MCP servers
|
|
1173
|
+
// changed while preserving `<sessionId>.db` on disk.
|
|
1174
|
+
await Promise.race([client.closeSession(sessionId, request.signal), interrupted]);
|
|
1175
|
+
const resumedState = await Promise.race([
|
|
1176
|
+
client.resumeSession(
|
|
1177
|
+
{
|
|
1178
|
+
sessionId,
|
|
1179
|
+
cwd,
|
|
1180
|
+
additionalDirectories,
|
|
1181
|
+
mcpServers,
|
|
1182
|
+
...(desiredSystemPrompt ? { systemPromptAppend: desiredSystemPrompt } : {}),
|
|
1183
|
+
},
|
|
1184
|
+
request.signal,
|
|
1185
|
+
),
|
|
1186
|
+
interrupted,
|
|
1187
|
+
]);
|
|
1188
|
+
hooks.onSessionState(resumedState);
|
|
1189
|
+
rememberOpenSession(hooks.openSessions, client, sessionId, {
|
|
1190
|
+
systemPrompt: desiredSystemPrompt,
|
|
1191
|
+
mcpFingerprint,
|
|
1192
|
+
});
|
|
1193
|
+
}
|
|
1194
|
+
} else {
|
|
1195
|
+
const created = await Promise.race([
|
|
1196
|
+
client.newSession(
|
|
1111
1197
|
{
|
|
1112
|
-
sessionId,
|
|
1113
1198
|
cwd,
|
|
1114
1199
|
additionalDirectories,
|
|
1115
1200
|
mcpServers,
|
|
@@ -1119,78 +1204,50 @@ async function awaitTurnExecution(
|
|
|
1119
1204
|
),
|
|
1120
1205
|
interrupted,
|
|
1121
1206
|
]);
|
|
1122
|
-
|
|
1207
|
+
sessionId = created.sessionId;
|
|
1208
|
+
hooks.onSessionState(created);
|
|
1123
1209
|
rememberOpenSession(hooks.openSessions, client, sessionId, {
|
|
1124
1210
|
systemPrompt: desiredSystemPrompt,
|
|
1125
1211
|
mcpFingerprint,
|
|
1126
1212
|
});
|
|
1127
|
-
}
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1215
|
+
turn.sessionId = sessionId;
|
|
1216
|
+
outcome.conversationId = sessionId;
|
|
1217
|
+
thoughtTracker.setConversationId(sessionId);
|
|
1218
|
+
hooks.onConversation(sessionId);
|
|
1219
|
+
hooks.onQueueOrphanScan(sessionId);
|
|
1220
|
+
|
|
1221
|
+
try {
|
|
1222
|
+
await Promise.race([
|
|
1223
|
+
client.configureSession(
|
|
1224
|
+
sessionId,
|
|
1136
1225
|
{
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
...(desiredSystemPrompt ? { systemPromptAppend: desiredSystemPrompt } : {}),
|
|
1226
|
+
model: request.model,
|
|
1227
|
+
effort: request.effort,
|
|
1228
|
+
mode: request.mode,
|
|
1229
|
+
readOnlyPermissions: request.readOnlyPermissions,
|
|
1142
1230
|
},
|
|
1143
1231
|
request.signal,
|
|
1144
1232
|
),
|
|
1145
1233
|
interrupted,
|
|
1146
1234
|
]);
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
mcpFingerprint,
|
|
1151
|
-
});
|
|
1235
|
+
} catch (configureErr) {
|
|
1236
|
+
hooks.openSessions.delete(sessionId);
|
|
1237
|
+
throw configureErr;
|
|
1152
1238
|
}
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
request.signal,
|
|
1163
|
-
),
|
|
1164
|
-
interrupted,
|
|
1165
|
-
]);
|
|
1166
|
-
sessionId = created.sessionId;
|
|
1167
|
-
hooks.onSessionState(created);
|
|
1168
|
-
rememberOpenSession(hooks.openSessions, client, sessionId, {
|
|
1169
|
-
systemPrompt: desiredSystemPrompt,
|
|
1170
|
-
mcpFingerprint,
|
|
1171
|
-
});
|
|
1239
|
+
hooks.onSessionState(client.getSessionState(sessionId));
|
|
1240
|
+
} catch (setupErr) {
|
|
1241
|
+
if (setupErr instanceof AgyAcpTimeoutError || client.closed) {
|
|
1242
|
+
await hooks.onForceKillOnCancelTimeout().catch(() => undefined);
|
|
1243
|
+
}
|
|
1244
|
+
if (setupErr instanceof AgyAcpTimeoutError) {
|
|
1245
|
+
throw new AgySpawnError(setupErr.message, hooks.getStderrTail());
|
|
1246
|
+
}
|
|
1247
|
+
throw setupErr;
|
|
1172
1248
|
}
|
|
1173
1249
|
|
|
1174
|
-
|
|
1175
|
-
outcome.conversationId = sessionId;
|
|
1176
|
-
thoughtTracker.setConversationId(sessionId);
|
|
1177
|
-
hooks.onConversation(sessionId);
|
|
1178
|
-
hooks.onQueueOrphanScan(sessionId);
|
|
1179
|
-
|
|
1180
|
-
await Promise.race([
|
|
1181
|
-
client.configureSession(
|
|
1182
|
-
sessionId,
|
|
1183
|
-
{
|
|
1184
|
-
model: request.model,
|
|
1185
|
-
effort: request.effort,
|
|
1186
|
-
mode: request.mode,
|
|
1187
|
-
readOnlyPermissions: request.readOnlyPermissions,
|
|
1188
|
-
},
|
|
1189
|
-
request.signal,
|
|
1190
|
-
),
|
|
1191
|
-
interrupted,
|
|
1192
|
-
]);
|
|
1193
|
-
hooks.onSessionState(client.getSessionState(sessionId));
|
|
1250
|
+
const useNativeSystemPrompt = client.supportsSystemPromptAppend();
|
|
1194
1251
|
|
|
1195
1252
|
const promptText =
|
|
1196
1253
|
!useNativeSystemPrompt && request.systemPromptRelay
|
|
@@ -1206,7 +1263,7 @@ async function awaitTurnExecution(
|
|
|
1206
1263
|
}
|
|
1207
1264
|
}
|
|
1208
1265
|
if (promptText || blocks.length === 0) {
|
|
1209
|
-
blocks.push({ type: "text", text: promptText });
|
|
1266
|
+
blocks.push({ type: "text", text: neutralizeAgyInterceptedSlashCommands(promptText) });
|
|
1210
1267
|
}
|
|
1211
1268
|
|
|
1212
1269
|
let resolveAbortedOutcome: ((abortedOutcome: AgyTurnOutcome) => void) | undefined;
|
|
@@ -1238,6 +1295,7 @@ async function awaitTurnExecution(
|
|
|
1238
1295
|
}
|
|
1239
1296
|
|
|
1240
1297
|
let promptResponse;
|
|
1298
|
+
turn.promptInFlight = true;
|
|
1241
1299
|
try {
|
|
1242
1300
|
const winner = await Promise.race([
|
|
1243
1301
|
client.prompt(sessionId, blocks).then((res) => ({ kind: "prompt" as const, res })),
|
|
@@ -1259,9 +1317,11 @@ async function awaitTurnExecution(
|
|
|
1259
1317
|
throw turn.stallError;
|
|
1260
1318
|
}
|
|
1261
1319
|
if (err instanceof AgyAcpRpcError) {
|
|
1262
|
-
throw new AgySpawnError(err.message,
|
|
1320
|
+
throw new AgySpawnError(err.message, hooks.getStderrTail());
|
|
1263
1321
|
}
|
|
1264
1322
|
throw err;
|
|
1323
|
+
} finally {
|
|
1324
|
+
turn.promptInFlight = false;
|
|
1265
1325
|
}
|
|
1266
1326
|
|
|
1267
1327
|
if (turn.cancelTimer) {
|
|
@@ -1302,6 +1362,8 @@ async function awaitTurnExecution(
|
|
|
1302
1362
|
const normalizedUsage = normalizeAcpTurnUsageForStepCount(
|
|
1303
1363
|
turnUsage,
|
|
1304
1364
|
normalizer.estimatedModelCallCount(),
|
|
1365
|
+
normalizer.lastContextUsed(),
|
|
1366
|
+
normalizer.usageInferenceCount(),
|
|
1305
1367
|
);
|
|
1306
1368
|
outcome.usage = normalizedUsage;
|
|
1307
1369
|
const usageActivity: AgyActivity = { type: "usage", usage: normalizedUsage };
|
|
@@ -1317,7 +1379,14 @@ async function awaitTurnExecution(
|
|
|
1317
1379
|
finalText,
|
|
1318
1380
|
thoughtTracker.lastApiError,
|
|
1319
1381
|
);
|
|
1320
|
-
const
|
|
1382
|
+
const plainQuotaFailure = detectAcpPlainTextQuotaExhaustion(
|
|
1383
|
+
stopReason,
|
|
1384
|
+
finalText,
|
|
1385
|
+
outcome.usage,
|
|
1386
|
+
quota,
|
|
1387
|
+
);
|
|
1388
|
+
const turnFailure = emptyTurnFailure ?? plainQuotaFailure;
|
|
1389
|
+
const isOk = stopReason === "end_turn" && !turnFailure;
|
|
1321
1390
|
const finishedReason: AgyFinishedReason | undefined = isOk
|
|
1322
1391
|
? undefined
|
|
1323
1392
|
: stopReason === "cancelled"
|
|
@@ -1334,10 +1403,10 @@ async function awaitTurnExecution(
|
|
|
1334
1403
|
outcome.finished = true;
|
|
1335
1404
|
outcome.status = isOk ? "OK" : "ERROR";
|
|
1336
1405
|
outcome.finishedReason = finishedReason;
|
|
1337
|
-
outcome.response = finalText;
|
|
1406
|
+
outcome.response = plainQuotaFailure ? "" : finalText;
|
|
1338
1407
|
outcome.error = isOk
|
|
1339
1408
|
? undefined
|
|
1340
|
-
: (
|
|
1409
|
+
: (turnFailure ?? formatAcpStopError(stopReason, fallbackErr, finalText, quota));
|
|
1341
1410
|
|
|
1342
1411
|
const resultActivity: AgyActivity = {
|
|
1343
1412
|
type: "result",
|
package/lib/agy-client.ts
CHANGED
|
@@ -264,12 +264,25 @@ export const READ_ONLY_AGY_SETTINGS_JSON = JSON.stringify({
|
|
|
264
264
|
permissions: { allow: [] },
|
|
265
265
|
});
|
|
266
266
|
|
|
267
|
+
/** Scrub OAuth access tokens, Google API keys, and bridge tokens from surfaced stderr text. */
|
|
268
|
+
export function redactAgySecrets(text: string): string {
|
|
269
|
+
if (!text) return text;
|
|
270
|
+
return text
|
|
271
|
+
.replace(/\bya29\.[A-Za-z0-9._-]+/g, "ya29.<redacted>")
|
|
272
|
+
.replace(/\bAIza[A-Za-z0-9_-]{16,}/g, "AIza<redacted>")
|
|
273
|
+
.replace(
|
|
274
|
+
/\b(access_token|refresh_token|api_key|apikey|x-pi-bridge-token)(\s*[:=]\s*)([^\s"',;&]+)/gi,
|
|
275
|
+
"$1$2<redacted>",
|
|
276
|
+
)
|
|
277
|
+
.replace(/\b(Bearer\s+)[A-Za-z0-9._~+/=-]{12,}/gi, "$1<redacted>");
|
|
278
|
+
}
|
|
279
|
+
|
|
267
280
|
/**
|
|
268
281
|
* Extract a concise, structured failure reason from agy stderr when the process
|
|
269
282
|
* exits early (e.g. exit code 3 with `AGY_ERROR: {...}` or `[agy] print timeout after ...`).
|
|
270
283
|
*/
|
|
271
284
|
export function extractAgyStderrSummary(stderr: string): string {
|
|
272
|
-
const trimmed = stderr.trim();
|
|
285
|
+
const trimmed = redactAgySecrets(stderr).trim();
|
|
273
286
|
if (!trimmed) return "";
|
|
274
287
|
const lines = trimmed
|
|
275
288
|
.split(/\r?\n/)
|
|
@@ -533,9 +546,14 @@ async function runSingleAgyTurn(request: AgyTurnRequest): Promise<AgyTurnOutcome
|
|
|
533
546
|
return;
|
|
534
547
|
}
|
|
535
548
|
const likelyCompacting = thoughtTracker.isLikelyCompacting(activeTools.size > 0);
|
|
549
|
+
const awaitingUpstream =
|
|
550
|
+
activeTools.size === 0 && thoughtTracker.isAwaitingUpstreamStream;
|
|
536
551
|
if (
|
|
537
|
-
(thoughtTracker.isGenerating ||
|
|
538
|
-
|
|
552
|
+
(thoughtTracker.isGenerating ||
|
|
553
|
+
likelyCompacting ||
|
|
554
|
+
thoughtTracker.isRetrying ||
|
|
555
|
+
awaitingUpstream) &&
|
|
556
|
+
stallGraces < 6
|
|
539
557
|
) {
|
|
540
558
|
stallGraces += 1;
|
|
541
559
|
armStall();
|
package/lib/agy-driver.ts
CHANGED
|
@@ -184,8 +184,8 @@ const TERM_CLOSE_MS = 500;
|
|
|
184
184
|
*/
|
|
185
185
|
const STALL_PARKED_POLL_MS = 10_000;
|
|
186
186
|
const STALL_PARKED_LIMIT = 2;
|
|
187
|
-
const MAX_MODEL_LIVENESS_GRACES =
|
|
188
|
-
const MAX_UPSTREAM_STREAM_GRACES =
|
|
187
|
+
const MAX_MODEL_LIVENESS_GRACES = 6;
|
|
188
|
+
const MAX_UPSTREAM_STREAM_GRACES = 6;
|
|
189
189
|
/**
|
|
190
190
|
* While a tool step is ACTIVE, poll the off-stream transcript this often for a
|
|
191
191
|
* final answer agy is withholding. agy emits nothing on stdout for the whole
|
package/lib/agy-thoughts.ts
CHANGED
|
@@ -43,6 +43,13 @@ const MIN_COMPACTION_CONTEXT_TOKENS = 100_000;
|
|
|
43
43
|
/** Default total attempts in agy's internal model retry loop (`ModelRequestStatus.max_attempts = 10`). */
|
|
44
44
|
export const AGY_DEFAULT_MAX_RETRY_ATTEMPTS = 10;
|
|
45
45
|
|
|
46
|
+
/**
|
|
47
|
+
* Maximum silent TTFB / prefill window (`5m30s`) while awaiting upstream HTTP
|
|
48
|
+
* headers or the first `PLANNER_RESPONSE` step — matches `agy`'s own `5m0s`
|
|
49
|
+
* stream idle timeout plus a 30s buffer so internal `run.go` retries fire first.
|
|
50
|
+
*/
|
|
51
|
+
export const UPSTREAM_STREAM_WAIT_WINDOW_MS = 330_000;
|
|
52
|
+
|
|
46
53
|
export function resolveAgyRetryLabel(apiError?: string): string {
|
|
47
54
|
if (!apiError) return "Model unavailable";
|
|
48
55
|
if (
|
|
@@ -683,6 +690,10 @@ export class AgyThoughtTracker {
|
|
|
683
690
|
#lastCliLogScanAtMs = 0;
|
|
684
691
|
#pendingMonitorCalls = 0;
|
|
685
692
|
#awaitingUpstreamHeadersSinceMs: number | undefined;
|
|
693
|
+
#awaitingPlannerSinceMs: number | undefined;
|
|
694
|
+
#plannerWaitStepIdx = -1;
|
|
695
|
+
#exitedAtStepIdx = -1;
|
|
696
|
+
#sawStreamExit = false;
|
|
686
697
|
#activeStreamStartedAtMs: number | undefined;
|
|
687
698
|
#activeStreamResponseId: string | undefined;
|
|
688
699
|
#streamStartMaxIdx = -1;
|
|
@@ -753,6 +764,10 @@ export class AgyThoughtTracker {
|
|
|
753
764
|
this.#lastCliLogScanAtMs = 0;
|
|
754
765
|
this.#pendingMonitorCalls = 0;
|
|
755
766
|
this.#awaitingUpstreamHeadersSinceMs = undefined;
|
|
767
|
+
this.#awaitingPlannerSinceMs = undefined;
|
|
768
|
+
this.#plannerWaitStepIdx = -1;
|
|
769
|
+
this.#exitedAtStepIdx = -1;
|
|
770
|
+
this.#sawStreamExit = false;
|
|
756
771
|
this.#activeStreamStartedAtMs = undefined;
|
|
757
772
|
this.#activeStreamResponseId = undefined;
|
|
758
773
|
this.#streamStartMaxIdx = -1;
|
|
@@ -979,13 +994,31 @@ export class AgyThoughtTracker {
|
|
|
979
994
|
nowMs,
|
|
980
995
|
);
|
|
981
996
|
const isCompacting = sqlCheckpointActive || cliCompacting;
|
|
997
|
+
if (this.#sawStreamExit) {
|
|
998
|
+
this.#exitedAtStepIdx = overallMaxIdx;
|
|
999
|
+
this.#sawStreamExit = false;
|
|
1000
|
+
}
|
|
982
1001
|
if (hasRunningStep || isGenerating) {
|
|
983
1002
|
this.#awaitingUpstreamHeadersSinceMs = undefined;
|
|
1003
|
+
this.#awaitingPlannerSinceMs = undefined;
|
|
1004
|
+
this.#plannerWaitStepIdx = -1;
|
|
984
1005
|
this.#retryUntilMs = 0;
|
|
985
1006
|
this.#retryStreak = 0;
|
|
986
1007
|
this.#activeRetryInfo = undefined;
|
|
987
1008
|
this.#lastApiError = undefined;
|
|
988
1009
|
this.#lastApiErrorRetryEndsAtMs = 0;
|
|
1010
|
+
} else if (
|
|
1011
|
+
awaitingPlannerAfterStep &&
|
|
1012
|
+
overallMaxIdx >= this.#minStepIndex &&
|
|
1013
|
+
overallMaxIdx > this.#exitedAtStepIdx
|
|
1014
|
+
) {
|
|
1015
|
+
if (this.#plannerWaitStepIdx !== overallMaxIdx || this.#awaitingPlannerSinceMs === undefined) {
|
|
1016
|
+
this.#plannerWaitStepIdx = overallMaxIdx;
|
|
1017
|
+
this.#awaitingPlannerSinceMs = nowMs;
|
|
1018
|
+
}
|
|
1019
|
+
} else {
|
|
1020
|
+
this.#awaitingPlannerSinceMs = undefined;
|
|
1021
|
+
this.#plannerWaitStepIdx = -1;
|
|
989
1022
|
}
|
|
990
1023
|
const retryAttempted = this.#sawRetrySinceLastPoll;
|
|
991
1024
|
this.#sawRetrySinceLastPoll = false;
|
|
@@ -1050,20 +1083,27 @@ export class AgyThoughtTracker {
|
|
|
1050
1083
|
}
|
|
1051
1084
|
|
|
1052
1085
|
/**
|
|
1053
|
-
* True when `acp-server-*.log`
|
|
1054
|
-
*
|
|
1086
|
+
* True when `cli-*.log` / `acp-server-*.log` or SQLite step state shows an
|
|
1087
|
+
* upstream request or post-step planner call still awaiting its first SQLite
|
|
1088
|
+
* planner step (TTFB / high-effort prefill, up to `agy`'s `5m0s` stream timeout).
|
|
1055
1089
|
*/
|
|
1056
1090
|
get isAwaitingUpstreamStream(): boolean {
|
|
1057
1091
|
const nowMs = this.#now();
|
|
1058
1092
|
if (
|
|
1059
1093
|
this.#activeStreamStartedAtMs !== undefined &&
|
|
1060
|
-
nowMs - this.#activeStreamStartedAtMs <
|
|
1094
|
+
nowMs - this.#activeStreamStartedAtMs < UPSTREAM_STREAM_WAIT_WINDOW_MS
|
|
1061
1095
|
) {
|
|
1062
1096
|
return true;
|
|
1063
1097
|
}
|
|
1064
1098
|
if (
|
|
1065
1099
|
this.#awaitingUpstreamHeadersSinceMs !== undefined &&
|
|
1066
|
-
nowMs - this.#awaitingUpstreamHeadersSinceMs <
|
|
1100
|
+
nowMs - this.#awaitingUpstreamHeadersSinceMs < UPSTREAM_STREAM_WAIT_WINDOW_MS
|
|
1101
|
+
) {
|
|
1102
|
+
return true;
|
|
1103
|
+
}
|
|
1104
|
+
if (
|
|
1105
|
+
this.#awaitingPlannerSinceMs !== undefined &&
|
|
1106
|
+
nowMs - this.#awaitingPlannerSinceMs < UPSTREAM_STREAM_WAIT_WINDOW_MS
|
|
1067
1107
|
) {
|
|
1068
1108
|
return true;
|
|
1069
1109
|
}
|
|
@@ -1215,6 +1255,8 @@ export class AgyThoughtTracker {
|
|
|
1215
1255
|
this.#lastApiError = undefined;
|
|
1216
1256
|
this.#lastApiErrorRetryEndsAtMs = 0;
|
|
1217
1257
|
this.#awaitingUpstreamHeadersSinceMs = this.#now();
|
|
1258
|
+
this.#exitedAtStepIdx = -1;
|
|
1259
|
+
this.#sawStreamExit = false;
|
|
1218
1260
|
return true;
|
|
1219
1261
|
}
|
|
1220
1262
|
if (
|
|
@@ -1226,6 +1268,8 @@ export class AgyThoughtTracker {
|
|
|
1226
1268
|
this.#lastApiError = undefined;
|
|
1227
1269
|
this.#lastApiErrorRetryEndsAtMs = 0;
|
|
1228
1270
|
this.#awaitingUpstreamHeadersSinceMs = undefined;
|
|
1271
|
+
this.#awaitingPlannerSinceMs = undefined;
|
|
1272
|
+
this.#plannerWaitStepIdx = -1;
|
|
1229
1273
|
}
|
|
1230
1274
|
if (activity.type === "checkpoint") {
|
|
1231
1275
|
this.#lastApiError = undefined;
|
|
@@ -1398,7 +1442,7 @@ export class AgyThoughtTracker {
|
|
|
1398
1442
|
} catch {}
|
|
1399
1443
|
}
|
|
1400
1444
|
candidates.sort((a, b) => b.mtimeMs - a.mtimeMs);
|
|
1401
|
-
for (const candidate of candidates.slice(0,
|
|
1445
|
+
for (const candidate of candidates.slice(0, 16)) {
|
|
1402
1446
|
const tailBytes = Math.min(candidate.size, 131_072);
|
|
1403
1447
|
const startOffset = candidate.size - tailBytes;
|
|
1404
1448
|
const buf = readFileSlice(candidate.path, startOffset, tailBytes);
|
|
@@ -1500,6 +1544,15 @@ export class AgyThoughtTracker {
|
|
|
1500
1544
|
const parsedMs = parseGlogTimestampMs(line, nowMs);
|
|
1501
1545
|
this.#pendingMonitorCalls = 0;
|
|
1502
1546
|
this.#awaitingUpstreamHeadersSinceMs = exited ? undefined : (parsedMs ?? nowMs);
|
|
1547
|
+
if (exited) {
|
|
1548
|
+
this.#awaitingPlannerSinceMs = undefined;
|
|
1549
|
+
this.#plannerWaitStepIdx = -1;
|
|
1550
|
+
this.#exitedAtStepIdx = overallMaxIdx;
|
|
1551
|
+
this.#sawStreamExit = true;
|
|
1552
|
+
} else {
|
|
1553
|
+
this.#exitedAtStepIdx = -1;
|
|
1554
|
+
this.#sawStreamExit = false;
|
|
1555
|
+
}
|
|
1503
1556
|
this.#activeStreamStartedAtMs = undefined;
|
|
1504
1557
|
this.#activeStreamResponseId = undefined;
|
|
1505
1558
|
this.#streamStartMaxIdx = -1;
|
|
@@ -1516,6 +1569,7 @@ export class AgyThoughtTracker {
|
|
|
1516
1569
|
);
|
|
1517
1570
|
if (retryMatch) {
|
|
1518
1571
|
this.#awaitingUpstreamHeadersSinceMs = undefined;
|
|
1572
|
+
this.#awaitingPlannerSinceMs = undefined;
|
|
1519
1573
|
this.#activeStreamStartedAtMs = undefined;
|
|
1520
1574
|
this.#activeStreamResponseId = undefined;
|
|
1521
1575
|
this.#streamStartMaxIdx = -1;
|
|
@@ -1577,7 +1631,11 @@ export class AgyThoughtTracker {
|
|
|
1577
1631
|
continue;
|
|
1578
1632
|
}
|
|
1579
1633
|
const parsedMs = isInitialReplay ? parseGlogTimestampMs(line, nowMs) : undefined;
|
|
1580
|
-
if (
|
|
1634
|
+
if (
|
|
1635
|
+
isInitialReplay &&
|
|
1636
|
+
parsedMs !== undefined &&
|
|
1637
|
+
nowMs - parsedMs > UPSTREAM_STREAM_WAIT_WINDOW_MS
|
|
1638
|
+
) {
|
|
1581
1639
|
continue;
|
|
1582
1640
|
}
|
|
1583
1641
|
// A response arrived, so any earlier API error was recovered by agy's own retry.
|