@openai/codex-security 0.1.5 → 0.1.7
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/README.md +89 -8
- package/_bundled_plugin/.codex-plugin/plugin.json +1 -1
- package/_bundled_plugin/.mcp.json +24 -1
- package/_bundled_plugin/mcp/mcp-app.html.br +0 -0
- package/_bundled_plugin/mcp/server.mjs.br.part-000 +0 -0
- package/_bundled_plugin/mcp/server.mjs.br.part-001 +0 -0
- package/_bundled_plugin/preflight/capability-profiles.toml +0 -57
- package/_bundled_plugin/references/config-preflight.md +1 -1
- package/_bundled_plugin/references/final-report.md +15 -7
- package/_bundled_plugin/references/scan-artifacts.md +11 -9
- package/_bundled_plugin/references/scan-contract.md +3 -1
- package/_bundled_plugin/schemas/definitions/artifact-common.schema.json +42 -0
- package/_bundled_plugin/schemas/definitions/discovery-candidate.schema.json +100 -0
- package/_bundled_plugin/schemas/tools/candidate-attack-paths.schema.json +181 -0
- package/_bundled_plugin/schemas/tools/candidate-validations.schema.json +166 -0
- package/_bundled_plugin/schemas/tools/deep-reducer.schema.json +120 -0
- package/_bundled_plugin/schemas/tools/discovery-candidates.schema.json +83 -0
- package/_bundled_plugin/schemas/tools/review-items.schema.json +80 -0
- package/_bundled_plugin/schemas/tools/scan-draft.schema.json +572 -0
- package/_bundled_plugin/schemas/tools/worker-threat-model.schema.json +18 -0
- package/_bundled_plugin/scripts/deep_scan_config.py +11 -3
- package/_bundled_plugin/scripts/deep_scan_workbench.py +153 -133
- package/_bundled_plugin/scripts/finalize_scan_contract.py +21 -0
- package/_bundled_plugin/scripts/generate_in_scope_files.py +134 -0
- package/_bundled_plugin/scripts/workbench_cli.py +33 -1
- package/_bundled_plugin/scripts/workbench_db.py +127 -117
- package/_bundled_plugin/scripts/workbench_feedback.py +2 -0
- package/_bundled_plugin/scripts/workbench_native_indexes.py +5 -49
- package/_bundled_plugin/scripts/workbench_progress.py +86 -3
- package/_bundled_plugin/scripts/workbench_remediation.py +19 -1
- package/_bundled_plugin/scripts/workbench_scan_history.py +7 -1
- package/_bundled_plugin/scripts/workbench_scan_start.py +11 -5
- package/_bundled_plugin/scripts/workbench_scan_usage.py +557 -0
- package/_bundled_plugin/scripts/workbench_schema.py +501 -0
- package/_bundled_plugin/scripts/workbench_validation.py +117 -13
- package/_bundled_plugin/skills/attack-path-analysis/SKILL.md +4 -4
- package/_bundled_plugin/skills/deep-security-scan/SKILL.md +34 -28
- package/_bundled_plugin/skills/finding-discovery/SKILL.md +1 -1
- package/_bundled_plugin/skills/security-diff-scan/SKILL.md +11 -8
- package/_bundled_plugin/skills/security-scan/SKILL.md +16 -9
- package/_bundled_plugin/skills/security-scan/references/repository-wide-scan.md +26 -14
- package/_bundled_plugin/skills/security-scan/references/scan-artifacts-and-ledger.md +1 -1
- package/_bundled_plugin/skills/threat-model/SKILL.md +2 -2
- package/_bundled_plugin/skills/validation/SKILL.md +5 -5
- package/_bundled_plugin/skills/vulnerability-writeup/SKILL.md +145 -227
- package/_bundled_plugin/skills/vulnerability-writeup/references/report-format.md +53 -68
- package/dist/api.d.ts +33 -7
- package/dist/api.d.ts.map +1 -1
- package/dist/api.js +361 -48
- package/dist/api.js.map +1 -1
- package/dist/auth.js +1 -1
- package/dist/auth.js.map +1 -1
- package/dist/cli.d.ts +3 -2
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +477 -40
- package/dist/cli.js.map +1 -1
- package/dist/config.d.ts +29 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +47 -2
- package/dist/config.js.map +1 -1
- package/dist/cost.d.ts +7 -0
- package/dist/cost.d.ts.map +1 -1
- package/dist/cost.js +349 -18
- package/dist/cost.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/multiscan.d.ts +1 -0
- package/dist/multiscan.d.ts.map +1 -1
- package/dist/multiscan.js +3 -0
- package/dist/multiscan.js.map +1 -1
- package/dist/runtime.d.ts +16 -0
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +540 -25
- package/dist/runtime.js.map +1 -1
- package/dist/scan-activity.d.ts +13 -0
- package/dist/scan-activity.d.ts.map +1 -0
- package/dist/scan-activity.js +319 -0
- package/dist/scan-activity.js.map +1 -0
- package/dist/scan-dashboard.d.ts +47 -0
- package/dist/scan-dashboard.d.ts.map +1 -0
- package/dist/scan-dashboard.js +444 -0
- package/dist/scan-dashboard.js.map +1 -0
- package/dist/scan-history-renderer.d.ts.map +1 -1
- package/dist/scan-history-renderer.js +4 -1
- package/dist/scan-history-renderer.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/version.js.map +1 -1
- package/dist/worker-progress.d.ts +7 -0
- package/dist/worker-progress.d.ts.map +1 -1
- package/dist/worker-progress.js +68 -0
- package/dist/worker-progress.js.map +1 -1
- package/package.json +3 -3
package/dist/cost.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { open, readdir } from "node:fs/promises";
|
|
2
2
|
import { join } from "node:path";
|
|
3
|
+
import { scanActivityFromSessionEvent, } from "./scan-activity.js";
|
|
4
|
+
import { scanProgressUpdatesFromEvent, } from "./worker-progress.js";
|
|
3
5
|
const MODEL_PRICING_NANODOLLARS = {
|
|
4
6
|
"gpt-5.6": [5_000, 500, 6_250, 30_000],
|
|
5
7
|
"gpt-5.6-sol": [5_000, 500, 6_250, 30_000],
|
|
6
|
-
"gpt-5.6-terra": [
|
|
7
|
-
"gpt-5.6-luna": [
|
|
8
|
+
"gpt-5.6-terra": [2_000, 200, 2_500, 12_000],
|
|
9
|
+
"gpt-5.6-luna": [200, 20, 250, 1_200],
|
|
8
10
|
};
|
|
9
11
|
const COST_POLL_INTERVAL_MS = 100;
|
|
10
12
|
const SESSION_READ_SIZE = 64 * 1_024;
|
|
@@ -12,23 +14,51 @@ const MAX_SESSION_EVENT_BYTES = 1 * 1_024 * 1_024;
|
|
|
12
14
|
export class ScanCostTracker {
|
|
13
15
|
#options;
|
|
14
16
|
#sessions = new Map();
|
|
17
|
+
#workers = new Map();
|
|
18
|
+
#workerProgress = new Map();
|
|
19
|
+
#reportedProgress = new Set();
|
|
15
20
|
#threadId = null;
|
|
16
21
|
#timer = null;
|
|
17
22
|
#pending = Promise.resolve();
|
|
18
23
|
#snapshot = { usage: null, cost: null };
|
|
19
24
|
#lastCost = null;
|
|
25
|
+
#highestFilesCompleted = 0;
|
|
26
|
+
#expectedFilesTotal;
|
|
20
27
|
constructor(options) {
|
|
21
28
|
this.#options = options;
|
|
29
|
+
this.#expectedFilesTotal = options.expectedFilesTotal;
|
|
30
|
+
}
|
|
31
|
+
setExpectedFilesTotal(filesTotal) {
|
|
32
|
+
this.#expectedFilesTotal = filesTotal;
|
|
22
33
|
}
|
|
23
34
|
start(threadId) {
|
|
24
35
|
if (this.#threadId !== null)
|
|
25
36
|
return;
|
|
26
37
|
this.#threadId = threadId;
|
|
27
|
-
if (this.#options.maxCostUsd === undefined
|
|
38
|
+
if (this.#options.maxCostUsd === undefined &&
|
|
39
|
+
this.#options.onCost === undefined &&
|
|
40
|
+
this.#options.onActivity === undefined &&
|
|
41
|
+
this.#options.onProgress === undefined) {
|
|
28
42
|
return;
|
|
43
|
+
}
|
|
44
|
+
let polling = false;
|
|
45
|
+
let rerun = false;
|
|
29
46
|
const poll = () => {
|
|
30
|
-
|
|
47
|
+
if (polling) {
|
|
48
|
+
rerun = true;
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
polling = true;
|
|
52
|
+
void this.refresh()
|
|
53
|
+
.catch((error) => {
|
|
31
54
|
this.#options.onError?.(error);
|
|
55
|
+
})
|
|
56
|
+
.finally(() => {
|
|
57
|
+
polling = false;
|
|
58
|
+
if (rerun && this.#timer !== null) {
|
|
59
|
+
rerun = false;
|
|
60
|
+
poll();
|
|
61
|
+
}
|
|
32
62
|
});
|
|
33
63
|
};
|
|
34
64
|
this.#timer = setInterval(poll, COST_POLL_INTERVAL_MS);
|
|
@@ -70,12 +100,23 @@ export class ScanCostTracker {
|
|
|
70
100
|
unreadable: false,
|
|
71
101
|
threadId: null,
|
|
72
102
|
parentThreadId: null,
|
|
103
|
+
startedAt: null,
|
|
104
|
+
inheritedUsage: null,
|
|
105
|
+
replaying: false,
|
|
73
106
|
usage: null,
|
|
107
|
+
calls: new Map(),
|
|
108
|
+
activities: [],
|
|
109
|
+
progress: [],
|
|
110
|
+
filesCompleted: 0,
|
|
111
|
+
filesTotal: null,
|
|
112
|
+
prose: new Set(),
|
|
113
|
+
reasoning: null,
|
|
114
|
+
reasoningCount: 0,
|
|
74
115
|
};
|
|
75
116
|
this.#sessions.set(path, session);
|
|
76
117
|
}
|
|
77
118
|
try {
|
|
78
|
-
await readSessionUsage(path, session);
|
|
119
|
+
await readSessionUsage(path, session, this.#options.repository);
|
|
79
120
|
}
|
|
80
121
|
catch (error) {
|
|
81
122
|
if (session.threadId === null)
|
|
@@ -103,10 +144,14 @@ export class ScanCostTracker {
|
|
|
103
144
|
}
|
|
104
145
|
let usage = null;
|
|
105
146
|
for (const session of this.#sessions.values()) {
|
|
106
|
-
if (session.threadId !== null &&
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
147
|
+
if (session.threadId !== null && included.has(session.threadId)) {
|
|
148
|
+
if (session.threadId !== this.#threadId) {
|
|
149
|
+
this.#reportWorkerActivities(session);
|
|
150
|
+
this.#reportWorkerProgress(session);
|
|
151
|
+
}
|
|
152
|
+
if (session.usage !== null) {
|
|
153
|
+
usage = addTokenUsage(usage, session.usage);
|
|
154
|
+
}
|
|
110
155
|
}
|
|
111
156
|
}
|
|
112
157
|
if (usage === null)
|
|
@@ -115,6 +160,55 @@ export class ScanCostTracker {
|
|
|
115
160
|
this.#snapshot = { usage, cost };
|
|
116
161
|
this.#reportCost(cost);
|
|
117
162
|
}
|
|
163
|
+
#reportWorkerActivities(session) {
|
|
164
|
+
if (this.#options.onActivity === undefined || session.threadId === null) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
let worker = this.#workers.get(session.threadId);
|
|
168
|
+
if (worker === undefined) {
|
|
169
|
+
worker = this.#workers.size + 1;
|
|
170
|
+
this.#workers.set(session.threadId, worker);
|
|
171
|
+
}
|
|
172
|
+
for (const activity of session.activities.splice(0)) {
|
|
173
|
+
this.#options.onActivity({
|
|
174
|
+
...activity,
|
|
175
|
+
id: `${session.threadId}:${activity.id}`,
|
|
176
|
+
worker,
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
#reportWorkerProgress(session) {
|
|
181
|
+
if (this.#options.onProgress === undefined || session.threadId === null) {
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
for (const progress of session.progress.splice(0)) {
|
|
185
|
+
const expectedFilesTotal = this.#expectedFilesTotal;
|
|
186
|
+
if ((expectedFilesTotal !== undefined &&
|
|
187
|
+
progress.filesTotal > expectedFilesTotal) ||
|
|
188
|
+
(session.filesTotal !== null &&
|
|
189
|
+
progress.filesTotal !== session.filesTotal) ||
|
|
190
|
+
progress.filesCompleted < session.filesCompleted) {
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
session.filesTotal = progress.filesTotal;
|
|
194
|
+
session.filesCompleted = progress.filesCompleted;
|
|
195
|
+
this.#workerProgress.set(session.threadId, progress.filesCompleted);
|
|
196
|
+
const filesCompleted = Math.min(expectedFilesTotal ?? Number.MAX_SAFE_INTEGER, [...this.#workerProgress.values()].reduce((total, reviewed) => total + reviewed, 0));
|
|
197
|
+
if (filesCompleted < this.#highestFilesCompleted)
|
|
198
|
+
continue;
|
|
199
|
+
const update = {
|
|
200
|
+
...progress,
|
|
201
|
+
filesCompleted,
|
|
202
|
+
filesTotal: expectedFilesTotal ?? Math.max(progress.filesTotal, filesCompleted),
|
|
203
|
+
};
|
|
204
|
+
const key = `${update.phase}:${update.filesCompleted}:${update.filesTotal}`;
|
|
205
|
+
if (this.#reportedProgress.has(key))
|
|
206
|
+
continue;
|
|
207
|
+
this.#reportedProgress.add(key);
|
|
208
|
+
this.#highestFilesCompleted = update.filesCompleted;
|
|
209
|
+
this.#options.onProgress(update);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
118
212
|
#reportCost(cost) {
|
|
119
213
|
if (cost === null || cost.estimatedUsd === this.#lastCost)
|
|
120
214
|
return;
|
|
@@ -142,7 +236,7 @@ async function* sessionFiles(directory) {
|
|
|
142
236
|
}
|
|
143
237
|
}
|
|
144
238
|
}
|
|
145
|
-
async function readSessionUsage(path, session) {
|
|
239
|
+
async function readSessionUsage(path, session, repository) {
|
|
146
240
|
if (session.unreadable)
|
|
147
241
|
return;
|
|
148
242
|
let file;
|
|
@@ -162,7 +256,7 @@ async function readSessionUsage(path, session) {
|
|
|
162
256
|
return;
|
|
163
257
|
session.offset += bytesRead;
|
|
164
258
|
try {
|
|
165
|
-
readSessionChunk(buffer.subarray(0, bytesRead), session);
|
|
259
|
+
readSessionChunk(buffer.subarray(0, bytesRead), session, repository);
|
|
166
260
|
}
|
|
167
261
|
catch (error) {
|
|
168
262
|
session.unreadable = true;
|
|
@@ -176,7 +270,7 @@ async function readSessionUsage(path, session) {
|
|
|
176
270
|
await file.close();
|
|
177
271
|
}
|
|
178
272
|
}
|
|
179
|
-
function readSessionChunk(contents, session) {
|
|
273
|
+
function readSessionChunk(contents, session, repository) {
|
|
180
274
|
let lineStart = 0;
|
|
181
275
|
while (lineStart < contents.length) {
|
|
182
276
|
const newline = contents.indexOf(0x0a, lineStart);
|
|
@@ -194,19 +288,19 @@ function readSessionChunk(contents, session) {
|
|
|
194
288
|
return;
|
|
195
289
|
}
|
|
196
290
|
if (session.pendingLineBytes === 0) {
|
|
197
|
-
readSessionEvent(fragment.toString("utf8"), session);
|
|
291
|
+
readSessionEvent(fragment.toString("utf8"), session, repository);
|
|
198
292
|
}
|
|
199
293
|
else {
|
|
200
294
|
if (fragment.length > 0)
|
|
201
295
|
session.pendingLine.push(Buffer.from(fragment));
|
|
202
|
-
readSessionEvent(Buffer.concat(session.pendingLine, lineBytes).toString("utf8"), session);
|
|
296
|
+
readSessionEvent(Buffer.concat(session.pendingLine, lineBytes).toString("utf8"), session, repository);
|
|
203
297
|
session.pendingLine = [];
|
|
204
298
|
session.pendingLineBytes = 0;
|
|
205
299
|
}
|
|
206
300
|
lineStart = newline + 1;
|
|
207
301
|
}
|
|
208
302
|
}
|
|
209
|
-
function readSessionEvent(line, session) {
|
|
303
|
+
function readSessionEvent(line, session, repository) {
|
|
210
304
|
if (line.length === 0)
|
|
211
305
|
return;
|
|
212
306
|
let event;
|
|
@@ -220,9 +314,16 @@ function readSessionEvent(line, session) {
|
|
|
220
314
|
return;
|
|
221
315
|
const payload = event["payload"];
|
|
222
316
|
if (event["type"] === "session_meta") {
|
|
317
|
+
if (session.threadId !== null) {
|
|
318
|
+
session.replaying = payload["id"] !== session.threadId;
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
223
321
|
if (typeof payload["id"] === "string") {
|
|
224
322
|
session.threadId = payload["id"];
|
|
225
323
|
}
|
|
324
|
+
if (typeof payload["timestamp"] === "string") {
|
|
325
|
+
session.startedAt = Math.floor(Date.parse(payload["timestamp"]) / 1_000);
|
|
326
|
+
}
|
|
226
327
|
const source = payload["source"];
|
|
227
328
|
const subagent = isRecord(source) ? source["subagent"] : undefined;
|
|
228
329
|
const spawn = isRecord(subagent) ? subagent["thread_spawn"] : undefined;
|
|
@@ -232,14 +333,232 @@ function readSessionEvent(line, session) {
|
|
|
232
333
|
session.parentThreadId = parent;
|
|
233
334
|
return;
|
|
234
335
|
}
|
|
336
|
+
if (session.replaying) {
|
|
337
|
+
if (event["type"] !== "event_msg")
|
|
338
|
+
return;
|
|
339
|
+
if (payload["type"] === "token_count" && isRecord(payload["info"])) {
|
|
340
|
+
const usage = tokenUsage(payload["info"]["total_token_usage"]);
|
|
341
|
+
if (usage !== null)
|
|
342
|
+
session.inheritedUsage = usage;
|
|
343
|
+
}
|
|
344
|
+
if (payload["type"] === "task_started" &&
|
|
345
|
+
typeof payload["started_at"] === "number" &&
|
|
346
|
+
session.startedAt !== null &&
|
|
347
|
+
payload["started_at"] >= session.startedAt) {
|
|
348
|
+
session.replaying = false;
|
|
349
|
+
}
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
if (event["type"] === "response_item") {
|
|
353
|
+
session.progress.push(...sessionProgressUpdates(payload));
|
|
354
|
+
if (repository === undefined)
|
|
355
|
+
return;
|
|
356
|
+
if (payload["type"] === "reasoning" &&
|
|
357
|
+
typeof payload["id"] === "string" &&
|
|
358
|
+
Array.isArray(payload["summary"]) &&
|
|
359
|
+
payload["summary"].length > 1 &&
|
|
360
|
+
session.reasoning?.raw !== true) {
|
|
361
|
+
for (const [index, summary] of payload["summary"].entries()) {
|
|
362
|
+
const activity = scanActivityFromSessionEvent({
|
|
363
|
+
...event,
|
|
364
|
+
payload: {
|
|
365
|
+
...payload,
|
|
366
|
+
id: `${payload["id"]}:${index}`,
|
|
367
|
+
summary: [summary],
|
|
368
|
+
},
|
|
369
|
+
}, repository);
|
|
370
|
+
if (activity === null ||
|
|
371
|
+
session.prose.has(`${activity.kind}:${activity.description}`)) {
|
|
372
|
+
continue;
|
|
373
|
+
}
|
|
374
|
+
session.reasoning = {
|
|
375
|
+
id: activity.id,
|
|
376
|
+
text: activity.description,
|
|
377
|
+
raw: false,
|
|
378
|
+
activity: null,
|
|
379
|
+
};
|
|
380
|
+
recordReasoningActivity(session, activity);
|
|
381
|
+
}
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
const activity = scanActivityFromSessionEvent(event, repository);
|
|
385
|
+
if (activity !== null) {
|
|
386
|
+
if (activity.kind === "reasoning") {
|
|
387
|
+
const reasoning = (session.reasoning ??= {
|
|
388
|
+
id: activity.id,
|
|
389
|
+
text: activity.description,
|
|
390
|
+
raw: false,
|
|
391
|
+
activity: null,
|
|
392
|
+
});
|
|
393
|
+
recordReasoningActivity(session, {
|
|
394
|
+
...activity,
|
|
395
|
+
id: reasoning.id,
|
|
396
|
+
description: reasoning.raw && reasoning.activity !== null
|
|
397
|
+
? reasoning.activity.description
|
|
398
|
+
: activity.description,
|
|
399
|
+
});
|
|
400
|
+
return;
|
|
401
|
+
}
|
|
402
|
+
session.reasoning = null;
|
|
403
|
+
if (activity.kind === "message" &&
|
|
404
|
+
session.prose.has(`${activity.kind}:${activity.description}`)) {
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
if (activity.kind === "message") {
|
|
408
|
+
session.prose.add(`${activity.kind}:${activity.description}`);
|
|
409
|
+
}
|
|
410
|
+
if (activity.status === "running") {
|
|
411
|
+
session.calls.set(activity.id, activity);
|
|
412
|
+
}
|
|
413
|
+
session.activities.push(activity);
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
if ((payload["type"] === "function_call_output" ||
|
|
417
|
+
payload["type"] === "custom_tool_call_output") &&
|
|
418
|
+
typeof payload["call_id"] === "string") {
|
|
419
|
+
const call = session.calls.get(payload["call_id"]);
|
|
420
|
+
if (call !== undefined) {
|
|
421
|
+
session.activities.push({
|
|
422
|
+
...call,
|
|
423
|
+
status: payload["status"] === "failed" ? "failed" : "completed",
|
|
424
|
+
});
|
|
425
|
+
session.calls.delete(call.id);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
if (event["type"] === "event_msg" &&
|
|
431
|
+
(payload["type"] === "agent_reasoning" ||
|
|
432
|
+
payload["type"] === "agent_reasoning_delta" ||
|
|
433
|
+
payload["type"] === "agent_reasoning_raw_content" ||
|
|
434
|
+
payload["type"] === "agent_reasoning_raw_content_delta" ||
|
|
435
|
+
payload["type"] === "agent_message")) {
|
|
436
|
+
if (payload["type"] === "agent_message" &&
|
|
437
|
+
typeof payload["message"] === "string") {
|
|
438
|
+
session.progress.push(...scanProgressUpdatesFromEvent({
|
|
439
|
+
type: "item.completed",
|
|
440
|
+
item: { type: "agent_message", text: payload["message"] },
|
|
441
|
+
}));
|
|
442
|
+
}
|
|
443
|
+
if (repository === undefined)
|
|
444
|
+
return;
|
|
445
|
+
if (payload["type"] !== "agent_message") {
|
|
446
|
+
readSessionReasoning(event, payload, session, repository);
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
session.reasoning = null;
|
|
450
|
+
const activity = scanActivityFromSessionEvent(event, repository);
|
|
451
|
+
if (activity !== null &&
|
|
452
|
+
!session.prose.has(`${activity.kind}:${activity.description}`)) {
|
|
453
|
+
session.prose.add(`${activity.kind}:${activity.description}`);
|
|
454
|
+
session.activities.push(activity);
|
|
455
|
+
}
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
235
458
|
if (event["type"] !== "event_msg" ||
|
|
236
459
|
payload["type"] !== "token_count" ||
|
|
237
460
|
!isRecord(payload["info"])) {
|
|
238
461
|
return;
|
|
239
462
|
}
|
|
240
463
|
const usage = tokenUsage(payload["info"]["total_token_usage"]);
|
|
241
|
-
if (usage
|
|
242
|
-
|
|
464
|
+
if (usage === null)
|
|
465
|
+
return;
|
|
466
|
+
const ownUsage = session.inheritedUsage === null
|
|
467
|
+
? usage
|
|
468
|
+
: subtractTokenUsage(usage, session.inheritedUsage);
|
|
469
|
+
if (ownUsage !== null)
|
|
470
|
+
session.usage = ownUsage;
|
|
471
|
+
}
|
|
472
|
+
function readSessionReasoning(event, payload, session, repository) {
|
|
473
|
+
const type = payload["type"];
|
|
474
|
+
const raw = type === "agent_reasoning_raw_content" ||
|
|
475
|
+
type === "agent_reasoning_raw_content_delta";
|
|
476
|
+
const delta = type === "agent_reasoning_delta" ||
|
|
477
|
+
type === "agent_reasoning_raw_content_delta";
|
|
478
|
+
const text = payload[delta ? "delta" : "text"];
|
|
479
|
+
if (typeof text !== "string")
|
|
480
|
+
return;
|
|
481
|
+
if (!delta &&
|
|
482
|
+
!raw &&
|
|
483
|
+
session.reasoning?.raw !== true &&
|
|
484
|
+
session.reasoning?.activity?.status === "completed" &&
|
|
485
|
+
session.reasoning.text !== text) {
|
|
486
|
+
session.reasoning = null;
|
|
487
|
+
}
|
|
488
|
+
const reasoning = (session.reasoning ??= {
|
|
489
|
+
id: `reasoning-${++session.reasoningCount}`,
|
|
490
|
+
text: "",
|
|
491
|
+
raw: false,
|
|
492
|
+
activity: null,
|
|
493
|
+
});
|
|
494
|
+
if (reasoning.raw && !raw)
|
|
495
|
+
return;
|
|
496
|
+
if (raw && !reasoning.raw) {
|
|
497
|
+
reasoning.text = "";
|
|
498
|
+
reasoning.raw = true;
|
|
499
|
+
}
|
|
500
|
+
reasoning.text = delta ? `${reasoning.text}${text}` : text;
|
|
501
|
+
const activity = scanActivityFromSessionEvent({
|
|
502
|
+
...event,
|
|
503
|
+
payload: {
|
|
504
|
+
...payload,
|
|
505
|
+
[delta ? "delta" : "text"]: reasoning.text,
|
|
506
|
+
},
|
|
507
|
+
}, repository);
|
|
508
|
+
if (activity === null)
|
|
509
|
+
return;
|
|
510
|
+
recordReasoningActivity(session, { ...activity, id: reasoning.id });
|
|
511
|
+
}
|
|
512
|
+
function recordReasoningActivity(session, activity) {
|
|
513
|
+
const reasoning = session.reasoning;
|
|
514
|
+
if (reasoning.activity?.description === activity.description &&
|
|
515
|
+
reasoning.activity.status === activity.status) {
|
|
516
|
+
return;
|
|
517
|
+
}
|
|
518
|
+
reasoning.activity = activity;
|
|
519
|
+
session.prose.add(`${activity.kind}:${activity.description}`);
|
|
520
|
+
session.activities.push(activity);
|
|
521
|
+
}
|
|
522
|
+
function sessionProgressUpdates(payload) {
|
|
523
|
+
if (payload["type"] === "message" && payload["role"] === "assistant") {
|
|
524
|
+
const content = payload["content"];
|
|
525
|
+
if (!Array.isArray(content))
|
|
526
|
+
return [];
|
|
527
|
+
return scanProgressUpdatesFromEvent({
|
|
528
|
+
type: "item.completed",
|
|
529
|
+
item: {
|
|
530
|
+
type: "agent_message",
|
|
531
|
+
text: sessionContentText(content, false),
|
|
532
|
+
},
|
|
533
|
+
});
|
|
534
|
+
}
|
|
535
|
+
if (payload["type"] !== "function_call_output" &&
|
|
536
|
+
payload["type"] !== "custom_tool_call_output" &&
|
|
537
|
+
payload["type"] !== "local_shell_call_output") {
|
|
538
|
+
return [];
|
|
539
|
+
}
|
|
540
|
+
const value = payload["output"];
|
|
541
|
+
const output = typeof value === "string"
|
|
542
|
+
? value
|
|
543
|
+
: Array.isArray(value)
|
|
544
|
+
? sessionContentText(value, true)
|
|
545
|
+
: null;
|
|
546
|
+
if (payload["status"] === "failed" || output === null) {
|
|
547
|
+
return [];
|
|
548
|
+
}
|
|
549
|
+
return scanProgressUpdatesFromEvent({
|
|
550
|
+
type: "item.completed",
|
|
551
|
+
item: { type: "command_execution", aggregated_output: output },
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
function sessionContentText(content, includeInputText) {
|
|
555
|
+
return content
|
|
556
|
+
.filter((item) => isRecord(item) &&
|
|
557
|
+
(item["type"] === "output_text" ||
|
|
558
|
+
(includeInputText && item["type"] === "input_text")) &&
|
|
559
|
+
typeof item["text"] === "string")
|
|
560
|
+
.map((item) => item.text)
|
|
561
|
+
.join("\n");
|
|
243
562
|
}
|
|
244
563
|
function tokenUsage(value) {
|
|
245
564
|
if (!isRecord(value))
|
|
@@ -279,6 +598,15 @@ function addTokenUsage(previous, next) {
|
|
|
279
598
|
total_tokens: previous.total_tokens + next.total_tokens,
|
|
280
599
|
};
|
|
281
600
|
}
|
|
601
|
+
function subtractTokenUsage(usage, inherited) {
|
|
602
|
+
return tokenUsage({
|
|
603
|
+
input_tokens: usage.input_tokens - inherited.input_tokens,
|
|
604
|
+
cached_input_tokens: usage.cached_input_tokens - inherited.cached_input_tokens,
|
|
605
|
+
cache_write_input_tokens: usage.cache_write_input_tokens - inherited.cache_write_input_tokens,
|
|
606
|
+
output_tokens: usage.output_tokens - inherited.output_tokens,
|
|
607
|
+
reasoning_output_tokens: usage.reasoning_output_tokens - inherited.reasoning_output_tokens,
|
|
608
|
+
});
|
|
609
|
+
}
|
|
282
610
|
function isRecord(value) {
|
|
283
611
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
284
612
|
}
|
|
@@ -288,7 +616,10 @@ function isMissingFile(error) {
|
|
|
288
616
|
export function estimateScanCost(model, usage) {
|
|
289
617
|
if (model === undefined)
|
|
290
618
|
return null;
|
|
291
|
-
const
|
|
619
|
+
const pricingModel = model.startsWith("openai.")
|
|
620
|
+
? model.slice("openai.".length)
|
|
621
|
+
: model;
|
|
622
|
+
const pricing = MODEL_PRICING_NANODOLLARS[pricingModel];
|
|
292
623
|
const normalized = tokenUsage(usage);
|
|
293
624
|
if (pricing === undefined || normalized === null)
|
|
294
625
|
return null;
|