@botbotgo/agent-harness 0.0.475 → 0.0.476
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/README.md +3 -1234
- package/README.zh.md +3 -1191
- package/dist/acp.js +1 -1
- package/dist/api.js +1 -404
- package/dist/benchmark/checkpoint-resume-cost-benchmark.js +1 -55
- package/dist/benchmark/deepagent-local-model-benchmark.js +2 -35
- package/dist/benchmark/upstream-runtime-ab-benchmark.js +1 -179
- package/dist/cli/chat-interactive.js +25 -244
- package/dist/cli/chat-rendering.js +6 -100
- package/dist/cli/chat-stream.js +23 -512
- package/dist/cli/chat-ui.js +21 -199
- package/dist/cli/chat-workspace.js +2 -210
- package/dist/cli/main.js +21 -428
- package/dist/cli/managed-service-commands.js +9 -63
- package/dist/cli/managed-service.js +2 -137
- package/dist/cli/options-init-chat.js +1 -108
- package/dist/cli/options-runtime.js +1 -158
- package/dist/cli/options-serve.js +1 -282
- package/dist/cli/options.js +2 -19
- package/dist/cli/process-guards.js +1 -139
- package/dist/cli/request-tree.js +7 -296
- package/dist/cli/runtime-commands.js +12 -258
- package/dist/cli/runtime-output.js +16 -155
- package/dist/cli/server-commands.js +16 -270
- package/dist/cli/workspace.js +1 -67
- package/dist/cli.js +1 -7
- package/dist/client/acp.js +1 -1
- package/dist/client/in-process.js +1 -67
- package/dist/client/index.js +1 -2
- package/dist/client/types.js +0 -1
- package/dist/client.js +1 -1
- package/dist/contracts/core.js +1 -1
- package/dist/contracts/runtime-evaluation.js +0 -1
- package/dist/contracts/runtime-memory.js +0 -1
- package/dist/contracts/runtime-observability.js +0 -1
- package/dist/contracts/runtime-requests.js +0 -1
- package/dist/contracts/runtime-scheduling.js +0 -1
- package/dist/contracts/runtime.js +1 -27
- package/dist/contracts/types.js +1 -3
- package/dist/contracts/workspace.js +0 -1
- package/dist/flow/build-flow-graph.js +1 -50
- package/dist/flow/export-mermaid.js +2 -464
- package/dist/flow/export-sequence-mermaid.js +2 -325
- package/dist/flow/flow-graph-normalization.js +1 -214
- package/dist/flow/flow-graph-runtime.js +1 -107
- package/dist/flow/flow-graph-upstream.js +1 -494
- package/dist/flow/index.js +1 -3
- package/dist/flow/types.js +0 -1
- package/dist/index.js +1 -5
- package/dist/init-project.js +1 -1
- package/dist/knowledge/config.js +1 -32
- package/dist/knowledge/contracts.js +0 -1
- package/dist/knowledge/index.js +1 -2
- package/dist/knowledge/module.js +12 -909
- package/dist/knowledge/procedural/config.js +1 -125
- package/dist/knowledge/procedural/index.js +1 -2
- package/dist/knowledge/procedural/manager.js +9 -345
- package/dist/mcp.js +1 -2
- package/dist/package-version.d.ts +1 -1
- package/dist/package-version.js +1 -2
- package/dist/persistence/file-store.js +3 -758
- package/dist/persistence/sqlite-request-context-store.js +5 -54
- package/dist/persistence/sqlite-request-queue-store.js +10 -108
- package/dist/persistence/sqlite-runtime.js +1 -86
- package/dist/persistence/sqlite-store.js +62 -810
- package/dist/persistence/types.js +0 -1
- package/dist/projections/presentation.js +37 -206
- package/dist/projections/request-events.js +2 -502
- package/dist/projections/upstream-events.js +1 -201
- package/dist/protocol/a2a/http-discovery.js +1 -178
- package/dist/protocol/a2a/http-rpc.js +6 -622
- package/dist/protocol/a2a/http.js +1 -138
- package/dist/protocol/a2a/task-state.js +3 -317
- package/dist/protocol/acp/client.js +8 -294
- package/dist/protocol/acp/harness-client.js +1 -218
- package/dist/protocol/acp/http.js +5 -130
- package/dist/protocol/acp/server.js +1 -310
- package/dist/protocol/acp/stdio.js +2 -69
- package/dist/protocol/ag-ui/http.js +3 -378
- package/dist/protocol/mcp/server.js +1 -428
- package/dist/resource/backend/workspace-scoped-backend.js +1 -319
- package/dist/resource/isolation.js +1 -237
- package/dist/resource/mcp/tool-support.js +3 -296
- package/dist/resource/mcp-tool-support.js +1 -2
- package/dist/resource/providers/resource-provider.js +1 -215
- package/dist/resource/resource-impl.js +1 -3
- package/dist/resource/resource-types.js +0 -1
- package/dist/resource/resource.js +1 -1
- package/dist/resource/sources.js +1 -247
- package/dist/resource/tools/function-tool-resolver.js +2 -272
- package/dist/runtime/adapter/compat/deepagent-compat.js +1 -29
- package/dist/runtime/adapter/compat/openai-compatible.js +1 -55
- package/dist/runtime/adapter/direct-builtin-utility.js +2 -90
- package/dist/runtime/adapter/flow/execution-context.js +1 -71
- package/dist/runtime/adapter/flow/invocation-flow.js +8 -425
- package/dist/runtime/adapter/flow/invoke-runtime.js +1 -20
- package/dist/runtime/adapter/flow/stream-runtime.js +11 -1395
- package/dist/runtime/adapter/invocation-result.js +2 -473
- package/dist/runtime/adapter/local-tool-invocation.js +6 -638
- package/dist/runtime/adapter/middleware/context-hygiene.js +1 -83
- package/dist/runtime/adapter/middleware-assembly.js +5 -477
- package/dist/runtime/adapter/model/invocation-request.js +3 -183
- package/dist/runtime/adapter/model/message-assembly.js +1 -28
- package/dist/runtime/adapter/model/model-providers.js +23 -1115
- package/dist/runtime/adapter/model/prompted-json-tool-call-capture.js +1 -40
- package/dist/runtime/adapter/model/prompted-json-tool-policy.js +1 -22
- package/dist/runtime/adapter/resilience.js +1 -104
- package/dist/runtime/adapter/runtime-adapter-support.js +3 -141
- package/dist/runtime/adapter/runtime-shell.js +5 -166
- package/dist/runtime/adapter/stream-event-projection.js +2 -622
- package/dist/runtime/adapter/stream-text-consumption.js +1 -18
- package/dist/runtime/adapter/terminal-status.js +2 -67
- package/dist/runtime/adapter/tool/builtin-middleware-tools.js +6 -627
- package/dist/runtime/adapter/tool/declared-middleware.js +1 -154
- package/dist/runtime/adapter/tool/interrupt-policy.js +1 -34
- package/dist/runtime/adapter/tool/provider-tool.js +1 -25
- package/dist/runtime/adapter/tool/resolved-tool.js +1 -225
- package/dist/runtime/adapter/tool/tool-arguments.js +3 -486
- package/dist/runtime/adapter/tool/tool-hitl.js +1 -346
- package/dist/runtime/adapter/tool/tool-name-mapping.js +1 -128
- package/dist/runtime/adapter/tool/tool-output-artifacts.js +2 -88
- package/dist/runtime/adapter/tool/tool-replay.js +1 -37
- package/dist/runtime/adapter/tool-resolution.js +1 -86
- package/dist/runtime/adapter/upstream-configurable-keys.js +1 -2
- package/dist/runtime/agent-runtime-adapter.js +60 -2338
- package/dist/runtime/agent-runtime-assembly.js +7 -249
- package/dist/runtime/env/runtime-env.js +1 -62
- package/dist/runtime/harness/background-runtime.js +1 -8
- package/dist/runtime/harness/bindings.js +1 -58
- package/dist/runtime/harness/events/event-bus.js +1 -16
- package/dist/runtime/harness/events/event-sink.js +1 -61
- package/dist/runtime/harness/events/events.js +1 -80
- package/dist/runtime/harness/events/listener-runtime.js +1 -13
- package/dist/runtime/harness/events/runtime-event-operations.js +1 -9
- package/dist/runtime/harness/events/streaming.js +1 -100
- package/dist/runtime/harness/events/timeline.js +1 -52
- package/dist/runtime/harness/public-shapes.js +1 -186
- package/dist/runtime/harness/run/artifact-paths.js +1 -15
- package/dist/runtime/harness/run/governance.js +1 -295
- package/dist/runtime/harness/run/helpers.js +1 -71
- package/dist/runtime/harness/run/inspection.js +1 -409
- package/dist/runtime/harness/run/operator-overview.js +1 -80
- package/dist/runtime/harness/run/queue-diagnostics.js +1 -15
- package/dist/runtime/harness/run/recovery.js +1 -162
- package/dist/runtime/harness/run/resources.js +1 -60
- package/dist/runtime/harness/run/resume.js +1 -56
- package/dist/runtime/harness/run/routing.js +1 -48
- package/dist/runtime/harness/run/run-lifecycle.js +1 -66
- package/dist/runtime/harness/run/run-operations.js +1 -217
- package/dist/runtime/harness/run/run-queue.js +1 -43
- package/dist/runtime/harness/run/run-slot-acquisition.js +1 -157
- package/dist/runtime/harness/run/session-records.js +1 -97
- package/dist/runtime/harness/run/start-run.js +1 -120
- package/dist/runtime/harness/run/startup-runtime.js +1 -69
- package/dist/runtime/harness/run/stream-run.js +8 -1418
- package/dist/runtime/harness/run/surface-semantics.js +1 -79
- package/dist/runtime/harness/runtime-defaults.js +1 -39
- package/dist/runtime/harness/system/boundary-analysis.js +1 -234
- package/dist/runtime/harness/system/health-monitor.js +1 -258
- package/dist/runtime/harness/system/inventory.js +1 -129
- package/dist/runtime/harness/system/mem0-ingestion-sync.js +5 -345
- package/dist/runtime/harness/system/policy-engine.js +1 -175
- package/dist/runtime/harness/system/runtime-memory-candidates.js +4 -110
- package/dist/runtime/harness/system/runtime-memory-consolidation.js +1 -51
- package/dist/runtime/harness/system/runtime-memory-manager.js +10 -693
- package/dist/runtime/harness/system/runtime-memory-policy.js +1 -155
- package/dist/runtime/harness/system/runtime-memory-records.js +11 -577
- package/dist/runtime/harness/system/runtime-memory-sync.js +5 -206
- package/dist/runtime/harness/system/session-memory-sync.js +3 -113
- package/dist/runtime/harness/system/skill-requirements.js +1 -112
- package/dist/runtime/harness/system/store.js +9 -365
- package/dist/runtime/harness/tool-gateway/index.js +1 -2
- package/dist/runtime/harness/tool-gateway/policy.js +1 -45
- package/dist/runtime/harness/tool-gateway/validation.js +1 -176
- package/dist/runtime/harness/tool-schema.js +1 -3
- package/dist/runtime/harness.js +3 -1490
- package/dist/runtime/index.js +1 -3
- package/dist/runtime/layout/runtime-layout.js +1 -31
- package/dist/runtime/maintenance/checkpoint-maintenance.js +2 -178
- package/dist/runtime/maintenance/file-checkpoint-saver.js +1 -106
- package/dist/runtime/maintenance/runtime-record-maintenance.js +2 -169
- package/dist/runtime/maintenance/sqlite-checkpoint-saver.js +4 -289
- package/dist/runtime/parsing/output-content.js +10 -550
- package/dist/runtime/parsing/output-parsing.js +1 -4
- package/dist/runtime/parsing/output-recovery.js +3 -213
- package/dist/runtime/parsing/output-tool-args.js +7 -663
- package/dist/runtime/parsing/stream-event-parsing.js +3 -362
- package/dist/runtime/prompts/runtime-prompts.js +4 -73
- package/dist/runtime/scheduling/system-schedule-manager.js +11 -532
- package/dist/runtime/skills/skill-metadata.js +1 -197
- package/dist/runtime/startup-tracing.js +2 -37
- package/dist/runtime/support/compiled-binding.js +1 -290
- package/dist/runtime/support/embedding-models.js +1 -118
- package/dist/runtime/support/harness-support.js +5 -137
- package/dist/runtime/support/llamaindex.js +1 -108
- package/dist/runtime/support/runtime-adapter-options.js +1 -29
- package/dist/runtime/support/runtime-factories.js +1 -51
- package/dist/runtime/support/vector-stores.js +9 -270
- package/dist/scaffold/init-project.js +54 -233
- package/dist/tooling/extensions.js +1 -311
- package/dist/tooling/module-loader.js +1 -55
- package/dist/tools.js +1 -176
- package/dist/utils/agent-display.js +1 -18
- package/dist/utils/bundled-text.js +4 -39
- package/dist/utils/compiled-binding.js +1 -33
- package/dist/utils/fs.js +2 -45
- package/dist/utils/id.js +1 -9
- package/dist/utils/message-content.js +1 -30
- package/dist/utils/object.js +1 -6
- package/dist/workspace/agent-binding-compiler.js +3 -613
- package/dist/workspace/compile.js +1 -472
- package/dist/workspace/framework-contract-validation.js +2 -322
- package/dist/workspace/index.js +1 -1
- package/dist/workspace/object-loader-paths.js +1 -71
- package/dist/workspace/object-loader-readers.js +1 -187
- package/dist/workspace/object-loader.js +1 -754
- package/dist/workspace/resource-compilers.js +1 -374
- package/dist/workspace/support/agent-capabilities.js +1 -37
- package/dist/workspace/support/agent-execution-config.js +1 -44
- package/dist/workspace/support/discovery.js +1 -147
- package/dist/workspace/support/source-collectors.js +1 -30
- package/dist/workspace/support/source-protocols.js +2 -192
- package/dist/workspace/support/workspace-ref-utils.js +1 -362
- package/dist/workspace/tool-hydration.js +1 -280
- package/dist/workspace/validate.js +1 -99
- package/dist/workspace/yaml-object-reader.js +1 -285
- package/package.json +7 -3
|
@@ -1,43 +1 @@
|
|
|
1
|
-
|
|
2
|
-
const previousPositions = new Map(pendingRequestSlots
|
|
3
|
-
.filter((candidate) => Boolean(candidate.sessionId && candidate.requestId))
|
|
4
|
-
.map((candidate, index) => [candidate.requestId, index + 1]));
|
|
5
|
-
pendingRequestSlots.push({
|
|
6
|
-
...entry,
|
|
7
|
-
insertionOrder,
|
|
8
|
-
});
|
|
9
|
-
pendingRequestSlots.sort((left, right) => {
|
|
10
|
-
if (right.priority !== left.priority) {
|
|
11
|
-
return right.priority - left.priority;
|
|
12
|
-
}
|
|
13
|
-
return left.insertionOrder - right.insertionOrder;
|
|
14
|
-
});
|
|
15
|
-
return pendingRequestSlots.flatMap((candidate, index) => {
|
|
16
|
-
if (!candidate.sessionId || !candidate.requestId) {
|
|
17
|
-
return [];
|
|
18
|
-
}
|
|
19
|
-
const previousPosition = previousPositions.get(candidate.requestId);
|
|
20
|
-
const queuePosition = index + 1;
|
|
21
|
-
if (previousPosition === undefined || previousPosition === queuePosition) {
|
|
22
|
-
return [];
|
|
23
|
-
}
|
|
24
|
-
return [{
|
|
25
|
-
sessionId: candidate.sessionId,
|
|
26
|
-
requestId: candidate.requestId,
|
|
27
|
-
priority: candidate.priority,
|
|
28
|
-
queuePosition,
|
|
29
|
-
}];
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
export function shiftNextPendingRequestSlot(pendingRequestSlots) {
|
|
33
|
-
return pendingRequestSlots.shift();
|
|
34
|
-
}
|
|
35
|
-
export function dropPendingRequestSlot(pendingRequestSlots, requestId) {
|
|
36
|
-
const index = pendingRequestSlots.findIndex((entry) => entry.requestId === requestId);
|
|
37
|
-
if (index < 0) {
|
|
38
|
-
return false;
|
|
39
|
-
}
|
|
40
|
-
const [entry] = pendingRequestSlots.splice(index, 1);
|
|
41
|
-
entry?.abort();
|
|
42
|
-
return true;
|
|
43
|
-
}
|
|
1
|
+
function p(e,i,o){const s=new Map(e.filter(r=>!!(r.sessionId&&r.requestId)).map((r,t)=>[r.requestId,t+1]));return e.push({...i,insertionOrder:o}),e.sort((r,t)=>t.priority!==r.priority?t.priority-r.priority:r.insertionOrder-t.insertionOrder),e.flatMap((r,t)=>{if(!r.sessionId||!r.requestId)return[];const n=s.get(r.requestId),u=t+1;return n===void 0||n===u?[]:[{sessionId:r.sessionId,requestId:r.requestId,priority:r.priority,queuePosition:u}]})}function f(e){return e.shift()}function I(e,i){const o=e.findIndex(r=>r.requestId===i);if(o<0)return!1;const[s]=e.splice(o,1);return s?.abort(),!0}export{I as dropPendingRequestSlot,p as enqueuePendingRequestSlot,f as shiftNextPendingRequestSlot};
|
|
@@ -1,157 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
export async function acquireRequestSlot(runtime, options = {}) {
|
|
3
|
-
const { sessionId, requestId } = options;
|
|
4
|
-
const activeState = options.activeState ?? "running";
|
|
5
|
-
const priority = options.priority ?? 0;
|
|
6
|
-
let stopHeartbeat = () => undefined;
|
|
7
|
-
const beginLease = async (mode) => {
|
|
8
|
-
if (!sessionId || !requestId) {
|
|
9
|
-
return;
|
|
10
|
-
}
|
|
11
|
-
const claimedAt = new Date().toISOString();
|
|
12
|
-
const leaseExpiresAt = new Date(Date.now() + runtime.concurrencyConfig.leaseMs).toISOString();
|
|
13
|
-
if (mode === "queue-claim") {
|
|
14
|
-
await runtime.persistence.claimQueuedRequest({
|
|
15
|
-
sessionId,
|
|
16
|
-
requestId,
|
|
17
|
-
workerId: runtime.workerId,
|
|
18
|
-
claimedAt,
|
|
19
|
-
leaseExpiresAt,
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
else {
|
|
23
|
-
await runtime.persistence.renewRequestLease({
|
|
24
|
-
requestId,
|
|
25
|
-
workerId: runtime.workerId,
|
|
26
|
-
heartbeatAt: claimedAt,
|
|
27
|
-
leaseExpiresAt,
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
if (runtime.concurrencyConfig.heartbeatIntervalMs <= 0) {
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
const timer = setInterval(() => {
|
|
34
|
-
void runtime.persistence.renewRequestLease({
|
|
35
|
-
requestId,
|
|
36
|
-
workerId: runtime.workerId,
|
|
37
|
-
heartbeatAt: new Date().toISOString(),
|
|
38
|
-
leaseExpiresAt: new Date(Date.now() + runtime.concurrencyConfig.leaseMs).toISOString(),
|
|
39
|
-
});
|
|
40
|
-
}, runtime.concurrencyConfig.heartbeatIntervalMs);
|
|
41
|
-
timer.unref?.();
|
|
42
|
-
stopHeartbeat = () => {
|
|
43
|
-
clearInterval(timer);
|
|
44
|
-
};
|
|
45
|
-
};
|
|
46
|
-
const releaseLease = async () => {
|
|
47
|
-
stopHeartbeat();
|
|
48
|
-
if (requestId) {
|
|
49
|
-
await runtime.persistence.releaseRequestClaim(requestId);
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
const releaseAndAdvance = async (shiftNext) => {
|
|
53
|
-
await releaseLease();
|
|
54
|
-
runtime.setActiveRequestSlots(Math.max(0, runtime.getActiveRequestSlots() - 1));
|
|
55
|
-
const next = shiftNext();
|
|
56
|
-
void next?.activate();
|
|
57
|
-
};
|
|
58
|
-
const maxConcurrentRuns = runtime.concurrencyConfig.maxConcurrentRuns;
|
|
59
|
-
if (!maxConcurrentRuns) {
|
|
60
|
-
await beginLease("direct-heartbeat");
|
|
61
|
-
return async () => {
|
|
62
|
-
await releaseLease();
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
const canActivateImmediately = runtime.getActiveRequestSlots() < maxConcurrentRuns;
|
|
66
|
-
const useDirectHeartbeatFastPath = canActivateImmediately && maxConcurrentRuns > 1;
|
|
67
|
-
if (canActivateImmediately) {
|
|
68
|
-
runtime.setActiveRequestSlots(runtime.getActiveRequestSlots() + 1);
|
|
69
|
-
if (sessionId && requestId && !useDirectHeartbeatFastPath) {
|
|
70
|
-
await runtime.persistence.enqueueRequest({ sessionId, requestId, priority });
|
|
71
|
-
}
|
|
72
|
-
await beginLease(useDirectHeartbeatFastPath ? "direct-heartbeat" : "queue-claim");
|
|
73
|
-
let released = false;
|
|
74
|
-
return async () => {
|
|
75
|
-
if (released) {
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
released = true;
|
|
79
|
-
await releaseAndAdvance(() => shiftNextPendingRequestSlot(runtime.pendingRequestSlots));
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
const activateQueuedRequest = async () => {
|
|
83
|
-
const currentRequest = requestId ? await runtime.persistence.getRequest(requestId) : null;
|
|
84
|
-
if (currentRequest?.state === "cancelled") {
|
|
85
|
-
return "abort";
|
|
86
|
-
}
|
|
87
|
-
runtime.setActiveRequestSlots(runtime.getActiveRequestSlots() + 1);
|
|
88
|
-
if (sessionId && requestId) {
|
|
89
|
-
await runtime.emit(sessionId, requestId, 4, "request.dequeued", {
|
|
90
|
-
queuePosition: 0,
|
|
91
|
-
activeRequestCount: runtime.getActiveRequestSlots(),
|
|
92
|
-
maxConcurrentRuns,
|
|
93
|
-
priority,
|
|
94
|
-
});
|
|
95
|
-
await runtime.setRequestStateAndEmit(sessionId, requestId, 5, activeState, {
|
|
96
|
-
previousState: "queued",
|
|
97
|
-
});
|
|
98
|
-
await beginLease("queue-claim");
|
|
99
|
-
}
|
|
100
|
-
return "activate";
|
|
101
|
-
};
|
|
102
|
-
if (sessionId && requestId) {
|
|
103
|
-
await runtime.persistence.enqueueRequest({ sessionId, requestId, priority });
|
|
104
|
-
const slotAcquisition = new Promise((resolve, reject) => {
|
|
105
|
-
const displacedEntries = runtime.enqueuePendingRequestSlot({
|
|
106
|
-
sessionId,
|
|
107
|
-
requestId,
|
|
108
|
-
priority,
|
|
109
|
-
activate: async () => {
|
|
110
|
-
try {
|
|
111
|
-
resolve(await activateQueuedRequest());
|
|
112
|
-
}
|
|
113
|
-
catch (error) {
|
|
114
|
-
reject(error);
|
|
115
|
-
}
|
|
116
|
-
},
|
|
117
|
-
abort: () => resolve("abort"),
|
|
118
|
-
});
|
|
119
|
-
void Promise.all(displacedEntries.map((candidate) => runtime.emit(candidate.sessionId, candidate.requestId, 3, "request.queued", {
|
|
120
|
-
queuePosition: candidate.queuePosition,
|
|
121
|
-
activeRequestCount: runtime.getActiveRequestSlots(),
|
|
122
|
-
maxConcurrentRuns,
|
|
123
|
-
priority: candidate.priority,
|
|
124
|
-
})));
|
|
125
|
-
});
|
|
126
|
-
const queuePosition = runtime.pendingRequestSlots.findIndex((entry) => entry.requestId === requestId) + 1;
|
|
127
|
-
await runtime.setRequestStateAndEmit(sessionId, requestId, 2, "queued", {
|
|
128
|
-
previousState: activeState,
|
|
129
|
-
});
|
|
130
|
-
await runtime.emit(sessionId, requestId, 3, "request.queued", {
|
|
131
|
-
queuePosition,
|
|
132
|
-
activeRequestCount: runtime.getActiveRequestSlots(),
|
|
133
|
-
maxConcurrentRuns,
|
|
134
|
-
priority,
|
|
135
|
-
});
|
|
136
|
-
const slotAcquisitionResult = await slotAcquisition;
|
|
137
|
-
if (slotAcquisitionResult === "abort") {
|
|
138
|
-
return async () => undefined;
|
|
139
|
-
}
|
|
140
|
-
let released = false;
|
|
141
|
-
return async () => {
|
|
142
|
-
if (released) {
|
|
143
|
-
return;
|
|
144
|
-
}
|
|
145
|
-
released = true;
|
|
146
|
-
await releaseAndAdvance(() => runtime.pendingRequestSlots.shift());
|
|
147
|
-
};
|
|
148
|
-
}
|
|
149
|
-
let released = false;
|
|
150
|
-
return async () => {
|
|
151
|
-
if (released) {
|
|
152
|
-
return;
|
|
153
|
-
}
|
|
154
|
-
released = true;
|
|
155
|
-
await releaseAndAdvance(() => shiftNextPendingRequestSlot(runtime.pendingRequestSlots));
|
|
156
|
-
};
|
|
157
|
-
}
|
|
1
|
+
import{shiftNextPendingRequestSlot as p}from"./run-queue.js";async function C(e,l={}){const{sessionId:s,requestId:t}=l,w=l.activeState??"running",o=l.priority??0;let S=()=>{};const q=async a=>{if(!s||!t)return;const c=new Date().toISOString(),R=new Date(Date.now()+e.concurrencyConfig.leaseMs).toISOString();if(a==="queue-claim"?await e.persistence.claimQueuedRequest({sessionId:s,requestId:t,workerId:e.workerId,claimedAt:c,leaseExpiresAt:R}):await e.persistence.renewRequestLease({requestId:t,workerId:e.workerId,heartbeatAt:c,leaseExpiresAt:R}),e.concurrencyConfig.heartbeatIntervalMs<=0)return;const r=setInterval(()=>{e.persistence.renewRequestLease({requestId:t,workerId:e.workerId,heartbeatAt:new Date().toISOString(),leaseExpiresAt:new Date(Date.now()+e.concurrencyConfig.leaseMs).toISOString()})},e.concurrencyConfig.heartbeatIntervalMs);r.unref?.(),S=()=>{clearInterval(r)}},f=async()=>{S(),t&&await e.persistence.releaseRequestClaim(t)},d=async a=>{await f(),e.setActiveRequestSlots(Math.max(0,e.getActiveRequestSlots()-1)),a()?.activate()},i=e.concurrencyConfig.maxConcurrentRuns;if(!i)return await q("direct-heartbeat"),async()=>{await f()};const v=e.getActiveRequestSlots()<i,g=v&&i>1;if(v){e.setActiveRequestSlots(e.getActiveRequestSlots()+1),s&&t&&!g&&await e.persistence.enqueueRequest({sessionId:s,requestId:t,priority:o}),await q(g?"direct-heartbeat":"queue-claim");let a=!1;return async()=>{a||(a=!0,await d(()=>p(e.pendingRequestSlots)))}}const y=async()=>(t?await e.persistence.getRequest(t):null)?.state==="cancelled"?"abort":(e.setActiveRequestSlots(e.getActiveRequestSlots()+1),s&&t&&(await e.emit(s,t,4,"request.dequeued",{queuePosition:0,activeRequestCount:e.getActiveRequestSlots(),maxConcurrentRuns:i,priority:o}),await e.setRequestStateAndEmit(s,t,5,w,{previousState:"queued"}),await q("queue-claim")),"activate");if(s&&t){await e.persistence.enqueueRequest({sessionId:s,requestId:t,priority:o});const a=new Promise((u,I)=>{const b=e.enqueuePendingRequestSlot({sessionId:s,requestId:t,priority:o,activate:async()=>{try{u(await y())}catch(n){I(n)}},abort:()=>u("abort")});Promise.all(b.map(n=>e.emit(n.sessionId,n.requestId,3,"request.queued",{queuePosition:n.queuePosition,activeRequestCount:e.getActiveRequestSlots(),maxConcurrentRuns:i,priority:n.priority})))}),c=e.pendingRequestSlots.findIndex(u=>u.requestId===t)+1;if(await e.setRequestStateAndEmit(s,t,2,"queued",{previousState:w}),await e.emit(s,t,3,"request.queued",{queuePosition:c,activeRequestCount:e.getActiveRequestSlots(),maxConcurrentRuns:i,priority:o}),await a==="abort")return async()=>{};let r=!1;return async()=>{r||(r=!0,await d(()=>e.pendingRequestSlots.shift()))}}let A=!1;return async()=>{A||(A=!0,await d(()=>p(e.pendingRequestSlots)))}}export{C as acquireRequestSlot};
|
|
@@ -1,97 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { projectRuntimeTimeline } from "../events/timeline.js";
|
|
3
|
-
function selectLatestPendingApproval(approvals) {
|
|
4
|
-
return approvals
|
|
5
|
-
.filter((approval) => approval.status === "pending")
|
|
6
|
-
.sort((left, right) => right.requestedAt.localeCompare(left.requestedAt))[0];
|
|
7
|
-
}
|
|
8
|
-
export async function buildRequestInspectionRecord(persistence, request) {
|
|
9
|
-
const inspection = await persistence.getRequestInspection(request.sessionId, request.requestId);
|
|
10
|
-
const runtimeEvents = await persistence.listRequestEvents(request.sessionId, request.requestId);
|
|
11
|
-
return {
|
|
12
|
-
requestId: request.requestId,
|
|
13
|
-
sessionId: request.sessionId,
|
|
14
|
-
agentId: request.agentId,
|
|
15
|
-
parentRequestId: request.parentRequestId,
|
|
16
|
-
executionMode: request.executionMode,
|
|
17
|
-
adapterKind: request.adapterKind,
|
|
18
|
-
createdAt: request.createdAt,
|
|
19
|
-
updatedAt: request.updatedAt,
|
|
20
|
-
state: request.state,
|
|
21
|
-
checkpointRef: request.checkpointRef,
|
|
22
|
-
resumable: request.resumable,
|
|
23
|
-
startedAt: request.startedAt,
|
|
24
|
-
endedAt: request.endedAt,
|
|
25
|
-
lastActivityAt: request.lastActivityAt,
|
|
26
|
-
currentAgentId: request.currentAgentId,
|
|
27
|
-
delegationChain: request.delegationChain,
|
|
28
|
-
runtimeSnapshot: request.runtimeSnapshot,
|
|
29
|
-
traceItems: inspection.traceItems,
|
|
30
|
-
runtimeTimeline: projectRuntimeTimeline(runtimeEvents, {
|
|
31
|
-
sessionId: request.sessionId,
|
|
32
|
-
requestId: request.requestId,
|
|
33
|
-
}),
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
export async function buildSessionInspectionRecord(input, sessionId) {
|
|
37
|
-
const [sessionSummary, meta, messages, requestSummaries] = await Promise.all([
|
|
38
|
-
input.getSession(sessionId),
|
|
39
|
-
input.persistence.getSessionMeta(sessionId),
|
|
40
|
-
input.persistence.listSessionMessages(sessionId, 200),
|
|
41
|
-
input.persistence.listSessionRequests(sessionId),
|
|
42
|
-
]);
|
|
43
|
-
if (!sessionSummary || !meta) {
|
|
44
|
-
return null;
|
|
45
|
-
}
|
|
46
|
-
const latestRequestId = sessionSummary.latestRequestId;
|
|
47
|
-
const requests = await Promise.all(requestSummaries.map((request) => buildRequestInspectionRecord(input.persistence, request)));
|
|
48
|
-
const latestApprovals = await input.persistence.getRequestApprovals(sessionId, latestRequestId);
|
|
49
|
-
const pendingApproval = selectLatestPendingApproval(latestApprovals);
|
|
50
|
-
return {
|
|
51
|
-
sessionId,
|
|
52
|
-
entryAgentId: meta.entryAgentId,
|
|
53
|
-
currentAgentId: sessionSummary.currentAgentId,
|
|
54
|
-
currentState: sessionSummary.status,
|
|
55
|
-
latestRequestId,
|
|
56
|
-
createdAt: meta.createdAt,
|
|
57
|
-
updatedAt: sessionSummary.updatedAt,
|
|
58
|
-
messages,
|
|
59
|
-
requests,
|
|
60
|
-
pendingDecision: pendingApproval
|
|
61
|
-
? {
|
|
62
|
-
approvalId: pendingApproval.approvalId,
|
|
63
|
-
pendingActionId: pendingApproval.pendingActionId,
|
|
64
|
-
toolName: pendingApproval.toolName,
|
|
65
|
-
allowedDecisions: pendingApproval.allowedDecisions,
|
|
66
|
-
requestedAt: pendingApproval.requestedAt,
|
|
67
|
-
}
|
|
68
|
-
: undefined,
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
export async function listPublicApprovals(input, filter) {
|
|
72
|
-
const approvals = await input.persistence.listApprovals({
|
|
73
|
-
status: filter?.status,
|
|
74
|
-
sessionId: filter?.sessionId ?? filter?.legacySessionId,
|
|
75
|
-
requestId: filter?.requestId ?? filter?.legacyRunId,
|
|
76
|
-
});
|
|
77
|
-
return approvals.map((approval) => toInspectableApprovalRecord(approval));
|
|
78
|
-
}
|
|
79
|
-
export async function getPublicApproval(input, approvalId) {
|
|
80
|
-
const approval = await input.persistence.getApproval(approvalId);
|
|
81
|
-
return approval ? toInspectableApprovalRecord(approval) : null;
|
|
82
|
-
}
|
|
83
|
-
export async function deleteSessionRecord(input, sessionId) {
|
|
84
|
-
const session = await input.getSession(sessionId);
|
|
85
|
-
if (!session) {
|
|
86
|
-
return false;
|
|
87
|
-
}
|
|
88
|
-
const activeRequest = session.requests.find((request) => !isTerminalRequestState(request.state));
|
|
89
|
-
if (activeRequest) {
|
|
90
|
-
throw new Error(`Cannot delete session ${sessionId} while request ${activeRequest.requestId} is ${activeRequest.state}`);
|
|
91
|
-
}
|
|
92
|
-
const deleted = await input.deleteSession(sessionId);
|
|
93
|
-
if (deleted) {
|
|
94
|
-
await input.deleteSessionCheckpoints(sessionId);
|
|
95
|
-
}
|
|
96
|
-
return deleted;
|
|
97
|
-
}
|
|
1
|
+
import{isTerminalRequestState as u,toInspectableApprovalRecord as d}from"./helpers.js";import{projectRuntimeTimeline as A}from"../events/timeline.js";function I(t){return t.filter(e=>e.status==="pending").sort((e,s)=>s.requestedAt.localeCompare(e.requestedAt))[0]}async function m(t,e){const s=await t.getRequestInspection(e.sessionId,e.requestId),n=await t.listRequestEvents(e.sessionId,e.requestId);return{requestId:e.requestId,sessionId:e.sessionId,agentId:e.agentId,parentRequestId:e.parentRequestId,executionMode:e.executionMode,adapterKind:e.adapterKind,createdAt:e.createdAt,updatedAt:e.updatedAt,state:e.state,checkpointRef:e.checkpointRef,resumable:e.resumable,startedAt:e.startedAt,endedAt:e.endedAt,lastActivityAt:e.lastActivityAt,currentAgentId:e.currentAgentId,delegationChain:e.delegationChain,runtimeSnapshot:e.runtimeSnapshot,traceItems:s.traceItems,runtimeTimeline:A(n,{sessionId:e.sessionId,requestId:e.requestId})}}async function R(t,e){const[s,n,o,i]=await Promise.all([t.getSession(e),t.persistence.getSessionMeta(e),t.persistence.listSessionMessages(e,200),t.persistence.listSessionRequests(e)]);if(!s||!n)return null;const r=s.latestRequestId,c=await Promise.all(i.map(l=>m(t.persistence,l))),p=await t.persistence.getRequestApprovals(e,r),a=I(p);return{sessionId:e,entryAgentId:n.entryAgentId,currentAgentId:s.currentAgentId,currentState:s.status,latestRequestId:r,createdAt:n.createdAt,updatedAt:s.updatedAt,messages:o,requests:c,pendingDecision:a?{approvalId:a.approvalId,pendingActionId:a.pendingActionId,toolName:a.toolName,allowedDecisions:a.allowedDecisions,requestedAt:a.requestedAt}:void 0}}async function S(t,e){return(await t.persistence.listApprovals({status:e?.status,sessionId:e?.sessionId??e?.legacySessionId,requestId:e?.requestId??e?.legacyRunId})).map(n=>d(n))}async function w(t,e){const s=await t.persistence.getApproval(e);return s?d(s):null}async function f(t,e){const s=await t.getSession(e);if(!s)return!1;const n=s.requests.find(i=>!u(i.state));if(n)throw new Error(`Cannot delete session ${e} while request ${n.requestId} is ${n.state}`);const o=await t.deleteSession(e);return o&&await t.deleteSessionCheckpoints(e),o}export{m as buildRequestInspectionRecord,R as buildSessionInspectionRecord,f as deleteSessionRecord,w as getPublicApproval,S as listPublicApprovals};
|
|
@@ -1,120 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { createPersistentId } from "../../../utils/id.js";
|
|
3
|
-
import { normalizeMessageContent } from "../../../utils/message-content.js";
|
|
4
|
-
import { buildPersistedRequestInput, normalizeRequestPriority } from "./helpers.js";
|
|
5
|
-
import { getRequiredWorkspaceBinding } from "../bindings.js";
|
|
6
|
-
import { getBindingAdapterKind, getBindingRuntimeExecutionMode } from "../../support/compiled-binding.js";
|
|
7
|
-
import { buildRequestRuntimeSnapshot } from "./inspection.js";
|
|
8
|
-
async function appendUserMessage(runtime, sessionId, message) {
|
|
9
|
-
await runtime.persistence.appendSessionMessage?.(sessionId, message);
|
|
10
|
-
}
|
|
11
|
-
async function saveRequestInput(runtime, sessionId, requestId, requestInput) {
|
|
12
|
-
await runtime.persistence.saveRequestInput?.(sessionId, requestId, requestInput);
|
|
13
|
-
}
|
|
14
|
-
async function createSessionRecord(runtime, sessionId, input) {
|
|
15
|
-
await runtime.persistence.createSession({
|
|
16
|
-
sessionId,
|
|
17
|
-
agentId: input.agentId,
|
|
18
|
-
requestId: input.requestId,
|
|
19
|
-
status: input.status,
|
|
20
|
-
createdAt: input.createdAt,
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
async function createRequestRecord(runtime, sessionId, requestId, input) {
|
|
24
|
-
await runtime.persistence.createRequest({
|
|
25
|
-
sessionId,
|
|
26
|
-
requestId,
|
|
27
|
-
...input,
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
export async function ensureSessionStarted(runtime, input) {
|
|
31
|
-
const { selectedAgentId, binding, message, requestInput, existingSessionId } = input;
|
|
32
|
-
const sessionId = existingSessionId ?? createPersistentId();
|
|
33
|
-
const requestId = createPersistentId();
|
|
34
|
-
const createdAt = new Date().toISOString();
|
|
35
|
-
const isNewSession = !existingSessionId;
|
|
36
|
-
const startedAt = createdAt;
|
|
37
|
-
const currentAgentId = selectedAgentId;
|
|
38
|
-
const delegationChain = [selectedAgentId];
|
|
39
|
-
const runtimeSnapshot = buildRequestRuntimeSnapshot(binding, { requestId });
|
|
40
|
-
const userMessage = {
|
|
41
|
-
role: "user",
|
|
42
|
-
content: normalizeMessageContent(message),
|
|
43
|
-
requestId,
|
|
44
|
-
createdAt,
|
|
45
|
-
};
|
|
46
|
-
if (typeof runtime.persistence.bootstrapRequest === "function") {
|
|
47
|
-
await runtime.persistence.bootstrapRequest({
|
|
48
|
-
sessionId,
|
|
49
|
-
agentId: binding.agent.id,
|
|
50
|
-
requestId,
|
|
51
|
-
status: "running",
|
|
52
|
-
createdAt,
|
|
53
|
-
executionMode: getBindingRuntimeExecutionMode(binding),
|
|
54
|
-
adapterKind: getBindingAdapterKind(binding),
|
|
55
|
-
userMessage,
|
|
56
|
-
requestInput,
|
|
57
|
-
createSession: isNewSession,
|
|
58
|
-
startedAt,
|
|
59
|
-
currentAgentId,
|
|
60
|
-
delegationChain,
|
|
61
|
-
runtimeSnapshot,
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
else {
|
|
65
|
-
if (isNewSession) {
|
|
66
|
-
await createSessionRecord(runtime, sessionId, {
|
|
67
|
-
agentId: selectedAgentId,
|
|
68
|
-
requestId,
|
|
69
|
-
status: "running",
|
|
70
|
-
createdAt,
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
await Promise.all([
|
|
74
|
-
appendUserMessage(runtime, sessionId, userMessage),
|
|
75
|
-
createRequestRecord(runtime, sessionId, requestId, {
|
|
76
|
-
agentId: binding.agent.id,
|
|
77
|
-
executionMode: getBindingRuntimeExecutionMode(binding),
|
|
78
|
-
adapterKind: getBindingAdapterKind(binding),
|
|
79
|
-
createdAt,
|
|
80
|
-
startedAt,
|
|
81
|
-
currentAgentId,
|
|
82
|
-
delegationChain,
|
|
83
|
-
runtimeSnapshot,
|
|
84
|
-
}),
|
|
85
|
-
saveRequestInput(runtime, sessionId, requestId, requestInput),
|
|
86
|
-
]);
|
|
87
|
-
}
|
|
88
|
-
return { sessionId, requestId, createdAt, isNewSession };
|
|
89
|
-
}
|
|
90
|
-
export async function prepareRunStart(runtime, input) {
|
|
91
|
-
const { options, invocation, requestCreatedPayload } = input;
|
|
92
|
-
const selectedAgentId = await runtime.resolveSelectedAgentId(options.input, options.agentId, options.sessionId);
|
|
93
|
-
const binding = getRequiredWorkspaceBinding(runtime.workspace, selectedAgentId);
|
|
94
|
-
const policyDecision = runtime.policyEngine.evaluate(binding);
|
|
95
|
-
if (!policyDecision.allowed) {
|
|
96
|
-
throw new Error(`Policy evaluation blocked agent ${selectedAgentId}: ${policyDecision.reasons.join(", ")}`);
|
|
97
|
-
}
|
|
98
|
-
const priority = normalizeRequestPriority(options.priority);
|
|
99
|
-
const requestInput = buildPersistedRequestInput(options.input, invocation, priority);
|
|
100
|
-
const { sessionId, requestId, isNewSession } = await ensureSessionStarted(runtime, {
|
|
101
|
-
selectedAgentId,
|
|
102
|
-
binding,
|
|
103
|
-
message: options.input,
|
|
104
|
-
requestInput,
|
|
105
|
-
existingSessionId: options.sessionId,
|
|
106
|
-
});
|
|
107
|
-
return {
|
|
108
|
-
binding,
|
|
109
|
-
selectedAgentId,
|
|
110
|
-
priority,
|
|
111
|
-
sessionId,
|
|
112
|
-
requestId,
|
|
113
|
-
isNewSession,
|
|
114
|
-
requestCreatedEventPromise: runtime.emitRequestCreated(sessionId, requestId, requestCreatedPayload(binding, selectedAgentId)),
|
|
115
|
-
releaseRequestSlotPromise: runtime.acquireRequestSlot(sessionId, requestId, "running", priority),
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
export function defaultRequestedAgentId(agentId) {
|
|
119
|
-
return agentId ?? AUTO_AGENT_ID;
|
|
120
|
-
}
|
|
1
|
+
import{AUTO_AGENT_ID as w}from"../../../contracts/types.js";import{createPersistentId as f}from"../../../utils/id.js";import{normalizeMessageContent as m}from"../../../utils/message-content.js";import{buildPersistedRequestInput as y,normalizeRequestPriority as A}from"./helpers.js";import{getRequiredWorkspaceBinding as x}from"../bindings.js";import{getBindingAdapterKind as R,getBindingRuntimeExecutionMode as S}from"../../support/compiled-binding.js";import{buildRequestRuntimeSnapshot as P}from"./inspection.js";async function b(e,n,t){await e.persistence.appendSessionMessage?.(n,t)}async function v(e,n,t,s){await e.persistence.saveRequestInput?.(n,t,s)}async function M(e,n,t){await e.persistence.createSession({sessionId:n,agentId:t.agentId,requestId:t.requestId,status:t.status,createdAt:t.createdAt})}async function C(e,n,t,s){await e.persistence.createRequest({sessionId:n,requestId:t,...s})}async function E(e,n){const{selectedAgentId:t,binding:s,message:g,requestInput:a,existingSessionId:c}=n,i=c??f(),o=f(),r=new Date().toISOString(),d=!c,u=r,p=t,I=[t],l=P(s,{requestId:o}),q={role:"user",content:m(g),requestId:o,createdAt:r};return typeof e.persistence.bootstrapRequest=="function"?await e.persistence.bootstrapRequest({sessionId:i,agentId:s.agent.id,requestId:o,status:"running",createdAt:r,executionMode:S(s),adapterKind:R(s),userMessage:q,requestInput:a,createSession:d,startedAt:u,currentAgentId:p,delegationChain:I,runtimeSnapshot:l}):(d&&await M(e,i,{agentId:t,requestId:o,status:"running",createdAt:r}),await Promise.all([b(e,i,q),C(e,i,o,{agentId:s.agent.id,executionMode:S(s),adapterKind:R(s),createdAt:r,startedAt:u,currentAgentId:p,delegationChain:I,runtimeSnapshot:l}),v(e,i,o,a)])),{sessionId:i,requestId:o,createdAt:r,isNewSession:d}}async function O(e,n){const{options:t,invocation:s,requestCreatedPayload:g}=n,a=await e.resolveSelectedAgentId(t.input,t.agentId,t.sessionId),c=x(e.workspace,a),i=e.policyEngine.evaluate(c);if(!i.allowed)throw new Error(`Policy evaluation blocked agent ${a}: ${i.reasons.join(", ")}`);const o=A(t.priority),r=y(t.input,s,o),{sessionId:d,requestId:u,isNewSession:p}=await E(e,{selectedAgentId:a,binding:c,message:t.input,requestInput:r,existingSessionId:t.sessionId});return{binding:c,selectedAgentId:a,priority:o,sessionId:d,requestId:u,isNewSession:p,requestCreatedEventPromise:e.emitRequestCreated(d,u,g(c,a)),releaseRequestSlotPromise:e.acquireRequestSlot(d,u,"running",o)}}function T(e){return e??w}export{T as defaultRequestedAgentId,E as ensureSessionStarted,O as prepareRunStart};
|
|
@@ -1,69 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { traceStartupStage } from "../../startup-tracing.js";
|
|
3
|
-
export async function initializeHarnessRuntime(input) {
|
|
4
|
-
await traceStartupStage("runtime.initialize.persistence", () => input.persistence.initialize());
|
|
5
|
-
if (input.healthMonitor) {
|
|
6
|
-
const healthMonitorStartTask = traceStartupStage("runtime.initialize.healthMonitor", () => input.healthMonitor.start());
|
|
7
|
-
if (input.scheduleBackgroundTask) {
|
|
8
|
-
input.scheduleBackgroundTask(healthMonitorStartTask);
|
|
9
|
-
}
|
|
10
|
-
else {
|
|
11
|
-
await healthMonitorStartTask;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
export async function recoverStartupRequests(input) {
|
|
16
|
-
if (!input.recoveryConfig.enabled) {
|
|
17
|
-
return;
|
|
18
|
-
}
|
|
19
|
-
await input.reclaimExpiredClaimedRequests();
|
|
20
|
-
const sessions = await input.persistence.listSessions();
|
|
21
|
-
if (sessions.length === 0) {
|
|
22
|
-
return;
|
|
23
|
-
}
|
|
24
|
-
const context = input.createStartupRecoveryContext();
|
|
25
|
-
for (const session of sessions) {
|
|
26
|
-
if (await recoverQueuedStartupRun(context, session)) {
|
|
27
|
-
continue;
|
|
28
|
-
}
|
|
29
|
-
if (await recoverRunningStartupRun(context, session)) {
|
|
30
|
-
continue;
|
|
31
|
-
}
|
|
32
|
-
await recoverResumingStartupRun(context, session);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
export async function reclaimExpiredClaimedRequests(input, nowIso = new Date().toISOString()) {
|
|
36
|
-
const expiredRuns = await input.persistence.listExpiredClaimedRequests(nowIso);
|
|
37
|
-
if (expiredRuns.length === 0) {
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
const activeRequestCount = input.getActiveRequestSlots();
|
|
41
|
-
const maxConcurrentRuns = input.concurrencyConfig.maxConcurrentRuns;
|
|
42
|
-
for (const [index, claimedRequest] of expiredRuns.entries()) {
|
|
43
|
-
const sessionId = claimedRequest.sessionId;
|
|
44
|
-
const requestId = claimedRequest.requestId;
|
|
45
|
-
await input.persistence.releaseRequestClaim(requestId);
|
|
46
|
-
await input.setRequestStateAndEmit(sessionId, requestId, 100, "queued", {
|
|
47
|
-
previousState: "claimed",
|
|
48
|
-
});
|
|
49
|
-
await input.emit(sessionId, requestId, 101, "request.queued", {
|
|
50
|
-
queuePosition: activeRequestCount + index + 1,
|
|
51
|
-
activeRequestCount,
|
|
52
|
-
...(maxConcurrentRuns !== undefined ? { maxConcurrentRuns } : {}),
|
|
53
|
-
priority: claimedRequest.priority,
|
|
54
|
-
recoveryKind: "startup-claim-recovery",
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
export async function isStaleRunningRequest(input, session, nowMs = Date.now()) {
|
|
59
|
-
const control = await input.persistence.getRequestControl(session.latestRequestId);
|
|
60
|
-
const heartbeatAt = control?.heartbeatAt;
|
|
61
|
-
if (!heartbeatAt) {
|
|
62
|
-
return true;
|
|
63
|
-
}
|
|
64
|
-
const heartbeatAtMs = Date.parse(heartbeatAt);
|
|
65
|
-
if (!Number.isFinite(heartbeatAtMs)) {
|
|
66
|
-
return true;
|
|
67
|
-
}
|
|
68
|
-
return nowMs - heartbeatAtMs >= input.concurrencyConfig.heartbeatTimeoutMs;
|
|
69
|
-
}
|
|
1
|
+
import{recoverQueuedStartupRun as l,recoverResumingStartupRun as d,recoverRunningStartupRun as f}from"./recovery.js";import{traceStartupStage as u}from"../../startup-tracing.js";async function q(e){if(await u("runtime.initialize.persistence",()=>e.persistence.initialize()),e.healthMonitor){const t=u("runtime.initialize.healthMonitor",()=>e.healthMonitor.start());e.scheduleBackgroundTask?e.scheduleBackgroundTask(t):await t}}async function h(e){if(!e.recoveryConfig.enabled)return;await e.reclaimExpiredClaimedRequests();const t=await e.persistence.listSessions();if(t.length===0)return;const r=e.createStartupRecoveryContext();for(const i of t)await l(r,i)||await f(r,i)||await d(r,i)}async function S(e,t=new Date().toISOString()){const r=await e.persistence.listExpiredClaimedRequests(t);if(r.length===0)return;const i=e.getActiveRequestSlots(),s=e.concurrencyConfig.maxConcurrentRuns;for(const[n,a]of r.entries()){const c=a.sessionId,o=a.requestId;await e.persistence.releaseRequestClaim(o),await e.setRequestStateAndEmit(c,o,100,"queued",{previousState:"claimed"}),await e.emit(c,o,101,"request.queued",{queuePosition:i+n+1,activeRequestCount:i,...s!==void 0?{maxConcurrentRuns:s}:{},priority:a.priority,recoveryKind:"startup-claim-recovery"})}}async function g(e,t,r=Date.now()){const s=(await e.persistence.getRequestControl(t.latestRequestId))?.heartbeatAt;if(!s)return!0;const n=Date.parse(s);return Number.isFinite(n)?r-n>=e.concurrencyConfig.heartbeatTimeoutMs:!0}export{q as initializeHarnessRuntime,g as isStaleRunningRequest,S as reclaimExpiredClaimedRequests,h as recoverStartupRequests};
|