blun-king-cli 9.1.536 → 9.1.561
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/compaction-transaction-policy.cjs +122 -0
- 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/default-model-output-budget-policy.cjs +28 -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/file-observation-policy.cjs +133 -0
- package/bin/html-to-research-markdown.cjs +146 -0
- package/bin/launcher-runtime.js +0 -1
- package/bin/micro-compaction-policy.cjs +64 -0
- package/bin/mnemo-connect-heartbeat.cjs +1 -3
- package/bin/programmatic-tool-runtime.mjs +330 -4
- package/bin/read-continuation-policy.cjs +36 -5
- package/bin/retry-checkpoint-policy.cjs +13 -0
- package/bin/scoped-cron-run-policy.cjs +358 -0
- package/bin/session-checkpoint-policy.cjs +25 -0
- package/bin/startup-preferences.cjs +4 -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-file-persistence.cjs +141 -0
- package/bin/tool-result-offload-policy.cjs +25 -33
- package/bin/turn-thinking-policy.cjs +2 -26
- package/bin/update-notice.js +30 -18
- package/bin/user-message-offload-policy.cjs +3 -1
- package/blun.mjs +1564 -640
- 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-mnemo-capture.cjs +1 -3
- 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/empty-response-retry-policy.cjs +0 -29
- 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,88 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('node:fs/promises');
|
|
4
|
+
const path = require('node:path');
|
|
5
|
+
const {
|
|
6
|
+
TASK_ID_PATTERN,
|
|
7
|
+
contractError,
|
|
8
|
+
isResumeIntent,
|
|
9
|
+
} = require('./durable-task-resume-policy.cjs');
|
|
10
|
+
|
|
11
|
+
class DurableTaskResumeStore {
|
|
12
|
+
constructor(sessionDir) {
|
|
13
|
+
if (typeof sessionDir !== 'string' || sessionDir.length === 0) {
|
|
14
|
+
throw contractError('session directory is required', 'persistence');
|
|
15
|
+
}
|
|
16
|
+
this.directory = path.join(sessionDir, 'task-resume-intents');
|
|
17
|
+
this.intents = new Map();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
fileFor(sourceTaskId) {
|
|
21
|
+
if (!TASK_ID_PATTERN.test(sourceTaskId)) throw contractError('source task ID is invalid');
|
|
22
|
+
return path.join(this.directory, `${sourceTaskId}.json`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async load() {
|
|
26
|
+
this.intents.clear();
|
|
27
|
+
let names;
|
|
28
|
+
try {
|
|
29
|
+
names = await fs.readdir(this.directory);
|
|
30
|
+
} catch (error) {
|
|
31
|
+
if (error && error.code === 'ENOENT') return;
|
|
32
|
+
throw error;
|
|
33
|
+
}
|
|
34
|
+
for (const name of names) {
|
|
35
|
+
if (!name.endsWith('.json')) continue;
|
|
36
|
+
const sourceTaskId = name.slice(0, -5);
|
|
37
|
+
if (!TASK_ID_PATTERN.test(sourceTaskId)) continue;
|
|
38
|
+
try {
|
|
39
|
+
const intent = JSON.parse(await fs.readFile(this.fileFor(sourceTaskId), 'utf8'));
|
|
40
|
+
if (isResumeIntent(intent) && intent.sourceTaskId === sourceTaskId) {
|
|
41
|
+
this.intents.set(sourceTaskId, intent);
|
|
42
|
+
}
|
|
43
|
+
} catch {}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
getBySourceTaskId(sourceTaskId) {
|
|
48
|
+
const intent = this.intents.get(sourceTaskId);
|
|
49
|
+
return intent === undefined ? undefined : { ...intent };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
list() {
|
|
53
|
+
return [...this.intents.values()].map((intent) => ({ ...intent }));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
startupRecoveryPlan() {
|
|
57
|
+
return [];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async write(intent) {
|
|
61
|
+
if (!isResumeIntent(intent)) throw contractError('resume intent is invalid', 'persistence');
|
|
62
|
+
await fs.mkdir(this.directory, { recursive: true, mode: 0o700 });
|
|
63
|
+
const target = this.fileFor(intent.sourceTaskId);
|
|
64
|
+
const temporary = `${target}.tmp-${process.pid}-${Date.now().toString(36)}`;
|
|
65
|
+
try {
|
|
66
|
+
const handle = await fs.open(temporary, 'wx', 0o600);
|
|
67
|
+
try {
|
|
68
|
+
await handle.writeFile(JSON.stringify(intent, null, 2), 'utf8');
|
|
69
|
+
await handle.sync();
|
|
70
|
+
} finally {
|
|
71
|
+
await handle.close();
|
|
72
|
+
}
|
|
73
|
+
await fs.rename(temporary, target);
|
|
74
|
+
} catch (error) {
|
|
75
|
+
await fs.rm(temporary, { force: true }).catch(() => {});
|
|
76
|
+
throw error;
|
|
77
|
+
}
|
|
78
|
+
this.intents.set(intent.sourceTaskId, { ...intent });
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async markStarted(sourceTaskId, startedAt) {
|
|
82
|
+
const intent = this.intents.get(sourceTaskId);
|
|
83
|
+
if (!intent) throw contractError('prepared resume intent not found', 'persistence');
|
|
84
|
+
await this.write({ ...intent, status: 'started', startedAt });
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
module.exports = { DurableTaskResumeStore };
|
|
@@ -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
|
+
});
|