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.
- package/LICENSE +21 -0
- package/README.md +179 -2
- package/assets/presets/extra-plan/agent.cordis.yml +580 -0
- package/assets/presets/extra-plan/preset-definition-extra-plan-executor.generated.yml +115 -0
- package/assets/presets/extra-plan/preset-definition-extra-plan-planner.generated.yml +113 -0
- package/assets/presets/extra-plan/preset-definition-extra-plan-prober.generated.yml +107 -0
- package/assets/presets/extra-plan/preset-definition-extra-plan-reviewer.generated.yml +115 -0
- package/assets/presets/extra-plan/preset-definition.generated.yml +335 -0
- package/assets/presets/extra-plan/preset.yml +2 -0
- package/cordis.patch.yml +7 -0
- package/index.js +1671 -0
- package/lib/agent-runtime.js +172 -0
- package/lib/agent-session.js +65 -0
- package/lib/assembly-presentation.js +267 -0
- package/lib/client-carrier.js +7 -0
- package/lib/client.js +314 -0
- package/lib/extra-plan-gate-tool.js +387 -0
- package/lib/gate-decisions.js +744 -0
- package/lib/gate-words.js +108 -0
- package/lib/live-config.js +293 -0
- package/lib/log-retention.js +132 -0
- package/lib/model-routing.js +648 -0
- package/lib/path-policy.js +464 -0
- package/lib/plan-draft-tool.js +294 -0
- package/lib/planner-budget.js +136 -0
- package/lib/preset-defaults.generated.js +17 -0
- package/lib/preset-settings.js +3 -0
- package/lib/preset-sync.js +809 -0
- package/lib/preset-yaml.js +438 -0
- package/lib/role-contract.js +11 -0
- package/lib/run-code-scanner.js +360 -0
- package/lib/run-code-static.js +621 -0
- package/lib/runtime-diagnostics.js +89 -0
- package/lib/runtime-lifecycle.js +162 -0
- package/lib/runtime-static.js +23 -0
- package/lib/save-contract.js +263 -0
- package/lib/save-persistence.js +223 -0
- package/lib/save-plan-markdown-parser.js +369 -0
- package/lib/save-plan-validation-diagnostics.js +49 -0
- package/lib/save-plan-validation.js +812 -0
- package/lib/save-probe-validation.js +173 -0
- package/lib/save-source-verification.js +195 -0
- package/lib/save-tool-factories.js +270 -0
- package/lib/sdk-text-cache.js +161 -0
- package/lib/settings-contract.js +191 -0
- package/lib/settings.js +369 -0
- package/lib/shell-mutation.js +484 -0
- package/lib/usage-ledger-background.js +214 -0
- package/lib/usage-ledger-worker.js +116 -0
- package/lib/usage-ledger.js +1393 -0
- package/locale/en.json +6 -0
- package/locale/settings/en.json +6 -0
- package/locale/settings/zh.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +56 -4
- package/scripts/generate-runtime-defaults.mjs +796 -0
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto'
|
|
2
|
+
import { resolve as resolvePath } from 'node:path'
|
|
3
|
+
import { Worker } from 'node:worker_threads'
|
|
4
|
+
|
|
5
|
+
const PROTOCOL_VERSION = 1
|
|
6
|
+
const WORKER_URL = new URL('./usage-ledger-worker.js', import.meta.url)
|
|
7
|
+
const ownerHandles = new WeakMap()
|
|
8
|
+
const rootTails = new Map()
|
|
9
|
+
const fallbackOwner = {}
|
|
10
|
+
|
|
11
|
+
function isObject(value) {
|
|
12
|
+
return value !== null && (typeof value === 'object' || typeof value === 'function')
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function errorDetails(error, fallbackCode = 'worker-failed') {
|
|
16
|
+
const code = error !== null && typeof error === 'object' && typeof error.code === 'string' ? error.code : fallbackCode
|
|
17
|
+
const message = error instanceof Error ? error.message : String(error === undefined || error === null ? fallbackCode : error)
|
|
18
|
+
return { code, message }
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function normalizedRootOf(pathContext) {
|
|
22
|
+
const value = pathContext !== null && typeof pathContext === 'object' ? pathContext.hostDataRoot : null
|
|
23
|
+
if (typeof value !== 'string' || value.trim() === '') return ''
|
|
24
|
+
const normalized = resolvePath(value)
|
|
25
|
+
return process.platform === 'win32' ? normalized.toLowerCase() : normalized
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function capabilitySnapshot(value) {
|
|
29
|
+
if (value === null || typeof value !== 'object') return {}
|
|
30
|
+
const result = {}
|
|
31
|
+
for (const [key, descriptor] of Object.entries(value)) {
|
|
32
|
+
if (descriptor === null || typeof descriptor !== 'object') continue
|
|
33
|
+
if (typeof key !== 'string' || typeof descriptor.name !== 'string' || descriptor.name !== key) continue
|
|
34
|
+
if (typeof descriptor.path !== 'string' || typeof descriptor.type !== 'string' || typeof descriptor.access !== 'string') continue
|
|
35
|
+
result[key] = {
|
|
36
|
+
name: descriptor.name,
|
|
37
|
+
path: descriptor.path,
|
|
38
|
+
type: descriptor.type,
|
|
39
|
+
purpose: descriptor.purpose === null || typeof descriptor.purpose === 'string' ? descriptor.purpose : null,
|
|
40
|
+
access: descriptor.access,
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return result
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function pathContextSnapshot(pathContext) {
|
|
47
|
+
const source = pathContext !== null && typeof pathContext === 'object' ? pathContext : {}
|
|
48
|
+
return {
|
|
49
|
+
workspaceRoot: typeof source.workspaceRoot === 'string' ? source.workspaceRoot : null,
|
|
50
|
+
hostDataRoot: typeof source.hostDataRoot === 'string' ? source.hostDataRoot : null,
|
|
51
|
+
hostCapabilities: capabilitySnapshot(source.hostCapabilities),
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function normalizedResult(value, fallback = {}) {
|
|
56
|
+
const source = value !== null && typeof value === 'object' ? value : {}
|
|
57
|
+
const status = typeof source.status === 'string' ? source.status : 'failed'
|
|
58
|
+
if (status === 'archived' || status === 'up-to-date' || status === 'cancelled' || status === 'skipped') {
|
|
59
|
+
const result = { ...source, status }
|
|
60
|
+
delete result.error
|
|
61
|
+
return result
|
|
62
|
+
}
|
|
63
|
+
const details = source.error !== undefined ? errorDetails(source.error, typeof source.reason === 'string' ? source.reason : 'worker-failed') : errorDetails(fallback.error, typeof fallback.reason === 'string' ? fallback.reason : 'worker-failed')
|
|
64
|
+
return { status: 'failed', reason: typeof source.reason === 'string' && source.reason !== '' ? source.reason : details.code, error: details }
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function protocolFailure(reason, message) {
|
|
68
|
+
return { status: 'failed', reason, error: { code: reason, message } }
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function createWorkerHandle(id, pathContext, workerFactory, cancelView) {
|
|
72
|
+
let worker = null
|
|
73
|
+
let terminal = null
|
|
74
|
+
let failure = null
|
|
75
|
+
let readySeen = false
|
|
76
|
+
let runningSeen = false
|
|
77
|
+
let reportedThreadId
|
|
78
|
+
let runSent = false
|
|
79
|
+
let resultSeen = false
|
|
80
|
+
let exitCode = null
|
|
81
|
+
let resolveDone
|
|
82
|
+
const done = new Promise((resolve) => { resolveDone = resolve })
|
|
83
|
+
const send = (message) => {
|
|
84
|
+
if (worker === null || terminal !== null) return
|
|
85
|
+
try { worker.postMessage(message) } catch (error) { failure = failure || errorDetails(error, 'worker-post-failed') }
|
|
86
|
+
}
|
|
87
|
+
const markProtocolFailure = (reason, message) => {
|
|
88
|
+
failure = failure || protocolFailure(reason, message).error
|
|
89
|
+
Atomics.store(cancelView, 0, 1)
|
|
90
|
+
send({ type: 'cancel', version: PROTOCOL_VERSION, id })
|
|
91
|
+
}
|
|
92
|
+
const settleAfterExit = () => {
|
|
93
|
+
if (exitCode === null) return
|
|
94
|
+
if (failure !== null) {
|
|
95
|
+
resolveDone({ id, version: PROTOCOL_VERSION, status: 'failed', reason: failure.code || 'worker-failed', error: failure, threadId: reportedThreadId })
|
|
96
|
+
return
|
|
97
|
+
}
|
|
98
|
+
if (exitCode !== 0) {
|
|
99
|
+
resolveDone({ id, version: PROTOCOL_VERSION, status: 'failed', reason: 'worker-exit', error: { code: 'worker-exit', message: 'Worker exit code ' + exitCode }, threadId: reportedThreadId })
|
|
100
|
+
return
|
|
101
|
+
}
|
|
102
|
+
if (terminal !== null) {
|
|
103
|
+
resolveDone({ ...terminal, id, version: PROTOCOL_VERSION, threadId: reportedThreadId })
|
|
104
|
+
return
|
|
105
|
+
}
|
|
106
|
+
resolveDone({ ...protocolFailure(exitCode === 0 ? 'worker-exit-no-result' : 'worker-exit', 'Worker 未返回终态即退出'), id, version: PROTOCOL_VERSION })
|
|
107
|
+
}
|
|
108
|
+
try {
|
|
109
|
+
const workerOptions = {
|
|
110
|
+
execArgv: [],
|
|
111
|
+
workerData: {
|
|
112
|
+
version: PROTOCOL_VERSION,
|
|
113
|
+
id,
|
|
114
|
+
pathContext: pathContextSnapshot(pathContext),
|
|
115
|
+
cancelBuffer: cancelView.buffer,
|
|
116
|
+
},
|
|
117
|
+
}
|
|
118
|
+
worker = workerFactory === undefined ? new Worker(WORKER_URL, workerOptions) : workerFactory(WORKER_URL, workerOptions)
|
|
119
|
+
if (!worker || typeof worker.on !== 'function' || typeof worker.postMessage !== 'function') throw new Error('workerFactory 必须返回 Worker-like 对象')
|
|
120
|
+
worker.on('message', (message) => {
|
|
121
|
+
if (message === null || typeof message !== 'object' || message.version !== PROTOCOL_VERSION || message.id !== id || typeof message.type !== 'string') {
|
|
122
|
+
markProtocolFailure('worker-protocol', 'Worker 消息缺少匹配的 version/id/type')
|
|
123
|
+
return
|
|
124
|
+
}
|
|
125
|
+
if (message.type === 'ready') {
|
|
126
|
+
if (readySeen || runSent || !Number.isInteger(message.threadId) || message.threadId <= 0) {
|
|
127
|
+
markProtocolFailure('worker-protocol', 'Worker ready 消息无效或重复')
|
|
128
|
+
return
|
|
129
|
+
}
|
|
130
|
+
readySeen = true
|
|
131
|
+
reportedThreadId = message.threadId
|
|
132
|
+
if (Atomics.load(cancelView, 0) !== 0) send({ type: 'cancel', version: PROTOCOL_VERSION, id })
|
|
133
|
+
else {
|
|
134
|
+
runSent = true
|
|
135
|
+
send({ type: 'run', version: PROTOCOL_VERSION, id })
|
|
136
|
+
}
|
|
137
|
+
return
|
|
138
|
+
}
|
|
139
|
+
if (message.type === 'running') {
|
|
140
|
+
if (!readySeen || !runSent || runningSeen || !Number.isInteger(message.threadId) || message.threadId <= 0) {
|
|
141
|
+
markProtocolFailure('worker-protocol', 'Worker running 消息无效')
|
|
142
|
+
return
|
|
143
|
+
}
|
|
144
|
+
runningSeen = true
|
|
145
|
+
reportedThreadId = message.threadId
|
|
146
|
+
return
|
|
147
|
+
}
|
|
148
|
+
if (message.type === 'result') {
|
|
149
|
+
if (resultSeen || !readySeen || (message.result && message.result.status !== 'cancelled' && !runningSeen)) {
|
|
150
|
+
markProtocolFailure('worker-protocol', 'Worker result 消息无效或重复')
|
|
151
|
+
return
|
|
152
|
+
}
|
|
153
|
+
resultSeen = true
|
|
154
|
+
terminal = normalizedResult(message.result)
|
|
155
|
+
return
|
|
156
|
+
}
|
|
157
|
+
markProtocolFailure('worker-protocol', 'Worker 未知消息类型: ' + message.type)
|
|
158
|
+
})
|
|
159
|
+
worker.on('error', (error) => {
|
|
160
|
+
failure = failure || errorDetails(error, 'worker-error')
|
|
161
|
+
Atomics.store(cancelView, 0, 1)
|
|
162
|
+
})
|
|
163
|
+
worker.on('messageerror', (error) => {
|
|
164
|
+
failure = failure || errorDetails(error, 'worker-messageerror')
|
|
165
|
+
Atomics.store(cancelView, 0, 1)
|
|
166
|
+
})
|
|
167
|
+
worker.on('exit', (code) => {
|
|
168
|
+
exitCode = code
|
|
169
|
+
settleAfterExit()
|
|
170
|
+
})
|
|
171
|
+
} catch (error) {
|
|
172
|
+
failure = errorDetails(error, 'worker-create-failed')
|
|
173
|
+
exitCode = 0
|
|
174
|
+
settleAfterExit()
|
|
175
|
+
}
|
|
176
|
+
return { done, cancel: () => { Atomics.store(cancelView, 0, 1); send({ type: 'cancel', version: PROTOCOL_VERSION, id }) } }
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export function startUsageLedgerBackground(owner, pathContext, options = {}) {
|
|
180
|
+
const ownerKey = isObject(owner) ? owner : fallbackOwner
|
|
181
|
+
const active = ownerHandles.get(ownerKey)
|
|
182
|
+
if (active !== undefined) return active
|
|
183
|
+
const id = randomUUID()
|
|
184
|
+
const cancelBuffer = new SharedArrayBuffer(4)
|
|
185
|
+
const cancelView = new Int32Array(cancelBuffer)
|
|
186
|
+
let cancelRequested = false
|
|
187
|
+
let launched = null
|
|
188
|
+
const rootKey = normalizedRootOf(pathContext)
|
|
189
|
+
const previous = rootTails.get(rootKey) || Promise.resolve()
|
|
190
|
+
const workerFactory = options !== null && typeof options === 'object' && typeof options.workerFactory === 'function' ? options.workerFactory : undefined
|
|
191
|
+
const done = previous.then(() => {
|
|
192
|
+
if (cancelRequested) return { status: 'cancelled', reason: 'cancel-requested', id, version: PROTOCOL_VERSION }
|
|
193
|
+
launched = createWorkerHandle(id, pathContext, workerFactory, cancelView)
|
|
194
|
+
return launched.done
|
|
195
|
+
}).catch((error) => ({ status: 'failed', reason: 'background-schedule-failed', error: errorDetails(error, 'background-schedule-failed'), id, version: PROTOCOL_VERSION }))
|
|
196
|
+
const handle = {
|
|
197
|
+
id,
|
|
198
|
+
version: PROTOCOL_VERSION,
|
|
199
|
+
done,
|
|
200
|
+
cancel: () => {
|
|
201
|
+
if (cancelRequested) return
|
|
202
|
+
cancelRequested = true
|
|
203
|
+
Atomics.store(cancelView, 0, 1)
|
|
204
|
+
if (launched !== null) launched.cancel()
|
|
205
|
+
},
|
|
206
|
+
}
|
|
207
|
+
ownerHandles.set(ownerKey, handle)
|
|
208
|
+
rootTails.set(rootKey, done)
|
|
209
|
+
done.then(() => {
|
|
210
|
+
if (ownerHandles.get(ownerKey) === handle) ownerHandles.delete(ownerKey)
|
|
211
|
+
if (rootTails.get(rootKey) === done) rootTails.delete(rootKey)
|
|
212
|
+
})
|
|
213
|
+
return handle
|
|
214
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { parentPort, threadId, workerData } from 'node:worker_threads'
|
|
2
|
+
import { createPathContext } from './path-policy.js'
|
|
3
|
+
import { startupArchive } from './usage-ledger.js'
|
|
4
|
+
|
|
5
|
+
const PROTOCOL_VERSION = 1
|
|
6
|
+
const port = parentPort
|
|
7
|
+
const id = workerData !== null && typeof workerData === 'object' ? workerData.id : ''
|
|
8
|
+
let finished = false
|
|
9
|
+
let runStarted = false
|
|
10
|
+
let cancelView
|
|
11
|
+
let pathContext
|
|
12
|
+
|
|
13
|
+
function errorDetails(error, fallbackCode = 'worker-failed') {
|
|
14
|
+
const code = error !== null && typeof error === 'object' && typeof error.code === 'string' ? error.code : fallbackCode
|
|
15
|
+
const message = error instanceof Error ? error.message : String(error === undefined || error === null ? fallbackCode : error)
|
|
16
|
+
return { code, message }
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function resultOf(value) {
|
|
20
|
+
const source = value !== null && typeof value === 'object' ? value : {}
|
|
21
|
+
const status = typeof source.status === 'string' ? source.status : 'failed'
|
|
22
|
+
if (status === 'archived' || status === 'up-to-date' || status === 'cancelled' || status === 'skipped') {
|
|
23
|
+
const result = { status }
|
|
24
|
+
for (const key of ['reason', 'archivePath', 'rows', 'groups', 'scannedRows', 'scannedFiles', 'scannedSessions', 'scannedUsageSessions', 'monotonicWarnings', 'archiveBytes', 'cacheRepaired']) {
|
|
25
|
+
if (source[key] !== undefined && (typeof source[key] === 'string' || typeof source[key] === 'number' || typeof source[key] === 'boolean')) result[key] = source[key]
|
|
26
|
+
}
|
|
27
|
+
return result
|
|
28
|
+
}
|
|
29
|
+
const details = source.error !== undefined ? errorDetails(source.error, typeof source.reason === 'string' ? source.reason : 'worker-failed') : { code: typeof source.reason === 'string' ? source.reason : 'worker-failed', message: typeof source.reason === 'string' ? source.reason : 'Worker 归档失败' }
|
|
30
|
+
return { status: 'failed', reason: typeof source.reason === 'string' && source.reason !== '' ? source.reason : details.code, error: details }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function post(type, payload = {}) {
|
|
34
|
+
if (port === null) return
|
|
35
|
+
port.postMessage({ type, version: PROTOCOL_VERSION, id, ...payload })
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function close() {
|
|
39
|
+
if (port !== null) port.close()
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function finish(value) {
|
|
43
|
+
if (finished) return
|
|
44
|
+
finished = true
|
|
45
|
+
post('result', { result: resultOf(value) })
|
|
46
|
+
close()
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function cancellationRequested() {
|
|
50
|
+
return cancelView !== undefined && Atomics.load(cancelView, 0) !== 0
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function capabilitySnapshot(value) {
|
|
54
|
+
if (value === null || typeof value !== 'object') return {}
|
|
55
|
+
const result = {}
|
|
56
|
+
for (const [key, descriptor] of Object.entries(value)) {
|
|
57
|
+
if (descriptor === null || typeof descriptor !== 'object') continue
|
|
58
|
+
if (typeof key !== 'string' || descriptor.name !== key || typeof descriptor.path !== 'string' || typeof descriptor.type !== 'string' || typeof descriptor.access !== 'string') continue
|
|
59
|
+
result[key] = {
|
|
60
|
+
name: descriptor.name,
|
|
61
|
+
path: descriptor.path,
|
|
62
|
+
type: descriptor.type,
|
|
63
|
+
purpose: descriptor.purpose === null || typeof descriptor.purpose === 'string' ? descriptor.purpose : null,
|
|
64
|
+
access: descriptor.access,
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return result
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function validateAndRebuild() {
|
|
71
|
+
if (port === null) throw new Error('Worker parentPort 不可用')
|
|
72
|
+
if (workerData === null || typeof workerData !== 'object' || workerData.version !== PROTOCOL_VERSION || typeof workerData.id !== 'string' || workerData.id === '') throw new Error('WorkerData version/id 无效')
|
|
73
|
+
if (!(workerData.cancelBuffer instanceof SharedArrayBuffer)) throw new Error('WorkerData cancelBuffer 无效')
|
|
74
|
+
const raw = workerData.pathContext
|
|
75
|
+
if (raw === null || typeof raw !== 'object') throw new Error('WorkerData pathContext 无效')
|
|
76
|
+
cancelView = new Int32Array(workerData.cancelBuffer)
|
|
77
|
+
pathContext = createPathContext({
|
|
78
|
+
workspaceRoot: typeof raw.workspaceRoot === 'string' ? raw.workspaceRoot : null,
|
|
79
|
+
hostDataRoot: typeof raw.hostDataRoot === 'string' ? raw.hostDataRoot : null,
|
|
80
|
+
hostCapabilities: capabilitySnapshot(raw.hostCapabilities),
|
|
81
|
+
})
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
try {
|
|
85
|
+
validateAndRebuild()
|
|
86
|
+
post('ready', { threadId })
|
|
87
|
+
} catch (error) {
|
|
88
|
+
finish({ status: 'failed', reason: 'worker-init-failed', error: errorDetails(error, 'worker-init-failed') })
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (port !== null && !finished) {
|
|
92
|
+
port.on('message', (message) => {
|
|
93
|
+
if (finished) return
|
|
94
|
+
if (message === null || typeof message !== 'object' || message.version !== PROTOCOL_VERSION || message.id !== id || (message.type !== 'run' && message.type !== 'cancel')) {
|
|
95
|
+
finish({ status: 'failed', reason: 'worker-protocol', error: { code: 'worker-protocol', message: 'Worker 控制消息无效' } })
|
|
96
|
+
return
|
|
97
|
+
}
|
|
98
|
+
if (message.type === 'cancel') {
|
|
99
|
+
Atomics.store(cancelView, 0, 1)
|
|
100
|
+
if (!runStarted) finish({ status: 'cancelled', reason: 'cancel-requested' })
|
|
101
|
+
return
|
|
102
|
+
}
|
|
103
|
+
if (runStarted) return
|
|
104
|
+
runStarted = true
|
|
105
|
+
if (cancellationRequested()) {
|
|
106
|
+
finish({ status: 'cancelled', reason: 'cancel-requested' })
|
|
107
|
+
return
|
|
108
|
+
}
|
|
109
|
+
post('running', { threadId })
|
|
110
|
+
try {
|
|
111
|
+
finish(startupArchive(pathContext, { shouldCancel: cancellationRequested }))
|
|
112
|
+
} catch (error) {
|
|
113
|
+
finish({ status: 'failed', reason: 'worker-run-failed', error: errorDetails(error, 'worker-run-failed') })
|
|
114
|
+
}
|
|
115
|
+
})
|
|
116
|
+
}
|