@elecnix/bellwether 0.1.0 → 0.1.2
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 +2 -1
- package/dist/cli.js +295 -0
- package/dist/core/classifier.js +66 -0
- package/dist/core/herdr-client.js +368 -0
- package/dist/core/outbox.js +80 -0
- package/dist/core/paths.js +32 -0
- package/dist/core/registry.js +93 -0
- package/dist/core/report.js +50 -0
- package/dist/core/spawn.js +128 -0
- package/dist/core/transcript.js +79 -0
- package/dist/core/types.js +1 -0
- package/dist/core/watcher.js +593 -0
- package/dist/daemon/client.js +377 -0
- package/dist/daemon/lock.js +68 -0
- package/dist/daemon/main.js +34 -0
- package/dist/daemon/protocol.js +90 -0
- package/dist/daemon/server.js +244 -0
- package/dist/daemon/service.js +63 -0
- package/dist/integrations/claude/channel.js +245 -0
- package/dist/integrations/claude/transcript.js +135 -0
- package/dist/integrations/pi/extension.js +117 -0
- package/dist/integrations/tools.js +220 -0
- package/package.json +7 -6
- package/bin/bellwether-mcp.js +0 -4
- package/bin/bellwether.js +0 -2
- package/src/cli.ts +0 -293
- package/src/core/classifier.ts +0 -108
- package/src/core/herdr-client.ts +0 -410
- package/src/core/outbox.ts +0 -89
- package/src/core/paths.ts +0 -41
- package/src/core/registry.ts +0 -96
- package/src/core/report.ts +0 -54
- package/src/core/spawn.ts +0 -159
- package/src/core/transcript.ts +0 -74
- package/src/core/types.ts +0 -95
- package/src/core/watcher.ts +0 -590
- package/src/daemon/client.ts +0 -383
- package/src/daemon/lock.ts +0 -72
- package/src/daemon/main.ts +0 -38
- package/src/daemon/protocol.ts +0 -184
- package/src/daemon/server.ts +0 -275
- package/src/daemon/service.ts +0 -80
- package/src/integrations/claude/channel.ts +0 -247
- package/src/integrations/claude/transcript.ts +0 -124
- package/src/integrations/pi/extension.ts +0 -136
- package/src/integrations/tools.ts +0 -235
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
// Starting a child. The order follows the design: write the registry record
|
|
2
|
+
// first, then create the tab with the Bellwether environment, then ask herdr
|
|
3
|
+
// to start the harness in the tab's root pane, then watch the new pane.
|
|
4
|
+
import os from 'node:os';
|
|
5
|
+
import { HerdrApiError } from './herdr-client.js';
|
|
6
|
+
import { newChildId } from './outbox.js';
|
|
7
|
+
import { updateChild, upsertChild } from './registry.js';
|
|
8
|
+
import { resolveTranscript } from './transcript.js';
|
|
9
|
+
/** How deep a Bellwether child may spawn. The design starts at 2. */
|
|
10
|
+
export const DEFAULT_MAX_DEPTH = 2;
|
|
11
|
+
export const AGENT_KINDS = ['pi', 'claude', 'codex', 'opencode', 'cline'];
|
|
12
|
+
export function harnessOf(kind) {
|
|
13
|
+
if (kind === 'pi')
|
|
14
|
+
return 'pi';
|
|
15
|
+
if (kind === 'claude')
|
|
16
|
+
return 'claude-code';
|
|
17
|
+
return 'other';
|
|
18
|
+
}
|
|
19
|
+
/** The extra arguments a child harness needs to load Bellwether itself. */
|
|
20
|
+
export function childArgs(kind, options) {
|
|
21
|
+
const args = [];
|
|
22
|
+
if (options.model)
|
|
23
|
+
args.push('--model', options.model);
|
|
24
|
+
// Claude Code reads this flag as a list and keeps taking values until the
|
|
25
|
+
// next flag, so the two-argument form swallowed the prompt that follows it.
|
|
26
|
+
// The equals form pins the flag to one value.
|
|
27
|
+
if (kind === 'claude')
|
|
28
|
+
args.push('--dangerously-load-development-channels=server:bellwether');
|
|
29
|
+
if (options.prompt)
|
|
30
|
+
args.push(options.prompt);
|
|
31
|
+
if (options.args?.length)
|
|
32
|
+
args.push(...options.args);
|
|
33
|
+
return args;
|
|
34
|
+
}
|
|
35
|
+
export async function spawnChild(client, parent, options, signal) {
|
|
36
|
+
const depth = parent.depth + 1;
|
|
37
|
+
const maxDepth = options.maxDepth ?? DEFAULT_MAX_DEPTH;
|
|
38
|
+
if (depth > maxDepth)
|
|
39
|
+
throw new Error(`Bellwether is ${maxDepth} levels deep already, so ${options.name} would go one level too far. Raise max_depth on the spawn if that is what you want.`);
|
|
40
|
+
const now = new Date().toISOString();
|
|
41
|
+
const child = {
|
|
42
|
+
id: newChildId(),
|
|
43
|
+
name: options.name,
|
|
44
|
+
kind: options.kind,
|
|
45
|
+
harness: harnessOf(options.kind),
|
|
46
|
+
paneId: '',
|
|
47
|
+
cwd: options.cwd,
|
|
48
|
+
startedAt: now,
|
|
49
|
+
lastStatus: 'unknown',
|
|
50
|
+
statusChangedAt: now,
|
|
51
|
+
lastBusyAt: now,
|
|
52
|
+
depth,
|
|
53
|
+
stuckMs: options.stuckMinutes === undefined ? undefined : Math.max(1_000, Math.round(options.stuckMinutes * 60_000)),
|
|
54
|
+
};
|
|
55
|
+
// 1. The record lands before herdr is asked for anything.
|
|
56
|
+
upsertChild(parent.id, child);
|
|
57
|
+
// 2. The tab carries the environment the child needs to find its parent.
|
|
58
|
+
const env = {
|
|
59
|
+
BELLWETHER_PARENT: parent.id,
|
|
60
|
+
BELLWETHER_CHILD: child.id,
|
|
61
|
+
BELLWETHER_CHILD_NAME: child.name,
|
|
62
|
+
BELLWETHER_DEPTH: String(depth),
|
|
63
|
+
BELLWETHER_HARNESS: child.harness,
|
|
64
|
+
};
|
|
65
|
+
const tab = await client.tabCreate({ workspaceId: options.workspaceId ?? parent.workspaceId, cwd: options.cwd, label: child.name, env, focus: options.focus ?? false }, signal);
|
|
66
|
+
if (!tab.paneId)
|
|
67
|
+
throw new Error('herdr created no pane for the child tab');
|
|
68
|
+
child.tabId = tab.tabId;
|
|
69
|
+
child.paneId = tab.paneId;
|
|
70
|
+
child.workspaceId = options.workspaceId ?? parent.workspaceId;
|
|
71
|
+
upsertChild(parent.id, child);
|
|
72
|
+
// 3. Start the harness in the tab's root pane. A tab answers tab.create
|
|
73
|
+
// before its shell is at a prompt, so herdr refuses the start with
|
|
74
|
+
// agent_pane_busy until the shell is ready.
|
|
75
|
+
try {
|
|
76
|
+
await startWithRetry(client, { name: child.name, kind: options.kind, paneId: tab.paneId, args: childArgs(options.kind, options) }, signal);
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
await client.tabClose(tab.tabId ?? tab.paneId).catch(() => { });
|
|
80
|
+
throw error;
|
|
81
|
+
}
|
|
82
|
+
// 4. Resolve the transcript, then hand back the child and its herdr link.
|
|
83
|
+
// The registry is re-read rather than written from the caller's copy, which
|
|
84
|
+
// would erase the child record that upsertChild just added.
|
|
85
|
+
await new Promise((resolve) => setTimeout(resolve, 750));
|
|
86
|
+
try {
|
|
87
|
+
const panes = await client.agentList(signal);
|
|
88
|
+
const pane = panes.find((entry) => entry.paneId === child.paneId);
|
|
89
|
+
if (pane) {
|
|
90
|
+
child.transcript = resolveTranscript(child.harness, pane.agentSessionId, child.cwd);
|
|
91
|
+
child.lastStatus = pane.agentStatus;
|
|
92
|
+
updateChild(parent.id, child.id, (entry) => {
|
|
93
|
+
entry.transcript = child.transcript;
|
|
94
|
+
entry.lastStatus = child.lastStatus;
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
// A missed session id only costs the transcript-based summary.
|
|
100
|
+
}
|
|
101
|
+
return { child, link: herdrLink('pane', child.paneId) };
|
|
102
|
+
}
|
|
103
|
+
/** herdr refuses a start until the new tab's shell reaches its prompt. */
|
|
104
|
+
const PANE_NOT_READY = new Set(['agent_pane_busy', 'agent_pane_not_ready', 'pane_busy']);
|
|
105
|
+
export async function startWithRetry(client, params, signal, totalMs = 60_000) {
|
|
106
|
+
const deadline = Date.now() + totalMs;
|
|
107
|
+
let delay = 400;
|
|
108
|
+
for (;;) {
|
|
109
|
+
try {
|
|
110
|
+
await client.agentStart(params, signal);
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
const code = error instanceof HerdrApiError ? error.code : undefined;
|
|
115
|
+
if (!code || !PANE_NOT_READY.has(code) || Date.now() > deadline)
|
|
116
|
+
throw error;
|
|
117
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
118
|
+
delay = Math.min(delay * 2, 4_000);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
export function herdrLink(kind, id) {
|
|
123
|
+
const fingerprint = process.env['HERDR_ENV'] === '1' ? '' : '';
|
|
124
|
+
return `https://herdr.invalid/v1/${fingerprint}${kind}/${id}`;
|
|
125
|
+
}
|
|
126
|
+
export function hostName() {
|
|
127
|
+
return os.hostname();
|
|
128
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// Finding a child's transcript. The watcher reads the last assistant message
|
|
2
|
+
// from it, which is where a finished child's answer already sits.
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import os from 'node:os';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
/** Claude Code replaces every non-alphanumeric character of the cwd with a dash. */
|
|
7
|
+
export function claudeProjectDir(cwd) {
|
|
8
|
+
return cwd.replace(/[^A-Za-z0-9]/g, '-');
|
|
9
|
+
}
|
|
10
|
+
function newestFile(dir, matches, depth = 3) {
|
|
11
|
+
let best;
|
|
12
|
+
const walk = (current, remaining) => {
|
|
13
|
+
let entries;
|
|
14
|
+
try {
|
|
15
|
+
entries = fs.readdirSync(current, { withFileTypes: true });
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
for (const entry of entries) {
|
|
21
|
+
const full = path.join(current, entry.name);
|
|
22
|
+
if (entry.isDirectory() && remaining > 0)
|
|
23
|
+
walk(full, remaining - 1);
|
|
24
|
+
else if (entry.isFile() && matches(entry.name)) {
|
|
25
|
+
try {
|
|
26
|
+
const mtime = fs.statSync(full).mtimeMs;
|
|
27
|
+
if (!best || mtime > best.mtime)
|
|
28
|
+
best = { file: full, mtime };
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
// A transcript that vanished is not an error here.
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
walk(dir, depth);
|
|
37
|
+
return best?.file;
|
|
38
|
+
}
|
|
39
|
+
/** Pi names a session file `<timestamp>_<session id>.jsonl`. */
|
|
40
|
+
const endsWith = (name) => (sessionId) => name.endsWith(`${sessionId}.jsonl`);
|
|
41
|
+
/**
|
|
42
|
+
* Locate the transcript for a harness session. Pi and Claude Code both keep
|
|
43
|
+
* one JSONL file per session, named after the session id, under a per-project
|
|
44
|
+
* directory.
|
|
45
|
+
*/
|
|
46
|
+
export function resolveTranscript(harness, sessionId, cwd) {
|
|
47
|
+
if (!sessionId)
|
|
48
|
+
return undefined;
|
|
49
|
+
// Pi reports its session as a path, Claude Code as an id. Take a path as one.
|
|
50
|
+
if (sessionId.includes('/') || sessionId.endsWith('.jsonl'))
|
|
51
|
+
return fs.existsSync(sessionId) ? sessionId : undefined;
|
|
52
|
+
const home = os.homedir();
|
|
53
|
+
if (harness === 'claude-code') {
|
|
54
|
+
const root = path.join(home, '.claude', 'projects');
|
|
55
|
+
const direct = cwd ? path.join(root, claudeProjectDir(cwd), `${sessionId}.jsonl`) : undefined;
|
|
56
|
+
if (direct && fs.existsSync(direct))
|
|
57
|
+
return direct;
|
|
58
|
+
return newestFile(root, endsWith(sessionId));
|
|
59
|
+
}
|
|
60
|
+
if (harness === 'pi') {
|
|
61
|
+
const root = process.env['PI_CODING_AGENT_DIR'] ?? path.join(home, '.pi', 'agent');
|
|
62
|
+
return newestFile(path.join(root, 'sessions'), endsWith(sessionId));
|
|
63
|
+
}
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
/** The session value herdr reports for a pane, whatever shape it takes. */
|
|
67
|
+
export function sessionValueFrom(agentSession) {
|
|
68
|
+
if (typeof agentSession === 'string')
|
|
69
|
+
return agentSession;
|
|
70
|
+
if (typeof agentSession === 'object' && agentSession !== null) {
|
|
71
|
+
const value = agentSession['value'];
|
|
72
|
+
if (typeof value === 'string' && value.length > 0)
|
|
73
|
+
return value;
|
|
74
|
+
const id = agentSession['id'];
|
|
75
|
+
if (typeof id === 'string' && id.length > 0)
|
|
76
|
+
return id;
|
|
77
|
+
}
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|