@bunshin-cc/bunshin 0.13.1 → 0.14.1
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/bin.js +62 -26
- package/package.json +1 -1
package/dist/bin.js
CHANGED
|
@@ -3575,6 +3575,16 @@ var init_adopt = __esm({
|
|
|
3575
3575
|
}
|
|
3576
3576
|
});
|
|
3577
3577
|
|
|
3578
|
+
// ../core/src/v2/dream.ts
|
|
3579
|
+
var init_dream = __esm({
|
|
3580
|
+
"../core/src/v2/dream.ts"() {
|
|
3581
|
+
"use strict";
|
|
3582
|
+
init_merge();
|
|
3583
|
+
init_guidelines();
|
|
3584
|
+
init_reconcile();
|
|
3585
|
+
}
|
|
3586
|
+
});
|
|
3587
|
+
|
|
3578
3588
|
// ../core/src/v2/index.ts
|
|
3579
3589
|
var init_v2 = __esm({
|
|
3580
3590
|
"../core/src/v2/index.ts"() {
|
|
@@ -3602,6 +3612,7 @@ var init_v2 = __esm({
|
|
|
3602
3612
|
init_scopes();
|
|
3603
3613
|
init_generalize();
|
|
3604
3614
|
init_adopt();
|
|
3615
|
+
init_dream();
|
|
3605
3616
|
}
|
|
3606
3617
|
});
|
|
3607
3618
|
|
|
@@ -8651,7 +8662,7 @@ async function onSessionEnd(ctx) {
|
|
|
8651
8662
|
}
|
|
8652
8663
|
let action = "none";
|
|
8653
8664
|
const ephemeral = ephemeralHost();
|
|
8654
|
-
if (gate.reflect && config.reflect.enabled && !ephemeral) {
|
|
8665
|
+
if (gate.reflect && config.reflect.enabled && (!ephemeral || config.upload === "header-only")) {
|
|
8655
8666
|
episode.reflected = "edge";
|
|
8656
8667
|
action = "fork";
|
|
8657
8668
|
} else if (gate.worth) action = "cloud";
|
|
@@ -9049,13 +9060,13 @@ function removeBunshinHooks(settings) {
|
|
|
9049
9060
|
else delete out.hooks;
|
|
9050
9061
|
return out;
|
|
9051
9062
|
}
|
|
9052
|
-
function mergeClaudeSettings(settings, { bin = "bunshin", harness = "claude-code", user = false, allTools = false, autoConnect: autoConnect2 = false } = {}) {
|
|
9063
|
+
function mergeClaudeSettings(settings, { bin = "bunshin", harness = "claude-code", user = false, allTools = false, autoConnect: autoConnect2 = false, upload } = {}) {
|
|
9053
9064
|
const base = removeBunshinHooks(settings);
|
|
9054
9065
|
const hooks = { ...base.hooks ?? {} };
|
|
9055
9066
|
for (const { event, arg, timeout, matcher } of harness === "codex" ? CODEX_HOOK_EVENTS : HOOK_EVENTS) {
|
|
9056
9067
|
const groups = Array.isArray(hooks[event]) ? [...hooks[event]] : [];
|
|
9057
9068
|
const hook = bunshinHookEntry(arg, autoConnect2 && arg === "session-start" ? AUTO_CONNECT_SESSION_START_TIMEOUT : timeout, bin);
|
|
9058
|
-
hook.command = `${user ? "BUNSHIN_HOOK_SCOPE=user " : ""}${user && autoConnect2 ? "BUNSHIN_AUTO_CONNECT=1 " : ""}BUNSHIN_HARNESS=${harness} ${hook.command}`;
|
|
9069
|
+
hook.command = `${user ? "BUNSHIN_HOOK_SCOPE=user " : ""}${user && autoConnect2 ? "BUNSHIN_AUTO_CONNECT=1 " : ""}${user && autoConnect2 && upload ? `BUNSHIN_UPLOAD=${upload} ` : ""}BUNSHIN_HARNESS=${harness} ${hook.command}`;
|
|
9059
9070
|
if (harness === "codex") delete hook._bunshin;
|
|
9060
9071
|
const m = matcher && allTools && event === "PreToolUse" ? ALL_TOOLS_MATCHER : matcher;
|
|
9061
9072
|
groups.push(m ? { matcher: m, hooks: [hook] } : { hooks: [hook] });
|
|
@@ -9096,10 +9107,10 @@ export { default } from ${JSON.stringify(pathToFileURL(extension).href)};
|
|
|
9096
9107
|
return { file: file2, installed: 0, changed: false, error: error instanceof Error ? error.message : String(error) };
|
|
9097
9108
|
}
|
|
9098
9109
|
}
|
|
9099
|
-
function installClaudeHooks({ file: file2 = claudeSettingsPath(), bin = "bunshin", remove = false, harness = "claude-code", user = false, allTools = false, autoConnect: autoConnect2 = false } = {}) {
|
|
9110
|
+
function installClaudeHooks({ file: file2 = claudeSettingsPath(), bin = "bunshin", remove = false, harness = "claude-code", user = false, allTools = false, autoConnect: autoConnect2 = false, upload } = {}) {
|
|
9100
9111
|
try {
|
|
9101
9112
|
const before = fileExists(file2) ? JSON.parse(fs22.readFileSync(file2, "utf8")) : {};
|
|
9102
|
-
const after = remove ? removeBunshinHooks(before) : mergeClaudeSettings(before, { bin, harness, user, allTools, autoConnect: autoConnect2 });
|
|
9113
|
+
const after = remove ? removeBunshinHooks(before) : mergeClaudeSettings(before, { bin, harness, user, allTools, autoConnect: autoConnect2, ...upload ? { upload } : {} });
|
|
9103
9114
|
const changed = JSON.stringify(before) !== JSON.stringify(after);
|
|
9104
9115
|
if (changed) {
|
|
9105
9116
|
ensureDir(path18.dirname(file2));
|
|
@@ -9362,6 +9373,7 @@ var setup_exports = {};
|
|
|
9362
9373
|
__export(setup_exports, {
|
|
9363
9374
|
HARNESS_NAMES: () => HARNESS_NAMES,
|
|
9364
9375
|
HOSTED_API: () => HOSTED_API,
|
|
9376
|
+
UPLOAD_MODES: () => UPLOAD_MODES,
|
|
9365
9377
|
apiAddress: () => apiAddress,
|
|
9366
9378
|
askSources: () => askSources,
|
|
9367
9379
|
autoConnect: () => autoConnect,
|
|
@@ -9521,6 +9533,7 @@ ${profile.userId}`).digest("hex");
|
|
|
9521
9533
|
store.saveConfig({
|
|
9522
9534
|
...base,
|
|
9523
9535
|
...sources ? { sources } : {},
|
|
9536
|
+
...opts.upload ? { upload: opts.upload } : {},
|
|
9524
9537
|
...profile,
|
|
9525
9538
|
apiKey: void 0,
|
|
9526
9539
|
credentialFile,
|
|
@@ -9568,7 +9581,7 @@ function setupEnvironment(opts) {
|
|
|
9568
9581
|
const bin = opts.bin ?? hookBinary(opts.harness);
|
|
9569
9582
|
const claudeDir = path19.dirname(claudeSettingsPath());
|
|
9570
9583
|
return {
|
|
9571
|
-
hooks: installClaudeHooks({ bin, harness: opts.harness, user: true, autoConnect: true }),
|
|
9584
|
+
hooks: installClaudeHooks({ bin, harness: opts.harness, user: true, autoConnect: true, ...opts.upload ? { upload: opts.upload } : {} }),
|
|
9572
9585
|
permissions: installClaudePermissions(),
|
|
9573
9586
|
mcp: installUserMcp({ launcher: mcpLauncher(bin), ...opts.claude ? { claude: opts.claude } : {} }),
|
|
9574
9587
|
learnCommand: installLearnCommand({ file: path19.join(claudeDir, "commands", "learn.md") }),
|
|
@@ -9592,13 +9605,14 @@ async function autoConnect(cwd, env = process.env) {
|
|
|
9592
9605
|
const root = repositoryRoot(cwd);
|
|
9593
9606
|
if (!root || root === path19.resolve(homeDir()) || root === path19.parse(root).root) return { connected: false, reason: "not-a-repository" };
|
|
9594
9607
|
try {
|
|
9595
|
-
const
|
|
9608
|
+
const upload = UPLOAD_MODES.find((m) => m === env.BUNSHIN_UPLOAD);
|
|
9609
|
+
const result = await setupProject({ cwd: root, apiKey, harness, noHooks: true, hooks: "user", ...env.BUNSHIN_API_URL ? { apiUrl: env.BUNSHIN_API_URL } : {}, ...upload ? { upload } : {} });
|
|
9596
9610
|
return { connected: true, root, profile: result.profile };
|
|
9597
9611
|
} catch (error) {
|
|
9598
9612
|
return { connected: false, reason: "failed", error: error instanceof Error ? error.message : String(error) };
|
|
9599
9613
|
}
|
|
9600
9614
|
}
|
|
9601
|
-
var shellQuote, HARNESS_NAMES;
|
|
9615
|
+
var shellQuote, HARNESS_NAMES, UPLOAD_MODES;
|
|
9602
9616
|
var init_setup = __esm({
|
|
9603
9617
|
"src/setup.ts"() {
|
|
9604
9618
|
"use strict";
|
|
@@ -9609,6 +9623,7 @@ var init_setup = __esm({
|
|
|
9609
9623
|
init_trust();
|
|
9610
9624
|
shellQuote = (value) => `'${value.replace(/'/g, `'"'"'`)}'`;
|
|
9611
9625
|
HARNESS_NAMES = { "claude-code": "Claude Code", codex: "Codex", pi: "Pi" };
|
|
9626
|
+
UPLOAD_MODES = ["redacted", "header-only", "full"];
|
|
9612
9627
|
}
|
|
9613
9628
|
});
|
|
9614
9629
|
|
|
@@ -9656,8 +9671,13 @@ async function handleHook(rawEvent, input, opts = {}) {
|
|
|
9656
9671
|
if (isChild()) return {};
|
|
9657
9672
|
const cwd = opts.cwd ?? input.cwd ?? process.cwd();
|
|
9658
9673
|
let store = opts.store !== void 0 ? opts.store : Store.discover(cwd);
|
|
9674
|
+
let connected;
|
|
9659
9675
|
if (!store && opts.store === void 0 && event === "session-start" && process.env.BUNSHIN_AUTO_CONNECT === "1" && !deferToProject(cwd)) {
|
|
9660
|
-
|
|
9676
|
+
const r = await connectHere(cwd);
|
|
9677
|
+
if ("problem" in r) return r.problem ? { systemMessage: `\u25C6 Bunshin: ${r.problem}`, hookSpecificOutput: { hookEventName: "SessionStart", additionalContext: `## Bunshin
|
|
9678
|
+
Bunshin is installed in this environment but did not connect: ${r.problem} If the person asks about Bunshin, tell them this.` } } : {};
|
|
9679
|
+
store = r.store;
|
|
9680
|
+
connected = r.connected;
|
|
9661
9681
|
}
|
|
9662
9682
|
if (!store) return {};
|
|
9663
9683
|
const config = store.config();
|
|
@@ -9672,9 +9692,11 @@ async function handleHook(rawEvent, input, opts = {}) {
|
|
|
9672
9692
|
timer = setTimeout(() => resolve({}), Math.max(1, deadline - Date.now()) + 250);
|
|
9673
9693
|
timer.unref?.();
|
|
9674
9694
|
});
|
|
9675
|
-
const out = await Promise.race([work, late]);
|
|
9695
|
+
const out = await Promise.race([work, late]) ?? {};
|
|
9676
9696
|
if (timer) clearTimeout(timer);
|
|
9677
|
-
|
|
9697
|
+
if (connected) out.systemMessage = out.systemMessage ? `\u25C6 Bunshin: ${connected}
|
|
9698
|
+
${out.systemMessage}` : `\u25C6 Bunshin: ${connected}`;
|
|
9699
|
+
return out;
|
|
9678
9700
|
} catch (err) {
|
|
9679
9701
|
try {
|
|
9680
9702
|
store.log("hook-error", { event, session: input.session_id, error: err instanceof Error ? err.message : String(err) });
|
|
@@ -9683,21 +9705,22 @@ async function handleHook(rawEvent, input, opts = {}) {
|
|
|
9683
9705
|
return {};
|
|
9684
9706
|
}
|
|
9685
9707
|
}
|
|
9686
|
-
async function connectHere(cwd) {
|
|
9708
|
+
async function connectHere(cwd, env = process.env) {
|
|
9687
9709
|
try {
|
|
9688
9710
|
const { autoConnect: autoConnect2 } = await Promise.resolve().then(() => (init_setup(), setup_exports));
|
|
9689
|
-
const result = await autoConnect2(cwd);
|
|
9711
|
+
const result = await autoConnect2(cwd, env);
|
|
9690
9712
|
if (!result.connected) {
|
|
9691
|
-
|
|
9692
|
-
if (
|
|
9713
|
+
const problem = result.reason === "no-key" ? "it has no key yet. Add the secret BUNSHIN_API_KEY to this environment (create it in the Bunshin console: Overview \u2192 A cloud environment); new sessions then connect on their own." : result.reason === "failed" ? `it could not connect this repository: ${result.error ?? "unknown error"}` : result.reason === "no-harness" ? "its hooks were installed without an agent. Run bunshin setup --env again in the environment's setup script." : "";
|
|
9714
|
+
if (problem) process.stderr.write(`bunshin: ${problem}
|
|
9693
9715
|
`);
|
|
9694
|
-
return
|
|
9716
|
+
return { problem };
|
|
9695
9717
|
}
|
|
9696
9718
|
const store = Store.discover(cwd);
|
|
9697
|
-
store
|
|
9698
|
-
|
|
9699
|
-
|
|
9700
|
-
|
|
9719
|
+
if (!store) return { problem: "it connected this repository but could not open its store." };
|
|
9720
|
+
store.log("auto-connect", { root: result.root, role: result.profile.role, orgUnit: result.profile.orgUnit });
|
|
9721
|
+
return { store, connected: `connected ${path20.basename(result.root)} to your workspace as ${result.profile.role}${result.profile.orgUnit ? ` (${result.profile.orgUnit})` : ""}. It learns from this session and recalls what the workspace knows.` };
|
|
9722
|
+
} catch (err) {
|
|
9723
|
+
return { problem: `it could not connect this repository: ${err instanceof Error ? err.message : String(err)}` };
|
|
9701
9724
|
}
|
|
9702
9725
|
}
|
|
9703
9726
|
function deferToProject(cwd, env = process.env) {
|
|
@@ -11555,7 +11578,8 @@ async function runOnce(c, args, out, err) {
|
|
|
11555
11578
|
if (!job) break;
|
|
11556
11579
|
const started = Date.now();
|
|
11557
11580
|
const scratch = fs31.mkdtempSync(path27.join(os7.tmpdir(), "bunshin-job-"));
|
|
11558
|
-
|
|
11581
|
+
let usage;
|
|
11582
|
+
const res = await askJson(config, { system: job.system, user: job.prompt, schema: job.schema, enforceSchema: false, timeoutMs: 15 * 6e4, maxTokens: 16e3, cwd: scratch, onUsage: (u) => usage = u }).finally(
|
|
11559
11583
|
() => fs31.rmSync(scratch, { recursive: true, force: true })
|
|
11560
11584
|
);
|
|
11561
11585
|
const model = config.backend === "codex-cli" ? config.codexModel ?? "codex default" : config.backend === "pi-cli" ? config.piModel ?? "pi default" : config.model;
|
|
@@ -11567,7 +11591,7 @@ async function runOnce(c, args, out, err) {
|
|
|
11567
11591
|
if (r2.body.status === "retry" && /no LLM backend|not found|ENOENT/.test(res.error)) break;
|
|
11568
11592
|
continue;
|
|
11569
11593
|
}
|
|
11570
|
-
const r = await call(c, "POST", `/jobs/${job.id}/answer`, { answer: res.value, model, runner: c.name }, 12e4);
|
|
11594
|
+
const r = await call(c, "POST", `/jobs/${job.id}/answer`, { answer: res.value, model, runner: c.name, ...usage ? { usage } : {} }, 12e4);
|
|
11571
11595
|
out(`${job.id} ${job.kind}: ${r.body.status}${r.body.error ? ` (${r.body.error})` : ""} in ${Math.round((Date.now() - started) / 1e3)}s via ${res.backend}
|
|
11572
11596
|
`);
|
|
11573
11597
|
if (r.body.status === "done" || r.body.status === "stale") done++;
|
|
@@ -11583,7 +11607,7 @@ function effortOf(job) {
|
|
|
11583
11607
|
const tokens = Math.round(chars / 4);
|
|
11584
11608
|
const minutes = Math.max(2, Math.round(2 + tokens / 15e3));
|
|
11585
11609
|
const proposals = (job.prompt.match(/^### g_\w+/gm) ?? []).length;
|
|
11586
|
-
const what = job.kind === "reflect" ? "read every [USER] message and each failed or denied result first; most sessions teach nothing new, and at most 4 lessons are kept" : job.kind === "merge" ? `${proposals} proposal${proposals === 1 ? "" : "s"} against one document: decide each, edit only what the applied ones need` : job.kind === "generalize" ? `${(job.prompt.match(/^<document /gm) ?? []).length} documents from sibling scopes: write once at the parent what at least two of them share, and trim it out of them; "nothing_shared" is a normal answer` : `${proposals} proposed new document${proposals === 1 ? "" : "s"}: combine, fold into existing ones, or reject`;
|
|
11610
|
+
const what = job.kind === "reflect" ? "read every [USER] message and each failed or denied result first; most sessions teach nothing new, and at most 4 lessons are kept" : job.kind === "merge" ? `${proposals} proposal${proposals === 1 ? "" : "s"} against one document: decide each, edit only what the applied ones need` : job.kind === "dream" ? `${proposals} proposal${proposals === 1 ? "" : "s"} that would wait for a person: integrate (as is, reworded, or into the document it belongs in), reject, or leave it for a person; check each against its session, and add rather than overwrite` : job.kind === "generalize" ? `${(job.prompt.match(/^<document /gm) ?? []).length} documents from sibling scopes: write once at the parent what at least two of them share, and trim it out of them; "nothing_shared" is a normal answer` : `${proposals} proposed new document${proposals === 1 ? "" : "s"}: combine, fold into existing ones, or reject`;
|
|
11587
11611
|
return `${chars.toLocaleString("en")} chars (~${tokens.toLocaleString("en")} tokens); about ${minutes} min. ${what}.`;
|
|
11588
11612
|
}
|
|
11589
11613
|
function jobIndex(text2) {
|
|
@@ -12071,7 +12095,7 @@ var init_runner = __esm({
|
|
|
12071
12095
|
init_trust();
|
|
12072
12096
|
init_version();
|
|
12073
12097
|
DEFAULT_API = "https://api.bunshin.cc";
|
|
12074
|
-
KINDS2 = ["reflect", "merge", "create", "generalize"];
|
|
12098
|
+
KINDS2 = ["reflect", "merge", "create", "generalize", "dream"];
|
|
12075
12099
|
str5 = (v) => typeof v === "string" && v.trim() ? v.trim() : void 0;
|
|
12076
12100
|
LAUNCHD_LABEL = "cc.bunshin.runner";
|
|
12077
12101
|
CRON_MARK = "# bunshin-runner";
|
|
@@ -13329,7 +13353,8 @@ async function main(argv, opts = {}) {
|
|
|
13329
13353
|
const harness = str7(args.flags.harness) ?? "claude-code";
|
|
13330
13354
|
if (harness !== "claude-code") throw new Error("--env supports --harness claude-code for now.");
|
|
13331
13355
|
const { setupEnvironment: setupEnvironment2 } = await Promise.resolve().then(() => (init_setup(), setup_exports));
|
|
13332
|
-
const
|
|
13356
|
+
const upload = transcriptsFlag(args);
|
|
13357
|
+
const result = setupEnvironment2({ harness, ...upload ? { upload } : {} });
|
|
13333
13358
|
if (result.hooks.error) throw new Error(`hooks could not be configured in ${result.hooks.file}: ${result.hooks.error}`);
|
|
13334
13359
|
out(`${result.hooks.installed} hooks configured in ${result.hooks.file}: each git repository connects on its first session.
|
|
13335
13360
|
`);
|
|
@@ -13343,6 +13368,7 @@ async function main(argv, opts = {}) {
|
|
|
13343
13368
|
if (command.error) err(`! ${path31.basename(command.file, ".md")} not installed at ${command.file}: ${command.error}
|
|
13344
13369
|
`);
|
|
13345
13370
|
}
|
|
13371
|
+
if (upload === "header-only") out("Transcripts stay in the container: sessions upload what they learned and a header, and reflect here.\n");
|
|
13346
13372
|
out("No key is needed now. Give sessions BUNSHIN_API_KEY as an environment variable; a repository with no `.bunshin/` store is connected when a session starts in it.\n");
|
|
13347
13373
|
return 0;
|
|
13348
13374
|
} catch (error) {
|
|
@@ -13383,12 +13409,14 @@ async function main(argv, opts = {}) {
|
|
|
13383
13409
|
const exclude = str7(args.flags.exclude);
|
|
13384
13410
|
const listed = (v) => v.split(",").map((x) => x.trim().replace(/\/+$/, "")).filter(Boolean);
|
|
13385
13411
|
out("Connecting to Bunshin\u2026\n");
|
|
13412
|
+
const upload = transcriptsFlag(args);
|
|
13386
13413
|
const result = await setupProject2({
|
|
13387
13414
|
cwd,
|
|
13388
13415
|
harness,
|
|
13389
13416
|
apiKey: key,
|
|
13390
13417
|
apiUrl,
|
|
13391
13418
|
noHooks: bool5(args.flags["no-hooks"]),
|
|
13419
|
+
...upload ? { upload } : {},
|
|
13392
13420
|
...hooksFlag ? { hooks: hooksFlag } : {},
|
|
13393
13421
|
...bool5(args.flags.all) ? { sources: null } : only || exclude ? { sources: { ...only ? { only: listed(only) } : {}, ...exclude ? { exclude: listed(exclude) } : {} } } : {}
|
|
13394
13422
|
});
|
|
@@ -14000,6 +14028,13 @@ async function cmdConflict(store, args, out, err) {
|
|
|
14000
14028
|
}
|
|
14001
14029
|
return 0;
|
|
14002
14030
|
}
|
|
14031
|
+
function transcriptsFlag(args) {
|
|
14032
|
+
const v = str7(args.flags.transcripts);
|
|
14033
|
+
if (v === void 0) return void 0;
|
|
14034
|
+
if (v === "local" || v === "header-only") return "header-only";
|
|
14035
|
+
if (v === "redacted" || v === "full") return v;
|
|
14036
|
+
throw new Error("--transcripts must be local (keep them on this machine), redacted (the default) or full");
|
|
14037
|
+
}
|
|
14003
14038
|
async function signIn(apiUrl, harness, out) {
|
|
14004
14039
|
const { deviceLogin: deviceLogin2, DeviceLoginUnavailable: DeviceLoginUnavailable2 } = await Promise.resolve().then(() => (init_login(), login_exports));
|
|
14005
14040
|
try {
|
|
@@ -14062,13 +14097,14 @@ Usage: bunshin <command> [options]
|
|
|
14062
14097
|
it (no key to copy); later projects connect without asking
|
|
14063
14098
|
--harness codex|claude-code|pi pick the agent when several are installed
|
|
14064
14099
|
--login approve again (another workspace: select it in the console first)
|
|
14100
|
+
--transcripts local keep session transcripts on this machine (upload only what was learned)
|
|
14065
14101
|
--api-url <url> --no-hooks a self-hosted workspace; hooks off
|
|
14066
14102
|
--key-stdin read an agent key from stdin (for automated setup)
|
|
14067
14103
|
--hooks user a folder of projects: hooks in your user settings, so every
|
|
14068
14104
|
session under this folder uses its workspace (default: project)
|
|
14069
14105
|
--only a,b | --exclude c | --all
|
|
14070
14106
|
which of the folder's projects take part (asked when omitted)
|
|
14071
|
-
setup --env [--harness claude-code]
|
|
14107
|
+
setup --env [--harness claude-code] [--transcripts local]
|
|
14072
14108
|
an environment's setup script (a cloud container): no key now;
|
|
14073
14109
|
user hooks connect each git repository on its first session,
|
|
14074
14110
|
with BUNSHIN_API_KEY from the environment
|