@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,758 +1,3 @@
|
|
|
1
|
-
import path from "node:path";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import { resolveRuntimeArtifactsRoot } from "../runtime/layout/runtime-layout.js";
|
|
5
|
-
function nowIso() {
|
|
6
|
-
return new Date(Date.now()).toISOString();
|
|
7
|
-
}
|
|
8
|
-
export class FilePersistence {
|
|
9
|
-
runtimeRoot;
|
|
10
|
-
constructor(runtimeRoot) {
|
|
11
|
-
this.runtimeRoot = runtimeRoot;
|
|
12
|
-
}
|
|
13
|
-
artifactsRoot() {
|
|
14
|
-
return resolveRuntimeArtifactsRoot(this.runtimeRoot);
|
|
15
|
-
}
|
|
16
|
-
sessionIndexPath(sessionId) {
|
|
17
|
-
return path.join(this.artifactsRoot(), "indexes", "sessions", `${sessionId}.json`);
|
|
18
|
-
}
|
|
19
|
-
requestIndexPath(requestId) {
|
|
20
|
-
return path.join(this.artifactsRoot(), "indexes", "requests", `${requestId}.json`);
|
|
21
|
-
}
|
|
22
|
-
approvalIndexPath(approvalId) {
|
|
23
|
-
return path.join(this.artifactsRoot(), "indexes", "approvals", `${approvalId}.json`);
|
|
24
|
-
}
|
|
25
|
-
requestQueuePath(requestId) {
|
|
26
|
-
return path.join(this.artifactsRoot(), "indexes", "requests-queue", `${requestId}.json`);
|
|
27
|
-
}
|
|
28
|
-
requestControlPath(requestId) {
|
|
29
|
-
return path.join(this.artifactsRoot(), "indexes", "requests-control", `${requestId}.json`);
|
|
30
|
-
}
|
|
31
|
-
traceItemsPath(sessionId, requestId) {
|
|
32
|
-
return path.join(this.requestDir(sessionId, requestId), "trace-items.ndjson");
|
|
33
|
-
}
|
|
34
|
-
requestPlanStatePath(sessionId, requestId) {
|
|
35
|
-
return path.join(this.requestDir(sessionId, requestId), "plan-state.json");
|
|
36
|
-
}
|
|
37
|
-
async writeSessionHead(sessionId, input) {
|
|
38
|
-
const sessionMetaPath = path.join(this.sessionDir(sessionId), "meta.json");
|
|
39
|
-
const sessionMeta = await readJson(sessionMetaPath);
|
|
40
|
-
await Promise.all([
|
|
41
|
-
writeJson(sessionMetaPath, {
|
|
42
|
-
...sessionMeta,
|
|
43
|
-
status: input.status,
|
|
44
|
-
latestRequestId: input.latestRequestId,
|
|
45
|
-
updatedAt: input.updatedAt,
|
|
46
|
-
}),
|
|
47
|
-
writeJson(this.sessionIndexPath(sessionId), {
|
|
48
|
-
sessionId,
|
|
49
|
-
status: input.status,
|
|
50
|
-
latestRequestId: input.latestRequestId,
|
|
51
|
-
updatedAt: input.updatedAt,
|
|
52
|
-
}),
|
|
53
|
-
]);
|
|
54
|
-
}
|
|
55
|
-
async initialize() {
|
|
56
|
-
await Promise.all([
|
|
57
|
-
"indexes/sessions",
|
|
58
|
-
"indexes/requests",
|
|
59
|
-
"indexes/approvals",
|
|
60
|
-
"indexes/requests-queue",
|
|
61
|
-
"indexes/requests-control",
|
|
62
|
-
"sessions",
|
|
63
|
-
].map((segment) => ensureDir(path.join(this.artifactsRoot(), segment))));
|
|
64
|
-
}
|
|
65
|
-
sessionDir(sessionId) {
|
|
66
|
-
return path.join(this.artifactsRoot(), "sessions", sessionId);
|
|
67
|
-
}
|
|
68
|
-
requestDir(sessionId, requestId) {
|
|
69
|
-
return path.join(this.sessionDir(sessionId), "requests", requestId);
|
|
70
|
-
}
|
|
71
|
-
async createSession(input) {
|
|
72
|
-
const meta = {
|
|
73
|
-
sessionId: input.sessionId,
|
|
74
|
-
workspaceId: "default",
|
|
75
|
-
entryAgentId: input.agentId,
|
|
76
|
-
createdAt: input.createdAt,
|
|
77
|
-
updatedAt: input.createdAt,
|
|
78
|
-
status: input.status,
|
|
79
|
-
latestRequestId: input.requestId,
|
|
80
|
-
};
|
|
81
|
-
await writeJson(path.join(this.sessionDir(input.sessionId), "meta.json"), meta);
|
|
82
|
-
await writeJson(path.join(this.sessionDir(input.sessionId), "messages.json"), {
|
|
83
|
-
sessionId: input.sessionId,
|
|
84
|
-
items: [],
|
|
85
|
-
});
|
|
86
|
-
await writeJson(this.sessionIndexPath(input.sessionId), {
|
|
87
|
-
sessionId: input.sessionId,
|
|
88
|
-
status: input.status,
|
|
89
|
-
latestRequestId: input.requestId,
|
|
90
|
-
updatedAt: input.createdAt,
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
async createRequest(input) {
|
|
94
|
-
const requestDir = this.requestDir(input.sessionId, input.requestId);
|
|
95
|
-
await ensureDir(path.join(requestDir, "events"));
|
|
96
|
-
await ensureDir(path.join(requestDir, "approvals"));
|
|
97
|
-
const sessionMeta = await this.getSessionMeta(input.sessionId);
|
|
98
|
-
const parentRequestId = sessionMeta?.latestRequestId === input.requestId
|
|
99
|
-
? null
|
|
100
|
-
: sessionMeta?.latestRequestId ?? null;
|
|
101
|
-
const meta = {
|
|
102
|
-
requestId: input.requestId,
|
|
103
|
-
sessionId: input.sessionId,
|
|
104
|
-
agentId: input.agentId,
|
|
105
|
-
parentRequestId,
|
|
106
|
-
executionMode: input.executionMode,
|
|
107
|
-
adapterKind: input.adapterKind ?? input.executionMode,
|
|
108
|
-
createdAt: input.createdAt,
|
|
109
|
-
updatedAt: input.createdAt,
|
|
110
|
-
};
|
|
111
|
-
const lifecycle = {
|
|
112
|
-
state: "running",
|
|
113
|
-
previousState: null,
|
|
114
|
-
stateEnteredAt: input.createdAt,
|
|
115
|
-
lastTransitionAt: input.createdAt,
|
|
116
|
-
resumable: false,
|
|
117
|
-
checkpointRef: null,
|
|
118
|
-
};
|
|
119
|
-
const inspection = {
|
|
120
|
-
startedAt: input.startedAt ?? input.createdAt,
|
|
121
|
-
endedAt: null,
|
|
122
|
-
lastActivityAt: input.createdAt,
|
|
123
|
-
currentAgentId: input.currentAgentId ?? input.agentId,
|
|
124
|
-
delegationChain: input.delegationChain ?? [input.currentAgentId ?? input.agentId],
|
|
125
|
-
runtimeSnapshot: input.runtimeSnapshot ?? null,
|
|
126
|
-
traceItems: [],
|
|
127
|
-
};
|
|
128
|
-
await Promise.all([
|
|
129
|
-
writeJson(path.join(requestDir, "meta.json"), meta),
|
|
130
|
-
writeJson(path.join(requestDir, "lifecycle.json"), lifecycle),
|
|
131
|
-
writeJson(path.join(requestDir, "inspection.json"), inspection),
|
|
132
|
-
writeFile(this.traceItemsPath(input.sessionId, input.requestId), "", "utf8"),
|
|
133
|
-
writeJson(path.join(requestDir, "checkpoint-ref.json"), {
|
|
134
|
-
sessionId: input.sessionId,
|
|
135
|
-
requestId: input.requestId,
|
|
136
|
-
checkpointRef: null,
|
|
137
|
-
savedAt: input.createdAt,
|
|
138
|
-
kind: "langgraph-checkpoint",
|
|
139
|
-
}),
|
|
140
|
-
writeJson(path.join(requestDir, "artifacts.json"), {
|
|
141
|
-
sessionId: input.sessionId,
|
|
142
|
-
requestId: input.requestId,
|
|
143
|
-
items: [],
|
|
144
|
-
}),
|
|
145
|
-
writeJson(path.join(requestDir, "error.json"), {
|
|
146
|
-
hasError: false,
|
|
147
|
-
lastError: null,
|
|
148
|
-
}),
|
|
149
|
-
writeJson(this.requestIndexPath(input.requestId), {
|
|
150
|
-
requestId: input.requestId,
|
|
151
|
-
sessionId: input.sessionId,
|
|
152
|
-
state: "running",
|
|
153
|
-
resumable: false,
|
|
154
|
-
updatedAt: input.createdAt,
|
|
155
|
-
}),
|
|
156
|
-
writeJson(this.requestControlPath(input.requestId), {
|
|
157
|
-
requestId: input.requestId,
|
|
158
|
-
cancelRequested: false,
|
|
159
|
-
cancelReason: null,
|
|
160
|
-
cancelRequestedAt: null,
|
|
161
|
-
heartbeatAt: null,
|
|
162
|
-
workerId: null,
|
|
163
|
-
workerStartedAt: null,
|
|
164
|
-
}),
|
|
165
|
-
]);
|
|
166
|
-
await this.writeSessionHead(input.sessionId, {
|
|
167
|
-
latestRequestId: input.requestId,
|
|
168
|
-
status: "running",
|
|
169
|
-
updatedAt: input.createdAt,
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
async setRequestState(sessionId, requestId, state, checkpointRef) {
|
|
173
|
-
const lifecyclePath = path.join(this.requestDir(sessionId, requestId), "lifecycle.json");
|
|
174
|
-
const requestMetaPath = path.join(this.requestDir(sessionId, requestId), "meta.json");
|
|
175
|
-
const inspectionPath = path.join(this.requestDir(sessionId, requestId), "inspection.json");
|
|
176
|
-
const [lifecycle, requestMeta, inspection] = await Promise.all([
|
|
177
|
-
readJson(lifecyclePath),
|
|
178
|
-
readJson(requestMetaPath),
|
|
179
|
-
readJson(inspectionPath),
|
|
180
|
-
]);
|
|
181
|
-
const now = nowIso();
|
|
182
|
-
const next = {
|
|
183
|
-
state,
|
|
184
|
-
previousState: lifecycle.state,
|
|
185
|
-
stateEnteredAt: now,
|
|
186
|
-
lastTransitionAt: now,
|
|
187
|
-
resumable: state === "waiting_for_approval",
|
|
188
|
-
checkpointRef: checkpointRef ?? lifecycle.checkpointRef,
|
|
189
|
-
};
|
|
190
|
-
await Promise.all([
|
|
191
|
-
writeJson(lifecyclePath, next),
|
|
192
|
-
writeJson(requestMetaPath, {
|
|
193
|
-
...requestMeta,
|
|
194
|
-
updatedAt: now,
|
|
195
|
-
}),
|
|
196
|
-
writeJson(inspectionPath, {
|
|
197
|
-
...inspection,
|
|
198
|
-
endedAt: state === "completed" || state === "failed" || state === "cancelled" ? now : inspection.endedAt,
|
|
199
|
-
lastActivityAt: now,
|
|
200
|
-
}),
|
|
201
|
-
]);
|
|
202
|
-
if (checkpointRef !== undefined) {
|
|
203
|
-
await writeJson(path.join(this.requestDir(sessionId, requestId), "checkpoint-ref.json"), {
|
|
204
|
-
sessionId,
|
|
205
|
-
requestId,
|
|
206
|
-
checkpointRef,
|
|
207
|
-
savedAt: now,
|
|
208
|
-
kind: "langgraph-checkpoint",
|
|
209
|
-
});
|
|
210
|
-
}
|
|
211
|
-
await writeJson(this.requestIndexPath(requestId), {
|
|
212
|
-
requestId,
|
|
213
|
-
sessionId,
|
|
214
|
-
state,
|
|
215
|
-
resumable: next.resumable,
|
|
216
|
-
updatedAt: now,
|
|
217
|
-
});
|
|
218
|
-
await this.writeSessionHead(sessionId, {
|
|
219
|
-
latestRequestId: requestId,
|
|
220
|
-
status: state,
|
|
221
|
-
updatedAt: now,
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
async appendEvent(event) {
|
|
225
|
-
const sequenceId = String(event.sequence).padStart(6, "0");
|
|
226
|
-
const inspectionPath = path.join(this.requestDir(event.sessionId, event.requestId), "inspection.json");
|
|
227
|
-
const inspection = await readJson(inspectionPath);
|
|
228
|
-
await Promise.all([
|
|
229
|
-
writeJson(path.join(this.requestDir(event.sessionId, event.requestId), "events", `${sequenceId}.json`), event),
|
|
230
|
-
writeJson(inspectionPath, {
|
|
231
|
-
...inspection,
|
|
232
|
-
lastActivityAt: event.timestamp,
|
|
233
|
-
}),
|
|
234
|
-
]);
|
|
235
|
-
}
|
|
236
|
-
async listSessions(filter = {}) {
|
|
237
|
-
const sessionIndexDir = path.join(this.artifactsRoot(), "indexes", "sessions");
|
|
238
|
-
if (!(await fileExists(sessionIndexDir))) {
|
|
239
|
-
return [];
|
|
240
|
-
}
|
|
241
|
-
const entries = await readdir(sessionIndexDir);
|
|
242
|
-
const records = await Promise.all(entries.map(async (entry) => {
|
|
243
|
-
const index = await readJson(path.join(sessionIndexDir, entry));
|
|
244
|
-
const [meta, runInspection] = await Promise.all([
|
|
245
|
-
readJson(path.join(this.sessionDir(index.sessionId), "meta.json")),
|
|
246
|
-
readJson(path.join(this.requestDir(index.sessionId, index.latestRequestId), "inspection.json")).catch(() => null),
|
|
247
|
-
]);
|
|
248
|
-
return {
|
|
249
|
-
agentId: meta.entryAgentId,
|
|
250
|
-
sessionId: index.sessionId,
|
|
251
|
-
latestRequestId: index.latestRequestId,
|
|
252
|
-
createdAt: meta.createdAt,
|
|
253
|
-
updatedAt: index.updatedAt,
|
|
254
|
-
status: index.status,
|
|
255
|
-
currentAgentId: runInspection?.currentAgentId ?? undefined,
|
|
256
|
-
};
|
|
257
|
-
}));
|
|
258
|
-
return records
|
|
259
|
-
.filter((record) => !filter.agentId || record.agentId === filter.agentId)
|
|
260
|
-
.sort((a, b) => b.updatedAt.localeCompare(a.updatedAt));
|
|
261
|
-
}
|
|
262
|
-
async listSessionSummaries(filter = {}) {
|
|
263
|
-
const sessions = await this.listSessions(filter);
|
|
264
|
-
const summaries = await Promise.all(sessions.map(async (session) => {
|
|
265
|
-
const [meta, messageBundle] = await Promise.all([
|
|
266
|
-
readJson(path.join(this.sessionDir(session.sessionId), "meta.json")),
|
|
267
|
-
readJson(path.join(this.sessionDir(session.sessionId), "messages.json"))
|
|
268
|
-
.catch(() => ({ sessionId: session.sessionId, items: [] })),
|
|
269
|
-
]);
|
|
270
|
-
const items = messageBundle.items;
|
|
271
|
-
return {
|
|
272
|
-
...session,
|
|
273
|
-
entryAgentId: meta.entryAgentId,
|
|
274
|
-
messageCount: items.length,
|
|
275
|
-
firstMessage: items[0],
|
|
276
|
-
lastMessage: items.at(-1),
|
|
277
|
-
};
|
|
278
|
-
}));
|
|
279
|
-
return summaries.sort((a, b) => b.updatedAt.localeCompare(a.updatedAt));
|
|
280
|
-
}
|
|
281
|
-
async listRequestIndexes() {
|
|
282
|
-
const runIndexDir = path.join(this.artifactsRoot(), "indexes", "requests");
|
|
283
|
-
if (!(await fileExists(runIndexDir))) {
|
|
284
|
-
return [];
|
|
285
|
-
}
|
|
286
|
-
const entries = (await readdir(runIndexDir)).sort();
|
|
287
|
-
return Promise.all(entries.map((entry) => readJson(path.join(runIndexDir, entry))));
|
|
288
|
-
}
|
|
289
|
-
async readRequestSummary(sessionId, requestId) {
|
|
290
|
-
const requestDir = this.requestDir(sessionId, requestId);
|
|
291
|
-
const [meta, lifecycle, inspection] = await Promise.all([
|
|
292
|
-
readJson(path.join(requestDir, "meta.json")),
|
|
293
|
-
readJson(path.join(requestDir, "lifecycle.json")),
|
|
294
|
-
readJson(path.join(requestDir, "inspection.json")),
|
|
295
|
-
]);
|
|
296
|
-
return {
|
|
297
|
-
requestId: meta.requestId,
|
|
298
|
-
sessionId: meta.sessionId,
|
|
299
|
-
agentId: meta.agentId,
|
|
300
|
-
...(meta.parentRequestId ? { parentRequestId: meta.parentRequestId } : {}),
|
|
301
|
-
executionMode: meta.executionMode,
|
|
302
|
-
adapterKind: meta.adapterKind ?? meta.executionMode,
|
|
303
|
-
createdAt: meta.createdAt,
|
|
304
|
-
updatedAt: meta.updatedAt,
|
|
305
|
-
state: lifecycle.state,
|
|
306
|
-
checkpointRef: lifecycle.checkpointRef,
|
|
307
|
-
resumable: lifecycle.resumable,
|
|
308
|
-
startedAt: inspection.startedAt,
|
|
309
|
-
...(inspection.endedAt ? { endedAt: inspection.endedAt } : {}),
|
|
310
|
-
lastActivityAt: inspection.lastActivityAt,
|
|
311
|
-
...(inspection.currentAgentId ? { currentAgentId: inspection.currentAgentId } : {}),
|
|
312
|
-
delegationChain: inspection.delegationChain,
|
|
313
|
-
...(inspection.runtimeSnapshot ? { runtimeSnapshot: inspection.runtimeSnapshot } : {}),
|
|
314
|
-
};
|
|
315
|
-
}
|
|
316
|
-
async listRequests(filter = {}) {
|
|
317
|
-
const indexes = await this.listRequestIndexes();
|
|
318
|
-
const requests = await Promise.all(indexes.map((record) => this.readRequestSummary(record.sessionId, record.requestId)));
|
|
319
|
-
return requests
|
|
320
|
-
.filter((request) => {
|
|
321
|
-
if (filter.agentId && request.agentId !== filter.agentId) {
|
|
322
|
-
return false;
|
|
323
|
-
}
|
|
324
|
-
if (filter.sessionId && request.sessionId !== filter.sessionId) {
|
|
325
|
-
return false;
|
|
326
|
-
}
|
|
327
|
-
if (filter.state && request.state !== filter.state) {
|
|
328
|
-
return false;
|
|
329
|
-
}
|
|
330
|
-
return true;
|
|
331
|
-
})
|
|
332
|
-
.sort((left, right) => right.updatedAt.localeCompare(left.updatedAt));
|
|
333
|
-
}
|
|
334
|
-
async getRequest(requestId) {
|
|
335
|
-
const indexPath = this.requestIndexPath(requestId);
|
|
336
|
-
if (!(await fileExists(indexPath))) {
|
|
337
|
-
return null;
|
|
338
|
-
}
|
|
339
|
-
const index = await readJson(indexPath);
|
|
340
|
-
return this.readRequestSummary(index.sessionId, index.requestId);
|
|
341
|
-
}
|
|
342
|
-
async getSession(sessionId) {
|
|
343
|
-
const filePath = this.sessionIndexPath(sessionId);
|
|
344
|
-
if (!(await fileExists(filePath))) {
|
|
345
|
-
return null;
|
|
346
|
-
}
|
|
347
|
-
const [index, meta] = await Promise.all([
|
|
348
|
-
readJson(filePath),
|
|
349
|
-
readJson(path.join(this.sessionDir(sessionId), "meta.json")),
|
|
350
|
-
]);
|
|
351
|
-
const runInspection = await readJson(path.join(this.requestDir(sessionId, index.latestRequestId), "inspection.json")).catch(() => null);
|
|
352
|
-
return {
|
|
353
|
-
agentId: meta.entryAgentId,
|
|
354
|
-
sessionId,
|
|
355
|
-
latestRequestId: index.latestRequestId,
|
|
356
|
-
createdAt: meta.createdAt,
|
|
357
|
-
updatedAt: index.updatedAt,
|
|
358
|
-
status: index.status,
|
|
359
|
-
currentAgentId: runInspection?.currentAgentId ?? undefined,
|
|
360
|
-
};
|
|
361
|
-
}
|
|
362
|
-
async getSessionMeta(sessionId) {
|
|
363
|
-
const filePath = path.join(this.sessionDir(sessionId), "meta.json");
|
|
364
|
-
if (!(await fileExists(filePath))) {
|
|
365
|
-
return null;
|
|
366
|
-
}
|
|
367
|
-
return readJson(filePath);
|
|
368
|
-
}
|
|
369
|
-
async listSessionRequests(sessionId) {
|
|
370
|
-
const requestsDir = path.join(this.sessionDir(sessionId), "requests");
|
|
371
|
-
if (!(await fileExists(requestsDir))) {
|
|
372
|
-
return [];
|
|
373
|
-
}
|
|
374
|
-
const requestIds = (await readdir(requestsDir)).sort();
|
|
375
|
-
const requests = await Promise.all(requestIds.map(async (requestId) => this.readRequestSummary(sessionId, requestId)));
|
|
376
|
-
return requests.sort((left, right) => right.createdAt.localeCompare(left.createdAt));
|
|
377
|
-
}
|
|
378
|
-
async listRequestEvents(sessionId, requestId) {
|
|
379
|
-
const eventsDir = path.join(this.requestDir(sessionId, requestId), "events");
|
|
380
|
-
if (!(await fileExists(eventsDir))) {
|
|
381
|
-
return [];
|
|
382
|
-
}
|
|
383
|
-
const entries = (await readdir(eventsDir))
|
|
384
|
-
.filter((entry) => entry.endsWith(".json"))
|
|
385
|
-
.sort((left, right) => left.localeCompare(right));
|
|
386
|
-
return Promise.all(entries.map((entry) => readJson(path.join(eventsDir, entry))));
|
|
387
|
-
}
|
|
388
|
-
async listApprovals(filter = {}) {
|
|
389
|
-
const approvalsDir = path.join(this.artifactsRoot(), "indexes", "approvals");
|
|
390
|
-
if (!(await fileExists(approvalsDir))) {
|
|
391
|
-
return [];
|
|
392
|
-
}
|
|
393
|
-
const entries = (await readdir(approvalsDir)).sort();
|
|
394
|
-
const approvals = await Promise.all(entries.map((entry) => readJson(path.join(approvalsDir, entry))));
|
|
395
|
-
return approvals.filter((approval) => {
|
|
396
|
-
if (filter.status && approval.status !== filter.status) {
|
|
397
|
-
return false;
|
|
398
|
-
}
|
|
399
|
-
if (filter.sessionId && approval.sessionId !== filter.sessionId) {
|
|
400
|
-
return false;
|
|
401
|
-
}
|
|
402
|
-
if (filter.requestId && approval.requestId !== filter.requestId) {
|
|
403
|
-
return false;
|
|
404
|
-
}
|
|
405
|
-
return true;
|
|
406
|
-
});
|
|
407
|
-
}
|
|
408
|
-
async getApproval(approvalId) {
|
|
409
|
-
const approvalPath = this.approvalIndexPath(approvalId);
|
|
410
|
-
if (!(await fileExists(approvalPath))) {
|
|
411
|
-
return null;
|
|
412
|
-
}
|
|
413
|
-
return readJson(approvalPath);
|
|
414
|
-
}
|
|
415
|
-
async getRequestApprovals(sessionId, requestId) {
|
|
416
|
-
const approvalsDir = path.join(this.requestDir(sessionId, requestId), "approvals");
|
|
417
|
-
if (!(await fileExists(approvalsDir))) {
|
|
418
|
-
return [];
|
|
419
|
-
}
|
|
420
|
-
const entries = (await readdir(approvalsDir)).sort();
|
|
421
|
-
return Promise.all(entries.map((entry) => readJson(path.join(approvalsDir, entry))));
|
|
422
|
-
}
|
|
423
|
-
async getRequestMeta(sessionId, requestId) {
|
|
424
|
-
return readJson(path.join(this.requestDir(sessionId, requestId), "meta.json"));
|
|
425
|
-
}
|
|
426
|
-
async getRequestLifecycle(sessionId, requestId) {
|
|
427
|
-
return readJson(path.join(this.requestDir(sessionId, requestId), "lifecycle.json"));
|
|
428
|
-
}
|
|
429
|
-
async getRequestInspection(sessionId, requestId) {
|
|
430
|
-
const inspection = await readJson(path.join(this.requestDir(sessionId, requestId), "inspection.json"));
|
|
431
|
-
const traceItems = await this.listRequestTraceItems(sessionId, requestId);
|
|
432
|
-
return {
|
|
433
|
-
...inspection,
|
|
434
|
-
traceItems,
|
|
435
|
-
};
|
|
436
|
-
}
|
|
437
|
-
async updateRequestInspection(sessionId, requestId, patch) {
|
|
438
|
-
const inspectionPath = path.join(this.requestDir(sessionId, requestId), "inspection.json");
|
|
439
|
-
const current = await readJson(inspectionPath);
|
|
440
|
-
const traceItems = Array.isArray(current.traceItems)
|
|
441
|
-
? current.traceItems
|
|
442
|
-
: Array.isArray(current.upstreamEvents)
|
|
443
|
-
? current.upstreamEvents
|
|
444
|
-
: [];
|
|
445
|
-
await writeJson(inspectionPath, {
|
|
446
|
-
...current,
|
|
447
|
-
...(patch.endedAt !== undefined ? { endedAt: patch.endedAt } : {}),
|
|
448
|
-
...(patch.lastActivityAt ? { lastActivityAt: patch.lastActivityAt } : {}),
|
|
449
|
-
...(patch.currentAgentId !== undefined ? { currentAgentId: patch.currentAgentId } : {}),
|
|
450
|
-
...(patch.delegationChain ? { delegationChain: patch.delegationChain } : {}),
|
|
451
|
-
...(patch.runtimeSnapshot !== undefined ? { runtimeSnapshot: patch.runtimeSnapshot } : {}),
|
|
452
|
-
traceItems,
|
|
453
|
-
});
|
|
454
|
-
}
|
|
455
|
-
async appendRequestTraceItem(sessionId, requestId, item) {
|
|
456
|
-
await appendFile(this.traceItemsPath(sessionId, requestId), `${JSON.stringify(item)}\n`, "utf8");
|
|
457
|
-
}
|
|
458
|
-
async listRequestTraceItems(sessionId, requestId) {
|
|
459
|
-
const traceItemsPath = this.traceItemsPath(sessionId, requestId);
|
|
460
|
-
if (await fileExists(traceItemsPath)) {
|
|
461
|
-
const contents = await readFile(traceItemsPath, "utf8");
|
|
462
|
-
const traceItems = contents
|
|
463
|
-
.split("\n")
|
|
464
|
-
.map((line) => line.trim())
|
|
465
|
-
.filter((line) => line.length > 0)
|
|
466
|
-
.map((line) => JSON.parse(line));
|
|
467
|
-
if (traceItems.length > 0) {
|
|
468
|
-
return traceItems;
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
const inspection = await readJson(path.join(this.requestDir(sessionId, requestId), "inspection.json"));
|
|
472
|
-
return Array.isArray(inspection.traceItems)
|
|
473
|
-
? inspection.traceItems
|
|
474
|
-
: Array.isArray(inspection.upstreamEvents)
|
|
475
|
-
? inspection.upstreamEvents
|
|
476
|
-
: [];
|
|
477
|
-
}
|
|
478
|
-
async deleteSession(sessionId) {
|
|
479
|
-
const sessionDir = this.sessionDir(sessionId);
|
|
480
|
-
const sessionIndexPath = this.sessionIndexPath(sessionId);
|
|
481
|
-
if (!(await fileExists(sessionDir)) && !(await fileExists(sessionIndexPath))) {
|
|
482
|
-
return false;
|
|
483
|
-
}
|
|
484
|
-
const [runIndexes, approvals] = await Promise.all([
|
|
485
|
-
this.listRequestIndexes(),
|
|
486
|
-
this.listApprovals(),
|
|
487
|
-
]);
|
|
488
|
-
await Promise.all([
|
|
489
|
-
...runIndexes
|
|
490
|
-
.filter((record) => record.sessionId === sessionId)
|
|
491
|
-
.map((record) => rm(this.requestIndexPath(record.requestId), { force: true })),
|
|
492
|
-
...approvals
|
|
493
|
-
.filter((record) => record.sessionId === sessionId)
|
|
494
|
-
.map((record) => rm(this.approvalIndexPath(record.approvalId), { force: true })),
|
|
495
|
-
...runIndexes
|
|
496
|
-
.filter((record) => record.sessionId === sessionId)
|
|
497
|
-
.flatMap((record) => [rm(this.requestQueuePath(record.requestId), { force: true }), rm(this.requestControlPath(record.requestId), { force: true })]),
|
|
498
|
-
rm(sessionIndexPath, { force: true }),
|
|
499
|
-
rm(sessionDir, { recursive: true, force: true }),
|
|
500
|
-
]);
|
|
501
|
-
return true;
|
|
502
|
-
}
|
|
503
|
-
async saveRequestInput(sessionId, requestId, request) {
|
|
504
|
-
await writeJson(path.join(this.requestDir(sessionId, requestId), "request.json"), request);
|
|
505
|
-
}
|
|
506
|
-
async getRequestInput(sessionId, requestId) {
|
|
507
|
-
const requestPath = path.join(this.requestDir(sessionId, requestId), "request.json");
|
|
508
|
-
if (!(await fileExists(requestPath))) {
|
|
509
|
-
return null;
|
|
510
|
-
}
|
|
511
|
-
return readJson(requestPath);
|
|
512
|
-
}
|
|
513
|
-
async clearRequestInput(sessionId, requestId) {
|
|
514
|
-
const requestPath = path.join(this.requestDir(sessionId, requestId), "request.json");
|
|
515
|
-
if (await fileExists(requestPath)) {
|
|
516
|
-
await rm(requestPath, { force: true });
|
|
517
|
-
}
|
|
518
|
-
}
|
|
519
|
-
async createApproval(record) {
|
|
520
|
-
const sessionId = record.sessionId;
|
|
521
|
-
const requestId = record.requestId;
|
|
522
|
-
const normalized = {
|
|
523
|
-
...record,
|
|
524
|
-
sessionId,
|
|
525
|
-
requestId,
|
|
526
|
-
};
|
|
527
|
-
await Promise.all([
|
|
528
|
-
writeJson(path.join(this.requestDir(sessionId, requestId), "approvals", `${record.approvalId}.json`), normalized),
|
|
529
|
-
writeJson(this.approvalIndexPath(record.approvalId), normalized),
|
|
530
|
-
]);
|
|
531
|
-
}
|
|
532
|
-
async resolveApproval(sessionId, requestId, approvalId, status) {
|
|
533
|
-
const approvalPath = path.join(this.requestDir(sessionId, requestId), "approvals", `${approvalId}.json`);
|
|
534
|
-
const current = await readJson(approvalPath);
|
|
535
|
-
const updated = {
|
|
536
|
-
...current,
|
|
537
|
-
status,
|
|
538
|
-
resolvedAt: nowIso(),
|
|
539
|
-
};
|
|
540
|
-
await Promise.all([
|
|
541
|
-
writeJson(approvalPath, updated),
|
|
542
|
-
writeJson(this.approvalIndexPath(approvalId), updated),
|
|
543
|
-
]);
|
|
544
|
-
return updated;
|
|
545
|
-
}
|
|
546
|
-
async saveRequestPlanState(sessionId, requestId, planState) {
|
|
547
|
-
await writeJson(this.requestPlanStatePath(sessionId, requestId), planState);
|
|
548
|
-
}
|
|
549
|
-
async getRequestPlanState(sessionId, requestId) {
|
|
550
|
-
const planStatePath = this.requestPlanStatePath(sessionId, requestId);
|
|
551
|
-
if (!(await fileExists(planStatePath))) {
|
|
552
|
-
return null;
|
|
553
|
-
}
|
|
554
|
-
return readJson(planStatePath);
|
|
555
|
-
}
|
|
556
|
-
async clearRequestPlanState(sessionId, requestId) {
|
|
557
|
-
await rm(this.requestPlanStatePath(sessionId, requestId), { force: true });
|
|
558
|
-
}
|
|
559
|
-
async createRequestArtifact(sessionId, requestId, artifact, content) {
|
|
560
|
-
const requestDir = this.requestDir(sessionId, requestId);
|
|
561
|
-
await writeJson(path.join(requestDir, artifact.path), content);
|
|
562
|
-
const artifactsPath = path.join(requestDir, "artifacts.json");
|
|
563
|
-
const current = await readJson(artifactsPath);
|
|
564
|
-
current.items.push(artifact);
|
|
565
|
-
await writeJson(artifactsPath, current);
|
|
566
|
-
return artifact;
|
|
567
|
-
}
|
|
568
|
-
async listRequestArtifacts(sessionId, requestId) {
|
|
569
|
-
return readJson(path.join(this.requestDir(sessionId, requestId), "artifacts.json"));
|
|
570
|
-
}
|
|
571
|
-
async readRequestArtifact(sessionId, requestId, artifactPath) {
|
|
572
|
-
const filePath = path.join(this.requestDir(sessionId, requestId), artifactPath);
|
|
573
|
-
if (!(await fileExists(filePath))) {
|
|
574
|
-
return null;
|
|
575
|
-
}
|
|
576
|
-
return readJson(filePath);
|
|
577
|
-
}
|
|
578
|
-
async appendSessionMessage(sessionId, message) {
|
|
579
|
-
const messagesPath = path.join(this.sessionDir(sessionId), "messages.json");
|
|
580
|
-
const current = (await fileExists(messagesPath))
|
|
581
|
-
? await readJson(messagesPath)
|
|
582
|
-
: { sessionId, items: [] };
|
|
583
|
-
current.items.push(message);
|
|
584
|
-
await writeJson(messagesPath, {
|
|
585
|
-
sessionId: current.sessionId ?? sessionId,
|
|
586
|
-
items: current.items,
|
|
587
|
-
});
|
|
588
|
-
}
|
|
589
|
-
async listSessionMessages(sessionId, limit = 12) {
|
|
590
|
-
const messagesPath = path.join(this.sessionDir(sessionId), "messages.json");
|
|
591
|
-
if (!(await fileExists(messagesPath))) {
|
|
592
|
-
return [];
|
|
593
|
-
}
|
|
594
|
-
const current = await readJson(messagesPath);
|
|
595
|
-
return current.items.slice(-limit);
|
|
596
|
-
}
|
|
597
|
-
async saveRequestRecoveryIntent(sessionId, requestId, intent) {
|
|
598
|
-
await writeJson(path.join(this.requestDir(sessionId, requestId), "recovery-intent.json"), intent);
|
|
599
|
-
}
|
|
600
|
-
async getRequestRecoveryIntent(sessionId, requestId) {
|
|
601
|
-
const intentPath = path.join(this.requestDir(sessionId, requestId), "recovery-intent.json");
|
|
602
|
-
if (!(await fileExists(intentPath))) {
|
|
603
|
-
return null;
|
|
604
|
-
}
|
|
605
|
-
return readJson(intentPath);
|
|
606
|
-
}
|
|
607
|
-
async clearRequestRecoveryIntent(sessionId, requestId) {
|
|
608
|
-
const intentPath = path.join(this.requestDir(sessionId, requestId), "recovery-intent.json");
|
|
609
|
-
if (!(await fileExists(intentPath))) {
|
|
610
|
-
return;
|
|
611
|
-
}
|
|
612
|
-
await rm(intentPath, { force: true });
|
|
613
|
-
}
|
|
614
|
-
async enqueueRequest(input) {
|
|
615
|
-
const current = (await this.getQueuedRequest(input.requestId)) ?? {
|
|
616
|
-
requestId: input.requestId,
|
|
617
|
-
sessionId: input.sessionId,
|
|
618
|
-
priority: input.priority ?? 0,
|
|
619
|
-
queueKey: input.queueKey ?? null,
|
|
620
|
-
enqueuedAt: nowIso(),
|
|
621
|
-
availableAt: input.availableAt ?? nowIso(),
|
|
622
|
-
claimedBy: null,
|
|
623
|
-
claimedAt: null,
|
|
624
|
-
leaseExpiresAt: null,
|
|
625
|
-
attemptCount: 0,
|
|
626
|
-
lastError: null,
|
|
627
|
-
};
|
|
628
|
-
await writeJson(this.requestQueuePath(input.requestId), {
|
|
629
|
-
...current,
|
|
630
|
-
sessionId: input.sessionId,
|
|
631
|
-
priority: input.priority ?? current.priority,
|
|
632
|
-
queueKey: input.queueKey ?? current.queueKey,
|
|
633
|
-
availableAt: input.availableAt ?? current.availableAt,
|
|
634
|
-
claimedBy: null,
|
|
635
|
-
claimedAt: null,
|
|
636
|
-
leaseExpiresAt: null,
|
|
637
|
-
lastError: null,
|
|
638
|
-
});
|
|
639
|
-
}
|
|
640
|
-
async getQueuedRequest(requestId) {
|
|
641
|
-
const filePath = this.requestQueuePath(requestId);
|
|
642
|
-
if (!(await fileExists(filePath))) {
|
|
643
|
-
return null;
|
|
644
|
-
}
|
|
645
|
-
return readJson(filePath);
|
|
646
|
-
}
|
|
647
|
-
async claimQueuedRequest(input) {
|
|
648
|
-
const current = await this.getQueuedRequest(input.requestId);
|
|
649
|
-
if (!current) {
|
|
650
|
-
throw new Error(`Missing queued request ${input.requestId}`);
|
|
651
|
-
}
|
|
652
|
-
const claimedAt = input.claimedAt ?? nowIso();
|
|
653
|
-
const next = {
|
|
654
|
-
...current,
|
|
655
|
-
sessionId: input.sessionId,
|
|
656
|
-
claimedBy: input.workerId,
|
|
657
|
-
claimedAt,
|
|
658
|
-
leaseExpiresAt: input.leaseExpiresAt,
|
|
659
|
-
attemptCount: current.attemptCount + 1,
|
|
660
|
-
};
|
|
661
|
-
await writeJson(this.requestQueuePath(input.requestId), next);
|
|
662
|
-
await writeJson(this.requestControlPath(input.requestId), {
|
|
663
|
-
...(await this.getRequestControl(input.requestId) ?? {
|
|
664
|
-
requestId: input.requestId,
|
|
665
|
-
cancelRequested: false,
|
|
666
|
-
cancelReason: null,
|
|
667
|
-
cancelRequestedAt: null,
|
|
668
|
-
heartbeatAt: null,
|
|
669
|
-
workerId: null,
|
|
670
|
-
workerStartedAt: null,
|
|
671
|
-
}),
|
|
672
|
-
heartbeatAt: claimedAt,
|
|
673
|
-
workerId: input.workerId,
|
|
674
|
-
workerStartedAt: claimedAt,
|
|
675
|
-
});
|
|
676
|
-
return next;
|
|
677
|
-
}
|
|
678
|
-
async renewRequestLease(input) {
|
|
679
|
-
const current = await this.getQueuedRequest(input.requestId);
|
|
680
|
-
if (current) {
|
|
681
|
-
await writeJson(this.requestQueuePath(input.requestId), {
|
|
682
|
-
...current,
|
|
683
|
-
claimedBy: input.workerId,
|
|
684
|
-
claimedAt: current.claimedAt ?? input.heartbeatAt ?? nowIso(),
|
|
685
|
-
leaseExpiresAt: input.leaseExpiresAt,
|
|
686
|
-
});
|
|
687
|
-
}
|
|
688
|
-
await writeJson(this.requestControlPath(input.requestId), {
|
|
689
|
-
...(await this.getRequestControl(input.requestId) ?? {
|
|
690
|
-
requestId: input.requestId,
|
|
691
|
-
cancelRequested: false,
|
|
692
|
-
cancelReason: null,
|
|
693
|
-
cancelRequestedAt: null,
|
|
694
|
-
heartbeatAt: null,
|
|
695
|
-
workerId: null,
|
|
696
|
-
workerStartedAt: null,
|
|
697
|
-
}),
|
|
698
|
-
heartbeatAt: input.heartbeatAt ?? nowIso(),
|
|
699
|
-
workerId: input.workerId,
|
|
700
|
-
workerStartedAt: (await this.getRequestControl(input.requestId))?.workerStartedAt ?? input.heartbeatAt ?? nowIso(),
|
|
701
|
-
});
|
|
702
|
-
}
|
|
703
|
-
async releaseRequestClaim(requestId) {
|
|
704
|
-
await rm(this.requestQueuePath(requestId), { force: true });
|
|
705
|
-
const current = await this.getRequestControl(requestId);
|
|
706
|
-
if (!current) {
|
|
707
|
-
return;
|
|
708
|
-
}
|
|
709
|
-
await writeJson(this.requestControlPath(requestId), {
|
|
710
|
-
...current,
|
|
711
|
-
heartbeatAt: null,
|
|
712
|
-
workerId: null,
|
|
713
|
-
workerStartedAt: null,
|
|
714
|
-
});
|
|
715
|
-
}
|
|
716
|
-
async listExpiredClaimedRequests(cutoffIso) {
|
|
717
|
-
const queueDir = path.join(this.artifactsRoot(), "indexes", "requests-queue");
|
|
718
|
-
if (!(await fileExists(queueDir))) {
|
|
719
|
-
return [];
|
|
720
|
-
}
|
|
721
|
-
const entries = (await readdir(queueDir)).sort();
|
|
722
|
-
const records = await Promise.all(entries.map((entry) => readJson(path.join(queueDir, entry))));
|
|
723
|
-
return records.filter((record) => record.claimedBy && record.leaseExpiresAt && record.leaseExpiresAt <= cutoffIso);
|
|
724
|
-
}
|
|
725
|
-
async getRequestControl(requestId) {
|
|
726
|
-
const filePath = this.requestControlPath(requestId);
|
|
727
|
-
if (!(await fileExists(filePath))) {
|
|
728
|
-
return null;
|
|
729
|
-
}
|
|
730
|
-
return readJson(filePath);
|
|
731
|
-
}
|
|
732
|
-
async requestRequestCancel(requestId, reason) {
|
|
733
|
-
const current = await this.getRequestControl(requestId);
|
|
734
|
-
if (!current) {
|
|
735
|
-
throw new Error(`Missing request control for ${requestId}`);
|
|
736
|
-
}
|
|
737
|
-
const updated = {
|
|
738
|
-
...current,
|
|
739
|
-
cancelRequested: true,
|
|
740
|
-
cancelReason: reason ?? null,
|
|
741
|
-
cancelRequestedAt: nowIso(),
|
|
742
|
-
};
|
|
743
|
-
await writeJson(this.requestControlPath(requestId), updated);
|
|
744
|
-
return updated;
|
|
745
|
-
}
|
|
746
|
-
async clearRequestCancel(requestId) {
|
|
747
|
-
const current = await this.getRequestControl(requestId);
|
|
748
|
-
if (!current) {
|
|
749
|
-
return;
|
|
750
|
-
}
|
|
751
|
-
await writeJson(this.requestControlPath(requestId), {
|
|
752
|
-
...current,
|
|
753
|
-
cancelRequested: false,
|
|
754
|
-
cancelReason: null,
|
|
755
|
-
cancelRequestedAt: null,
|
|
756
|
-
});
|
|
757
|
-
}
|
|
758
|
-
}
|
|
1
|
+
import r from"node:path";import{appendFile as y,readFile as R,readdir as I,rm as d,writeFile as p}from"node:fs/promises";import{ensureDir as A,fileExists as l,readJson as o,writeJson as u}from"../utils/fs.js";import{resolveRuntimeArtifactsRoot as P}from"../runtime/layout/runtime-layout.js";function q(){return new Date(Date.now()).toISOString()}class C{runtimeRoot;constructor(e){this.runtimeRoot=e}artifactsRoot(){return P(this.runtimeRoot)}sessionIndexPath(e){return r.join(this.artifactsRoot(),"indexes","sessions",`${e}.json`)}requestIndexPath(e){return r.join(this.artifactsRoot(),"indexes","requests",`${e}.json`)}approvalIndexPath(e){return r.join(this.artifactsRoot(),"indexes","approvals",`${e}.json`)}requestQueuePath(e){return r.join(this.artifactsRoot(),"indexes","requests-queue",`${e}.json`)}requestControlPath(e){return r.join(this.artifactsRoot(),"indexes","requests-control",`${e}.json`)}traceItemsPath(e,t){return r.join(this.requestDir(e,t),"trace-items.ndjson")}requestPlanStatePath(e,t){return r.join(this.requestDir(e,t),"plan-state.json")}async writeSessionHead(e,t){const s=r.join(this.sessionDir(e),"meta.json"),a=await o(s);await Promise.all([u(s,{...a,status:t.status,latestRequestId:t.latestRequestId,updatedAt:t.updatedAt}),u(this.sessionIndexPath(e),{sessionId:e,status:t.status,latestRequestId:t.latestRequestId,updatedAt:t.updatedAt})])}async initialize(){await Promise.all(["indexes/sessions","indexes/requests","indexes/approvals","indexes/requests-queue","indexes/requests-control","sessions"].map(e=>A(r.join(this.artifactsRoot(),e))))}sessionDir(e){return r.join(this.artifactsRoot(),"sessions",e)}requestDir(e,t){return r.join(this.sessionDir(e),"requests",t)}async createSession(e){const t={sessionId:e.sessionId,workspaceId:"default",entryAgentId:e.agentId,createdAt:e.createdAt,updatedAt:e.createdAt,status:e.status,latestRequestId:e.requestId};await u(r.join(this.sessionDir(e.sessionId),"meta.json"),t),await u(r.join(this.sessionDir(e.sessionId),"messages.json"),{sessionId:e.sessionId,items:[]}),await u(this.sessionIndexPath(e.sessionId),{sessionId:e.sessionId,status:e.status,latestRequestId:e.requestId,updatedAt:e.createdAt})}async createRequest(e){const t=this.requestDir(e.sessionId,e.requestId);await A(r.join(t,"events")),await A(r.join(t,"approvals"));const s=await this.getSessionMeta(e.sessionId),a=s?.latestRequestId===e.requestId?null:s?.latestRequestId??null,n={requestId:e.requestId,sessionId:e.sessionId,agentId:e.agentId,parentRequestId:a,executionMode:e.executionMode,adapterKind:e.adapterKind??e.executionMode,createdAt:e.createdAt,updatedAt:e.createdAt},i={state:"running",previousState:null,stateEnteredAt:e.createdAt,lastTransitionAt:e.createdAt,resumable:!1,checkpointRef:null},c={startedAt:e.startedAt??e.createdAt,endedAt:null,lastActivityAt:e.createdAt,currentAgentId:e.currentAgentId??e.agentId,delegationChain:e.delegationChain??[e.currentAgentId??e.agentId],runtimeSnapshot:e.runtimeSnapshot??null,traceItems:[]};await Promise.all([u(r.join(t,"meta.json"),n),u(r.join(t,"lifecycle.json"),i),u(r.join(t,"inspection.json"),c),p(this.traceItemsPath(e.sessionId,e.requestId),"","utf8"),u(r.join(t,"checkpoint-ref.json"),{sessionId:e.sessionId,requestId:e.requestId,checkpointRef:null,savedAt:e.createdAt,kind:"langgraph-checkpoint"}),u(r.join(t,"artifacts.json"),{sessionId:e.sessionId,requestId:e.requestId,items:[]}),u(r.join(t,"error.json"),{hasError:!1,lastError:null}),u(this.requestIndexPath(e.requestId),{requestId:e.requestId,sessionId:e.sessionId,state:"running",resumable:!1,updatedAt:e.createdAt}),u(this.requestControlPath(e.requestId),{requestId:e.requestId,cancelRequested:!1,cancelReason:null,cancelRequestedAt:null,heartbeatAt:null,workerId:null,workerStartedAt:null})]),await this.writeSessionHead(e.sessionId,{latestRequestId:e.requestId,status:"running",updatedAt:e.createdAt})}async setRequestState(e,t,s,a){const n=r.join(this.requestDir(e,t),"lifecycle.json"),i=r.join(this.requestDir(e,t),"meta.json"),c=r.join(this.requestDir(e,t),"inspection.json"),[m,f,w]=await Promise.all([o(n),o(i),o(c)]),h=q(),j={state:s,previousState:m.state,stateEnteredAt:h,lastTransitionAt:h,resumable:s==="waiting_for_approval",checkpointRef:a??m.checkpointRef};await Promise.all([u(n,j),u(i,{...f,updatedAt:h}),u(c,{...w,endedAt:s==="completed"||s==="failed"||s==="cancelled"?h:w.endedAt,lastActivityAt:h})]),a!==void 0&&await u(r.join(this.requestDir(e,t),"checkpoint-ref.json"),{sessionId:e,requestId:t,checkpointRef:a,savedAt:h,kind:"langgraph-checkpoint"}),await u(this.requestIndexPath(t),{requestId:t,sessionId:e,state:s,resumable:j.resumable,updatedAt:h}),await this.writeSessionHead(e,{latestRequestId:t,status:s,updatedAt:h})}async appendEvent(e){const t=String(e.sequence).padStart(6,"0"),s=r.join(this.requestDir(e.sessionId,e.requestId),"inspection.json"),a=await o(s);await Promise.all([u(r.join(this.requestDir(e.sessionId,e.requestId),"events",`${t}.json`),e),u(s,{...a,lastActivityAt:e.timestamp})])}async listSessions(e={}){const t=r.join(this.artifactsRoot(),"indexes","sessions");if(!await l(t))return[];const s=await I(t);return(await Promise.all(s.map(async n=>{const i=await o(r.join(t,n)),[c,m]=await Promise.all([o(r.join(this.sessionDir(i.sessionId),"meta.json")),o(r.join(this.requestDir(i.sessionId,i.latestRequestId),"inspection.json")).catch(()=>null)]);return{agentId:c.entryAgentId,sessionId:i.sessionId,latestRequestId:i.latestRequestId,createdAt:c.createdAt,updatedAt:i.updatedAt,status:i.status,currentAgentId:m?.currentAgentId??void 0}}))).filter(n=>!e.agentId||n.agentId===e.agentId).sort((n,i)=>i.updatedAt.localeCompare(n.updatedAt))}async listSessionSummaries(e={}){const t=await this.listSessions(e);return(await Promise.all(t.map(async a=>{const[n,i]=await Promise.all([o(r.join(this.sessionDir(a.sessionId),"meta.json")),o(r.join(this.sessionDir(a.sessionId),"messages.json")).catch(()=>({sessionId:a.sessionId,items:[]}))]),c=i.items;return{...a,entryAgentId:n.entryAgentId,messageCount:c.length,firstMessage:c[0],lastMessage:c.at(-1)}}))).sort((a,n)=>n.updatedAt.localeCompare(a.updatedAt))}async listRequestIndexes(){const e=r.join(this.artifactsRoot(),"indexes","requests");if(!await l(e))return[];const t=(await I(e)).sort();return Promise.all(t.map(s=>o(r.join(e,s))))}async readRequestSummary(e,t){const s=this.requestDir(e,t),[a,n,i]=await Promise.all([o(r.join(s,"meta.json")),o(r.join(s,"lifecycle.json")),o(r.join(s,"inspection.json"))]);return{requestId:a.requestId,sessionId:a.sessionId,agentId:a.agentId,...a.parentRequestId?{parentRequestId:a.parentRequestId}:{},executionMode:a.executionMode,adapterKind:a.adapterKind??a.executionMode,createdAt:a.createdAt,updatedAt:a.updatedAt,state:n.state,checkpointRef:n.checkpointRef,resumable:n.resumable,startedAt:i.startedAt,...i.endedAt?{endedAt:i.endedAt}:{},lastActivityAt:i.lastActivityAt,...i.currentAgentId?{currentAgentId:i.currentAgentId}:{},delegationChain:i.delegationChain,...i.runtimeSnapshot?{runtimeSnapshot:i.runtimeSnapshot}:{}}}async listRequests(e={}){const t=await this.listRequestIndexes();return(await Promise.all(t.map(a=>this.readRequestSummary(a.sessionId,a.requestId)))).filter(a=>!(e.agentId&&a.agentId!==e.agentId||e.sessionId&&a.sessionId!==e.sessionId||e.state&&a.state!==e.state)).sort((a,n)=>n.updatedAt.localeCompare(a.updatedAt))}async getRequest(e){const t=this.requestIndexPath(e);if(!await l(t))return null;const s=await o(t);return this.readRequestSummary(s.sessionId,s.requestId)}async getSession(e){const t=this.sessionIndexPath(e);if(!await l(t))return null;const[s,a]=await Promise.all([o(t),o(r.join(this.sessionDir(e),"meta.json"))]),n=await o(r.join(this.requestDir(e,s.latestRequestId),"inspection.json")).catch(()=>null);return{agentId:a.entryAgentId,sessionId:e,latestRequestId:s.latestRequestId,createdAt:a.createdAt,updatedAt:s.updatedAt,status:s.status,currentAgentId:n?.currentAgentId??void 0}}async getSessionMeta(e){const t=r.join(this.sessionDir(e),"meta.json");return await l(t)?o(t):null}async listSessionRequests(e){const t=r.join(this.sessionDir(e),"requests");if(!await l(t))return[];const s=(await I(t)).sort();return(await Promise.all(s.map(async n=>this.readRequestSummary(e,n)))).sort((n,i)=>i.createdAt.localeCompare(n.createdAt))}async listRequestEvents(e,t){const s=r.join(this.requestDir(e,t),"events");if(!await l(s))return[];const a=(await I(s)).filter(n=>n.endsWith(".json")).sort((n,i)=>n.localeCompare(i));return Promise.all(a.map(n=>o(r.join(s,n))))}async listApprovals(e={}){const t=r.join(this.artifactsRoot(),"indexes","approvals");if(!await l(t))return[];const s=(await I(t)).sort();return(await Promise.all(s.map(n=>o(r.join(t,n))))).filter(n=>!(e.status&&n.status!==e.status||e.sessionId&&n.sessionId!==e.sessionId||e.requestId&&n.requestId!==e.requestId))}async getApproval(e){const t=this.approvalIndexPath(e);return await l(t)?o(t):null}async getRequestApprovals(e,t){const s=r.join(this.requestDir(e,t),"approvals");if(!await l(s))return[];const a=(await I(s)).sort();return Promise.all(a.map(n=>o(r.join(s,n))))}async getRequestMeta(e,t){return o(r.join(this.requestDir(e,t),"meta.json"))}async getRequestLifecycle(e,t){return o(r.join(this.requestDir(e,t),"lifecycle.json"))}async getRequestInspection(e,t){const s=await o(r.join(this.requestDir(e,t),"inspection.json")),a=await this.listRequestTraceItems(e,t);return{...s,traceItems:a}}async updateRequestInspection(e,t,s){const a=r.join(this.requestDir(e,t),"inspection.json"),n=await o(a),i=Array.isArray(n.traceItems)?n.traceItems:Array.isArray(n.upstreamEvents)?n.upstreamEvents:[];await u(a,{...n,...s.endedAt!==void 0?{endedAt:s.endedAt}:{},...s.lastActivityAt?{lastActivityAt:s.lastActivityAt}:{},...s.currentAgentId!==void 0?{currentAgentId:s.currentAgentId}:{},...s.delegationChain?{delegationChain:s.delegationChain}:{},...s.runtimeSnapshot!==void 0?{runtimeSnapshot:s.runtimeSnapshot}:{},traceItems:i})}async appendRequestTraceItem(e,t,s){await y(this.traceItemsPath(e,t),`${JSON.stringify(s)}
|
|
2
|
+
`,"utf8")}async listRequestTraceItems(e,t){const s=this.traceItemsPath(e,t);if(await l(s)){const i=(await R(s,"utf8")).split(`
|
|
3
|
+
`).map(c=>c.trim()).filter(c=>c.length>0).map(c=>JSON.parse(c));if(i.length>0)return i}const a=await o(r.join(this.requestDir(e,t),"inspection.json"));return Array.isArray(a.traceItems)?a.traceItems:Array.isArray(a.upstreamEvents)?a.upstreamEvents:[]}async deleteSession(e){const t=this.sessionDir(e),s=this.sessionIndexPath(e);if(!await l(t)&&!await l(s))return!1;const[a,n]=await Promise.all([this.listRequestIndexes(),this.listApprovals()]);return await Promise.all([...a.filter(i=>i.sessionId===e).map(i=>d(this.requestIndexPath(i.requestId),{force:!0})),...n.filter(i=>i.sessionId===e).map(i=>d(this.approvalIndexPath(i.approvalId),{force:!0})),...a.filter(i=>i.sessionId===e).flatMap(i=>[d(this.requestQueuePath(i.requestId),{force:!0}),d(this.requestControlPath(i.requestId),{force:!0})]),d(s,{force:!0}),d(t,{recursive:!0,force:!0})]),!0}async saveRequestInput(e,t,s){await u(r.join(this.requestDir(e,t),"request.json"),s)}async getRequestInput(e,t){const s=r.join(this.requestDir(e,t),"request.json");return await l(s)?o(s):null}async clearRequestInput(e,t){const s=r.join(this.requestDir(e,t),"request.json");await l(s)&&await d(s,{force:!0})}async createApproval(e){const t=e.sessionId,s=e.requestId,a={...e,sessionId:t,requestId:s};await Promise.all([u(r.join(this.requestDir(t,s),"approvals",`${e.approvalId}.json`),a),u(this.approvalIndexPath(e.approvalId),a)])}async resolveApproval(e,t,s,a){const n=r.join(this.requestDir(e,t),"approvals",`${s}.json`),c={...await o(n),status:a,resolvedAt:q()};return await Promise.all([u(n,c),u(this.approvalIndexPath(s),c)]),c}async saveRequestPlanState(e,t,s){await u(this.requestPlanStatePath(e,t),s)}async getRequestPlanState(e,t){const s=this.requestPlanStatePath(e,t);return await l(s)?o(s):null}async clearRequestPlanState(e,t){await d(this.requestPlanStatePath(e,t),{force:!0})}async createRequestArtifact(e,t,s,a){const n=this.requestDir(e,t);await u(r.join(n,s.path),a);const i=r.join(n,"artifacts.json"),c=await o(i);return c.items.push(s),await u(i,c),s}async listRequestArtifacts(e,t){return o(r.join(this.requestDir(e,t),"artifacts.json"))}async readRequestArtifact(e,t,s){const a=r.join(this.requestDir(e,t),s);return await l(a)?o(a):null}async appendSessionMessage(e,t){const s=r.join(this.sessionDir(e),"messages.json"),a=await l(s)?await o(s):{sessionId:e,items:[]};a.items.push(t),await u(s,{sessionId:a.sessionId??e,items:a.items})}async listSessionMessages(e,t=12){const s=r.join(this.sessionDir(e),"messages.json");return await l(s)?(await o(s)).items.slice(-t):[]}async saveRequestRecoveryIntent(e,t,s){await u(r.join(this.requestDir(e,t),"recovery-intent.json"),s)}async getRequestRecoveryIntent(e,t){const s=r.join(this.requestDir(e,t),"recovery-intent.json");return await l(s)?o(s):null}async clearRequestRecoveryIntent(e,t){const s=r.join(this.requestDir(e,t),"recovery-intent.json");await l(s)&&await d(s,{force:!0})}async enqueueRequest(e){const t=await this.getQueuedRequest(e.requestId)??{requestId:e.requestId,sessionId:e.sessionId,priority:e.priority??0,queueKey:e.queueKey??null,enqueuedAt:q(),availableAt:e.availableAt??q(),claimedBy:null,claimedAt:null,leaseExpiresAt:null,attemptCount:0,lastError:null};await u(this.requestQueuePath(e.requestId),{...t,sessionId:e.sessionId,priority:e.priority??t.priority,queueKey:e.queueKey??t.queueKey,availableAt:e.availableAt??t.availableAt,claimedBy:null,claimedAt:null,leaseExpiresAt:null,lastError:null})}async getQueuedRequest(e){const t=this.requestQueuePath(e);return await l(t)?o(t):null}async claimQueuedRequest(e){const t=await this.getQueuedRequest(e.requestId);if(!t)throw new Error(`Missing queued request ${e.requestId}`);const s=e.claimedAt??q(),a={...t,sessionId:e.sessionId,claimedBy:e.workerId,claimedAt:s,leaseExpiresAt:e.leaseExpiresAt,attemptCount:t.attemptCount+1};return await u(this.requestQueuePath(e.requestId),a),await u(this.requestControlPath(e.requestId),{...await this.getRequestControl(e.requestId)??{requestId:e.requestId,cancelRequested:!1,cancelReason:null,cancelRequestedAt:null,heartbeatAt:null,workerId:null,workerStartedAt:null},heartbeatAt:s,workerId:e.workerId,workerStartedAt:s}),a}async renewRequestLease(e){const t=await this.getQueuedRequest(e.requestId);t&&await u(this.requestQueuePath(e.requestId),{...t,claimedBy:e.workerId,claimedAt:t.claimedAt??e.heartbeatAt??q(),leaseExpiresAt:e.leaseExpiresAt}),await u(this.requestControlPath(e.requestId),{...await this.getRequestControl(e.requestId)??{requestId:e.requestId,cancelRequested:!1,cancelReason:null,cancelRequestedAt:null,heartbeatAt:null,workerId:null,workerStartedAt:null},heartbeatAt:e.heartbeatAt??q(),workerId:e.workerId,workerStartedAt:(await this.getRequestControl(e.requestId))?.workerStartedAt??e.heartbeatAt??q()})}async releaseRequestClaim(e){await d(this.requestQueuePath(e),{force:!0});const t=await this.getRequestControl(e);t&&await u(this.requestControlPath(e),{...t,heartbeatAt:null,workerId:null,workerStartedAt:null})}async listExpiredClaimedRequests(e){const t=r.join(this.artifactsRoot(),"indexes","requests-queue");if(!await l(t))return[];const s=(await I(t)).sort();return(await Promise.all(s.map(n=>o(r.join(t,n))))).filter(n=>n.claimedBy&&n.leaseExpiresAt&&n.leaseExpiresAt<=e)}async getRequestControl(e){const t=this.requestControlPath(e);return await l(t)?o(t):null}async requestRequestCancel(e,t){const s=await this.getRequestControl(e);if(!s)throw new Error(`Missing request control for ${e}`);const a={...s,cancelRequested:!0,cancelReason:t??null,cancelRequestedAt:q()};return await u(this.requestControlPath(e),a),a}async clearRequestCancel(e){const t=await this.getRequestControl(e);t&&await u(this.requestControlPath(e),{...t,cancelRequested:!1,cancelReason:null,cancelRequestedAt:null})}}export{C as FilePersistence};
|