neurastream 0.2.31 → 0.2.32
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/package.json +1 -1
- package/src/adapters/index.ts +55 -0
- package/src/adapters/omp/adapter.ts +28 -0
- package/src/adapters/omp/index.ts +2 -5
- package/src/adapters/omp/launch.ts +9 -8
- package/src/adapters/omp/sessions.ts +4 -4
- package/src/adapters/omp/traces.ts +4 -11
- package/src/cli/commands.ts +8 -7
- package/src/cli/dispatch.ts +3 -4
- package/src/cli/metrics.ts +11 -10
- package/src/cli/pages.ts +2 -2
- package/src/cli/runtime.ts +2 -2
- package/src/cli/traces.ts +19 -17
- package/src/cli/ui.ts +2 -2
- package/src/core/adapter.ts +39 -0
- package/src/core/config.ts +3 -0
- package/templates/config.yml +3 -0
package/package.json
CHANGED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// The one door from src/cli to a harness (plan 20261026_0010 P2). Every adapter import in the
|
|
2
|
+
// CLI goes through this file, and tests/host-neutral.test.ts holds that line. Adding a harness
|
|
3
|
+
// is one folder under src/adapters plus one entry in ADAPTERS below — zero core changes
|
|
4
|
+
// (docs/adapter.md, "Adding a harness").
|
|
5
|
+
|
|
6
|
+
import type { DumpRef, LaunchOptions, RelatedTrace, RunSource, TaskResult, TraceRun, TraceSource } from "../core/adapter.ts";
|
|
7
|
+
import type { Config } from "../core/config.ts";
|
|
8
|
+
import type { DumpParts, DumpTokens } from "../core/trace-bloat.ts";
|
|
9
|
+
import type { SpawnBrief } from "../core/trace-briefs.ts";
|
|
10
|
+
import { ompAdapter } from "./omp/adapter.ts";
|
|
11
|
+
|
|
12
|
+
export interface HarnessAdapter {
|
|
13
|
+
name: string;
|
|
14
|
+
/** Session reader: the runs and the main session (docs/adapter.md). */
|
|
15
|
+
runSource(cfg: Config): RunSource;
|
|
16
|
+
/** Completed subagent task-results announced in the newest main session file. */
|
|
17
|
+
completedResults(cfg: Config): TaskResult[];
|
|
18
|
+
/** TraceSource for agent-score, over the config's sessions root. */
|
|
19
|
+
scoreTraces(cfg: Config): TraceSource;
|
|
20
|
+
/** Every run id with a trace under the root, each mapped to its newest file. */
|
|
21
|
+
traceRuns(root: string): TraceRun[];
|
|
22
|
+
traceSource(root: string): TraceSource;
|
|
23
|
+
/** Read only the session banner at the start of a trace; null means it is unreadable. */
|
|
24
|
+
traceStart(file: string): number | null;
|
|
25
|
+
/** Every failed-request dump under the harness log dir, oldest first. */
|
|
26
|
+
dumps(home: string): DumpRef[];
|
|
27
|
+
/** Model requested by a failed request dump, if the JSON contains one. */
|
|
28
|
+
dumpModel(file: string): string | undefined;
|
|
29
|
+
/** What a dumped request's prompt is made of (src/core/trace-bloat.ts). */
|
|
30
|
+
dumpParts(file: string): DumpParts;
|
|
31
|
+
/** The prompt tokens a dump states itself, when it does. */
|
|
32
|
+
dumpPromptTokens(file: string): DumpTokens | null;
|
|
33
|
+
/** The spawn briefs in one session file. */
|
|
34
|
+
spawnBriefsIn(file: string, runId: string): SpawnBrief[];
|
|
35
|
+
/** The subagent runs of one main run. */
|
|
36
|
+
subagentTraces(root: string, mainRunId: string): RelatedTrace[];
|
|
37
|
+
launchMain(args: string[], cfg: Config, opts?: LaunchOptions): Promise<number>;
|
|
38
|
+
/** `neurastream adapter install <name>` — validates its own args. */
|
|
39
|
+
install(args: string[]): number;
|
|
40
|
+
/** `neurastream doctor`. */
|
|
41
|
+
doctor(): number;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const ADAPTERS: Record<string, HarnessAdapter> = {
|
|
45
|
+
[ompAdapter.name]: ompAdapter,
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/** The adapter the config names (`harness:`; without a config, the shipped default). An unknown
|
|
49
|
+
* name throws, so a typo fails loudly instead of silently reading another harness's files. */
|
|
50
|
+
export function getAdapter(cfg?: Pick<Config, "harness">): HarnessAdapter {
|
|
51
|
+
const name = cfg?.harness ?? "omp";
|
|
52
|
+
const adapter = ADAPTERS[name];
|
|
53
|
+
if (!adapter) throw new Error(`unknown harness "${name}"; known: ${Object.keys(ADAPTERS).join(", ")}`);
|
|
54
|
+
return adapter;
|
|
55
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// The shipped adapter's HarnessAdapter implementation: thin wiring over this folder's readers,
|
|
2
|
+
// launcher, installer and doctor. Only src/adapters may name the harness.
|
|
3
|
+
|
|
4
|
+
import type { HarnessAdapter } from "../index.ts";
|
|
5
|
+
import { runDoctor } from "./doctor.ts";
|
|
6
|
+
import { runAdapterInstall } from "./install.ts";
|
|
7
|
+
import { runMainLauncher } from "./launch.ts";
|
|
8
|
+
import { defaultCompletedResults, defaultRunSource } from "./sessions.ts";
|
|
9
|
+
import { dumpModel, dumpParts, dumpPromptTokens, ompDumps, ompScoreTraces, ompTraceRuns, ompTraceSource, ompTraceStart, spawnBriefsIn, subagentTraces } from "./traces.ts";
|
|
10
|
+
|
|
11
|
+
export const ompAdapter: HarnessAdapter = {
|
|
12
|
+
name: "omp",
|
|
13
|
+
runSource: defaultRunSource,
|
|
14
|
+
completedResults: defaultCompletedResults,
|
|
15
|
+
scoreTraces: ompScoreTraces,
|
|
16
|
+
traceRuns: ompTraceRuns,
|
|
17
|
+
traceSource: ompTraceSource,
|
|
18
|
+
traceStart: ompTraceStart,
|
|
19
|
+
dumps: ompDumps,
|
|
20
|
+
dumpModel,
|
|
21
|
+
dumpParts,
|
|
22
|
+
dumpPromptTokens,
|
|
23
|
+
spawnBriefsIn,
|
|
24
|
+
subagentTraces,
|
|
25
|
+
launchMain: runMainLauncher,
|
|
26
|
+
install: runAdapterInstall,
|
|
27
|
+
doctor: runDoctor,
|
|
28
|
+
};
|
|
@@ -35,6 +35,7 @@ import { mainStartText, runDailyCheck } from "../../core/main-start.ts";
|
|
|
35
35
|
import { readBlocks } from "../../core/metrics.ts";
|
|
36
36
|
import { buildHandoff, holdsBlock, notePath, noteRequest, OWNER_AWAY, ownerText, relaunchPath, rolloverState, spawnRefusal, type RolloverState } from "../../core/rollover.ts";
|
|
37
37
|
import { defaultCompletedResults, defaultRunSource, runIdOf } from "./sessions.ts";
|
|
38
|
+
import { adapterLog } from "./launch.ts";
|
|
38
39
|
import { parseTraceFile } from "./traces.ts";
|
|
39
40
|
import { packageJsonPath, packageVersion } from "./package-root.ts";
|
|
40
41
|
import * as q from "../../core/queue/lib.ts";
|
|
@@ -130,11 +131,7 @@ const PUSH_MERGE = /\bgit\b[^\n;|&]*\bpush\b|\b(glab\s+mr|gh\s+pr)\s+merge\b/;
|
|
|
130
131
|
|
|
131
132
|
// The installed package and its version: see ./package-root.ts.
|
|
132
133
|
|
|
133
|
-
export
|
|
134
|
-
try {
|
|
135
|
-
appendFileSync(cfg.paths.guard_log, `${new Date().toISOString()} ${line}\n`);
|
|
136
|
-
} catch {}
|
|
137
|
-
}
|
|
134
|
+
export { adapterLog };
|
|
138
135
|
|
|
139
136
|
/**
|
|
140
137
|
* Real-time incident detectors (plan 20261022_2330 P3) at Main's prompt and after each Main bash
|
|
@@ -4,18 +4,25 @@
|
|
|
4
4
|
// flags (carriedArgs) and the handoff as its message. Plain `omp` never gets the flag, so it never relaunches.
|
|
5
5
|
|
|
6
6
|
import { spawn } from "node:child_process";
|
|
7
|
-
import { mkdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
7
|
+
import { appendFileSync, mkdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
8
8
|
import { basename, dirname, join } from "node:path";
|
|
9
|
+
import type { LaunchOptions } from "../../core/adapter.ts";
|
|
9
10
|
import type { Config } from "../../core/config.ts";
|
|
10
11
|
import { mainStartText } from "../../core/main-start.ts";
|
|
11
12
|
import { applyQueueEnv } from "../../core/queue/env.ts";
|
|
12
13
|
import * as q from "../../core/queue/lib.ts";
|
|
13
14
|
import { buildHandoff, holdsBlock, MAX_NOTE_AGE_MS, OWNER_AWAY, relaunchPath } from "../../core/rollover.ts";
|
|
14
15
|
import { PROXY_BIN, proxyOnPath } from "../../core/secret-redact.ts";
|
|
15
|
-
import { adapterLog } from "./index.ts";
|
|
16
16
|
import { PACKAGE_ROOT, packageJsonPath, packageVersion } from "./package-root.ts";
|
|
17
17
|
import { defaultCompletedResults, defaultRunSource } from "./sessions.ts";
|
|
18
18
|
|
|
19
|
+
/** One line in the guard log: the adapter's own trace of what it did (relaunches, detectors). */
|
|
20
|
+
export function adapterLog(cfg: Config, line: string): void {
|
|
21
|
+
try {
|
|
22
|
+
appendFileSync(cfg.paths.guard_log, `${new Date().toISOString()} ${line}\n`);
|
|
23
|
+
} catch {}
|
|
24
|
+
}
|
|
25
|
+
|
|
19
26
|
/** The harness the launcher runs. Tests pass another path through LaunchOptions. */
|
|
20
27
|
export const OMP_BIN = "omp";
|
|
21
28
|
|
|
@@ -111,12 +118,6 @@ function hasPositional(args: string[]): boolean {
|
|
|
111
118
|
return false;
|
|
112
119
|
}
|
|
113
120
|
|
|
114
|
-
export interface LaunchOptions {
|
|
115
|
-
harness?: string;
|
|
116
|
-
/** The main-start text generator for the handoff (default: src/core/main-start.ts). Tests inject a failing one. */
|
|
117
|
-
mainStart?: typeof mainStartText;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
121
|
interface RelaunchRequest {
|
|
121
122
|
previousSessionFile: string;
|
|
122
123
|
notePath: string;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
|
|
2
2
|
import { basename, join } from "node:path";
|
|
3
3
|
import type { Config } from "../../core/config.ts";
|
|
4
|
-
import type { MainSession, Run, RunMessage, RunSource, RunStatus } from "../../core/adapter.ts";
|
|
4
|
+
import type { MainSession, Run, RunMessage, RunSource, RunStatus, TaskResult } from "../../core/adapter.ts";
|
|
5
5
|
import { nowSec } from "../../core/clock.ts";
|
|
6
6
|
import { readBudgets } from "../../core/guards.ts";
|
|
7
7
|
import { pyDumps } from "../../core/pyjson.ts";
|
|
@@ -312,8 +312,8 @@ export function defaultRunSource(cfg: Config): RunSource {
|
|
|
312
312
|
* wait were invisible here, so D1 never saw them). A line without a readable timestamp is
|
|
313
313
|
* skipped — without one the contradiction grace cannot be aged.
|
|
314
314
|
*/
|
|
315
|
-
export function completedResults(text: string):
|
|
316
|
-
const out:
|
|
315
|
+
export function completedResults(text: string): TaskResult[] {
|
|
316
|
+
const out: TaskResult[] = [];
|
|
317
317
|
for (const line of text.split("\n")) {
|
|
318
318
|
const isAsync = line.includes('"async-result"');
|
|
319
319
|
const isWait = line.includes('"toolName":"wait"');
|
|
@@ -349,7 +349,7 @@ export function completedResults(text: string): { agent: string; ts: number }[]
|
|
|
349
349
|
}
|
|
350
350
|
|
|
351
351
|
/** `completedResults` of the newest Main session file (the one `mainSession()` picks). */
|
|
352
|
-
export function defaultCompletedResults(cfg: Config):
|
|
352
|
+
export function defaultCompletedResults(cfg: Config): TaskResult[] {
|
|
353
353
|
try {
|
|
354
354
|
const s = defaultRunSource(cfg).mainSession();
|
|
355
355
|
return s ? completedResults(readFileSync(s.file, "utf8")) : [];
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
import { closeSync, openSync, readFileSync, readSync, readdirSync, statSync } from "node:fs";
|
|
9
9
|
import { dirname, join } from "node:path";
|
|
10
10
|
import type { Config } from "../../core/config.ts";
|
|
11
|
-
import type { TraceEvent, TraceSource, TraceUsage } from "../../core/adapter.ts";
|
|
11
|
+
import type { DumpRef, RelatedTrace, TraceEvent, TraceRun, TraceSource, TraceUsage } from "../../core/adapter.ts";
|
|
12
12
|
import type { DumpParts, DumpTokens } from "../../core/trace-bloat.ts";
|
|
13
13
|
import type { SpawnBrief } from "../../core/trace-briefs.ts";
|
|
14
14
|
import { asObj, jsonlAt, parseTs, runIdOf } from "./sessions.ts";
|
|
@@ -135,7 +135,7 @@ export function parseTraceFile(file: string, runId: string): TraceEvent[] {
|
|
|
135
135
|
}
|
|
136
136
|
|
|
137
137
|
/** Every run id with a trace under the root, each mapped to its newest file. */
|
|
138
|
-
export function ompTraceRuns(root: string):
|
|
138
|
+
export function ompTraceRuns(root: string): TraceRun[] {
|
|
139
139
|
const newest: Record<string, { file: string; mtimeSec: number }> = {};
|
|
140
140
|
for (const file of [...jsonlAt(root, 1), ...jsonlAt(root, 2)]) {
|
|
141
141
|
let mt: number;
|
|
@@ -208,14 +208,14 @@ export function ompDumpDir(home: string): string {
|
|
|
208
208
|
* Every request dump under the harness log dir, oldest first. Dumps are named
|
|
209
209
|
* `<epoch ms of the failed request>-<request id>.json`; anything else in the dir is not a dump.
|
|
210
210
|
*/
|
|
211
|
-
export function ompDumps(home: string):
|
|
211
|
+
export function ompDumps(home: string): DumpRef[] {
|
|
212
212
|
let names: string[];
|
|
213
213
|
try {
|
|
214
214
|
names = readdirSync(ompDumpDir(home));
|
|
215
215
|
} catch {
|
|
216
216
|
return [];
|
|
217
217
|
}
|
|
218
|
-
const out:
|
|
218
|
+
const out: DumpRef[] = [];
|
|
219
219
|
for (const name of names) {
|
|
220
220
|
const m = /^(\d+)-[A-Za-z0-9]+\.json$/.exec(name);
|
|
221
221
|
if (m) out.push({ file: join(ompDumpDir(home), name), ms: Number(m[1]) });
|
|
@@ -318,13 +318,6 @@ export function ompScoreTraces(cfg: Config): TraceSource {
|
|
|
318
318
|
return ompTraceSource(cfg.adapter?.sessions || join(cfg.paths.home, ".omp", "agent", "sessions"));
|
|
319
319
|
}
|
|
320
320
|
|
|
321
|
-
export interface RelatedTrace {
|
|
322
|
-
/** The subagent run's trace run id — the agent name. */
|
|
323
|
-
name: string;
|
|
324
|
-
mtimeSec: number;
|
|
325
|
-
events: TraceEvent[];
|
|
326
|
-
}
|
|
327
|
-
|
|
328
321
|
/**
|
|
329
322
|
* The subagent runs of one Main run: the depth-2 session files under the Main file's session
|
|
330
323
|
* dir (plan 20261024_0301 P5) — the runs Main spawned there, whose results Main's report cites.
|
package/src/cli/commands.ts
CHANGED
|
@@ -18,9 +18,7 @@ import { wipBackup } from "../core/wip-backup.ts";
|
|
|
18
18
|
import { wipCheck } from "../core/wip-check.ts";
|
|
19
19
|
import { wipSnapshot } from "../core/wip-snapshot.ts";
|
|
20
20
|
import { runtime } from "./runtime.ts";
|
|
21
|
-
import {
|
|
22
|
-
import { defaultCompletedResults, defaultRunSource } from "../adapters/omp/sessions.ts";
|
|
23
|
-
import { ompScoreTraces } from "../adapters/omp/traces.ts";
|
|
21
|
+
import { getAdapter } from "../adapters/index.ts";
|
|
24
22
|
|
|
25
23
|
export const PORTED = [
|
|
26
24
|
"agent-score",
|
|
@@ -45,14 +43,15 @@ export const PORTED = [
|
|
|
45
43
|
] as const;
|
|
46
44
|
|
|
47
45
|
export async function runMain(args: string[]): Promise<number> {
|
|
48
|
-
|
|
46
|
+
const cfg = runtime().cfg;
|
|
47
|
+
return getAdapter(cfg).launchMain(args, cfg);
|
|
49
48
|
}
|
|
50
49
|
|
|
51
50
|
export async function runPorted(cmd: string, args: string[]): Promise<number> {
|
|
52
51
|
const rt = runtime();
|
|
53
52
|
switch (cmd) {
|
|
54
53
|
case "agent-score":
|
|
55
|
-
return agentScore(args, rt.cfg, rt.source,
|
|
54
|
+
return agentScore(args, rt.cfg, rt.source, getAdapter(rt.cfg).scoreTraces(rt.cfg));
|
|
56
55
|
case "agent-ledger":
|
|
57
56
|
return agentLedger(args, rt.cfg, rt.source);
|
|
58
57
|
case "brief-lint":
|
|
@@ -75,8 +74,10 @@ export async function runPorted(cmd: string, args: string[]): Promise<number> {
|
|
|
75
74
|
return queueClose(args, rt.cfg);
|
|
76
75
|
case "queue-cancel":
|
|
77
76
|
return queueCancel(args, rt.cfg);
|
|
78
|
-
case "main-start":
|
|
79
|
-
|
|
77
|
+
case "main-start": {
|
|
78
|
+
const a = getAdapter(rt.cfg);
|
|
79
|
+
return mainStart(args, rt.cfg, { source: a.runSource(rt.cfg), results: a.completedResults(rt.cfg) });
|
|
80
|
+
}
|
|
80
81
|
case "machine-guard":
|
|
81
82
|
return machineGuard(args, rt.cfg);
|
|
82
83
|
case "quota-reset":
|
package/src/cli/dispatch.ts
CHANGED
|
@@ -4,8 +4,7 @@ import { ConfigError, configFile, loadConfig, type Config } from "../core/config
|
|
|
4
4
|
import { postReply } from "../core/notify.ts";
|
|
5
5
|
import { loadKnownSecrets, redactSecrets, redactSummary } from "../core/secret-redact.ts";
|
|
6
6
|
import { applyQueueEnv } from "../core/queue/env.ts";
|
|
7
|
-
import {
|
|
8
|
-
import { runDoctor } from "../adapters/omp/doctor.ts";
|
|
7
|
+
import { getAdapter } from "../adapters/index.ts";
|
|
9
8
|
import { GUARD_USAGE, runGuard } from "./guard.ts";
|
|
10
9
|
import { runInit } from "./init.ts";
|
|
11
10
|
import { PORTED, runMain, runPorted } from "./commands.ts";
|
|
@@ -110,7 +109,7 @@ export async function run(cmd: string, args: string[]): Promise<number> {
|
|
|
110
109
|
if (cmd === "traces") return runTraces(args);
|
|
111
110
|
if (cmd === "ui") return runUi(args);
|
|
112
111
|
if (cmd === "pages") return runPages(args);
|
|
113
|
-
if (cmd === "adapter") return
|
|
112
|
+
if (cmd === "adapter") return getAdapter().install(args);
|
|
114
113
|
if (cmd === "config") {
|
|
115
114
|
try {
|
|
116
115
|
const cfg = loadConfig();
|
|
@@ -123,7 +122,7 @@ export async function run(cmd: string, args: string[]): Promise<number> {
|
|
|
123
122
|
}
|
|
124
123
|
if (cmd === "doctor") {
|
|
125
124
|
for (const warning of pagesDoctor()) console.warn(`warning: ${warning}`);
|
|
126
|
-
return
|
|
125
|
+
return getAdapter().doctor();
|
|
127
126
|
}
|
|
128
127
|
if (cmd === "notify") return runNotify(args);
|
|
129
128
|
if (cmd === "redact") return runRedact(args);
|
package/src/cli/metrics.ts
CHANGED
|
@@ -11,8 +11,7 @@ import type { RunSource } from "../core/adapter.ts";
|
|
|
11
11
|
import { buildReport, type BloatSummary, type ProviderSummary } from "../core/report.ts";
|
|
12
12
|
import { providerFindings } from "../core/trace-provider.ts";
|
|
13
13
|
import { bloatRules } from "../core/trace-bloat.ts";
|
|
14
|
-
import {
|
|
15
|
-
import { ompDumps, ompTraceRuns, ompTraceSource } from "../adapters/omp/traces.ts";
|
|
14
|
+
import { getAdapter } from "../adapters/index.ts";
|
|
16
15
|
import { assignDumps, bloatReport } from "./traces.ts";
|
|
17
16
|
|
|
18
17
|
export const METRICS_USAGE = [
|
|
@@ -97,12 +96,13 @@ export function recordRule(cfg: Config, text: string, decision: "approved" | "re
|
|
|
97
96
|
* rules of src/core/trace-provider.ts under the config's provider rules and price table. */
|
|
98
97
|
function providerSummary(cfg: Config, now: number, days: number): ProviderSummary {
|
|
99
98
|
const root = cfg.adapter?.sessions || join(cfg.paths.home, ".omp", "agent", "sessions");
|
|
99
|
+
const a = getAdapter(cfg);
|
|
100
100
|
const cutoff = now - days * 86400;
|
|
101
|
-
const src =
|
|
101
|
+
const src = a.traceSource(root);
|
|
102
102
|
const rules = cfg.traces.provider;
|
|
103
103
|
const byKind: Record<string, number> = {};
|
|
104
104
|
let runs = 0;
|
|
105
|
-
for (const r of
|
|
105
|
+
for (const r of a.traceRuns(root)) {
|
|
106
106
|
if (r.mtimeSec < cutoff) continue;
|
|
107
107
|
runs++;
|
|
108
108
|
for (const f of providerFindings(src.events(r.runId), rules, rules.prices)) byKind[f.kind] = (byKind[f.kind] ?? 0) + 1;
|
|
@@ -115,20 +115,21 @@ function providerSummary(cfg: Config, now: number, days: number): ProviderSummar
|
|
|
115
115
|
* rejected call flags only through its dump). */
|
|
116
116
|
function bloatSummary(cfg: Config, now: number, days: number): BloatSummary {
|
|
117
117
|
const root = cfg.adapter?.sessions || join(cfg.paths.home, ".omp", "agent", "sessions");
|
|
118
|
+
const a = getAdapter(cfg);
|
|
118
119
|
const cutoff = now - days * 86400;
|
|
119
|
-
const src =
|
|
120
|
+
const src = a.traceSource(root);
|
|
120
121
|
const rules = bloatRules(cfg);
|
|
121
|
-
const dumps =
|
|
122
|
+
const dumps = a.dumps(cfg.paths.home);
|
|
122
123
|
let runs = 0;
|
|
123
124
|
let flagged = 0;
|
|
124
125
|
let largest: BloatSummary["largest"] = null;
|
|
125
|
-
const allRuns =
|
|
126
|
-
const { owners, events: assignmentEvents } = assignDumps(allRuns, src, dumps);
|
|
126
|
+
const allRuns = a.traceRuns(root);
|
|
127
|
+
const { owners, events: assignmentEvents } = assignDumps(allRuns, src, dumps, a);
|
|
127
128
|
for (const r of allRuns) {
|
|
128
129
|
if (r.mtimeSec < cutoff) continue;
|
|
129
130
|
runs++;
|
|
130
131
|
const owned = dumps.filter((dump) => owners.get(dump.file) === r.runId);
|
|
131
|
-
const rep = bloatReport(src, r.runId, rules, cfg.traces.bloat.tool_group, owned, r.mtimeSec, assignmentEvents.get(r.runId));
|
|
132
|
+
const rep = bloatReport(src, r.runId, rules, cfg.traces.bloat.tool_group, owned, r.mtimeSec, a, assignmentEvents.get(r.runId));
|
|
132
133
|
if (rep.findings.length === 0) continue;
|
|
133
134
|
flagged++;
|
|
134
135
|
const firstCall = Math.max(rep.firstPrompt ?? 0, rep.dumpPrompt?.tokens ?? 0);
|
|
@@ -163,7 +164,7 @@ export async function runMetrics(args: string[]): Promise<number> {
|
|
|
163
164
|
const [sub = "", ...rest] = args;
|
|
164
165
|
const now = nowSec();
|
|
165
166
|
if (sub === "ci") return recordCi(cfg, rest[0] ?? "", rest[1] ?? "", now);
|
|
166
|
-
if (sub === "report") return reportCmd(cfg,
|
|
167
|
+
if (sub === "report") return reportCmd(cfg, getAdapter(cfg).runSource(cfg), rest, now);
|
|
167
168
|
if (sub === "false-alarm") return recordFalseAlarm(cfg, rest[0] ?? "", flagValue(rest, "--why") ?? "", now);
|
|
168
169
|
if (sub === "rule") {
|
|
169
170
|
const approved = rest.includes("--approved");
|
package/src/cli/pages.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { readdirSync } from "node:fs";
|
|
2
2
|
import { homedir } from "node:os";
|
|
3
|
-
import {
|
|
3
|
+
import { getAdapter } from "../adapters/index.ts";
|
|
4
4
|
import { loadConfig } from "../core/config.ts";
|
|
5
5
|
import { readExpiry, sweepPages } from "../core/pages-expiry.ts";
|
|
6
6
|
import { startPagesServer } from "../core/pages.ts";
|
|
@@ -19,7 +19,7 @@ export async function runPages(args: string[] = [], deps: PagesDeps = {}): Promi
|
|
|
19
19
|
if (args[0] === "serve" && args.length === 1) {
|
|
20
20
|
if (!cfg.pages.enabled) throw new Error("pages.enabled is false");
|
|
21
21
|
sweepPages(cfg.pages.dir, cfg.pages.keep_days, clock());
|
|
22
|
-
startPagesServer({ cfg, tailscale: deps.tailscale ?? realTailscale, source:
|
|
22
|
+
startPagesServer({ cfg, tailscale: deps.tailscale ?? realTailscale, source: getAdapter(cfg).runSource(cfg) });
|
|
23
23
|
setInterval(() => { try { sweepPages(cfg.pages.dir, cfg.pages.keep_days, clock()); } catch (e) { console.error(e instanceof Error ? e.message : String(e)); } }, HOUR_MS);
|
|
24
24
|
return new Promise<number>(() => {});
|
|
25
25
|
}
|
package/src/cli/runtime.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { loadConfig, type Config } from "../core/config.ts";
|
|
2
2
|
import type { RunSource } from "../core/adapter.ts";
|
|
3
|
-
import {
|
|
3
|
+
import { getAdapter } from "../adapters/index.ts";
|
|
4
4
|
|
|
5
5
|
export interface Runtime {
|
|
6
6
|
cfg: Config;
|
|
@@ -9,5 +9,5 @@ export interface Runtime {
|
|
|
9
9
|
|
|
10
10
|
export function runtime(): Runtime {
|
|
11
11
|
const cfg = loadConfig();
|
|
12
|
-
return { cfg, source:
|
|
12
|
+
return { cfg, source: getAdapter(cfg).runSource(cfg) };
|
|
13
13
|
}
|
package/src/cli/traces.ts
CHANGED
|
@@ -14,7 +14,8 @@
|
|
|
14
14
|
|
|
15
15
|
import { join } from "node:path";
|
|
16
16
|
import { nowSec } from "../core/clock.ts";
|
|
17
|
-
import type { TraceEvent, TraceSource } from "../core/adapter.ts";
|
|
17
|
+
import type { DumpRef, TraceEvent, TraceSource } from "../core/adapter.ts";
|
|
18
|
+
import { getAdapter, type HarnessAdapter } from "../adapters/index.ts";
|
|
18
19
|
import { ConfigError, loadConfig, type Config } from "../core/config.ts";
|
|
19
20
|
import { claimUnbacked } from "../core/incident-detectors.ts";
|
|
20
21
|
import { claimsFromEvents, type RelatedRun } from "../core/trace-claims.ts";
|
|
@@ -22,7 +23,6 @@ import { actionsRules, suspiciousActions, type ActionsRules, type SuspiciousActi
|
|
|
22
23
|
import { bloatBreakdown, bloatFindings, bloatRules, callSizes, dumpFinding, type BloatBreakdown, type BloatRules } from "../core/trace-bloat.ts";
|
|
23
24
|
import { efficiencyRules, runEfficiency, RULE_ORDER, type EfficiencyRules, type WasteRule } from "../core/trace-efficiency.ts";
|
|
24
25
|
import { pyPercent0 } from "../core/pyfmt.ts";
|
|
25
|
-
import { dumpModel, dumpParts, dumpPromptTokens, ompDumps, ompTraceRuns, ompTraceSource, ompTraceStart, spawnBriefsIn, subagentTraces } from "../adapters/omp/traces.ts";
|
|
26
26
|
import { briefReport, briefRows, type SideStat } from "../core/trace-briefs.ts";
|
|
27
27
|
import { readEvents } from "../core/agent-score.ts";
|
|
28
28
|
|
|
@@ -111,16 +111,16 @@ export interface DumpAssignmentRun { runId: string; file: string; mtimeSec: numb
|
|
|
111
111
|
export interface DumpAssignment { owners: Map<string, string>; unmatched: { file: string; ms: number }[]; events: Map<string, TraceEvent[]> }
|
|
112
112
|
|
|
113
113
|
/** Assign each dump once, parsing only traces whose session start and mtime contain it. */
|
|
114
|
-
export function assignDumps(runs: DumpAssignmentRun[], src: TraceSource, dumps:
|
|
114
|
+
export function assignDumps(runs: DumpAssignmentRun[], src: TraceSource, dumps: DumpRef[], a: Pick<HarnessAdapter, "dumpModel" | "traceStart"> = getAdapter()): DumpAssignment {
|
|
115
115
|
const owners = new Map<string, string>();
|
|
116
|
-
const unmatched:
|
|
116
|
+
const unmatched: DumpRef[] = [];
|
|
117
117
|
const cached = new Map<string, ReturnType<TraceSource["events"]>>();
|
|
118
118
|
for (const dump of dumps) {
|
|
119
|
-
const model = dumpModel(dump.file);
|
|
119
|
+
const model = a.dumpModel(dump.file);
|
|
120
120
|
const eligible: { ts: number; runId: string }[] = [];
|
|
121
121
|
for (const run of runs) {
|
|
122
122
|
if (dump.ms > Math.round(run.mtimeSec * 1000)) continue;
|
|
123
|
-
const start =
|
|
123
|
+
const start = a.traceStart(run.file);
|
|
124
124
|
if (start !== null && dump.ms < start * 1000) continue;
|
|
125
125
|
let events = cached.get(run.runId);
|
|
126
126
|
if (!events) {
|
|
@@ -144,7 +144,7 @@ export function assignDumps(runs: DumpAssignmentRun[], src: TraceSource, dumps:
|
|
|
144
144
|
return { owners, unmatched, events: cached };
|
|
145
145
|
}
|
|
146
146
|
|
|
147
|
-
export function bloatReport(src: TraceSource, runId: string, rules: BloatRules, groupRegex: string, dumps:
|
|
147
|
+
export function bloatReport(src: TraceSource, runId: string, rules: BloatRules, groupRegex: string, dumps: DumpRef[], mtimeSec: number, a: Pick<HarnessAdapter, "dumpParts" | "dumpPromptTokens"> = getAdapter(), parsedEvents?: TraceEvent[]): RunBloatReport {
|
|
148
148
|
const events = parsedEvents ?? src.events(runId);
|
|
149
149
|
const sizes = callSizes(events, rules.aggregate_apis);
|
|
150
150
|
const findings = bloatFindings(events, rules);
|
|
@@ -161,8 +161,8 @@ export function bloatReport(src: TraceSource, runId: string, rules: BloatRules,
|
|
|
161
161
|
if (first !== undefined) {
|
|
162
162
|
const dump = dumps.find((d) => d.ms >= first * 1000 && d.ms <= Math.round(mtimeSec * 1000));
|
|
163
163
|
if (dump) {
|
|
164
|
-
breakdown = bloatBreakdown(dumpParts(dump.file), groupRegex);
|
|
165
|
-
const stated = dumpPromptTokens(dump.file);
|
|
164
|
+
breakdown = bloatBreakdown(a.dumpParts(dump.file), groupRegex);
|
|
165
|
+
const stated = a.dumpPromptTokens(dump.file);
|
|
166
166
|
if (stated) {
|
|
167
167
|
dumpPrompt = { tokens: stated.tokens, ...(stated.limit !== undefined ? { limit: stated.limit } : {}) };
|
|
168
168
|
if (findings.length === 0) {
|
|
@@ -208,10 +208,11 @@ function briefsCommand(cfg: Config, root: string, rest: string[]): number {
|
|
|
208
208
|
}
|
|
209
209
|
}
|
|
210
210
|
const cutoff = nowSec() - windowSec;
|
|
211
|
-
const
|
|
211
|
+
const a = getAdapter(cfg);
|
|
212
|
+
const briefs = a.traceRuns(root)
|
|
212
213
|
.filter((r) => r.mtimeSec >= cutoff)
|
|
213
|
-
.flatMap((r) => spawnBriefsIn(r.file, r.runId));
|
|
214
|
-
const rows = briefRows(briefs,
|
|
214
|
+
.flatMap((r) => a.spawnBriefsIn(r.file, r.runId));
|
|
215
|
+
const rows = briefRows(briefs, a.traceSource(root), readEvents(cfg), { brief: cfg.traces.briefs, efficiency: efficiencyRules(cfg) });
|
|
215
216
|
const summary = briefReport(rows, { min_n: cfg.traces.briefs.min_n, char_buckets: cfg.traces.briefs.char_buckets });
|
|
216
217
|
if (json) {
|
|
217
218
|
console.log(JSON.stringify(report ? summary : rows, null, 2));
|
|
@@ -279,15 +280,16 @@ export async function runTraces(args: string[], opts: { home?: string } = {}): P
|
|
|
279
280
|
console.error(TRACES_USAGE);
|
|
280
281
|
return 2;
|
|
281
282
|
}
|
|
282
|
-
const
|
|
283
|
-
const
|
|
283
|
+
const a = getAdapter(cfg);
|
|
284
|
+
const runs = a.traceRuns(root);
|
|
285
|
+
const src = a.traceSource(root);
|
|
284
286
|
const rules: ActionsRules | EfficiencyRules | BloatRules | null = sub === "actions" ? actionsRules(cfg) : sub === "efficiency" ? efficiencyRules(cfg) : sub === "bloat" ? bloatRules(cfg) : null;
|
|
285
287
|
// A Main run's report cites its subagents' results: their traces under the Main session dir
|
|
286
288
|
// back the pure result-line claims (P5). No time filter — the text match is the guard, and
|
|
287
289
|
// a sub's file keeps being written while its Main run lives, inside whatever window.
|
|
288
290
|
const relatedOf = (id: string): RelatedRun[] =>
|
|
289
|
-
sub === "claims" ? subagentTraces(root, id).map(({ name, events }) => ({ name, events })) : [];
|
|
290
|
-
const dumps = sub === "bloat" ?
|
|
291
|
+
sub === "claims" ? a.subagentTraces(root, id).map(({ name, events }) => ({ name, events })) : [];
|
|
292
|
+
const dumps = sub === "bloat" ? a.dumps(cfg.paths.home) : [];
|
|
291
293
|
const assignment = sub === "bloat" ? assignDumps(runs, src, dumps) : { owners: new Map<string, string>(), unmatched: [] as { file: string; ms: number }[], events: new Map<string, TraceEvent[]>() };
|
|
292
294
|
const dumpOwners = assignment.owners;
|
|
293
295
|
const unmatchedDumps = assignment.unmatched;
|
|
@@ -298,7 +300,7 @@ export async function runTraces(args: string[], opts: { home?: string } = {}): P
|
|
|
298
300
|
if (sub === "claims") return claimsReport(src, id, relatedOf(id));
|
|
299
301
|
if (sub === "actions") return actionsReport(src, id, rules as ActionsRules);
|
|
300
302
|
if (sub === "efficiency") return efficiencyReport(src, id, rules as EfficiencyRules);
|
|
301
|
-
return bloatReport(src, id, rules as BloatRules, cfg.traces.bloat.tool_group, assignedDumps(id), mtimeOf(id), assignment.events.get(id));
|
|
303
|
+
return bloatReport(src, id, rules as BloatRules, cfg.traces.bloat.tool_group, assignedDumps(id), mtimeOf(id), a, assignment.events.get(id));
|
|
302
304
|
};
|
|
303
305
|
if (windowSec !== undefined) {
|
|
304
306
|
const cutoff = nowSec() - windowSec;
|
package/src/cli/ui.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// `ui on --login <email> [--port N]` / `ui off` set up and undo that whole setup in one command
|
|
5
5
|
// (the core lives in src/core/ui-setup.ts); plain `neurastream ui` keeps serving as today.
|
|
6
6
|
|
|
7
|
-
import {
|
|
7
|
+
import { getAdapter } from "../adapters/index.ts";
|
|
8
8
|
import { ConfigError, loadConfig, type Config } from "../core/config.ts";
|
|
9
9
|
import { startOwnerUi, type OwnerUiOptions, type OwnerUiServer } from "../core/owner-ui.ts";
|
|
10
10
|
import { applyQueueEnv } from "../core/queue/env.ts";
|
|
@@ -84,7 +84,7 @@ export async function runUi(args: string[] = [], deps: UiDeps = {}): Promise<num
|
|
|
84
84
|
console.error("neurastream ui: ui.owner_login is empty — set the tailscale login that may open the page");
|
|
85
85
|
return 1;
|
|
86
86
|
}
|
|
87
|
-
(deps.start ?? startOwnerUi)({ cfg, tailscale: realTailscale, source:
|
|
87
|
+
(deps.start ?? startOwnerUi)({ cfg, tailscale: realTailscale, source: getAdapter(cfg).runSource(cfg) });
|
|
88
88
|
// src/cli/main.ts exits with the code this returns, which would kill the server: serving
|
|
89
89
|
// never resolves, and the process runs until systemd or a signal stops it.
|
|
90
90
|
return new Promise<number>(() => {});
|
package/src/core/adapter.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// Guard decisions and run data. No harness names. See docs/adapter.md.
|
|
2
2
|
|
|
3
|
+
import type { mainStartText } from "./main-start.ts";
|
|
4
|
+
|
|
3
5
|
export type RunStatus = "running" | "waiting" | "done" | "died";
|
|
4
6
|
|
|
5
7
|
export interface RunMessage {
|
|
@@ -137,3 +139,40 @@ export interface StopEvent {
|
|
|
137
139
|
|
|
138
140
|
export const GUARD_EVENTS = ["pre-tool", "inject", "spawn", "plan-edit", "stop"] as const;
|
|
139
141
|
export type GuardEvent = (typeof GUARD_EVENTS)[number];
|
|
142
|
+
|
|
143
|
+
// Shapes the registry (src/adapters/index.ts) and the CLI share. No harness names — only
|
|
144
|
+
// src/adapters may name one.
|
|
145
|
+
|
|
146
|
+
/** Every run id with a trace under a sessions root, each mapped to its newest file. */
|
|
147
|
+
export interface TraceRun {
|
|
148
|
+
runId: string;
|
|
149
|
+
file: string;
|
|
150
|
+
mtimeSec: number;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** One failed-request dump file: its path plus the epoch-ms stamp in its name. */
|
|
154
|
+
export interface DumpRef {
|
|
155
|
+
file: string;
|
|
156
|
+
ms: number;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** One subagent run's trace under one main run's session dir. */
|
|
160
|
+
export interface RelatedTrace {
|
|
161
|
+
/** The subagent run's trace run id — the agent name. */
|
|
162
|
+
name: string;
|
|
163
|
+
mtimeSec: number;
|
|
164
|
+
events: TraceEvent[];
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** One finished subagent task-result announced in the newest main session file. */
|
|
168
|
+
export interface TaskResult {
|
|
169
|
+
agent: string;
|
|
170
|
+
ts: number;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Options for launching the harness binary; tests inject another path and a failing generator. */
|
|
174
|
+
export interface LaunchOptions {
|
|
175
|
+
harness?: string;
|
|
176
|
+
/** The main-start text generator for the handoff (default: src/core/main-start.ts). Tests inject a failing one. */
|
|
177
|
+
mainStart?: typeof mainStartText;
|
|
178
|
+
}
|
package/src/core/config.ts
CHANGED
|
@@ -17,6 +17,8 @@ export class ConfigError extends Error {
|
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
export interface Config {
|
|
20
|
+
/** Which adapter (src/adapters/index.ts) serves runs, traces, launch, install and doctor. The template ships the only one; a second harness adds its name here (docs/adapter.md). */
|
|
21
|
+
harness: string;
|
|
20
22
|
paths: {
|
|
21
23
|
home: string;
|
|
22
24
|
work_roots: string[];
|
|
@@ -387,6 +389,7 @@ function applyEnv(cfg: Record<string, unknown>, env: NodeJS.ProcessEnv): void {
|
|
|
387
389
|
}
|
|
388
390
|
|
|
389
391
|
function check(cfg: Config): void {
|
|
392
|
+
if (typeof cfg.harness !== "string" || cfg.harness === "") throw new ConfigError("harness must be a non-empty string");
|
|
390
393
|
try {
|
|
391
394
|
const pattern = cfg.quota.reset_pattern;
|
|
392
395
|
new RegExp(pattern);
|
package/templates/config.yml
CHANGED
|
@@ -21,6 +21,9 @@ paths:
|
|
|
21
21
|
alert_state: "~/.cache/resource-alerts"
|
|
22
22
|
snapshot_lock: "~/.cache/tmp-snapshot.lock"
|
|
23
23
|
|
|
24
|
+
# Which adapter (docs/adapter.md) serves runs, traces, launch, install and doctor.
|
|
25
|
+
harness: omp
|
|
26
|
+
|
|
24
27
|
score:
|
|
25
28
|
default_repo: ""
|
|
26
29
|
default_branch: "" # no default: pass --branch (agent-score refuses to guess a branch)
|