@cortexkit/pi-magic-context 0.44.1 → 0.44.3
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/dist/historian-calibration-extension.js +17 -0
- package/dist/{index-mwpk0wn9.js → index-atb1ecv9.js} +14766 -14284
- package/dist/index-n8kg008j.js +245 -0
- package/dist/{index-nm3c0sn8.js → index-tckvezry.js} +191 -39
- package/dist/index.js +1752 -750
- package/dist/{read-session-chunk-ka7n4kah.js → read-session-chunk-z2weg25w.js} +1 -1
- package/dist/subagent-entry.js +18 -2
- package/package.json +8 -4
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
// src/subagent-telemetry.ts
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
|
|
4
|
+
// ../plugin/src/agents/permissions.ts
|
|
5
|
+
function applyDisallowedTools(defaults, disallowed) {
|
|
6
|
+
if (disallowed.includes("*"))
|
|
7
|
+
return [];
|
|
8
|
+
return defaults.filter((t) => !disallowed.includes(t));
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// ../plugin/src/agents/hidden-agent-registrations.ts
|
|
12
|
+
var HIDDEN_AGENT_DESCRIPTION = "Internal Magic Context maintenance agent. Not for general tasks — do not select for user work.";
|
|
13
|
+
function buildHiddenAgentRegistrations(args) {
|
|
14
|
+
const historianAllowedTools = applyDisallowedTools(["read", "aft_outline", "aft_zoom", "aft_search"], args.historianDisallowed);
|
|
15
|
+
return [
|
|
16
|
+
{
|
|
17
|
+
id: "dreamer",
|
|
18
|
+
mode: "primary",
|
|
19
|
+
hidden: true,
|
|
20
|
+
description: HIDDEN_AGENT_DESCRIPTION,
|
|
21
|
+
prompt: args.dreamerPrompt,
|
|
22
|
+
allowedTools: ["ctx_memory"],
|
|
23
|
+
maxSteps: 150,
|
|
24
|
+
overrides: args.dreamerOverrides,
|
|
25
|
+
lockPermissions: true
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
id: "dreamer-docs",
|
|
29
|
+
mode: "primary",
|
|
30
|
+
hidden: true,
|
|
31
|
+
description: HIDDEN_AGENT_DESCRIPTION,
|
|
32
|
+
prompt: args.dreamerPrompt,
|
|
33
|
+
allowedTools: ["read", "grep", "glob", "aft_outline", "aft_zoom", "aft_search"],
|
|
34
|
+
maxSteps: 60,
|
|
35
|
+
overrides: args.dreamerOverrides,
|
|
36
|
+
lockPermissions: true
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
id: "dreamer-reviewer",
|
|
40
|
+
mode: "primary",
|
|
41
|
+
hidden: true,
|
|
42
|
+
description: HIDDEN_AGENT_DESCRIPTION,
|
|
43
|
+
prompt: args.dreamerPrompt,
|
|
44
|
+
allowedTools: [],
|
|
45
|
+
maxSteps: 4,
|
|
46
|
+
overrides: args.dreamerOverrides,
|
|
47
|
+
lockPermissions: true
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
id: "dreamer-retrospective",
|
|
51
|
+
mode: "primary",
|
|
52
|
+
hidden: true,
|
|
53
|
+
description: HIDDEN_AGENT_DESCRIPTION,
|
|
54
|
+
prompt: args.dreamerPrompt,
|
|
55
|
+
allowedTools: ["ctx_search"],
|
|
56
|
+
maxSteps: 40,
|
|
57
|
+
overrides: args.dreamerOverrides,
|
|
58
|
+
lockPermissions: true
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
id: "dreamer-primer-investigator",
|
|
62
|
+
mode: "primary",
|
|
63
|
+
hidden: true,
|
|
64
|
+
description: HIDDEN_AGENT_DESCRIPTION,
|
|
65
|
+
prompt: args.dreamerPrompt,
|
|
66
|
+
allowedTools: [
|
|
67
|
+
"read",
|
|
68
|
+
"grep",
|
|
69
|
+
"glob",
|
|
70
|
+
"aft_outline",
|
|
71
|
+
"aft_zoom",
|
|
72
|
+
"aft_search",
|
|
73
|
+
"ctx_search"
|
|
74
|
+
],
|
|
75
|
+
maxSteps: 40,
|
|
76
|
+
overrides: args.dreamerOverrides,
|
|
77
|
+
lockPermissions: true
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
id: "dreamer-memory-mapper",
|
|
81
|
+
mode: "primary",
|
|
82
|
+
hidden: true,
|
|
83
|
+
description: HIDDEN_AGENT_DESCRIPTION,
|
|
84
|
+
prompt: args.dreamerPrompt,
|
|
85
|
+
allowedTools: ["read", "grep", "glob", "aft_outline", "aft_zoom", "aft_search"],
|
|
86
|
+
maxSteps: 60,
|
|
87
|
+
overrides: args.dreamerOverrides,
|
|
88
|
+
lockPermissions: true
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
id: "dreamer-classifier",
|
|
92
|
+
mode: "primary",
|
|
93
|
+
hidden: true,
|
|
94
|
+
description: HIDDEN_AGENT_DESCRIPTION,
|
|
95
|
+
prompt: args.dreamerPrompt,
|
|
96
|
+
allowedTools: [],
|
|
97
|
+
maxSteps: 4,
|
|
98
|
+
overrides: args.dreamerOverrides,
|
|
99
|
+
lockPermissions: true
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
id: "smart-note-compiler",
|
|
103
|
+
mode: "primary",
|
|
104
|
+
hidden: true,
|
|
105
|
+
description: HIDDEN_AGENT_DESCRIPTION,
|
|
106
|
+
prompt: args.smartNoteCompilerPrompt,
|
|
107
|
+
allowedTools: [],
|
|
108
|
+
maxSteps: 8,
|
|
109
|
+
overrides: args.dreamerOverrides,
|
|
110
|
+
lockPermissions: true
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
id: "historian",
|
|
114
|
+
mode: "primary",
|
|
115
|
+
hidden: true,
|
|
116
|
+
description: HIDDEN_AGENT_DESCRIPTION,
|
|
117
|
+
prompt: args.historianPrompt,
|
|
118
|
+
allowedTools: historianAllowedTools,
|
|
119
|
+
maxSteps: 40,
|
|
120
|
+
overrides: args.historianOverrides
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
id: "historian-recomp",
|
|
124
|
+
mode: "primary",
|
|
125
|
+
hidden: true,
|
|
126
|
+
description: HIDDEN_AGENT_DESCRIPTION,
|
|
127
|
+
prompt: args.historianRecompPrompt ?? args.historianPrompt,
|
|
128
|
+
allowedTools: historianAllowedTools,
|
|
129
|
+
maxSteps: 40,
|
|
130
|
+
overrides: args.historianOverrides
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
id: "historian-editor",
|
|
134
|
+
mode: "primary",
|
|
135
|
+
hidden: true,
|
|
136
|
+
description: HIDDEN_AGENT_DESCRIPTION,
|
|
137
|
+
prompt: args.historianEditorPrompt,
|
|
138
|
+
allowedTools: historianAllowedTools,
|
|
139
|
+
maxSteps: 40,
|
|
140
|
+
overrides: args.historianOverrides
|
|
141
|
+
}
|
|
142
|
+
];
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// src/subagent-telemetry.ts
|
|
146
|
+
var stepCaps = new Map(buildHiddenAgentRegistrations({
|
|
147
|
+
dreamerPrompt: undefined,
|
|
148
|
+
historianPrompt: undefined,
|
|
149
|
+
historianEditorPrompt: undefined,
|
|
150
|
+
historianDisallowed: []
|
|
151
|
+
}).map(({ id, maxSteps }) => [id, maxSteps]));
|
|
152
|
+
function subagentStepCap(agent) {
|
|
153
|
+
return stepCaps.get(agent.replace(/^magic-context-/, ""));
|
|
154
|
+
}
|
|
155
|
+
function promptFingerprint(prompt) {
|
|
156
|
+
return {
|
|
157
|
+
bytes: Buffer.byteLength(prompt, "utf8"),
|
|
158
|
+
sha256: createHash("sha256").update(prompt).digest("hex")
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
function record(value) {
|
|
162
|
+
return value !== null && typeof value === "object" ? value : {};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
class SubagentTelemetry {
|
|
166
|
+
log;
|
|
167
|
+
steps = 0;
|
|
168
|
+
intended;
|
|
169
|
+
effective = null;
|
|
170
|
+
tools = Object.create(null);
|
|
171
|
+
promptTokens = {
|
|
172
|
+
first: null,
|
|
173
|
+
last: null,
|
|
174
|
+
max: null
|
|
175
|
+
};
|
|
176
|
+
warned = false;
|
|
177
|
+
streamedSteps = 0;
|
|
178
|
+
terminalSeen = false;
|
|
179
|
+
results = new Set;
|
|
180
|
+
constructor(prompt, log) {
|
|
181
|
+
this.log = log;
|
|
182
|
+
this.intended = promptFingerprint(prompt);
|
|
183
|
+
}
|
|
184
|
+
observe(value) {
|
|
185
|
+
const event = record(value);
|
|
186
|
+
if (event.type === "mc_system_prompt" && typeof event.bytes === "number" && typeof event.sha256 === "string" && /^[a-f0-9]{64}$/.test(event.sha256)) {
|
|
187
|
+
this.effective = { bytes: event.bytes, sha256: event.sha256 };
|
|
188
|
+
if (!this.warned && event.sha256 !== this.intended.sha256) {
|
|
189
|
+
this.warned = true;
|
|
190
|
+
this.log(`subagent_system_prompt_${event.containsIntended === true ? "extended" : "replaced"} ${JSON.stringify({ intended: this.intended, effective: this.effective, extension: null })}`);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
if (event.type === "message_end") {
|
|
194
|
+
if (record(event.message).role === "assistant") {
|
|
195
|
+
this.streamedSteps++;
|
|
196
|
+
this.assistant(event.message);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
if (event.type === "agent_end" && Array.isArray(event.messages) && !this.terminalSeen) {
|
|
200
|
+
this.terminalSeen = true;
|
|
201
|
+
const assistants = event.messages.filter((message) => record(message).role === "assistant");
|
|
202
|
+
for (const message of assistants.slice(this.streamedSteps))
|
|
203
|
+
this.assistant(message);
|
|
204
|
+
}
|
|
205
|
+
if (event.type === "tool_execution_end" && typeof event.toolCallId === "string" && typeof event.toolName === "string" && !this.results.has(event.toolCallId)) {
|
|
206
|
+
this.results.add(event.toolCallId);
|
|
207
|
+
const tool = this.tool(event.toolName);
|
|
208
|
+
const content = record(event.result).content;
|
|
209
|
+
if (Array.isArray(content))
|
|
210
|
+
for (const part of content) {
|
|
211
|
+
const block = record(part);
|
|
212
|
+
const text = typeof block.text === "string" ? block.text : typeof block.data === "string" ? block.data : "";
|
|
213
|
+
tool.outputBytes += Buffer.byteLength(text, "utf8");
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
tool(name) {
|
|
218
|
+
this.tools[name] ??= { calls: 0, outputBytes: 0 };
|
|
219
|
+
return this.tools[name];
|
|
220
|
+
}
|
|
221
|
+
assistant(value) {
|
|
222
|
+
const message = record(value);
|
|
223
|
+
this.steps++;
|
|
224
|
+
const usage = record(message.usage);
|
|
225
|
+
const fields = [usage.input, usage.cacheRead, usage.cacheWrite];
|
|
226
|
+
const known = fields.some((value) => typeof value === "number" && Number.isFinite(value));
|
|
227
|
+
const tokens = known ? fields.reduce((sum, value) => sum + (typeof value === "number" && Number.isFinite(value) ? value : 0), 0) : null;
|
|
228
|
+
if (this.steps === 1)
|
|
229
|
+
this.promptTokens.first = tokens;
|
|
230
|
+
this.promptTokens.last = tokens;
|
|
231
|
+
if (tokens !== null)
|
|
232
|
+
this.promptTokens.max = Math.max(this.promptTokens.max ?? 0, tokens);
|
|
233
|
+
if (Array.isArray(message.content))
|
|
234
|
+
for (const part of message.content) {
|
|
235
|
+
const call = record(part);
|
|
236
|
+
if (call.type === "toolCall" && typeof call.name === "string")
|
|
237
|
+
this.tool(call.name).calls++;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
finish(agent, outcome, cap) {
|
|
241
|
+
this.log(`subagent_telemetry ${JSON.stringify({ agent, outcome, cap, steps: this.steps, tools: this.tools, promptTokens: this.promptTokens, intended: this.intended, effective: this.effective, provenanceSource: this.effective ? "pi_context_system_prompt" : "unavailable" })}`);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export { subagentStepCap, promptFingerprint, SubagentTelemetry };
|
|
@@ -2234,6 +2234,7 @@ function claimOpenCodeDbDiagnosticOnce(surface, resolution) {
|
|
|
2234
2234
|
}
|
|
2235
2235
|
|
|
2236
2236
|
// ../plugin/src/shared/sqlite.ts
|
|
2237
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2237
2238
|
var reportSlowPrivilegedWrite;
|
|
2238
2239
|
function registerSlowWriteReporter(reporter) {
|
|
2239
2240
|
reportSlowPrivilegedWrite = reporter;
|
|
@@ -2288,6 +2289,65 @@ var sqliteModule = await loadSqliteModule(detectedRuntime);
|
|
|
2288
2289
|
var DatabaseImpl = isBun ? sqliteModule.Database : buildNodeSqliteDatabaseClass(sqliteModule.DatabaseSync);
|
|
2289
2290
|
var trackedSqliteConnections = new Map;
|
|
2290
2291
|
var nextSqliteConnectionSequence = 1;
|
|
2292
|
+
function installTransactionRouting(db, readonly) {
|
|
2293
|
+
const nativeExec = db.exec.bind(db);
|
|
2294
|
+
Object.defineProperty(db, "exec", {
|
|
2295
|
+
configurable: true,
|
|
2296
|
+
writable: true,
|
|
2297
|
+
value: (sql) => {
|
|
2298
|
+
if (/^\s*BEGIN\s+(?:IMMEDIATE|EXCLUSIVE)(?:\s+TRANSACTION)?\s*;?\s*$/i.test(sql)) {
|
|
2299
|
+
if (transformPassScope.getStore()?.active || backgroundWriterScope.getStore())
|
|
2300
|
+
acquireShort(db, () => nativeExec(sql), sql.trim());
|
|
2301
|
+
else
|
|
2302
|
+
nativeExec(sql);
|
|
2303
|
+
return db;
|
|
2304
|
+
}
|
|
2305
|
+
return nativeExec(sql);
|
|
2306
|
+
}
|
|
2307
|
+
});
|
|
2308
|
+
Object.defineProperty(db, "transaction", {
|
|
2309
|
+
configurable: true,
|
|
2310
|
+
writable: true,
|
|
2311
|
+
value: (fn) => {
|
|
2312
|
+
const make = (mode) => function(...args) {
|
|
2313
|
+
const nested = isInTransaction(db);
|
|
2314
|
+
const savepoint = "mc_tx_sp";
|
|
2315
|
+
db.exec(nested ? `SAVEPOINT ${savepoint}` : `BEGIN ${mode}`);
|
|
2316
|
+
try {
|
|
2317
|
+
const result = fn.apply(this, args);
|
|
2318
|
+
db.exec(nested ? `RELEASE ${savepoint}` : "COMMIT");
|
|
2319
|
+
return result;
|
|
2320
|
+
} catch (error) {
|
|
2321
|
+
if (isInTransaction(db)) {
|
|
2322
|
+
if (nested) {
|
|
2323
|
+
db.exec(`ROLLBACK TO ${savepoint}`);
|
|
2324
|
+
db.exec(`RELEASE ${savepoint}`);
|
|
2325
|
+
} else
|
|
2326
|
+
db.exec("ROLLBACK");
|
|
2327
|
+
}
|
|
2328
|
+
throw error;
|
|
2329
|
+
}
|
|
2330
|
+
};
|
|
2331
|
+
const defaultTransaction = make(readonly ? "DEFERRED" : "IMMEDIATE");
|
|
2332
|
+
const variants = {
|
|
2333
|
+
default: defaultTransaction,
|
|
2334
|
+
deferred: make("DEFERRED"),
|
|
2335
|
+
immediate: make("IMMEDIATE"),
|
|
2336
|
+
exclusive: make("EXCLUSIVE"),
|
|
2337
|
+
database: db
|
|
2338
|
+
};
|
|
2339
|
+
for (const transaction of [
|
|
2340
|
+
variants.default,
|
|
2341
|
+
variants.deferred,
|
|
2342
|
+
variants.immediate,
|
|
2343
|
+
variants.exclusive
|
|
2344
|
+
]) {
|
|
2345
|
+
Object.assign(transaction, variants);
|
|
2346
|
+
}
|
|
2347
|
+
return defaultTransaction;
|
|
2348
|
+
}
|
|
2349
|
+
});
|
|
2350
|
+
}
|
|
2291
2351
|
function trackSqliteConnection(db, filename, options) {
|
|
2292
2352
|
const originalClose = db.close.bind(db);
|
|
2293
2353
|
const sequence = nextSqliteConnectionSequence++;
|
|
@@ -2296,6 +2356,7 @@ function trackSqliteConnection(db, filename, options) {
|
|
|
2296
2356
|
filename: typeof filename === "string" ? filename : Buffer.isBuffer(filename) ? "<buffer>" : ":memory:",
|
|
2297
2357
|
readonly: Boolean(options) && typeof options === "object" && (options.readonly === true || options.readOnly === true)
|
|
2298
2358
|
};
|
|
2359
|
+
installTransactionRouting(db, metadata.readonly);
|
|
2299
2360
|
Object.defineProperty(db, "close", {
|
|
2300
2361
|
configurable: true,
|
|
2301
2362
|
value: (...args) => {
|
|
@@ -2319,7 +2380,6 @@ var TrackedDatabase = new Proxy(DatabaseImpl, {
|
|
|
2319
2380
|
}
|
|
2320
2381
|
});
|
|
2321
2382
|
function buildNodeSqliteDatabaseClass(DatabaseSync) {
|
|
2322
|
-
const SAVEPOINT = "mc_tx_sp";
|
|
2323
2383
|
|
|
2324
2384
|
class NodeSqliteDatabase extends DatabaseSync {
|
|
2325
2385
|
constructor(filename, options) {
|
|
@@ -2338,47 +2398,70 @@ function buildNodeSqliteDatabaseClass(DatabaseSync) {
|
|
|
2338
2398
|
}
|
|
2339
2399
|
return stmt;
|
|
2340
2400
|
}
|
|
2341
|
-
transaction(fn) {
|
|
2342
|
-
const self = this;
|
|
2343
|
-
const execute = (mode, receiver, args) => {
|
|
2344
|
-
const nested = self.isTransaction === true;
|
|
2345
|
-
self.exec(nested ? `SAVEPOINT ${SAVEPOINT}` : `BEGIN${mode ? ` ${mode}` : ""}`);
|
|
2346
|
-
try {
|
|
2347
|
-
const result = fn.apply(receiver, args);
|
|
2348
|
-
self.exec(nested ? `RELEASE ${SAVEPOINT}` : "COMMIT");
|
|
2349
|
-
return result;
|
|
2350
|
-
} catch (error) {
|
|
2351
|
-
if (nested) {
|
|
2352
|
-
self.exec(`ROLLBACK TO ${SAVEPOINT}`);
|
|
2353
|
-
self.exec(`RELEASE ${SAVEPOINT}`);
|
|
2354
|
-
} else {
|
|
2355
|
-
self.exec("ROLLBACK");
|
|
2356
|
-
}
|
|
2357
|
-
throw error;
|
|
2358
|
-
}
|
|
2359
|
-
};
|
|
2360
|
-
const wrapped = function(...args) {
|
|
2361
|
-
return execute("", this, args);
|
|
2362
|
-
};
|
|
2363
|
-
wrapped.default = function(...args) {
|
|
2364
|
-
return execute("", this, args);
|
|
2365
|
-
};
|
|
2366
|
-
wrapped.deferred = function(...args) {
|
|
2367
|
-
return execute("DEFERRED", this, args);
|
|
2368
|
-
};
|
|
2369
|
-
wrapped.immediate = function(...args) {
|
|
2370
|
-
return execute("IMMEDIATE", this, args);
|
|
2371
|
-
};
|
|
2372
|
-
wrapped.exclusive = function(...args) {
|
|
2373
|
-
return execute("EXCLUSIVE", this, args);
|
|
2374
|
-
};
|
|
2375
|
-
return wrapped;
|
|
2376
|
-
}
|
|
2377
2401
|
}
|
|
2378
2402
|
return NodeSqliteDatabase;
|
|
2379
2403
|
}
|
|
2380
2404
|
var Database = TrackedDatabase;
|
|
2405
|
+
function pragmaValue(db, name) {
|
|
2406
|
+
const row = db.prepare(`PRAGMA ${name}`).get();
|
|
2407
|
+
if (!row)
|
|
2408
|
+
return;
|
|
2409
|
+
return row[name] ?? Object.values(row)[0];
|
|
2410
|
+
}
|
|
2411
|
+
function pragmaNumber(db, name) {
|
|
2412
|
+
try {
|
|
2413
|
+
const value = pragmaValue(db, name);
|
|
2414
|
+
return typeof value === "number" && Number.isFinite(value) ? value : null;
|
|
2415
|
+
} catch {
|
|
2416
|
+
return null;
|
|
2417
|
+
}
|
|
2418
|
+
}
|
|
2381
2419
|
var privilegeDepth = new WeakMap;
|
|
2420
|
+
var transformPassScope = new AsyncLocalStorage;
|
|
2421
|
+
var admissionScope = new AsyncLocalStorage;
|
|
2422
|
+
var backgroundWriterScope = new AsyncLocalStorage;
|
|
2423
|
+
function withSqliteBackgroundWriter(operation) {
|
|
2424
|
+
return backgroundWriterScope.run(true, operation);
|
|
2425
|
+
}
|
|
2426
|
+
function isTransientSqliteError(error) {
|
|
2427
|
+
if (!error || typeof error !== "object")
|
|
2428
|
+
return false;
|
|
2429
|
+
const value = error;
|
|
2430
|
+
return typeof value.code === "string" && /^(SQLITE_BUSY|SQLITE_LOCKED)(_|$)/.test(value.code) || typeof value.errcode === "number" && [5, 6].includes(value.errcode & 255);
|
|
2431
|
+
}
|
|
2432
|
+
|
|
2433
|
+
class SqliteAcquisitionBusyError extends Error {
|
|
2434
|
+
code = "SQLITE_BUSY";
|
|
2435
|
+
stage;
|
|
2436
|
+
constructor(cause, stage = "BEGIN IMMEDIATE") {
|
|
2437
|
+
super("SQLite writer acquisition remained busy", { cause });
|
|
2438
|
+
this.name = "SqliteAcquisitionBusyError";
|
|
2439
|
+
this.stage = stage;
|
|
2440
|
+
}
|
|
2441
|
+
}
|
|
2442
|
+
var SHORT_BUSY_TIMEOUT_MS = 25;
|
|
2443
|
+
var FOREGROUND_IN_PASS_BUSY_TIMEOUT_MS = 250;
|
|
2444
|
+
function acquireShort(db, acquire, site) {
|
|
2445
|
+
const started = performance.now();
|
|
2446
|
+
const previous = pragmaNumber(db, "busy_timeout");
|
|
2447
|
+
let acquired = false;
|
|
2448
|
+
try {
|
|
2449
|
+
const timeout = transformPassScope.getStore()?.active && !admissionScope.getStore() && !backgroundWriterScope.getStore() ? FOREGROUND_IN_PASS_BUSY_TIMEOUT_MS : SHORT_BUSY_TIMEOUT_MS;
|
|
2450
|
+
db.exec(`PRAGMA busy_timeout=${timeout}`);
|
|
2451
|
+
acquire();
|
|
2452
|
+
acquired = true;
|
|
2453
|
+
} catch (error) {
|
|
2454
|
+
if (transformPassScope.getStore()?.active && isTransientSqliteError(error))
|
|
2455
|
+
throw new SqliteAcquisitionBusyError(error, site);
|
|
2456
|
+
throw error;
|
|
2457
|
+
} finally {
|
|
2458
|
+
if (previous !== null)
|
|
2459
|
+
db.exec(`PRAGMA busy_timeout=${previous}`);
|
|
2460
|
+
const elapsed = performance.now() - started;
|
|
2461
|
+
if (elapsed >= 250)
|
|
2462
|
+
console.warn(`[${new Date().toISOString()}] [magic-context] sqlite acquisition site=${site} lane=${transformPassScope.getStore()?.active ? "foreground" : "background"} elapsed=${Math.round(elapsed)}ms attempts=1 outcome=${acquired ? "acquired" : "busy"}`);
|
|
2463
|
+
}
|
|
2464
|
+
}
|
|
2382
2465
|
function isInTransaction(db) {
|
|
2383
2466
|
const candidate = db;
|
|
2384
2467
|
return candidate.inTransaction === true || candidate.isTransaction === true;
|
|
@@ -2391,7 +2474,13 @@ function withPrivilegedWriter(db, operation) {
|
|
|
2391
2474
|
if (nested) {
|
|
2392
2475
|
db.exec(`SAVEPOINT ${savepoint}`);
|
|
2393
2476
|
} else {
|
|
2394
|
-
|
|
2477
|
+
try {
|
|
2478
|
+
db.exec("BEGIN IMMEDIATE");
|
|
2479
|
+
} catch (error) {
|
|
2480
|
+
if (transformPassScope.getStore()?.active && isTransientSqliteError(error))
|
|
2481
|
+
throw new SqliteAcquisitionBusyError(error);
|
|
2482
|
+
throw error;
|
|
2483
|
+
}
|
|
2395
2484
|
}
|
|
2396
2485
|
privilegeDepth.set(db, previousDepth + 1);
|
|
2397
2486
|
try {
|
|
@@ -3393,6 +3482,69 @@ function isFilePart(part) {
|
|
|
3393
3482
|
return p.type === "file" && typeof p.url === "string";
|
|
3394
3483
|
}
|
|
3395
3484
|
|
|
3485
|
+
// ../plugin/src/features/magic-context/dreamer/token-budget.ts
|
|
3486
|
+
class DreamTokenBudgetExceeded extends Error {
|
|
3487
|
+
sessionId;
|
|
3488
|
+
spent;
|
|
3489
|
+
constructor(sessionId, spent) {
|
|
3490
|
+
super(`Dreamer child ${sessionId} spent ${spent} prompt tokens (token_budget)`);
|
|
3491
|
+
this.sessionId = sessionId;
|
|
3492
|
+
this.spent = spent;
|
|
3493
|
+
this.name = "DreamTokenBudgetExceeded";
|
|
3494
|
+
}
|
|
3495
|
+
}
|
|
3496
|
+
var TOKEN_BUDGET_FINALIZE_MESSAGE = "You're out of token budget. Stop investigating: make no more tool calls and output your result now, covering only what you've already checked; leave the rest out.";
|
|
3497
|
+
var TOKEN_BUDGET_TOOL_REFUSAL = "Out of token budget: no more tool calls. Output your result now.";
|
|
3498
|
+
var finalizingChildren = new Map;
|
|
3499
|
+
function registerBudgetFinalizeChild(sessionId, guard) {
|
|
3500
|
+
finalizingChildren.set(sessionId, guard);
|
|
3501
|
+
}
|
|
3502
|
+
function releaseBudgetFinalizeChild(sessionId) {
|
|
3503
|
+
finalizingChildren.delete(sessionId);
|
|
3504
|
+
}
|
|
3505
|
+
function createDreamTokenBudget(budget) {
|
|
3506
|
+
if (!Number.isSafeInteger(budget) || budget <= 0)
|
|
3507
|
+
throw new Error("Invalid token budget");
|
|
3508
|
+
let spent = 0;
|
|
3509
|
+
let finalizeFired = false;
|
|
3510
|
+
let refusedCalls = 0;
|
|
3511
|
+
let forcedStop = false;
|
|
3512
|
+
const snapshot = () => ({
|
|
3513
|
+
budget,
|
|
3514
|
+
spent,
|
|
3515
|
+
finalizeFired,
|
|
3516
|
+
refusedCalls,
|
|
3517
|
+
hardStopped: forcedStop || spent >= budget || refusedCalls >= 2
|
|
3518
|
+
});
|
|
3519
|
+
return {
|
|
3520
|
+
snapshot,
|
|
3521
|
+
charge(input, cacheRead, cacheWrite, completed = false, canFinalize = true) {
|
|
3522
|
+
for (const value of [input, cacheRead, cacheWrite]) {
|
|
3523
|
+
if (!Number.isSafeInteger(value) || value < 0)
|
|
3524
|
+
throw new Error("Invalid prompt usage");
|
|
3525
|
+
}
|
|
3526
|
+
spent += input + cacheRead + cacheWrite;
|
|
3527
|
+
if (spent >= budget)
|
|
3528
|
+
return "stop";
|
|
3529
|
+
if (!completed && !finalizeFired && spent >= budget * 0.8) {
|
|
3530
|
+
if (!canFinalize) {
|
|
3531
|
+
forcedStop = true;
|
|
3532
|
+
return "stop";
|
|
3533
|
+
}
|
|
3534
|
+
finalizeFired = true;
|
|
3535
|
+
return "finalize";
|
|
3536
|
+
}
|
|
3537
|
+
return "continue";
|
|
3538
|
+
},
|
|
3539
|
+
refuseTool() {
|
|
3540
|
+
if (!finalizeFired)
|
|
3541
|
+
return null;
|
|
3542
|
+
refusedCalls += 1;
|
|
3543
|
+
return { message: TOKEN_BUDGET_TOOL_REFUSAL, hardStopped: snapshot().hardStopped };
|
|
3544
|
+
}
|
|
3545
|
+
};
|
|
3546
|
+
}
|
|
3547
|
+
|
|
3396
3548
|
// ../plugin/src/hooks/magic-context/dropped-input-guard.ts
|
|
3397
3549
|
var LEGACY_TRUNCATED_VALUE = /^[\s\S]{0,5}\.\.\.\[truncated\]$/;
|
|
3398
3550
|
var MAX_QUOTED_ARGUMENTS = 160;
|
|
@@ -4869,4 +5021,4 @@ function readSessionChunk2(sessionId, tokenBudget, offset = 1, eligibleEndOrdina
|
|
|
4869
5021
|
toolResultBoundaries
|
|
4870
5022
|
};
|
|
4871
5023
|
}
|
|
4872
|
-
export { CTX_REDUCE_KEEP, newestCtxReduceTagNumbers, textMentionsRecentCommit, removeSystemReminders, hasMeaningfulUserText2, extractTexts2, extractToolCallSummaries, preloadTokenizer, estimateTokens, normalizeText, stripWellFormedLeadingTagPrefix, stripPersistedAssistantText, byteSize, stripTagPrefix, peelLeadingMcTagNotation, prependTag, isRecord, stableStringify, estimateImageTokensFromDataUrl, TODO_STATUSES, TODO_PRIORITIES, TITLE_DONE_STATUSES, normalizeTodoStateJson, buildSyntheticTodoPart, stripChannel1ReminderSpans, compareMeasuredTailPrefix, formatTailHygienePrefixMismatch, freezeTailHygieneMeasurement, effectiveTailHygiene, CHANNEL1_SENTINEL, CHANNEL1_FLOOR_TOKENS, decideChannel1, evaluateChannel2, reclaimableToolOutputCount, buildChannel2Reminder, buildChannel1Reminder, planEmergencyDrop, measureEmergencyTag, updateTagByteSize, getRecentTagOwnerMessageIds, AGE_RECLAIM_MIN_TOKENS, getOldestActiveUnprotectedToolTags, getActiveToolTagsForAgeReclaim, getTriggerTagTokenUpperBound, updateTagInputByteSize, updateTagTokenCount, getPersistedToolTagAccounting, getAllStatusTagTokenTotalsFlat, updateTagInputTokenCount, tagTokenCountIsNull, backfillTagTokenCounts, insertTag, updateTagStatus, getInertWhitespaceAssistantTags, updateTagDropMode, updateCavemanDepth, hasPiFallbackMessageTags, findAdoptableFallbackTags, hasPiFallbackToolOwnerTags, findPiFallbackToolOwnerTags, adoptPiFallbackToolOwnerTag, adoptPiFallbackMessageTag, getMaxTagNumberBySession, getTagNumberByMessageId, getAssignableTagNumberByMessageId, deriveTagLoadFloor, TAG_SELECT_COLUMNS, getTagsBySession, getActiveTagsBySession, getTagsForPendingOperations, getTagsByNumbers, getDroppedTagsByNumbers, getMaxDroppedTagNumber, getToolTagNumberByOwner, getNullOwnerToolTag, adoptNullOwnerToolTag, retreatPastHostUnservedRows, resolveOpenCodeDbPath, hasV1MessageTables, assertOpenCodeStoreGeneration, openCodeDbPathExists, getOpenCodeDbProbeDescriptions, recordOpenCodeDbReadFailure, clearOpenCodeDbReadFailure, claimOpenCodeDbDiagnosticOnce,
|
|
5024
|
+
export { registerSlowWriteReporter, detectSqliteRuntime, Database, withSqliteBackgroundWriter, withPrivilegedWriter, CTX_REDUCE_KEEP, newestCtxReduceTagNumbers, textMentionsRecentCommit, removeSystemReminders, hasMeaningfulUserText2, extractTexts2, extractToolCallSummaries, preloadTokenizer, estimateTokens, normalizeText, stripWellFormedLeadingTagPrefix, stripPersistedAssistantText, byteSize, stripTagPrefix, peelLeadingMcTagNotation, prependTag, isRecord, stableStringify, estimateImageTokensFromDataUrl, TODO_STATUSES, TODO_PRIORITIES, TITLE_DONE_STATUSES, normalizeTodoStateJson, buildSyntheticTodoPart, stripChannel1ReminderSpans, compareMeasuredTailPrefix, formatTailHygienePrefixMismatch, freezeTailHygieneMeasurement, effectiveTailHygiene, CHANNEL1_SENTINEL, CHANNEL1_FLOOR_TOKENS, decideChannel1, evaluateChannel2, reclaimableToolOutputCount, buildChannel2Reminder, buildChannel1Reminder, planEmergencyDrop, measureEmergencyTag, updateTagByteSize, getRecentTagOwnerMessageIds, AGE_RECLAIM_MIN_TOKENS, getOldestActiveUnprotectedToolTags, getActiveToolTagsForAgeReclaim, getTriggerTagTokenUpperBound, updateTagInputByteSize, updateTagTokenCount, getPersistedToolTagAccounting, getAllStatusTagTokenTotalsFlat, updateTagInputTokenCount, tagTokenCountIsNull, backfillTagTokenCounts, insertTag, updateTagStatus, getInertWhitespaceAssistantTags, updateTagDropMode, updateCavemanDepth, hasPiFallbackMessageTags, findAdoptableFallbackTags, hasPiFallbackToolOwnerTags, findPiFallbackToolOwnerTags, adoptPiFallbackToolOwnerTag, adoptPiFallbackMessageTag, getMaxTagNumberBySession, getTagNumberByMessageId, getAssignableTagNumberByMessageId, deriveTagLoadFloor, TAG_SELECT_COLUMNS, getTagsBySession, getActiveTagsBySession, getTagsForPendingOperations, getTagsByNumbers, getDroppedTagsByNumbers, getMaxDroppedTagNumber, getToolTagNumberByOwner, getNullOwnerToolTag, adoptNullOwnerToolTag, retreatPastHostUnservedRows, resolveOpenCodeDbPath, hasV1MessageTables, assertOpenCodeStoreGeneration, openCodeDbPathExists, getOpenCodeDbProbeDescriptions, recordOpenCodeDbReadFailure, clearOpenCodeDbReadFailure, claimOpenCodeDbDiagnosticOnce, closeQuietly, withReadOnlySessionDb, RAW_SUMMARY_TEXT_MAX_CHARS, completedToolArcCrossesBoundary, estimateTrueRawMessageTokens, buildToolArcs, fenceBoundaryForCompletedToolArcs, fenceBoundaryForToolArcs, buildTrueRawTokenIndex, computeRawRangeFingerprint, invalidateTrueRawTokenCache, DreamTokenBudgetExceeded, TOKEN_BUDGET_FINALIZE_MESSAGE, TOKEN_BUDGET_TOOL_REFUSAL, registerBudgetFinalizeChild, releaseBudgetFinalizeChild, createDreamTokenBudget, droppedInputMarker, createDroppedInputGuard, isEditTool, applyEditMarkerToInput, setDatabase, loadToolDefinitionMeasurements, resolveDecisionCalibration, providerMass, localBudget, calibrationForModelKey, historyLocalBudget, SKELETON_REAL_INPUT_MAX_BYTES, toolInputStringBytes, resolveHostServedBoundaryId2, hasRawMessageProvider2, setRawMessageProvider2, withRawMessageProvider2, cleanUserText2, withRawSessionMessageCache2, readRawSessionMessages2, readRawSessionMessagePage2, getRawSessionMessageOrdinalCount2, visitRawSessionMessages2, readRawSessionMessageRange2, primeTailRawMessageCache2, getCachedAbsoluteMessageCount2, primeInMemoryTailRawMessageCache2, readRawSessionMessageIdOrdinalsForRange2, hasRawSessionMessageById2, readRawSessionMessageOrdinalById2, compareRawSessionMessageOrder2, getRawSessionMessageCount2, getRawSessionTagKeysThrough2, getLegacyProtectedTailStartOrdinal2, readSessionChunk2 };
|