@bunshin-cc/bunshin 0.14.1 → 0.14.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.
Files changed (2) hide show
  1. package/dist/bin.js +62 -11
  2. package/package.json +1 -1
package/dist/bin.js CHANGED
@@ -6956,9 +6956,34 @@ function transcriptIdleMs(transcriptPath, now = Date.now()) {
6956
6956
  return void 0;
6957
6957
  }
6958
6958
  }
6959
- function forkOwnCost(reported, before) {
6959
+ function forkOwnCost(reported, before, estimate) {
6960
+ const diff = reported !== void 0 && before !== void 0 ? reported - before : void 0;
6961
+ const near = (a, b) => a > 0 && b > 0 && a / b < 2 && b / a < 2;
6962
+ if (estimate !== void 0) {
6963
+ if (diff !== void 0 && near(diff, estimate)) return diff;
6964
+ if (reported !== void 0 && near(reported, estimate)) return reported;
6965
+ return estimate;
6966
+ }
6960
6967
  if (reported === void 0) return void 0;
6961
- return before === void 0 ? reported : Math.max(0, reported - before);
6968
+ if (before === void 0) return reported;
6969
+ if (diff < 0) return reported;
6970
+ return diff > 0 ? diff : void 0;
6971
+ }
6972
+ function estimateForkCost(usage, modelUsage, model) {
6973
+ if (!usage || !modelUsage) return void 0;
6974
+ const base = (m) => m.replace(/\[.*\]$/, "");
6975
+ const entries = Object.entries(modelUsage);
6976
+ const entry = entries.find(([m]) => model && base(m) === base(model))?.[1] ?? entries.sort((a, b) => (b[1].costUSD ?? 0) - (a[1].costUSD ?? 0))[0]?.[1];
6977
+ if (!entry?.costUSD) return void 0;
6978
+ const w1h = entry.cacheCreationEphemeral1hInputTokens ?? 0;
6979
+ const w5m = entry.cacheCreationEphemeral5mInputTokens ?? Math.max(0, (entry.cacheCreationInputTokens ?? 0) - w1h);
6980
+ const weighted = (entry.inputTokens ?? 0) * TOKEN_WEIGHT.input + (entry.cacheReadInputTokens ?? 0) * TOKEN_WEIGHT.read + w5m * TOKEN_WEIGHT.write5m + w1h * TOKEN_WEIGHT.write1h + (entry.outputTokens ?? 0) * TOKEN_WEIGHT.output;
6981
+ if (weighted <= 0) return void 0;
6982
+ const own1h = usage.cache_creation?.ephemeral_1h_input_tokens;
6983
+ const ownWrite = usage.cache_creation_input_tokens ?? 0;
6984
+ const ownW = own1h !== void 0 ? own1h * TOKEN_WEIGHT.write1h + Math.max(0, ownWrite - own1h) * TOKEN_WEIGHT.write5m : ownWrite * TOKEN_WEIGHT.write1h;
6985
+ const own = (usage.input_tokens ?? 0) * TOKEN_WEIGHT.input + (usage.cache_read_input_tokens ?? 0) * TOKEN_WEIGHT.read + ownW + (usage.output_tokens ?? 0) * TOKEN_WEIGHT.output;
6986
+ return own > 0 ? own * entry.costUSD / weighted : void 0;
6962
6987
  }
6963
6988
  function cacheHitRate(u) {
6964
6989
  if (!u) return void 0;
@@ -6966,6 +6991,9 @@ function cacheHitRate(u) {
6966
6991
  const total = read + (u.cache_creation_input_tokens ?? 0) + (u.input_tokens ?? 0);
6967
6992
  return total > 0 ? read / total : void 0;
6968
6993
  }
6994
+ function cloudSession(env = process.env) {
6995
+ return env.CLAUDE_CODE_REMOTE === "true";
6996
+ }
6969
6997
  function readForkCache(v2) {
6970
6998
  try {
6971
6999
  return JSON.parse(fs16.readFileSync(v2.p(FORK_CACHE_FILE), "utf8")).forks ?? [];
@@ -6979,10 +7007,28 @@ function recordForkCache(v2, rec) {
6979
7007
  } catch {
6980
7008
  }
6981
7009
  }
7010
+ async function forkRecords(v2, config, fromServer) {
7011
+ const local = readForkCache(v2);
7012
+ if (!fromServer || !canCall(config)) return local;
7013
+ const recent = local.some((r) => r.cloud && !r.cold && Date.now() - Date.parse(r.at) < MISS_HOLD_MS);
7014
+ if (recent) return local;
7015
+ try {
7016
+ const res = await v2api(config, "GET", "/reflections/forks", void 0, { timeoutMs: 4e3 });
7017
+ const remote = (res.forks ?? []).filter((r) => typeof r.at === "string" && typeof r.hitRate === "number").map((r) => ({ ...r, cloud: true }));
7018
+ return [...remote, ...local].sort((a, b) => Date.parse(a.at) - Date.parse(b.at));
7019
+ } catch {
7020
+ return local;
7021
+ }
7022
+ }
6982
7023
  function forkDecision(records, o) {
6983
7024
  if (o.learn) return { fork: true, reason: "asked for (/learn)" };
6984
- if (o.tokens <= o.limit) return { fork: true, reason: "short session" };
6985
7025
  const now = o.now ?? Date.now();
7026
+ if (o.cloud) {
7027
+ const latest2 = [...records].reverse().find((r) => r.cloud && r.mode === "faithful" && !r.cold && now - Date.parse(r.at) < MISS_HOLD_MS);
7028
+ if (latest2 && latest2.hitRate < CACHE_MISS) return { fork: false, reason: `forks of your cloud sessions missed their cache (${Math.round(latest2.hitRate * 100)}% read from cache): ${CLOUD_MISS_WHY}. Digest of the new turns` };
7029
+ if (latest2) return { fork: true, reason: `forks of your cloud sessions read ${Math.round(latest2.hitRate * 100)}% from the session's cache` };
7030
+ }
7031
+ if (o.tokens <= o.limit) return { fork: true, reason: "short session" };
6986
7032
  const latest = [...records].reverse().find((r) => r.mode === "faithful" && !r.cold && r.tokens > o.limit && now - Date.parse(r.at) < MISS_HOLD_MS);
6987
7033
  if (latest && latest.hitRate < CACHE_MISS) return { fork: false, reason: `the last fork here missed the session's cache (${Math.round(latest.hitRate * 100)}% read from cache): digest of the new turns` };
6988
7034
  return { fork: true, reason: latest ? `forks here read ${Math.round(latest.hitRate * 100)}% from the session's cache` : "first fork of a large session here" };
@@ -7040,7 +7086,7 @@ function runClaudeFork(sessionId, prompt, { cwd, model, mode = "faithful", timeo
7040
7086
  env = parseJsonLoose(out);
7041
7087
  }
7042
7088
  if (!env || typeof env !== "object") return finish({ ok: false, error: `fork exited ${code2}: ${(err || out).slice(0, 300)}` });
7043
- const base = { usage: env.usage, costUsd: env.total_cost_usd, forkSessionId: env.session_id };
7089
+ const base = { usage: env.usage, costUsd: env.total_cost_usd, forkSessionId: env.session_id, ...env.modelUsage && typeof env.modelUsage === "object" ? { modelUsage: env.modelUsage } : {} };
7044
7090
  if (env.is_error || typeof env.result !== "string") return finish({ ok: false, ...base, error: `fork error: ${String(env.result ?? err).slice(0, 300)}` });
7045
7091
  finish({ ok: true, text: env.result, ...base });
7046
7092
  });
@@ -7161,7 +7207,8 @@ async function runReflectionV2(store, opts) {
7161
7207
  const call2 = {};
7162
7208
  const sessionTokens = estimateSessionTokens(session.transcriptPath);
7163
7209
  const limit = config.reflect.forkMaxTokens ?? 6e4;
7164
- const decision = forkDecision(readForkCache(v2), { learn: origin === "learn", tokens: sessionTokens, limit });
7210
+ const cloud = cloudSession();
7211
+ const decision = forkDecision(await forkRecords(v2, config, cloud && origin !== "learn"), { learn: origin === "learn", tokens: sessionTokens, limit, cloud });
7165
7212
  const useFork = harness === "claude-code" && decision.fork;
7166
7213
  if (harness === "claude-code") call2.cacheNote = decision.reason;
7167
7214
  if (useFork) {
@@ -7174,7 +7221,7 @@ async function runReflectionV2(store, opts) {
7174
7221
  const idleMs = transcriptIdleMs(session.transcriptPath);
7175
7222
  const cold = idleMs !== void 0 && idleMs > Math.max(config.reflect.cacheTtlMs, 5 * 6e4);
7176
7223
  const fork = await runClaudeFork(opts.sessionId, prompt, { cwd: session.cwd, model, mode });
7177
- fork.costUsd = forkOwnCost(fork.costUsd, before);
7224
+ fork.costUsd = forkOwnCost(fork.costUsd, before, estimateForkCost(fork.usage, fork.modelUsage, model));
7178
7225
  const hitRate = cacheHitRate(fork.usage);
7179
7226
  call2.cacheMode = mode;
7180
7227
  if (hitRate !== void 0) {
@@ -7183,12 +7230,14 @@ async function runReflectionV2(store, opts) {
7183
7230
  if (coldMiss) {
7184
7231
  call2.cacheCold = true;
7185
7232
  call2.cacheNote = `the session's prompt cache had expired (idle ${Math.round(idleMs / 6e4)} min before this fork): not counted as a miss`;
7233
+ } else if (cloud && mode === "faithful" && hitRate < CACHE_MISS) {
7234
+ call2.cacheNote = `missed: ${CLOUD_MISS_WHY}. Your cloud sessions reflect from a digest of their new turns for a week`;
7186
7235
  }
7187
- recordForkCache(v2, { at: (/* @__PURE__ */ new Date()).toISOString(), sessionId: opts.sessionId, mode, tokens: sessionTokens, hitRate, ...fork.costUsd !== void 0 ? { costUsd: fork.costUsd } : {}, ...coldMiss ? { cold: true } : {} });
7188
- if (mode === "faithful" && hitRate < CACHE_MISS && sessionTokens > limit && !coldMiss) {
7236
+ recordForkCache(v2, { at: (/* @__PURE__ */ new Date()).toISOString(), sessionId: opts.sessionId, mode, tokens: sessionTokens, hitRate, ...fork.costUsd !== void 0 ? { costUsd: fork.costUsd } : {}, ...coldMiss ? { cold: true } : {}, ...cloud ? { cloud: true } : {} });
7237
+ if (mode === "faithful" && hitRate < CACHE_MISS && (sessionTokens > limit || cloud) && !coldMiss) {
7189
7238
  const cost = fork.costUsd !== void 0 ? ` ($${fork.costUsd.toFixed(2)})` : "";
7190
7239
  v2.log("v2-reflect-cache-miss", { session: opts.sessionId, hitRate, tokens: sessionTokens, costUsd: fork.costUsd ?? null });
7191
- updateFeed(v2, opts.sessionId, (f) => addEvent(f, "warning", `reflection did not reuse this session's prompt cache${cost}: large sessions here reflect from a digest of their new turns for now`));
7240
+ updateFeed(v2, opts.sessionId, (f) => addEvent(f, "warning", `reflection did not reuse this session's prompt cache${cost}: ${cloud ? "your cloud sessions" : "large sessions here"} reflect from a digest of their new turns for now`));
7192
7241
  }
7193
7242
  }
7194
7243
  Object.assign(call2, {
@@ -7315,7 +7364,7 @@ async function runReflectionV2(store, opts) {
7315
7364
  nothingNew: result.nothingNew,
7316
7365
  ...call2.usage ? { usage: call2.usage } : {},
7317
7366
  ...call2.costUsd !== void 0 ? { costUsd: call2.costUsd } : {},
7318
- ...call2.cacheNote || call2.hitRate !== void 0 ? { cache: { ...call2.cacheMode ? { mode: call2.cacheMode } : {}, ...call2.hitRate !== void 0 ? { hitRate: Math.round(call2.hitRate * 1e3) / 1e3 } : {}, ...call2.cacheNote ? { note: call2.cacheNote } : {}, ...call2.cacheCold ? { cold: true } : {} } } : {},
7367
+ ...call2.cacheNote || call2.hitRate !== void 0 ? { cache: { ...call2.cacheMode ? { mode: call2.cacheMode } : {}, ...call2.hitRate !== void 0 ? { hitRate: Math.round(call2.hitRate * 1e3) / 1e3 } : {}, ...call2.cacheNote ? { note: call2.cacheNote } : {}, ...call2.cacheCold ? { cold: true } : {}, ...cloudSession() ? { cloud: true } : {} } } : {},
7319
7368
  durationMs: Date.now() - Date.parse(startedAt),
7320
7369
  ...result.error ? { error: result.error } : {},
7321
7370
  startedAt,
@@ -7382,7 +7431,7 @@ function learnLines(result, view) {
7382
7431
  return applied ? `captured: ${s.summary ?? summarize(s.op)} \u2192 ${target}` : `proposed (not applied): ${s.summary ?? summarize(s.op)} \u2192 ${target}`;
7383
7432
  });
7384
7433
  }
7385
- var FORK_TIMEOUT_MS, FORK_DENIED_TOOLS, FORK_GUARD_SETTINGS, readSettings, FORK_CACHE_FILE, CACHE_MISS, MISS_HOLD_MS;
7434
+ var FORK_TIMEOUT_MS, FORK_DENIED_TOOLS, FORK_GUARD_SETTINGS, readSettings, TOKEN_WEIGHT, CLOUD_MISS_WHY, FORK_CACHE_FILE, CACHE_MISS, MISS_HOLD_MS;
7386
7435
  var init_reflect3 = __esm({
7387
7436
  "src/v2/reflect.ts"() {
7388
7437
  "use strict";
@@ -7407,6 +7456,8 @@ var init_reflect3 = __esm({
7407
7456
  return {};
7408
7457
  }
7409
7458
  };
7459
+ TOKEN_WEIGHT = { input: 1, read: 0.1, write5m: 1.25, write1h: 2, output: 5 };
7460
+ CLOUD_MISS_WHY = "in claude.ai cloud sessions the host adds instructions to the session's system prompt (an organisation's, for one) that a fork cannot reproduce, so a fork reuses only the tools and the start of the system prompt";
7410
7461
  FORK_CACHE_FILE = "fork-cache.json";
7411
7462
  CACHE_MISS = 0.5;
7412
7463
  MISS_HOLD_MS = 7 * 24 * 36e5;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bunshin-cc/bunshin",
3
- "version": "0.14.1",
3
+ "version": "0.14.2",
4
4
  "description": "Shared memory for company AI agents. Connect Codex, Claude Code, or Pi to Bunshin.",
5
5
  "type": "module",
6
6
  "bin": {