neurastream 0.3.18 → 0.3.21
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 +88 -490
- package/package.json +1 -1
- package/src/adapters/index.ts +3 -2
- package/src/adapters/omp/adapter.ts +2 -1
- package/src/adapters/omp/auto-close.ts +64 -0
- package/src/adapters/omp/chat-commands.ts +161 -0
- package/src/adapters/omp/inbox.ts +221 -0
- package/src/adapters/omp/index.ts +138 -1050
- package/src/adapters/omp/launch.ts +35 -9
- package/src/adapters/omp/rate-limit-controller.ts +196 -0
- package/src/adapters/omp/rollover-controller.ts +334 -0
- package/src/adapters/omp/types.ts +143 -0
- package/src/cli/claim.ts +1 -3
- package/src/cli/commands.ts +33 -2
- package/src/cli/dispatch.ts +55 -30
- package/src/cli/guard.ts +0 -3
- package/src/cli/incident.ts +2 -2
- package/src/cli/metrics.ts +1 -1
- package/src/cli/recall.ts +6 -8
- package/src/cli/results-build.ts +2 -4
- package/src/cli/workspace.ts +13 -5
- package/src/core/adapter.ts +16 -0
- package/src/core/autonomy.ts +24 -21
- package/src/core/blockers.ts +326 -30
- package/src/core/file-lock.ts +71 -12
- package/src/core/guards/plan-edit.ts +338 -0
- package/src/core/guards/planner.ts +70 -0
- package/src/core/guards/review.ts +391 -0
- package/src/core/guards/spawn.ts +699 -0
- package/src/core/guards/state.ts +581 -0
- package/src/core/guards/stop.ts +235 -0
- package/src/core/guards/tool.ts +541 -0
- package/src/core/guards.ts +12 -2661
- package/src/core/incidents.ts +134 -99
- package/src/core/machine-guard.ts +23 -5
- package/src/core/main-start.ts +11 -9
- package/src/core/mr-sweep.ts +4 -2
- package/src/core/notify.ts +15 -15
- package/src/core/owner-log.ts +19 -17
- package/src/core/owner-topics.ts +38 -48
- package/src/core/owner-ui/auth.ts +164 -0
- package/src/core/owner-ui/data.ts +437 -0
- package/src/core/owner-ui/handlers.ts +250 -0
- package/src/core/owner-ui/messages.ts +209 -0
- package/src/core/owner-ui/render.ts +714 -0
- package/src/core/owner-ui/results.ts +266 -0
- package/src/core/owner-ui/server.ts +142 -0
- package/src/core/owner-ui.ts +11 -2118
- package/src/core/page-notes.ts +25 -0
- package/src/core/pages.ts +452 -78
- package/src/core/plan-approval.ts +24 -26
- package/src/core/queue/lib.ts +170 -137
- package/src/core/queue-cancel.ts +29 -15
- package/src/core/queue-check.ts +5 -7
- package/src/core/queue-close.ts +54 -29
- package/src/core/queue-finished.ts +9 -10
- package/src/core/queue-migrate.ts +1 -3
- package/src/core/queue-trim.ts +2 -4
- package/src/core/results.ts +0 -2
- package/src/core/rollover.ts +2 -3
- package/src/core/secret-redact.ts +65 -14
- package/src/core/tailscale.ts +79 -3
- package/src/core/team-client.ts +18 -6
- package/src/core/uninstall.ts +2 -0
- package/src/core/watch.ts +3 -2
- package/src/core/queue/env.ts +0 -14
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
// Shared omp extension host types and runtime-checked payload accessors.
|
|
2
|
+
|
|
3
|
+
import type { Decision } from "../../core/adapter.ts";
|
|
4
|
+
|
|
5
|
+
/** Subset of the harness extension host the adapter needs. */
|
|
6
|
+
export interface ExtensionHost {
|
|
7
|
+
on(event: string, cb: (...args: unknown[]) => unknown): void;
|
|
8
|
+
registerCommand(name: string, opts: { description?: string; handler: (args: unknown, ctx: unknown) => unknown }): void;
|
|
9
|
+
sendUserMessage(content: string | ({ type: "text"; text: string } | { type: "image"; data: string; mimeType: string })[]): void;
|
|
10
|
+
sendMessage(message: { customType: string; content: string; display: boolean }, options: { triggerTurn: boolean; deliverAs: "nextTurn" }): void;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** The parts of a harness call context the handlers look at. */
|
|
14
|
+
export interface HostCtx {
|
|
15
|
+
hasAgent: boolean;
|
|
16
|
+
kind?: string;
|
|
17
|
+
id?: string;
|
|
18
|
+
cwd?: string;
|
|
19
|
+
setInterval?: (cb: () => void, ms: number) => unknown;
|
|
20
|
+
spawnLimitProbe?: () => void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** The last assistant message of an omp event's `messages` array, as the adapter needs it: its
|
|
24
|
+
* joined text-block text and the shape that tells a real hand-back from a mid-run continuation
|
|
25
|
+
* (plan 20261025_0130). omp sends the reply in `session_stop.last_assistant_message` (falling
|
|
26
|
+
* back to the last assistant entry of `messages`) and in `agent_end.messages`; reading the
|
|
27
|
+
* session file instead lags a whole turn because session_stop fires BEFORE agent_end. */
|
|
28
|
+
export interface EventReply {
|
|
29
|
+
text: string;
|
|
30
|
+
stopReason: string | null;
|
|
31
|
+
toolCalls: number;
|
|
32
|
+
/** The provider's error text on a turn that ended on one (`429 … retry-after-ms=…`); null otherwise. */
|
|
33
|
+
errorMessage: string | null;
|
|
34
|
+
/** The provider and model fields of the assistant message, when the event carries them; the provider-out recording keys on the provider. */
|
|
35
|
+
provider: string | null;
|
|
36
|
+
model: string | null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface RolloverCtx {
|
|
40
|
+
sessionId: string;
|
|
41
|
+
sessionFile: string;
|
|
42
|
+
runningJobs(): string[];
|
|
43
|
+
waitForIdle(): unknown;
|
|
44
|
+
newSession(opts: { parentSession: string }): unknown;
|
|
45
|
+
notify(message: string): void;
|
|
46
|
+
draft(): string;
|
|
47
|
+
shutdown(): void;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function asRecord(v: unknown): Record<string, unknown> {
|
|
51
|
+
return v !== null && typeof v === "object" && !Array.isArray(v) ? (v as Record<string, unknown>) : {};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function asString(v: unknown, fallback = ""): string {
|
|
55
|
+
return typeof v === "string" ? v : fallback;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Call a method on a harness object by name; undefined when it is not a function. */
|
|
59
|
+
export function call(obj: Record<string, unknown>, key: string, args: unknown[] = []): unknown {
|
|
60
|
+
const fn = obj[key];
|
|
61
|
+
return typeof fn === "function" ? Reflect.apply(fn, obj, args) : undefined;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function lastAssistantIn(messages: unknown): EventReply | null {
|
|
65
|
+
const arr = Array.isArray(messages) ? messages : [];
|
|
66
|
+
for (let i = arr.length - 1; i >= 0; i--) {
|
|
67
|
+
const m = asRecord(arr[i]);
|
|
68
|
+
if (m.role !== "assistant") continue;
|
|
69
|
+
const content = Array.isArray(m.content) ? m.content : [];
|
|
70
|
+
const text = content
|
|
71
|
+
.filter(c => asRecord(c).type === "text")
|
|
72
|
+
.map(c => asString(asRecord(c).text))
|
|
73
|
+
.join("\n")
|
|
74
|
+
.trim();
|
|
75
|
+
return {
|
|
76
|
+
text,
|
|
77
|
+
stopReason: typeof m.stopReason === "string" ? m.stopReason : null,
|
|
78
|
+
toolCalls: content.filter(c => asRecord(c).type === "toolCall").length,
|
|
79
|
+
errorMessage: typeof m.errorMessage === "string" ? m.errorMessage : null,
|
|
80
|
+
provider: typeof m.provider === "string" ? m.provider : null,
|
|
81
|
+
model: typeof m.model === "string" ? m.model : null,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function asCtx(v: unknown): HostCtx {
|
|
88
|
+
const r = asRecord(v);
|
|
89
|
+
// The agent envelope only ever carries kind/id strings (original extensions read just those);
|
|
90
|
+
// cast fixes the envelope, every field below is checked before use.
|
|
91
|
+
const agent = r.agent as { kind?: unknown; id?: unknown } | null | undefined;
|
|
92
|
+
const out: HostCtx = { hasAgent: agent !== null && agent !== undefined };
|
|
93
|
+
if (agent) {
|
|
94
|
+
if (typeof agent.kind === "string") out.kind = agent.kind;
|
|
95
|
+
if (typeof agent.id === "string") out.id = agent.id;
|
|
96
|
+
}
|
|
97
|
+
if (typeof r.cwd === "string") out.cwd = r.cwd;
|
|
98
|
+
// Harness-injected timer with the original main-guard signature (ctx.setInterval).
|
|
99
|
+
const interval = r.setInterval as ((cb: () => void, ms: number) => unknown) | undefined;
|
|
100
|
+
if (typeof interval === "function") out.setInterval = interval;
|
|
101
|
+
// Harness-injected probe spawner (tests); the default starts the niced probe child.
|
|
102
|
+
const probe = r.spawnLimitProbe as (() => void) | undefined;
|
|
103
|
+
if (typeof probe === "function") out.spawnLimitProbe = probe;
|
|
104
|
+
return out;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function asRolloverCtx(v: unknown): RolloverCtx | null {
|
|
108
|
+
const r = asRecord(v);
|
|
109
|
+
const sm = asRecord(r.sessionManager);
|
|
110
|
+
const sessionId = call(sm, "getSessionId");
|
|
111
|
+
const sessionFile = call(sm, "getSessionFile");
|
|
112
|
+
if (typeof sessionId !== "string" || typeof sessionFile !== "string") return null;
|
|
113
|
+
return {
|
|
114
|
+
sessionId,
|
|
115
|
+
sessionFile,
|
|
116
|
+
runningJobs: () => {
|
|
117
|
+
const running = asRecord(call(r, "getAsyncJobSnapshot")).running;
|
|
118
|
+
return Array.isArray(running) ? running.map(j => asString(asRecord(j).label, "job")) : [];
|
|
119
|
+
},
|
|
120
|
+
waitForIdle: () => call(r, "waitForIdle"),
|
|
121
|
+
newSession: opts => call(r, "newSession", [opts]),
|
|
122
|
+
notify: message => call(asRecord(r.ui), "notify", [message]),
|
|
123
|
+
draft: () => {
|
|
124
|
+
const text = call(asRecord(r.ui), "getEditorText");
|
|
125
|
+
return typeof text === "string" ? text : "";
|
|
126
|
+
},
|
|
127
|
+
shutdown: () => {
|
|
128
|
+
call(r, "shutdown");
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function isMain(ctx: unknown): boolean {
|
|
134
|
+
const c = asCtx(ctx);
|
|
135
|
+
return !c.hasAgent || c.kind === "main";
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Map a core decision onto the extension return shapes. */
|
|
139
|
+
export function toExtensionResult(d: Decision): Record<string, unknown> {
|
|
140
|
+
if (!d.allow) return { block: true, reason: d.reason, decision: "block" };
|
|
141
|
+
if (d.inject) return { additionalContext: d.inject };
|
|
142
|
+
return {};
|
|
143
|
+
}
|
package/src/cli/claim.ts
CHANGED
|
@@ -9,7 +9,6 @@ import path from "node:path";
|
|
|
9
9
|
import { fileClaimStore, heldByOther, type Claim, type ClaimStore } from "../core/claims.ts";
|
|
10
10
|
import { nowSec, strftime } from "../core/clock.ts";
|
|
11
11
|
import { mainId } from "../core/guards.ts";
|
|
12
|
-
import { applyQueueEnv } from "../core/queue/env.ts";
|
|
13
12
|
import { activePlans, openWaits } from "../core/queue/lib.ts";
|
|
14
13
|
import { claimStoreFor, remoteStoreFor, type TeamEntry } from "../core/team-client.ts";
|
|
15
14
|
import type { Config } from "../core/config.ts";
|
|
@@ -21,13 +20,12 @@ const USAGE = [
|
|
|
21
20
|
].join("\n");
|
|
22
21
|
|
|
23
22
|
export function claimCli(args: string[], cfg: Config): number {
|
|
24
|
-
applyQueueEnv(cfg);
|
|
25
23
|
const me = mainId();
|
|
26
24
|
|
|
27
25
|
if (args[0] === "next") {
|
|
28
26
|
// Index order IS priority; the skips are queueSummary's flags minus plans another live
|
|
29
27
|
// Main holds. A claim that races away mid-loop just moves the pick to the next plan.
|
|
30
|
-
const plans = activePlans();
|
|
28
|
+
const plans = activePlans(cfg.paths);
|
|
31
29
|
const rank = new Map(plans.map((p, i) => [p.file, i + 1] as const));
|
|
32
30
|
for (const p of plans) {
|
|
33
31
|
if (p.status !== "open" || p.waiting || openWaits(p, plans, rank).length) continue;
|
package/src/cli/commands.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
1
4
|
import { agentLedger } from "../core/agent-ledger.ts";
|
|
2
5
|
import { agentScore } from "../core/agent-score.ts";
|
|
3
6
|
import { briefLint } from "../core/brief-lint.ts";
|
|
@@ -48,6 +51,34 @@ export const PORTED = [
|
|
|
48
51
|
"heavy-run",
|
|
49
52
|
] as const;
|
|
50
53
|
|
|
54
|
+
const CLI_MAIN = join(dirname(fileURLToPath(import.meta.url)), "main.ts");
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Runs the CLI `doctor` command asynchronously and returns its combined stdout and stderr.
|
|
58
|
+
*
|
|
59
|
+
* Returns:
|
|
60
|
+
* Promise<string>: Output produced by `neurastream doctor`, including any spawn error message
|
|
61
|
+
*/
|
|
62
|
+
function runCliDoctor(): Promise<string> {
|
|
63
|
+
const { promise, resolve } = Promise.withResolvers<string>();
|
|
64
|
+
const child = spawn(CLI_MAIN, ["doctor"], { stdio: ["ignore", "pipe", "pipe"] });
|
|
65
|
+
let stdout = "";
|
|
66
|
+
let stderr = "";
|
|
67
|
+
child.stdout?.setEncoding("utf8").on("data", (chunk: string) => {
|
|
68
|
+
stdout += chunk;
|
|
69
|
+
});
|
|
70
|
+
child.stderr?.setEncoding("utf8").on("data", (chunk: string) => {
|
|
71
|
+
stderr += chunk;
|
|
72
|
+
});
|
|
73
|
+
child.on("error", err => {
|
|
74
|
+
const text = `${stdout}${stderr}`;
|
|
75
|
+
const body = text.endsWith("\n") || text === "" ? text : `${text}\n`;
|
|
76
|
+
resolve(`${body}${err.message}\n`);
|
|
77
|
+
});
|
|
78
|
+
child.on("close", () => resolve(`${stdout}${stderr}`));
|
|
79
|
+
return promise;
|
|
80
|
+
}
|
|
81
|
+
|
|
51
82
|
export async function runMain(args: string[]): Promise<number> {
|
|
52
83
|
const cfg = runtime().cfg;
|
|
53
84
|
return getAdapter(cfg).launchMain(args, cfg);
|
|
@@ -87,10 +118,10 @@ export async function runPorted(cmd: string, args: string[]): Promise<number> {
|
|
|
87
118
|
return claimCli(args, rt.cfg);
|
|
88
119
|
case "main-start": {
|
|
89
120
|
const a = getAdapter(rt.cfg);
|
|
90
|
-
return mainStart(args, rt.cfg, { source: a.runSource(rt.cfg), results: a.completedResults(rt.cfg), providers: a.providers(rt.cfg), episodesFor: cutoff => a.episodes(rt.cfg, cutoff) });
|
|
121
|
+
return mainStart(args, rt.cfg, { source: a.runSource(rt.cfg), results: a.completedResults(rt.cfg), providers: a.providers(rt.cfg), episodesFor: cutoff => a.episodes(rt.cfg, cutoff), doctor: runCliDoctor });
|
|
91
122
|
}
|
|
92
123
|
case "machine-guard":
|
|
93
|
-
return machineGuard(args, rt.cfg);
|
|
124
|
+
return machineGuard(args, rt.cfg, { heavyMain: CLI_MAIN, heavyStatus: process.execPath });
|
|
94
125
|
case "quota-reset":
|
|
95
126
|
return quotaReset(args, rt.cfg);
|
|
96
127
|
case "priority-watch":
|
package/src/cli/dispatch.ts
CHANGED
|
@@ -12,7 +12,6 @@ import { nowSec } from "../core/clock.ts";
|
|
|
12
12
|
import { postReply } from "../core/notify.ts";
|
|
13
13
|
import { log } from "../core/guards.ts";
|
|
14
14
|
import { loadKnownSecrets, redactSecrets, redactSummary } from "../core/secret-redact.ts";
|
|
15
|
-
import { applyQueueEnv } from "../core/queue/env.ts";
|
|
16
15
|
import { getAdapter } from "../adapters/index.ts";
|
|
17
16
|
import { GUARD_USAGE, runGuard } from "./guard.ts";
|
|
18
17
|
import { runInit } from "./init.ts";
|
|
@@ -32,7 +31,7 @@ import { watchOn, watchOff } from "./watch-setup.ts";
|
|
|
32
31
|
import { runWip } from "./wip.ts";
|
|
33
32
|
import { runUninstall } from "../core/uninstall.ts";
|
|
34
33
|
import { queueMigrate } from "../core/queue-migrate.ts";
|
|
35
|
-
import { activePlans, allowedAgents, enforceReadOnlyIndex, gitTop,
|
|
34
|
+
import { activePlans, allowedAgents, enforceReadOnlyIndex, gitTop, parseIndex, updateIndex } from "../core/queue/lib.ts";
|
|
36
35
|
import { defaultRunner } from "../core/mr-sweep.ts";
|
|
37
36
|
import { allowAgents, denyAgents, invalidSlugs } from "../core/subagents.ts";
|
|
38
37
|
import { runLabels } from "./labels.ts";
|
|
@@ -206,9 +205,10 @@ export async function run(cmd: string, args: string[]): Promise<number> {
|
|
|
206
205
|
if (cmd === "doctor") {
|
|
207
206
|
for (const warning of pagesDoctor()) console.warn(`warning: ${warning}`);
|
|
208
207
|
try {
|
|
209
|
-
const
|
|
210
|
-
|
|
211
|
-
console.
|
|
208
|
+
const cfg = loadConfig();
|
|
209
|
+
const was = enforceReadOnlyIndex(cfg.paths.queue_index);
|
|
210
|
+
if (was) console.warn(`warning: ${cfg.paths.queue_index} — was ${was}, set 0444 (the plan index is read-only on disk)`);
|
|
211
|
+
console.log(watchDoctorLine(watchHealth(cfg, Date.now())));
|
|
212
212
|
} catch {} // a broken config is doctor's own report; the adapter doctor names it
|
|
213
213
|
return getAdapter().doctor();
|
|
214
214
|
}
|
|
@@ -266,9 +266,9 @@ export async function run(cmd: string, args: string[]): Promise<number> {
|
|
|
266
266
|
);
|
|
267
267
|
},
|
|
268
268
|
runner: defaultRunner,
|
|
269
|
-
plans: () => activePlans(),
|
|
269
|
+
plans: () => activePlans(cfg.paths),
|
|
270
270
|
// Plans whose newest spawn (staffing.json) is inside the watch.ci_red_hours window: a timer run has no live job list to ask.
|
|
271
|
-
staffed: now => timerStaffedPlans(now, cfg.watch.ci_red_hours),
|
|
271
|
+
staffed: now => timerStaffedPlans(now, cfg.watch.ci_red_hours, cfg.paths),
|
|
272
272
|
});
|
|
273
273
|
console.log(`watch: ${report.opened.length} opened, ${report.deduped.length} deduped, ${report.closed.length} recovered, ${report.detectors.length} detector findings`);
|
|
274
274
|
return 0;
|
|
@@ -290,13 +290,14 @@ export async function run(cmd: string, args: string[]): Promise<number> {
|
|
|
290
290
|
}
|
|
291
291
|
}
|
|
292
292
|
if (cmd === "queue" && (args[0] === "add" || args[0] === "move")) {
|
|
293
|
+
let cfg: Config;
|
|
293
294
|
try {
|
|
294
|
-
|
|
295
|
+
cfg = loadConfig();
|
|
295
296
|
} catch (err) {
|
|
296
297
|
console.error(err instanceof ConfigError ? err.message : String(err));
|
|
297
298
|
return 1;
|
|
298
299
|
}
|
|
299
|
-
return args[0] === "add" ? runQueueAdd(args.slice(1)) : runQueueMove(args.slice(1));
|
|
300
|
+
return args[0] === "add" ? runQueueAdd(args.slice(1), cfg) : runQueueMove(args.slice(1), cfg);
|
|
300
301
|
}
|
|
301
302
|
if (cmd === "guard") {
|
|
302
303
|
return runGuard(args[0] ?? "");
|
|
@@ -304,7 +305,7 @@ export async function run(cmd: string, args: string[]): Promise<number> {
|
|
|
304
305
|
if (cmd === "agents") return runAgents(args);
|
|
305
306
|
if ((PORTED as readonly string[]).includes(cmd)) {
|
|
306
307
|
try {
|
|
307
|
-
|
|
308
|
+
loadConfig();
|
|
308
309
|
} catch (err) {
|
|
309
310
|
console.error(err instanceof ConfigError ? err.message : String(err));
|
|
310
311
|
return 1;
|
|
@@ -322,7 +323,7 @@ const QUEUE_EDIT_USAGE =
|
|
|
322
323
|
// The index file is 0444 and guard-protected (plan 20261031_0911 P2), so these two commands are the
|
|
323
324
|
// only way an agent adds a plan or reorders it. Both write through writeIndex: the set of plans can
|
|
324
325
|
// only grow, and the file stays read-only on disk after every write.
|
|
325
|
-
function runQueueAdd(args: string[]): number {
|
|
326
|
+
function runQueueAdd(args: string[], cfg: Config): number {
|
|
326
327
|
const ri = args.indexOf("--rank");
|
|
327
328
|
const rest = args.filter((a, i) => a !== "--rank" && args[i - 1] !== "--rank");
|
|
328
329
|
const rankArg = ri >= 0 ? args[ri + 1] : undefined;
|
|
@@ -337,19 +338,32 @@ function runQueueAdd(args: string[]): number {
|
|
|
337
338
|
console.error(`queue add: ${abs} does not exist`);
|
|
338
339
|
return 1;
|
|
339
340
|
}
|
|
340
|
-
const
|
|
341
|
-
|
|
342
|
-
|
|
341
|
+
const index = cfg.paths.queue_index;
|
|
342
|
+
let existingRank = -1;
|
|
343
|
+
let insertedAt = -1;
|
|
344
|
+
let total = 0;
|
|
345
|
+
const wrote = updateIndex(index, undefined, (currentText) => {
|
|
346
|
+
const lines = parseIndex(currentText);
|
|
347
|
+
const found = lines.indexOf(abs);
|
|
348
|
+
if (found !== -1) {
|
|
349
|
+
existingRank = found + 1;
|
|
350
|
+
return null;
|
|
351
|
+
}
|
|
352
|
+
const at = rank === null ? lines.length : Math.min(rank - 1, lines.length);
|
|
353
|
+
lines.splice(at, 0, abs);
|
|
354
|
+
insertedAt = at + 1;
|
|
355
|
+
total = lines.length;
|
|
356
|
+
return lines;
|
|
357
|
+
});
|
|
358
|
+
if (!wrote) {
|
|
359
|
+
console.error(`queue add: ${abs} is already listed at rank ${existingRank} (queue move reorders it)`);
|
|
343
360
|
return 1;
|
|
344
361
|
}
|
|
345
|
-
|
|
346
|
-
lines.splice(at, 0, abs);
|
|
347
|
-
writeIndex(lines);
|
|
348
|
-
console.log(`queue add: ${abs} at rank ${at + 1} of ${lines.length} in ${indexPath()}`);
|
|
362
|
+
console.log(`queue add: ${abs} at rank ${insertedAt} of ${total} in ${index}`);
|
|
349
363
|
return 0;
|
|
350
364
|
}
|
|
351
365
|
|
|
352
|
-
function runQueueMove(args: string[]): number {
|
|
366
|
+
function runQueueMove(args: string[], cfg: Config): number {
|
|
353
367
|
const [fileArg, rankArg] = args;
|
|
354
368
|
const n = Number(rankArg);
|
|
355
369
|
const rank = rankArg !== undefined && rankArg !== "" && Number.isInteger(n) && n >= 1 ? n : null;
|
|
@@ -358,17 +372,27 @@ function runQueueMove(args: string[]): number {
|
|
|
358
372
|
return 2;
|
|
359
373
|
}
|
|
360
374
|
const abs = path.resolve(fileArg!);
|
|
361
|
-
const
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
375
|
+
const index = cfg.paths.queue_index;
|
|
376
|
+
let fromRank = -1;
|
|
377
|
+
let toRank = -1;
|
|
378
|
+
let total = 0;
|
|
379
|
+
const wrote = updateIndex(index, undefined, (currentText) => {
|
|
380
|
+
const lines = parseIndex(currentText);
|
|
381
|
+
const from = lines.indexOf(abs);
|
|
382
|
+
if (from === -1) return null;
|
|
383
|
+
lines.splice(from, 1);
|
|
384
|
+
const to = Math.min(rank - 1, lines.length);
|
|
385
|
+
lines.splice(to, 0, abs);
|
|
386
|
+
fromRank = from + 1;
|
|
387
|
+
toRank = to + 1;
|
|
388
|
+
total = lines.length;
|
|
389
|
+
return lines;
|
|
390
|
+
});
|
|
391
|
+
if (!wrote) {
|
|
392
|
+
console.error(`queue move: ${abs} is not in ${index}`);
|
|
365
393
|
return 1;
|
|
366
394
|
}
|
|
367
|
-
|
|
368
|
-
const to = Math.min(rank - 1, lines.length);
|
|
369
|
-
lines.splice(to, 0, abs);
|
|
370
|
-
writeIndex(lines);
|
|
371
|
-
console.log(`queue move: ${abs} rank ${from + 1} → ${to + 1} of ${lines.length} in ${indexPath()}`);
|
|
395
|
+
console.log(`queue move: ${abs} rank ${fromRank} → ${toRank} of ${total} in ${index}`);
|
|
372
396
|
return 0;
|
|
373
397
|
}
|
|
374
398
|
|
|
@@ -442,13 +466,14 @@ function runAgents(args: string[]): number {
|
|
|
442
466
|
console.error(`invalid slug: ${bad.join(", ")} (allowed: a-z, 0-9, -)`);
|
|
443
467
|
return 2;
|
|
444
468
|
}
|
|
469
|
+
let cfg: Config;
|
|
445
470
|
try {
|
|
446
|
-
|
|
471
|
+
cfg = loadConfig();
|
|
447
472
|
} catch (err) {
|
|
448
473
|
console.error(err instanceof ConfigError ? err.message : String(err));
|
|
449
474
|
return 1;
|
|
450
475
|
}
|
|
451
|
-
const file =
|
|
476
|
+
const file = cfg.paths.subagents;
|
|
452
477
|
if (action === "list") {
|
|
453
478
|
for (const slug of allowedAgents(file)) console.log(slug);
|
|
454
479
|
console.log(`file: ${file}`);
|
package/src/cli/guard.ts
CHANGED
|
@@ -6,7 +6,6 @@ import type { Decision, InjectEvent, PlanEditEvent, SpawnEvent, StopEvent, ToolE
|
|
|
6
6
|
import { plannerPin, verifyPlannerMode } from "../core/autonomy.ts";
|
|
7
7
|
import { ConfigError, loadConfig } from "../core/config.ts";
|
|
8
8
|
import { checkPlanEdit, checkSpawn, checkStop, checkTool, checkInject } from "../core/guards.ts";
|
|
9
|
-
import { applyQueueEnv } from "../core/queue/env.ts";
|
|
10
9
|
import type { Config } from "../core/config.ts";
|
|
11
10
|
|
|
12
11
|
export const GUARD_EVENTS = ["pre-tool", "prompt", "inject", "spawn", "plan-edit", "stop"] as const;
|
|
@@ -105,14 +104,12 @@ export async function runGuard(event: string): Promise<number> {
|
|
|
105
104
|
let cfg: Config;
|
|
106
105
|
try {
|
|
107
106
|
cfg = loadConfig();
|
|
108
|
-
applyQueueEnv(cfg);
|
|
109
107
|
// P2 d: hold the mode facts to the owner's baseline before any decision reads them; a
|
|
110
108
|
// reverted config is re-read so this guard sees the restored values.
|
|
111
109
|
const held = verifyPlannerMode(plannerPin(cfg));
|
|
112
110
|
if (held.reverted) {
|
|
113
111
|
if (held.hint !== null) console.error(held.hint);
|
|
114
112
|
cfg = loadConfig();
|
|
115
|
-
applyQueueEnv(cfg);
|
|
116
113
|
}
|
|
117
114
|
} catch (err) {
|
|
118
115
|
console.error(err instanceof ConfigError ? err.message : String(err));
|
package/src/cli/incident.ts
CHANGED
|
@@ -108,10 +108,10 @@ export function proposeIncidentCmd(cfg: Config, id: string, why: string | undefi
|
|
|
108
108
|
return 0;
|
|
109
109
|
}
|
|
110
110
|
|
|
111
|
-
export async function runIncident(args: string[]): Promise<number> {
|
|
111
|
+
export async function runIncident(args: string[], injected?: Config): Promise<number> {
|
|
112
112
|
let cfg: Config;
|
|
113
113
|
try {
|
|
114
|
-
cfg = loadConfig();
|
|
114
|
+
cfg = injected ?? loadConfig();
|
|
115
115
|
} catch (err) {
|
|
116
116
|
console.error(err instanceof ConfigError ? err.message : String(err));
|
|
117
117
|
return 1;
|
package/src/cli/metrics.ts
CHANGED
|
@@ -51,7 +51,7 @@ export function recordCi(cfg: Config, repoDir: string, mr: string, now: number,
|
|
|
51
51
|
console.log(`ci: ${mr} needed ${r.tries} pipeline(s)`);
|
|
52
52
|
for (const s of r.skipped) console.error(s);
|
|
53
53
|
// A new CI record can change what a hold check sees, so every hold is re-checked now, not on its TTL.
|
|
54
|
-
const plans = activePlans(cfg.paths
|
|
54
|
+
const plans = activePlans(cfg.paths);
|
|
55
55
|
for (const line of staleHoldLines(recheckHolds(cfg, plans, run, now, true), plans)) console.log(line);
|
|
56
56
|
return 0;
|
|
57
57
|
}
|
package/src/cli/recall.ts
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
// `neurastream recall`: searches the owner's messages and Main's answers (the recall log, see
|
|
2
2
|
// core/owner-log.ts) and shows an entry with the entry it pairs with. Read-only.
|
|
3
3
|
|
|
4
|
-
import { getAdapter
|
|
4
|
+
import { getAdapter } from "../adapters/index.ts";
|
|
5
5
|
import { nowSec } from "../core/clock.ts";
|
|
6
6
|
import { ConfigError, loadConfig, type Config } from "../core/config.ts";
|
|
7
7
|
import { helpDoc, printText } from "../core/help.ts";
|
|
8
8
|
import { backfillOwnerLog, type LogEntry, searchEntries, showEntries } from "../core/owner-log.ts";
|
|
9
9
|
import { rangeEntries, runTopics, searchTopics, type Segment, type TopicsRun } from "../core/owner-topics.ts";
|
|
10
|
-
import { applyQueueEnv } from "../core/queue/env.ts";
|
|
11
10
|
|
|
12
11
|
const USAGE = "usage: neurastream recall <words>... [--since 7d|12h|YYYY-MM-DD] [--limit N] [--json]\n neurastream recall --show <id> [--json]";
|
|
13
12
|
const BACKFILL_USAGE = "usage: neurastream recall backfill [--since 7d|12h|YYYY-MM-DD]";
|
|
@@ -117,7 +116,8 @@ async function topicsCli(args: string[], cfg: Config): Promise<number> {
|
|
|
117
116
|
return 2;
|
|
118
117
|
}
|
|
119
118
|
try {
|
|
120
|
-
const
|
|
119
|
+
const adapter = getAdapter(cfg);
|
|
120
|
+
const run: TopicsRun = await runTopics(cfg, req => adapter.complete(req), { close: args.includes("--close") });
|
|
121
121
|
for (const line of run.lines) console.log(line);
|
|
122
122
|
return run.failures === 0 ? 0 : 1;
|
|
123
123
|
} catch {
|
|
@@ -132,8 +132,6 @@ export function recallCli(args: string[], cfg: Config): number | Promise<number>
|
|
|
132
132
|
printText(helpDoc("recall"));
|
|
133
133
|
return 0;
|
|
134
134
|
}
|
|
135
|
-
// The log paths follow the queue state directory, so the config's queue env is applied first.
|
|
136
|
-
applyQueueEnv(cfg);
|
|
137
135
|
if (args[0] === "backfill") return backfillCli(args.slice(1), cfg);
|
|
138
136
|
if (args[0] === "topics") return topicsCli(args.slice(1), cfg);
|
|
139
137
|
const parsed = parseArgs(args);
|
|
@@ -143,7 +141,7 @@ export function recallCli(args: string[], cfg: Config): number | Promise<number>
|
|
|
143
141
|
}
|
|
144
142
|
if (parsed.show !== undefined) {
|
|
145
143
|
const parts = parsed.show.split("..");
|
|
146
|
-
const pair = parts.length === 2 ? rangeEntries(parts[0]!, parts[1]
|
|
144
|
+
const pair = parts.length === 2 ? rangeEntries(parts[0]!, parts[1]!, cfg.paths) : showEntries(parsed.show, cfg.paths);
|
|
147
145
|
if (pair === undefined || pair.length === 0) {
|
|
148
146
|
console.error(`no entry with id ${parsed.show}`);
|
|
149
147
|
return 2;
|
|
@@ -157,11 +155,11 @@ export function recallCli(args: string[], cfg: Config): number | Promise<number>
|
|
|
157
155
|
console.error(USAGE);
|
|
158
156
|
return 2;
|
|
159
157
|
}
|
|
160
|
-
const topics = searchTopics(parsed.words)
|
|
158
|
+
const topics = searchTopics(parsed.words, cfg.paths)
|
|
161
159
|
.filter((s) => parsed.sinceSec === undefined || s.at >= parsed.sinceSec)
|
|
162
160
|
.slice(0, parsed.limit);
|
|
163
161
|
const left = parsed.limit - topics.length;
|
|
164
|
-
const hits = left > 0 ? searchEntries({ words: parsed.words, sinceSec: parsed.sinceSec, limit: left }) : [];
|
|
162
|
+
const hits = left > 0 ? searchEntries({ words: parsed.words, sinceSec: parsed.sinceSec, limit: left }, cfg.paths) : [];
|
|
165
163
|
if (topics.length === 0 && hits.length === 0) {
|
|
166
164
|
console.log("no match");
|
|
167
165
|
return 1;
|
package/src/cli/results-build.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// The owner page's Results build, run as its own process by spawnResultsBuild (src/
|
|
1
|
+
// The owner page's Results build, run as its own process by spawnResultsBuild (src/cli/workspace.ts):
|
|
2
2
|
// it parses months of sessions (over a minute, GBs of memory), which froze the page when the server
|
|
3
3
|
// did it itself (I-20261030-121). Writes the numbers to resultsCachePath(); the page only reads them.
|
|
4
4
|
|
|
@@ -7,13 +7,11 @@ import { getAdapter } from "../adapters/index.ts";
|
|
|
7
7
|
import { nowSec } from "../core/clock.ts";
|
|
8
8
|
import { loadConfig } from "../core/config.ts";
|
|
9
9
|
import { computeResults, resultsCachePath } from "../core/owner-ui.ts";
|
|
10
|
-
import { applyQueueEnv } from "../core/queue/env.ts";
|
|
11
10
|
|
|
12
11
|
const cfg = loadConfig();
|
|
13
|
-
applyQueueEnv(cfg);
|
|
14
12
|
const at = nowSec();
|
|
15
13
|
const data = computeResults(cfg, getAdapter(cfg).runSource(cfg), at);
|
|
16
|
-
const path = resultsCachePath();
|
|
14
|
+
const path = resultsCachePath(cfg.paths);
|
|
17
15
|
// Write then rename: the page never reads a half-written file.
|
|
18
16
|
writeFileSync(`${path}.tmp`, JSON.stringify({ at, data }));
|
|
19
17
|
renameSync(`${path}.tmp`, path);
|
package/src/cli/workspace.ts
CHANGED
|
@@ -14,8 +14,7 @@ import { homedir } from "node:os";
|
|
|
14
14
|
import { getAdapter } from "../adapters/index.ts";
|
|
15
15
|
import { ConfigError, loadConfig, type Config } from "../core/config.ts";
|
|
16
16
|
import { beat, errorLine } from "../core/heartbeat.ts";
|
|
17
|
-
import { startOwnerUi, type OwnerUiOptions, type OwnerUiServer } from "../core/owner-ui.ts";
|
|
18
|
-
import { applyQueueEnv } from "../core/queue/env.ts";
|
|
17
|
+
import { startOwnerUi, type OwnerUiOptions, type OwnerUiServer, type ResultsBuild } from "../core/owner-ui.ts";
|
|
19
18
|
import { realTailscale, type Tailscale, untilIp4Changes } from "../core/tailscale.ts";
|
|
20
19
|
import { TEAM_NAME_HINT, TEAM_NAME_RE } from "../core/team-labels.ts";
|
|
21
20
|
import { addTeamMember, listTeamMembers, removeTeamMember, startTeamServer, type TeamServer, type TeamServerConfig, type TeamServerOptions } from "../core/team-server.ts";
|
|
@@ -93,6 +92,17 @@ function runWorkspaceSetup(args: string[], deps: WorkspaceDeps): number {
|
|
|
93
92
|
return uiOn({ ...setup, login, admin, repos, port, terminal });
|
|
94
93
|
}
|
|
95
94
|
|
|
95
|
+
/**
|
|
96
|
+
* Spawns the standalone Results cache builder process at low CPU priority and notifies `done` on exit.
|
|
97
|
+
*
|
|
98
|
+
* Args:
|
|
99
|
+
* done (() => void): Callback invoked when the child build process finishes or fails
|
|
100
|
+
*/
|
|
101
|
+
export const spawnResultsBuild: ResultsBuild = done => {
|
|
102
|
+
const proc = Bun.spawn(["nice", "-n", "19", process.execPath, `${import.meta.dir}/results-build.ts`], { stdout: "ignore", stderr: "inherit" });
|
|
103
|
+
void proc.exited.then(done, done);
|
|
104
|
+
};
|
|
105
|
+
|
|
96
106
|
export async function runWorkspace(args: string[] = [], deps: WorkspaceDeps = {}): Promise<number> {
|
|
97
107
|
if (args[0] === "on" || args[0] === "off") return runWorkspaceSetup(args, deps);
|
|
98
108
|
if (args[0] === "members") return runMembers(args.slice(1), deps);
|
|
@@ -110,7 +120,6 @@ export async function runWorkspace(args: string[] = [], deps: WorkspaceDeps = {}
|
|
|
110
120
|
return 1;
|
|
111
121
|
}
|
|
112
122
|
}
|
|
113
|
-
applyQueueEnv(cfg);
|
|
114
123
|
if (!cfg.workspace.enabled) {
|
|
115
124
|
console.error("neurastream workspace: workspace.enabled is false — set it in the config first");
|
|
116
125
|
return 1;
|
|
@@ -122,7 +131,7 @@ export async function runWorkspace(args: string[] = [], deps: WorkspaceDeps = {}
|
|
|
122
131
|
const tailscale = deps.tailscale ?? realTailscale;
|
|
123
132
|
let server: OwnerUiServer;
|
|
124
133
|
try {
|
|
125
|
-
server = (deps.start ?? startOwnerUi)({ cfg, tailscale, source: getAdapter(cfg).runSource(cfg) });
|
|
134
|
+
server = (deps.start ?? startOwnerUi)({ cfg, tailscale, source: getAdapter(cfg).runSource(cfg), resultsBuild: spawnResultsBuild });
|
|
126
135
|
} catch (e) {
|
|
127
136
|
beat(cfg, "workspace", { ok: false, detail: errorLine(e) });
|
|
128
137
|
throw e;
|
|
@@ -171,7 +180,6 @@ async function serveTeam(name: string, deps: WorkspaceDeps): Promise<number> {
|
|
|
171
180
|
console.error(state);
|
|
172
181
|
return 1;
|
|
173
182
|
}
|
|
174
|
-
applyQueueEnv(state.cfg);
|
|
175
183
|
const tailscale = deps.tailscale ?? realTailscale;
|
|
176
184
|
let server: TeamServer;
|
|
177
185
|
try {
|
package/src/core/adapter.ts
CHANGED
|
@@ -185,6 +185,22 @@ export interface TaskResult {
|
|
|
185
185
|
ts: number;
|
|
186
186
|
}
|
|
187
187
|
|
|
188
|
+
/** One completion request sent through the active harness adapter's completion gateway. */
|
|
189
|
+
export interface CompleteRequest {
|
|
190
|
+
model: string;
|
|
191
|
+
system: string;
|
|
192
|
+
prompt: string;
|
|
193
|
+
maxTokens: number;
|
|
194
|
+
/** Reasoning effort for the model ("" or absent = none). */
|
|
195
|
+
effort?: string;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** Plain-text result and token usage returned by a harness adapter's completion call. */
|
|
199
|
+
export interface CompleteResult {
|
|
200
|
+
text: string;
|
|
201
|
+
usage: { prompt_tokens: number; completion_tokens: number };
|
|
202
|
+
}
|
|
203
|
+
|
|
188
204
|
/** Options for launching the harness binary; tests inject another path and a failing generator. */
|
|
189
205
|
export interface LaunchOptions {
|
|
190
206
|
harness?: string;
|