@opengeni/worker-bundle 0.2.3 → 0.3.0
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 +470 -220
- package/dist/index.js.map +1 -1
- package/package.json +8 -8
- package/src/activities/agent-turn.ts +470 -231
- package/src/activities/capabilities.ts +42 -0
- package/src/activities/context-compaction.ts +51 -58
- package/src/activities/run-input.ts +2 -2
package/dist/index.js
CHANGED
|
@@ -44,7 +44,7 @@ import {
|
|
|
44
44
|
consumeSessionCompactionRequest,
|
|
45
45
|
countSessionHistoryItems,
|
|
46
46
|
getActiveSessionHistoryItems as getActiveSessionHistoryItems3,
|
|
47
|
-
nextSessionHistoryPosition,
|
|
47
|
+
nextSessionHistoryPosition as nextSessionHistoryPosition2,
|
|
48
48
|
requeuePreemptedTurn,
|
|
49
49
|
saveRunState,
|
|
50
50
|
upsertSandboxSessionEnvelope,
|
|
@@ -62,8 +62,10 @@ import {
|
|
|
62
62
|
maxTurnsExceededRunState,
|
|
63
63
|
modelResponseUsageFromSdkEvent,
|
|
64
64
|
normalizeSdkEvent,
|
|
65
|
-
sanitizeHistoryItemsForModel,
|
|
66
|
-
summarizeForCompaction as summarizeForCompaction2
|
|
65
|
+
sanitizeHistoryItemsForModel as sanitizeHistoryItemsForModel2,
|
|
66
|
+
summarizeForCompaction as summarizeForCompaction2,
|
|
67
|
+
findCompactionNeededError,
|
|
68
|
+
SUMMARY_BUFFER_TOKENS as SUMMARY_BUFFER_TOKENS2
|
|
67
69
|
} from "@opengeni/runtime";
|
|
68
70
|
import { calculateModelUsageCostMicros, configuredModelPricing, configuredStaticUsageLimits as configuredStaticUsageLimits2, sandboxWarmRateMicrosPerSecond } from "@opengeni/config";
|
|
69
71
|
import { CancelledFailure } from "@temporalio/activity";
|
|
@@ -83,12 +85,46 @@ import {
|
|
|
83
85
|
import {
|
|
84
86
|
decryptedCapabilityHeaders,
|
|
85
87
|
listEnabledMcpCapabilityServers,
|
|
88
|
+
listSessionMcpServerMetadata,
|
|
89
|
+
listSessionMcpServersForRun,
|
|
86
90
|
workspaceCodexSubscriptionActive
|
|
87
91
|
} from "@opengeni/db";
|
|
88
92
|
async function settingsWithEnabledCapabilityMcpServers(db, workspaceId, settings) {
|
|
89
93
|
const enabled = await listEnabledMcpCapabilityServers(db, workspaceId);
|
|
90
94
|
return settingsWithMcpCapabilityServers(settings, enabled);
|
|
91
95
|
}
|
|
96
|
+
async function settingsWithSessionMcpServersForRun(db, workspaceId, sessionId, settings) {
|
|
97
|
+
const encryptionKey = environmentsEncryptionKeyBytes(settings);
|
|
98
|
+
if (!encryptionKey) {
|
|
99
|
+
const metadata = await listSessionMcpServerMetadata(db, workspaceId, sessionId);
|
|
100
|
+
if (metadata.length === 0) {
|
|
101
|
+
return settings;
|
|
102
|
+
}
|
|
103
|
+
throw new Error("session MCP server credentials require OPENGENI_ENVIRONMENTS_ENCRYPTION_KEY");
|
|
104
|
+
}
|
|
105
|
+
return settingsWithSessionMcpServers(settings, await listSessionMcpServersForRun(db, workspaceId, sessionId, encryptionKey));
|
|
106
|
+
}
|
|
107
|
+
function settingsWithSessionMcpServers(settings, servers) {
|
|
108
|
+
if (servers.length === 0) {
|
|
109
|
+
return settings;
|
|
110
|
+
}
|
|
111
|
+
const sessionIds = new Set(servers.map((server) => server.id));
|
|
112
|
+
return {
|
|
113
|
+
...settings,
|
|
114
|
+
mcpServers: [
|
|
115
|
+
...settings.mcpServers.filter((server) => !sessionIds.has(server.id)),
|
|
116
|
+
...servers.map((server) => ({
|
|
117
|
+
id: server.id,
|
|
118
|
+
...server.name ? { name: server.name } : {},
|
|
119
|
+
url: server.url,
|
|
120
|
+
...server.allowedTools ? { allowedTools: server.allowedTools } : {},
|
|
121
|
+
...server.timeoutMs ? { timeoutMs: server.timeoutMs } : {},
|
|
122
|
+
cacheToolsList: server.cacheToolsList ?? false,
|
|
123
|
+
headers: server.headers
|
|
124
|
+
}))
|
|
125
|
+
]
|
|
126
|
+
};
|
|
127
|
+
}
|
|
92
128
|
async function settingsWithCodexCredential(db, workspaceId, settings, activeOverride) {
|
|
93
129
|
const active = activeOverride ?? await workspaceCodexSubscriptionActive(db, settings, workspaceId);
|
|
94
130
|
if (!active) {
|
|
@@ -324,19 +360,19 @@ function workflowIdForSession(sessionId) {
|
|
|
324
360
|
// src/activities/context-compaction.ts
|
|
325
361
|
import {
|
|
326
362
|
applyContextCompaction,
|
|
327
|
-
getActiveSessionHistoryItems
|
|
363
|
+
getActiveSessionHistoryItems,
|
|
364
|
+
nextSessionHistoryPosition
|
|
328
365
|
} from "@opengeni/db";
|
|
329
366
|
import {
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
367
|
+
SUMMARY_BUFFER_TOKENS,
|
|
368
|
+
buildCompactionPromptInput,
|
|
369
|
+
buildCompactionReplacementHistory,
|
|
370
|
+
decideClientCompaction,
|
|
371
|
+
sanitizeHistoryItemsForModel,
|
|
333
372
|
summarizeForCompaction
|
|
334
373
|
} from "@opengeni/runtime";
|
|
335
|
-
import {
|
|
336
|
-
|
|
337
|
-
resolveContextCompactionMode
|
|
338
|
-
} from "@opengeni/config";
|
|
339
|
-
async function maybeCompactContext(db, settings, scope, lastInputTokens, summarize = (s, m) => summarizeForCompaction(s, m, { maxOutputTokens: s.contextSummaryMaxTokens }), options = {}) {
|
|
374
|
+
import { resolveContextCompactionMode } from "@opengeni/config";
|
|
375
|
+
async function maybeCompactContext(db, settings, scope, lastInputTokens, summarize = (s, m) => summarizeForCompaction(s, m, { maxOutputTokens: SUMMARY_BUFFER_TOKENS }), options = {}) {
|
|
340
376
|
if (resolveContextCompactionMode(settings) !== "client") {
|
|
341
377
|
return { compacted: false, reason: "mode_not_client" };
|
|
342
378
|
}
|
|
@@ -344,48 +380,50 @@ async function maybeCompactContext(db, settings, scope, lastInputTokens, summari
|
|
|
344
380
|
if (active.length === 0) {
|
|
345
381
|
return { compacted: false, reason: "no_history" };
|
|
346
382
|
}
|
|
347
|
-
const items = active.map((row) => row.item);
|
|
348
|
-
const
|
|
383
|
+
const items = sanitizeHistoryItemsForModel(active.map((row) => row.item));
|
|
384
|
+
const decision = decideClientCompaction({
|
|
349
385
|
items,
|
|
350
386
|
lastInputTokens,
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
hardFraction: settings.contextCompactHardFraction,
|
|
354
|
-
keepRecentTokens: settings.contextKeepRecentTokens,
|
|
387
|
+
contextWindowTokens: settings.contextWindowTokens,
|
|
388
|
+
contextReservedOutputTokens: settings.contextReservedOutputTokens,
|
|
355
389
|
...options.force ? { force: true } : {}
|
|
356
390
|
});
|
|
357
|
-
if (!
|
|
358
|
-
return { compacted: false, reason:
|
|
359
|
-
}
|
|
360
|
-
if (plan.hardForced) {
|
|
361
|
-
console.warn(
|
|
362
|
-
`context compaction HARD-FORCED for session ${scope.sessionId}: signal ${plan.signalTokens} tokens at/over hard ceiling`
|
|
363
|
-
);
|
|
391
|
+
if (!decision.shouldCompact) {
|
|
392
|
+
return { compacted: false, reason: decision.reason };
|
|
364
393
|
}
|
|
365
|
-
const
|
|
366
|
-
const summaryBody = await summarize(settings,
|
|
394
|
+
const promptInput = buildCompactionPromptInput(items);
|
|
395
|
+
const summaryBody = await summarize(settings, promptInput);
|
|
367
396
|
if (!summaryBody) {
|
|
368
397
|
return { compacted: false, reason: "summarize_failed" };
|
|
369
398
|
}
|
|
370
|
-
const
|
|
371
|
-
|
|
372
|
-
|
|
399
|
+
const replacementHistory = buildCompactionReplacementHistory(items, summaryBody);
|
|
400
|
+
const summaryItem = replacementHistory.at(-1);
|
|
401
|
+
if (!summaryItem) {
|
|
402
|
+
return { compacted: false, reason: "empty_replacement_history" };
|
|
373
403
|
}
|
|
374
|
-
const
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
404
|
+
const nextPosition = await nextSessionHistoryPosition(db, scope.workspaceId, scope.sessionId);
|
|
405
|
+
const replacementItems = replacementHistory.slice(0, -1).map((item, index) => ({
|
|
406
|
+
position: nextPosition + index,
|
|
407
|
+
item
|
|
408
|
+
}));
|
|
409
|
+
const summaryPosition = nextPosition + replacementItems.length;
|
|
379
410
|
await applyContextCompaction(db, {
|
|
380
411
|
accountId: scope.accountId,
|
|
381
412
|
workspaceId: scope.workspaceId,
|
|
382
413
|
sessionId: scope.sessionId,
|
|
383
414
|
turnId: scope.turnId ?? null,
|
|
384
|
-
boundaryPosition,
|
|
415
|
+
boundaryPosition: nextPosition,
|
|
416
|
+
replacementItems,
|
|
385
417
|
summaryPosition,
|
|
386
|
-
summaryItem
|
|
418
|
+
summaryItem
|
|
387
419
|
});
|
|
388
|
-
return {
|
|
420
|
+
return {
|
|
421
|
+
compacted: true,
|
|
422
|
+
supersededFrom: nextPosition,
|
|
423
|
+
summaryPosition,
|
|
424
|
+
signalTokens: decision.signalTokens,
|
|
425
|
+
thresholdTokens: decision.thresholdTokens
|
|
426
|
+
};
|
|
389
427
|
}
|
|
390
428
|
|
|
391
429
|
// src/activities/environment.ts
|
|
@@ -886,7 +924,7 @@ function isPlainObject(value) {
|
|
|
886
924
|
}
|
|
887
925
|
|
|
888
926
|
// src/activities/run-input.ts
|
|
889
|
-
import { contextInputBudgetTokens
|
|
927
|
+
import { contextInputBudgetTokens, resolveContextCompactionMode as resolveContextCompactionMode2 } from "@opengeni/config";
|
|
890
928
|
import {
|
|
891
929
|
getActiveSessionHistoryItems as getActiveSessionHistoryItems2,
|
|
892
930
|
getLatestRunState,
|
|
@@ -1030,7 +1068,7 @@ function readPathBudgetTokens(settings) {
|
|
|
1030
1068
|
if (!settings || resolveContextCompactionMode2(settings) !== "client") {
|
|
1031
1069
|
return void 0;
|
|
1032
1070
|
}
|
|
1033
|
-
const budget =
|
|
1071
|
+
const budget = contextInputBudgetTokens(settings);
|
|
1034
1072
|
return budget > 0 ? budget : void 0;
|
|
1035
1073
|
}
|
|
1036
1074
|
async function userMessageTextWithAttachments(db, workspaceId, text, resources) {
|
|
@@ -1635,6 +1673,10 @@ function selectCodexCredentialForTurn(args) {
|
|
|
1635
1673
|
}
|
|
1636
1674
|
return null;
|
|
1637
1675
|
}
|
|
1676
|
+
var CONTEXT_OVERFLOW_RESUME_TEXT = [
|
|
1677
|
+
"[TURN RESUMED AFTER CONTEXT COMPACTION] The conversation approached or exceeded the model's context window mid-turn; older history above was compacted into a summary.",
|
|
1678
|
+
"Continue the original task from where it left off. Before repeating any action with side effects, check whether it already happened."
|
|
1679
|
+
].join("\n");
|
|
1638
1680
|
var WORKER_SHUTDOWN_RESUME_TEXT = [
|
|
1639
1681
|
"[TURN RESUMED AFTER WORKER RESTART] The platform worker running this turn restarted (deploy/rollout) before the turn finished.",
|
|
1640
1682
|
"Your conversation history above, including this turn's earlier work, is preserved up to the last checkpoint; at most the single in-flight model step after it was lost.",
|
|
@@ -1648,6 +1690,47 @@ var WORKER_DEATH_RESUME_TEXT = [
|
|
|
1648
1690
|
function isWorkerShutdownCancellation(error) {
|
|
1649
1691
|
return error instanceof CancelledFailure && error.message === "WORKER_SHUTDOWN";
|
|
1650
1692
|
}
|
|
1693
|
+
var CONTEXT_WINDOW_OVERFLOW_RECOVERY_MESSAGE = "The conversation reached the model context compaction threshold; it has been compacted. Send a message to continue.";
|
|
1694
|
+
function classifyContextWindowOverflowError(error) {
|
|
1695
|
+
const fields = collectErrorStrings(error);
|
|
1696
|
+
const matched = fields.find(
|
|
1697
|
+
(value) => /context[_\s-]*length[_\s-]*exceeded/i.test(value) || /exceeds?\s+(?:the\s+)?context\s+window/i.test(value) || /maximum\s+context\s+length/i.test(value) || /context\s+window[^.]*exceed/i.test(value)
|
|
1698
|
+
);
|
|
1699
|
+
if (!matched) {
|
|
1700
|
+
return null;
|
|
1701
|
+
}
|
|
1702
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1703
|
+
const code = fields.find((value) => /context[_\s-]*length[_\s-]*exceeded/i.test(value));
|
|
1704
|
+
return {
|
|
1705
|
+
message,
|
|
1706
|
+
...code ? { code } : {},
|
|
1707
|
+
...matched && matched !== message ? { detail: matched } : {}
|
|
1708
|
+
};
|
|
1709
|
+
}
|
|
1710
|
+
function collectErrorStrings(value, seen = /* @__PURE__ */ new WeakSet()) {
|
|
1711
|
+
if (typeof value === "string") {
|
|
1712
|
+
return [value];
|
|
1713
|
+
}
|
|
1714
|
+
if (!value || typeof value !== "object") {
|
|
1715
|
+
return [];
|
|
1716
|
+
}
|
|
1717
|
+
if (seen.has(value)) {
|
|
1718
|
+
return [];
|
|
1719
|
+
}
|
|
1720
|
+
seen.add(value);
|
|
1721
|
+
const out = [];
|
|
1722
|
+
const record = value;
|
|
1723
|
+
for (const key of ["message", "code", "type", "name", "param"]) {
|
|
1724
|
+
const field = record[key];
|
|
1725
|
+
if (typeof field === "string" && field.length > 0) {
|
|
1726
|
+
out.push(field);
|
|
1727
|
+
}
|
|
1728
|
+
}
|
|
1729
|
+
for (const key of ["error", "cause", "response", "data"]) {
|
|
1730
|
+
out.push(...collectErrorStrings(record[key], seen));
|
|
1731
|
+
}
|
|
1732
|
+
return out;
|
|
1733
|
+
}
|
|
1651
1734
|
function modelUsageSourceKey(input) {
|
|
1652
1735
|
if (input.responseId) {
|
|
1653
1736
|
return input.responseId;
|
|
@@ -1655,7 +1738,7 @@ function modelUsageSourceKey(input) {
|
|
|
1655
1738
|
return input.dispatchId ? `${input.dispatchId}:${input.positionalKey}` : input.positionalKey;
|
|
1656
1739
|
}
|
|
1657
1740
|
function historyRowsToAppend(rawHistory, persistedHistoryCount, nextPosition = persistedHistoryCount) {
|
|
1658
|
-
const sanitized =
|
|
1741
|
+
const sanitized = sanitizeHistoryItemsForModel2(rawHistory);
|
|
1659
1742
|
if (sanitized.length <= persistedHistoryCount) {
|
|
1660
1743
|
return { rows: [], nextWatermark: persistedHistoryCount, nextPosition };
|
|
1661
1744
|
}
|
|
@@ -1665,8 +1748,20 @@ function historyRowsToAppend(rawHistory, persistedHistoryCount, nextPosition = p
|
|
|
1665
1748
|
}));
|
|
1666
1749
|
return { rows, nextWatermark: sanitized.length, nextPosition: nextPosition + rows.length };
|
|
1667
1750
|
}
|
|
1751
|
+
function isModelOrToolProgressHistoryItem(item) {
|
|
1752
|
+
if (item.type === "message") {
|
|
1753
|
+
return item.role === "assistant";
|
|
1754
|
+
}
|
|
1755
|
+
if (item.type === "reasoning" || item.type === "compaction") {
|
|
1756
|
+
return true;
|
|
1757
|
+
}
|
|
1758
|
+
if (typeof item.type === "string") {
|
|
1759
|
+
return item.type !== "message";
|
|
1760
|
+
}
|
|
1761
|
+
return false;
|
|
1762
|
+
}
|
|
1668
1763
|
function reconcileSeedCount(activeSeedRows, modelHistoryFromItems, current) {
|
|
1669
|
-
return
|
|
1764
|
+
return sanitizeHistoryItemsForModel2(
|
|
1670
1765
|
modelHistoryFromItems ? applyCodexHistoryStrip(activeSeedRows, current) : activeSeedRows.map((row) => row.item)
|
|
1671
1766
|
).length;
|
|
1672
1767
|
}
|
|
@@ -1733,14 +1828,19 @@ function createRunAgentTurnActivity(services) {
|
|
|
1733
1828
|
let activeRecording = null;
|
|
1734
1829
|
let didComputerUse = false;
|
|
1735
1830
|
let batcher = null;
|
|
1831
|
+
const flushRuntimeBatcher = async () => {
|
|
1832
|
+
const current = batcher;
|
|
1833
|
+
await current?.flush().catch(() => void 0);
|
|
1834
|
+
};
|
|
1736
1835
|
let preparedTools = null;
|
|
1737
1836
|
let publish = null;
|
|
1738
1837
|
let turnStartedPublished = false;
|
|
1739
1838
|
let stream;
|
|
1740
1839
|
let persistedHistoryCount = 0;
|
|
1741
1840
|
let historyCountAtTurnStart = 0;
|
|
1841
|
+
let modelOrToolProgressPersisted = false;
|
|
1742
1842
|
let nextHistoryPosition = 0;
|
|
1743
|
-
const reconcileConversationTruth = async () => {
|
|
1843
|
+
const reconcileConversationTruth = async (options = {}) => {
|
|
1744
1844
|
if (!stream || !turnId) {
|
|
1745
1845
|
return;
|
|
1746
1846
|
}
|
|
@@ -1752,7 +1852,9 @@ function createRunAgentTurnActivity(services) {
|
|
|
1752
1852
|
persistedHistoryCount,
|
|
1753
1853
|
nextHistoryPosition
|
|
1754
1854
|
);
|
|
1755
|
-
|
|
1855
|
+
const hasModelOrToolProgress = rows.some((row) => isModelOrToolProgressHistoryItem(row.item));
|
|
1856
|
+
const shouldAppendRows = rows.length > 0 && (!options.skipInputOnlyRows || hasModelOrToolProgress);
|
|
1857
|
+
if (shouldAppendRows) {
|
|
1756
1858
|
await appendSessionHistoryItems(db, {
|
|
1757
1859
|
accountId: input.accountId,
|
|
1758
1860
|
workspaceId: input.workspaceId,
|
|
@@ -1766,8 +1868,13 @@ function createRunAgentTurnActivity(services) {
|
|
|
1766
1868
|
items: rows
|
|
1767
1869
|
});
|
|
1768
1870
|
}
|
|
1769
|
-
|
|
1770
|
-
|
|
1871
|
+
if (shouldAppendRows || !options.skipInputOnlyRows) {
|
|
1872
|
+
persistedHistoryCount = nextWatermark;
|
|
1873
|
+
nextHistoryPosition = nextPosition;
|
|
1874
|
+
}
|
|
1875
|
+
if (hasModelOrToolProgress) {
|
|
1876
|
+
modelOrToolProgressPersisted = true;
|
|
1877
|
+
}
|
|
1771
1878
|
}
|
|
1772
1879
|
const envelope = sandboxStateEntryFromRunState(stream.state);
|
|
1773
1880
|
if (envelope) {
|
|
@@ -1944,12 +2051,13 @@ function createRunAgentTurnActivity(services) {
|
|
|
1944
2051
|
}
|
|
1945
2052
|
const packRuntime = await resolveWorkspacePackRuntime(db, input.workspaceId);
|
|
1946
2053
|
const workspaceAgentInstructions = await resolveWorkspaceAgentInstructions(db, input.workspaceId);
|
|
1947
|
-
const
|
|
2054
|
+
const baseRunSettings = {
|
|
1948
2055
|
...settingsWithPackSandboxImage(capabilitySettings, packRuntime.sandboxImage),
|
|
1949
2056
|
openaiModel: turn.model,
|
|
1950
2057
|
openaiReasoningEffort: turn.reasoningEffort,
|
|
1951
2058
|
sandboxBackend: turn.sandboxBackend
|
|
1952
2059
|
};
|
|
2060
|
+
const runSettings = await settingsWithSessionMcpServersForRun(db, input.workspaceId, input.sessionId, baseRunSettings);
|
|
1953
2061
|
const resolvedModel = runtime.resolveTurnModel(capabilitySettings, turn.model);
|
|
1954
2062
|
const codexContext = resolvedModel?.provider.kind === "codex-subscription" ? (() => {
|
|
1955
2063
|
const resolver = buildCodexTokenResolver(db, runSettings, input.workspaceId, effectiveCodexCredentialId ?? "");
|
|
@@ -2222,6 +2330,12 @@ function createRunAgentTurnActivity(services) {
|
|
|
2222
2330
|
}
|
|
2223
2331
|
} : {}
|
|
2224
2332
|
});
|
|
2333
|
+
const compactSummarizer = resolvedModel ? (s, m) => withCodex(() => summarizeForCompaction2(s, m, {
|
|
2334
|
+
client: resolvedModel.client,
|
|
2335
|
+
api: resolvedModel.provider.api,
|
|
2336
|
+
model: resolvedModel.configured.id,
|
|
2337
|
+
maxOutputTokens: SUMMARY_BUFFER_TOKENS2
|
|
2338
|
+
})) : void 0;
|
|
2225
2339
|
if (triggerType === "user.message" || triggerType === "goal.continuation") {
|
|
2226
2340
|
try {
|
|
2227
2341
|
const forced = await consumeSessionCompactionRequest(db, input.workspaceId, input.sessionId);
|
|
@@ -2234,199 +2348,335 @@ function createRunAgentTurnActivity(services) {
|
|
|
2234
2348
|
// registry provider, summarize on THAT provider's client + wire API
|
|
2235
2349
|
// (a chat provider can't summarize through OpenAI/Azure). Null
|
|
2236
2350
|
// resolution keeps the default built-in Responses summarizer.
|
|
2237
|
-
|
|
2238
|
-
client: resolvedModel.client,
|
|
2239
|
-
api: resolvedModel.provider.api,
|
|
2240
|
-
model: resolvedModel.configured.id,
|
|
2241
|
-
maxOutputTokens: s.contextSummaryMaxTokens
|
|
2242
|
-
})) : void 0,
|
|
2351
|
+
compactSummarizer,
|
|
2243
2352
|
forced ? { force: true } : {}
|
|
2244
2353
|
);
|
|
2245
2354
|
if (outcome.compacted) {
|
|
2246
|
-
const trigger2 = forced ? "operator" :
|
|
2355
|
+
const trigger2 = forced ? "operator" : void 0;
|
|
2247
2356
|
await publish([{ type: "session.context.compacted", payload: { summaryPosition: outcome.summaryPosition, ...trigger2 ? { trigger: trigger2 } : {} } }]);
|
|
2248
2357
|
}
|
|
2249
2358
|
} catch (compactError) {
|
|
2250
2359
|
console.error("context compaction failed (turn proceeds un-compacted)", compactError);
|
|
2251
2360
|
}
|
|
2252
2361
|
}
|
|
2253
|
-
const
|
|
2254
|
-
|
|
2255
|
-
|
|
2256
|
-
|
|
2257
|
-
|
|
2258
|
-
|
|
2259
|
-
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
|
|
2286
|
-
|
|
2287
|
-
|
|
2288
|
-
}
|
|
2289
|
-
}
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
|
|
2295
|
-
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
|
|
2362
|
+
const activeTurnId = turnId;
|
|
2363
|
+
if (!activeTurnId) {
|
|
2364
|
+
throw new Error("Turn id was not initialized");
|
|
2365
|
+
}
|
|
2366
|
+
let runInput = null;
|
|
2367
|
+
const prepareRunAttemptInput = async () => {
|
|
2368
|
+
const prepared = await turnInput(
|
|
2369
|
+
db,
|
|
2370
|
+
runtime,
|
|
2371
|
+
agent,
|
|
2372
|
+
trigger,
|
|
2373
|
+
runSettings,
|
|
2374
|
+
{ currentCodexCredentialId: effectiveCodexCredentialId }
|
|
2375
|
+
);
|
|
2376
|
+
runInput = prepared.input;
|
|
2377
|
+
const activeSeedRows = await getActiveSessionHistoryItems3(db, input.workspaceId, input.sessionId);
|
|
2378
|
+
persistedHistoryCount = reconcileSeedCount(
|
|
2379
|
+
activeSeedRows,
|
|
2380
|
+
prepared.modelHistoryFromItems,
|
|
2381
|
+
{ currentCodexCredentialId: effectiveCodexCredentialId }
|
|
2382
|
+
);
|
|
2383
|
+
historyCountAtTurnStart = persistedHistoryCount;
|
|
2384
|
+
nextHistoryPosition = await nextSessionHistoryPosition2(db, input.workspaceId, input.sessionId);
|
|
2385
|
+
};
|
|
2386
|
+
const forceContextCompaction = async (triggerLabel) => {
|
|
2387
|
+
const clientCompactionSettings = { ...runSettings, contextCompactionMode: "client" };
|
|
2388
|
+
const outcome = await maybeCompactContext(
|
|
2389
|
+
db,
|
|
2390
|
+
clientCompactionSettings,
|
|
2391
|
+
{ accountId: input.accountId, workspaceId: input.workspaceId, sessionId: input.sessionId, turnId: activeTurnId },
|
|
2392
|
+
session.lastInputTokens,
|
|
2393
|
+
compactSummarizer,
|
|
2394
|
+
{ force: true }
|
|
2395
|
+
);
|
|
2396
|
+
if (outcome.compacted) {
|
|
2397
|
+
await publish([{ type: "session.context.compacted", payload: { summaryPosition: outcome.summaryPosition, trigger: triggerLabel } }]);
|
|
2398
|
+
}
|
|
2399
|
+
return outcome;
|
|
2400
|
+
};
|
|
2401
|
+
const runStreamAttempt = async () => {
|
|
2402
|
+
if (!runInput) {
|
|
2403
|
+
throw new Error("Run input was not prepared");
|
|
2404
|
+
}
|
|
2405
|
+
stream = void 0;
|
|
2406
|
+
batcher = null;
|
|
2407
|
+
let responseUsageCount = 0;
|
|
2408
|
+
let lastInputTokensObserved = null;
|
|
2409
|
+
throwIfWorkerShuttingDown();
|
|
2410
|
+
const runStreamOnce = () => runtime.runStream(agent, runInput, runSettings, {
|
|
2411
|
+
sandboxEnvironment,
|
|
2412
|
+
onRuntimeEvent: async (event) => {
|
|
2413
|
+
await publish([{ type: event.type, payload: event.payload }], true);
|
|
2414
|
+
},
|
|
2415
|
+
// P1.2: inject the resumed box NON-OWNED (the SDK never reaps it — the
|
|
2416
|
+
// keystone). Absent when the flag is off -> legacy build-and-discard.
|
|
2417
|
+
...resolvedSandbox ? {
|
|
2418
|
+
ownedSandbox: {
|
|
2419
|
+
client: resolvedSandbox.established.client,
|
|
2420
|
+
session: resolvedSandbox.established.session,
|
|
2421
|
+
sessionState: resolvedSandbox.established.sessionState,
|
|
2422
|
+
// Pin platform setup (hooks + file materialization) to the un-proxied
|
|
2423
|
+
// established box — never through the routing proxy, which would
|
|
2424
|
+
// re-route those execs onto a machine swapped in mid-turn.
|
|
2425
|
+
...setupBoxSession ? { setupSession: setupBoxSession } : {}
|
|
2310
2426
|
}
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
2318
|
-
|
|
2319
|
-
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
|
|
2326
|
-
|
|
2327
|
-
|
|
2328
|
-
|
|
2329
|
-
|
|
2427
|
+
} : {},
|
|
2428
|
+
contextCompactionSignalTokens: () => lastInputTokensObserved
|
|
2429
|
+
});
|
|
2430
|
+
stream = await withCodex(runStreamOnce);
|
|
2431
|
+
batcher = createRuntimeBatcher(async (events) => {
|
|
2432
|
+
await publish(events);
|
|
2433
|
+
});
|
|
2434
|
+
const iterator = stream.toStream()[Symbol.asyncIterator]();
|
|
2435
|
+
let streamDone = false;
|
|
2436
|
+
try {
|
|
2437
|
+
while (true) {
|
|
2438
|
+
const next = await nextStreamEvent(iterator, activityContext);
|
|
2439
|
+
if (next.done) {
|
|
2440
|
+
streamDone = true;
|
|
2441
|
+
break;
|
|
2442
|
+
}
|
|
2443
|
+
const responseUsage = modelResponseUsageFromSdkEvent(next.value);
|
|
2444
|
+
if (responseUsage) {
|
|
2445
|
+
responseUsageCount += 1;
|
|
2446
|
+
const observed = responseUsage.usage?.inputTokens;
|
|
2447
|
+
if (typeof observed === "number" && observed > 0) {
|
|
2448
|
+
lastInputTokensObserved = observed;
|
|
2449
|
+
await setSessionLastInputTokens(db, input.workspaceId, input.sessionId, observed).catch((error) => console.error("persist last_input_tokens failed (non-fatal)", error));
|
|
2450
|
+
}
|
|
2451
|
+
await recordModelUsageAndDebitCredits(settings, db, {
|
|
2452
|
+
accountId: input.accountId,
|
|
2453
|
+
workspaceId: input.workspaceId,
|
|
2454
|
+
sessionId: input.sessionId,
|
|
2455
|
+
turnId: activeTurnId,
|
|
2456
|
+
model: turn.model,
|
|
2457
|
+
isCodexTurn,
|
|
2458
|
+
usage: responseUsage.usage,
|
|
2459
|
+
sourceKey: modelUsageSourceKey({
|
|
2460
|
+
responseId: responseUsage.responseId,
|
|
2461
|
+
dispatchId,
|
|
2462
|
+
positionalKey: `response-${responseUsageCount}`
|
|
2463
|
+
})
|
|
2464
|
+
});
|
|
2465
|
+
await reconcileConversationTruth();
|
|
2330
2466
|
try {
|
|
2331
|
-
|
|
2332
|
-
} catch {
|
|
2333
|
-
serializedRunState = null;
|
|
2467
|
+
await ensureRunAllowed(settings, db, input.accountId, input.workspaceId, isCodexTurn, entitlements);
|
|
2468
|
+
} catch (limitError) {
|
|
2469
|
+
let serializedRunState = null;
|
|
2470
|
+
try {
|
|
2471
|
+
serializedRunState = stream.state.toString();
|
|
2472
|
+
} catch {
|
|
2473
|
+
serializedRunState = null;
|
|
2474
|
+
}
|
|
2475
|
+
throw new BudgetExhaustedError(
|
|
2476
|
+
limitError instanceof Error ? limitError.message : String(limitError),
|
|
2477
|
+
serializedRunState
|
|
2478
|
+
);
|
|
2334
2479
|
}
|
|
2335
|
-
|
|
2336
|
-
|
|
2337
|
-
|
|
2338
|
-
|
|
2480
|
+
}
|
|
2481
|
+
if (activeRecording && !didComputerUse && isComputerCallStreamEvent(next.value)) {
|
|
2482
|
+
didComputerUse = true;
|
|
2483
|
+
}
|
|
2484
|
+
const normalized = normalizeSdkEvent(next.value);
|
|
2485
|
+
for (const event of normalized) {
|
|
2486
|
+
await batcher.push(event);
|
|
2339
2487
|
}
|
|
2340
2488
|
}
|
|
2341
|
-
|
|
2342
|
-
|
|
2489
|
+
} finally {
|
|
2490
|
+
if (!streamDone) {
|
|
2491
|
+
await iterator.return?.();
|
|
2343
2492
|
}
|
|
2344
|
-
|
|
2345
|
-
|
|
2346
|
-
|
|
2493
|
+
}
|
|
2494
|
+
await batcher.flush();
|
|
2495
|
+
await stream.completed.catch(() => void 0);
|
|
2496
|
+
if (responseUsageCount === 0) {
|
|
2497
|
+
const aggregateInput = stream.state.usage?.inputTokens;
|
|
2498
|
+
if (typeof aggregateInput === "number" && aggregateInput > 0) {
|
|
2499
|
+
lastInputTokensObserved = aggregateInput;
|
|
2347
2500
|
}
|
|
2501
|
+
await recordModelUsageAndDebitCredits(settings, db, {
|
|
2502
|
+
accountId: input.accountId,
|
|
2503
|
+
workspaceId: input.workspaceId,
|
|
2504
|
+
sessionId: input.sessionId,
|
|
2505
|
+
turnId: activeTurnId,
|
|
2506
|
+
model: turn.model,
|
|
2507
|
+
isCodexTurn,
|
|
2508
|
+
usage: stream.state.usage,
|
|
2509
|
+
sourceKey: modelUsageSourceKey({ responseId: null, dispatchId, positionalKey: "aggregate" })
|
|
2510
|
+
});
|
|
2348
2511
|
}
|
|
2349
|
-
|
|
2350
|
-
|
|
2351
|
-
await iterator.return?.();
|
|
2512
|
+
if (lastInputTokensObserved !== null) {
|
|
2513
|
+
await setSessionLastInputTokens(db, input.workspaceId, input.sessionId, lastInputTokensObserved).catch((error) => console.error("persist last_input_tokens failed (non-fatal)", error));
|
|
2352
2514
|
}
|
|
2353
|
-
|
|
2354
|
-
|
|
2355
|
-
|
|
2356
|
-
|
|
2357
|
-
|
|
2358
|
-
|
|
2359
|
-
|
|
2515
|
+
if (stream.interruptions.length > 0) {
|
|
2516
|
+
await reconcileConversationTruth();
|
|
2517
|
+
const approvals = runtime.serializeApprovals(stream.interruptions);
|
|
2518
|
+
await saveRunState(db, {
|
|
2519
|
+
accountId: input.accountId,
|
|
2520
|
+
workspaceId: input.workspaceId,
|
|
2521
|
+
sessionId: input.sessionId,
|
|
2522
|
+
turnId: activeTurnId,
|
|
2523
|
+
serializedRunState: stream.state.toString(),
|
|
2524
|
+
pendingApprovals: approvals,
|
|
2525
|
+
// Record the account freezing this state so a resume on a DIFFERENT
|
|
2526
|
+
// codex account strips its account-bound reasoning before replay (HOLE C).
|
|
2527
|
+
frozenCodexCredentialId: effectiveCodexCredentialId
|
|
2528
|
+
});
|
|
2529
|
+
await publish([
|
|
2530
|
+
{ type: "session.requiresAction", payload: { approvals } },
|
|
2531
|
+
{ type: "session.status.changed", payload: { status: "requires_action" } }
|
|
2532
|
+
], true);
|
|
2533
|
+
await finishTurn(db, input.workspaceId, activeTurnId, "requires_action");
|
|
2534
|
+
await setSessionStatus(db, input.workspaceId, input.sessionId, "requires_action", activeTurnId);
|
|
2535
|
+
activityStatus = "requires_action";
|
|
2536
|
+
return { status: "requires_action" };
|
|
2360
2537
|
}
|
|
2361
|
-
|
|
2362
|
-
accountId: input.accountId,
|
|
2363
|
-
workspaceId: input.workspaceId,
|
|
2364
|
-
sessionId: input.sessionId,
|
|
2365
|
-
turnId,
|
|
2366
|
-
model: turn.model,
|
|
2367
|
-
isCodexTurn,
|
|
2368
|
-
usage: stream.state.usage,
|
|
2369
|
-
sourceKey: modelUsageSourceKey({ responseId: null, dispatchId, positionalKey: "aggregate" })
|
|
2370
|
-
});
|
|
2371
|
-
}
|
|
2372
|
-
if (lastInputTokensObserved !== null) {
|
|
2373
|
-
await setSessionLastInputTokens(db, input.workspaceId, input.sessionId, lastInputTokensObserved).catch((error) => console.error("persist last_input_tokens failed (non-fatal)", error));
|
|
2374
|
-
}
|
|
2375
|
-
if (stream.interruptions.length > 0) {
|
|
2538
|
+
const finalOutput = String(stream.finalOutput ?? "");
|
|
2376
2539
|
await reconcileConversationTruth();
|
|
2377
|
-
|
|
2378
|
-
|
|
2379
|
-
|
|
2380
|
-
|
|
2381
|
-
|
|
2382
|
-
|
|
2383
|
-
|
|
2384
|
-
|
|
2385
|
-
|
|
2386
|
-
|
|
2387
|
-
|
|
2388
|
-
});
|
|
2540
|
+
if (settings.sessionHistorySource !== "items") {
|
|
2541
|
+
await saveRunState(db, {
|
|
2542
|
+
accountId: input.accountId,
|
|
2543
|
+
workspaceId: input.workspaceId,
|
|
2544
|
+
sessionId: input.sessionId,
|
|
2545
|
+
turnId: activeTurnId,
|
|
2546
|
+
serializedRunState: stream.state.toString(),
|
|
2547
|
+
pendingApprovals: [],
|
|
2548
|
+
frozenCodexCredentialId: effectiveCodexCredentialId
|
|
2549
|
+
});
|
|
2550
|
+
}
|
|
2389
2551
|
await publish([
|
|
2390
|
-
{ type: "
|
|
2391
|
-
{ type: "
|
|
2552
|
+
{ type: "agent.message.completed", payload: { text: finalOutput } },
|
|
2553
|
+
{ type: "turn.completed", payload: { output: finalOutput } },
|
|
2554
|
+
{ type: "session.status.changed", payload: { status: "idle" } }
|
|
2392
2555
|
], true);
|
|
2393
|
-
await finishTurn(db, input.workspaceId,
|
|
2394
|
-
await setSessionStatus(db, input.workspaceId, input.sessionId, "
|
|
2395
|
-
|
|
2396
|
-
return { status: "requires_action" };
|
|
2397
|
-
}
|
|
2398
|
-
const finalOutput = String(stream.finalOutput ?? "");
|
|
2399
|
-
await reconcileConversationTruth();
|
|
2400
|
-
if (settings.sessionHistorySource !== "items") {
|
|
2401
|
-
await saveRunState(db, {
|
|
2556
|
+
await finishTurn(db, input.workspaceId, activeTurnId, "idle");
|
|
2557
|
+
await setSessionStatus(db, input.workspaceId, input.sessionId, "idle", null);
|
|
2558
|
+
await recordUsageEvent2(db, {
|
|
2402
2559
|
accountId: input.accountId,
|
|
2403
2560
|
workspaceId: input.workspaceId,
|
|
2404
|
-
|
|
2405
|
-
|
|
2406
|
-
|
|
2407
|
-
|
|
2408
|
-
|
|
2561
|
+
eventType: "agent_run.completed",
|
|
2562
|
+
quantity: 1,
|
|
2563
|
+
unit: "run",
|
|
2564
|
+
sourceResourceType: "session_turn",
|
|
2565
|
+
sourceResourceId: activeTurnId,
|
|
2566
|
+
idempotencyKey: `usage:agent_run.completed:${activeTurnId}`
|
|
2409
2567
|
});
|
|
2568
|
+
activityStatus = "idle";
|
|
2569
|
+
return { status: "idle" };
|
|
2570
|
+
};
|
|
2571
|
+
await prepareRunAttemptInput();
|
|
2572
|
+
let compactionRecoveryAttempted = false;
|
|
2573
|
+
let retriedAfterCompaction = false;
|
|
2574
|
+
while (true) {
|
|
2575
|
+
try {
|
|
2576
|
+
const result = await runStreamAttempt();
|
|
2577
|
+
if (retriedAfterCompaction) {
|
|
2578
|
+
observability.info("context compaction recovery succeeded after in-activity retry", {
|
|
2579
|
+
sessionId: input.sessionId,
|
|
2580
|
+
turnId: activeTurnId
|
|
2581
|
+
});
|
|
2582
|
+
}
|
|
2583
|
+
return result;
|
|
2584
|
+
} catch (attemptError) {
|
|
2585
|
+
const overflow = classifyContextWindowOverflowError(attemptError);
|
|
2586
|
+
const compactionNeeded = findCompactionNeededError(attemptError);
|
|
2587
|
+
const recoveryKind = compactionNeeded ? "proactive" : overflow ? "overflow" : null;
|
|
2588
|
+
if (!recoveryKind || !publish || !turnStartedPublished || compactionRecoveryAttempted) {
|
|
2589
|
+
throw attemptError;
|
|
2590
|
+
}
|
|
2591
|
+
compactionRecoveryAttempted = true;
|
|
2592
|
+
await flushRuntimeBatcher();
|
|
2593
|
+
await reconcileConversationTruth({ skipInputOnlyRows: true });
|
|
2594
|
+
const progressPersisted = modelOrToolProgressPersisted;
|
|
2595
|
+
observability.warn("context compaction recovery attempted", {
|
|
2596
|
+
sessionId: input.sessionId,
|
|
2597
|
+
turnId: activeTurnId,
|
|
2598
|
+
reason: recoveryKind,
|
|
2599
|
+
progressPersisted,
|
|
2600
|
+
code: overflow?.code,
|
|
2601
|
+
error: overflow?.message ?? compactionNeeded?.message,
|
|
2602
|
+
signalTokens: compactionNeeded?.signalTokens,
|
|
2603
|
+
thresholdTokens: compactionNeeded?.thresholdTokens
|
|
2604
|
+
});
|
|
2605
|
+
let compacted = false;
|
|
2606
|
+
try {
|
|
2607
|
+
const outcome = await forceContextCompaction(recoveryKind);
|
|
2608
|
+
compacted = outcome.compacted;
|
|
2609
|
+
} catch (compactError) {
|
|
2610
|
+
observability.warn("context compaction recovery compaction failed", {
|
|
2611
|
+
sessionId: input.sessionId,
|
|
2612
|
+
turnId: activeTurnId,
|
|
2613
|
+
error: compactError instanceof Error ? compactError.message : String(compactError)
|
|
2614
|
+
});
|
|
2615
|
+
}
|
|
2616
|
+
if (progressPersisted) {
|
|
2617
|
+
const triggerPayload = trigger.payload && typeof trigger.payload === "object" && !Array.isArray(trigger.payload) ? trigger.payload : {};
|
|
2618
|
+
const compactionResumedTurn = triggerType === "turn.preempted" && (triggerPayload.reason === "context_compacted" || triggerPayload.reason === "context_overflow_compacted");
|
|
2619
|
+
const canAutoContinue = compacted && !compactionResumedTurn && settings.sessionHistorySource === "items" && activeTurnId != null;
|
|
2620
|
+
if (canAutoContinue) {
|
|
2621
|
+
const [preemptedEvent] = await appendAndPublishEvents2(db, bus, input.workspaceId, input.sessionId, [
|
|
2622
|
+
{
|
|
2623
|
+
turnId: activeTurnId,
|
|
2624
|
+
type: "turn.preempted",
|
|
2625
|
+
payload: {
|
|
2626
|
+
triggerEventId: input.triggerEventId,
|
|
2627
|
+
reason: "context_compacted",
|
|
2628
|
+
resumeWithNotice: true,
|
|
2629
|
+
text: CONTEXT_OVERFLOW_RESUME_TEXT
|
|
2630
|
+
}
|
|
2631
|
+
},
|
|
2632
|
+
{ turnId: activeTurnId, type: "session.status.changed", payload: { status: "queued" } }
|
|
2633
|
+
]);
|
|
2634
|
+
await requeuePreemptedTurn(db, input.workspaceId, activeTurnId, preemptedEvent ? preemptedEvent.id : input.triggerEventId);
|
|
2635
|
+
await setSessionStatus(db, input.workspaceId, input.sessionId, "queued", null).catch(() => void 0);
|
|
2636
|
+
activityStatus = "preempted";
|
|
2637
|
+
observability.info("context compaction recovery succeeded by compacting and auto-continuing", {
|
|
2638
|
+
sessionId: input.sessionId,
|
|
2639
|
+
turnId: activeTurnId,
|
|
2640
|
+
reason: recoveryKind,
|
|
2641
|
+
compacted
|
|
2642
|
+
});
|
|
2643
|
+
return { status: "preempted" };
|
|
2644
|
+
}
|
|
2645
|
+
await publish([
|
|
2646
|
+
{
|
|
2647
|
+
type: "turn.failed",
|
|
2648
|
+
payload: {
|
|
2649
|
+
error: CONTEXT_WINDOW_OVERFLOW_RECOVERY_MESSAGE,
|
|
2650
|
+
code: recoveryKind === "overflow" ? "context_window_overflow_compacted" : "context_compacted",
|
|
2651
|
+
retryable: false,
|
|
2652
|
+
recovery: "user_message",
|
|
2653
|
+
compacted
|
|
2654
|
+
}
|
|
2655
|
+
},
|
|
2656
|
+
{ type: "session.status.changed", payload: { status: "idle" } }
|
|
2657
|
+
], true);
|
|
2658
|
+
await finishTurn(db, input.workspaceId, activeTurnId, "failed");
|
|
2659
|
+
await setSessionStatus(db, input.workspaceId, input.sessionId, "idle", null);
|
|
2660
|
+
activityStatus = "idle";
|
|
2661
|
+
activityError = attemptError;
|
|
2662
|
+
observability.info("context compaction recovery succeeded by compacting and idling", {
|
|
2663
|
+
sessionId: input.sessionId,
|
|
2664
|
+
turnId: activeTurnId,
|
|
2665
|
+
reason: recoveryKind,
|
|
2666
|
+
compacted
|
|
2667
|
+
});
|
|
2668
|
+
return { status: "idle" };
|
|
2669
|
+
}
|
|
2670
|
+
retriedAfterCompaction = true;
|
|
2671
|
+
observability.info("context compaction recovery retrying turn after compaction", {
|
|
2672
|
+
sessionId: input.sessionId,
|
|
2673
|
+
turnId: activeTurnId,
|
|
2674
|
+
reason: recoveryKind,
|
|
2675
|
+
compacted
|
|
2676
|
+
});
|
|
2677
|
+
await prepareRunAttemptInput();
|
|
2678
|
+
}
|
|
2410
2679
|
}
|
|
2411
|
-
await publish([
|
|
2412
|
-
{ type: "agent.message.completed", payload: { text: finalOutput } },
|
|
2413
|
-
{ type: "turn.completed", payload: { output: finalOutput } },
|
|
2414
|
-
{ type: "session.status.changed", payload: { status: "idle" } }
|
|
2415
|
-
], true);
|
|
2416
|
-
await finishTurn(db, input.workspaceId, turnId, "idle");
|
|
2417
|
-
await setSessionStatus(db, input.workspaceId, input.sessionId, "idle", null);
|
|
2418
|
-
await recordUsageEvent2(db, {
|
|
2419
|
-
accountId: input.accountId,
|
|
2420
|
-
workspaceId: input.workspaceId,
|
|
2421
|
-
eventType: "agent_run.completed",
|
|
2422
|
-
quantity: 1,
|
|
2423
|
-
unit: "run",
|
|
2424
|
-
sourceResourceType: "session_turn",
|
|
2425
|
-
sourceResourceId: turnId,
|
|
2426
|
-
idempotencyKey: `usage:agent_run.completed:${turnId}`
|
|
2427
|
-
});
|
|
2428
|
-
activityStatus = "idle";
|
|
2429
|
-
return { status: "idle" };
|
|
2430
2680
|
} catch (error) {
|
|
2431
2681
|
const preemptTurnId = turnId ?? input.turnId;
|
|
2432
2682
|
if (error instanceof SandboxLeaseSupersededError2 && preemptTurnId) {
|
|
@@ -2441,7 +2691,7 @@ function createRunAgentTurnActivity(services) {
|
|
|
2441
2691
|
}
|
|
2442
2692
|
if (isWorkerShutdownCancellation(error) && preemptTurnId) {
|
|
2443
2693
|
try {
|
|
2444
|
-
await
|
|
2694
|
+
await flushRuntimeBatcher();
|
|
2445
2695
|
await reconcileConversationTruth();
|
|
2446
2696
|
const approvalRerun = triggerType === "user.approvalDecision";
|
|
2447
2697
|
let resumeWithNotice = !approvalRerun && settings.sessionHistorySource === "items" && persistedHistoryCount > historyCountAtTurnStart;
|
|
@@ -2489,7 +2739,7 @@ function createRunAgentTurnActivity(services) {
|
|
|
2489
2739
|
if (error instanceof CancelledFailure) {
|
|
2490
2740
|
activityStatus = "cancelled";
|
|
2491
2741
|
activityError = error;
|
|
2492
|
-
await
|
|
2742
|
+
await flushRuntimeBatcher();
|
|
2493
2743
|
if (preemptTurnId) {
|
|
2494
2744
|
await finishTurn(db, input.workspaceId, preemptTurnId, "cancelled").catch(() => void 0);
|
|
2495
2745
|
}
|
|
@@ -2497,7 +2747,7 @@ function createRunAgentTurnActivity(services) {
|
|
|
2497
2747
|
}
|
|
2498
2748
|
const maxTurns = maxTurnsExceededRunState(error);
|
|
2499
2749
|
if (maxTurns && publish && turnId && turnStartedPublished) {
|
|
2500
|
-
await
|
|
2750
|
+
await flushRuntimeBatcher();
|
|
2501
2751
|
await reconcileConversationTruth();
|
|
2502
2752
|
const runStateSaved = Boolean(maxTurns.serializedRunState) && settings.sessionHistorySource !== "items";
|
|
2503
2753
|
if (maxTurns.serializedRunState && settings.sessionHistorySource !== "items") {
|
|
@@ -2534,7 +2784,7 @@ function createRunAgentTurnActivity(services) {
|
|
|
2534
2784
|
if (usageLimit && publish && turnId && turnStartedPublished) {
|
|
2535
2785
|
const goal = await getSessionGoal3(db, input.workspaceId, input.sessionId).catch(() => null);
|
|
2536
2786
|
const goalActive = Boolean(goal && goal.status === "active");
|
|
2537
|
-
await
|
|
2787
|
+
await flushRuntimeBatcher();
|
|
2538
2788
|
await reconcileConversationTruth();
|
|
2539
2789
|
const serializedRunState = agentsErrorRunState(error);
|
|
2540
2790
|
const runStateSaved = Boolean(serializedRunState) && settings.sessionHistorySource !== "items";
|
|
@@ -2618,7 +2868,7 @@ function createRunAgentTurnActivity(services) {
|
|
|
2618
2868
|
return { status: "idle" };
|
|
2619
2869
|
}
|
|
2620
2870
|
if (error instanceof BudgetExhaustedError && publish && turnId && turnStartedPublished) {
|
|
2621
|
-
await
|
|
2871
|
+
await flushRuntimeBatcher();
|
|
2622
2872
|
await reconcileConversationTruth();
|
|
2623
2873
|
const runStateSaved = Boolean(error.serializedRunState) && settings.sessionHistorySource !== "items";
|
|
2624
2874
|
if (error.serializedRunState && settings.sessionHistorySource !== "items") {
|
|
@@ -2655,7 +2905,7 @@ function createRunAgentTurnActivity(services) {
|
|
|
2655
2905
|
if (failure.retryable && publish && turnId && turnStartedPublished) {
|
|
2656
2906
|
const goal = await getSessionGoal3(db, input.workspaceId, input.sessionId).catch(() => null);
|
|
2657
2907
|
const goalActive = Boolean(goal && goal.status === "active");
|
|
2658
|
-
await
|
|
2908
|
+
await flushRuntimeBatcher();
|
|
2659
2909
|
await reconcileConversationTruth();
|
|
2660
2910
|
const serializedRunState = agentsErrorRunState(error);
|
|
2661
2911
|
const runStateSaved = Boolean(serializedRunState) && settings.sessionHistorySource !== "items";
|