dsh-coding 0.0.1 → 0.1.1-rc2

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.
Files changed (78) hide show
  1. package/AGENTS.md +80 -0
  2. package/LICENSE +21 -21
  3. package/README.md +120 -54
  4. package/cordis.patch.yml +274 -4
  5. package/docs/CHANGELOG.md +47 -0
  6. package/docs/DETAIL.md +272 -0
  7. package/icon.svg +6 -6
  8. package/lib/apply-patch.js +268 -0
  9. package/lib/client/capsule.js +120 -0
  10. package/lib/client/card.js +308 -0
  11. package/lib/client/command-row.js +76 -0
  12. package/lib/client/config-page.js +392 -0
  13. package/lib/client/definition.js +164 -0
  14. package/lib/client/index.js +129 -0
  15. package/lib/client/locales.js +170 -0
  16. package/lib/client/mode-picker.js +77 -0
  17. package/lib/client/styles.js +283 -0
  18. package/lib/client.js +1802 -0
  19. package/lib/coding-projection.js +349 -0
  20. package/lib/config.js +218 -0
  21. package/lib/contract.js +138 -0
  22. package/lib/editor.js +23 -0
  23. package/lib/failures.js +142 -0
  24. package/lib/handoff.js +347 -0
  25. package/lib/index.js +110 -7
  26. package/lib/language.js +136 -0
  27. package/lib/memory.js +336 -0
  28. package/lib/messages.js +43 -0
  29. package/lib/models.js +154 -0
  30. package/lib/patch.js +235 -0
  31. package/lib/preset.js +1552 -0
  32. package/lib/reasoning-levels.js +28 -0
  33. package/lib/reasoning-projection.js +178 -0
  34. package/lib/reasoning.js +585 -0
  35. package/lib/stages.js +344 -0
  36. package/lib/state.js +585 -0
  37. package/lib/system-language.js +56 -0
  38. package/lib/types/apply-patch.d.ts +13 -0
  39. package/lib/types/client/capsule.d.ts +9 -0
  40. package/lib/types/client/card.d.ts +76 -0
  41. package/lib/types/client/command-row.d.ts +42 -0
  42. package/lib/types/client/config-page.d.ts +136 -0
  43. package/lib/types/client/definition.d.ts +51 -0
  44. package/lib/types/client/index.d.ts +10 -0
  45. package/lib/types/client/locales.d.ts +18 -0
  46. package/lib/types/client/mode-picker.d.ts +25 -0
  47. package/lib/types/client/styles.d.ts +28 -0
  48. package/lib/types/coding-projection.d.ts +116 -0
  49. package/lib/types/config.d.ts +208 -0
  50. package/lib/types/contract.d.ts +33 -0
  51. package/lib/types/editor.d.ts +9 -0
  52. package/lib/types/failures.d.ts +62 -0
  53. package/lib/types/handoff.d.ts +132 -0
  54. package/lib/types/index.d.ts +150 -0
  55. package/lib/types/language.d.ts +56 -0
  56. package/lib/types/memory.d.ts +71 -0
  57. package/lib/types/messages.d.ts +27 -0
  58. package/lib/types/models.d.ts +82 -0
  59. package/lib/types/patch.d.ts +37 -0
  60. package/lib/types/preset.d.ts +22 -0
  61. package/lib/types/reasoning-levels.d.ts +20 -0
  62. package/lib/types/reasoning-projection.d.ts +64 -0
  63. package/lib/types/reasoning.d.ts +141 -0
  64. package/lib/types/stages.d.ts +193 -0
  65. package/lib/types/state.d.ts +273 -0
  66. package/lib/types/system-language.d.ts +10 -0
  67. package/lib/types/verification-tool.d.ts +32 -0
  68. package/lib/types/verification.d.ts +77 -0
  69. package/lib/types/verifier.d.ts +9 -0
  70. package/lib/types/volatile.d.ts +17 -0
  71. package/lib/verification-tool.js +176 -0
  72. package/lib/verification.js +337 -0
  73. package/lib/verifier.js +25 -0
  74. package/lib/volatile.js +20 -0
  75. package/locale/en.json +3 -2
  76. package/locale/zh.json +3 -2
  77. package/package.json +84 -9
  78. package/lib/index.d.ts +0 -11
package/lib/stages.js ADDED
@@ -0,0 +1,344 @@
1
+ /**
2
+ * The enforced coding stages and the deterministic tool policy that belongs to
3
+ * each one. This module is deliberately dependency-free: the Host preset, the
4
+ * worker presets, and the Web client all import it, so it must never pull in a
5
+ * Node-only or React-only module.
6
+ *
7
+ * @module dsh-coding/stages
8
+ */
9
+ import { asFailure } from './failures.js';
10
+ import { asHandoff } from './handoff.js';
11
+ /** Every stage, in forward order. */
12
+ export const CODING_STAGES = ['requirements', 'plan', 'implement', 'verify', 'deliver'];
13
+ /** Every mode, in roster order. */
14
+ export const CODING_MODES = ['auto', 'consult', 'review', 'codex'];
15
+ /**
16
+ * The stages a mode opens. A mode never gains a stage at runtime: the set is
17
+ * fixed here and is the first thing `coding_stage` checks.
18
+ */
19
+ export const STAGES_BY_MODE = {
20
+ // Auto opens only requirements because the requirements stage chooses a concrete workflow
21
+ // with `coding_mode`; the chosen mode then supplies its own stage set.
22
+ auto: ['requirements'],
23
+ consult: ['requirements', 'plan', 'deliver'],
24
+ review: ['requirements', 'verify', 'deliver'],
25
+ codex: ['requirements', 'plan', 'implement', 'verify', 'deliver'],
26
+ };
27
+ /** Stage at which a session of this mode starts. */
28
+ export function entryStage(mode) {
29
+ return STAGES_BY_MODE[mode][0];
30
+ }
31
+ /** Whether `stage` belongs to `mode`. */
32
+ export function stageInMode(mode, stage) {
33
+ return STAGES_BY_MODE[mode].includes(stage);
34
+ }
35
+ /** Model-facing explanation of the mode's stage set, e.g. `requirements → plan → deliver`. */
36
+ export function stagePath(mode) {
37
+ return STAGES_BY_MODE[mode].join(' → ');
38
+ }
39
+ /** The workflow and control verbs this package registers by name. */
40
+ export const TOOL = {
41
+ stage: 'coding_stage',
42
+ mode: 'coding_mode',
43
+ reasoning: 'coding_reasoning',
44
+ handoff: 'coding_handoff',
45
+ implement: 'delegate_implement',
46
+ verify: 'delegate_verify',
47
+ plan: 'delegate_plan',
48
+ verification: 'run_verification',
49
+ };
50
+ /** Every tool name this package owns; the Web Definition matches on exactly these. */
51
+ export const CODING_TOOLS = Object.values(TOOL);
52
+ /** Whether a model-facing tool name belongs to this package. */
53
+ export function isCodingTool(name) {
54
+ return CODING_TOOLS.includes(name);
55
+ }
56
+ const TOOL_CLASSES = {
57
+ read: 'read',
58
+ read_image: 'read',
59
+ glob: 'read',
60
+ grep: 'read',
61
+ web_search: 'read',
62
+ web_fetch: 'read',
63
+ job_output: 'read',
64
+ job_list: 'read',
65
+ skill: 'read',
66
+ list_agents: 'read',
67
+ list_subagent_models: 'read',
68
+ todo_write: 'plan',
69
+ write: 'write',
70
+ edit: 'write',
71
+ apply_patch: 'write',
72
+ bash: 'exec',
73
+ pwsh: 'exec',
74
+ job_kill: 'exec',
75
+ terminal_open: 'exec',
76
+ terminal_read: 'exec',
77
+ terminal_signal: 'exec',
78
+ run_verification: 'verify',
79
+ delegate_verify: 'verify',
80
+ delegate_implement: 'delegate',
81
+ delegate_plan: 'delegate',
82
+ coding_stage: 'stage',
83
+ coding_mode: 'mode',
84
+ coding_reasoning: 'reasoning',
85
+ coding_handoff: 'stage',
86
+ };
87
+ /**
88
+ * Classify a tool. Unknown (third-party) tools stay `undefined`: the gate owns
89
+ * the coding toolset it can reason about and never silently blocks an
90
+ * extension whose semantics it does not know.
91
+ */
92
+ export function toolClass(name) {
93
+ return TOOL_CLASSES[name];
94
+ }
95
+ /** Classes each stage admits for the lead agent. */
96
+ const STAGE_CLASSES = {
97
+ // Requirements is observation: no file mutation, no plan authoring, no verification.
98
+ requirements: ['read', 'exec', 'stage', 'mode', 'reasoning'],
99
+ // Planning may inspect and must author the structured task list.
100
+ plan: ['read', 'exec', 'plan', 'delegate', 'stage', 'mode', 'reasoning'],
101
+ // Implementation is delegated: the lead keeps the task list, the shell it needs
102
+ // for installs and one-off commands, and the verbs. Editing stays the worker's.
103
+ implement: ['delegate', 'plan', 'exec', 'stage', 'mode', 'reasoning'],
104
+ // Verification is read + a structured check, plus ad-hoc commands to reproduce a
105
+ // failure; delegation stays available and the task list must be closable.
106
+ verify: ['read', 'verify', 'exec', 'plan', 'stage', 'mode', 'reasoning'],
107
+ // Delivery reports and closes the task list; nothing may mutate source, run a
108
+ // command, or start a fresh verification.
109
+ deliver: ['read', 'plan', 'stage', 'mode', 'reasoning'],
110
+ };
111
+ /** Whether `stage` admits the tool class `kind`. */
112
+ export function stageAdmits(stage, kind) {
113
+ return STAGE_CLASSES[stage].includes(kind);
114
+ }
115
+ /** Human-readable list of the classes a stage admits, for a denial message. */
116
+ export function stageAdmissions(stage) {
117
+ return STAGE_CLASSES[stage].join(', ');
118
+ }
119
+ /**
120
+ * The lead's tool table during `implement`: exactly these names remain visible.
121
+ * Naming the allow-list (rather than denying individual tools) is what makes the
122
+ * trim total, including tools a later plugin registers.
123
+ * `bash` and `pwsh` are both listed because only one exists per platform;
124
+ * `syncTable` drops the absent one, since `tools.restrict` rejects names the
125
+ * scope does not register.
126
+ */
127
+ export const IMPLEMENT_TABLE = [
128
+ TOOL.implement,
129
+ TOOL.handoff,
130
+ 'todo_write',
131
+ 'bash',
132
+ 'pwsh',
133
+ TOOL.stage,
134
+ TOOL.mode,
135
+ TOOL.reasoning,
136
+ ];
137
+ /** The worker presets this package declares and dispatches to. */
138
+ export const WORKER_PRESETS = {
139
+ planner: 'dsh-coding-planner',
140
+ implement: 'dsh-coding-implement',
141
+ verify: 'dsh-coding-verify',
142
+ verifyWithInstructions: 'dsh-coding-verify-instructions',
143
+ };
144
+ /** 触发 verify 注入仓库指令的最小子任务数:多个子任务意味着跨任务依赖。 */
145
+ export const VERIFY_INSTRUCTION_TASK_THRESHOLD = 2;
146
+ /** 触发 verify 注入仓库指令的最小改动文件数:改动面大时需要仓库规则。 */
147
+ export const VERIFY_INSTRUCTION_FILE_THRESHOLD = 3;
148
+ /**
149
+ * verify 是否应加载仓库指令:仅 codex 模式,且(多子任务)或(改动文件多)。
150
+ * 单任务单文件的核查不注入,避免为一次检查付出整份 AGENTS.md 的上下文。
151
+ */
152
+ export function needsWorkspaceInstructions(demand) {
153
+ if (demand.mode !== 'codex')
154
+ return false;
155
+ return demand.taskCount >= VERIFY_INSTRUCTION_TASK_THRESHOLD
156
+ || demand.changedFiles >= VERIFY_INSTRUCTION_FILE_THRESHOLD;
157
+ }
158
+ /** The stable route identity a session's coding run is checkpointed under. */
159
+ export const CHECKPOINT_VERSION = 1;
160
+ /**
161
+ * Validate one untyped value as a {@link CodingCheckpoint}. The client reads
162
+ * checkpoints out of `tool/result.meta`, which is durable JSON written by an
163
+ * arbitrary earlier build of this plugin, so every field is re-checked here.
164
+ */
165
+ export function asCheckpoint(value) {
166
+ if (typeof value !== 'object' || value === null)
167
+ return undefined;
168
+ const v = value;
169
+ if (v['v'] !== CHECKPOINT_VERSION)
170
+ return undefined;
171
+ if (typeof v['runId'] !== 'string' || v['runId'] === '')
172
+ return undefined;
173
+ if (!isFiniteNumber(v['turn']) || !isFiniteNumber(v['step']))
174
+ return undefined;
175
+ if (!isMode(v['mode']))
176
+ return undefined;
177
+ const stage = asStage(v['stage']);
178
+ if (stage === undefined)
179
+ return undefined;
180
+ const allowedStages = Array.isArray(v['allowedStages'])
181
+ ? v['allowedStages'].map(asStage)
182
+ : undefined;
183
+ if (allowedStages === undefined || allowedStages.some(item => item === undefined))
184
+ return undefined;
185
+ if (!isFiniteNumber(v['planItems']) || !isFiniteNumber(v['openPlanItems']))
186
+ return undefined;
187
+ if (!isFiniteNumber(v['edits']) || !isFiniteNumber(v['updatedAt']))
188
+ return undefined;
189
+ const files = v['files'];
190
+ if (files !== undefined && (!Array.isArray(files) || !files.every(item => typeof item === 'string' && item !== '')))
191
+ return undefined;
192
+ const verification = asVerification(v['verification']);
193
+ const worker = asWorker(v['worker']);
194
+ const refusal = asRefusal(v['refusal']);
195
+ // A handoff that is missing, stale, or malformed reads as "no handoff": a
196
+ // history page must not lose the whole card to junk from an earlier build.
197
+ const handoff = asHandoff(v['handoff']);
198
+ // A failure that is missing or malformed reads as "no failure", by the same
199
+ // tolerance: one junk field must not drop the whole card.
200
+ const failure = asFailure(v['failure']);
201
+ // The retry counters are newer fields, so absent means an older checkpoint;
202
+ // a present value that does not validate is a corrupt checkpoint.
203
+ const attemptsValue = v['verifyAttempts'];
204
+ const verifyAttempts = isFiniteNumber(attemptsValue) && attemptsValue >= 0 ? attemptsValue : undefined;
205
+ if (attemptsValue !== undefined && verifyAttempts === undefined)
206
+ return undefined;
207
+ const blockedValue = v['verifyBlocked'];
208
+ const verifyBlocked = asVerifyBlocked(blockedValue);
209
+ if (blockedValue !== undefined && verifyBlocked === undefined)
210
+ return undefined;
211
+ const plannerValue = v['planner'];
212
+ const planner = asPlanner(plannerValue);
213
+ if (plannerValue !== undefined && planner === undefined)
214
+ return undefined;
215
+ return {
216
+ v: CHECKPOINT_VERSION,
217
+ runId: v['runId'],
218
+ turn: v['turn'],
219
+ step: v['step'],
220
+ mode: v['mode'],
221
+ stage,
222
+ allowedStages: allowedStages,
223
+ planItems: v['planItems'],
224
+ openPlanItems: v['openPlanItems'],
225
+ edits: v['edits'],
226
+ ...files === undefined ? {} : { files: files },
227
+ ...verification === undefined ? {} : { verification },
228
+ ...handoff === undefined ? {} : { handoff },
229
+ ...verifyAttempts === undefined ? {} : { verifyAttempts },
230
+ ...verifyBlocked === undefined ? {} : { verifyBlocked },
231
+ ...worker === undefined ? {} : { worker },
232
+ ...planner === undefined ? {} : { planner },
233
+ ...refusal === undefined ? {} : { refusal },
234
+ ...failure === undefined ? {} : { failure },
235
+ updatedAt: v['updatedAt'],
236
+ };
237
+ }
238
+ /** Stage ids persisted by earlier builds, read back as their renamed current value. */
239
+ const LEGACY_STAGES = { recon: 'requirements' };
240
+ /**
241
+ * Read one stage value, accepting the ids an earlier build persisted. The
242
+ * durable projection and checkpoints store stages as strings, so a rename must
243
+ * keep old sessions readable.
244
+ * @param value - raw persisted value.
245
+ * @returns the current stage id, mapping legacy ids, or undefined when unknown.
246
+ */
247
+ export function asStage(value) {
248
+ if (typeof value !== 'string')
249
+ return undefined;
250
+ if (CODING_STAGES.includes(value))
251
+ return value;
252
+ return LEGACY_STAGES[value];
253
+ }
254
+ function isFiniteNumber(value) {
255
+ return typeof value === 'number' && Number.isFinite(value);
256
+ }
257
+ function isMode(value) {
258
+ return typeof value === 'string' && CODING_MODES.includes(value);
259
+ }
260
+ function isStage(value) {
261
+ return asStage(value) !== undefined;
262
+ }
263
+ function asVerification(value) {
264
+ if (typeof value !== 'object' || value === null)
265
+ return undefined;
266
+ const v = value;
267
+ if (typeof v['passed'] !== 'boolean')
268
+ return undefined;
269
+ if (typeof v['kind'] !== 'string' || typeof v['scope'] !== 'string')
270
+ return undefined;
271
+ if (v['source'] !== 'lead' && v['source'] !== 'worker')
272
+ return undefined;
273
+ if (typeof v['command'] !== 'string' || typeof v['durationMs'] !== 'number')
274
+ return undefined;
275
+ if (!isFiniteNumber(v['ranAt']))
276
+ return undefined;
277
+ const exitCode = v['exitCode'];
278
+ if (exitCode !== null && typeof exitCode !== 'number')
279
+ return undefined;
280
+ return {
281
+ passed: v['passed'],
282
+ kind: v['kind'],
283
+ scope: v['scope'],
284
+ source: v['source'],
285
+ command: v['command'],
286
+ exitCode,
287
+ durationMs: v['durationMs'],
288
+ failedTests: stringArray(v['failedTests']),
289
+ errors: stringArray(v['errors']),
290
+ ranAt: v['ranAt'],
291
+ };
292
+ }
293
+ function asWorker(value) {
294
+ if (typeof value !== 'object' || value === null)
295
+ return undefined;
296
+ const v = value;
297
+ if (v['role'] !== 'plan' && v['role'] !== 'implement' && v['role'] !== 'verify')
298
+ return undefined;
299
+ if (v['status'] !== 'running' && v['status'] !== 'completed' && v['status'] !== 'failed')
300
+ return undefined;
301
+ if (!isFiniteNumber(v['startedAt']))
302
+ return undefined;
303
+ return {
304
+ role: v['role'],
305
+ status: v['status'],
306
+ ...typeof v['childId'] === 'string' ? { childId: v['childId'] } : {},
307
+ ...typeof v['summary'] === 'string' ? { summary: v['summary'] } : {},
308
+ ...Array.isArray(v['files']) ? { files: stringArray(v['files']) } : {},
309
+ startedAt: v['startedAt'],
310
+ ...isFiniteNumber(v['finishedAt']) ? { finishedAt: v['finishedAt'] } : {},
311
+ };
312
+ }
313
+ function asPlanner(value) {
314
+ if (typeof value !== 'object' || value === null)
315
+ return undefined;
316
+ const v = value;
317
+ if (typeof v['route'] !== 'string' || v['route'] === '')
318
+ return undefined;
319
+ if (v['outcome'] !== 'completed' && v['outcome'] !== 'failed')
320
+ return undefined;
321
+ if (!isFiniteNumber(v['at']))
322
+ return undefined;
323
+ return { route: v['route'], outcome: v['outcome'], at: v['at'] };
324
+ }
325
+ function asRefusal(value) {
326
+ if (typeof value !== 'object' || value === null)
327
+ return undefined;
328
+ const v = value;
329
+ const target = asStage(v['target']);
330
+ if (target === undefined || typeof v['reason'] !== 'string' || !isFiniteNumber(v['at']))
331
+ return undefined;
332
+ return { target, reason: v['reason'], at: v['at'] };
333
+ }
334
+ function asVerifyBlocked(value) {
335
+ if (typeof value !== 'object' || value === null)
336
+ return undefined;
337
+ const v = value;
338
+ if (!isFiniteNumber(v['attempts']) || typeof v['reason'] !== 'string' || !isFiniteNumber(v['at']))
339
+ return undefined;
340
+ return { attempts: v['attempts'], reason: v['reason'], at: v['at'] };
341
+ }
342
+ function stringArray(value) {
343
+ return Array.isArray(value) ? value.filter((item) => typeof item === 'string') : [];
344
+ }