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,360 @@
|
|
|
1
|
+
// run_code 无宿主状态词法扫描器:字符串/注释遮蔽、括号配平、调用点收集。
|
|
2
|
+
|
|
3
|
+
export function maskCodeLiteralsAndComments(code) {
|
|
4
|
+
const text = typeof code === 'string' ? code : ''
|
|
5
|
+
const chars = text.split('')
|
|
6
|
+
const n = chars.length
|
|
7
|
+
const templateQuote = String.fromCharCode(96)
|
|
8
|
+
const regexAfterWord = new Set(['return', 'throw', 'case', 'delete', 'void', 'typeof', 'instanceof', 'in', 'of', 'yield', 'await', 'else', 'do', 'new'])
|
|
9
|
+
const controlParenWords = new Set(['if', 'for', 'while', 'with', 'switch', 'catch'])
|
|
10
|
+
const blockWords = new Set(['else', 'try', 'catch', 'finally', 'do'])
|
|
11
|
+
const functionDeclarationBefore = new Set([';', '{', '}', ')', 'export', 'default'])
|
|
12
|
+
const isIdentifierStart = (ch) => typeof ch === 'string' && /[A-Za-z_$]/.test(ch)
|
|
13
|
+
const isIdentifierPart = (ch) => typeof ch === 'string' && /[A-Za-z0-9_$]/.test(ch)
|
|
14
|
+
const blank = (start, end) => {
|
|
15
|
+
for (let k = start; k < end; k += 1) {
|
|
16
|
+
if (chars[k] !== '\n' && chars[k] !== '\r') chars[k] = ' '
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function scanString(start, quote) {
|
|
20
|
+
let i = start
|
|
21
|
+
blank(i, i + 1)
|
|
22
|
+
i += 1
|
|
23
|
+
while (i < n) {
|
|
24
|
+
if (text[i] === '\\') { blank(i, Math.min(i + 2, n)); i += 2; continue }
|
|
25
|
+
blank(i, i + 1)
|
|
26
|
+
if (text[i] === quote) return i + 1
|
|
27
|
+
i += 1
|
|
28
|
+
}
|
|
29
|
+
return n
|
|
30
|
+
}
|
|
31
|
+
function scanRegex(start) {
|
|
32
|
+
let i = start
|
|
33
|
+
let inClass = false
|
|
34
|
+
blank(i, i + 1)
|
|
35
|
+
i += 1
|
|
36
|
+
while (i < n) {
|
|
37
|
+
if (text[i] === '\\') { blank(i, Math.min(i + 2, n)); i += 2; continue }
|
|
38
|
+
if (text[i] === '\n' || text[i] === '\r') return i
|
|
39
|
+
if (text[i] === '[') inClass = true
|
|
40
|
+
else if (text[i] === ']') inClass = false
|
|
41
|
+
else if (text[i] === '/' && !inClass) {
|
|
42
|
+
blank(i, i + 1)
|
|
43
|
+
i += 1
|
|
44
|
+
while (i < n && isIdentifierPart(text[i])) { blank(i, i + 1); i += 1 }
|
|
45
|
+
return i
|
|
46
|
+
}
|
|
47
|
+
blank(i, i + 1)
|
|
48
|
+
i += 1
|
|
49
|
+
}
|
|
50
|
+
return n
|
|
51
|
+
}
|
|
52
|
+
function scanTemplate(start) {
|
|
53
|
+
blank(start, start + 1)
|
|
54
|
+
let i = start + 1
|
|
55
|
+
while (i < n) {
|
|
56
|
+
if (text[i] === '\\') { blank(i, Math.min(i + 2, n)); i += 2; continue }
|
|
57
|
+
if (text[i] === templateQuote) { blank(i, i + 1); return i + 1 }
|
|
58
|
+
if (text[i] === '$' && text[i + 1] === '{') {
|
|
59
|
+
blank(i, i + 1)
|
|
60
|
+
i = scanCode(i + 2, true)
|
|
61
|
+
if (i < n && text[i] === '}') i += 1
|
|
62
|
+
continue
|
|
63
|
+
}
|
|
64
|
+
blank(i, i + 1)
|
|
65
|
+
i += 1
|
|
66
|
+
}
|
|
67
|
+
return n
|
|
68
|
+
}
|
|
69
|
+
function scanCode(start, stopAtBrace) {
|
|
70
|
+
let i = start
|
|
71
|
+
const braceKinds = []
|
|
72
|
+
const parens = []
|
|
73
|
+
let canStartRegex = true
|
|
74
|
+
let lastWord = ''
|
|
75
|
+
let lastSignificant = ''
|
|
76
|
+
let lastWordWasProperty = false
|
|
77
|
+
let pendingFunction = null
|
|
78
|
+
let functionBodyExpression
|
|
79
|
+
let asyncFunctionExpression = false
|
|
80
|
+
let asyncWordEnd = 0
|
|
81
|
+
while (i < n) {
|
|
82
|
+
const ch = text[i]
|
|
83
|
+
if (stopAtBrace && ch === '}' && braceKinds.length === 0) return i
|
|
84
|
+
if (/\s/.test(ch)) { i += 1; continue }
|
|
85
|
+
if (ch === "'" || ch === '"') { i = scanString(i, ch); canStartRegex = false; lastWord = ''; lastSignificant = 'value'; continue }
|
|
86
|
+
if (ch === templateQuote) { i = scanTemplate(i); canStartRegex = false; lastWord = ''; lastSignificant = 'value'; continue }
|
|
87
|
+
if (ch === '/' && i + 1 < n && text[i + 1] === '/') {
|
|
88
|
+
blank(i, i + 2)
|
|
89
|
+
i += 2
|
|
90
|
+
while (i < n && text[i] !== '\n') { blank(i, i + 1); i += 1 }
|
|
91
|
+
continue
|
|
92
|
+
}
|
|
93
|
+
if (ch === '/' && i + 1 < n && text[i + 1] === '*') {
|
|
94
|
+
const end = text.indexOf('*/', i + 2)
|
|
95
|
+
const stop = end === -1 ? n : end + 2
|
|
96
|
+
blank(i, stop)
|
|
97
|
+
i = stop
|
|
98
|
+
continue
|
|
99
|
+
}
|
|
100
|
+
if (ch === '/' && canStartRegex) { i = scanRegex(i); canStartRegex = false; lastWord = ''; lastSignificant = 'value'; continue }
|
|
101
|
+
if (isIdentifierStart(ch)) {
|
|
102
|
+
let j = i + 1
|
|
103
|
+
while (j < n && isIdentifierPart(text[j])) j += 1
|
|
104
|
+
const word = text.slice(i, j)
|
|
105
|
+
const property = lastSignificant === '.'
|
|
106
|
+
if (!property && word === 'async') {
|
|
107
|
+
asyncFunctionExpression = lastSignificant === '' ? stopAtBrace : !functionDeclarationBefore.has(lastSignificant)
|
|
108
|
+
asyncWordEnd = j
|
|
109
|
+
}
|
|
110
|
+
if (!property && word === 'function') {
|
|
111
|
+
pendingFunction = lastWord === 'async'
|
|
112
|
+
? (!/[\r\n]/.test(text.slice(asyncWordEnd, i)) && asyncFunctionExpression)
|
|
113
|
+
: (lastSignificant === '' ? stopAtBrace : !functionDeclarationBefore.has(lastSignificant))
|
|
114
|
+
}
|
|
115
|
+
lastWord = word
|
|
116
|
+
lastWordWasProperty = property
|
|
117
|
+
lastSignificant = lastWord
|
|
118
|
+
canStartRegex = !property && regexAfterWord.has(lastWord)
|
|
119
|
+
i = j
|
|
120
|
+
continue
|
|
121
|
+
}
|
|
122
|
+
if (/[0-9]/.test(ch)) {
|
|
123
|
+
let j = i + 1
|
|
124
|
+
while (j < n && /[A-Za-z0-9._]/.test(text[j])) j += 1
|
|
125
|
+
i = j
|
|
126
|
+
canStartRegex = false
|
|
127
|
+
lastWord = ''
|
|
128
|
+
lastSignificant = 'value'
|
|
129
|
+
continue
|
|
130
|
+
}
|
|
131
|
+
if (ch === '(') {
|
|
132
|
+
parens.push({ control: !lastWordWasProperty && controlParenWords.has(lastWord), functionExpression: pendingFunction })
|
|
133
|
+
pendingFunction = null
|
|
134
|
+
functionBodyExpression = undefined
|
|
135
|
+
i += 1
|
|
136
|
+
canStartRegex = true
|
|
137
|
+
lastWord = ''
|
|
138
|
+
lastSignificant = '('
|
|
139
|
+
continue
|
|
140
|
+
}
|
|
141
|
+
if (ch === ')') {
|
|
142
|
+
const paren = parens.pop()
|
|
143
|
+
functionBodyExpression = paren?.functionExpression ?? undefined
|
|
144
|
+
i += 1
|
|
145
|
+
canStartRegex = paren?.control === true
|
|
146
|
+
lastWord = ''
|
|
147
|
+
lastSignificant = ')'
|
|
148
|
+
continue
|
|
149
|
+
}
|
|
150
|
+
if (ch === '[') { i += 1; canStartRegex = true; lastWord = ''; lastSignificant = '['; continue }
|
|
151
|
+
if (ch === ']') { i += 1; canStartRegex = false; lastWord = ''; lastSignificant = ']'; continue }
|
|
152
|
+
if (ch === '{') {
|
|
153
|
+
const isBlock = typeof functionBodyExpression === 'boolean'
|
|
154
|
+
? !functionBodyExpression
|
|
155
|
+
: (!stopAtBrace && lastSignificant === '') || lastSignificant === ')' || lastSignificant === ';' || lastSignificant === '=>' || blockWords.has(lastWord)
|
|
156
|
+
functionBodyExpression = undefined
|
|
157
|
+
braceKinds.push(isBlock)
|
|
158
|
+
i += 1
|
|
159
|
+
canStartRegex = true
|
|
160
|
+
lastWord = ''
|
|
161
|
+
lastSignificant = '{'
|
|
162
|
+
continue
|
|
163
|
+
}
|
|
164
|
+
if (ch === '}') {
|
|
165
|
+
const isBlock = braceKinds.length > 0 ? braceKinds.pop() : true
|
|
166
|
+
i += 1
|
|
167
|
+
canStartRegex = isBlock === true
|
|
168
|
+
lastWord = ''
|
|
169
|
+
lastSignificant = '}'
|
|
170
|
+
continue
|
|
171
|
+
}
|
|
172
|
+
if (ch === '.') { i += 1; canStartRegex = false; lastWord = ''; lastSignificant = '.'; continue }
|
|
173
|
+
if (ch === '=' && text[i + 1] === '>') {
|
|
174
|
+
i += 2
|
|
175
|
+
canStartRegex = true
|
|
176
|
+
lastWord = ''
|
|
177
|
+
lastSignificant = '=>'
|
|
178
|
+
continue
|
|
179
|
+
}
|
|
180
|
+
if ((ch === '+' || ch === '-') && text[i + 1] === ch) {
|
|
181
|
+
i += 2
|
|
182
|
+
canStartRegex = false
|
|
183
|
+
lastWord = ''
|
|
184
|
+
lastSignificant = ch + ch
|
|
185
|
+
continue
|
|
186
|
+
}
|
|
187
|
+
canStartRegex = true
|
|
188
|
+
lastWord = ''
|
|
189
|
+
lastSignificant = ch
|
|
190
|
+
i += 1
|
|
191
|
+
}
|
|
192
|
+
return n
|
|
193
|
+
}
|
|
194
|
+
scanCode(0, false)
|
|
195
|
+
return chars.join('')
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// 从 '(' 起括号配平(计数 ( ) [ ] { },遮蔽后无字符串干扰)取参数切片:
|
|
199
|
+
// 在遮蔽文本上配平,innerText 取原文本(JSON.parse 需要原始字面量)。
|
|
200
|
+
// 返回 { closeIdx(配平闭括号索引,未闭合取文本末尾), innerText }。
|
|
201
|
+
|
|
202
|
+
export function sliceBalancedArgs(maskedText, text, parenIdx) {
|
|
203
|
+
let depth = 0
|
|
204
|
+
let i = parenIdx
|
|
205
|
+
while (i < maskedText.length) {
|
|
206
|
+
const ch = maskedText[i]
|
|
207
|
+
if (ch === '(') depth += 1
|
|
208
|
+
else if (ch === ')') { depth -= 1; if (depth === 0) break }
|
|
209
|
+
else if (ch === '[') depth += 1
|
|
210
|
+
else if (ch === ']') depth -= 1
|
|
211
|
+
else if (ch === '{') depth += 1
|
|
212
|
+
else if (ch === '}') depth -= 1
|
|
213
|
+
i += 1
|
|
214
|
+
}
|
|
215
|
+
const closeIdx = i < maskedText.length ? i : text.length - 1
|
|
216
|
+
const innerText = text.slice(parenIdx + 1, i < maskedText.length ? i : text.length)
|
|
217
|
+
return { closeIdx, innerText }
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function staticBracketKeyOf(text, masked, bracketStart, close) {
|
|
221
|
+
const q = skipStaticKeyTrivia(text, bracketStart + 1)
|
|
222
|
+
if (text[q] !== "'" && text[q] !== '"') return null
|
|
223
|
+
const quote = text[q]
|
|
224
|
+
let endQuote = q + 1
|
|
225
|
+
while (endQuote < text.length) {
|
|
226
|
+
if (text[endQuote] === '\\') { endQuote += 2; continue }
|
|
227
|
+
if (text[endQuote] === quote) break
|
|
228
|
+
endQuote += 1
|
|
229
|
+
}
|
|
230
|
+
if (endQuote >= text.length) return null
|
|
231
|
+
const literal = text.slice(q + 1, endQuote)
|
|
232
|
+
const afterQuote = skipStaticKeyTrivia(text, endQuote + 1)
|
|
233
|
+
if (afterQuote !== close || !/^[A-Za-z_$][A-Za-z0-9_$-]*$/.test(literal)) return null
|
|
234
|
+
return literal
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function skipStaticKeyTrivia(value, start) {
|
|
238
|
+
let i = start
|
|
239
|
+
while (i < value.length) {
|
|
240
|
+
while (i < value.length && /\s/.test(value[i])) i += 1
|
|
241
|
+
if (value.startsWith('/*', i)) {
|
|
242
|
+
const end = value.indexOf('*/', i + 2)
|
|
243
|
+
if (end === -1) return value.length
|
|
244
|
+
i = end + 2
|
|
245
|
+
continue
|
|
246
|
+
}
|
|
247
|
+
if (value.startsWith('//', i)) {
|
|
248
|
+
const end = value.indexOf(String.fromCharCode(10), i + 2)
|
|
249
|
+
if (end === -1) return value.length
|
|
250
|
+
i = end + 1
|
|
251
|
+
continue
|
|
252
|
+
}
|
|
253
|
+
break
|
|
254
|
+
}
|
|
255
|
+
return i
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// 判断是否存在无法静态解析的 tools[...] 访问;调用点区间由 collectRunCodeSites 提供。
|
|
259
|
+
// 静态调用参数仍继续扫描,避免漏掉其中的动态 tools[...] 访问。
|
|
260
|
+
export function hasDynamicRunCodeAccess(txt, msk, sites = []) {
|
|
261
|
+
const text = typeof txt === 'string' ? txt : ''
|
|
262
|
+
const masked = typeof msk === 'string' && msk.length === text.length ? msk : maskCodeLiteralsAndComments(text)
|
|
263
|
+
const isIdentifierPart = (ch) => typeof ch === 'string' && /[A-Za-z0-9_$]/.test(ch)
|
|
264
|
+
const skipWhitespace = (value, start) => {
|
|
265
|
+
let i = start
|
|
266
|
+
while (i < value.length && /\s/.test(value[i])) i += 1
|
|
267
|
+
return i
|
|
268
|
+
}
|
|
269
|
+
const bracketEndOf = (start) => {
|
|
270
|
+
let depth = 0
|
|
271
|
+
for (let i = start; i < masked.length; i += 1) {
|
|
272
|
+
if (masked[i] === '[') depth += 1
|
|
273
|
+
else if (masked[i] === ']') {
|
|
274
|
+
depth -= 1
|
|
275
|
+
if (depth === 0) return i
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
return -1
|
|
279
|
+
}
|
|
280
|
+
let i = 0
|
|
281
|
+
while (i < text.length) {
|
|
282
|
+
if (!masked.startsWith('tools', i) || (i > 0 && isIdentifierPart(masked[i - 1]))) { i += 1; continue }
|
|
283
|
+
let owner = i - 1
|
|
284
|
+
while (owner >= 0 && /\s/.test(masked[owner])) owner -= 1
|
|
285
|
+
if (owner >= 0 && masked[owner] === '.') { i += 5; continue }
|
|
286
|
+
const accessStart = skipWhitespace(masked, i + 5)
|
|
287
|
+
if (masked[accessStart] !== '[') { i += 1; continue }
|
|
288
|
+
const close = bracketEndOf(accessStart)
|
|
289
|
+
if (close === -1) return true
|
|
290
|
+
const literal = staticBracketKeyOf(text, masked, accessStart, close)
|
|
291
|
+
const afterBracket = skipWhitespace(masked, close + 1)
|
|
292
|
+
const staticCall = literal !== null && text[afterBracket] === '('
|
|
293
|
+
if (!staticCall) return true
|
|
294
|
+
i = close + 1
|
|
295
|
+
}
|
|
296
|
+
return false
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
export function collectRunCodeSites(txt, msk) {
|
|
300
|
+
const text = typeof txt === 'string' ? txt : ''
|
|
301
|
+
const masked = typeof msk === 'string' && msk.length === text.length ? msk : maskCodeLiteralsAndComments(text)
|
|
302
|
+
const sites = []
|
|
303
|
+
const isIdentifierStart = (ch) => typeof ch === 'string' && /[A-Za-z_$]/.test(ch)
|
|
304
|
+
const isIdentifierPart = (ch) => typeof ch === 'string' && /[A-Za-z0-9_$]/.test(ch)
|
|
305
|
+
const skipWhitespace = (value, start) => {
|
|
306
|
+
let i = start
|
|
307
|
+
while (i < value.length && /\s/.test(value[i])) i += 1
|
|
308
|
+
return i
|
|
309
|
+
}
|
|
310
|
+
const bracketEndOf = (start) => {
|
|
311
|
+
let depth = 0
|
|
312
|
+
for (let i = start; i < masked.length; i += 1) {
|
|
313
|
+
if (masked[i] === '[') depth += 1
|
|
314
|
+
else if (masked[i] === ']') {
|
|
315
|
+
depth -= 1
|
|
316
|
+
if (depth === 0) return i
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
return -1
|
|
320
|
+
}
|
|
321
|
+
let i = 0
|
|
322
|
+
while (i < text.length) {
|
|
323
|
+
if (!masked.startsWith('tools', i) || (i > 0 && isIdentifierPart(masked[i - 1]))) { i += 1; continue }
|
|
324
|
+
let owner = i - 1
|
|
325
|
+
while (owner >= 0 && /\s/.test(masked[owner])) owner -= 1
|
|
326
|
+
if (owner >= 0 && masked[owner] === '.') { i += 5; continue }
|
|
327
|
+
let j = skipWhitespace(masked, i + 5)
|
|
328
|
+
let name
|
|
329
|
+
let parenIdx = -1
|
|
330
|
+
if (masked[j] === '.') {
|
|
331
|
+
j = skipWhitespace(masked, j + 1)
|
|
332
|
+
if (isIdentifierStart(masked[j])) {
|
|
333
|
+
let k = j + 1
|
|
334
|
+
while (k < text.length && isIdentifierPart(masked[k])) k += 1
|
|
335
|
+
const candidate = masked.slice(j, k)
|
|
336
|
+
k = skipWhitespace(masked, k)
|
|
337
|
+
if (masked[k] === '(') { name = candidate; parenIdx = k }
|
|
338
|
+
}
|
|
339
|
+
} else if (masked[j] === '[') {
|
|
340
|
+
const close = bracketEndOf(j)
|
|
341
|
+
if (close !== -1) {
|
|
342
|
+
const k = skipWhitespace(masked, close + 1)
|
|
343
|
+
if (masked[k] === '(') {
|
|
344
|
+
const literal = staticBracketKeyOf(text, masked, j, close)
|
|
345
|
+
if (literal !== null) name = literal
|
|
346
|
+
parenIdx = k
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
if (parenIdx !== -1) {
|
|
351
|
+
const balanced = sliceBalancedArgs(masked, text, parenIdx)
|
|
352
|
+
sites.push({ start: i, calleeEnd: parenIdx, end: balanced.closeIdx, innerText: balanced.innerText.trim(), name })
|
|
353
|
+
i += 5
|
|
354
|
+
continue
|
|
355
|
+
}
|
|
356
|
+
i += 1
|
|
357
|
+
}
|
|
358
|
+
return sites
|
|
359
|
+
}
|
|
360
|
+
|