dsh-coding 0.0.0-stage → 0.1.1-rc1
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/LICENSE +21 -0
- package/README.md +91 -2
- package/cordis.patch.yml +233 -0
- package/docs/DETAIL.md +183 -0
- package/docs/DEVELOPMENT.md +189 -0
- package/icon.svg +6 -0
- package/lib/apply-patch.js +268 -0
- package/lib/client/capsule.js +120 -0
- package/lib/client/card.js +220 -0
- package/lib/client/command-row.js +76 -0
- package/lib/client/config-page.js +392 -0
- package/lib/client/definition.js +164 -0
- package/lib/client/index.js +129 -0
- package/lib/client/locales.js +156 -0
- package/lib/client/mode-picker.js +77 -0
- package/lib/client/styles.js +283 -0
- package/lib/client.js +1691 -0
- package/lib/coding-projection.js +349 -0
- package/lib/config.js +204 -0
- package/lib/contract.js +124 -0
- package/lib/editor.js +23 -0
- package/lib/failures.js +142 -0
- package/lib/handoff.js +312 -0
- package/lib/index.js +115 -0
- package/lib/language.js +136 -0
- package/lib/messages.js +43 -0
- package/lib/models.js +154 -0
- package/lib/patch.js +235 -0
- package/lib/preset.js +1488 -0
- package/lib/reasoning-levels.js +28 -0
- package/lib/reasoning-projection.js +178 -0
- package/lib/reasoning.js +585 -0
- package/lib/stages.js +306 -0
- package/lib/state.js +550 -0
- package/lib/system-language.js +56 -0
- package/lib/types/apply-patch.d.ts +13 -0
- package/lib/types/client/capsule.d.ts +9 -0
- package/lib/types/client/card.d.ts +72 -0
- package/lib/types/client/command-row.d.ts +42 -0
- package/lib/types/client/config-page.d.ts +136 -0
- package/lib/types/client/definition.d.ts +51 -0
- package/lib/types/client/index.d.ts +10 -0
- package/lib/types/client/locales.d.ts +18 -0
- package/lib/types/client/mode-picker.d.ts +25 -0
- package/lib/types/client/styles.d.ts +28 -0
- package/lib/types/coding-projection.d.ts +116 -0
- package/lib/types/config.d.ts +193 -0
- package/lib/types/contract.d.ts +33 -0
- package/lib/types/editor.d.ts +9 -0
- package/lib/types/failures.d.ts +62 -0
- package/lib/types/handoff.d.ts +112 -0
- package/lib/types/index.d.ts +144 -0
- package/lib/types/language.d.ts +56 -0
- package/lib/types/messages.d.ts +27 -0
- package/lib/types/models.d.ts +82 -0
- package/lib/types/patch.d.ts +37 -0
- package/lib/types/preset.d.ts +22 -0
- package/lib/types/reasoning-levels.d.ts +20 -0
- package/lib/types/reasoning-projection.d.ts +64 -0
- package/lib/types/reasoning.d.ts +141 -0
- package/lib/types/stages.d.ts +164 -0
- package/lib/types/state.d.ts +261 -0
- package/lib/types/system-language.d.ts +10 -0
- package/lib/types/verification-tool.d.ts +32 -0
- package/lib/types/verification.d.ts +77 -0
- package/lib/types/verifier.d.ts +9 -0
- package/lib/types/volatile.d.ts +17 -0
- package/lib/verification-tool.js +176 -0
- package/lib/verification.js +337 -0
- package/lib/verifier.js +25 -0
- package/lib/volatile.js +20 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +129 -3
package/lib/state.js
ADDED
|
@@ -0,0 +1,550 @@
|
|
|
1
|
+
import { hasDefinitionOfDone, hasGoal } from './handoff.js';
|
|
2
|
+
import { IMPLEMENT_TABLE, STAGES_BY_MODE, entryStage, stageAdmits, stageInMode, stagePath, toolClass, } from './stages.js';
|
|
3
|
+
/** Build the initial state of a session in one mode. */
|
|
4
|
+
export function createState(sessionId, mode) {
|
|
5
|
+
const now = Date.now();
|
|
6
|
+
return {
|
|
7
|
+
runId: `${sessionId}:${now.toString(36)}`,
|
|
8
|
+
sessionId,
|
|
9
|
+
startedAt: now,
|
|
10
|
+
mode,
|
|
11
|
+
stage: entryStage(mode),
|
|
12
|
+
updatedAt: now,
|
|
13
|
+
turn: 0,
|
|
14
|
+
step: 0,
|
|
15
|
+
requirementsObserved: 0,
|
|
16
|
+
edits: 0,
|
|
17
|
+
files: new Set(),
|
|
18
|
+
baseEdits: 0,
|
|
19
|
+
verificationAttempts: 0,
|
|
20
|
+
sameFailureCount: 0,
|
|
21
|
+
deliveryBlocked: false,
|
|
22
|
+
transitions: [],
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Seed the live state from the durable projection. A resumed session must keep
|
|
27
|
+
* the stage, plan, verification, and edit evidence its Session log recorded,
|
|
28
|
+
* or the runtime would silently restart the run while the card still shows it.
|
|
29
|
+
* A refusal is deliberately not restored: an old gate rejection must not block
|
|
30
|
+
* the first step after a restart. The verification retry budget is restored
|
|
31
|
+
* from the checkpoint, while the repeat detector starts over: the signature of
|
|
32
|
+
* the failure that exhausted it is not part of the checkpoint.
|
|
33
|
+
* @param state - live state of the resumed agent (mutated).
|
|
34
|
+
* @param view - validated projection value for the same session.
|
|
35
|
+
*/
|
|
36
|
+
export function restoreFromView(state, view) {
|
|
37
|
+
state.mode = view.mode;
|
|
38
|
+
state.stage = stageInMode(view.mode, view.stage) ? view.stage : entryStage(view.mode);
|
|
39
|
+
state.plan = view.plan.length === 0
|
|
40
|
+
? undefined
|
|
41
|
+
: { items: view.plan.map(item => ({ content: item.content, status: item.status })), at: view.updatedAt };
|
|
42
|
+
state.refusal = undefined;
|
|
43
|
+
state.lastRefusal = undefined;
|
|
44
|
+
// A restart cannot know which failure signature the live state last held, so
|
|
45
|
+
// the repeat detector starts over: the next failure counts as its own first.
|
|
46
|
+
state.sameFailureCount = 0;
|
|
47
|
+
state.lastFailureSignature = undefined;
|
|
48
|
+
state.deliveryBlocked = false;
|
|
49
|
+
const checkpoint = view.checkpoint ?? undefined;
|
|
50
|
+
if (checkpoint === undefined) {
|
|
51
|
+
state.updatedAt = Math.max(state.updatedAt, view.updatedAt);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
state.runId = checkpoint.runId;
|
|
55
|
+
state.turn = checkpoint.turn;
|
|
56
|
+
state.step = checkpoint.step;
|
|
57
|
+
state.files = new Set(checkpoint.files ?? []);
|
|
58
|
+
// A checkpoint written before `files` existed only knows the total; keep it
|
|
59
|
+
// as a floor so a resumed session never reports fewer edits than it made.
|
|
60
|
+
state.baseEdits = checkpoint.files === undefined ? checkpoint.edits : 0;
|
|
61
|
+
state.edits = checkpoint.edits;
|
|
62
|
+
// A checkpoint proves the session already inspected the workspace before it
|
|
63
|
+
// progressed; do not make a resumed plan stage re-satisfy the requirements gate.
|
|
64
|
+
state.requirementsObserved = Math.max(state.requirementsObserved, 1);
|
|
65
|
+
if (checkpoint.verification !== undefined)
|
|
66
|
+
state.verification = checkpoint.verification;
|
|
67
|
+
if (checkpoint.handoff !== undefined)
|
|
68
|
+
state.handoff = checkpoint.handoff;
|
|
69
|
+
// The failure is restored without its notice: the user saw the reason before
|
|
70
|
+
// the restart, so a resumed session must not report it a second time.
|
|
71
|
+
if (checkpoint.failure !== undefined)
|
|
72
|
+
state.failure = checkpoint.failure;
|
|
73
|
+
state.verificationAttempts = checkpoint.verifyAttempts ?? 0;
|
|
74
|
+
state.verifyBlocked = checkpoint.verifyBlocked;
|
|
75
|
+
if (checkpoint.worker !== undefined)
|
|
76
|
+
state.worker = checkpoint.worker;
|
|
77
|
+
state.updatedAt = Math.max(checkpoint.updatedAt, view.updatedAt);
|
|
78
|
+
}
|
|
79
|
+
/** Stage at which this mode's own gates can be satisfied, one line. */
|
|
80
|
+
export function stageGuidance(mode) {
|
|
81
|
+
return `stages: ${stagePath(mode)}`;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Decide whether the model may move to `target` now. Conditions are checked by
|
|
85
|
+
* plugin code, never taken from the model's assertion.
|
|
86
|
+
* @param state - live session state.
|
|
87
|
+
* @param target - requested stage.
|
|
88
|
+
* @returns whether the move is allowed, and why not when it is refused.
|
|
89
|
+
*/
|
|
90
|
+
export function checkAdvance(state, target) {
|
|
91
|
+
if (!stageInMode(state.mode, target)) {
|
|
92
|
+
if (state.mode === 'auto') {
|
|
93
|
+
return {
|
|
94
|
+
ok: false,
|
|
95
|
+
soft: true,
|
|
96
|
+
reason: 'auto mode has not chosen a workflow yet: in the requirements stage call coding_mode with consult '
|
|
97
|
+
+ '(the fallback when the request is unclear), review, or codex, then follow that mode\'s stages. '
|
|
98
|
+
+ 'Do not skip the workflow choice by advancing a stage.',
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
return {
|
|
102
|
+
ok: false,
|
|
103
|
+
reason: `stage "${target}" is not part of ${state.mode} mode (${stagePath(state.mode)}). `
|
|
104
|
+
+ 'Switch mode with coding_mode, or advance to a stage this mode opens.',
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
if (target === 'plan' && state.requirementsObserved === 0) {
|
|
108
|
+
return {
|
|
109
|
+
ok: false,
|
|
110
|
+
reason: 'the requirements stage is not complete: inspect the workspace first (read, grep, glob, or one shell command), '
|
|
111
|
+
+ 'then advance to plan.',
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
// The planner worker receives the handoff and nothing else of the lead's
|
|
115
|
+
// conversation, so a run without a recorded goal would plan from guesswork.
|
|
116
|
+
if (target === 'plan' && state.mode === 'codex' && !goalRecorded(state)) {
|
|
117
|
+
return {
|
|
118
|
+
ok: false,
|
|
119
|
+
reason: 'the requirements stage has no recorded goal: call coding_handoff({ goal, constraints, acceptance }) '
|
|
120
|
+
+ 'before advancing to plan. The handoff is the only context the planner and the implement workers receive.',
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
if (target === 'implement' && (state.plan === undefined || state.plan.items.length === 0)) {
|
|
124
|
+
return {
|
|
125
|
+
ok: false,
|
|
126
|
+
reason: 'no structured task list yet: call todo_write with 3-6 verifiable steps (exactly one in_progress), '
|
|
127
|
+
+ 'then advance to implement.',
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
// In codex mode verification follows an applied edit; in review mode the
|
|
131
|
+
// verify stage checks existing behavior, so no edit is expected.
|
|
132
|
+
if (target === 'verify' && state.mode === 'codex' && state.edits === 0) {
|
|
133
|
+
return {
|
|
134
|
+
ok: false,
|
|
135
|
+
reason: 'nothing has been implemented yet: advance to implement, run delegate_implement, and let the '
|
|
136
|
+
+ 'implement-worker report the files it changed before moving to verify.',
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
// Verification is only evidence against a stated standard: without one the
|
|
140
|
+
// verifier cannot tell a finished change from a modified file.
|
|
141
|
+
if (target === 'verify' && state.mode === 'codex' && !doneRecorded(state)) {
|
|
142
|
+
return {
|
|
143
|
+
ok: false,
|
|
144
|
+
reason: 'no definition of done: call coding_handoff({ acceptance: [...] }) with the observable checks that prove '
|
|
145
|
+
+ 'this change (one per task), then advance to verify. A task without a verification standard cannot be delivered.',
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
if (target === 'deliver') {
|
|
149
|
+
if (state.mode === 'codex' && state.verification?.passed !== true) {
|
|
150
|
+
// The retry budget turns an endless fix/verify loop into one blocked
|
|
151
|
+
// delivery: report the failing check instead of repeating the same fix.
|
|
152
|
+
if (state.verifyBlocked !== undefined) {
|
|
153
|
+
return {
|
|
154
|
+
ok: true,
|
|
155
|
+
blocked: true,
|
|
156
|
+
reason: 'verification retry budget exhausted: deliver a BLOCKED report naming the failing check and the last '
|
|
157
|
+
+ 'error; do not retry the same failure.',
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
return {
|
|
161
|
+
ok: false,
|
|
162
|
+
reason: 'verification has not passed: run run_verification in the verify stage (or delegate_verify) and '
|
|
163
|
+
+ 'get a passing result before delivering. Fix the failure in implement first.'
|
|
164
|
+
+ (state.verificationAttempts > 0
|
|
165
|
+
? ` Attempt ${state.verificationAttempts} of the retry budget has been used.`
|
|
166
|
+
: ''),
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
if (state.mode === 'codex' && (!goalRecorded(state) || !doneRecorded(state))) {
|
|
170
|
+
return {
|
|
171
|
+
ok: false,
|
|
172
|
+
reason: 'the handoff is incomplete for delivery: call coding_handoff({ goal, acceptance: [...] }) so the delivery '
|
|
173
|
+
+ 'report can state what was asked and which acceptance checks prove it.',
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
if (state.mode === 'review' && state.verification === undefined) {
|
|
177
|
+
return {
|
|
178
|
+
ok: false,
|
|
179
|
+
reason: 'review mode delivers findings: run run_verification (or delegate_verify) at least once so '
|
|
180
|
+
+ 'the report states what was checked.',
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return { ok: true };
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Apply one granted stage transition.
|
|
188
|
+
* @param state - live session state (mutated).
|
|
189
|
+
* @param target - granted stage.
|
|
190
|
+
* @param reason - why the move happened, kept in the transition log.
|
|
191
|
+
*/
|
|
192
|
+
export function applyStage(state, target, reason) {
|
|
193
|
+
if (state.stage === target)
|
|
194
|
+
return;
|
|
195
|
+
// A granted move is a fresh attempt: an earlier blocked delivery is moot here.
|
|
196
|
+
state.deliveryBlocked = false;
|
|
197
|
+
// A granted move satisfies whatever gate was last refused.
|
|
198
|
+
state.refusal = undefined;
|
|
199
|
+
state.lastRefusal = undefined;
|
|
200
|
+
const from = state.stage;
|
|
201
|
+
state.stage = target;
|
|
202
|
+
state.updatedAt = Date.now();
|
|
203
|
+
state.transitions.push({ from, to: target, at: state.updatedAt, reason });
|
|
204
|
+
if (state.transitions.length > 40)
|
|
205
|
+
state.transitions.splice(0, state.transitions.length - 40);
|
|
206
|
+
}
|
|
207
|
+
/** Whether the handoff already states a goal, which codex `plan` requires. */
|
|
208
|
+
function goalRecorded(state) {
|
|
209
|
+
return state.handoff !== undefined && hasGoal(state.handoff);
|
|
210
|
+
}
|
|
211
|
+
/** Whether the handoff already states a definition of done, which `verify` and `deliver` require. */
|
|
212
|
+
function doneRecorded(state) {
|
|
213
|
+
return state.handoff !== undefined && hasDefinitionOfDone(state.handoff);
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Reduce one failed verification to a comparable signature. The signature is
|
|
217
|
+
* what decides whether the lead is fixing the failure or repeating the same
|
|
218
|
+
* attempt, so it keeps only the stable parts: failing test names (sorted, the
|
|
219
|
+
* order they were reported in means nothing), the first three error lines, and
|
|
220
|
+
* the exit code.
|
|
221
|
+
* @param outcome - the verification result to fingerprint.
|
|
222
|
+
* @returns a deterministic signature string.
|
|
223
|
+
*/
|
|
224
|
+
export function failureSignature(outcome) {
|
|
225
|
+
const errors = outcome.errors
|
|
226
|
+
.map(error => error.trim())
|
|
227
|
+
.filter(error => error !== '')
|
|
228
|
+
.slice(0, 3);
|
|
229
|
+
return JSON.stringify({
|
|
230
|
+
failed: [...outcome.failedTests].sort(),
|
|
231
|
+
errors,
|
|
232
|
+
exit: outcome.exitCode,
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Record one verification result and spend or refund the retry budget.
|
|
237
|
+
*
|
|
238
|
+
* A pass ends the loop: every counter is cleared, so the next edit starts from
|
|
239
|
+
* a clean budget. A failure increments the attempt counter and the repeat
|
|
240
|
+
* counter, and once either budget is spent it sets {@link CodingState.verifyBlocked},
|
|
241
|
+
* which is the state `checkAdvance` reads to allow the blocked delivery. A
|
|
242
|
+
* failure never clears that flag: only a passing verification does.
|
|
243
|
+
* @param state - live session state (mutated).
|
|
244
|
+
* @param outcome - the verification result.
|
|
245
|
+
* @param budget - the configured retry budget.
|
|
246
|
+
*/
|
|
247
|
+
export function recordVerification(state, outcome, budget) {
|
|
248
|
+
state.verification = outcome;
|
|
249
|
+
state.updatedAt = outcome.ranAt;
|
|
250
|
+
if (outcome.passed) {
|
|
251
|
+
clearBudget(state);
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
state.verificationAttempts += 1;
|
|
255
|
+
const signature = failureSignature(outcome);
|
|
256
|
+
state.sameFailureCount = state.lastFailureSignature === signature ? state.sameFailureCount + 1 : 1;
|
|
257
|
+
state.lastFailureSignature = signature;
|
|
258
|
+
const exhausted = (budget.maxAttempts > 0 && state.verificationAttempts >= budget.maxAttempts)
|
|
259
|
+
|| (budget.maxSameFailure > 0 && state.sameFailureCount >= budget.maxSameFailure);
|
|
260
|
+
if (!exhausted)
|
|
261
|
+
return;
|
|
262
|
+
const attempts = state.verificationAttempts;
|
|
263
|
+
state.verifyBlocked = {
|
|
264
|
+
attempts,
|
|
265
|
+
reason: `verification failed ${attempts} time(s) (${state.sameFailureCount} identical in a row); retry budget exhausted. `
|
|
266
|
+
+ 'Stop the fix/verify loop: change the approach with a materially different fix, or advance to deliver and '
|
|
267
|
+
+ 'report a BLOCKED delivery naming the failing check and the last error.',
|
|
268
|
+
at: outcome.ranAt,
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Forget the retry budget of a run, keeping its last verification as evidence.
|
|
273
|
+
* Used by an explicit reset: the counters belong to the current fix/verify loop,
|
|
274
|
+
* not to the session's history.
|
|
275
|
+
* @param state - live session state (mutated).
|
|
276
|
+
*/
|
|
277
|
+
export function resetVerification(state) {
|
|
278
|
+
clearBudget(state);
|
|
279
|
+
state.updatedAt = Date.now();
|
|
280
|
+
}
|
|
281
|
+
/** Clear every retry-budget field, leaving `verification` untouched. */
|
|
282
|
+
function clearBudget(state) {
|
|
283
|
+
state.verificationAttempts = 0;
|
|
284
|
+
state.sameFailureCount = 0;
|
|
285
|
+
state.lastFailureSignature = undefined;
|
|
286
|
+
state.verifyBlocked = undefined;
|
|
287
|
+
state.deliveryBlocked = false;
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Record a refused advance. The next `agent/pre-step` rejects once with this
|
|
291
|
+
* reason, which is the documented penalty for skipping a gate.
|
|
292
|
+
* @param state - live session state (mutated).
|
|
293
|
+
* @param target - stage the model tried to enter.
|
|
294
|
+
* @param reason - the gate that refused it.
|
|
295
|
+
*/
|
|
296
|
+
export function recordRefusal(state, target, reason) {
|
|
297
|
+
state.refusal = { target, reason, at: Date.now() };
|
|
298
|
+
state.lastRefusal = reason;
|
|
299
|
+
state.updatedAt = state.refusal.at;
|
|
300
|
+
}
|
|
301
|
+
/** Clear the one-shot refusal after the blocking step has run. */
|
|
302
|
+
export function clearRefusal(state) {
|
|
303
|
+
const refusal = state.refusal;
|
|
304
|
+
state.refusal = undefined;
|
|
305
|
+
return refusal?.reason;
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Read and clear the refusal text that rides the next step's contract. The
|
|
309
|
+
* blocked turn itself carries no messages, so this is the only way the model
|
|
310
|
+
* learns why its step was rejected.
|
|
311
|
+
* @param state - live session state.
|
|
312
|
+
* @returns the refusal text, once.
|
|
313
|
+
*/
|
|
314
|
+
export function takeLastRefusal(state) {
|
|
315
|
+
const value = state.lastRefusal;
|
|
316
|
+
state.lastRefusal = undefined;
|
|
317
|
+
return value;
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Record one blocking exception. It becomes the session's current failure, and
|
|
321
|
+
* is remembered as a notice the next step delivers once.
|
|
322
|
+
* @param state - live session state (mutated).
|
|
323
|
+
* @param failure - the report built at the failing dispatch point.
|
|
324
|
+
*/
|
|
325
|
+
export function recordFailure(state, failure) {
|
|
326
|
+
state.failure = failure;
|
|
327
|
+
state.failureNotice = failure;
|
|
328
|
+
state.updatedAt = failure.at;
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Read and clear the notice that rides the next step's contract. A failure is
|
|
332
|
+
* reported to the user exactly once, so the same mechanism cannot flood the
|
|
333
|
+
* conversation with a repeated card.
|
|
334
|
+
* @param state - live session state (mutated).
|
|
335
|
+
* @returns the failure to announce, once.
|
|
336
|
+
*/
|
|
337
|
+
export function takeFailureNotice(state) {
|
|
338
|
+
const failure = state.failureNotice;
|
|
339
|
+
state.failureNotice = undefined;
|
|
340
|
+
return failure;
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Forget a failure once its subsystem works again. Only the matching point is
|
|
344
|
+
* cleared, so recovering one subsystem cannot hide a different, still-current
|
|
345
|
+
* interruption.
|
|
346
|
+
* @param state - live session state (mutated).
|
|
347
|
+
* @param point - the dispatch point whose retry just succeeded.
|
|
348
|
+
*/
|
|
349
|
+
export function clearFailure(state, point) {
|
|
350
|
+
if (state.failure?.point === point)
|
|
351
|
+
state.failure = undefined;
|
|
352
|
+
if (state.failureNotice?.point === point)
|
|
353
|
+
state.failureNotice = undefined;
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Switch mode. A stage the new mode does not open resets to that mode's entry
|
|
357
|
+
* stage, so a mode switch can never leave the session in a stage its mode
|
|
358
|
+
* forbids.
|
|
359
|
+
* @param state - live session state (mutated).
|
|
360
|
+
* @param mode - requested mode.
|
|
361
|
+
* @returns the resulting stage.
|
|
362
|
+
*/
|
|
363
|
+
export function applyMode(state, mode) {
|
|
364
|
+
state.mode = mode;
|
|
365
|
+
state.updatedAt = Date.now();
|
|
366
|
+
state.refusal = undefined;
|
|
367
|
+
state.lastRefusal = undefined;
|
|
368
|
+
if (!stageInMode(mode, state.stage)) {
|
|
369
|
+
const from = state.stage;
|
|
370
|
+
state.stage = entryStage(mode);
|
|
371
|
+
state.transitions.push({
|
|
372
|
+
from,
|
|
373
|
+
to: state.stage,
|
|
374
|
+
at: state.updatedAt,
|
|
375
|
+
reason: `mode switched to ${mode}`,
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
return state.stage;
|
|
379
|
+
}
|
|
380
|
+
/** Guidance appended to a denial, chosen from the stage and the tool class. */
|
|
381
|
+
function remedy(stage, kind, mode) {
|
|
382
|
+
if (kind === 'write') {
|
|
383
|
+
return stage === 'implement'
|
|
384
|
+
? 'File edits are delegated: call delegate_implement and let the implement-worker apply the patch.'
|
|
385
|
+
: 'Implementation is delegated: advance to the implement stage with coding_stage, then call delegate_implement.';
|
|
386
|
+
}
|
|
387
|
+
if (kind === 'delegate') {
|
|
388
|
+
return 'Delegation runs in its own stage: delegate_plan in plan, delegate_implement in implement; advance with coding_stage first.';
|
|
389
|
+
}
|
|
390
|
+
if (kind === 'plan') {
|
|
391
|
+
return 'Author the task list in the plan stage with todo_write.';
|
|
392
|
+
}
|
|
393
|
+
if (kind === 'verify') {
|
|
394
|
+
return 'Verification runs in the verify stage: advance with coding_stage, then call run_verification.';
|
|
395
|
+
}
|
|
396
|
+
if (kind === 'exec' && (stage === 'implement' || stage === 'verify' || stage === 'deliver')) {
|
|
397
|
+
return 'The lead agent does not run commands in this stage; verification commands belong to run_verification or delegate_verify.';
|
|
398
|
+
}
|
|
399
|
+
if (kind === 'read' && mode === 'consult') {
|
|
400
|
+
return 'Consult mode answers from inspection only; keep reading, or advance to deliver.';
|
|
401
|
+
}
|
|
402
|
+
return `allowed tool classes here: ${stageInMode(mode, stage) ? stagePath(mode) : mode}.`;
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* The single policy decision shared by the friendly waterfall listener and the
|
|
406
|
+
* monotonic guard. Both evaluate this function, so they can never disagree.
|
|
407
|
+
* @param state - live session state of the calling agent.
|
|
408
|
+
* @param toolName - tool being dispatched.
|
|
409
|
+
* @returns a denial message, or an empty verdict when the call may proceed.
|
|
410
|
+
*/
|
|
411
|
+
export function checkTool(state, toolName) {
|
|
412
|
+
const kind = toolClass(toolName);
|
|
413
|
+
// Unknown tools belong to other plugins; this gate does not own their semantics.
|
|
414
|
+
if (kind === undefined)
|
|
415
|
+
return {};
|
|
416
|
+
if (kind === 'stage' || kind === 'mode' || kind === 'reasoning')
|
|
417
|
+
return {};
|
|
418
|
+
if (stageAdmits(state.stage, kind))
|
|
419
|
+
return {};
|
|
420
|
+
const reason = `dsh-coding: ${toolName} is not available in the ${state.stage} stage of ${state.mode} mode. `
|
|
421
|
+
+ `${remedy(state.stage, kind, state.mode)} `
|
|
422
|
+
+ `Current stages for this mode: ${stagePath(state.mode)}.`;
|
|
423
|
+
return { deny: reason };
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
426
|
+
* Whether the lead's tool table must be trimmed to the delegation set. In the
|
|
427
|
+
* implement stage the model keeps only the delegation verbs: the edit tools are
|
|
428
|
+
* removed from the presentation table, not merely denied at dispatch.
|
|
429
|
+
* @param stage - current stage.
|
|
430
|
+
* @returns the allow-list to apply, or undefined when the table stays whole.
|
|
431
|
+
*/
|
|
432
|
+
export function tableTrim(stage) {
|
|
433
|
+
return stage === 'implement' ? IMPLEMENT_TABLE : undefined;
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
* The verification part of the status line: the last result, the attempt it
|
|
437
|
+
* cost, and the blocked state once the retry budget is gone.
|
|
438
|
+
*/
|
|
439
|
+
function verificationStatus(state) {
|
|
440
|
+
if (state.verifyBlocked !== undefined)
|
|
441
|
+
return `BLOCKED after ${state.verifyBlocked.attempts} attempts`;
|
|
442
|
+
const outcome = state.verification;
|
|
443
|
+
if (outcome === undefined)
|
|
444
|
+
return 'not run';
|
|
445
|
+
if (outcome.passed)
|
|
446
|
+
return `passed (${outcome.kind}/${outcome.scope}, ${outcome.durationMs}ms)`;
|
|
447
|
+
return `FAILED (${outcome.kind}/${outcome.scope}, exit ${String(outcome.exitCode)}, attempt ${state.verificationAttempts})`;
|
|
448
|
+
}
|
|
449
|
+
/** Render the model-facing status line injected before every step. */
|
|
450
|
+
export function statusLine(state) {
|
|
451
|
+
const plan = state.plan === undefined
|
|
452
|
+
? 'none'
|
|
453
|
+
: `${state.plan.items.filter(item => item.status === 'completed').length}/${state.plan.items.length} done`;
|
|
454
|
+
const verification = verificationStatus(state);
|
|
455
|
+
const handoff = `handoff goal=${goalRecorded(state) ? 'set' : 'missing'}, `
|
|
456
|
+
+ `acceptance=${state.handoff?.acceptance.length ?? 0}`;
|
|
457
|
+
const worker = state.worker === undefined ? undefined : `${state.worker.role}-worker ${state.worker.status}`;
|
|
458
|
+
return [
|
|
459
|
+
`stage ${state.stage} of ${stagePath(state.mode)}; mode ${state.mode}`,
|
|
460
|
+
`plan ${plan}`,
|
|
461
|
+
`verification ${verification}`,
|
|
462
|
+
handoff,
|
|
463
|
+
`files changed ${state.edits}`,
|
|
464
|
+
...state.stage === 'deliver' && state.deliveryBlocked ? ['delivery BLOCKED'] : [],
|
|
465
|
+
...state.failure === undefined ? [] : [`failure ${state.failure.point}`],
|
|
466
|
+
...worker === undefined ? [] : [worker],
|
|
467
|
+
].join(' | ');
|
|
468
|
+
}
|
|
469
|
+
/** Build the whole-value checkpoint published with a Coding tool result. */
|
|
470
|
+
export function checkpointOf(state) {
|
|
471
|
+
const items = state.plan?.items ?? [];
|
|
472
|
+
return {
|
|
473
|
+
v: 1,
|
|
474
|
+
runId: state.runId,
|
|
475
|
+
turn: state.turn,
|
|
476
|
+
step: state.step,
|
|
477
|
+
mode: state.mode,
|
|
478
|
+
stage: state.stage,
|
|
479
|
+
allowedStages: STAGES_BY_MODE[state.mode],
|
|
480
|
+
planItems: items.length,
|
|
481
|
+
openPlanItems: items.filter(item => item.status !== 'completed').length,
|
|
482
|
+
edits: state.edits,
|
|
483
|
+
files: [...state.files].sort(),
|
|
484
|
+
...state.verification === undefined ? {} : { verification: state.verification },
|
|
485
|
+
...state.handoff === undefined ? {} : { handoff: state.handoff },
|
|
486
|
+
verifyAttempts: state.verificationAttempts,
|
|
487
|
+
...state.verifyBlocked === undefined ? {} : { verifyBlocked: state.verifyBlocked },
|
|
488
|
+
...state.worker === undefined ? {} : { worker: state.worker },
|
|
489
|
+
...state.refusal === undefined ? {} : { refusal: state.refusal },
|
|
490
|
+
...state.failure === undefined ? {} : { failure: state.failure },
|
|
491
|
+
updatedAt: state.updatedAt,
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
/** Per-session state store keyed by the live Agent, with a session-id index. */
|
|
495
|
+
export class CodingStateStore {
|
|
496
|
+
config;
|
|
497
|
+
byAgent = new WeakMap();
|
|
498
|
+
bySession = new Map();
|
|
499
|
+
constructor(config) {
|
|
500
|
+
this.config = config;
|
|
501
|
+
}
|
|
502
|
+
/** Read an agent's state without creating one. */
|
|
503
|
+
peek(agent) {
|
|
504
|
+
if (agent === undefined)
|
|
505
|
+
return undefined;
|
|
506
|
+
const existing = this.byAgent.get(agent);
|
|
507
|
+
if (existing !== undefined)
|
|
508
|
+
return existing;
|
|
509
|
+
const sessionId = sessionIdOf(agent);
|
|
510
|
+
return sessionId === undefined ? undefined : this.bySession.get(sessionId);
|
|
511
|
+
}
|
|
512
|
+
/** Read the live state for a session id, for `session/event` folds. */
|
|
513
|
+
bySessionId(sessionId) {
|
|
514
|
+
return this.bySession.get(sessionId);
|
|
515
|
+
}
|
|
516
|
+
/** Read or create the state of one lead agent. */
|
|
517
|
+
ensure(agent, sessionId) {
|
|
518
|
+
const existing = this.byAgent.get(agent) ?? this.bySession.get(sessionId);
|
|
519
|
+
if (existing !== undefined) {
|
|
520
|
+
this.byAgent.set(agent, existing);
|
|
521
|
+
return existing;
|
|
522
|
+
}
|
|
523
|
+
const created = createState(sessionId, this.config().defaultMode);
|
|
524
|
+
this.byAgent.set(agent, created);
|
|
525
|
+
this.bySession.set(sessionId, created);
|
|
526
|
+
return created;
|
|
527
|
+
}
|
|
528
|
+
/** Forget one agent's state, releasing its tool-table restriction. */
|
|
529
|
+
release(agent, sessionId) {
|
|
530
|
+
const state = this.byAgent.get(agent);
|
|
531
|
+
state?.tableRestriction?.();
|
|
532
|
+
this.byAgent.delete(agent);
|
|
533
|
+
if (sessionId !== undefined)
|
|
534
|
+
this.bySession.delete(sessionId);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
/** Read a session id from a live Agent without importing the runtime class. */
|
|
538
|
+
export function sessionIdOf(agent) {
|
|
539
|
+
if (agent === undefined)
|
|
540
|
+
return undefined;
|
|
541
|
+
const value = agent.session?.header?.id;
|
|
542
|
+
return typeof value === 'string' ? value : undefined;
|
|
543
|
+
}
|
|
544
|
+
/** Whether the agent is the lead of its own conversation (never a delegated child). */
|
|
545
|
+
export function isLeadAgent(agent) {
|
|
546
|
+
if (agent === undefined)
|
|
547
|
+
return false;
|
|
548
|
+
const depth = agent.session?.header?.delegationDepth;
|
|
549
|
+
return typeof depth !== 'number' || depth === 0;
|
|
550
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The operating system's preferred languages, read once per Host process.
|
|
3
|
+
*
|
|
4
|
+
* DSH desktop launches the Host with the user's login-shell environment, so on
|
|
5
|
+
* macOS `LANG` is frequently `C.UTF-8` or unset while the window itself runs in
|
|
6
|
+
* the system language. Node's `Intl` default follows those variables instead of
|
|
7
|
+
* the OS, so the system list (`AppleLanguages`) is the only host-reachable
|
|
8
|
+
* signal for what the interface language actually is. Windows and Linux report
|
|
9
|
+
* the system language through `Intl` already, so they need no probe.
|
|
10
|
+
*
|
|
11
|
+
* This module is Host-only: `src/language.ts` stays free of Node builtins
|
|
12
|
+
* because the browser bundle imports it through `config.ts`.
|
|
13
|
+
*/
|
|
14
|
+
import { execFileSync } from 'node:child_process';
|
|
15
|
+
/** Probe timeout: a hung `defaults` must not stall prompt assembly. */
|
|
16
|
+
const PROBE_TIMEOUT_MS = 1000;
|
|
17
|
+
let cached;
|
|
18
|
+
/**
|
|
19
|
+
* Parse `defaults read -g AppleLanguages`, a NeXTSTEP property-list array of
|
|
20
|
+
* quoted BCP 47 tags such as `("zh-Hans-CN", "en-US")`.
|
|
21
|
+
*/
|
|
22
|
+
export function parseAppleLanguages(output) {
|
|
23
|
+
const languages = [];
|
|
24
|
+
for (const match of output.matchAll(/"([^"]+)"|^\s*([A-Za-z][A-Za-z0-9-]*)\s*,?\s*$/gmu)) {
|
|
25
|
+
const value = (match[1] ?? match[2] ?? '').trim();
|
|
26
|
+
if (value !== '')
|
|
27
|
+
languages.push(value);
|
|
28
|
+
}
|
|
29
|
+
return languages;
|
|
30
|
+
}
|
|
31
|
+
function readAppleLanguages() {
|
|
32
|
+
try {
|
|
33
|
+
const output = execFileSync('/usr/bin/defaults', ['read', '-g', 'AppleLanguages'], {
|
|
34
|
+
encoding: 'utf8',
|
|
35
|
+
timeout: PROBE_TIMEOUT_MS,
|
|
36
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
37
|
+
windowsHide: true,
|
|
38
|
+
});
|
|
39
|
+
return parseAppleLanguages(output);
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
// A denied or missing `defaults` only means "no OS signal"; the caller
|
|
43
|
+
// falls back to the POSIX variables and `Intl`.
|
|
44
|
+
return [];
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* OS interface languages in preference order. Empty on platforms whose Node
|
|
49
|
+
* runtime already follows the system locale.
|
|
50
|
+
*/
|
|
51
|
+
export function systemLanguages() {
|
|
52
|
+
if (cached === undefined) {
|
|
53
|
+
cached = process.platform === 'darwin' ? readAppleLanguages() : [];
|
|
54
|
+
}
|
|
55
|
+
return cached;
|
|
56
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export interface PatchAppliedFile {
|
|
2
|
+
path: string;
|
|
3
|
+
operation: 'created' | 'updated' | 'deleted' | 'moved';
|
|
4
|
+
moveTo?: string;
|
|
5
|
+
}
|
|
6
|
+
export interface PatchResult {
|
|
7
|
+
files: PatchAppliedFile[];
|
|
8
|
+
}
|
|
9
|
+
/** Parse and apply one Codex-format patch. Exported for focused tests. */
|
|
10
|
+
export declare function applyPatchInput(ctx: any, input: string, exec: any): Promise<PatchResult>;
|
|
11
|
+
export declare function renderPatchResult(value: PatchResult): string;
|
|
12
|
+
/** Register `apply_patch` in an agent scope that has the filesystem service. */
|
|
13
|
+
export declare function registerApplyPatch(ctx: any, enabled?: () => boolean): void;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { PropsLocale, PropsRuntime } from '@deepseek-ai/dsh-client-ui-slots';
|
|
2
|
+
/** Props the keyed Chat renderer receives from the Chat owner. */
|
|
3
|
+
type CapsuleProps = PropsRuntime<'conversation.chat.node', 'coding-stage'> & PropsLocale<'dsh-coding'>;
|
|
4
|
+
/**
|
|
5
|
+
* One turn's Coding stage capsule.
|
|
6
|
+
* @param props - Chat renderer props and the `dsh-coding` translate seat.
|
|
7
|
+
*/
|
|
8
|
+
export declare const CodingStageCapsule: import("react").NamedExoticComponent<CapsuleProps>;
|
|
9
|
+
export {};
|