dsh-extra-plan 0.0.0-stage → 1.7.0

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 (56) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +179 -2
  3. package/assets/presets/extra-plan/agent.cordis.yml +580 -0
  4. package/assets/presets/extra-plan/preset-definition-extra-plan-executor.generated.yml +115 -0
  5. package/assets/presets/extra-plan/preset-definition-extra-plan-planner.generated.yml +113 -0
  6. package/assets/presets/extra-plan/preset-definition-extra-plan-prober.generated.yml +107 -0
  7. package/assets/presets/extra-plan/preset-definition-extra-plan-reviewer.generated.yml +115 -0
  8. package/assets/presets/extra-plan/preset-definition.generated.yml +335 -0
  9. package/assets/presets/extra-plan/preset.yml +2 -0
  10. package/cordis.patch.yml +7 -0
  11. package/index.js +1671 -0
  12. package/lib/agent-runtime.js +172 -0
  13. package/lib/agent-session.js +65 -0
  14. package/lib/assembly-presentation.js +267 -0
  15. package/lib/client-carrier.js +7 -0
  16. package/lib/client.js +314 -0
  17. package/lib/extra-plan-gate-tool.js +387 -0
  18. package/lib/gate-decisions.js +744 -0
  19. package/lib/gate-words.js +108 -0
  20. package/lib/live-config.js +293 -0
  21. package/lib/log-retention.js +132 -0
  22. package/lib/model-routing.js +648 -0
  23. package/lib/path-policy.js +464 -0
  24. package/lib/plan-draft-tool.js +294 -0
  25. package/lib/planner-budget.js +136 -0
  26. package/lib/preset-defaults.generated.js +17 -0
  27. package/lib/preset-settings.js +3 -0
  28. package/lib/preset-sync.js +809 -0
  29. package/lib/preset-yaml.js +438 -0
  30. package/lib/role-contract.js +11 -0
  31. package/lib/run-code-scanner.js +360 -0
  32. package/lib/run-code-static.js +621 -0
  33. package/lib/runtime-diagnostics.js +89 -0
  34. package/lib/runtime-lifecycle.js +162 -0
  35. package/lib/runtime-static.js +23 -0
  36. package/lib/save-contract.js +263 -0
  37. package/lib/save-persistence.js +223 -0
  38. package/lib/save-plan-markdown-parser.js +369 -0
  39. package/lib/save-plan-validation-diagnostics.js +49 -0
  40. package/lib/save-plan-validation.js +812 -0
  41. package/lib/save-probe-validation.js +173 -0
  42. package/lib/save-source-verification.js +195 -0
  43. package/lib/save-tool-factories.js +270 -0
  44. package/lib/sdk-text-cache.js +161 -0
  45. package/lib/settings-contract.js +191 -0
  46. package/lib/settings.js +369 -0
  47. package/lib/shell-mutation.js +484 -0
  48. package/lib/usage-ledger-background.js +214 -0
  49. package/lib/usage-ledger-worker.js +116 -0
  50. package/lib/usage-ledger.js +1393 -0
  51. package/locale/en.json +6 -0
  52. package/locale/settings/en.json +6 -0
  53. package/locale/settings/zh.json +6 -0
  54. package/locale/zh.json +6 -0
  55. package/package.json +56 -4
  56. package/scripts/generate-runtime-defaults.mjs +796 -0
@@ -0,0 +1,369 @@
1
+ // dsh-extra-plan-settings 的唯一全局管理入口。
2
+ // settings 行 config 是全部 11 个权威值的载体;gateWords 由 preset-sync 的写回路径严格校验并保留。
3
+ // 本入口统一拥有 settings/carrier、当前值补齐;AgentPreset 仅由 preset-sync 初始/结构路径管理。
4
+ // 1 项宿主设置(toolPresentationMode)只从权威值派生到内存 definition,不再写独立公开投影行。
5
+
6
+ import { resolve as pathResolve } from 'node:path'
7
+ import z from '@deepseek-ai/schemastery'
8
+ import {
9
+ SETTING_DEFINITIONS,
10
+ SETTINGS_ROW_ID,
11
+ } from './preset-settings.js'
12
+ import { mountPresetSync } from './preset-sync.js'
13
+ import { createPathContext } from './path-policy.js'
14
+ import { startUsageLedgerBackground } from './usage-ledger-background.js'
15
+ import { pruneDiagnosticLogs } from './log-retention.js'
16
+
17
+ const DIAGNOSTIC_PRUNE_STATE = Symbol.for('dsh-extra-plan.diagnostic-prune-state')
18
+
19
+ function safeContextGet(ctx, key) {
20
+ try { return ctx !== undefined && ctx !== null && typeof ctx.get === 'function' ? ctx.get(key) : undefined } catch (error) { return undefined }
21
+ }
22
+
23
+ function absoluteRoot(value) {
24
+ if (typeof value !== 'string' || value.trim() === '') return null
25
+ try {
26
+ return createPathContext({ workspaceRoot: value }).workspaceRoot
27
+ } catch {
28
+ return null
29
+ }
30
+ }
31
+
32
+ function rootFact(value, present) {
33
+ if (present !== true) return { state: 'missing', path: null }
34
+ const normalized = absoluteRoot(value)
35
+ return normalized === null ? { state: 'invalid', path: null } : { state: 'valid', path: normalized }
36
+ }
37
+
38
+ function sameRoot(left, right) {
39
+ if (left === null || right === null) return false
40
+ const a = pathResolve(left)
41
+ const b = pathResolve(right)
42
+ return process.platform === 'win32' ? a.toLowerCase() === b.toLowerCase() : a === b
43
+ }
44
+
45
+ function documentFact(value, present) {
46
+ if (present !== true || value === undefined || value === null || value === '') return { state: 'missing', path: null }
47
+ const normalized = absoluteRoot(value)
48
+ return normalized === null ? { state: 'invalid', path: null } : { state: 'valid', path: normalized }
49
+ }
50
+
51
+ function configEditorServiceOf(ctx) {
52
+ const service = safeContextGet(ctx, 'configEditor')
53
+ return service !== undefined && service !== null && typeof service.configuration === 'function' && typeof service.edit === 'function' ? service : null
54
+ }
55
+
56
+ function settingsPathContext(ctx, editor) {
57
+ const profile = safeContextGet(ctx, 'profileContext')
58
+ const has = (key) => profile !== undefined && profile !== null && Object.prototype.hasOwnProperty.call(profile, key)
59
+ const profileHomeFact = rootFact(profile?.home, has('home'))
60
+ const profilePatchFact = rootFact(profile?.patchPath, has('patchPath'))
61
+ const installAnchorFact = rootFact(profile?.installAnchor, has('installAnchor'))
62
+ const service = configEditorServiceOf(ctx)
63
+ const actualEditor = service !== null && service === editor
64
+ let editorDocumentFact = { state: 'missing', path: null }
65
+ if (actualEditor) {
66
+ try { editorDocumentFact = documentFact(editor.documentPath, editor.documentPath !== undefined) } catch (error) { editorDocumentFact = { state: 'invalid', path: null } }
67
+ }
68
+ let documentState = 'missing'
69
+ let documentPath = null
70
+ let documentCapability = null
71
+ if (profilePatchFact.state === 'invalid' || editorDocumentFact.state === 'invalid') documentState = 'invalid'
72
+ else if (profilePatchFact.state === 'valid' && editorDocumentFact.state === 'valid' && !sameRoot(profilePatchFact.path, editorDocumentFact.path)) documentState = 'conflict'
73
+ else if (editorDocumentFact.state === 'valid') {
74
+ documentState = 'valid'
75
+ documentPath = editorDocumentFact.path
76
+ documentCapability = profilePatchFact.state === 'valid' ? 'profilePatch' : 'editorDocument'
77
+ } else if (profilePatchFact.state === 'valid') {
78
+ documentState = 'valid'
79
+ documentPath = profilePatchFact.path
80
+ documentCapability = 'profilePatch'
81
+ }
82
+ const homeProvider = safeContextGet(ctx, 'dshHomePath')
83
+ let dshHomeFact = { state: 'missing', path: null }
84
+ if (typeof homeProvider === 'function') {
85
+ try { dshHomeFact = rootFact(homeProvider(), true) } catch (error) { dshHomeFact = { state: 'invalid', path: null } }
86
+ }
87
+ const envPresent = Object.prototype.hasOwnProperty.call(process.env, 'DSH_HOME')
88
+ const envHomeFact = rootFact(process.env.DSH_HOME, envPresent)
89
+ const trustedSources = [profileHomeFact, dshHomeFact]
90
+ const invalidTrustedSource = trustedSources.some((item) => item.state === 'invalid')
91
+ const validSources = trustedSources.filter((item) => item.state === 'valid')
92
+ const trustedConflict = validSources.some((item) => !sameRoot(item.path, validSources[0]?.path))
93
+ const envConflict = validSources.length > 0 && envHomeFact.state === 'valid' && !sameRoot(envHomeFact.path, validSources[0]?.path)
94
+ const envInvalidWithTrustedRoot = validSources.length > 0 && envHomeFact.state === 'invalid'
95
+ const hostConflict = trustedConflict || envConflict
96
+ const hostState = invalidTrustedSource || envInvalidWithTrustedRoot ? 'invalid' : hostConflict ? 'conflict' : validSources.length === 0 ? 'missing' : 'valid'
97
+ const hostDataRoot = hostState === 'valid' ? validSources[0].path : null
98
+ const capabilities = {}
99
+ if (profilePatchFact.state === 'valid') capabilities.profilePatch = { path: profilePatchFact.path, type: 'file', access: 'write', purpose: 'config-editor' }
100
+ if (documentCapability === 'editorDocument' && documentPath !== null) capabilities.editorDocument = { path: documentPath, type: 'file', access: 'read', purpose: 'config-editor' }
101
+ if (installAnchorFact.state === 'valid') capabilities.installAnchor = { path: installAnchorFact.path, type: 'file', access: 'read', purpose: 'install-anchor' }
102
+ const options = { hostCapabilities: capabilities }
103
+ if (hostState === 'valid' && hostDataRoot !== null) options.hostDataRoot = hostDataRoot
104
+ if (options.hostDataRoot === undefined && Object.keys(capabilities).length === 0) return null
105
+ let pathContext
106
+ try { pathContext = createPathContext(options) } catch (error) { return null }
107
+ return pathContext
108
+ }
109
+
110
+ export const name = 'dsh-extra-plan-settings'
111
+ export const inject = []
112
+
113
+ const CLIENT_CARRIER_ENTRY_ID = 'dsh-extra-plan-client-carrier'
114
+ const carrierByLoader = new WeakMap()
115
+ function mountClientCarrier(child) {
116
+ const loader = typeof child.get === 'function' ? child.get('loader') : child.loader
117
+ const Group = loader !== undefined && loader !== null && loader.builtins !== undefined ? loader.builtins.group : undefined
118
+ if (loader === undefined || loader === null || typeof Group !== 'function') {
119
+ console.warn('[dsh-extra-plan] 宿主未提供 Loader Group,Client carrier 未挂载')
120
+ return undefined
121
+ }
122
+ let shared = carrierByLoader.get(loader)
123
+ if (shared === undefined) {
124
+ const fiber = child.plugin(Group, [{
125
+ id: CLIENT_CARRIER_ENTRY_ID,
126
+ name: new URL('./client-carrier.js', import.meta.url).href,
127
+ }])
128
+ shared = { fiber, refs: 0 }
129
+ carrierByLoader.set(loader, shared)
130
+ }
131
+ shared.refs += 1
132
+ let released = false
133
+ return () => {
134
+ if (released) return undefined
135
+ released = true
136
+ shared.refs -= 1
137
+ if (shared.refs === 0) {
138
+ carrierByLoader.delete(loader)
139
+ const fiber = shared.fiber
140
+ shared.fiber = undefined
141
+ return fiber.dispose()
142
+ }
143
+ return undefined
144
+ }
145
+ }
146
+
147
+ /** descriptor scalarType → schemastery 字段;合同负责类型/默认值,settings 只负责宿主 schema 链。 */
148
+ function schemaField(definition) {
149
+ let field
150
+ if (definition.scalarType === 'boolean') field = z.boolean()
151
+ else if (definition.scalarType === 'integer') field = z.number().step(1).min(1)
152
+ else field = z.string()
153
+ return field.default(definition.defaultValue).volatile()
154
+ }
155
+
156
+ /** 11 项 settings 全局行 Config;volatile 字段由官方 ConfigForms 投影。 */
157
+ export const Config = z.object(Object.fromEntries(
158
+ SETTING_DEFINITIONS.map((definition) => [definition.key, schemaField(definition)]),
159
+ ))
160
+
161
+ function diagnosticPruneRootOf(ctx) {
162
+ try {
163
+ if (ctx === null || ctx === undefined) return null
164
+ const root = ctx.root !== undefined && ctx.root !== null ? ctx.root : ctx
165
+ return root !== null && (typeof root === 'object' || typeof root === 'function') ? root : null
166
+ } catch (error) {
167
+ return null
168
+ }
169
+ }
170
+
171
+ function diagnosticPruneStateOf(ctx) {
172
+ const root = diagnosticPruneRootOf(ctx)
173
+ if (root === null) return null
174
+ try {
175
+ const existing = root[DIAGNOSTIC_PRUNE_STATE]
176
+ if (existing instanceof Map) return existing
177
+ if (existing !== undefined) return null
178
+ const state = new Map()
179
+ Object.defineProperty(root, DIAGNOSTIC_PRUNE_STATE, {
180
+ value: state,
181
+ enumerable: false,
182
+ configurable: false,
183
+ writable: false,
184
+ })
185
+ return state
186
+ } catch (error) {
187
+ return null
188
+ }
189
+ }
190
+
191
+ function diagnosticPruneKey(hostDataRoot) {
192
+ const normalized = pathResolve(hostDataRoot)
193
+ return process.platform === 'win32' ? normalized.toLowerCase() : normalized
194
+ }
195
+
196
+ function startupPruneDiagnosticLogsForApply(ctx) {
197
+ let startupContext = null
198
+ try { startupContext = settingsPathContext(ctx, undefined) } catch (error) { startupContext = null }
199
+ const hostDataRoot = startupContext !== null && startupContext !== undefined ? startupContext.hostDataRoot : null
200
+ if (typeof hostDataRoot !== 'string' || hostDataRoot === '') {
201
+ console.warn('[dsh-extra-plan] diagnostic log prune skipped: no-host-root')
202
+ return
203
+ }
204
+ if (ctx === null || ctx === undefined || typeof ctx.effect !== 'function') {
205
+ console.warn('[dsh-extra-plan] diagnostic log prune skipped: root ctx.effect unavailable')
206
+ return
207
+ }
208
+ const records = diagnosticPruneStateOf(ctx)
209
+ if (!(records instanceof Map)) {
210
+ console.warn('[dsh-extra-plan] diagnostic log prune skipped: root state unavailable')
211
+ return
212
+ }
213
+ let key
214
+ try {
215
+ key = diagnosticPruneKey(hostDataRoot)
216
+ } catch (error) {
217
+ console.warn('[dsh-extra-plan] diagnostic log prune skipped: invalid-host-root')
218
+ return
219
+ }
220
+ try {
221
+ ctx.effect(() => {
222
+ const owner = { released: false }
223
+ let record = records.get(key)
224
+ let created = false
225
+ if (record === undefined) {
226
+ record = { owners: new Set(), status: 'scheduled', timer: null, cancelReady: null, promise: null, error: null }
227
+ records.set(key, record)
228
+ created = true
229
+ }
230
+ record.owners.add(owner)
231
+ if (created) {
232
+ const run = () => {
233
+ if (records.get(key) !== record || record.status !== 'scheduled') return
234
+ record.status = 'running'
235
+ record.timer = null
236
+ record.promise = Promise.resolve().then(() => pruneDiagnosticLogs(startupContext)).then((result) => {
237
+ record.status = 'done'
238
+ if (result !== null && typeof result === 'object' && result.status === 'pruned') {
239
+ try { console.warn('[dsh-extra-plan] diagnostic log prune: ' + JSON.stringify(result)) } catch (error) {}
240
+ }
241
+ }, (error) => {
242
+ record.error = error
243
+ record.status = 'done'
244
+ try { console.warn('[dsh-extra-plan] diagnostic log prune failed(不阻断启动): ' + (error instanceof Error ? error.message : String(error))) } catch (warnError) {}
245
+ })
246
+ }
247
+ const scheduleFailed = (error) => {
248
+ record.error = error
249
+ record.status = 'done'
250
+ if (records.get(key) === record) records.delete(key)
251
+ try { console.warn('[dsh-extra-plan] diagnostic log prune scheduling failed(不阻断启动): ' + (error instanceof Error ? error.message : String(error))) } catch (warnError) {}
252
+ }
253
+ const schedule = () => {
254
+ if (records.get(key) !== record || record.status !== 'scheduled' || record.timer !== null) return
255
+ try {
256
+ const timer = setTimeout(run, 0)
257
+ record.timer = timer
258
+ if (timer !== null && typeof timer === 'object' && typeof timer.unref === 'function') timer.unref()
259
+ } catch (error) { scheduleFailed(error) }
260
+ }
261
+ try {
262
+ const ready = safeContextGet(ctx, 'appReady')
263
+ if (ready !== undefined && ready !== null && typeof ready.onReady === 'function') record.cancelReady = ready.onReady(schedule)
264
+ else schedule()
265
+ } catch (error) { scheduleFailed(error) }
266
+ }
267
+ return () => {
268
+ if (owner.released) return record.promise
269
+ owner.released = true
270
+ record.owners.delete(owner)
271
+ if (records.get(key) !== record) return record.promise
272
+ if (record.status === 'scheduled' && record.owners.size === 0) {
273
+ records.delete(key)
274
+ if (typeof record.cancelReady === 'function') {
275
+ try { record.cancelReady() } catch (error) {}
276
+ }
277
+ if (record.timer !== null) {
278
+ try { clearTimeout(record.timer) } catch (error) {}
279
+ record.timer = null
280
+ }
281
+ return undefined
282
+ }
283
+ if (record.status === 'running' || record.status === 'done') return record.promise
284
+ return undefined
285
+ }
286
+ }, 'dsh-extra-plan-settings: diagnostic log prune')
287
+ } catch (error) {
288
+ try { console.warn('[dsh-extra-plan] diagnostic log prune effect failed(不阻断启动): ' + (error instanceof Error ? error.message : String(error))) } catch (warnError) {}
289
+ }
290
+ }
291
+
292
+ function startupArchiveForApply(ctx) {
293
+ try {
294
+ const startupContext = settingsPathContext(ctx, undefined)
295
+ if (startupContext === null) {
296
+ console.warn('[dsh-extra-plan] startup archive skipped: no-host-root')
297
+ return
298
+ }
299
+ if (typeof ctx.effect !== 'function') {
300
+ console.warn('[dsh-extra-plan] startup archive failed(不阻断启动): root ctx.effect unavailable')
301
+ return
302
+ }
303
+ // 账本工作交由后台 controller/Worker 调度;保留 cancel/done 生命周期控制与非阻断诊断。
304
+ const startBackground = () => {
305
+ try {
306
+ const handle = startUsageLedgerBackground(ctx, startupContext)
307
+ let disposePromise
308
+ const dispose = () => {
309
+ if (disposePromise !== undefined) return disposePromise
310
+ handle.cancel()
311
+ disposePromise = Promise.resolve(handle.done).then(() => undefined, (error) => {
312
+ console.warn('[dsh-extra-plan] startup archive dispose failed(不阻断卸载): ' + (error instanceof Error ? error.message : String(error)))
313
+ })
314
+ return disposePromise
315
+ }
316
+ Promise.resolve(handle.done).then((result) => {
317
+ if (result.status !== 'archived' && result.status !== 'up-to-date' && result.status !== 'cancelled') console.warn('[dsh-extra-plan] startup archive ' + result.status + ': ' + (result.reason || 'unknown'))
318
+ }, (error) => {
319
+ console.warn('[dsh-extra-plan] startup archive failed(不阻断启动): ' + (error instanceof Error ? error.message : String(error)))
320
+ })
321
+ return dispose
322
+ } catch (error) {
323
+ console.warn('[dsh-extra-plan] startup archive failed(不阻断启动): ' + (error instanceof Error ? error.message : String(error)))
324
+ return undefined
325
+ }
326
+ }
327
+ const ready = safeContextGet(ctx, 'appReady')
328
+ // 无有效 appReady:root effect 直接调度后台 controller/Worker,不在主线程同步扫描。
329
+ if (ready === undefined || ready === null || typeof ready.onReady !== 'function') {
330
+ ctx.effect(startBackground, 'dsh-extra-plan-settings: usage ledger background')
331
+ return
332
+ }
333
+ // 有 appReady:把账本扫描推迟到启动完成后的下一个宏任务,启动关键路径不再等扫描。
334
+ ctx.effect(() => {
335
+ let closed = false
336
+ let pending = null
337
+ let dispose
338
+ const cancelReady = ready.onReady(() => {
339
+ pending = setImmediate(() => {
340
+ pending = null
341
+ if (closed) return
342
+ try {
343
+ dispose = startBackground()
344
+ } catch (error) {
345
+ console.warn('[dsh-extra-plan] startup archive failed(不阻断启动): ' + (error instanceof Error ? error.message : String(error)))
346
+ }
347
+ })
348
+ })
349
+ return () => {
350
+ closed = true
351
+ if (typeof cancelReady === 'function') cancelReady()
352
+ if (pending !== null) { clearImmediate(pending); pending = null }
353
+ return typeof dispose === 'function' ? dispose() : undefined
354
+ }
355
+ }, 'dsh-extra-plan-settings: usage ledger background')
356
+ } catch (error) {
357
+ console.warn('[dsh-extra-plan] startup archive failed(不阻断启动): ' + (error instanceof Error ? error.message : String(error)))
358
+ }
359
+ }
360
+
361
+ export function apply(ctx) {
362
+ startupPruneDiagnosticLogsForApply(ctx)
363
+ startupArchiveForApply(ctx)
364
+ ctx.inject(['loader'], (child) => child.effect(() => mountClientCarrier(child), 'dsh-extra-plan-settings: internal client carrier'))
365
+ ctx.inject(['settings'], (child) => child.effect(() => child.settings.configure({ auto: false }, ctx.fiber), 'dsh-extra-plan-settings: settings policy'))
366
+ ctx.inject(['configEditor'], (child) => child.effect(() => mountPresetSync(child, { installAnchor: absoluteRoot(safeContextGet(ctx, 'profileContext')?.installAnchor) }), 'dsh-extra-plan-settings: sync and adapter lifecycle'))
367
+ }
368
+
369
+ export { SETTINGS_ROW_ID }