@dsmm/dsmm 0.1.1 → 0.1.5
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 +26 -15
- package/agent-presets/dsmm-builder/agent.cordis.yml +14 -12
- package/agent-presets/dsmm-builder/preset.yml +1 -1
- package/agent-presets/dsmm-clarifier/agent.cordis.yml +4 -2
- package/agent-presets/dsmm-clarifier/preset.yml +1 -1
- package/agent-presets/dsmm-code-search/agent.cordis.yml +4 -2
- package/agent-presets/dsmm-code-search/preset.yml +1 -1
- package/agent-presets/dsmm-creative/agent.cordis.yml +3 -1
- package/agent-presets/dsmm-creative/preset.yml +1 -1
- package/agent-presets/dsmm-doc-search/agent.cordis.yml +4 -2
- package/agent-presets/dsmm-doc-search/preset.yml +1 -1
- package/agent-presets/dsmm-media-reader/agent.cordis.yml +4 -2
- package/agent-presets/dsmm-media-reader/preset.yml +1 -1
- package/agent-presets/dsmm-oracle/agent.cordis.yml +3 -1
- package/agent-presets/dsmm-oracle/preset.yml +1 -1
- package/agent-presets/dsmm-oracle-2nd/agent.cordis.yml +3 -1
- package/agent-presets/dsmm-oracle-2nd/preset.yml +1 -1
- package/agent-presets/dsmm-orchestrator/agent.cordis.yml +15 -13
- package/agent-presets/dsmm-orchestrator/preset.yml +2 -2
- package/agent-presets/dsmm-plan-critic/agent.cordis.yml +4 -2
- package/agent-presets/dsmm-plan-critic/preset.yml +1 -1
- package/agent-presets/dsmm-planner/agent.cordis.yml +4 -2
- package/agent-presets/dsmm-planner/preset.yml +1 -1
- package/agent-presets/dsmm-reviewer/agent.cordis.yml +4 -2
- package/agent-presets/dsmm-reviewer/preset.yml +1 -1
- package/docs/agent-presets.md +15 -5
- package/docs/compatibility.md +66 -11
- package/docs/migration-from-ocmm.md +8 -0
- package/docs/model-routing.md +25 -7
- package/docs/profiles.md +95 -0
- package/docs/releasing.md +66 -19
- package/docs/runtime-recovery.md +39 -14
- package/docs/settings-status.md +13 -5
- package/lib/client/ProfilesSection.d.ts +11 -0
- package/lib/client/ProfilesSection.js +66 -0
- package/lib/client/SessionProfiles.d.ts +19 -0
- package/lib/client/SessionProfiles.js +24 -0
- package/lib/client/StructuredEditor.d.ts +11 -0
- package/lib/client/StructuredEditor.js +74 -0
- package/lib/client/controller.d.ts +115 -0
- package/lib/client/controller.js +340 -0
- package/lib/client/index.d.ts +13 -0
- package/lib/client/index.js +54 -0
- package/lib/client/locales.d.ts +145 -0
- package/lib/client/locales.js +91 -0
- package/lib/client/session-labels.d.ts +8 -0
- package/lib/client/session-labels.js +13 -0
- package/lib/client/structured.d.ts +18 -0
- package/lib/client/structured.js +98 -0
- package/lib/client/styles.d.ts +3 -0
- package/lib/client/styles.js +36 -0
- package/lib/client.js +2825 -0
- package/lib/commands.d.ts +3 -3
- package/lib/commands.js +5 -4
- package/lib/dsh-types.d.ts +20 -0
- package/lib/guards.d.ts +2 -2
- package/lib/guards.js +3 -2
- package/lib/index.d.ts +12 -4
- package/lib/index.js +98 -20
- package/lib/mode.d.ts +2 -2
- package/lib/mode.js +1 -1
- package/lib/model-routing.d.ts +2 -2
- package/lib/model-routing.js +100 -15
- package/lib/preset-registry.js +5 -3
- package/lib/profile-remote.d.ts +37 -0
- package/lib/profile-remote.js +219 -0
- package/lib/profile-rpc.d.ts +35 -0
- package/lib/profile-rpc.js +187 -0
- package/lib/profile-runtime.d.ts +63 -0
- package/lib/profile-runtime.js +310 -0
- package/lib/profile-store.d.ts +60 -0
- package/lib/profile-store.js +588 -0
- package/lib/profile-types.d.ts +80 -0
- package/lib/profile-types.js +2 -0
- package/lib/profiles.d.ts +26 -0
- package/lib/profiles.js +269 -0
- package/lib/prompts.d.ts +2 -2
- package/lib/prompts.js +4 -4
- package/lib/role-providers.d.ts +2 -2
- package/lib/role-providers.js +49 -6
- package/lib/role-routing.d.ts +37 -3
- package/lib/role-routing.js +110 -35
- package/lib/role-subagents.d.ts +2 -2
- package/lib/role-subagents.js +6 -4
- package/lib/roles.d.ts +4 -0
- package/lib/roles.js +43 -27
- package/lib/routing-policy.d.ts +32 -0
- package/lib/routing-policy.js +92 -0
- package/lib/runtime-recovery.d.ts +2 -2
- package/lib/runtime-recovery.js +215 -54
- package/lib/session-metadata.d.ts +13 -0
- package/lib/session-metadata.js +34 -0
- package/lib/session-persistence.d.ts +35 -0
- package/lib/session-persistence.js +304 -0
- package/lib/settings.d.ts +34 -1
- package/lib/settings.js +33 -7
- package/lib/state.d.ts +1 -0
- package/lib/state.js +4 -0
- package/lib/status.d.ts +9 -1
- package/lib/status.js +34 -12
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +66 -15
- package/patches/agent-presets-root.example.cordis.patch.yml +6 -10
- package/prompts/deepseek-v4-pro.md +1 -1
- package/prompts/deepwork.md +3 -3
- package/scripts/repair-session-log.mjs +476 -0
- package/scripts/session-repair-native-verifier.mjs +367 -0
package/lib/runtime-recovery.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { createUserMessage } from "@deepseek-ai/dsh-llm";
|
|
2
|
-
import {
|
|
1
|
+
import { createUserMessage, expandAssistantStream } from "@deepseek-ai/dsh-llm";
|
|
2
|
+
import { foldDurableRecoveryWork, isCurrentRecoveryStep } from "./recovery-policy.js";
|
|
3
3
|
import { isDsmmRoleId } from "./roles.js";
|
|
4
|
-
import { applyModelRoute,
|
|
4
|
+
import { applyModelRoute, isUnavailableRouteFailure, latestNativeModelSelection, liveRolePolicyIdentity, recordAdmittedRecoveryRoute, roleRouteLock, sameModelRoute, selectInitialModelRoute } from "./role-routing.js";
|
|
5
5
|
import { childOwnedSessionEvents, resolveEffectiveDsmmRole, resolveSelectedAgentPreset, sessionEvents } from "./session-scope.js";
|
|
6
|
+
import { resolveRoleRuntimePolicy } from "./routing-policy.js";
|
|
6
7
|
const installedContexts = new WeakSet();
|
|
7
|
-
const RECOVERY_WARNING = "dsmm runtime recovery
|
|
8
|
+
const RECOVERY_WARNING = "dsmm runtime recovery refused an unproven or exhausted retry";
|
|
8
9
|
const CONTINUATION_WARNING = "dsmm runtime recovery continuation steering failed for turn";
|
|
9
10
|
function warnSafely(ctx, message) {
|
|
10
11
|
try {
|
|
@@ -15,27 +16,77 @@ function warnSafely(ctx, message) {
|
|
|
15
16
|
function recoveryEvents(agent) {
|
|
16
17
|
return agent.session.header?.origin === "subagent" ? childOwnedSessionEvents(agent.session) : sessionEvents(agent.session);
|
|
17
18
|
}
|
|
19
|
+
function isRecord(value) {
|
|
20
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
21
|
+
}
|
|
22
|
+
function isRateLimit(failure) {
|
|
23
|
+
if (["AUTH", "MISSING_CREDENTIAL", "INVALID_CREDENTIAL", "QUOTA", "ACCOUNT_QUOTA", "CONTEXT_WINDOW_EXCEEDED", "INVALID_ARGS", "CANCELLED", "ABORTED"].includes(failure.code) || failure.status === 402)
|
|
24
|
+
return false;
|
|
25
|
+
return failure.code === "RATE_LIMIT" || failure.status === 429;
|
|
26
|
+
}
|
|
27
|
+
function outputChunk(chunk) {
|
|
28
|
+
// Block boundaries and even empty fragments are protocol output, not proof of absence.
|
|
29
|
+
return chunk.type !== "usage" && chunk.type !== "finish";
|
|
30
|
+
}
|
|
31
|
+
function positiveNoOutput(frame, attempt, consumed = false) {
|
|
32
|
+
if (attempt === undefined || !attempt.valid || attempt.output || (attempt.consumed && !consumed) || attempt.failure !== frame.failure
|
|
33
|
+
|| attempt.turn !== frame.turn || attempt.step !== frame.step || attempt.eventType !== "assistant/attempt"
|
|
34
|
+
|| attempt.seq === undefined || attempt.route.provider !== frame.provider)
|
|
35
|
+
return false;
|
|
36
|
+
const events = recoveryEvents(frame.agent);
|
|
37
|
+
if (frame.agent.session.requestHeader?.() !== attempt.header)
|
|
38
|
+
return false;
|
|
39
|
+
const settlements = events.filter((event) => "seq" in event && event.seq === attempt.seq);
|
|
40
|
+
if (settlements.length !== 1)
|
|
41
|
+
return false;
|
|
42
|
+
const settlement = settlements[0];
|
|
43
|
+
if (settlement?.type !== "assistant/attempt" || !isRecord(settlement.data)
|
|
44
|
+
|| settlement.data.turn !== frame.turn || settlement.data.step !== frame.step || !Array.isArray(settlement.data.stream))
|
|
45
|
+
return false;
|
|
46
|
+
const later = events.slice(events.indexOf(settlement) + 1);
|
|
47
|
+
if (later.some((event) => ["assistant/attempt", "assistant/message", "tool/call", "tool/result", "request/header", "step/start", "step/end", "turn/end"].includes(event.type)))
|
|
48
|
+
return false;
|
|
49
|
+
try {
|
|
50
|
+
const chunks = expandAssistantStream(settlement.data.stream);
|
|
51
|
+
const finish = chunks.at(-1)?.chunk;
|
|
52
|
+
return chunks.length === attempt.chunks && chunks.every(({ chunk }) => !outputChunk(chunk))
|
|
53
|
+
&& finish?.type === "finish" && finish.reason.kind === "error"
|
|
54
|
+
&& JSON.stringify(finish.reason.failure) === JSON.stringify(frame.failure);
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
18
60
|
export function registerRuntimeRecovery(ctx, controller, getSettings) {
|
|
19
61
|
if (installedContexts.has(ctx) || ctx.on === undefined)
|
|
20
62
|
return;
|
|
21
63
|
installedContexts.add(ctx);
|
|
22
64
|
let pendingByAgent = new WeakMap();
|
|
23
|
-
let
|
|
65
|
+
let attemptsByAgent = new WeakMap();
|
|
24
66
|
let continuationsByAgent = new WeakMap();
|
|
67
|
+
const waits = new Set();
|
|
68
|
+
let active = true;
|
|
25
69
|
let disposeRequest;
|
|
26
70
|
let disposeRequestError;
|
|
27
71
|
let disposeTurnStopping;
|
|
72
|
+
let disposeAssistantStream;
|
|
28
73
|
const reset = () => {
|
|
29
74
|
const request = disposeRequest;
|
|
30
75
|
const requestError = disposeRequestError;
|
|
31
76
|
const turnStopping = disposeTurnStopping;
|
|
77
|
+
const assistantStream = disposeAssistantStream;
|
|
78
|
+
active = false;
|
|
79
|
+
for (const wait of waits)
|
|
80
|
+
wait.abort();
|
|
32
81
|
disposeRequest = undefined;
|
|
33
82
|
disposeRequestError = undefined;
|
|
34
83
|
disposeTurnStopping = undefined;
|
|
35
84
|
pendingByAgent = new WeakMap();
|
|
36
|
-
|
|
85
|
+
attemptsByAgent = new WeakMap();
|
|
37
86
|
continuationsByAgent = new WeakMap();
|
|
38
87
|
installedContexts.delete(ctx);
|
|
88
|
+
if (typeof assistantStream === "function")
|
|
89
|
+
assistantStream();
|
|
39
90
|
if (typeof turnStopping === "function")
|
|
40
91
|
turnStopping();
|
|
41
92
|
if (typeof requestError === "function")
|
|
@@ -44,11 +95,67 @@ export function registerRuntimeRecovery(ctx, controller, getSettings) {
|
|
|
44
95
|
request();
|
|
45
96
|
};
|
|
46
97
|
try {
|
|
98
|
+
disposeAssistantStream = ctx.on("agent/assistant-stream", ({ agent, frame }) => {
|
|
99
|
+
if (!active)
|
|
100
|
+
return;
|
|
101
|
+
if (frame.type === "start") {
|
|
102
|
+
const settings = getSettings(agent);
|
|
103
|
+
const role = resolveEffectiveDsmmRole(agent, settings, controller.active(agent, settings.defaultActive));
|
|
104
|
+
const policy = liveRolePolicyIdentity(agent, settings, role, getSettings.admission?.(agent).epoch);
|
|
105
|
+
const lock = policy === undefined ? undefined : roleRouteLock(agent, policy);
|
|
106
|
+
const header = agent.session.requestHeader?.();
|
|
107
|
+
if (header === undefined) {
|
|
108
|
+
attemptsByAgent.delete(agent);
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
const previous = attemptsByAgent.get(agent);
|
|
112
|
+
const attempt = { id: frame.attemptId, turn: frame.turn, step: frame.step, revision: frame.revision, chunks: 0,
|
|
113
|
+
route: { provider: header.config.provider, model: header.config.model }, header, policy, generation: lock?.generation, manualSelectionSeq: lock?.manualSelectionSeq,
|
|
114
|
+
valid: Number.isSafeInteger(frame.revision) && frame.revision >= 1 && (previous === undefined || frame.revision === previous.revision + 1), output: false, consumed: false };
|
|
115
|
+
attemptsByAgent.set(agent, attempt);
|
|
116
|
+
if (lock !== undefined)
|
|
117
|
+
lock.attempts += 1;
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
const attempt = attemptsByAgent.get(agent);
|
|
121
|
+
if (attempt === undefined)
|
|
122
|
+
return;
|
|
123
|
+
if (attempt.id !== frame.attemptId || frame.revision !== attempt.revision + 1 || frame.index !== attempt.chunks || attempt.seq !== undefined) {
|
|
124
|
+
attempt.valid = false;
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
attempt.revision = frame.revision;
|
|
128
|
+
if (frame.type === "chunk") {
|
|
129
|
+
attempt.chunks += 1;
|
|
130
|
+
attempt.output ||= outputChunk(frame.chunk);
|
|
131
|
+
if (frame.chunk.type === "finish" && (frame.chunk.reason.kind === "error" || frame.chunk.reason.kind === "aborted"))
|
|
132
|
+
attempt.failure = frame.chunk.reason.failure;
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
if (frame.outcome.kind !== "committed") {
|
|
136
|
+
attempt.valid = false;
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
attempt.seq = frame.outcome.seq;
|
|
140
|
+
attempt.eventType = frame.outcome.eventType;
|
|
141
|
+
const lock = attempt.policy === undefined ? undefined : roleRouteLock(agent, attempt.policy);
|
|
142
|
+
if (lock !== undefined && (attempt.output || frame.outcome.eventType === "assistant/message")) {
|
|
143
|
+
lock.startupSettled = true;
|
|
144
|
+
if (frame.outcome.eventType === "assistant/message") {
|
|
145
|
+
lock.retries = 0;
|
|
146
|
+
lock.rateLimits = 0;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}, { prepend: true });
|
|
47
150
|
disposeRequest = ctx.on("agent/request", async (frame, next) => {
|
|
48
151
|
const downstream = await next();
|
|
49
152
|
const pending = pendingByAgent.get(frame.agent);
|
|
50
153
|
if (pending === undefined)
|
|
51
154
|
return downstream;
|
|
155
|
+
if (latestNativeModelSelection(frame.agent)?.seq !== pending.manualSelectionSeq) {
|
|
156
|
+
pendingByAgent.delete(frame.agent);
|
|
157
|
+
throw new Error("dsmm retry was superseded by an explicit native model selection");
|
|
158
|
+
}
|
|
52
159
|
if (frame.signal.aborted || !isCurrentRecoveryStep(recoveryEvents(frame.agent), pending.turn, pending.step)) {
|
|
53
160
|
pendingByAgent.delete(frame.agent);
|
|
54
161
|
return downstream;
|
|
@@ -56,79 +163,133 @@ export function registerRuntimeRecovery(ctx, controller, getSettings) {
|
|
|
56
163
|
if (pending.turn !== frame.turn || pending.step !== frame.step)
|
|
57
164
|
return downstream;
|
|
58
165
|
if (pending.policy !== undefined) {
|
|
59
|
-
const settings = getSettings();
|
|
166
|
+
const settings = getSettings(frame.agent);
|
|
60
167
|
const role = resolveEffectiveDsmmRole(frame.agent, settings, controller.active(frame.agent, settings.defaultActive));
|
|
61
|
-
if (role !== pending.role ||
|
|
168
|
+
if (role !== pending.role || liveRolePolicyIdentity(frame.agent, settings, role, getSettings.admission?.(frame.agent).epoch) !== pending.policy) {
|
|
62
169
|
pendingByAgent.delete(frame.agent);
|
|
63
|
-
reservedByAgent.delete(frame.agent);
|
|
64
170
|
return downstream;
|
|
65
171
|
}
|
|
66
172
|
}
|
|
67
|
-
const reserved = reservedByAgent.get(frame.agent);
|
|
68
173
|
const accepted = frame.agent.session.requestHeader?.()?.config;
|
|
69
|
-
|
|
174
|
+
const lock = pending.policy === undefined ? undefined : roleRouteLock(frame.agent, pending.policy);
|
|
175
|
+
if (!active || lock === undefined || lock.generation !== pending.generation || attemptsByAgent.get(frame.agent) !== pending.attempt
|
|
176
|
+
|| accepted === undefined || !sameModelRoute(accepted, pending.failedRoute)) {
|
|
70
177
|
pendingByAgent.delete(frame.agent);
|
|
71
178
|
return downstream;
|
|
72
179
|
}
|
|
73
180
|
pendingByAgent.delete(frame.agent);
|
|
181
|
+
lock.route = { ...pending.route };
|
|
74
182
|
recordAdmittedRecoveryRoute(frame, pending.route);
|
|
75
183
|
return applyModelRoute(downstream, pending.route);
|
|
76
184
|
}, { prepend: true });
|
|
77
185
|
disposeRequestError = ctx.on("agent/request-error", async (frame, next) => {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
186
|
+
let settings;
|
|
187
|
+
let role;
|
|
188
|
+
try {
|
|
189
|
+
settings = getSettings(frame.agent);
|
|
190
|
+
role = resolveEffectiveDsmmRole(frame.agent, settings, controller.active(frame.agent, settings.defaultActive));
|
|
191
|
+
}
|
|
192
|
+
catch {
|
|
193
|
+
const prior = attemptsByAgent.get(frame.agent);
|
|
194
|
+
return isRateLimit(frame.failure) && prior?.policy !== undefined ? undefined : await next();
|
|
82
195
|
}
|
|
196
|
+
const unavailable = isUnavailableRouteFailure(frame.failure, true);
|
|
197
|
+
if (role === undefined || (!isRateLimit(frame.failure) && !unavailable))
|
|
198
|
+
return await next();
|
|
83
199
|
try {
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
return downstream;
|
|
88
|
-
const preset = resolveSelectedAgentPreset(frame.agent.session);
|
|
89
|
-
const role = resolveEffectiveDsmmRole(frame.agent, settings, controller.active(frame.agent, settings.defaultActive));
|
|
90
|
-
const inScope = role !== undefined || controller.active(frame.agent, settings.defaultActive)
|
|
91
|
-
|| (frame.agent.session.header?.origin !== "subagent" && isDsmmRoleId(preset) && settings.roles[preset]);
|
|
92
|
-
if (!inScope || classifyRecoveryFailure(frame.failure, settings.runtimeRecovery).kind !== "retryable")
|
|
93
|
-
return downstream;
|
|
200
|
+
const policy = liveRolePolicyIdentity(frame.agent, settings, role, getSettings.admission?.(frame.agent).epoch);
|
|
201
|
+
const lock = policy === undefined ? undefined : roleRouteLock(frame.agent, policy);
|
|
202
|
+
const attempt = attemptsByAgent.get(frame.agent);
|
|
94
203
|
const header = frame.agent.session.requestHeader?.();
|
|
95
|
-
if (
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
||
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
204
|
+
if (!active || frame.signal.aborted || lock === undefined || header === undefined
|
|
205
|
+
|| header.config.provider !== frame.provider || !sameModelRoute(header.config, lock.route)
|
|
206
|
+
|| attempt === undefined || attempt.policy !== policy || attempt.generation !== lock.generation
|
|
207
|
+
|| latestNativeModelSelection(frame.agent)?.seq !== attempt.manualSelectionSeq
|
|
208
|
+
|| !isCurrentRecoveryStep(recoveryEvents(frame.agent), frame.turn, frame.step) || !positiveNoOutput(frame, attempt))
|
|
209
|
+
return undefined;
|
|
210
|
+
attempt.consumed = true;
|
|
211
|
+
const resolved = resolveRoleRuntimePolicy(settings, role);
|
|
212
|
+
const bounds = resolved.rateLimit;
|
|
213
|
+
let route = lock.route;
|
|
214
|
+
let switching = false;
|
|
215
|
+
if (unavailable) {
|
|
216
|
+
if (lock.startupSettled || lock.attempts !== 1)
|
|
217
|
+
return undefined;
|
|
218
|
+
switching = true;
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
lock.startupSettled = true;
|
|
222
|
+
lock.rateLimits += 1;
|
|
223
|
+
switching = resolved.strategy === "rate-limit-fallback" && lock.rateLimits >= bounds.switchAfterRateLimits;
|
|
224
|
+
if (!switching && lock.retries >= bounds.maxRetries)
|
|
225
|
+
return undefined;
|
|
226
|
+
}
|
|
227
|
+
if (switching) {
|
|
228
|
+
if (!unavailable && lock.switches >= bounds.maxSwitches)
|
|
229
|
+
return undefined;
|
|
230
|
+
const index = lock.candidates.findIndex((candidate) => sameModelRoute(candidate, lock.route));
|
|
231
|
+
const candidates = lock.candidates.slice(index + 1);
|
|
232
|
+
if (index < 0 || candidates.length === 0)
|
|
233
|
+
return undefined;
|
|
234
|
+
const llm = frame.agent.ctx?.get?.("llm") ?? ctx.get?.("llm") ?? ctx.llm;
|
|
235
|
+
route = await selectInitialModelRoute(llm ?? {}, candidates, frame.signal);
|
|
236
|
+
}
|
|
237
|
+
const serverMinimum = frame.failure.providerRetryAfterMs;
|
|
238
|
+
if (serverMinimum !== undefined && (!Number.isSafeInteger(serverMinimum) || serverMinimum < 0))
|
|
239
|
+
return undefined;
|
|
240
|
+
const delay = unavailable ? 0 : Math.max(Math.min(bounds.maxDelayMs, bounds.initialDelayMs * 2 ** lock.retries), serverMinimum ?? 0);
|
|
241
|
+
if (delay > bounds.maxDelayMs || delay > bounds.maxTotalDelayMs - lock.totalDelayMs)
|
|
242
|
+
return undefined;
|
|
243
|
+
lock.totalDelayMs += delay;
|
|
244
|
+
const expectedGeneration = lock.generation;
|
|
245
|
+
if (delay > 0) {
|
|
246
|
+
const wait = new AbortController();
|
|
247
|
+
waits.add(wait);
|
|
248
|
+
try {
|
|
249
|
+
const signal = AbortSignal.any([frame.signal, wait.signal]);
|
|
250
|
+
await new Promise((resolve, reject) => {
|
|
251
|
+
const timer = setTimeout(() => { signal.removeEventListener("abort", abort); resolve(); }, delay);
|
|
252
|
+
const abort = () => { clearTimeout(timer); signal.removeEventListener("abort", abort); reject(new Error("dsmm retry delay aborted")); };
|
|
253
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
254
|
+
if (signal.aborted)
|
|
255
|
+
abort();
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
finally {
|
|
259
|
+
waits.delete(wait);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
const currentSettings = getSettings(frame.agent);
|
|
263
|
+
const currentRole = resolveEffectiveDsmmRole(frame.agent, currentSettings, controller.active(frame.agent, currentSettings.defaultActive));
|
|
264
|
+
if (!active || frame.signal.aborted || currentRole !== role
|
|
265
|
+
|| liveRolePolicyIdentity(frame.agent, currentSettings, currentRole, getSettings.admission?.(frame.agent).epoch) !== policy
|
|
266
|
+
|| roleRouteLock(frame.agent, policy) !== lock || lock.generation !== expectedGeneration
|
|
267
|
+
|| attemptsByAgent.get(frame.agent) !== attempt || !isCurrentRecoveryStep(recoveryEvents(frame.agent), frame.turn, frame.step)
|
|
268
|
+
|| latestNativeModelSelection(frame.agent)?.seq !== attempt.manualSelectionSeq
|
|
269
|
+
|| !positiveNoOutput(frame, attempt, true)
|
|
270
|
+
|| !sameModelRoute(frame.agent.session.requestHeader?.()?.config ?? { provider: "", model: "" }, attempt.route))
|
|
271
|
+
return undefined;
|
|
272
|
+
if (switching) {
|
|
273
|
+
lock.generation += 1;
|
|
274
|
+
if (!unavailable)
|
|
275
|
+
lock.switches += 1;
|
|
276
|
+
lock.retries = 0;
|
|
277
|
+
lock.rateLimits = 0;
|
|
278
|
+
}
|
|
279
|
+
else
|
|
280
|
+
lock.retries += 1;
|
|
281
|
+
pendingByAgent.set(frame.agent, { turn: frame.turn, step: frame.step, route: { ...route }, role, policy,
|
|
282
|
+
generation: lock.generation, failedRoute: attempt.route, attempt, manualSelectionSeq: attempt.manualSelectionSeq });
|
|
122
283
|
return { kind: "retry" };
|
|
123
284
|
}
|
|
124
285
|
catch {
|
|
125
286
|
warnSafely(ctx, RECOVERY_WARNING);
|
|
126
|
-
return
|
|
287
|
+
return undefined;
|
|
127
288
|
}
|
|
128
289
|
}, { prepend: true });
|
|
129
290
|
disposeTurnStopping = ctx.on("agent/turn-stopping", async (frame) => {
|
|
130
291
|
try {
|
|
131
|
-
const settings = getSettings();
|
|
292
|
+
const settings = getSettings(frame.agent);
|
|
132
293
|
const continuation = settings.runtimeRecovery.idleContinuation;
|
|
133
294
|
if (!settings.runtimeRecovery.enabled || !continuation.enabled || frame.signal.aborted)
|
|
134
295
|
return;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { DshContext } from "./dsh-types.js";
|
|
2
|
+
export declare const DSMM_PERSISTENCE_COMPATIBILITY: unique symbol;
|
|
3
|
+
/** An ephemeral Host without persistence is safe; a durable Host must use the compatible provider. */
|
|
4
|
+
export declare function assertDsmmMetadataPersistence(ctx: DshContext | undefined): void;
|
|
5
|
+
/** Only these audited, model-hidden records are safe for a stock reader to skip. */
|
|
6
|
+
export declare function annotateDsmmEvent<T extends {
|
|
7
|
+
type: string;
|
|
8
|
+
data?: unknown;
|
|
9
|
+
ignorable?: unknown;
|
|
10
|
+
}>(event: T): T & {
|
|
11
|
+
ignorable?: true;
|
|
12
|
+
};
|
|
13
|
+
//# sourceMappingURL=session-metadata.d.ts.map
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { isDsmmRoleId } from "./roles.js";
|
|
2
|
+
export const DSMM_PERSISTENCE_COMPATIBILITY = Symbol.for("dsmm.sessionPersistence.ignorable.v1");
|
|
3
|
+
/** An ephemeral Host without persistence is safe; a durable Host must use the compatible provider. */
|
|
4
|
+
export function assertDsmmMetadataPersistence(ctx) {
|
|
5
|
+
const persistence = ctx?.get?.("sessionPersistence");
|
|
6
|
+
if (persistence !== undefined && (typeof persistence !== "object" || persistence === null
|
|
7
|
+
|| persistence[DSMM_PERSISTENCE_COMPATIBILITY] !== true)) {
|
|
8
|
+
throw new Error("Deepwork requires @dsmm/dsmm/session-persistence for durable session metadata; no unsafe event was appended");
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
/** Only these audited, model-hidden records are safe for a stock reader to skip. */
|
|
12
|
+
export function annotateDsmmEvent(event) {
|
|
13
|
+
if (event.type !== "deepwork/mode" && event.type !== "dsmm/role-policy")
|
|
14
|
+
return event;
|
|
15
|
+
const envelope = event;
|
|
16
|
+
const allowedKeys = ["type", "seq", "time", "data", "ignorable"];
|
|
17
|
+
const validEnvelope = Reflect.ownKeys(event).every((key) => typeof key === "string" && allowedKeys.includes(key))
|
|
18
|
+
&& ["type", "seq", "time", "data"].every((key) => Object.hasOwn(event, key))
|
|
19
|
+
&& Number.isSafeInteger(envelope.seq) && envelope.seq >= 0 && !Object.is(envelope.seq, -0)
|
|
20
|
+
&& Number.isSafeInteger(envelope.time);
|
|
21
|
+
const data = event.data;
|
|
22
|
+
const record = typeof data === "object" && data !== null && !Array.isArray(data)
|
|
23
|
+
&& (Object.getPrototypeOf(data) === Object.prototype || Object.getPrototypeOf(data) === null)
|
|
24
|
+
? data : undefined;
|
|
25
|
+
const valid = record !== undefined && (event.type === "deepwork/mode"
|
|
26
|
+
? Reflect.ownKeys(record).length === 1 && typeof record.active === "boolean"
|
|
27
|
+
: Reflect.ownKeys(record).length === 3 && record.version === 1 && isDsmmRoleId(record.role)
|
|
28
|
+
&& (record.policy === null || typeof record.policy === "string" && /^[a-f0-9]{64}$/u.test(record.policy)));
|
|
29
|
+
if (!validEnvelope || !valid || event.ignorable !== undefined && event.ignorable !== true) {
|
|
30
|
+
throw new TypeError("invalid Deepwork session metadata; refusing to mark it ignorable");
|
|
31
|
+
}
|
|
32
|
+
return event.ignorable === true ? event : { ...event, ignorable: true };
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=session-metadata.js.map
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Context, Service } from "@deepseek-ai/cordis";
|
|
2
|
+
import type { SessionHeader, SessionId } from "@deepseek-ai/dsh-session";
|
|
3
|
+
import { SessionPersistence } from "@deepseek-ai/dsh-session-persistence";
|
|
4
|
+
import type { SessionAccess, SessionHandle, SessionPersistenceCreateOptions, SessionPersistenceListOptions, SessionPersistenceOpenOptions, SessionPersistenceStatOptions } from "@deepseek-ai/dsh-session-persistence";
|
|
5
|
+
import type { Config } from "@deepseek-ai/dsh-session-persistence-jsonl";
|
|
6
|
+
export { DSMM_PERSISTENCE_COMPATIBILITY } from "./session-metadata.js";
|
|
7
|
+
export type { Config } from "@deepseek-ai/dsh-session-persistence-jsonl";
|
|
8
|
+
/**
|
|
9
|
+
* Drop-in native JSONL persistence companion for audited DSMM metadata.
|
|
10
|
+
* A separate root Context is intentional: a child service-isolation scope still
|
|
11
|
+
* shares the host event bus and would persist every live event twice.
|
|
12
|
+
*/
|
|
13
|
+
export default class DsmmSessionPersistence extends SessionPersistence {
|
|
14
|
+
readonly config: Config;
|
|
15
|
+
static Config: import("@deepseek-ai/schemastery").default<Config>;
|
|
16
|
+
readonly name = "dsmm-session-persistence";
|
|
17
|
+
private readonly inner;
|
|
18
|
+
private readonly ready;
|
|
19
|
+
private readonly handles;
|
|
20
|
+
private readonly writers;
|
|
21
|
+
private readonly acquisitions;
|
|
22
|
+
private readonly closeFailures;
|
|
23
|
+
private disposed;
|
|
24
|
+
constructor(ctx: Context, config: Config);
|
|
25
|
+
[Service.init](): Promise<void>;
|
|
26
|
+
create(header: SessionHeader, options?: SessionPersistenceCreateOptions): Promise<SessionHandle>;
|
|
27
|
+
open(id: SessionId, access: SessionAccess, options?: SessionPersistenceOpenOptions): Promise<SessionHandle>;
|
|
28
|
+
flush(): Promise<void>;
|
|
29
|
+
stat(id: SessionId, options?: SessionPersistenceStatOptions): Promise<import("@deepseek-ai/dsh-session-persistence").SessionPersistenceSnapshot | undefined>;
|
|
30
|
+
list(options?: SessionPersistenceListOptions): Promise<readonly import("@deepseek-ai/dsh-session-persistence").SessionPersistenceSnapshot[]>;
|
|
31
|
+
private acquire;
|
|
32
|
+
private warn;
|
|
33
|
+
private assertActive;
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=session-persistence.d.ts.map
|