neurastream 0.3.26 → 0.3.27
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/agy/adapter.ts +649 -0
- package/src/adapters/claude/adapter.ts +545 -0
- package/src/adapters/codex/adapter.ts +588 -0
- package/src/adapters/hook-bridge.ts +429 -0
- package/src/adapters/index.ts +23 -0
- package/src/adapters/omp/adapter.ts +8 -0
- package/src/adapters/omp/index.ts +19 -7
- package/src/adapters/omp/launch.ts +5 -1
- package/src/adapters/omp/limit-wake.ts +4 -18
- package/src/adapters/omp/sessions.ts +6 -2
- package/src/adapters/omp/types.ts +19 -2
- package/src/adapters/shared.ts +1039 -0
- package/src/cli/commands.ts +109 -6
- package/src/cli/config-set.ts +26 -4
- package/src/cli/dispatch.ts +80 -7
- package/src/cli/recall.ts +5 -2
- package/src/cli/spawn.ts +616 -0
- package/src/core/adapter.ts +53 -0
- package/src/core/config-edit.ts +9 -1
- package/src/core/config.ts +79 -0
- package/src/core/guards/spawn.ts +46 -9
- package/src/core/guards/state.ts +23 -0
- package/src/core/guards/stop.ts +4 -4
- package/src/core/provider-state.ts +406 -19
- package/src/core/runner.ts +223 -0
- package/src/runners/herdr/index.ts +293 -0
- package/src/runners/index.ts +36 -0
- package/templates/AGENTS.md +1 -0
- package/templates/config.yml +10 -1
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
// AgentRunner implementation backed by the `herdr` 0.9.1 CLI.
|
|
2
|
+
// All command execution flows through an injectable `HerdrExec` seam so tests never touch a live daemon.
|
|
3
|
+
|
|
4
|
+
import type {
|
|
5
|
+
AgentLifecycleState,
|
|
6
|
+
AgentRunner,
|
|
7
|
+
RunnerAgentInfo,
|
|
8
|
+
SpawnHandle,
|
|
9
|
+
SpawnLaunchSpec,
|
|
10
|
+
SpawnReadOptions,
|
|
11
|
+
SpawnSendOptions,
|
|
12
|
+
SpawnSendResult,
|
|
13
|
+
SpawnWaitOptions,
|
|
14
|
+
SpawnWaitResult,
|
|
15
|
+
} from "../../core/runner.ts";
|
|
16
|
+
|
|
17
|
+
/** Result returned by invoking the `herdr` binary. */
|
|
18
|
+
export interface HerdrExecResult {
|
|
19
|
+
code: number;
|
|
20
|
+
stdout: string;
|
|
21
|
+
stderr: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Injectable command execution seam for `herdr` CLI calls. */
|
|
25
|
+
export type HerdrExec = (args: string[], opts?: { env?: NodeJS.ProcessEnv }) => Promise<HerdrExecResult>;
|
|
26
|
+
|
|
27
|
+
/** Configuration options for constructing a `herdr` `AgentRunner`. */
|
|
28
|
+
export interface HerdrRunnerOptions {
|
|
29
|
+
exec?: HerdrExec;
|
|
30
|
+
env?: NodeJS.ProcessEnv;
|
|
31
|
+
requireHerdrEnv?: boolean;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const VALID_STATES = new Set<AgentLifecycleState>(["idle", "working", "blocked", "done", "unknown"]);
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Default command executor invoking `herdr` via `Bun.spawn`.
|
|
38
|
+
*
|
|
39
|
+
* Args:
|
|
40
|
+
* args (string[]): Subcommand and flags to pass to `herdr`
|
|
41
|
+
* opts ({ env?: NodeJS.ProcessEnv }): Optional environment override
|
|
42
|
+
*
|
|
43
|
+
* Returns:
|
|
44
|
+
* Promise<HerdrExecResult>: Exit code and captured stdout/stderr
|
|
45
|
+
*/
|
|
46
|
+
export async function defaultHerdrExec(args: string[], opts: { env?: NodeJS.ProcessEnv } = {}): Promise<HerdrExecResult> {
|
|
47
|
+
let proc: ReturnType<typeof Bun.spawn>;
|
|
48
|
+
try {
|
|
49
|
+
proc = Bun.spawn(["herdr", ...args], {
|
|
50
|
+
env: opts.env ?? process.env,
|
|
51
|
+
stdin: "ignore",
|
|
52
|
+
stdout: "pipe",
|
|
53
|
+
stderr: "pipe",
|
|
54
|
+
});
|
|
55
|
+
} catch (err) {
|
|
56
|
+
throw new Error(`herdr ${args.join(" ")}: failed to spawn herdr (${err instanceof Error ? err.message : String(err)})`, { cause: err });
|
|
57
|
+
}
|
|
58
|
+
const [stdout, stderr, code] = await Promise.all([
|
|
59
|
+
new Response(proc.stdout as ReadableStream<Uint8Array>).text(),
|
|
60
|
+
new Response(proc.stderr as ReadableStream<Uint8Array>).text(),
|
|
61
|
+
proc.exited,
|
|
62
|
+
]);
|
|
63
|
+
return { code, stdout, stderr };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function asObj(v: unknown): Record<string, unknown> | null {
|
|
67
|
+
return v !== null && typeof v === "object" && !Array.isArray(v) ? (v as Record<string, unknown>) : null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function parseJson(text: string): Record<string, unknown> | null {
|
|
71
|
+
const trimmed = text.trim();
|
|
72
|
+
if (!trimmed.startsWith("{") && !trimmed.startsWith("[")) return null;
|
|
73
|
+
try {
|
|
74
|
+
return asObj(JSON.parse(trimmed));
|
|
75
|
+
} catch {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function normalizeState(v: unknown): AgentLifecycleState {
|
|
81
|
+
return typeof v === "string" && VALID_STATES.has(v as AgentLifecycleState) ? (v as AgentLifecycleState) : "unknown";
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function extractStatus(parsed: Record<string, unknown> | null): AgentLifecycleState | undefined {
|
|
85
|
+
const result = asObj(parsed?.result);
|
|
86
|
+
if (!result) return undefined;
|
|
87
|
+
const agent = asObj(result.agent);
|
|
88
|
+
if (agent && typeof agent.agent_status === "string") return normalizeState(agent.agent_status);
|
|
89
|
+
const event = asObj(result.event);
|
|
90
|
+
const data = asObj(event?.data);
|
|
91
|
+
if (data) {
|
|
92
|
+
if (typeof data.agent_status === "string") return normalizeState(data.agent_status);
|
|
93
|
+
if (typeof data.final_status === "string") return normalizeState(data.final_status);
|
|
94
|
+
}
|
|
95
|
+
return undefined;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function formatHerdrError(action: string, res: HerdrExecResult): Error {
|
|
99
|
+
const errJson = parseJson(res.stderr) ?? parseJson(res.stdout);
|
|
100
|
+
const errBody = asObj(errJson?.error);
|
|
101
|
+
const code = typeof errBody?.code === "string" ? errBody.code : undefined;
|
|
102
|
+
const msg = typeof errBody?.message === "string" ? errBody.message : undefined;
|
|
103
|
+
if (code && msg) return new Error(`herdr ${action} failed [${code}]: ${msg}`);
|
|
104
|
+
if (msg) return new Error(`herdr ${action} failed: ${msg}`);
|
|
105
|
+
const detail = (res.stderr.trim() || res.stdout.trim() || `exit ${res.code}`).slice(0, 400);
|
|
106
|
+
return new Error(`herdr ${action} failed (exit ${res.code}): ${detail}`);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function targetOf(handle: SpawnHandle | string): string {
|
|
110
|
+
return typeof handle === "string" ? handle : handle.target;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function paneIdOf(handle: SpawnHandle | string): string {
|
|
114
|
+
return typeof handle === "string" ? handle : handle.paneId;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Creates an `AgentRunner` that manages helper agents through the `herdr 0.9.1` CLI.
|
|
119
|
+
*
|
|
120
|
+
* Args:
|
|
121
|
+
* opts (HerdrRunnerOptions): Optional exec seam, environment override, and HERDR_ENV check flag
|
|
122
|
+
*
|
|
123
|
+
* Returns:
|
|
124
|
+
* AgentRunner: Configured herdr runner instance
|
|
125
|
+
*/
|
|
126
|
+
export function createHerdrRunner(opts: HerdrRunnerOptions = {}): AgentRunner {
|
|
127
|
+
const exec = opts.exec ?? defaultHerdrExec;
|
|
128
|
+
const execEnv = opts.env;
|
|
129
|
+
const checkEnv = opts.requireHerdrEnv ?? (!opts.exec || opts.env !== undefined);
|
|
130
|
+
|
|
131
|
+
const run = async (action: string, args: string[]): Promise<HerdrExecResult> => {
|
|
132
|
+
const res = await exec(args, execEnv ? { env: execEnv } : undefined);
|
|
133
|
+
if (res.code !== 0) throw formatHerdrError(action, res);
|
|
134
|
+
return res;
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
return {
|
|
138
|
+
name: "herdr",
|
|
139
|
+
|
|
140
|
+
async start(spec: SpawnLaunchSpec): Promise<SpawnHandle> {
|
|
141
|
+
const name = spec.name.trim();
|
|
142
|
+
if (!name) throw new Error("herdr start: agent name must be non-empty");
|
|
143
|
+
const kind = spec.kind.trim();
|
|
144
|
+
if (!kind) throw new Error("herdr start: agent kind must be non-empty");
|
|
145
|
+
|
|
146
|
+
let paneId = spec.pane?.trim() ?? "";
|
|
147
|
+
let createdPane = false;
|
|
148
|
+
if (!paneId) {
|
|
149
|
+
const activeEnv = execEnv ?? process.env;
|
|
150
|
+
if (checkEnv && activeEnv.HERDR_ENV !== "1") {
|
|
151
|
+
throw new Error("herdr start: HERDR_ENV=1 is required to split a pane inside Herdr");
|
|
152
|
+
}
|
|
153
|
+
const splitArgs = [
|
|
154
|
+
"pane",
|
|
155
|
+
"split",
|
|
156
|
+
"--current",
|
|
157
|
+
"--direction",
|
|
158
|
+
spec.direction ?? "right",
|
|
159
|
+
"--cwd",
|
|
160
|
+
spec.cwd,
|
|
161
|
+
];
|
|
162
|
+
for (const key of Object.keys(spec.env).sort()) {
|
|
163
|
+
splitArgs.push("--env", `${key}=${spec.env[key] ?? ""}`);
|
|
164
|
+
}
|
|
165
|
+
splitArgs.push("--no-focus");
|
|
166
|
+
|
|
167
|
+
const splitRes = await run("pane split", splitArgs);
|
|
168
|
+
const splitJson = parseJson(splitRes.stdout);
|
|
169
|
+
const parsedPaneId = asObj(asObj(splitJson?.result)?.pane)?.pane_id;
|
|
170
|
+
if (typeof parsedPaneId !== "string" || parsedPaneId.trim() === "") {
|
|
171
|
+
throw new Error(`herdr pane split did not return .result.pane.pane_id: ${splitRes.stdout.trim()}`);
|
|
172
|
+
}
|
|
173
|
+
paneId = parsedPaneId;
|
|
174
|
+
createdPane = true;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const startArgs = ["agent", "start", name, "--kind", kind, "--pane", paneId];
|
|
178
|
+
if (spec.startTimeoutMs !== undefined) {
|
|
179
|
+
const clamped = Math.max(1, Math.min(300_000, Math.round(spec.startTimeoutMs)));
|
|
180
|
+
startArgs.push("--timeout", String(clamped));
|
|
181
|
+
}
|
|
182
|
+
if (spec.args.length > 0) {
|
|
183
|
+
startArgs.push("--", ...spec.args);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
try {
|
|
187
|
+
await run("agent start", startArgs);
|
|
188
|
+
} catch (err) {
|
|
189
|
+
if (createdPane) {
|
|
190
|
+
try {
|
|
191
|
+
await exec(["pane", "close", paneId], execEnv ? { env: execEnv } : undefined);
|
|
192
|
+
} catch {
|
|
193
|
+
// best-effort rollback when agent start fails after splitting
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
throw err;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
return {
|
|
200
|
+
runner: "herdr",
|
|
201
|
+
target: name,
|
|
202
|
+
name,
|
|
203
|
+
paneId,
|
|
204
|
+
kind,
|
|
205
|
+
cwd: spec.cwd,
|
|
206
|
+
};
|
|
207
|
+
},
|
|
208
|
+
|
|
209
|
+
async send(handle: SpawnHandle | string, brief: string, sendOpts: SpawnSendOptions = {}): Promise<SpawnSendResult> {
|
|
210
|
+
const args = ["agent", "prompt", targetOf(handle), brief];
|
|
211
|
+
if (sendOpts.wait) args.push("--wait");
|
|
212
|
+
for (const status of sendOpts.until ?? []) {
|
|
213
|
+
args.push("--until", status);
|
|
214
|
+
}
|
|
215
|
+
if (sendOpts.timeoutMs !== undefined) {
|
|
216
|
+
args.push("--timeout", String(Math.max(1, Math.round(sendOpts.timeoutMs))));
|
|
217
|
+
}
|
|
218
|
+
const res = await run("agent prompt", args);
|
|
219
|
+
const state = extractStatus(parseJson(res.stdout));
|
|
220
|
+
return state ? { state } : {};
|
|
221
|
+
},
|
|
222
|
+
|
|
223
|
+
async wait(handle: SpawnHandle | string, waitOpts: SpawnWaitOptions = {}): Promise<SpawnWaitResult> {
|
|
224
|
+
const args = ["agent", "wait", targetOf(handle)];
|
|
225
|
+
for (const status of waitOpts.until ?? []) {
|
|
226
|
+
args.push("--until", status);
|
|
227
|
+
}
|
|
228
|
+
if (waitOpts.timeoutMs !== undefined) {
|
|
229
|
+
args.push("--timeout", String(Math.max(1, Math.round(waitOpts.timeoutMs))));
|
|
230
|
+
}
|
|
231
|
+
const res = await run("agent wait", args);
|
|
232
|
+
const state = extractStatus(parseJson(res.stdout)) ?? "unknown";
|
|
233
|
+
return { state };
|
|
234
|
+
},
|
|
235
|
+
|
|
236
|
+
async read(handle: SpawnHandle | string, readOpts: SpawnReadOptions = {}): Promise<string> {
|
|
237
|
+
const source = readOpts.source ?? "recent-unwrapped";
|
|
238
|
+
const args = ["agent", "read", targetOf(handle), "--source", source, "--format", "text"];
|
|
239
|
+
if (readOpts.lines !== undefined) {
|
|
240
|
+
args.push("--lines", String(Math.max(1, Math.round(readOpts.lines))));
|
|
241
|
+
}
|
|
242
|
+
const res = await run("agent read", args);
|
|
243
|
+
const parsed = parseJson(res.stdout);
|
|
244
|
+
if (parsed) {
|
|
245
|
+
const result = asObj(parsed.result);
|
|
246
|
+
const readObj = asObj(result?.read);
|
|
247
|
+
if (typeof readObj?.text === "string") return readObj.text;
|
|
248
|
+
if (typeof result?.text === "string") return result.text;
|
|
249
|
+
if (typeof parsed.text === "string") return parsed.text;
|
|
250
|
+
}
|
|
251
|
+
return res.stdout;
|
|
252
|
+
},
|
|
253
|
+
|
|
254
|
+
async list(): Promise<RunnerAgentInfo[]> {
|
|
255
|
+
const res = await run("agent list", ["agent", "list"]);
|
|
256
|
+
const parsed = parseJson(res.stdout);
|
|
257
|
+
const agentsRaw = asObj(parsed?.result)?.agents;
|
|
258
|
+
if (!Array.isArray(agentsRaw)) return [];
|
|
259
|
+
const out: RunnerAgentInfo[] = [];
|
|
260
|
+
for (const item of agentsRaw) {
|
|
261
|
+
const row = asObj(item);
|
|
262
|
+
if (!row) continue;
|
|
263
|
+
const paneId = typeof row.pane_id === "string" ? row.pane_id : "";
|
|
264
|
+
const name = typeof row.name === "string" && row.name !== "" ? row.name : paneId;
|
|
265
|
+
const kind =
|
|
266
|
+
typeof row.agent === "string" && row.agent !== ""
|
|
267
|
+
? row.agent
|
|
268
|
+
: typeof row.display_agent === "string" && row.display_agent !== ""
|
|
269
|
+
? row.display_agent
|
|
270
|
+
: "unknown";
|
|
271
|
+
out.push({
|
|
272
|
+
name,
|
|
273
|
+
kind,
|
|
274
|
+
paneId,
|
|
275
|
+
state: normalizeState(row.agent_status ?? row.status),
|
|
276
|
+
...(typeof row.cwd === "string" ? { cwd: row.cwd } : {}),
|
|
277
|
+
...(typeof row.workspace_id === "string" ? { workspaceId: row.workspace_id } : {}),
|
|
278
|
+
...(typeof row.tab_id === "string" ? { tabId: row.tab_id } : {}),
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
return out;
|
|
282
|
+
},
|
|
283
|
+
|
|
284
|
+
async stop(handle: SpawnHandle | string): Promise<void> {
|
|
285
|
+
const paneId = paneIdOf(handle).trim();
|
|
286
|
+
if (!paneId) throw new Error("herdr stop: pane id must be non-empty");
|
|
287
|
+
await run("pane close", ["pane", "close", paneId]);
|
|
288
|
+
},
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/** Default production `herdr` runner instance. */
|
|
293
|
+
export const herdrRunner: AgentRunner = createHerdrRunner();
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// Single registry door from src/cli to runner implementations.
|
|
2
|
+
// Tests in tests/host-neutral.test.ts enforce that src/cli imports runners only through this file.
|
|
3
|
+
|
|
4
|
+
import type { AgentRunner } from "../core/runner.ts";
|
|
5
|
+
import { herdrRunner } from "./herdr/index.ts";
|
|
6
|
+
|
|
7
|
+
const RUNNERS: Record<string, AgentRunner> = {
|
|
8
|
+
[herdrRunner.name]: herdrRunner,
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Returns the registered `AgentRunner` for `name`, throwing when unknown.
|
|
13
|
+
*
|
|
14
|
+
* Args:
|
|
15
|
+
* name (string): Runner name from config or CLI
|
|
16
|
+
*
|
|
17
|
+
* Returns:
|
|
18
|
+
* AgentRunner: Registered runner backend
|
|
19
|
+
*/
|
|
20
|
+
export function getRunner(name: string): AgentRunner {
|
|
21
|
+
const runner = RUNNERS[name];
|
|
22
|
+
if (!runner) {
|
|
23
|
+
throw new Error(`unknown runner "${name}"; known: ${knownRunners().join(", ")}`);
|
|
24
|
+
}
|
|
25
|
+
return runner;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Lists all registered runner backend names.
|
|
30
|
+
*
|
|
31
|
+
* Returns:
|
|
32
|
+
* string[]: Registered runner names
|
|
33
|
+
*/
|
|
34
|
+
export function knownRunners(): string[] {
|
|
35
|
+
return Object.keys(RUNNERS);
|
|
36
|
+
}
|
package/templates/AGENTS.md
CHANGED
|
@@ -36,6 +36,7 @@ You are Main, the orchestrator. Main plans, delegates, reviews and lands; implem
|
|
|
36
36
|
|`neurastream metrics report`|weekly when main-start says due; propose ONE rule change to the owner|
|
|
37
37
|
|`neurastream incident list --open`|session start, before status reports, when a spawn or stop is blocked|
|
|
38
38
|
|`machine-guard`, `heavy-run`|machine-guard at session start and before launches; every test run goes through heavy-run|
|
|
39
|
+
|`neurastream spawn --agent <name> --plan <plan.md> --cwd <worktree> --brief "<brief>"`|spawning helpers inside a managed terminal session (`HERDR_ENV=1`) so each helper runs in its configured native harness|
|
|
39
40
|
|
|
40
41
|
## Call formats (the guards refuse anything else)
|
|
41
42
|
|Call|Exact form|
|
package/templates/config.yml
CHANGED
|
@@ -24,6 +24,15 @@ paths:
|
|
|
24
24
|
# Which adapter (docs/adapter.md) serves runs, traces, launch, install and doctor.
|
|
25
25
|
harness: omp # the adapter that serves runs, traces, launch, install and doctor
|
|
26
26
|
|
|
27
|
+
# Cross-harness helper spawn runner settings and per-agent profiles.
|
|
28
|
+
spawn:
|
|
29
|
+
default_runner: herdr # runner backend used when spawning helper panes
|
|
30
|
+
fallback_harness: "" # optional fallback harness used if primary harness fails to start
|
|
31
|
+
direction: right # split direction for new helper panes (right or down)
|
|
32
|
+
start_timeout_ms: 30000 # startup readiness timeout in milliseconds
|
|
33
|
+
timeout_ms: 600000 # default prompt and wait timeout in milliseconds
|
|
34
|
+
profiles: {} # per-agent overrides ({harness, fallback_harness, runner, model, provider, role, direction, start_timeout_ms, timeout_ms, args, env})
|
|
35
|
+
|
|
27
36
|
# Live project rules (src/core/repo-rules.ts): the repo's own AGENTS.md, handed to the agent that
|
|
28
37
|
# works in it — a subagent on its plan's repo, Main the first time a session touches the repo.
|
|
29
38
|
# max_bytes caps the injected text; past it the text is cut and one line names the file to read.
|
|
@@ -136,7 +145,7 @@ guards:
|
|
|
136
145
|
idle_stops: 3 # idle stops before the guard intervenes
|
|
137
146
|
plan_offer: 'want me to (write|make) a plan|should i plan|shall i plan|if you want, i(''ll)? plan|plan it or|\bi will\b|\bi(''|’)ll\b' # refused: don't promise or offer it — do it now, or write the plan and index it
|
|
138
147
|
rate_limit:
|
|
139
|
-
patterns: ["429", "rate_limit", "rate limit", "overloaded", "529"] # error-text substrings (case-insensitive) that mark a provider rate limit
|
|
148
|
+
patterns: ["429", "rate_limit", "rate limit", "overloaded", "529", "usage limit", "limit reached", "hit your limit", "quota_exhausted", "quota exceeded"] # error-text substrings (case-insensitive) that mark a provider rate limit
|
|
140
149
|
slack_s: 15 # added on top of every wait
|
|
141
150
|
probe_interval_s: 300 # probe the provider this often (s) while it names no reset
|
|
142
151
|
max_probes: 288 # probes before the episode gives up (288 x 300 s = 24 h)
|