benignswarm 0.12.42 → 0.12.44
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/dist/antigravity.js +57 -0
- package/dist/commands/antigravity.js +26 -0
- package/dist/commands/codex.js +50 -0
- package/dist/commands/hook.js +168 -25
- package/dist/commands/join.js +21 -4
- package/dist/commands/watch.js +5 -0
- package/dist/harness.js +46 -0
- package/dist/help.js +2 -0
- package/dist/index.js +4 -0
- package/dist/sessions.js +65 -0
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { configRoot } from "./config.js";
|
|
5
|
+
import { CliError } from "./errors.js";
|
|
6
|
+
import { watcherPlists } from "./install.js";
|
|
7
|
+
/**
|
|
8
|
+
* Which agent each Antigravity conversation belongs to, on this machine. Antigravity's hooks are
|
|
9
|
+
* configured per workspace, and one workspace often hosts several Antigravity agents (ester has
|
|
10
|
+
* four), so the hook cannot take its identity from the workspace: it looks up the conversationId
|
|
11
|
+
* every event carries. A conversation nobody bound gets nothing, never another agent's messages.
|
|
12
|
+
*/
|
|
13
|
+
const file = () => join(configRoot(), "antigravity.json");
|
|
14
|
+
const UUID = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/i;
|
|
15
|
+
function read() {
|
|
16
|
+
if (!existsSync(file()))
|
|
17
|
+
return { conversations: {} };
|
|
18
|
+
try {
|
|
19
|
+
return JSON.parse(readFileSync(file(), "utf8"));
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
throw new CliError("bad_antigravity_map", `${file()} is not valid JSON`, "Remove it and run: bschat antigravity sync");
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export function bindConversation(conversationId, ref) {
|
|
26
|
+
if (!UUID.test(conversationId))
|
|
27
|
+
throw new CliError("usage", `"${conversationId}" is not an Antigravity conversation id (a UUID)`);
|
|
28
|
+
const m = read();
|
|
29
|
+
m.conversations[conversationId.toLowerCase()] = ref;
|
|
30
|
+
mkdirSync(dirname(file()), { recursive: true });
|
|
31
|
+
writeFileSync(file(), JSON.stringify(m, null, 2) + "\n");
|
|
32
|
+
}
|
|
33
|
+
export function conversationAgent(conversationId) {
|
|
34
|
+
return read().conversations[conversationId.toLowerCase()] ?? null;
|
|
35
|
+
}
|
|
36
|
+
/** The conversation an Antigravity wake command addresses (`agentapi send-message ... <id>`), if any. */
|
|
37
|
+
export function conversationOfExec(exec) {
|
|
38
|
+
return /agentapi\b/.test(exec) ? UUID.exec(exec)?.[0]?.toLowerCase() ?? null : null;
|
|
39
|
+
}
|
|
40
|
+
/** Binds every conversation an installed Antigravity watcher wakes to the agent it runs as. */
|
|
41
|
+
export function syncFromWatchers() {
|
|
42
|
+
const out = [];
|
|
43
|
+
for (const w of watcherPlists()) {
|
|
44
|
+
const r = spawnSync("plutil", ["-convert", "json", "-o", "-", w.path], { encoding: "utf8" });
|
|
45
|
+
if (r.status !== 0)
|
|
46
|
+
continue;
|
|
47
|
+
const args = JSON.parse(r.stdout).ProgramArguments ?? [];
|
|
48
|
+
const exec = args[args.indexOf("--exec") + 1] ?? "";
|
|
49
|
+
const as = args[args.indexOf("--as") + 1];
|
|
50
|
+
const conv = conversationOfExec(exec);
|
|
51
|
+
if (conv && as && args.includes("--as")) {
|
|
52
|
+
bindConversation(conv, as);
|
|
53
|
+
out.push({ conversation: conv, agent: as });
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return out;
|
|
57
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { str } from "../args.js";
|
|
2
|
+
import { agentRef, loadAgent, resolveAgentName } from "../config.js";
|
|
3
|
+
import { bindConversation, syncFromWatchers } from "../sessions.js";
|
|
4
|
+
import { CliError } from "../errors.js";
|
|
5
|
+
/**
|
|
6
|
+
* bschat antigravity bind <conversation-id> [--as <agent>] this Antigravity conversation is you: its hook delivers to you
|
|
7
|
+
* bschat antigravity sync bind every conversation an installed watcher wakes
|
|
8
|
+
*/
|
|
9
|
+
export async function antigravity(p, out) {
|
|
10
|
+
const sub = p.positionals[1];
|
|
11
|
+
if (sub === "bind") {
|
|
12
|
+
const conv = p.positionals[2];
|
|
13
|
+
if (!conv)
|
|
14
|
+
throw new CliError("usage", "antigravity bind <conversation-id> [--as <agent>] (the id is the folder name under ~/.gemini/antigravity/brain/ that holds your artifacts)");
|
|
15
|
+
const ref = agentRef(loadAgent(resolveAgentName(str(p.flags, "as"))));
|
|
16
|
+
bindConversation(conv, ref);
|
|
17
|
+
out.print(`Antigravity conversation ${conv} is ${ref}: its hook now delivers your mentions and assignments there.`, { conversation: conv, agent: ref });
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
if (sub === "sync") {
|
|
21
|
+
const bound = syncFromWatchers();
|
|
22
|
+
out.print(bound.length ? `Bound ${bound.length} conversation(s) from watchers:\n${bound.map((b) => ` ${b.conversation} -> ${b.agent}`).join("\n")}` : "No Antigravity watcher found on this machine.", { bound });
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
throw new CliError("usage", "antigravity bind <conversation-id> [--as <agent>] | antigravity sync");
|
|
26
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { str } from "../args.js";
|
|
5
|
+
import { agentRef, loadAgent, resolveAgentName } from "../config.js";
|
|
6
|
+
import { CliError } from "../errors.js";
|
|
7
|
+
import { withCodexHooks } from "../harness.js";
|
|
8
|
+
import { requireInstall } from "../install.js";
|
|
9
|
+
import { bindSession, syncFromWatchers } from "../sessions.js";
|
|
10
|
+
/**
|
|
11
|
+
* bschat codex hooks install bschat's hooks in ~/.codex/hooks.json (this machine's Codex)
|
|
12
|
+
* bschat codex bind <thread-id> [--as a] this Codex thread is you: its hooks deliver to you
|
|
13
|
+
* bschat codex sync bind every thread an installed watcher wakes
|
|
14
|
+
*/
|
|
15
|
+
export async function codex(p, out) {
|
|
16
|
+
const sub = p.positionals[1];
|
|
17
|
+
if (sub === "hooks") {
|
|
18
|
+
const path = codexHooksPath();
|
|
19
|
+
let doc = {};
|
|
20
|
+
if (existsSync(path)) {
|
|
21
|
+
try {
|
|
22
|
+
doc = JSON.parse(readFileSync(path, "utf8"));
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
throw new CliError("bad_settings", `${path} is not valid JSON; fix it or remove it`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
29
|
+
writeFileSync(path, JSON.stringify(withCodexHooks(doc, requireInstall().bin), null, 2) + "\n");
|
|
30
|
+
const bound = syncFromWatchers().filter((b) => b.kind === "codex");
|
|
31
|
+
out.print(`Wrote bschat's hooks to ${path} (SessionStart, UserPromptSubmit, PostToolUse, Stop).\nCodex asks you to trust new hooks the next time a session starts: approve them there.\nThreads bound from watchers: ${bound.length ? bound.map((b) => `${b.conversation} -> ${b.agent}`).join(", ") : "none"}. Any other Codex session gets nothing from them.`, { path, bound });
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
if (sub === "bind") {
|
|
35
|
+
const thread = p.positionals[2];
|
|
36
|
+
if (!thread)
|
|
37
|
+
throw new CliError("usage", "codex bind <thread-id> [--as <agent>] (the thread id is in the Codex session's rollout file name, ~/.codex/sessions/.../rollout-*-<id>.jsonl)");
|
|
38
|
+
const ref = agentRef(loadAgent(resolveAgentName(str(p.flags, "as"))));
|
|
39
|
+
bindSession("codex", thread, ref);
|
|
40
|
+
out.print(`Codex thread ${thread} is ${ref}: its hooks now deliver your mentions and assignments there.`, { thread, agent: ref });
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
if (sub === "sync") {
|
|
44
|
+
const bound = syncFromWatchers().filter((b) => b.kind === "codex");
|
|
45
|
+
out.print(bound.length ? `Bound ${bound.length} thread(s) from watchers:\n${bound.map((b) => ` ${b.conversation} -> ${b.agent}`).join("\n")}` : "No Codex watcher found on this machine.", { bound });
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
throw new CliError("usage", "codex hooks | codex bind <thread-id> [--as <agent>] | codex sync");
|
|
49
|
+
}
|
|
50
|
+
export const codexHooksPath = () => join(process.env.CODEX_HOME ?? join(homedir(), ".codex"), "hooks.json");
|
package/dist/commands/hook.js
CHANGED
|
@@ -6,6 +6,7 @@ import { heartbeatNow } from "../heartbeat.js";
|
|
|
6
6
|
import { fmtMessage } from "../output.js";
|
|
7
7
|
import { readState, writeState } from "../state.js";
|
|
8
8
|
import { goalFromClaudeTranscript, modelFromClaudeTranscript } from "../model.js";
|
|
9
|
+
import { conversationAgent, sessionAgent, syncFromWatchers } from "../sessions.js";
|
|
9
10
|
const HEARTBEAT_MS = 60_000;
|
|
10
11
|
/** Reads the harness's hook payload from stdin without blocking when there is none. */
|
|
11
12
|
function readStdinJson() {
|
|
@@ -152,6 +153,10 @@ export async function gatherUpdates(name, opts) {
|
|
|
152
153
|
*/
|
|
153
154
|
export async function hook(p) {
|
|
154
155
|
const harness = p.positionals[1] ?? "plain";
|
|
156
|
+
if (harness === "antigravity")
|
|
157
|
+
return antigravityHook(p);
|
|
158
|
+
if (harness === "codex")
|
|
159
|
+
return codexHook(p);
|
|
155
160
|
const event = str(p.flags, "event") ?? (harness === "claude-code" ? "UserPromptSubmit" : "");
|
|
156
161
|
let name;
|
|
157
162
|
try {
|
|
@@ -173,31 +178,7 @@ export async function hook(p) {
|
|
|
173
178
|
// Standing rules die with the context. On SessionStart (startup, resume, and after a compaction)
|
|
174
179
|
// re-deliver the role description and the project's process when the agent has not seen this
|
|
175
180
|
// version, and record that it was shown.
|
|
176
|
-
|
|
177
|
-
if (harness === "claude-code" && event === "SessionStart") {
|
|
178
|
-
try {
|
|
179
|
-
const creds = loadAgent(name);
|
|
180
|
-
const client = Client.forAgent(creds, undefined, { presence: false });
|
|
181
|
-
const me = await client.call("GET", "/me");
|
|
182
|
-
const proc = me.process;
|
|
183
|
-
const compacted = input?.source === "compact";
|
|
184
|
-
if (proc && (compacted || proc.seen_version !== proc.version)) {
|
|
185
|
-
const lines = [
|
|
186
|
-
`[bschat] ${compacted ? "Your context was compacted; here is what must survive it." : "Your standing role and process."}`,
|
|
187
|
-
me.role ? `Your role: ${me.role.name}. ${me.role.description}` : `You hold no role in ${me.project?.name ?? "this project"}.`,
|
|
188
|
-
`Process v${proc.version}: work goes through bschat wu open --tags <from the vocabulary> / submit; reviewers are whoever is qualified for the tags; evidence ${proc.definition.require_evidence_to_submit ? "required" : "optional"} before review; max ${proc.definition.max_open_per_agent} open units; nothing is done until every tag is approved. Details: bschat process.`,
|
|
189
|
-
proc.agreements ? `Working agreements:\n${proc.agreements}` : null,
|
|
190
|
-
proc.charter ? `Charter (how this project weighs its trade-offs):\n${proc.charter}` : null,
|
|
191
|
-
].filter(Boolean);
|
|
192
|
-
standing = lines.join("\n");
|
|
193
|
-
if (creds.project && proc.version > 0)
|
|
194
|
-
await client.call("POST", `/projects/${creds.project}/process/ack`, { version: proc.version });
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
catch (e) {
|
|
198
|
-
process.stderr.write(`bschat: could not fetch role/process: ${e.message}\n`);
|
|
199
|
-
}
|
|
200
|
-
}
|
|
181
|
+
const standing = harness === "claude-code" && event === "SessionStart" ? await standingRules(name, input?.source === "compact") : null;
|
|
201
182
|
// The hook firing is proof the harness is working; tell the server, at most once a minute.
|
|
202
183
|
// The session goal (/goal) rides along from the transcript, and a change goes out at once.
|
|
203
184
|
if (harness === "claude-code") {
|
|
@@ -226,3 +207,165 @@ export async function hook(p) {
|
|
|
226
207
|
process.stdout.write(text + "\n");
|
|
227
208
|
}
|
|
228
209
|
}
|
|
210
|
+
/**
|
|
211
|
+
* The role and process the agent must hold after a new session or a compaction, when it has not
|
|
212
|
+
* seen this process version (or always after a compaction). Records that it was shown.
|
|
213
|
+
*/
|
|
214
|
+
async function standingRules(name, compacted) {
|
|
215
|
+
let standing = null;
|
|
216
|
+
try {
|
|
217
|
+
const creds = loadAgent(name);
|
|
218
|
+
const client = Client.forAgent(creds, undefined, { presence: false });
|
|
219
|
+
const me = await client.call("GET", "/me");
|
|
220
|
+
const proc = me.process;
|
|
221
|
+
if (proc && (compacted || proc.seen_version !== proc.version)) {
|
|
222
|
+
const lines = [
|
|
223
|
+
`[bschat] ${compacted ? "Your context was compacted; here is what must survive it." : "Your standing role and process."}`,
|
|
224
|
+
me.role ? `Your role: ${me.role.name}. ${me.role.description}` : `You hold no role in ${me.project?.name ?? "this project"}.`,
|
|
225
|
+
`Process v${proc.version}: work goes through bschat wu open --tags <from the vocabulary> / submit; reviewers are whoever is qualified for the tags; evidence ${proc.definition.require_evidence_to_submit ? "required" : "optional"} before review; max ${proc.definition.max_open_per_agent} open units; nothing is done until every tag is approved. Details: bschat process.`,
|
|
226
|
+
proc.agreements ? `Working agreements:\n${proc.agreements}` : null,
|
|
227
|
+
proc.charter ? `Charter (how this project weighs its trade-offs):\n${proc.charter}` : null,
|
|
228
|
+
].filter(Boolean);
|
|
229
|
+
standing = lines.join("\n");
|
|
230
|
+
if (creds.project && proc.version > 0)
|
|
231
|
+
await client.call("POST", `/projects/${creds.project}/process/ack`, { version: proc.version });
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
catch (e) {
|
|
235
|
+
process.stderr.write(`bschat: could not fetch role/process: ${e.message}\n`);
|
|
236
|
+
}
|
|
237
|
+
return standing;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Antigravity's hooks (.agents/hooks.json, written by join): SessionStart and PreInvocation inject
|
|
241
|
+
* new mentions and assignments (and the standing rules) as a system message; PostToolUse is a
|
|
242
|
+
* liveness heartbeat; Stop, when the agent is about to go fully idle with something addressed to
|
|
243
|
+
* it, returns "continue" with the new items so it carries on instead of waiting for a wake-up.
|
|
244
|
+
* The agent is found by the conversation, never the workspace (several Antigravity agents share
|
|
245
|
+
* one). Always prints valid JSON and exits 0: a hook must never break the session.
|
|
246
|
+
*/
|
|
247
|
+
async function antigravityHook(p) {
|
|
248
|
+
const event = str(p.flags, "event") ?? "";
|
|
249
|
+
const out = (o) => { process.stdout.write(JSON.stringify(o) + "\n"); };
|
|
250
|
+
const input = readStdinJson();
|
|
251
|
+
const conv = typeof input?.conversationId === "string" ? input.conversationId : null;
|
|
252
|
+
let ref = conv ? conversationAgent(conv) : null;
|
|
253
|
+
if (conv && !ref) {
|
|
254
|
+
try {
|
|
255
|
+
syncFromWatchers();
|
|
256
|
+
}
|
|
257
|
+
catch (e) {
|
|
258
|
+
process.stderr.write(`bschat: could not read watchers: ${e.message}\n`);
|
|
259
|
+
}
|
|
260
|
+
ref = conversationAgent(conv);
|
|
261
|
+
}
|
|
262
|
+
if (!ref)
|
|
263
|
+
return out({}); // a conversation nobody bound: nothing, never another agent's messages
|
|
264
|
+
let name;
|
|
265
|
+
try {
|
|
266
|
+
name = resolveAgentName(ref);
|
|
267
|
+
}
|
|
268
|
+
catch {
|
|
269
|
+
return out({});
|
|
270
|
+
}
|
|
271
|
+
if (typeof input?.modelName === "string" && input.modelName) {
|
|
272
|
+
const st = readState(name);
|
|
273
|
+
if (st.model !== input.modelName)
|
|
274
|
+
writeState(name, { ...st, model: input.modelName, model_observed_at: Date.now() });
|
|
275
|
+
}
|
|
276
|
+
const st = readState(name);
|
|
277
|
+
if (Date.now() - (st.heartbeat_at ?? 0) > HEARTBEAT_MS) {
|
|
278
|
+
try {
|
|
279
|
+
await heartbeatNow(Client.forAgent(loadAgent(name), undefined, { presence: false }), "antigravity-hook");
|
|
280
|
+
writeState(name, { ...readState(name), heartbeat_at: Date.now() });
|
|
281
|
+
}
|
|
282
|
+
catch (e) {
|
|
283
|
+
process.stderr.write(`bschat: heartbeat failed: ${e.message}\n`);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
if (event === "PostToolUse")
|
|
287
|
+
return out({});
|
|
288
|
+
if (event === "Stop") {
|
|
289
|
+
if (input?.fullyIdle !== true)
|
|
290
|
+
return out({});
|
|
291
|
+
// Only something addressed to it keeps it going; chatter waits for its next turn.
|
|
292
|
+
const peek = await gatherUpdates(name, { force: true, delivery: "hook", dryRun: true });
|
|
293
|
+
if (!peek || (peek.tier !== "direct" && peek.tier !== "role"))
|
|
294
|
+
return out({});
|
|
295
|
+
const u = await gatherUpdates(name, { force: true, delivery: "hook" });
|
|
296
|
+
return out(u ? { decision: "continue", reason: `${u.text}\nHandle these, then carry on with what you were doing.` } : {});
|
|
297
|
+
}
|
|
298
|
+
if (event !== "SessionStart" && event !== "PreInvocation")
|
|
299
|
+
return out({});
|
|
300
|
+
const standing = event === "SessionStart" ? await standingRules(name, false) : null;
|
|
301
|
+
const u = await gatherUpdates(name, { force: event === "SessionStart", delivery: "hook" });
|
|
302
|
+
const text = [standing, u?.text].filter(Boolean).join("\n\n");
|
|
303
|
+
return out(text ? { injectSteps: [{ systemMessage: { systemMessage: text } }] } : {});
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Codex's hooks (~/.codex/hooks.json, written by `bschat codex hooks`; Claude Code's format):
|
|
307
|
+
* SessionStart injects the standing rules and anything unread, UserPromptSubmit new items,
|
|
308
|
+
* PostToolUse is a liveness heartbeat, and Stop with something addressed to the agent returns
|
|
309
|
+
* "block" with it, which Codex takes as the next input instead of stopping. The hooks file is the
|
|
310
|
+
* machine's, so the agent is found by the session id (the Codex thread a watcher wakes, or
|
|
311
|
+
* `bschat codex bind`); any other Codex session (the user's own chats) gets nothing.
|
|
312
|
+
* Always exits 0 with valid output: a hook must never break the session.
|
|
313
|
+
*/
|
|
314
|
+
async function codexHook(p) {
|
|
315
|
+
const event = str(p.flags, "event") ?? "";
|
|
316
|
+
const input = readStdinJson();
|
|
317
|
+
const sid = typeof input?.session_id === "string" ? input.session_id : null;
|
|
318
|
+
let ref = sid ? sessionAgent("codex", sid) : null;
|
|
319
|
+
if (sid && !ref) {
|
|
320
|
+
try {
|
|
321
|
+
syncFromWatchers();
|
|
322
|
+
}
|
|
323
|
+
catch (e) {
|
|
324
|
+
process.stderr.write(`bschat: could not read watchers: ${e.message}\n`);
|
|
325
|
+
}
|
|
326
|
+
ref = sessionAgent("codex", sid);
|
|
327
|
+
}
|
|
328
|
+
if (!ref)
|
|
329
|
+
return; // not an agent's thread: nothing
|
|
330
|
+
let name;
|
|
331
|
+
try {
|
|
332
|
+
name = resolveAgentName(ref);
|
|
333
|
+
}
|
|
334
|
+
catch {
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
if (typeof input?.model === "string" && input.model) {
|
|
338
|
+
const st = readState(name);
|
|
339
|
+
if (st.model !== input.model)
|
|
340
|
+
writeState(name, { ...st, model: input.model, model_observed_at: Date.now() });
|
|
341
|
+
}
|
|
342
|
+
if (Date.now() - (readState(name).heartbeat_at ?? 0) > HEARTBEAT_MS) {
|
|
343
|
+
try {
|
|
344
|
+
await heartbeatNow(Client.forAgent(loadAgent(name), undefined, { presence: false }), "codex-hook");
|
|
345
|
+
writeState(name, { ...readState(name), heartbeat_at: Date.now() });
|
|
346
|
+
}
|
|
347
|
+
catch (e) {
|
|
348
|
+
process.stderr.write(`bschat: heartbeat failed: ${e.message}\n`);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
const context = (text) => process.stdout.write(JSON.stringify({ hookSpecificOutput: { hookEventName: event, additionalContext: text } }) + "\n");
|
|
352
|
+
if (event === "PostToolUse")
|
|
353
|
+
return;
|
|
354
|
+
if (event === "Stop") {
|
|
355
|
+
// Only something addressed to it keeps it going; chatter waits for its next turn.
|
|
356
|
+
const peek = await gatherUpdates(name, { force: true, delivery: "hook", dryRun: true });
|
|
357
|
+
if (!peek || (peek.tier !== "direct" && peek.tier !== "role"))
|
|
358
|
+
return;
|
|
359
|
+
const u = await gatherUpdates(name, { force: true, delivery: "hook" });
|
|
360
|
+
if (u)
|
|
361
|
+
process.stdout.write(JSON.stringify({ decision: "block", reason: `${u.text}\nHandle these, then carry on with what you were doing.` }) + "\n");
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
if (event !== "SessionStart" && event !== "UserPromptSubmit")
|
|
365
|
+
return;
|
|
366
|
+
const standing = event === "SessionStart" ? await standingRules(name, false) : null;
|
|
367
|
+
const u = await gatherUpdates(name, { force: event === "SessionStart", delivery: "hook" });
|
|
368
|
+
const text = [standing, u?.text].filter(Boolean).join("\n\n");
|
|
369
|
+
if (text)
|
|
370
|
+
context(text);
|
|
371
|
+
}
|
package/dist/commands/join.js
CHANGED
|
@@ -2,12 +2,13 @@ import { CLI_VERSION } from "../version.js";
|
|
|
2
2
|
import { need, str, bool } from "../args.js";
|
|
3
3
|
import { agentRef, DEFAULT_URL, loadAgent, resolveModel, saveAgent } from "../config.js";
|
|
4
4
|
import { spawnSync } from "node:child_process";
|
|
5
|
-
import { readFileSync } from "node:fs";
|
|
6
|
-
import { join as pathJoin } from "node:path";
|
|
5
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
6
|
+
import { dirname, join as pathJoin } from "node:path";
|
|
7
7
|
import { CliError } from "../errors.js";
|
|
8
8
|
import { defaultMachine } from "../git.js";
|
|
9
9
|
import { ensureInstall } from "../install.js";
|
|
10
|
-
import {
|
|
10
|
+
import { codexHooksPath } from "./codex.js";
|
|
11
|
+
import { claudeCheckoutOwner, claudeCheckoutTaken, withCodexHooks, HARNESSES, findIdentityFile, installForHarness, normalizeVendor, readIdentityFile, writeIdentity } from "../harness.js";
|
|
11
12
|
import { detectVendor } from "../vendor.js";
|
|
12
13
|
function repoRoot() {
|
|
13
14
|
const r = spawnSync("git", ["rev-parse", "--show-toplevel"], { encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] });
|
|
@@ -126,7 +127,7 @@ export async function join(p, out) {
|
|
|
126
127
|
json.revived && json.resume ? `--- ${json.resume.title} ---\n${json.resume.body}\n---${json.resume.evidence.length ? `\nevidence: ${json.resume.evidence.map((e) => `evidence:${e}`).join(" ")}` : ""}` : null,
|
|
127
128
|
json.revived && json.resume ? `Read it before doing anything else.` : null,
|
|
128
129
|
``,
|
|
129
|
-
vendor !== "claude-code" ? `No hook exists for ${vendor}: you will not be told about replies unless you run a watcher. See the skill's "Knowing when someone talks to you" section (bschat watch --launchd).` : null,
|
|
130
|
+
vendor === "codex" ? `Codex: bschat's hooks are in ${installCodexHooks(inst.bin)} (approve them when Codex asks); they deliver your mentions and assignments inside your thread once bschat knows which thread is you: bschat codex bind <thread-id>, or a watcher (bschat watch --launchd), which is still what wakes you when you are idle.` : vendor === "antigravity" ? `Antigravity: .agents/hooks.json now delivers your mentions and assignments inside your conversation, once bschat knows which conversation is you: bschat antigravity bind <conversation-id> (the folder name under ~/.gemini/antigravity/brain/ holding your artifacts). A watcher (bschat watch --launchd) also binds it, and is still what wakes you when you are idle.` : vendor !== "claude-code" ? `No hook exists for ${vendor}: you will not be told about replies unless you run a watcher. See the skill's "Knowing when someone talks to you" section (bschat watch --launchd).` : null,
|
|
130
131
|
`Next: bschat me && bschat who && bschat inbox && bschat handoffs --open`,
|
|
131
132
|
].filter((l) => l !== null).join("\n");
|
|
132
133
|
out.print(human, { install: inst, agent: json.agent, project: json.project, saved: path, identity: idPath, bound: !noBind, written, revived: !!json.revived, resume: json.resume ?? null, export: { BSCHAT_AGENT: ref } });
|
|
@@ -162,3 +163,19 @@ function writeIdentityDryRun(root, vendor, name, replace) {
|
|
|
162
163
|
throw new CliError("identity_exists", `${p} already maps ${vendor} to agent "${existing.name}"`, `Two ${vendor} agents share this checkout. Join with --no-bind and address yourself per call (bschat --as ${name} ...), or --replace to make "${name}" this checkout's default instead of "${existing.name}".`);
|
|
163
164
|
}
|
|
164
165
|
}
|
|
166
|
+
/** Merges bschat's hooks into this machine's ~/.codex/hooks.json; returns its path. */
|
|
167
|
+
function installCodexHooks(bin) {
|
|
168
|
+
const path = codexHooksPath();
|
|
169
|
+
let doc = {};
|
|
170
|
+
if (existsSync(path)) {
|
|
171
|
+
try {
|
|
172
|
+
doc = JSON.parse(readFileSync(path, "utf8"));
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
throw new CliError("bad_settings", `${path} is not valid JSON; fix it or remove it`);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
179
|
+
writeFileSync(path, JSON.stringify(withCodexHooks(doc, bin), null, 2) + "\n");
|
|
180
|
+
return path;
|
|
181
|
+
}
|
package/dist/commands/watch.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
|
+
import { bindSession, sessionOfExec } from "../sessions.js";
|
|
2
3
|
import { reloadLaunchAgent, requireInstall } from "../install.js";
|
|
3
4
|
import { mkdirSync, writeFileSync } from "node:fs";
|
|
4
5
|
import { homedir } from "node:os";
|
|
@@ -334,6 +335,10 @@ function installLaunchd(name, exec, interval, followDelay, extraEnv, activity, o
|
|
|
334
335
|
// Load it from the file now, replacing a running copy: writing a plist changes nothing launchd
|
|
335
336
|
// has loaded, and a watcher re-pointed at a new session kept waking the old one for two days.
|
|
336
337
|
reloadLaunchAgent(label, path);
|
|
338
|
+
// A watcher names its session (Antigravity conversation, Codex thread): bind it, so that session's hook knows who it is.
|
|
339
|
+
const sess = sessionOfExec(exec);
|
|
340
|
+
if (sess)
|
|
341
|
+
bindSession(sess.kind, sess.id, name);
|
|
337
342
|
out.print(`Wrote ${path} and (re)loaded it: the watcher is running ${program[0]}.${Object.keys(extraEnv).length ? `\nCaptured env: ${Object.keys(extraEnv).join(", ")} (re-run this command if the harness restarts and these change)` : ""}\nStop it: launchctl bootout gui/$(id -u)/${label}\nLogs: ${logDir}/watch.${slug}.*.log\nCheck it: bschat doctor\nThe watcher runs from ${process.cwd()} so the repo identity file applies.`, { plist: path, label, program: program[0] });
|
|
338
343
|
}
|
|
339
344
|
function escapeXml(s) {
|
package/dist/harness.js
CHANGED
|
@@ -149,6 +149,18 @@ export function installForHarness(root, harness, files, agentName, apiUrl, bin)
|
|
|
149
149
|
mkdirSync(dirname(p), { recursive: true });
|
|
150
150
|
writeFileSync(p, `---\ntrigger: always_on\ndescription: "How to use bschat, the agent chat for this project"\n---\n\n${pointer(agentName, apiUrl, bin)}\n`);
|
|
151
151
|
written.push(p);
|
|
152
|
+
const hp = join(root, ".agents", "hooks.json");
|
|
153
|
+
let hooks = {};
|
|
154
|
+
if (existsSync(hp)) {
|
|
155
|
+
try {
|
|
156
|
+
hooks = JSON.parse(readFileSync(hp, "utf8"));
|
|
157
|
+
}
|
|
158
|
+
catch {
|
|
159
|
+
throw new CliError("bad_settings", `${hp} is not valid JSON; fix it or remove it`);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
writeFileSync(hp, JSON.stringify({ ...hooks, bschat: antigravityHooks(bin) }, null, 2) + "\n");
|
|
163
|
+
written.push(hp);
|
|
152
164
|
break;
|
|
153
165
|
}
|
|
154
166
|
case "gemini-cli": {
|
|
@@ -214,3 +226,37 @@ export function claudeCheckoutTaken(root, owner, name) {
|
|
|
214
226
|
const sibling = join(dirname(root), `${basename(root)}-${name}`);
|
|
215
227
|
return new CliError("checkout_taken", `this checkout's Claude Code hook runs as ${owner} for every Claude Code session opened here, so ${name} would be handed ${owner}'s messages (and they would count as delivered)`, `Give ${name} its own worktree: git -C ${root} worktree add ${sibling} then start Claude Code in ${sibling} and run this command there. (--replace makes ${name} this checkout's Claude Code agent instead of ${owner}.)`);
|
|
216
228
|
}
|
|
229
|
+
/**
|
|
230
|
+
* The `bschat` entry of an Antigravity workspace's .agents/hooks.json. The command carries no
|
|
231
|
+
* identity: several Antigravity agents share a workspace, so the hook finds its agent by the
|
|
232
|
+
* conversation (`bschat antigravity bind`, or a watcher installed for that conversation).
|
|
233
|
+
*/
|
|
234
|
+
export function antigravityHooks(bin) {
|
|
235
|
+
const cmd = (event) => ({ type: "command", command: `${shellPath(bin)} hook antigravity --event ${event}`, timeout: 20 });
|
|
236
|
+
return {
|
|
237
|
+
SessionStart: [cmd("SessionStart")],
|
|
238
|
+
PreInvocation: [cmd("PreInvocation")],
|
|
239
|
+
PostToolUse: [{ matcher: "*", hooks: [cmd("PostToolUse")] }],
|
|
240
|
+
Stop: [cmd("Stop")],
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
const CODEX_HOOK_MARK = "hook codex";
|
|
244
|
+
/**
|
|
245
|
+
* Merges bschat's hooks into a Codex hooks.json document ({"hooks": {Event: [groups]}}): any
|
|
246
|
+
* earlier bschat group is replaced, every other hook is kept. Commands carry no identity.
|
|
247
|
+
*/
|
|
248
|
+
export function withCodexHooks(doc, bin) {
|
|
249
|
+
const hooks = { ...(doc.hooks ?? {}) };
|
|
250
|
+
const cmd = (event) => ({ type: "command", command: `${shellPath(bin)} ${CODEX_HOOK_MARK} --event ${event}`, timeout: 20 });
|
|
251
|
+
const ours = {
|
|
252
|
+
SessionStart: { matcher: "startup|resume|clear|compact", hooks: [cmd("SessionStart")] },
|
|
253
|
+
UserPromptSubmit: { hooks: [cmd("UserPromptSubmit")] },
|
|
254
|
+
PostToolUse: { matcher: "*", hooks: [cmd("PostToolUse")] },
|
|
255
|
+
Stop: { hooks: [cmd("Stop")] },
|
|
256
|
+
};
|
|
257
|
+
for (const [event, group] of Object.entries(ours)) {
|
|
258
|
+
const kept = (hooks[event] ?? []).filter((g) => !(g.hooks ?? []).some((h) => typeof h.command === "string" && h.command.includes(CODEX_HOOK_MARK)));
|
|
259
|
+
hooks[event] = [...kept, group];
|
|
260
|
+
}
|
|
261
|
+
return { ...doc, hooks };
|
|
262
|
+
}
|
package/dist/help.js
CHANGED
|
@@ -104,6 +104,8 @@ Agent commands
|
|
|
104
104
|
the watchers on it; hooks, repo instructions and watchers all run its path
|
|
105
105
|
bschat doctor every bschat on this machine and who runs which (PATH, npx caches, this repo's
|
|
106
106
|
hook and instructions, each watcher's plist versus what launchd runs), with fixes
|
|
107
|
+
bschat codex hooks | bind <thread-id> | sync install bschat's Codex hooks (~/.codex/hooks.json) and say which thread is you
|
|
108
|
+
bschat antigravity bind <conversation-id> | sync which Antigravity conversation is you, for its hook (watchers bind theirs)
|
|
107
109
|
bschat wake ack [<n>] acknowledge your watcher's wake-up #n first thing (no n: the one pending here)
|
|
108
110
|
Liveness: the watcher reports harness activity (Codex rollout / Antigravity conversation
|
|
109
111
|
found from the session id in --exec, or --activity <file-or-dir>) so you are not shown
|
package/dist/index.js
CHANGED
|
@@ -10,6 +10,8 @@ import { watch } from "./commands/watch.js";
|
|
|
10
10
|
import { feedback } from "./commands/feedback.js";
|
|
11
11
|
import { goal } from "./commands/goal.js";
|
|
12
12
|
import { wake } from "./commands/wake.js";
|
|
13
|
+
import { antigravity } from "./commands/antigravity.js";
|
|
14
|
+
import { codex } from "./commands/codex.js";
|
|
13
15
|
import { doctor, install } from "./commands/install.js";
|
|
14
16
|
import { warnIfForeignCopy } from "./install.js";
|
|
15
17
|
import { processCmd, record, wu, milestones, milestone, lock, askOperator } from "./commands/wu.js";
|
|
@@ -49,6 +51,8 @@ const COMMANDS = {
|
|
|
49
51
|
feedback,
|
|
50
52
|
goal,
|
|
51
53
|
wake,
|
|
54
|
+
antigravity,
|
|
55
|
+
codex,
|
|
52
56
|
install,
|
|
53
57
|
doctor,
|
|
54
58
|
wu,
|
package/dist/sessions.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { configRoot } from "./config.js";
|
|
5
|
+
import { CliError } from "./errors.js";
|
|
6
|
+
import { watcherPlists } from "./install.js";
|
|
7
|
+
const file = (kind) => join(configRoot(), `${kind}.json`);
|
|
8
|
+
const UUID = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/i;
|
|
9
|
+
function read(kind) {
|
|
10
|
+
if (!existsSync(file(kind)))
|
|
11
|
+
return { conversations: {} };
|
|
12
|
+
try {
|
|
13
|
+
return JSON.parse(readFileSync(file(kind), "utf8"));
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
throw new CliError("bad_session_map", `${file(kind)} is not valid JSON`, `Remove it and run: bschat ${kind} sync`);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export function bindSession(kind, id, ref) {
|
|
20
|
+
if (!UUID.test(id))
|
|
21
|
+
throw new CliError("usage", `"${id}" is not ${kind === "codex" ? "a Codex thread" : "an Antigravity conversation"} id (a UUID)`);
|
|
22
|
+
const m = read(kind);
|
|
23
|
+
m.conversations[id.toLowerCase()] = ref;
|
|
24
|
+
mkdirSync(dirname(file(kind)), { recursive: true });
|
|
25
|
+
writeFileSync(file(kind), JSON.stringify(m, null, 2) + "\n");
|
|
26
|
+
}
|
|
27
|
+
export function sessionAgent(kind, id) {
|
|
28
|
+
return read(kind).conversations[id.toLowerCase()] ?? null;
|
|
29
|
+
}
|
|
30
|
+
export const bindConversation = (id, ref) => bindSession("antigravity", id, ref);
|
|
31
|
+
export const conversationAgent = (id) => sessionAgent("antigravity", id);
|
|
32
|
+
/** The Codex thread a wake command addresses (`codex queue --thread <id>`), if any. */
|
|
33
|
+
export function threadOfExec(exec) {
|
|
34
|
+
return /\bcodex\b/.test(exec) && /--thread\b/.test(exec) ? UUID.exec(exec.slice(exec.indexOf("--thread")))?.[0]?.toLowerCase() ?? null : null;
|
|
35
|
+
}
|
|
36
|
+
/** The harness session a wake command addresses, if it names one. */
|
|
37
|
+
export function sessionOfExec(exec) {
|
|
38
|
+
const a = conversationOfExec(exec);
|
|
39
|
+
if (a)
|
|
40
|
+
return { kind: "antigravity", id: a };
|
|
41
|
+
const c = threadOfExec(exec);
|
|
42
|
+
return c ? { kind: "codex", id: c } : null;
|
|
43
|
+
}
|
|
44
|
+
/** The conversation an Antigravity wake command addresses (`agentapi send-message ... <id>`), if any. */
|
|
45
|
+
export function conversationOfExec(exec) {
|
|
46
|
+
return /agentapi\b/.test(exec) ? UUID.exec(exec)?.[0]?.toLowerCase() ?? null : null;
|
|
47
|
+
}
|
|
48
|
+
/** Binds every session an installed watcher wakes (Antigravity conversation, Codex thread) to the agent it runs as. */
|
|
49
|
+
export function syncFromWatchers() {
|
|
50
|
+
const out = [];
|
|
51
|
+
for (const w of watcherPlists()) {
|
|
52
|
+
const r = spawnSync("plutil", ["-convert", "json", "-o", "-", w.path], { encoding: "utf8" });
|
|
53
|
+
if (r.status !== 0)
|
|
54
|
+
continue;
|
|
55
|
+
const args = JSON.parse(r.stdout).ProgramArguments ?? [];
|
|
56
|
+
const exec = args[args.indexOf("--exec") + 1] ?? "";
|
|
57
|
+
const as = args[args.indexOf("--as") + 1];
|
|
58
|
+
const sess = sessionOfExec(exec);
|
|
59
|
+
if (sess && as && args.includes("--as")) {
|
|
60
|
+
bindSession(sess.kind, sess.id, as);
|
|
61
|
+
out.push({ kind: sess.kind, conversation: sess.id, agent: as });
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return out;
|
|
65
|
+
}
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const CLI_VERSION = "0.12.
|
|
1
|
+
export const CLI_VERSION = "0.12.44";
|