blun-king-cli 9.1.536 → 9.1.550
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/LIESMICH.txt +13 -869
- package/README.md +41 -833
- package/bin/assistant-message-offload-policy.cjs +3 -1
- package/bin/context-performance-policy.cjs +2 -5
- package/bin/context-pressure-policy.cjs +20 -0
- package/bin/cron-run-output.cjs +45 -0
- package/bin/cron-run-store.cjs +145 -0
- package/bin/durable-task-resume-policy.cjs +130 -0
- package/bin/durable-task-resume-runtime.cjs +117 -0
- package/bin/durable-task-resume-store.cjs +88 -0
- package/bin/editable-tool-approval-policy.cjs +540 -0
- package/bin/editable-tool-approval-runtime.cjs +99 -0
- package/bin/html-to-research-markdown.cjs +146 -0
- package/bin/programmatic-tool-runtime.mjs +330 -4
- package/bin/read-continuation-policy.cjs +36 -5
- package/bin/scoped-cron-run-policy.cjs +358 -0
- package/bin/startup-preferences.cjs +3 -3
- package/bin/structured-agent-swarm-output.cjs +325 -0
- package/bin/subagent-context-fork-policy.cjs +155 -0
- package/bin/subagent-skill-policy.cjs +204 -0
- package/bin/telegram-approval-relay.cjs +2 -1
- package/bin/tool-result-offload-policy.cjs +14 -1
- package/bin/update-notice.js +14 -18
- package/bin/user-message-offload-policy.cjs +3 -1
- package/blun.mjs +1089 -477
- package/codebase-index/README.md +12 -0
- package/codebase-index/codebase_index.py +129 -18
- package/package.json +23 -58
- package/telegram-plugin/bin/telegram-typing-keepalive.cjs +89 -0
- package/telegram-plugin/dist/bridge.mjs +8 -1
- package/CHANGELOG.md +0 -321
- package/agent-spine-plugin/CHANGELOG.md +0 -406
- package/agent-spine-plugin/CONTRIBUTING.md +0 -52
- package/agent-spine-plugin/README.md +0 -344
- package/agent-spine-plugin/SECURITY.md +0 -47
- package/agent-spine-plugin/docs/acceptance.md +0 -61
- package/agent-spine-plugin/docs/architecture.md +0 -183
- package/agent-spine-plugin/docs/attention.md +0 -121
- package/agent-spine-plugin/docs/automatic-continuity.md +0 -79
- package/agent-spine-plugin/docs/channel-runtime.md +0 -92
- package/agent-spine-plugin/docs/coordination.md +0 -138
- package/agent-spine-plugin/docs/feed-transport.md +0 -99
- package/agent-spine-plugin/docs/gateway-runtime.md +0 -116
- package/agent-spine-plugin/docs/harness-reference.md +0 -45
- package/agent-spine-plugin/docs/host-integration.md +0 -129
- package/agent-spine-plugin/docs/https-transport.md +0 -116
- package/agent-spine-plugin/docs/learning.md +0 -133
- package/agent-spine-plugin/docs/object-transport.md +0 -93
- package/agent-spine-plugin/docs/peer-transport.md +0 -88
- package/agent-spine-plugin/docs/preflight-recall.md +0 -69
- package/agent-spine-plugin/docs/preservation-contract.md +0 -53
- package/agent-spine-plugin/docs/quality-gates.md +0 -50
- package/agent-spine-plugin/docs/relationships.md +0 -73
- package/agent-spine-plugin/docs/releasing.md +0 -83
- package/agent-spine-plugin/docs/roadmap.md +0 -307
- package/agent-spine-plugin/docs/selfstarter.md +0 -88
- package/agent-spine-plugin/docs/session-briefing.md +0 -74
- package/agent-spine-plugin/docs/shared-memory.md +0 -259
- package/agent-spine-plugin/docs/source-roots.md +0 -86
- package/agent-spine-plugin/docs/sqlite-transport.md +0 -76
- package/agent-spine-plugin/scripts/check-hosts.js +0 -195
- package/agent-spine-plugin/scripts/check-install.js +0 -569
- package/agent-spine-plugin/scripts/check-syntax.js +0 -29
- package/agent-spine-plugin/scripts/github-actions.js +0 -11
- package/agent-spine-plugin/scripts/release-check.js +0 -128
- package/agent-spine-plugin/scripts/run-acceptance.js +0 -19
- package/agent-spine-plugin/scripts/run-checks.js +0 -46
- package/agent-spine-plugin/scripts/run-tests-hermetic.js +0 -73
- package/agent-spine-plugin/spine-example/1-identity.md +0 -12
- package/agent-spine-plugin/spine-example/2-voice.md +0 -6
- package/agent-spine-plugin/spine-example/3-conduct.md +0 -8
- package/agent-spine-plugin/spine-example/4-history.md +0 -4
- package/bin/fredrik-glm-provider.cjs +0 -256
- package/bin/package-regression-policy.cjs +0 -77
- package/fredrik-glm-profile.toml.example +0 -26
- package/release-planned-removals.json +0 -15
- package/scripts/check-active-profile-plugin-startup.js +0 -36
- package/scripts/check-active-work-steer-regression.js +0 -46
- package/scripts/check-approval-observability-regression.js +0 -111
- package/scripts/check-approval-queue-shortcuts-regression.js +0 -65
- package/scripts/check-bundled-agent-spine-regression.js +0 -48
- package/scripts/check-codebase-search-packaging-regression.js +0 -92
- package/scripts/check-copy-command-regression.js +0 -74
- package/scripts/check-current-turn-read-pin-mutation-regression.js +0 -72
- package/scripts/check-current-turn-read-pin-regression.js +0 -94
- package/scripts/check-deepseek-native-max-regression.js +0 -49
- package/scripts/check-empty-response-effort-downgrade-regression.js +0 -48
- package/scripts/check-fredrik-glm-mutation-regression.js +0 -18
- package/scripts/check-fredrik-glm-regression.js +0 -169
- package/scripts/check-historical-tool-result-preview-regression.js +0 -77
- package/scripts/check-history-pressure-offload-regression.js +0 -77
- package/scripts/check-mcp-startup-wait-budget.js +0 -48
- package/scripts/check-package-regression.js +0 -38
- package/scripts/check-plugin-startup-regression.js +0 -53
- package/scripts/check-programmatic-context-isolation-regression.js +0 -193
- package/scripts/check-programmatic-tool-regression.js +0 -294
- package/scripts/check-queue-controls-regression.js +0 -189
- package/scripts/check-release-metadata.js +0 -103
- package/scripts/check-reload-agent-spine-regression.js +0 -76
- package/scripts/check-resume-replay-regression.js +0 -102
- package/scripts/check-session-cancel-regression.js +0 -43
- package/scripts/check-session-picker-resume-metrics-regression.js +0 -97
- package/scripts/check-session-start-hook-context-regression.js +0 -228
- package/scripts/check-shell-terminal-isolation-regression.js +0 -81
- package/scripts/check-slash-escape-regression.js +0 -89
- package/scripts/check-startup-swarm-command-regression.js +0 -24
- package/scripts/check-structured-subagent-output-regression.js +0 -331
- package/scripts/check-telegram-bridge-watchdog.js +0 -60
- package/scripts/check-telegram-direct-work-resume-regression.js +0 -53
- package/scripts/check-telegram-loop-exactly-once-regression.js +0 -71
- package/scripts/check-todo-loop-regression.js +0 -78
- package/scripts/check-todo-progress-regression.js +0 -416
- package/scripts/check-todo-recovery-catalog-regression.js +0 -50
- package/scripts/check-tool-schema-capacity-regression.js +0 -40
- package/scripts/programmatic-tool-runtime.test.mjs +0 -365
- package/scripts/structured-subagent-output.test.cjs +0 -170
- /package/{scripts → bin}/fix-node-pty-perms.js +0 -0
|
@@ -0,0 +1,540 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {createHash} = require('node:crypto');
|
|
4
|
+
|
|
5
|
+
const EDITABLE_TOOL_ALLOWLIST = Object.freeze([
|
|
6
|
+
'Bash',
|
|
7
|
+
'Read',
|
|
8
|
+
'Write',
|
|
9
|
+
'Edit',
|
|
10
|
+
'Glob',
|
|
11
|
+
'Grep',
|
|
12
|
+
'FetchURL',
|
|
13
|
+
]);
|
|
14
|
+
const EDITABLE_TOOL_ALLOWSET = new Set(EDITABLE_TOOL_ALLOWLIST);
|
|
15
|
+
const DANGEROUS_KEYS = new Set(['__proto__', 'prototype', 'constructor']);
|
|
16
|
+
const APPROVAL_RESPONSE_FIELDS = new Set([
|
|
17
|
+
'decision',
|
|
18
|
+
'scope',
|
|
19
|
+
'feedback',
|
|
20
|
+
'selected_label',
|
|
21
|
+
'edited_tool_input',
|
|
22
|
+
'edited_tool_input_json',
|
|
23
|
+
]);
|
|
24
|
+
const DECISIONS = new Set(['approved', 'rejected', 'cancelled', 'edited']);
|
|
25
|
+
const DEFAULT_LIMITS = Object.freeze({
|
|
26
|
+
maxBytes: 65_536,
|
|
27
|
+
maxDepth: 16,
|
|
28
|
+
maxProperties: 256,
|
|
29
|
+
maxCollectionItems: 512,
|
|
30
|
+
maxDecodedStringChars: 32_768,
|
|
31
|
+
maxChangedFields: 32,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
class EditableToolApprovalError extends Error {
|
|
35
|
+
constructor(code, message) {
|
|
36
|
+
super(message);
|
|
37
|
+
this.name = 'EditableToolApprovalError';
|
|
38
|
+
this.code = code;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function fail(code, message) {
|
|
43
|
+
throw new EditableToolApprovalError(code, message);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function resolveLimits(overrides = {}) {
|
|
47
|
+
if (overrides === null || typeof overrides !== 'object' || Array.isArray(overrides)) {
|
|
48
|
+
fail('EDITED_INPUT_LIMITS_INVALID', 'Edited input limits must be an object.');
|
|
49
|
+
}
|
|
50
|
+
const limits = {...DEFAULT_LIMITS};
|
|
51
|
+
for (const key of Object.keys(overrides)) {
|
|
52
|
+
if (!Object.hasOwn(DEFAULT_LIMITS, key)) {
|
|
53
|
+
fail('EDITED_INPUT_LIMITS_INVALID', `Unknown edited input limit: ${key}`);
|
|
54
|
+
}
|
|
55
|
+
const value = overrides[key];
|
|
56
|
+
if (!Number.isSafeInteger(value) || value < 0) {
|
|
57
|
+
fail('EDITED_INPUT_LIMITS_INVALID', `Edited input limit ${key} must be a non-negative safe integer.`);
|
|
58
|
+
}
|
|
59
|
+
if (value > DEFAULT_LIMITS[key]) {
|
|
60
|
+
fail('EDITED_INPUT_LIMIT_WIDENING_FORBIDDEN', `Edited input limit ${key} cannot exceed its production default.`);
|
|
61
|
+
}
|
|
62
|
+
limits[key] = value;
|
|
63
|
+
}
|
|
64
|
+
return limits;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function isEditableToolApprovalEligible(toolName) {
|
|
68
|
+
return typeof toolName === 'string' && EDITABLE_TOOL_ALLOWSET.has(toolName);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function validateUnicodeScalarString(value) {
|
|
72
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
73
|
+
const code = value.charCodeAt(index);
|
|
74
|
+
if (code >= 0xd800 && code <= 0xdbff) {
|
|
75
|
+
const next = value.charCodeAt(index + 1);
|
|
76
|
+
if (!(next >= 0xdc00 && next <= 0xdfff)) {
|
|
77
|
+
fail('EDITED_INPUT_INVALID_UNICODE', 'Edited input contains an unpaired high surrogate.');
|
|
78
|
+
}
|
|
79
|
+
index += 1;
|
|
80
|
+
} else if (code >= 0xdc00 && code <= 0xdfff) {
|
|
81
|
+
fail('EDITED_INPUT_INVALID_UNICODE', 'Edited input contains an unpaired low surrogate.');
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function createCounters(limits) {
|
|
87
|
+
return {
|
|
88
|
+
limits,
|
|
89
|
+
properties: 0,
|
|
90
|
+
collectionItems: 0,
|
|
91
|
+
decodedStringChars: 0,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function countString(counters, value) {
|
|
96
|
+
validateUnicodeScalarString(value);
|
|
97
|
+
counters.decodedStringChars += value.length;
|
|
98
|
+
if (counters.decodedStringChars > counters.limits.maxDecodedStringChars) {
|
|
99
|
+
fail('EDITED_INPUT_STRING_LIMIT', 'Edited input exceeds the decoded string limit.');
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function countProperty(counters) {
|
|
104
|
+
counters.properties += 1;
|
|
105
|
+
counters.collectionItems += 1;
|
|
106
|
+
if (counters.properties > counters.limits.maxProperties) {
|
|
107
|
+
fail('EDITED_INPUT_PROPERTIES_LIMIT', 'Edited input exceeds the property limit.');
|
|
108
|
+
}
|
|
109
|
+
if (counters.collectionItems > counters.limits.maxCollectionItems) {
|
|
110
|
+
fail('EDITED_INPUT_COLLECTION_LIMIT', 'Edited input exceeds the collection item limit.');
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function countArrayItem(counters) {
|
|
115
|
+
counters.collectionItems += 1;
|
|
116
|
+
if (counters.collectionItems > counters.limits.maxCollectionItems) {
|
|
117
|
+
fail('EDITED_INPUT_COLLECTION_LIMIT', 'Edited input exceeds the collection item limit.');
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function assertDepth(counters, depth) {
|
|
122
|
+
if (depth > counters.limits.maxDepth) {
|
|
123
|
+
fail('EDITED_INPUT_DEPTH_LIMIT', 'Edited input exceeds the nesting depth limit.');
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function parseEditedToolInputJson(rawJson, limitOverrides) {
|
|
128
|
+
if (typeof rawJson !== 'string') {
|
|
129
|
+
fail('EDITED_INPUT_INVALID_JSON', 'Edited tool input must be supplied as JSON text.');
|
|
130
|
+
}
|
|
131
|
+
const limits = resolveLimits(limitOverrides);
|
|
132
|
+
if (Buffer.byteLength(rawJson, 'utf8') > limits.maxBytes) {
|
|
133
|
+
fail('EDITED_INPUT_BYTES_LIMIT', 'Edited input exceeds the raw byte limit.');
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const counters = createCounters(limits);
|
|
137
|
+
let cursor = 0;
|
|
138
|
+
|
|
139
|
+
function skipWhitespace() {
|
|
140
|
+
while (cursor < rawJson.length && /[\u0009\u000a\u000d\u0020]/.test(rawJson[cursor])) cursor += 1;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function parseString() {
|
|
144
|
+
if (rawJson[cursor] !== '"') fail('EDITED_INPUT_INVALID_JSON', 'Expected a JSON string.');
|
|
145
|
+
cursor += 1;
|
|
146
|
+
let output = '';
|
|
147
|
+
while (cursor < rawJson.length) {
|
|
148
|
+
const char = rawJson[cursor];
|
|
149
|
+
cursor += 1;
|
|
150
|
+
if (char === '"') {
|
|
151
|
+
countString(counters, output);
|
|
152
|
+
return output;
|
|
153
|
+
}
|
|
154
|
+
if (char === '\\') {
|
|
155
|
+
if (cursor >= rawJson.length) fail('EDITED_INPUT_INVALID_JSON', 'Unterminated JSON escape.');
|
|
156
|
+
const escape = rawJson[cursor];
|
|
157
|
+
cursor += 1;
|
|
158
|
+
const simple = {'"': '"', '\\': '\\', '/': '/', b: '\b', f: '\f', n: '\n', r: '\r', t: '\t'};
|
|
159
|
+
if (Object.hasOwn(simple, escape)) {
|
|
160
|
+
output += simple[escape];
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
if (escape !== 'u') fail('EDITED_INPUT_INVALID_JSON', 'Invalid JSON escape.');
|
|
164
|
+
const hex = rawJson.slice(cursor, cursor + 4);
|
|
165
|
+
if (!/^[0-9a-fA-F]{4}$/.test(hex)) fail('EDITED_INPUT_INVALID_JSON', 'Invalid JSON Unicode escape.');
|
|
166
|
+
cursor += 4;
|
|
167
|
+
const code = Number.parseInt(hex, 16);
|
|
168
|
+
if (code >= 0xd800 && code <= 0xdbff) {
|
|
169
|
+
if (rawJson.slice(cursor, cursor + 2) !== '\\u') {
|
|
170
|
+
fail('EDITED_INPUT_INVALID_UNICODE', 'Unpaired high surrogate in JSON escape.');
|
|
171
|
+
}
|
|
172
|
+
const lowHex = rawJson.slice(cursor + 2, cursor + 6);
|
|
173
|
+
if (!/^[0-9a-fA-F]{4}$/.test(lowHex)) fail('EDITED_INPUT_INVALID_JSON', 'Invalid low surrogate escape.');
|
|
174
|
+
const low = Number.parseInt(lowHex, 16);
|
|
175
|
+
if (!(low >= 0xdc00 && low <= 0xdfff)) {
|
|
176
|
+
fail('EDITED_INPUT_INVALID_UNICODE', 'Unpaired high surrogate in JSON escape.');
|
|
177
|
+
}
|
|
178
|
+
cursor += 6;
|
|
179
|
+
output += String.fromCodePoint(0x10000 + ((code - 0xd800) << 10) + (low - 0xdc00));
|
|
180
|
+
} else if (code >= 0xdc00 && code <= 0xdfff) {
|
|
181
|
+
fail('EDITED_INPUT_INVALID_UNICODE', 'Unpaired low surrogate in JSON escape.');
|
|
182
|
+
} else {
|
|
183
|
+
output += String.fromCharCode(code);
|
|
184
|
+
}
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
const code = char.charCodeAt(0);
|
|
188
|
+
if (code < 0x20) fail('EDITED_INPUT_INVALID_JSON', 'Unescaped control character in JSON string.');
|
|
189
|
+
if (code >= 0xd800 && code <= 0xdbff) {
|
|
190
|
+
const next = rawJson.charCodeAt(cursor);
|
|
191
|
+
if (!(next >= 0xdc00 && next <= 0xdfff)) {
|
|
192
|
+
fail('EDITED_INPUT_INVALID_UNICODE', 'Unpaired high surrogate in JSON string.');
|
|
193
|
+
}
|
|
194
|
+
output += char + rawJson[cursor];
|
|
195
|
+
cursor += 1;
|
|
196
|
+
} else if (code >= 0xdc00 && code <= 0xdfff) {
|
|
197
|
+
fail('EDITED_INPUT_INVALID_UNICODE', 'Unpaired low surrogate in JSON string.');
|
|
198
|
+
} else {
|
|
199
|
+
output += char;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
fail('EDITED_INPUT_INVALID_JSON', 'Unterminated JSON string.');
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function parseNumber() {
|
|
206
|
+
const remainder = rawJson.slice(cursor);
|
|
207
|
+
const match = /^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?/.exec(remainder);
|
|
208
|
+
if (!match) fail('EDITED_INPUT_INVALID_JSON', 'Invalid JSON number.');
|
|
209
|
+
cursor += match[0].length;
|
|
210
|
+
const value = Number(match[0]);
|
|
211
|
+
if (!Number.isFinite(value)) fail('EDITED_INPUT_NONFINITE_NUMBER', 'Edited input number must be finite.');
|
|
212
|
+
return value;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function parseArray(depth) {
|
|
216
|
+
assertDepth(counters, depth);
|
|
217
|
+
cursor += 1;
|
|
218
|
+
const output = [];
|
|
219
|
+
skipWhitespace();
|
|
220
|
+
if (rawJson[cursor] === ']') {
|
|
221
|
+
cursor += 1;
|
|
222
|
+
Object.setPrototypeOf(output, null);
|
|
223
|
+
return Object.freeze(output);
|
|
224
|
+
}
|
|
225
|
+
while (true) {
|
|
226
|
+
countArrayItem(counters);
|
|
227
|
+
output[output.length] = parseValue(depth + 1);
|
|
228
|
+
skipWhitespace();
|
|
229
|
+
if (rawJson[cursor] === ']') {
|
|
230
|
+
cursor += 1;
|
|
231
|
+
Object.setPrototypeOf(output, null);
|
|
232
|
+
return Object.freeze(output);
|
|
233
|
+
}
|
|
234
|
+
if (rawJson[cursor] !== ',') fail('EDITED_INPUT_INVALID_JSON', 'Expected comma or array end.');
|
|
235
|
+
cursor += 1;
|
|
236
|
+
skipWhitespace();
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function parseObject(depth) {
|
|
241
|
+
assertDepth(counters, depth);
|
|
242
|
+
cursor += 1;
|
|
243
|
+
const output = Object.create(null);
|
|
244
|
+
const keys = new Set();
|
|
245
|
+
skipWhitespace();
|
|
246
|
+
if (rawJson[cursor] === '}') {
|
|
247
|
+
cursor += 1;
|
|
248
|
+
return Object.freeze(output);
|
|
249
|
+
}
|
|
250
|
+
while (true) {
|
|
251
|
+
if (rawJson[cursor] !== '"') fail('EDITED_INPUT_INVALID_JSON', 'Expected an object key.');
|
|
252
|
+
const key = parseString();
|
|
253
|
+
if (DANGEROUS_KEYS.has(key)) fail('EDITED_INPUT_DANGEROUS_KEY', `Dangerous edited input key: ${key}`);
|
|
254
|
+
if (keys.has(key)) fail('EDITED_INPUT_DUPLICATE_KEY', `Duplicate edited input key: ${key}`);
|
|
255
|
+
keys.add(key);
|
|
256
|
+
countProperty(counters);
|
|
257
|
+
skipWhitespace();
|
|
258
|
+
if (rawJson[cursor] !== ':') fail('EDITED_INPUT_INVALID_JSON', 'Expected colon after object key.');
|
|
259
|
+
cursor += 1;
|
|
260
|
+
output[key] = parseValue(depth + 1);
|
|
261
|
+
skipWhitespace();
|
|
262
|
+
if (rawJson[cursor] === '}') {
|
|
263
|
+
cursor += 1;
|
|
264
|
+
return Object.freeze(output);
|
|
265
|
+
}
|
|
266
|
+
if (rawJson[cursor] !== ',') fail('EDITED_INPUT_INVALID_JSON', 'Expected comma or object end.');
|
|
267
|
+
cursor += 1;
|
|
268
|
+
skipWhitespace();
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function parseValue(depth) {
|
|
273
|
+
skipWhitespace();
|
|
274
|
+
const char = rawJson[cursor];
|
|
275
|
+
if (char === '{') return parseObject(depth);
|
|
276
|
+
if (char === '[') return parseArray(depth);
|
|
277
|
+
if (char === '"') return parseString();
|
|
278
|
+
if (char === '-' || (char >= '0' && char <= '9')) return parseNumber();
|
|
279
|
+
if (rawJson.startsWith('true', cursor)) {
|
|
280
|
+
cursor += 4;
|
|
281
|
+
return true;
|
|
282
|
+
}
|
|
283
|
+
if (rawJson.startsWith('false', cursor)) {
|
|
284
|
+
cursor += 5;
|
|
285
|
+
return false;
|
|
286
|
+
}
|
|
287
|
+
if (rawJson.startsWith('null', cursor)) {
|
|
288
|
+
cursor += 4;
|
|
289
|
+
return null;
|
|
290
|
+
}
|
|
291
|
+
fail('EDITED_INPUT_INVALID_JSON', 'Invalid JSON value.');
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
skipWhitespace();
|
|
295
|
+
const value = parseValue(1);
|
|
296
|
+
skipWhitespace();
|
|
297
|
+
if (cursor !== rawJson.length) fail('EDITED_INPUT_TRAILING_CONTENT', 'Edited input has trailing content.');
|
|
298
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
299
|
+
fail('EDITED_INPUT_OBJECT_REQUIRED', 'Edited tool input must be a top-level JSON object.');
|
|
300
|
+
}
|
|
301
|
+
return value;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function validateEditedToolInputObject(input, limitOverrides) {
|
|
305
|
+
const limits = resolveLimits(limitOverrides);
|
|
306
|
+
const counters = createCounters(limits);
|
|
307
|
+
|
|
308
|
+
function clone(value, depth) {
|
|
309
|
+
if (value === null || typeof value === 'boolean') return value;
|
|
310
|
+
if (typeof value === 'string') {
|
|
311
|
+
countString(counters, value);
|
|
312
|
+
return value;
|
|
313
|
+
}
|
|
314
|
+
if (typeof value === 'number') {
|
|
315
|
+
if (!Number.isFinite(value)) fail('EDITED_INPUT_NONFINITE_NUMBER', 'Edited input number must be finite.');
|
|
316
|
+
return value;
|
|
317
|
+
}
|
|
318
|
+
if (value === undefined) fail('EDITED_INPUT_UNDEFINED_REJECTED', 'Undefined is not valid edited JSON input.');
|
|
319
|
+
if (typeof value === 'function' || typeof value === 'bigint') {
|
|
320
|
+
fail('EDITED_INPUT_EXECUTABLE_REJECTED', 'Executable or non-JSON values are forbidden in edited input.');
|
|
321
|
+
}
|
|
322
|
+
if (typeof value === 'symbol') fail('EDITED_INPUT_SYMBOL_REJECTED', 'Symbols are forbidden in edited input.');
|
|
323
|
+
|
|
324
|
+
assertDepth(counters, depth);
|
|
325
|
+
const symbols = Object.getOwnPropertySymbols(value);
|
|
326
|
+
if (symbols.length > 0) fail('EDITED_INPUT_SYMBOL_REJECTED', 'Symbol keys are forbidden in edited input.');
|
|
327
|
+
const descriptors = Object.getOwnPropertyDescriptors(value);
|
|
328
|
+
|
|
329
|
+
if (Array.isArray(value)) {
|
|
330
|
+
const prototype = Object.getPrototypeOf(value);
|
|
331
|
+
if (prototype !== Array.prototype && prototype !== null) {
|
|
332
|
+
fail('EDITED_INPUT_PROTOTYPE_REJECTED', 'Foreign array prototypes are forbidden in edited input.');
|
|
333
|
+
}
|
|
334
|
+
const output = [];
|
|
335
|
+
for (const key of Reflect.ownKeys(descriptors)) {
|
|
336
|
+
if (key === 'length') continue;
|
|
337
|
+
if (typeof key !== 'string' || !/^(?:0|[1-9]\d*)$/.test(key) || Number(key) >= value.length) {
|
|
338
|
+
fail('EDITED_INPUT_ARRAY_PROPERTY_REJECTED', 'Non-index array properties are forbidden in edited input.');
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
342
|
+
const descriptor = descriptors[String(index)];
|
|
343
|
+
if (!descriptor) fail('EDITED_INPUT_ARRAY_HOLE_REJECTED', 'Sparse arrays are forbidden in edited input.');
|
|
344
|
+
if (!Object.hasOwn(descriptor, 'value')) {
|
|
345
|
+
fail('EDITED_INPUT_ACCESSOR_REJECTED', 'Accessors are forbidden in edited input.');
|
|
346
|
+
}
|
|
347
|
+
countArrayItem(counters);
|
|
348
|
+
output[index] = clone(descriptor.value, depth + 1);
|
|
349
|
+
}
|
|
350
|
+
Object.setPrototypeOf(output, null);
|
|
351
|
+
return Object.freeze(output);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
const prototype = Object.getPrototypeOf(value);
|
|
355
|
+
if (prototype !== Object.prototype && prototype !== null) {
|
|
356
|
+
fail('EDITED_INPUT_PROTOTYPE_REJECTED', 'Foreign object prototypes are forbidden in edited input.');
|
|
357
|
+
}
|
|
358
|
+
const output = Object.create(null);
|
|
359
|
+
for (const key of Reflect.ownKeys(descriptors)) {
|
|
360
|
+
if (typeof key !== 'string') fail('EDITED_INPUT_SYMBOL_REJECTED', 'Symbol keys are forbidden in edited input.');
|
|
361
|
+
if (DANGEROUS_KEYS.has(key)) fail('EDITED_INPUT_DANGEROUS_KEY', `Dangerous edited input key: ${key}`);
|
|
362
|
+
const descriptor = descriptors[key];
|
|
363
|
+
if (!Object.hasOwn(descriptor, 'value')) {
|
|
364
|
+
fail('EDITED_INPUT_ACCESSOR_REJECTED', 'Accessors are forbidden in edited input.');
|
|
365
|
+
}
|
|
366
|
+
if (!descriptor.enumerable) {
|
|
367
|
+
fail('EDITED_INPUT_NONENUMERABLE_REJECTED', 'Non-enumerable properties are forbidden in edited input.');
|
|
368
|
+
}
|
|
369
|
+
countString(counters, key);
|
|
370
|
+
countProperty(counters);
|
|
371
|
+
output[key] = clone(descriptor.value, depth + 1);
|
|
372
|
+
}
|
|
373
|
+
return Object.freeze(output);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
if (input === null || typeof input !== 'object' || Array.isArray(input)) {
|
|
377
|
+
fail('EDITED_INPUT_OBJECT_REQUIRED', 'Edited tool input must be a top-level object.');
|
|
378
|
+
}
|
|
379
|
+
const output = clone(input, 1);
|
|
380
|
+
if (Buffer.byteLength(canonicalValueJson(output), 'utf8') > limits.maxBytes) {
|
|
381
|
+
fail('EDITED_INPUT_BYTES_LIMIT', 'Edited input exceeds the canonical byte limit.');
|
|
382
|
+
}
|
|
383
|
+
return output;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
function readApprovalResponse(response) {
|
|
387
|
+
if (response === null || typeof response !== 'object' || Array.isArray(response)) {
|
|
388
|
+
fail('EDITED_APPROVAL_RESPONSE_INVALID', 'Approval response must be an object.');
|
|
389
|
+
}
|
|
390
|
+
const prototype = Object.getPrototypeOf(response);
|
|
391
|
+
if (prototype !== Object.prototype && prototype !== null) {
|
|
392
|
+
fail('EDITED_APPROVAL_RESPONSE_INVALID', 'Approval response must have a plain prototype.');
|
|
393
|
+
}
|
|
394
|
+
if (Object.getOwnPropertySymbols(response).length > 0) {
|
|
395
|
+
fail('EDITED_APPROVAL_UNKNOWN_FIELD', 'Approval response symbol fields are forbidden.');
|
|
396
|
+
}
|
|
397
|
+
const descriptors = Object.getOwnPropertyDescriptors(response);
|
|
398
|
+
const values = Object.create(null);
|
|
399
|
+
for (const key of Object.keys(descriptors)) {
|
|
400
|
+
if (!APPROVAL_RESPONSE_FIELDS.has(key)) {
|
|
401
|
+
fail('EDITED_APPROVAL_UNKNOWN_FIELD', `Unknown approval response field: ${key}`);
|
|
402
|
+
}
|
|
403
|
+
const descriptor = descriptors[key];
|
|
404
|
+
if (!Object.hasOwn(descriptor, 'value')) {
|
|
405
|
+
fail('EDITED_APPROVAL_RESPONSE_INVALID', 'Approval response accessors are forbidden.');
|
|
406
|
+
}
|
|
407
|
+
values[key] = descriptor.value;
|
|
408
|
+
}
|
|
409
|
+
return {descriptors, values};
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
function validateEditedApprovalDecision({response, toolName, editCount, limits} = {}) {
|
|
413
|
+
const {descriptors, values} = readApprovalResponse(response);
|
|
414
|
+
if (!DECISIONS.has(values.decision)) {
|
|
415
|
+
fail('EDITED_APPROVAL_DECISION_INVALID', 'Unknown approval decision.');
|
|
416
|
+
}
|
|
417
|
+
if (!Number.isSafeInteger(editCount) || editCount < 0) {
|
|
418
|
+
fail('EDITED_APPROVAL_EDIT_COUNT_INVALID', 'Edit count must be a non-negative safe integer.');
|
|
419
|
+
}
|
|
420
|
+
const hasObjectInput = Object.hasOwn(descriptors, 'edited_tool_input');
|
|
421
|
+
const hasJsonInput = Object.hasOwn(descriptors, 'edited_tool_input_json');
|
|
422
|
+
|
|
423
|
+
if (values.decision !== 'edited') {
|
|
424
|
+
if (hasObjectInput || hasJsonInput) {
|
|
425
|
+
fail('EDITED_APPROVAL_INPUT_FORBIDDEN', 'Edited tool input is only valid for an edited decision.');
|
|
426
|
+
}
|
|
427
|
+
return Object.freeze({
|
|
428
|
+
decision: values.decision,
|
|
429
|
+
...(Object.hasOwn(descriptors, 'scope') ? {scope: values.scope} : {}),
|
|
430
|
+
...(Object.hasOwn(descriptors, 'feedback') ? {feedback: values.feedback} : {}),
|
|
431
|
+
...(Object.hasOwn(descriptors, 'selected_label') ? {selectedLabel: values.selected_label} : {}),
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
if (!isEditableToolApprovalEligible(toolName)) {
|
|
436
|
+
fail('EDITED_APPROVAL_TOOL_INELIGIBLE', `Tool ${String(toolName)} does not allow edited approval.`);
|
|
437
|
+
}
|
|
438
|
+
if (editCount !== 0) fail('EDITED_APPROVAL_ALREADY_EDITED', 'A tool call may be edited at most once.');
|
|
439
|
+
if (Object.hasOwn(descriptors, 'scope')) {
|
|
440
|
+
fail('EDITED_APPROVAL_SCOPE_FORBIDDEN', 'Edited approval cannot create session scope.');
|
|
441
|
+
}
|
|
442
|
+
if (hasObjectInput === hasJsonInput) {
|
|
443
|
+
fail('EDITED_APPROVAL_INPUT_REQUIRED', 'Edited approval requires exactly one edited input representation.');
|
|
444
|
+
}
|
|
445
|
+
const editedToolInput = hasJsonInput
|
|
446
|
+
? parseEditedToolInputJson(values.edited_tool_input_json, limits)
|
|
447
|
+
: validateEditedToolInputObject(values.edited_tool_input, limits);
|
|
448
|
+
return Object.freeze({decision: 'edited', editedToolInput});
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
function canonicalEditedToolInputJson(input) {
|
|
452
|
+
const safeInput = validateEditedToolInputObject(input);
|
|
453
|
+
function encode(value) {
|
|
454
|
+
if (value === null) return 'null';
|
|
455
|
+
if (typeof value === 'string' || typeof value === 'boolean' || typeof value === 'number') {
|
|
456
|
+
return JSON.stringify(value);
|
|
457
|
+
}
|
|
458
|
+
if (Array.isArray(value)) return `[${Array.from(value, encode).join(',')}]`;
|
|
459
|
+
const keys = Object.keys(value).sort();
|
|
460
|
+
return `{${keys.map((key) => `${JSON.stringify(key)}:${encode(value[key])}`).join(',')}}`;
|
|
461
|
+
}
|
|
462
|
+
return encode(safeInput);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
function hashEditedToolInput(input) {
|
|
466
|
+
return createHash('sha256').update(canonicalEditedToolInputJson(input), 'utf8').digest('hex');
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
function changedTopLevelFields(originalArgs, editedArgs) {
|
|
470
|
+
const keys = new Set([...Object.keys(originalArgs), ...Object.keys(editedArgs)]);
|
|
471
|
+
const changed = [];
|
|
472
|
+
for (const key of keys) {
|
|
473
|
+
const originalHas = Object.hasOwn(originalArgs, key);
|
|
474
|
+
const editedHas = Object.hasOwn(editedArgs, key);
|
|
475
|
+
if (originalHas !== editedHas) {
|
|
476
|
+
changed.push(key);
|
|
477
|
+
continue;
|
|
478
|
+
}
|
|
479
|
+
const originalValue = originalArgs[key];
|
|
480
|
+
const editedValue = editedArgs[key];
|
|
481
|
+
const originalJson = canonicalValueJson(originalValue);
|
|
482
|
+
const editedJson = canonicalValueJson(editedValue);
|
|
483
|
+
if (originalJson !== editedJson) changed.push(key);
|
|
484
|
+
}
|
|
485
|
+
return changed.sort();
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
function canonicalValueJson(value) {
|
|
489
|
+
if (value === null) return 'null';
|
|
490
|
+
if (typeof value === 'string' || typeof value === 'boolean' || typeof value === 'number') return JSON.stringify(value);
|
|
491
|
+
if (Array.isArray(value)) return `[${Array.from(value, canonicalValueJson).join(',')}]`;
|
|
492
|
+
const keys = Object.keys(value).sort();
|
|
493
|
+
return `{${keys.map((key) => `${JSON.stringify(key)}:${canonicalValueJson(value[key])}`).join(',')}}`;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
function buildApprovalEditedEvent({
|
|
497
|
+
turnId,
|
|
498
|
+
toolCallId,
|
|
499
|
+
toolName,
|
|
500
|
+
originalArgs,
|
|
501
|
+
editedArgs,
|
|
502
|
+
maxChangedFields = DEFAULT_LIMITS.maxChangedFields,
|
|
503
|
+
} = {}) {
|
|
504
|
+
if (!Number.isSafeInteger(turnId) || turnId < 0 || typeof toolCallId !== 'string' || toolCallId.length === 0 || toolCallId.length > 512) {
|
|
505
|
+
fail('EDITED_EVENT_IDENTITY_INVALID', 'Approval edited event identity is invalid.');
|
|
506
|
+
}
|
|
507
|
+
if (!isEditableToolApprovalEligible(toolName)) {
|
|
508
|
+
fail('EDITED_APPROVAL_TOOL_INELIGIBLE', `Tool ${String(toolName)} does not allow edited approval.`);
|
|
509
|
+
}
|
|
510
|
+
if (!Number.isSafeInteger(maxChangedFields) || maxChangedFields < 0 || maxChangedFields > DEFAULT_LIMITS.maxChangedFields) {
|
|
511
|
+
fail('EDITED_EVENT_CHANGED_FIELDS_LIMIT_INVALID', 'Changed-field limit is invalid.');
|
|
512
|
+
}
|
|
513
|
+
const safeOriginal = validateEditedToolInputObject(originalArgs);
|
|
514
|
+
const safeEdited = validateEditedToolInputObject(editedArgs);
|
|
515
|
+
const allChangedFields = changedTopLevelFields(safeOriginal, safeEdited);
|
|
516
|
+
const changedFields = Object.freeze(allChangedFields.slice(0, maxChangedFields));
|
|
517
|
+
return Object.freeze({
|
|
518
|
+
type: 'permission.approval_edited',
|
|
519
|
+
turnId,
|
|
520
|
+
toolCallId,
|
|
521
|
+
toolName,
|
|
522
|
+
originalArgsSha256: hashEditedToolInput(safeOriginal),
|
|
523
|
+
editedArgsSha256: hashEditedToolInput(safeEdited),
|
|
524
|
+
changedFields,
|
|
525
|
+
...(allChangedFields.length > changedFields.length ? {changedFieldsTruncated: true} : {}),
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
module.exports = Object.freeze({
|
|
530
|
+
DEFAULT_LIMITS,
|
|
531
|
+
EDITABLE_TOOL_ALLOWLIST,
|
|
532
|
+
EditableToolApprovalError,
|
|
533
|
+
buildApprovalEditedEvent,
|
|
534
|
+
canonicalEditedToolInputJson,
|
|
535
|
+
hashEditedToolInput,
|
|
536
|
+
isEditableToolApprovalEligible,
|
|
537
|
+
parseEditedToolInputJson,
|
|
538
|
+
validateEditedApprovalDecision,
|
|
539
|
+
validateEditedToolInputObject,
|
|
540
|
+
});
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const {
|
|
4
|
+
canonicalEditedToolInputJson,
|
|
5
|
+
validateEditedToolInputObject,
|
|
6
|
+
} = require('./editable-tool-approval-policy.cjs');
|
|
7
|
+
|
|
8
|
+
class EditableToolReprepareError extends Error {
|
|
9
|
+
constructor(code, message) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.name = 'EditableToolReprepareError';
|
|
12
|
+
this.code = code;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function fail(code, message) {
|
|
17
|
+
throw new EditableToolReprepareError(code, message);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function buildEditedToolCallPlan({
|
|
21
|
+
step,
|
|
22
|
+
call,
|
|
23
|
+
editedArgs,
|
|
24
|
+
editCount,
|
|
25
|
+
preflightToolCall,
|
|
26
|
+
} = {}) {
|
|
27
|
+
if (editCount !== 0) {
|
|
28
|
+
fail('EDITED_REPREPARE_ALREADY_EDITED', 'A tool call may be edited at most once.');
|
|
29
|
+
}
|
|
30
|
+
if (call?.kind !== 'runnable' || call.toolCall === null || typeof call.toolCall !== 'object' || typeof call.toolCall.id !== 'string' || typeof call.toolCall.name !== 'string') {
|
|
31
|
+
fail('EDITED_REPREPARE_CALL_INVALID', 'Only a runnable tool call can be re-prepared.');
|
|
32
|
+
}
|
|
33
|
+
if (!Array.isArray(step?.toolCalls) || typeof preflightToolCall !== 'function') {
|
|
34
|
+
fail('EDITED_REPREPARE_CONTEXT_INVALID', 'Edited re-preparation requires the active ordered tool-call list and preflight function.');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const matchingIndexes = [];
|
|
38
|
+
for (let index = 0; index < step.toolCalls.length; index += 1) {
|
|
39
|
+
if (step.toolCalls[index]?.id === call.toolCall.id) matchingIndexes.push(index);
|
|
40
|
+
}
|
|
41
|
+
if (matchingIndexes.length === 0) {
|
|
42
|
+
fail('EDITED_REPREPARE_IDENTITY_MISSING', 'The edited tool call is absent from the active ordered call list.');
|
|
43
|
+
}
|
|
44
|
+
if (matchingIndexes.length !== 1) {
|
|
45
|
+
fail('EDITED_REPREPARE_IDENTITY_AMBIGUOUS', 'The edited tool-call ID is not unique in the active call list.');
|
|
46
|
+
}
|
|
47
|
+
const position = matchingIndexes[0];
|
|
48
|
+
if (step.toolCalls[position] !== call.toolCall || call.toolName !== call.toolCall.name) {
|
|
49
|
+
fail('EDITED_REPREPARE_IDENTITY_CHANGED', 'The active tool-call identity changed before re-preparation.');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const safeEditedArgs = validateEditedToolInputObject(editedArgs);
|
|
53
|
+
const patchedToolCall = Object.freeze({
|
|
54
|
+
...call.toolCall,
|
|
55
|
+
id: call.toolCall.id,
|
|
56
|
+
name: call.toolCall.name,
|
|
57
|
+
arguments: canonicalEditedToolInputJson(safeEditedArgs),
|
|
58
|
+
});
|
|
59
|
+
const patchedToolCalls = step.toolCalls.slice();
|
|
60
|
+
patchedToolCalls[position] = patchedToolCall;
|
|
61
|
+
const editedStep = {
|
|
62
|
+
...step,
|
|
63
|
+
toolCalls: Object.freeze(patchedToolCalls),
|
|
64
|
+
};
|
|
65
|
+
const repreflight = preflightToolCall(editedStep, patchedToolCall);
|
|
66
|
+
if (repreflight === null || typeof repreflight !== 'object') {
|
|
67
|
+
fail('EDITED_REPREPARE_PREFLIGHT_INVALID', 'Edited tool preflight returned no result.');
|
|
68
|
+
}
|
|
69
|
+
if (repreflight.toolCall?.id !== call.toolCall.id || repreflight.toolCall?.name !== call.toolCall.name || repreflight.toolName !== call.toolName) {
|
|
70
|
+
fail('EDITED_REPREPARE_IDENTITY_CHANGED', 'Edited preflight changed the immutable tool identity.');
|
|
71
|
+
}
|
|
72
|
+
if (repreflight.kind === 'rejected') {
|
|
73
|
+
return Object.freeze({
|
|
74
|
+
kind: 'rejected',
|
|
75
|
+
step: editedStep,
|
|
76
|
+
call: repreflight,
|
|
77
|
+
editCount: 1,
|
|
78
|
+
output: repreflight.output,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
if (repreflight.kind !== 'runnable') {
|
|
82
|
+
fail('EDITED_REPREPARE_PREFLIGHT_INVALID', 'Edited preflight returned an unsupported call state.');
|
|
83
|
+
}
|
|
84
|
+
if (repreflight.tool !== call.tool) {
|
|
85
|
+
fail('EDITED_REPREPARE_REGISTRY_CHANGED', 'The authoritative tool registry entry changed during edited approval.');
|
|
86
|
+
}
|
|
87
|
+
return Object.freeze({
|
|
88
|
+
kind: 'runnable',
|
|
89
|
+
step: editedStep,
|
|
90
|
+
call: repreflight,
|
|
91
|
+
editCount: 1,
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
module.exports = Object.freeze({
|
|
96
|
+
EditableToolReprepareError,
|
|
97
|
+
buildEditedToolCallPlan,
|
|
98
|
+
});
|
|
99
|
+
|