@librechat/agents 4.2.10 → 4.2.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/runtime/admission.cjs +135 -0
- package/dist/cjs/runtime/admission.cjs.map +1 -0
- package/dist/cjs/runtime/checkpoint.cjs +40 -0
- package/dist/cjs/runtime/checkpoint.cjs.map +1 -0
- package/dist/cjs/runtime/client.cjs +435 -0
- package/dist/cjs/runtime/client.cjs.map +1 -0
- package/dist/cjs/runtime/files.cjs +301 -0
- package/dist/cjs/runtime/files.cjs.map +1 -0
- package/dist/cjs/runtime/hostTools.cjs +165 -0
- package/dist/cjs/runtime/hostTools.cjs.map +1 -0
- package/dist/cjs/runtime/index.cjs +11 -0
- package/dist/cjs/runtime/model.cjs +303 -0
- package/dist/cjs/runtime/model.cjs.map +1 -0
- package/dist/cjs/runtime/peer.cjs +209 -0
- package/dist/cjs/runtime/peer.cjs.map +1 -0
- package/dist/cjs/runtime/policy.cjs +77 -0
- package/dist/cjs/runtime/policy.cjs.map +1 -0
- package/dist/cjs/runtime/recovery/client.cjs +172 -0
- package/dist/cjs/runtime/recovery/client.cjs.map +1 -0
- package/dist/cjs/runtime/recovery/log.cjs +47 -0
- package/dist/cjs/runtime/recovery/log.cjs.map +1 -0
- package/dist/cjs/runtime/recovery/redis.cjs +239 -0
- package/dist/cjs/runtime/recovery/redis.cjs.map +1 -0
- package/dist/cjs/runtime/recovery/server.cjs +190 -0
- package/dist/cjs/runtime/recovery/server.cjs.map +1 -0
- package/dist/cjs/runtime/recovery/socket.cjs +298 -0
- package/dist/cjs/runtime/recovery/socket.cjs.map +1 -0
- package/dist/cjs/runtime/resume.cjs +115 -0
- package/dist/cjs/runtime/resume.cjs.map +1 -0
- package/dist/cjs/runtime/sealed.cjs +107 -0
- package/dist/cjs/runtime/sealed.cjs.map +1 -0
- package/dist/cjs/runtime/server.cjs +415 -0
- package/dist/cjs/runtime/server.cjs.map +1 -0
- package/dist/cjs/runtime/snapshot.cjs +61 -0
- package/dist/cjs/runtime/snapshot.cjs.map +1 -0
- package/dist/cjs/runtime/types.cjs +20 -0
- package/dist/cjs/runtime/types.cjs.map +1 -0
- package/dist/cjs/runtime/wire.cjs +84 -0
- package/dist/cjs/runtime/wire.cjs.map +1 -0
- package/dist/esm/runtime/admission.mjs +133 -0
- package/dist/esm/runtime/admission.mjs.map +1 -0
- package/dist/esm/runtime/checkpoint.mjs +39 -0
- package/dist/esm/runtime/checkpoint.mjs.map +1 -0
- package/dist/esm/runtime/client.mjs +432 -0
- package/dist/esm/runtime/client.mjs.map +1 -0
- package/dist/esm/runtime/files.mjs +299 -0
- package/dist/esm/runtime/files.mjs.map +1 -0
- package/dist/esm/runtime/hostTools.mjs +164 -0
- package/dist/esm/runtime/hostTools.mjs.map +1 -0
- package/dist/esm/runtime/index.mjs +5 -0
- package/dist/esm/runtime/model.mjs +301 -0
- package/dist/esm/runtime/model.mjs.map +1 -0
- package/dist/esm/runtime/peer.mjs +209 -0
- package/dist/esm/runtime/peer.mjs.map +1 -0
- package/dist/esm/runtime/policy.mjs +75 -0
- package/dist/esm/runtime/policy.mjs.map +1 -0
- package/dist/esm/runtime/recovery/client.mjs +172 -0
- package/dist/esm/runtime/recovery/client.mjs.map +1 -0
- package/dist/esm/runtime/recovery/log.mjs +46 -0
- package/dist/esm/runtime/recovery/log.mjs.map +1 -0
- package/dist/esm/runtime/recovery/redis.mjs +239 -0
- package/dist/esm/runtime/recovery/redis.mjs.map +1 -0
- package/dist/esm/runtime/recovery/server.mjs +189 -0
- package/dist/esm/runtime/recovery/server.mjs.map +1 -0
- package/dist/esm/runtime/recovery/socket.mjs +297 -0
- package/dist/esm/runtime/recovery/socket.mjs.map +1 -0
- package/dist/esm/runtime/resume.mjs +111 -0
- package/dist/esm/runtime/resume.mjs.map +1 -0
- package/dist/esm/runtime/sealed.mjs +106 -0
- package/dist/esm/runtime/sealed.mjs.map +1 -0
- package/dist/esm/runtime/server.mjs +413 -0
- package/dist/esm/runtime/server.mjs.map +1 -0
- package/dist/esm/runtime/snapshot.mjs +60 -0
- package/dist/esm/runtime/snapshot.mjs.map +1 -0
- package/dist/esm/runtime/types.mjs +19 -0
- package/dist/esm/runtime/types.mjs.map +1 -0
- package/dist/esm/runtime/wire.mjs +80 -0
- package/dist/esm/runtime/wire.mjs.map +1 -0
- package/dist/types/agents/AgentContext.d.ts +4 -4
- package/dist/types/agents/projection.d.ts +1 -1
- package/dist/types/events.d.ts +2 -2
- package/dist/types/graphs/Graph.d.ts +13 -13
- package/dist/types/graphs/MultiAgentGraph.d.ts +2 -2
- package/dist/types/graphs/acceptedModelResponse.d.ts +1 -1
- package/dist/types/graphs/createGraph.d.ts +4 -4
- package/dist/types/graphs/graphFactory.d.ts +1 -1
- package/dist/types/graphs/handoff.d.ts +1 -1
- package/dist/types/harness/index.d.ts +2 -2
- package/dist/types/harness/model.d.ts +3 -3
- package/dist/types/harness/providerText.d.ts +2 -2
- package/dist/types/hitl/approvalReview.d.ts +1 -1
- package/dist/types/hitl/askUserQuestion.d.ts +1 -1
- package/dist/types/hitl/askUserQuestions.d.ts +1 -1
- package/dist/types/hitl/askUserQuestionsInterrupt.d.ts +1 -1
- package/dist/types/hooks/types.d.ts +2 -2
- package/dist/types/instrumentation.d.ts +1 -1
- package/dist/types/langfuse.d.ts +3 -3
- package/dist/types/langfuseConfig.d.ts +2 -2
- package/dist/types/langfuseRuntimeContext.d.ts +1 -1
- package/dist/types/langfuseRuntimeScope.d.ts +2 -2
- package/dist/types/langfuseSpanRegistry.d.ts +1 -1
- package/dist/types/langfuseToolOutputTracing.d.ts +3 -3
- package/dist/types/llm/anthropic/index.d.ts +1 -1
- package/dist/types/llm/bedrock/index.d.ts +1 -1
- package/dist/types/llm/bedrock/toolCache.d.ts +2 -2
- package/dist/types/llm/contextOverflowRecovery.d.ts +2 -2
- package/dist/types/llm/contextPressureMeter.d.ts +2 -2
- package/dist/types/llm/google/index.d.ts +1 -1
- package/dist/types/llm/init.d.ts +1 -1
- package/dist/types/llm/invoke.d.ts +9 -9
- package/dist/types/llm/mistral/index.d.ts +1 -1
- package/dist/types/llm/openai/index.d.ts +2 -2
- package/dist/types/llm/openrouter/index.d.ts +2 -2
- package/dist/types/llm/openrouter/toolCache.d.ts +2 -2
- package/dist/types/llm/prepareProviderRequest.d.ts +3 -3
- package/dist/types/llm/promptCacheTools.d.ts +1 -1
- package/dist/types/llm/providerRegistry.d.ts +1 -1
- package/dist/types/llm/providerTextBoundary.d.ts +2 -2
- package/dist/types/llm/providerTextControls.d.ts +1 -1
- package/dist/types/llm/providers.d.ts +2 -2
- package/dist/types/llm/request.d.ts +1 -1
- package/dist/types/llm/stream/chunkAdapters.d.ts +1 -1
- package/dist/types/llm/streamLimits.d.ts +1 -1
- package/dist/types/llm/truncation.d.ts +1 -1
- package/dist/types/llm/vertexai/index.d.ts +1 -1
- package/dist/types/messages/alternation.d.ts +1 -1
- package/dist/types/messages/anthropicToolCache.d.ts +2 -2
- package/dist/types/messages/assistantPhase.d.ts +3 -3
- package/dist/types/messages/budget.d.ts +1 -1
- package/dist/types/messages/cache.d.ts +1 -1
- package/dist/types/messages/contextPruning.d.ts +2 -2
- package/dist/types/messages/core.d.ts +1 -1
- package/dist/types/messages/fading.d.ts +1 -1
- package/dist/types/messages/format.d.ts +2 -2
- package/dist/types/messages/ids.d.ts +2 -2
- package/dist/types/messages/injected.d.ts +1 -1
- package/dist/types/messages/langchain.d.ts +1 -1
- package/dist/types/messages/prune.d.ts +4 -4
- package/dist/types/messages/recency.d.ts +1 -1
- package/dist/types/messages/toolResultTypes.d.ts +1 -1
- package/dist/types/openai/arguments.d.ts +1 -1
- package/dist/types/openai/index.d.ts +1 -1
- package/dist/types/openai/toolProjection.d.ts +1 -1
- package/dist/types/prompts/activityLabel.d.ts +2 -2
- package/dist/types/prompts/reasoningLabel.d.ts +1 -1
- package/dist/types/rerank/search.d.ts +1 -1
- package/dist/types/responses/index.d.ts +1 -1
- package/dist/types/run.d.ts +3 -3
- package/dist/types/runtime/admission.d.ts +30 -0
- package/dist/types/runtime/checkpoint.d.ts +14 -0
- package/dist/types/runtime/client.d.ts +12 -0
- package/dist/types/runtime/files.d.ts +75 -0
- package/dist/types/runtime/hostTools.d.ts +22 -0
- package/dist/types/runtime/identity.d.ts +2 -0
- package/dist/types/runtime/index.d.ts +12 -0
- package/dist/types/runtime/model.d.ts +42 -0
- package/dist/types/runtime/peer.d.ts +55 -0
- package/dist/types/runtime/policy.d.ts +43 -0
- package/dist/types/runtime/recovery/client.d.ts +12 -0
- package/dist/types/runtime/recovery/journal.d.ts +62 -0
- package/dist/types/runtime/recovery/log.d.ts +9 -0
- package/dist/types/runtime/recovery/redis.d.ts +17 -0
- package/dist/types/runtime/recovery/server.d.ts +37 -0
- package/dist/types/runtime/recovery/socket.d.ts +72 -0
- package/dist/types/runtime/resume.d.ts +66 -0
- package/dist/types/runtime/sealed.d.ts +19 -0
- package/dist/types/runtime/server.d.ts +21 -0
- package/dist/types/runtime/snapshot.d.ts +12 -0
- package/dist/types/runtime/types.d.ts +125 -0
- package/dist/types/runtime/wire.d.ts +3 -0
- package/dist/types/session/handlers.d.ts +1 -1
- package/dist/types/session/types.d.ts +1 -1
- package/dist/types/stream.d.ts +3 -3
- package/dist/types/summarization/index.d.ts +1 -1
- package/dist/types/summarization/input.d.ts +1 -1
- package/dist/types/summarization/node.d.ts +6 -6
- package/dist/types/summarization/retry.d.ts +2 -2
- package/dist/types/summarization/semanticIndex.d.ts +2 -2
- package/dist/types/summarization/toolBudget.d.ts +1 -1
- package/dist/types/summarization/usage.d.ts +1 -1
- package/dist/types/tools/ArtifactDelivery.d.ts +1 -1
- package/dist/types/tools/ArtifactTruncation.d.ts +1 -1
- package/dist/types/tools/BashExecutor.d.ts +2 -2
- package/dist/types/tools/BashProgrammaticToolCalling.d.ts +2 -2
- package/dist/types/tools/CallerCapabilities.d.ts +1 -1
- package/dist/types/tools/CodeExecutor.d.ts +4 -4
- package/dist/types/tools/CodeSessionFileSummary.d.ts +1 -1
- package/dist/types/tools/ProgrammaticCallerPolicy.d.ts +1 -1
- package/dist/types/tools/ProgrammaticToolCalling.d.ts +2 -2
- package/dist/types/tools/ReadFile.d.ts +1 -1
- package/dist/types/tools/SkillTool.d.ts +1 -1
- package/dist/types/tools/SubagentTool.d.ts +3 -3
- package/dist/types/tools/ToolNode.d.ts +5 -5
- package/dist/types/tools/ToolSearch.d.ts +2 -2
- package/dist/types/tools/cloudflare/CloudflareBridgeRuntime.d.ts +1 -1
- package/dist/types/tools/cloudflare/CloudflareProgrammaticToolCalling.d.ts +1 -1
- package/dist/types/tools/cloudflare/CloudflareSandboxExecutionEngine.d.ts +2 -2
- package/dist/types/tools/cloudflare/CloudflareSandboxTools.d.ts +1 -1
- package/dist/types/tools/diagnostics.d.ts +1 -1
- package/dist/types/tools/eagerEventExecution.d.ts +1 -1
- package/dist/types/tools/handlers.d.ts +3 -3
- package/dist/types/tools/intentArg.d.ts +1 -1
- package/dist/types/tools/local/CompileCheckTool.d.ts +2 -2
- package/dist/types/tools/local/FileCheckpointer.d.ts +1 -1
- package/dist/types/tools/local/LocalCodingTools.d.ts +2 -2
- package/dist/types/tools/local/LocalExecutionEngine.d.ts +1 -1
- package/dist/types/tools/local/LocalExecutionTools.d.ts +1 -1
- package/dist/types/tools/local/LocalProgrammaticToolCalling.d.ts +1 -1
- package/dist/types/tools/local/bashAst.d.ts +1 -1
- package/dist/types/tools/local/resolveLocalExecutionTools.d.ts +1 -1
- package/dist/types/tools/local/syntaxCheck.d.ts +1 -1
- package/dist/types/tools/ptcTimeout.d.ts +1 -1
- package/dist/types/tools/runStepResume.d.ts +1 -1
- package/dist/types/tools/schema.d.ts +1 -1
- package/dist/types/tools/search/anthropic.d.ts +1 -1
- package/dist/types/tools/skillCatalog.d.ts +1 -1
- package/dist/types/tools/subagent/InMemorySubagentTaskStore.d.ts +1 -1
- package/dist/types/tools/subagent/SubagentExecutor.d.ts +5 -5
- package/dist/types/tools/subagent/SubagentReplay.d.ts +3 -3
- package/dist/types/tools/subagent/childGraphConfig.d.ts +2 -2
- package/dist/types/tools/subagent/diagnostics.d.ts +2 -2
- package/dist/types/tools/subagent/hostArgs.d.ts +2 -2
- package/dist/types/tools/toolBatchReplay.d.ts +3 -3
- package/dist/types/types/activityLabel.d.ts +2 -2
- package/dist/types/types/graph.d.ts +10 -10
- package/dist/types/types/llm.d.ts +9 -9
- package/dist/types/types/reasoningLabel.d.ts +1 -1
- package/dist/types/types/run.d.ts +8 -8
- package/dist/types/types/stream.d.ts +5 -5
- package/dist/types/types/summarize.d.ts +2 -2
- package/dist/types/types/tools.d.ts +8 -8
- package/dist/types/utils/errors.d.ts +1 -1
- package/dist/types/utils/events.d.ts +3 -3
- package/dist/types/utils/handlers.d.ts +3 -3
- package/dist/types/utils/llm.d.ts +2 -2
- package/dist/types/utils/llmConfig.d.ts +1 -1
- package/dist/types/utils/title.d.ts +1 -1
- package/dist/types/utils/toolSessions.d.ts +1 -1
- package/package.json +22 -3
- package/src/runtime/admission.ts +224 -0
- package/src/runtime/checkpoint.ts +66 -0
- package/src/runtime/client.ts +674 -0
- package/src/runtime/files.ts +503 -0
- package/src/runtime/hostTools.ts +243 -0
- package/src/runtime/identity.ts +3 -0
- package/src/runtime/index.ts +33 -0
- package/src/runtime/model.ts +452 -0
- package/src/runtime/peer.ts +262 -0
- package/src/runtime/policy.ts +150 -0
- package/src/runtime/recovery/client.ts +232 -0
- package/src/runtime/recovery/journal.ts +88 -0
- package/src/runtime/recovery/log.ts +70 -0
- package/src/runtime/recovery/redis.ts +342 -0
- package/src/runtime/recovery/server.ts +280 -0
- package/src/runtime/recovery/socket.ts +422 -0
- package/src/runtime/resume.ts +268 -0
- package/src/runtime/sealed.ts +144 -0
- package/src/runtime/server.ts +653 -0
- package/src/runtime/snapshot.ts +87 -0
- package/src/runtime/types.ts +184 -0
- package/src/runtime/wire.ts +96 -0
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import type {
|
|
3
|
+
RuntimeJournal,
|
|
4
|
+
RuntimeJournalFailure,
|
|
5
|
+
RuntimeJournalResult,
|
|
6
|
+
RuntimeJournalState,
|
|
7
|
+
} from './journal';
|
|
8
|
+
|
|
9
|
+
/** Operator-supplied client; no app singleton or credentials cross the runtime boundary. */
|
|
10
|
+
export interface RuntimeJournalRedis {
|
|
11
|
+
evalsha?(
|
|
12
|
+
sha: string,
|
|
13
|
+
numberOfKeys: number,
|
|
14
|
+
...args: Array<string | number>
|
|
15
|
+
): Promise<unknown>;
|
|
16
|
+
eval(
|
|
17
|
+
script: string,
|
|
18
|
+
numberOfKeys: number,
|
|
19
|
+
...args: Array<string | number>
|
|
20
|
+
): Promise<unknown>;
|
|
21
|
+
}
|
|
22
|
+
export interface RuntimeJournalLimits {
|
|
23
|
+
prefix: string;
|
|
24
|
+
retentionMs: number;
|
|
25
|
+
leaseMs: number;
|
|
26
|
+
maxFrames: number;
|
|
27
|
+
maxBytes: number;
|
|
28
|
+
maxBatchFrames: number;
|
|
29
|
+
maxBatchBytes: number;
|
|
30
|
+
}
|
|
31
|
+
const SCRIPT = `
|
|
32
|
+
local op, scope, owner = ARGV[1], ARGV[2], ARGV[3]
|
|
33
|
+
local t = redis.call('TIME')
|
|
34
|
+
local now = tonumber(t[1]) * 1000 + math.floor(tonumber(t[2]) / 1000)
|
|
35
|
+
local retention, lease = tonumber(ARGV[5]), tonumber(ARGV[6])
|
|
36
|
+
local function touch()
|
|
37
|
+
for _, key in ipairs(KEYS) do redis.call('PEXPIRE', key, retention) end
|
|
38
|
+
end
|
|
39
|
+
local function fail(code) return {0, code} end
|
|
40
|
+
if op == 'create' and redis.call('EXISTS', KEYS[1]) == 0 then
|
|
41
|
+
redis.call('HSET', KEYS[1], 'scope', scope, 'owner', owner, 'epoch', 1,
|
|
42
|
+
'until', now + lease, 'head', 0, 'ack', 0, 'bytes', 0, 'state', 'open', 'started', 0)
|
|
43
|
+
touch()
|
|
44
|
+
return {1, 1}
|
|
45
|
+
end
|
|
46
|
+
if redis.call('EXISTS', KEYS[1]) == 0 then return fail('missing') end
|
|
47
|
+
if redis.call('HGET', KEYS[1], 'scope') ~= scope then return fail('scope_mismatch') end
|
|
48
|
+
if op == 'purge' then redis.call('DEL', unpack(KEYS)); return {1} end
|
|
49
|
+
local state = redis.call('HGET', KEYS[1], 'state')
|
|
50
|
+
local head = tonumber(redis.call('HGET', KEYS[1], 'head'))
|
|
51
|
+
local ack = tonumber(redis.call('HGET', KEYS[1], 'ack'))
|
|
52
|
+
if op == 'read' then
|
|
53
|
+
local after, count = tonumber(ARGV[10]), tonumber(ARGV[11])
|
|
54
|
+
if after > head then return fail('cursor_ahead') end
|
|
55
|
+
local frames = redis.call('LRANGE', KEYS[2], after, after + count - 1)
|
|
56
|
+
return {1, state, redis.call('HGET', KEYS[1], 'started'), head, ack, frames}
|
|
57
|
+
end
|
|
58
|
+
-- A lost reply to a terminal append must remain retryable, but only by its original owner.
|
|
59
|
+
if op == 'append' then
|
|
60
|
+
local receipt = redis.call('HGET', KEYS[3], ARGV[10])
|
|
61
|
+
if receipt then
|
|
62
|
+
if redis.call('HGET', KEYS[1], 'owner') ~= owner or
|
|
63
|
+
tonumber(redis.call('HGET', KEYS[1], 'epoch')) ~= tonumber(ARGV[4]) then return fail('fenced') end
|
|
64
|
+
local decoded = cjson.decode(receipt)
|
|
65
|
+
if decoded[3] ~= ARGV[11] then return fail('batch_conflict') end
|
|
66
|
+
return {1, decoded[1], decoded[2]}
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
if state ~= 'open' then return fail('closed') end
|
|
70
|
+
local currentOwner = redis.call('HGET', KEYS[1], 'owner')
|
|
71
|
+
local epoch = tonumber(redis.call('HGET', KEYS[1], 'epoch'))
|
|
72
|
+
local expires = tonumber(redis.call('HGET', KEYS[1], 'until'))
|
|
73
|
+
if op == 'create' then
|
|
74
|
+
if currentOwner ~= owner or expires <= now then return fail('already_exists') end
|
|
75
|
+
return {1, epoch}
|
|
76
|
+
end
|
|
77
|
+
if op == 'claim' then
|
|
78
|
+
if currentOwner ~= owner and expires > now then return fail('fenced') end
|
|
79
|
+
if expires <= now or currentOwner ~= owner then epoch = epoch + 1 end
|
|
80
|
+
redis.call('HSET', KEYS[1], 'owner', owner, 'epoch', epoch, 'until', now + lease)
|
|
81
|
+
touch()
|
|
82
|
+
return {1, epoch, redis.call('HGET', KEYS[1], 'started')}
|
|
83
|
+
end
|
|
84
|
+
if currentOwner ~= owner or epoch ~= tonumber(ARGV[4]) or expires <= now then return fail('fenced') end
|
|
85
|
+
if op == 'renew' then
|
|
86
|
+
redis.call('HSET', KEYS[1], 'until', now + lease)
|
|
87
|
+
touch()
|
|
88
|
+
return {1}
|
|
89
|
+
end
|
|
90
|
+
if op == 'start' then
|
|
91
|
+
local started = redis.call('HGET', KEYS[1], 'started')
|
|
92
|
+
redis.call('HSET', KEYS[1], 'started', 1)
|
|
93
|
+
touch()
|
|
94
|
+
return {1, started == '0' and 1 or 0}
|
|
95
|
+
end
|
|
96
|
+
if op == 'append' then
|
|
97
|
+
local batch, digest = ARGV[10], ARGV[11]
|
|
98
|
+
local count, bytes = #ARGV - 11, 0
|
|
99
|
+
for i = 12, #ARGV do bytes = bytes + #ARGV[i] end
|
|
100
|
+
if head + count > tonumber(ARGV[7]) or
|
|
101
|
+
tonumber(redis.call('HGET', KEYS[1], 'bytes')) + bytes > tonumber(ARGV[8]) then return fail('limit') end
|
|
102
|
+
local first = head + 1
|
|
103
|
+
for i = 12, #ARGV do redis.call('RPUSH', KEYS[2], ARGV[i]) end
|
|
104
|
+
head = head + count
|
|
105
|
+
redis.call('HSET', KEYS[1], 'head', head)
|
|
106
|
+
redis.call('HINCRBY', KEYS[1], 'bytes', bytes)
|
|
107
|
+
redis.call('HSET', KEYS[3], batch, cjson.encode({first, head, digest}))
|
|
108
|
+
if ARGV[9] ~= '0' then redis.call('HSET', KEYS[1], 'state', ARGV[9], 'until', 0) end
|
|
109
|
+
touch()
|
|
110
|
+
return {1, first, head}
|
|
111
|
+
end
|
|
112
|
+
if op == 'ack' then
|
|
113
|
+
local cursor = tonumber(ARGV[10])
|
|
114
|
+
if cursor > head then return fail('cursor_ahead') end
|
|
115
|
+
ack = math.max(ack, cursor)
|
|
116
|
+
redis.call('HSET', KEYS[1], 'ack', ack)
|
|
117
|
+
touch()
|
|
118
|
+
return {1, ack}
|
|
119
|
+
end
|
|
120
|
+
if op == 'finish' then
|
|
121
|
+
if tonumber(ARGV[11]) ~= head then return fail('cursor_ahead') end
|
|
122
|
+
redis.call('HSET', KEYS[1], 'state', ARGV[10], 'until', 0)
|
|
123
|
+
touch()
|
|
124
|
+
return {1}
|
|
125
|
+
end
|
|
126
|
+
return fail('closed')
|
|
127
|
+
`;
|
|
128
|
+
const SCRIPT_SHA = createHash('sha1').update(SCRIPT).digest('hex');
|
|
129
|
+
const failures = new Set<RuntimeJournalFailure>([
|
|
130
|
+
'missing',
|
|
131
|
+
'scope_mismatch',
|
|
132
|
+
'fenced',
|
|
133
|
+
'closed',
|
|
134
|
+
'limit',
|
|
135
|
+
'cursor_ahead',
|
|
136
|
+
'batch_conflict',
|
|
137
|
+
'already_exists',
|
|
138
|
+
]);
|
|
139
|
+
function integer(value: unknown): number {
|
|
140
|
+
if (
|
|
141
|
+
typeof value !== 'number' &&
|
|
142
|
+
(typeof value !== 'string' || !/^[0-9]+$/.test(value))
|
|
143
|
+
)
|
|
144
|
+
throw new Error('Invalid runtime journal number');
|
|
145
|
+
const n = Number(value);
|
|
146
|
+
if (!Number.isSafeInteger(n) || n < 0)
|
|
147
|
+
throw new Error('Invalid runtime journal response');
|
|
148
|
+
return n;
|
|
149
|
+
}
|
|
150
|
+
function id(value: string): void {
|
|
151
|
+
if (typeof value !== 'string' || !/^[A-Za-z0-9_-]{1,128}$/.test(value))
|
|
152
|
+
throw new Error('Invalid runtime journal identity');
|
|
153
|
+
}
|
|
154
|
+
/** Redis persistence/AOF policy is deployment-owned; storage acknowledgement precedes publication. */
|
|
155
|
+
export function createRedisRuntimeJournal(
|
|
156
|
+
redis: RuntimeJournalRedis,
|
|
157
|
+
limits: RuntimeJournalLimits
|
|
158
|
+
): RuntimeJournal {
|
|
159
|
+
if (
|
|
160
|
+
!/^[A-Za-z0-9:_-]{1,128}$/.test(limits.prefix) ||
|
|
161
|
+
Object.entries(limits).some(
|
|
162
|
+
([key, value]) =>
|
|
163
|
+
key !== 'prefix' && (!Number.isSafeInteger(value) || Number(value) <= 0)
|
|
164
|
+
) ||
|
|
165
|
+
limits.retentionMs <= limits.leaseMs ||
|
|
166
|
+
limits.maxBatchFrames > limits.maxFrames ||
|
|
167
|
+
limits.maxBatchBytes > limits.maxBytes
|
|
168
|
+
) {
|
|
169
|
+
throw new Error('Invalid runtime journal limits');
|
|
170
|
+
}
|
|
171
|
+
const invoke = async <T>(
|
|
172
|
+
op: string,
|
|
173
|
+
session: { sessionId: string; scope: string },
|
|
174
|
+
owner: string,
|
|
175
|
+
epoch: number,
|
|
176
|
+
args: Array<string | number>,
|
|
177
|
+
map: (data: unknown[]) => T,
|
|
178
|
+
terminal?: Exclude<RuntimeJournalState, 'open'>
|
|
179
|
+
): Promise<RuntimeJournalResult<T>> => {
|
|
180
|
+
id(session.sessionId);
|
|
181
|
+
if (!/^[a-f0-9]{64}$/.test(session.scope))
|
|
182
|
+
throw new Error('Invalid runtime journal scope');
|
|
183
|
+
if (op !== 'read' && op !== 'purge') id(owner);
|
|
184
|
+
const key = `${limits.prefix}:{${session.sessionId}}`;
|
|
185
|
+
const arguments_: Array<string | number> = [
|
|
186
|
+
`${key}:meta`,
|
|
187
|
+
`${key}:frames`,
|
|
188
|
+
`${key}:receipts`,
|
|
189
|
+
op,
|
|
190
|
+
session.scope,
|
|
191
|
+
owner,
|
|
192
|
+
epoch,
|
|
193
|
+
limits.retentionMs,
|
|
194
|
+
limits.leaseMs,
|
|
195
|
+
limits.maxFrames,
|
|
196
|
+
limits.maxBytes,
|
|
197
|
+
terminal ?? 0,
|
|
198
|
+
...args,
|
|
199
|
+
];
|
|
200
|
+
let raw: unknown;
|
|
201
|
+
if (redis.evalsha) {
|
|
202
|
+
try {
|
|
203
|
+
raw = await redis.evalsha(SCRIPT_SHA, 3, ...arguments_);
|
|
204
|
+
} catch (error) {
|
|
205
|
+
// NOSCRIPT guarantees no execution. Never retry an ambiguous network failure.
|
|
206
|
+
if (!(error instanceof Error) || !/^NOSCRIPT\b/.test(error.message))
|
|
207
|
+
throw error;
|
|
208
|
+
raw = await redis.eval(SCRIPT, 3, ...arguments_);
|
|
209
|
+
}
|
|
210
|
+
} else raw = await redis.eval(SCRIPT, 3, ...arguments_);
|
|
211
|
+
if (!Array.isArray(raw))
|
|
212
|
+
throw new Error('Invalid runtime journal response');
|
|
213
|
+
if (raw[0] === 0 && failures.has(raw[1]))
|
|
214
|
+
return { ok: false, error: { code: raw[1] } };
|
|
215
|
+
if (raw[0] !== 1) throw new Error('Invalid runtime journal response');
|
|
216
|
+
return { ok: true, value: map(raw) };
|
|
217
|
+
};
|
|
218
|
+
return {
|
|
219
|
+
leaseMs: limits.leaseMs,
|
|
220
|
+
maxBatchFrames: limits.maxBatchFrames,
|
|
221
|
+
maxBatchBytes: limits.maxBatchBytes,
|
|
222
|
+
purge: (session) => invoke('purge', session, '', 0, [], () => null),
|
|
223
|
+
create: (session, owner) =>
|
|
224
|
+
invoke('create', session, owner, 0, [], (r) => ({
|
|
225
|
+
owner,
|
|
226
|
+
epoch: integer(r[1]),
|
|
227
|
+
})),
|
|
228
|
+
claim: (session, owner) =>
|
|
229
|
+
invoke('claim', session, owner, 0, [], (r) => ({
|
|
230
|
+
owner,
|
|
231
|
+
epoch: integer(r[1]),
|
|
232
|
+
started: r[2] === '1',
|
|
233
|
+
})),
|
|
234
|
+
renew: (session, lease) =>
|
|
235
|
+
invoke('renew', session, lease.owner, lease.epoch, [], () => null),
|
|
236
|
+
markStarted: (session, lease) =>
|
|
237
|
+
invoke('start', session, lease.owner, lease.epoch, [], (r) => ({
|
|
238
|
+
first: r[1] === 1,
|
|
239
|
+
})),
|
|
240
|
+
append: async (
|
|
241
|
+
session,
|
|
242
|
+
lease,
|
|
243
|
+
batchId,
|
|
244
|
+
payloads,
|
|
245
|
+
terminal
|
|
246
|
+
): Promise<RuntimeJournalResult<{ first: number; last: number }>> => {
|
|
247
|
+
id(batchId);
|
|
248
|
+
if (
|
|
249
|
+
!payloads.length ||
|
|
250
|
+
payloads.length > limits.maxBatchFrames ||
|
|
251
|
+
payloads.some((p) => typeof p !== 'string') ||
|
|
252
|
+
payloads.reduce((sum, p) => sum + Buffer.byteLength(p), 0) >
|
|
253
|
+
limits.maxBatchBytes
|
|
254
|
+
) {
|
|
255
|
+
return { ok: false, error: { code: 'limit' } };
|
|
256
|
+
}
|
|
257
|
+
if (
|
|
258
|
+
terminal != null &&
|
|
259
|
+
!['completed', 'failed', 'cancelled', 'paused'].includes(terminal)
|
|
260
|
+
)
|
|
261
|
+
throw new Error('Invalid runtime journal terminal state');
|
|
262
|
+
const digest = createHash('sha256')
|
|
263
|
+
.update(JSON.stringify([terminal ?? null, payloads]))
|
|
264
|
+
.digest('hex');
|
|
265
|
+
return invoke(
|
|
266
|
+
'append',
|
|
267
|
+
session,
|
|
268
|
+
lease.owner,
|
|
269
|
+
lease.epoch,
|
|
270
|
+
[batchId, digest, ...payloads],
|
|
271
|
+
(r) => ({ first: integer(r[1]), last: integer(r[2]) }),
|
|
272
|
+
terminal
|
|
273
|
+
);
|
|
274
|
+
},
|
|
275
|
+
acknowledge: (session, lease, sequence) =>
|
|
276
|
+
invoke(
|
|
277
|
+
'ack',
|
|
278
|
+
session,
|
|
279
|
+
lease.owner,
|
|
280
|
+
lease.epoch,
|
|
281
|
+
[integer(sequence)],
|
|
282
|
+
(r) => ({
|
|
283
|
+
acknowledged: integer(r[1]),
|
|
284
|
+
})
|
|
285
|
+
),
|
|
286
|
+
read: (
|
|
287
|
+
session,
|
|
288
|
+
after,
|
|
289
|
+
count
|
|
290
|
+
): Promise<
|
|
291
|
+
RuntimeJournalResult<{
|
|
292
|
+
state: RuntimeJournalState;
|
|
293
|
+
started: boolean;
|
|
294
|
+
head: number;
|
|
295
|
+
acknowledged: number;
|
|
296
|
+
frames: { sequence: number; payload: string }[];
|
|
297
|
+
}>
|
|
298
|
+
> => {
|
|
299
|
+
integer(after);
|
|
300
|
+
integer(count);
|
|
301
|
+
if (count === 0 || count > limits.maxBatchFrames)
|
|
302
|
+
throw new Error('Invalid runtime journal page limit');
|
|
303
|
+
return invoke('read', session, '', 0, [after, count], (r) => {
|
|
304
|
+
if (
|
|
305
|
+
!['open', 'completed', 'failed', 'cancelled', 'paused'].includes(
|
|
306
|
+
String(r[1])
|
|
307
|
+
)
|
|
308
|
+
)
|
|
309
|
+
throw new Error('Invalid runtime journal state');
|
|
310
|
+
if (!Array.isArray(r[5]) || r[5].some((p) => typeof p !== 'string'))
|
|
311
|
+
throw new Error('Invalid runtime journal frames');
|
|
312
|
+
return {
|
|
313
|
+
state: r[1] as RuntimeJournalState,
|
|
314
|
+
started: r[2] === '1',
|
|
315
|
+
head: integer(r[3]),
|
|
316
|
+
acknowledged: integer(r[4]),
|
|
317
|
+
frames: r[5].map((payload: string, index: number) => ({
|
|
318
|
+
sequence: after + index + 1,
|
|
319
|
+
payload,
|
|
320
|
+
})),
|
|
321
|
+
};
|
|
322
|
+
});
|
|
323
|
+
},
|
|
324
|
+
finish: (
|
|
325
|
+
session,
|
|
326
|
+
lease,
|
|
327
|
+
state,
|
|
328
|
+
head
|
|
329
|
+
): Promise<RuntimeJournalResult<null>> => {
|
|
330
|
+
if (!['completed', 'failed', 'cancelled', 'paused'].includes(state))
|
|
331
|
+
throw new Error('Invalid runtime journal terminal state');
|
|
332
|
+
return invoke(
|
|
333
|
+
'finish',
|
|
334
|
+
session,
|
|
335
|
+
lease.owner,
|
|
336
|
+
lease.epoch,
|
|
337
|
+
[state, integer(head)],
|
|
338
|
+
() => null
|
|
339
|
+
);
|
|
340
|
+
},
|
|
341
|
+
};
|
|
342
|
+
}
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
3
|
+
import type { WebSocket } from 'ws';
|
|
4
|
+
import type {
|
|
5
|
+
RuntimeJournal,
|
|
6
|
+
RuntimeJournalLease,
|
|
7
|
+
RuntimeJournalSession,
|
|
8
|
+
RuntimeJournalState,
|
|
9
|
+
} from './journal';
|
|
10
|
+
import type { SdkAgentIdentity } from '../identity';
|
|
11
|
+
import { createJournalRecoveryLog, journalValue } from './log';
|
|
12
|
+
import { RecoverySocket } from './socket';
|
|
13
|
+
|
|
14
|
+
export interface RecoveryClaim {
|
|
15
|
+
ownerId?: string;
|
|
16
|
+
sessionId: string;
|
|
17
|
+
after: number;
|
|
18
|
+
resume: boolean;
|
|
19
|
+
generation?: string;
|
|
20
|
+
}
|
|
21
|
+
export const recoveryClaimSchema: z.ZodType<RecoveryClaim> = z
|
|
22
|
+
.object({
|
|
23
|
+
ownerId: z.string().min(1).max(128).optional(),
|
|
24
|
+
sessionId: z.string().uuid(),
|
|
25
|
+
after: z.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER),
|
|
26
|
+
resume: z.boolean(),
|
|
27
|
+
generation: z.string().optional(),
|
|
28
|
+
})
|
|
29
|
+
.strict();
|
|
30
|
+
export interface RuntimeRecoveryOptions {
|
|
31
|
+
/** Stable operator identity used for direct reconnect routing. */
|
|
32
|
+
ownerId?: string;
|
|
33
|
+
journal: RuntimeJournal;
|
|
34
|
+
maxDisconnectMs?: number;
|
|
35
|
+
}
|
|
36
|
+
export interface RuntimeRecoverySession {
|
|
37
|
+
socket: RecoverySocket;
|
|
38
|
+
controller: AbortController;
|
|
39
|
+
needsEngine: boolean;
|
|
40
|
+
begin: () => Promise<void>;
|
|
41
|
+
complete: (state: Exclude<RuntimeJournalState, 'open'>) => Promise<void>;
|
|
42
|
+
attach: (socket: WebSocket, after: number) => void;
|
|
43
|
+
dispose: () => void;
|
|
44
|
+
}
|
|
45
|
+
/** One live execution owner; reconnect only replaces its wire. A restart never repeats a started Run. */
|
|
46
|
+
export function createRuntimeRecovery(
|
|
47
|
+
options: RuntimeRecoveryOptions & {
|
|
48
|
+
maxFrameBytes: number;
|
|
49
|
+
maxBufferedBytes: number;
|
|
50
|
+
onError?: (error: unknown) => void;
|
|
51
|
+
}
|
|
52
|
+
): {
|
|
53
|
+
prepare(
|
|
54
|
+
identity: Readonly<SdkAgentIdentity>,
|
|
55
|
+
threadId: string,
|
|
56
|
+
claim: RecoveryClaim
|
|
57
|
+
): Promise<RuntimeRecoverySession>;
|
|
58
|
+
close(): void;
|
|
59
|
+
} {
|
|
60
|
+
const sessions = new Map<
|
|
61
|
+
string,
|
|
62
|
+
{ scope: string; value: RuntimeRecoverySession }
|
|
63
|
+
>();
|
|
64
|
+
const pending = new Map<string, Promise<RuntimeRecoverySession>>();
|
|
65
|
+
let closed = false;
|
|
66
|
+
const isClosed = (): boolean => closed;
|
|
67
|
+
const graceMs = options.maxDisconnectMs ?? 15000;
|
|
68
|
+
if (
|
|
69
|
+
options.ownerId != null &&
|
|
70
|
+
(!options.ownerId.length || options.ownerId.length > 128)
|
|
71
|
+
)
|
|
72
|
+
throw new Error('Invalid SDK recovery owner identity');
|
|
73
|
+
if (
|
|
74
|
+
!Number.isSafeInteger(graceMs) ||
|
|
75
|
+
graceMs <= 0 ||
|
|
76
|
+
options.journal.leaseMs < 30
|
|
77
|
+
)
|
|
78
|
+
throw new Error('Invalid SDK recovery settings');
|
|
79
|
+
const report = (error: unknown): void => {
|
|
80
|
+
try {
|
|
81
|
+
options.onError?.(error);
|
|
82
|
+
} catch {
|
|
83
|
+
/* Trusted diagnostics only. */
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
const prepare = async (
|
|
87
|
+
identity: Readonly<SdkAgentIdentity>,
|
|
88
|
+
threadId: string,
|
|
89
|
+
claim: RecoveryClaim
|
|
90
|
+
): Promise<RuntimeRecoverySession> => {
|
|
91
|
+
if (isClosed()) throw new Error('SDK recovery closed');
|
|
92
|
+
if (claim.ownerId != null && claim.ownerId !== options.ownerId)
|
|
93
|
+
throw new Error('SDK recovery owner mismatch');
|
|
94
|
+
const scope = createHash('sha256')
|
|
95
|
+
.update(
|
|
96
|
+
JSON.stringify([
|
|
97
|
+
identity.runtimeId,
|
|
98
|
+
identity.agentId,
|
|
99
|
+
identity.runId,
|
|
100
|
+
identity.userId,
|
|
101
|
+
identity.tenantId ?? null,
|
|
102
|
+
identity.conversationId ?? null,
|
|
103
|
+
threadId,
|
|
104
|
+
claim.generation ?? null,
|
|
105
|
+
])
|
|
106
|
+
)
|
|
107
|
+
.digest('hex');
|
|
108
|
+
const existing = sessions.get(claim.sessionId);
|
|
109
|
+
if (existing) {
|
|
110
|
+
if (existing.scope !== scope || existing.value.socket.readyState !== 1)
|
|
111
|
+
throw new Error('SDK recovery identity mismatch');
|
|
112
|
+
return existing.value;
|
|
113
|
+
}
|
|
114
|
+
const session: RuntimeJournalSession = {
|
|
115
|
+
sessionId: claim.sessionId,
|
|
116
|
+
scope,
|
|
117
|
+
};
|
|
118
|
+
const journal = options.journal;
|
|
119
|
+
let lease: RuntimeJournalLease | undefined;
|
|
120
|
+
let page = await journal.read(session, 0, 1);
|
|
121
|
+
let needsEngine = false;
|
|
122
|
+
if (!page.ok) {
|
|
123
|
+
if (page.error.code !== 'missing' || claim.resume)
|
|
124
|
+
throw new Error('SDK recovery session unavailable');
|
|
125
|
+
lease = journalValue(await journal.create(session, randomUUID()));
|
|
126
|
+
page = await journal.read(session, 0, 1);
|
|
127
|
+
needsEngine = true;
|
|
128
|
+
} else if (page.value.state === 'open') {
|
|
129
|
+
const claimed = journalValue(await journal.claim(session, randomUUID()));
|
|
130
|
+
lease = claimed;
|
|
131
|
+
needsEngine = !claimed.started;
|
|
132
|
+
}
|
|
133
|
+
const initial = journalValue(page);
|
|
134
|
+
const log = createJournalRecoveryLog({
|
|
135
|
+
journal,
|
|
136
|
+
session,
|
|
137
|
+
lease,
|
|
138
|
+
head: initial.head,
|
|
139
|
+
});
|
|
140
|
+
if (!needsEngine && initial.state === 'open') {
|
|
141
|
+
// The previous process crossed the execution-start barrier. Its effects are uncertain.
|
|
142
|
+
// Replay committed output and fail; never invoke its factory again.
|
|
143
|
+
await log.append(
|
|
144
|
+
randomUUID(),
|
|
145
|
+
JSON.stringify({ kind: 'data', payload: '{"type":"error"}' })
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
const controller = new AbortController();
|
|
149
|
+
let deadline: ReturnType<typeof setTimeout> | undefined;
|
|
150
|
+
let heartbeat: ReturnType<typeof setInterval> | undefined;
|
|
151
|
+
let renewing = false;
|
|
152
|
+
let running = false;
|
|
153
|
+
let settled = false;
|
|
154
|
+
const expire = (): void => {
|
|
155
|
+
clearTimeout(deadline);
|
|
156
|
+
deadline = setTimeout(() => socket.terminate(), graceMs);
|
|
157
|
+
deadline.unref();
|
|
158
|
+
};
|
|
159
|
+
const socket = new RecoverySocket({
|
|
160
|
+
log,
|
|
161
|
+
maxFrameBytes: options.maxFrameBytes,
|
|
162
|
+
maxBufferedBytes: options.maxBufferedBytes,
|
|
163
|
+
terminalFailure:
|
|
164
|
+
initial.state === 'failed' || initial.state === 'cancelled',
|
|
165
|
+
onDisconnect: expire,
|
|
166
|
+
onFailure: (error): void => {
|
|
167
|
+
report(error);
|
|
168
|
+
controller.abort();
|
|
169
|
+
},
|
|
170
|
+
});
|
|
171
|
+
const dispose = (): void => {
|
|
172
|
+
clearTimeout(deadline);
|
|
173
|
+
clearInterval(heartbeat);
|
|
174
|
+
controller.abort();
|
|
175
|
+
socket.terminate();
|
|
176
|
+
if (sessions.get(claim.sessionId)?.value === value)
|
|
177
|
+
sessions.delete(claim.sessionId);
|
|
178
|
+
};
|
|
179
|
+
const value: RuntimeRecoverySession = {
|
|
180
|
+
socket,
|
|
181
|
+
controller,
|
|
182
|
+
needsEngine,
|
|
183
|
+
begin: async () => {
|
|
184
|
+
if (!lease || !needsEngine)
|
|
185
|
+
throw new Error('SDK recovery execution is already started');
|
|
186
|
+
const marker = journalValue(await journal.markStarted(session, lease));
|
|
187
|
+
if (!marker.first)
|
|
188
|
+
throw new Error('SDK recovery execution is already started');
|
|
189
|
+
running = true;
|
|
190
|
+
},
|
|
191
|
+
complete: async (state) => {
|
|
192
|
+
if (settled) return;
|
|
193
|
+
settled = true;
|
|
194
|
+
running = false;
|
|
195
|
+
await socket.flush();
|
|
196
|
+
if (
|
|
197
|
+
lease &&
|
|
198
|
+
journalValue(await journal.read(session, 0, 1)).state === 'open'
|
|
199
|
+
)
|
|
200
|
+
journalValue(await journal.finish(session, lease, state, log.head()));
|
|
201
|
+
clearInterval(heartbeat);
|
|
202
|
+
if (socket.readyState !== 1) dispose();
|
|
203
|
+
else expire();
|
|
204
|
+
},
|
|
205
|
+
attach: (wire, after) => {
|
|
206
|
+
clearTimeout(deadline);
|
|
207
|
+
const connection = randomUUID();
|
|
208
|
+
socket.attach(
|
|
209
|
+
wire,
|
|
210
|
+
connection,
|
|
211
|
+
after,
|
|
212
|
+
JSON.stringify({
|
|
213
|
+
version: 1,
|
|
214
|
+
kind: 'ready',
|
|
215
|
+
ownerId: options.ownerId,
|
|
216
|
+
connection,
|
|
217
|
+
sessionId: claim.sessionId,
|
|
218
|
+
received: socket.cursor,
|
|
219
|
+
})
|
|
220
|
+
);
|
|
221
|
+
if (settled || !needsEngine) expire();
|
|
222
|
+
},
|
|
223
|
+
dispose,
|
|
224
|
+
};
|
|
225
|
+
socket.once('close', () => {
|
|
226
|
+
controller.abort();
|
|
227
|
+
if (!running) dispose();
|
|
228
|
+
});
|
|
229
|
+
if (lease && needsEngine) {
|
|
230
|
+
heartbeat = setInterval(
|
|
231
|
+
() => {
|
|
232
|
+
if (renewing) return;
|
|
233
|
+
renewing = true;
|
|
234
|
+
void journal
|
|
235
|
+
.renew(session, lease!)
|
|
236
|
+
.then(journalValue)
|
|
237
|
+
.catch((error) => {
|
|
238
|
+
if (!settled) {
|
|
239
|
+
report(error);
|
|
240
|
+
dispose();
|
|
241
|
+
}
|
|
242
|
+
})
|
|
243
|
+
.finally(() => {
|
|
244
|
+
renewing = false;
|
|
245
|
+
});
|
|
246
|
+
},
|
|
247
|
+
Math.floor(journal.leaseMs / 3)
|
|
248
|
+
);
|
|
249
|
+
heartbeat.unref();
|
|
250
|
+
}
|
|
251
|
+
if (isClosed()) {
|
|
252
|
+
dispose();
|
|
253
|
+
throw new Error('SDK recovery closed');
|
|
254
|
+
}
|
|
255
|
+
sessions.set(claim.sessionId, { scope, value });
|
|
256
|
+
expire();
|
|
257
|
+
return value;
|
|
258
|
+
};
|
|
259
|
+
return {
|
|
260
|
+
async prepare(identity, threadId, claim): Promise<RuntimeRecoverySession> {
|
|
261
|
+
// Serialize admission by session, then recheck the caller's full scope.
|
|
262
|
+
const previous = pending.get(claim.sessionId);
|
|
263
|
+
const operation = (async (): Promise<RuntimeRecoverySession> => {
|
|
264
|
+
if (previous) await previous.catch(() => undefined);
|
|
265
|
+
return prepare(identity, threadId, claim);
|
|
266
|
+
})();
|
|
267
|
+
pending.set(claim.sessionId, operation);
|
|
268
|
+
try {
|
|
269
|
+
return await operation;
|
|
270
|
+
} finally {
|
|
271
|
+
if (pending.get(claim.sessionId) === operation)
|
|
272
|
+
pending.delete(claim.sessionId);
|
|
273
|
+
}
|
|
274
|
+
},
|
|
275
|
+
close: (): void => {
|
|
276
|
+
closed = true;
|
|
277
|
+
for (const entry of [...sessions.values()]) entry.value.dispose();
|
|
278
|
+
},
|
|
279
|
+
};
|
|
280
|
+
}
|