@vectorize-io/hindsight-coding-agents 0.3.0 → 0.3.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/dist/installer.js CHANGED
@@ -685,6 +685,7 @@ var HOOK_HARNESSES = {
685
685
  sessionStart: standardSessionStart("claude-code"),
686
686
  prompt: claudePrompt,
687
687
  retain: {
688
+ hostTimeoutSec: 60,
688
689
  harness: "claude-code",
689
690
  parse: (ev) => ({
690
691
  sessionId: ev.session_id,
@@ -703,6 +704,7 @@ var HOOK_HARNESSES = {
703
704
  sessionStart: standardSessionStart("codex"),
704
705
  prompt: codexPrompt,
705
706
  retain: {
707
+ hostTimeoutSec: 60,
706
708
  harness: "codex",
707
709
  parse: (ev) => ({
708
710
  sessionId: ev.session_id,
@@ -731,6 +733,7 @@ var HOOK_HARNESSES = {
731
733
  },
732
734
  prompt: antigravityPrompt,
733
735
  retain: {
736
+ hostTimeoutSec: 30,
734
737
  harness: "antigravity-cli",
735
738
  parse: (ev) => ({
736
739
  sessionId: ev.conversationId,
@@ -759,6 +762,7 @@ var HOOK_HARNESSES = {
759
762
  },
760
763
  prompt: cursorPrompt,
761
764
  retain: {
765
+ hostTimeoutSec: 30,
762
766
  harness: "cursor-cli",
763
767
  parse: (ev) => ({
764
768
  sessionId: ev.conversation_id ?? ev.session_id,
@@ -792,6 +796,7 @@ var HOOK_HARNESSES = {
792
796
  },
793
797
  prompt: copilotPrompt,
794
798
  retain: {
799
+ hostTimeoutSec: 60,
795
800
  harness: "copilot-cli",
796
801
  parse: (ev) => ({
797
802
  sessionId: ev.sessionId,
@@ -820,6 +825,7 @@ var HOOK_HARNESSES = {
820
825
  },
821
826
  prompt: devinPrompt,
822
827
  retain: {
828
+ hostTimeoutSec: 60,
823
829
  harness: "devin-cli",
824
830
  parse: (ev) => {
825
831
  const sessionId = ev.session_id;
@@ -859,6 +865,7 @@ var HOOK_HARNESSES = {
859
865
  })
860
866
  },
861
867
  retain: {
868
+ hostTimeoutSec: 60,
862
869
  harness: "grok-build",
863
870
  parse: (ev) => ({
864
871
  sessionId: ev.sessionId,
package/dist/kilo.js CHANGED
@@ -75,18 +75,32 @@ function gitProjectName(directory, resolveWorktrees) {
75
75
  }
76
76
  return dirName(directory);
77
77
  }
78
+ function configuredDir(dir) {
79
+ const expanded = dir === "~" || dir.startsWith("~/") ? join(homedir(), dir.slice(1)) : dir;
80
+ return normalize(expanded).replace(new RegExp(`\\${sep}+$`), "");
81
+ }
82
+ function isWithin(directory, configured) {
83
+ return directory === configured || directory.startsWith(configured + sep);
84
+ }
78
85
  function mapLookup(map3, directory) {
79
86
  const cwd = normalize(directory);
80
87
  let best;
81
88
  for (const [dir, bank] of Object.entries(map3)) {
82
- const expanded = dir === "~" || dir.startsWith("~/") ? join(homedir(), dir.slice(1)) : dir;
83
- const p = normalize(expanded).replace(new RegExp(`\\${sep}+$`), "");
84
- if (cwd === p || cwd.startsWith(p + sep)) {
89
+ const p = configuredDir(dir);
90
+ if (isWithin(cwd, p)) {
85
91
  if (!best || p.length > best.len) best = { len: p.length, bank };
86
92
  }
87
93
  }
88
94
  return best?.bank;
89
95
  }
96
+ function isOptedIn(config3, directory) {
97
+ if (!config3.optInOnly) return true;
98
+ if (!directory) return false;
99
+ const cwd = normalize(directory);
100
+ if ((config3.optInPaths ?? []).some((dir) => dir && isWithin(cwd, configuredDir(dir))))
101
+ return true;
102
+ return Boolean(config3.mapPathToBank && mapLookup(config3.mapPathToBank, directory));
103
+ }
90
104
  function deriveBankId(config3, directory, harness = "coding") {
91
105
  const mapped = directory && config3.mapPathToBank ? mapLookup(config3.mapPathToBank, directory) : void 0;
92
106
  if (mapped) return mapped;
@@ -164,6 +178,9 @@ function resolveConfig(raw = {}) {
164
178
  bankIdTemplate: raw.bankIdTemplate,
165
179
  mapPathToBank: raw.mapPathToBank,
166
180
  resolveWorktrees: raw.resolveWorktrees,
181
+ optInOnly: raw.optInOnly ?? false,
182
+ // Same shape as retainTags: a config typo must not become a path that silently approves nothing.
183
+ optInPaths: Array.isArray(raw.optInPaths) ? raw.optInPaths.filter((p) => typeof p === "string" && p.trim() !== "") : [],
167
184
  harness: raw.harness ?? "opencode",
168
185
  disabled: raw.disabled ?? false,
169
186
  retainSessions: raw.retainSessions ?? true,
@@ -224,6 +241,8 @@ var ENV_KEYS = {
224
241
  dynamicBankId: "HINDSIGHT_DYNAMIC_BANK_ID",
225
242
  bankIdTemplate: "HINDSIGHT_BANK_ID_TEMPLATE",
226
243
  resolveWorktrees: "HINDSIGHT_RESOLVE_WORKTREES",
244
+ optInOnly: "HINDSIGHT_OPT_IN_ONLY",
245
+ optInPaths: "HINDSIGHT_OPT_IN_PATHS",
227
246
  harness: "HINDSIGHT_HARNESS",
228
247
  disabled: "HINDSIGHT_DISABLED",
229
248
  retainSessions: "HINDSIGHT_RETAIN_SESSIONS",
@@ -246,13 +265,14 @@ var ENV_KEYS = {
246
265
  var ENV_BOOLEANS = /* @__PURE__ */ new Set([
247
266
  "dynamicBankId",
248
267
  "resolveWorktrees",
268
+ "optInOnly",
249
269
  "disabled",
250
270
  "retainSessions",
251
271
  "autoReflect",
252
272
  "autoSeed",
253
273
  "codebaseSurvey"
254
274
  ]);
255
- var ENV_LISTS = /* @__PURE__ */ new Set(["retainTags"]);
275
+ var ENV_LISTS = /* @__PURE__ */ new Set(["retainTags", "optInPaths"]);
256
276
  var ENV_NUMBERS = /* @__PURE__ */ new Set([
257
277
  "apiPort",
258
278
  "daemonIdleTimeout",
@@ -296,9 +316,15 @@ var BANK_OVERRIDE_EXCLUDED = [
296
316
  "mapPathToBank",
297
317
  "dynamicBankId",
298
318
  "resolveWorktrees",
319
+ // Approval is decided BEFORE the bank is resolved, so a `banks.<id>` section naming these could
320
+ // only ever arrive too late to matter — strip them rather than let them read as effective.
321
+ "optInOnly",
322
+ "optInPaths",
299
323
  "harness"
300
324
  ];
301
- function applyBankConfig(cfg, resolvedId) {
325
+ function applyBankConfig(cfg, resolvedId, directory) {
326
+ if (directory !== void 0 && !isOptedIn(cfg, directory))
327
+ return { cfg: { ...cfg, disabled: true }, bankId: resolvedId };
302
328
  const section = cfg.banks[resolvedId];
303
329
  if (!section) return { cfg, bankId: resolvedId };
304
330
  const safe = { ...section };
@@ -16048,10 +16074,10 @@ async function supportsAppend(client) {
16048
16074
  return false;
16049
16075
  }
16050
16076
  }
16077
+ var DEFAULT_RETRY_WINDOW_MS = 6e4;
16051
16078
  var RETAIN_RETRY_ATTEMPTS = 2;
16052
- var RETAIN_RETRY_BUDGET_MS = 6e3;
16053
- async function submitWithRetry(send, isStillNewest) {
16054
- let spent = 0;
16079
+ var REQUEST_BUDGET_MS = 15e3;
16080
+ async function submitWithRetry(send, isStillNewest, retryUntil) {
16055
16081
  for (let attempt = 0; ; attempt++) {
16056
16082
  try {
16057
16083
  return await send();
@@ -16059,8 +16085,7 @@ async function submitWithRetry(send, isStillNewest) {
16059
16085
  const limited = e instanceof RateLimitedError;
16060
16086
  if (!limited || attempt >= RETAIN_RETRY_ATTEMPTS || !isStillNewest()) throw e;
16061
16087
  const wait = e.retryAfterMs || 1e3;
16062
- if (wait > RETAIN_RETRY_BUDGET_MS - spent) throw e;
16063
- spent += wait;
16088
+ if (Date.now() + wait + REQUEST_BUDGET_MS > retryUntil) throw e;
16064
16089
  await sleep(wait);
16065
16090
  }
16066
16091
  }
@@ -16080,14 +16105,24 @@ function serialize(cursors, sessionId, write2) {
16080
16105
  }
16081
16106
  async function retainLiveSession(client, sessionId, turns, startTs, harness, opts = {}) {
16082
16107
  const cursors = opts.cursors;
16083
- if (!cursors) return writeSession(client, sessionId, turns, startTs, harness, opts.stamp);
16108
+ if (!cursors)
16109
+ return writeSession(
16110
+ client,
16111
+ sessionId,
16112
+ turns,
16113
+ startTs,
16114
+ harness,
16115
+ opts.stamp,
16116
+ void 0,
16117
+ opts.retryUntil
16118
+ );
16084
16119
  return serialize(
16085
16120
  cursors,
16086
16121
  sessionId,
16087
- () => writeSession(client, sessionId, turns, startTs, harness, opts.stamp, cursors)
16122
+ () => writeSession(client, sessionId, turns, startTs, harness, opts.stamp, cursors, opts.retryUntil)
16088
16123
  );
16089
16124
  }
16090
- async function writeSession(client, sessionId, turns, startTs, harness, stamp, cursors) {
16125
+ async function writeSession(client, sessionId, turns, startTs, harness, stamp, cursors, retryUntil = Date.now() + DEFAULT_RETRY_WINDOW_MS) {
16091
16126
  const refId = `conversation:${sessionId}`;
16092
16127
  const appendSupported = Boolean(cursors) && await supportsAppend(client);
16093
16128
  const plan = planRetain(turns, cursors?.read(sessionId), { appendSupported, bank: client.bank });
@@ -16139,7 +16174,8 @@ ${content}`),
16139
16174
  if (!cursors) return true;
16140
16175
  const now = cursors.read(sessionId);
16141
16176
  return now?.turns === claimed.turns && now?.fingerprint === claimed.fingerprint;
16142
- }
16177
+ },
16178
+ retryUntil
16143
16179
  );
16144
16180
  cursors?.write(sessionId, next);
16145
16181
  }
@@ -29306,7 +29342,11 @@ function createPluginEntry(harness) {
29306
29342
  const projectDir = resolveProjectDirectory(input);
29307
29343
  let cfg = loadConfig({ harness });
29308
29344
  if (cfg.disabled) return {};
29309
- const resolved = applyBankConfig(cfg, deriveBankId(cfg, projectDir || process.cwd(), harness));
29345
+ const resolved = applyBankConfig(
29346
+ cfg,
29347
+ deriveBankId(cfg, projectDir || process.cwd(), harness),
29348
+ projectDir || process.cwd()
29349
+ );
29310
29350
  cfg = resolved.cfg;
29311
29351
  const bankId = resolved.bankId;
29312
29352
  if (cfg.disabled) return {};
@@ -30959,8 +30959,8 @@ var StdioServerTransport = class {
30959
30959
 
30960
30960
  // src/core/config.ts
30961
30961
  import { readFileSync } from "fs";
30962
- import { homedir } from "os";
30963
- import { join as join2 } from "path";
30962
+ import { homedir as homedir2 } from "os";
30963
+ import { join as join3 } from "path";
30964
30964
 
30965
30965
  // src/core/seed.ts
30966
30966
  import { spawn as realSpawn } from "child_process";
@@ -30968,8 +30968,117 @@ import { dirname, join } from "path";
30968
30968
  import { fileURLToPath } from "url";
30969
30969
  var DEFAULT_SEED_LIMIT = 300;
30970
30970
 
30971
+ // src/core/bank.ts
30972
+ import { execFileSync } from "child_process";
30973
+ import { existsSync } from "fs";
30974
+ import { homedir } from "os";
30975
+ import { basename, dirname as dirname2, join as join2, normalize, sep } from "path";
30976
+
30977
+ // src/core/template.ts
30978
+ var PLACEHOLDER = /\{([a-zA-Z_]+)\}/g;
30979
+ function applyTemplate(template, resolvers, what) {
30980
+ return template.replace(PLACEHOLDER, (_, name) => {
30981
+ const resolve2 = resolvers[name];
30982
+ if (!resolve2) {
30983
+ console.error(
30984
+ `hindsight: unknown ${what} placeholder "{${name}}" \u2014 valid: ` + Object.keys(resolvers).sort().map((k) => `{${k}}`).join(", ")
30985
+ );
30986
+ return "unknown";
30987
+ }
30988
+ return resolve2();
30989
+ });
30990
+ }
30991
+
30992
+ // src/core/bank.ts
30993
+ var DEFAULT_BANK_NAME = "coding";
30994
+ var DEFAULT_TEMPLATE = "coding-agent::{gitProject}";
30995
+ function getProjectRootFromGit(directory) {
30996
+ if (!directory) return null;
30997
+ try {
30998
+ const commonDir = execFileSync(
30999
+ "git",
31000
+ ["rev-parse", "--path-format=absolute", "--git-common-dir"],
31001
+ { cwd: directory, encoding: "utf-8", stdio: ["ignore", "pipe", "ignore"], timeout: 1e3 }
31002
+ ).trim();
31003
+ if (!commonDir) return null;
31004
+ return basename(commonDir) === ".git" ? dirname2(commonDir) : commonDir;
31005
+ } catch {
31006
+ return null;
31007
+ }
31008
+ }
31009
+ function projectNameOf(directory) {
31010
+ return gitProjectName(directory, true);
31011
+ }
31012
+ var PROJECT_ROOT_ENV = ["CLAUDE_PROJECT_DIR"];
31013
+ function nearestExistingDir(directory) {
31014
+ let current2 = directory;
31015
+ while (current2) {
31016
+ if (existsSync(current2)) return current2;
31017
+ const parent = dirname2(current2);
31018
+ if (parent === current2) return "";
31019
+ current2 = parent;
31020
+ }
31021
+ return "";
31022
+ }
31023
+ function dirName(directory) {
31024
+ return directory && basename(directory) || "unknown";
31025
+ }
31026
+ function gitProjectName(directory, resolveWorktrees) {
31027
+ if (resolveWorktrees) {
31028
+ const candidates = [
31029
+ nearestExistingDir(directory),
31030
+ ...PROJECT_ROOT_ENV.map((v) => process.env[v] || "")
31031
+ ];
31032
+ for (const candidate of candidates) {
31033
+ const root = candidate ? getProjectRootFromGit(candidate) : null;
31034
+ if (root) return basename(root);
31035
+ }
31036
+ }
31037
+ return dirName(directory);
31038
+ }
31039
+ function configuredDir(dir) {
31040
+ const expanded = dir === "~" || dir.startsWith("~/") ? join2(homedir(), dir.slice(1)) : dir;
31041
+ return normalize(expanded).replace(new RegExp(`\\${sep}+$`), "");
31042
+ }
31043
+ function isWithin(directory, configured) {
31044
+ return directory === configured || directory.startsWith(configured + sep);
31045
+ }
31046
+ function mapLookup(map2, directory) {
31047
+ const cwd = normalize(directory);
31048
+ let best;
31049
+ for (const [dir, bank] of Object.entries(map2)) {
31050
+ const p = configuredDir(dir);
31051
+ if (isWithin(cwd, p)) {
31052
+ if (!best || p.length > best.len) best = { len: p.length, bank };
31053
+ }
31054
+ }
31055
+ return best?.bank;
31056
+ }
31057
+ function isOptedIn(config2, directory) {
31058
+ if (!config2.optInOnly) return true;
31059
+ if (!directory) return false;
31060
+ const cwd = normalize(directory);
31061
+ if ((config2.optInPaths ?? []).some((dir) => dir && isWithin(cwd, configuredDir(dir))))
31062
+ return true;
31063
+ return Boolean(config2.mapPathToBank && mapLookup(config2.mapPathToBank, directory));
31064
+ }
31065
+ function deriveBankId(config2, directory, harness = "coding") {
31066
+ const mapped = directory && config2.mapPathToBank ? mapLookup(config2.mapPathToBank, directory) : void 0;
31067
+ if (mapped) return mapped;
31068
+ const dynamic = config2.dynamicBankId ?? !config2.bankId;
31069
+ if (!dynamic) return config2.bankId || DEFAULT_BANK_NAME;
31070
+ const resolvers = {
31071
+ harness: () => harness,
31072
+ project: () => dirName(directory),
31073
+ gitProject: () => gitProjectName(directory, config2.resolveWorktrees ?? true),
31074
+ channel: () => process.env.HINDSIGHT_CHANNEL_ID || "default",
31075
+ user: () => process.env.HINDSIGHT_USER_ID || "anonymous"
31076
+ };
31077
+ return applyTemplate(config2.bankIdTemplate || DEFAULT_TEMPLATE, resolvers, "bankIdTemplate");
31078
+ }
31079
+
30971
31080
  // src/core/config.ts
30972
- var CONFIG_PATH = process.env.HINDSIGHT_CONFIG || join2(homedir(), ".hindsight", "coding-agent.json");
31081
+ var CONFIG_PATH = process.env.HINDSIGHT_CONFIG || join3(homedir2(), ".hindsight", "coding-agent.json");
30973
31082
  var DEFAULT_DAEMON_PORT = 9077;
30974
31083
  var DEFAULT_DAEMON_IDLE_TIMEOUT = 300;
30975
31084
  var DEFAULT_DAEMON_PROFILE = "coding-agent";
@@ -30993,6 +31102,9 @@ function resolveConfig(raw = {}) {
30993
31102
  bankIdTemplate: raw.bankIdTemplate,
30994
31103
  mapPathToBank: raw.mapPathToBank,
30995
31104
  resolveWorktrees: raw.resolveWorktrees,
31105
+ optInOnly: raw.optInOnly ?? false,
31106
+ // Same shape as retainTags: a config typo must not become a path that silently approves nothing.
31107
+ optInPaths: Array.isArray(raw.optInPaths) ? raw.optInPaths.filter((p) => typeof p === "string" && p.trim() !== "") : [],
30996
31108
  harness: raw.harness ?? "opencode",
30997
31109
  disabled: raw.disabled ?? false,
30998
31110
  retainSessions: raw.retainSessions ?? true,
@@ -31053,6 +31165,8 @@ var ENV_KEYS = {
31053
31165
  dynamicBankId: "HINDSIGHT_DYNAMIC_BANK_ID",
31054
31166
  bankIdTemplate: "HINDSIGHT_BANK_ID_TEMPLATE",
31055
31167
  resolveWorktrees: "HINDSIGHT_RESOLVE_WORKTREES",
31168
+ optInOnly: "HINDSIGHT_OPT_IN_ONLY",
31169
+ optInPaths: "HINDSIGHT_OPT_IN_PATHS",
31056
31170
  harness: "HINDSIGHT_HARNESS",
31057
31171
  disabled: "HINDSIGHT_DISABLED",
31058
31172
  retainSessions: "HINDSIGHT_RETAIN_SESSIONS",
@@ -31075,13 +31189,14 @@ var ENV_KEYS = {
31075
31189
  var ENV_BOOLEANS = /* @__PURE__ */ new Set([
31076
31190
  "dynamicBankId",
31077
31191
  "resolveWorktrees",
31192
+ "optInOnly",
31078
31193
  "disabled",
31079
31194
  "retainSessions",
31080
31195
  "autoReflect",
31081
31196
  "autoSeed",
31082
31197
  "codebaseSurvey"
31083
31198
  ]);
31084
- var ENV_LISTS = /* @__PURE__ */ new Set(["retainTags"]);
31199
+ var ENV_LISTS = /* @__PURE__ */ new Set(["retainTags", "optInPaths"]);
31085
31200
  var ENV_NUMBERS = /* @__PURE__ */ new Set([
31086
31201
  "apiPort",
31087
31202
  "daemonIdleTimeout",
@@ -31125,9 +31240,15 @@ var BANK_OVERRIDE_EXCLUDED = [
31125
31240
  "mapPathToBank",
31126
31241
  "dynamicBankId",
31127
31242
  "resolveWorktrees",
31243
+ // Approval is decided BEFORE the bank is resolved, so a `banks.<id>` section naming these could
31244
+ // only ever arrive too late to matter — strip them rather than let them read as effective.
31245
+ "optInOnly",
31246
+ "optInPaths",
31128
31247
  "harness"
31129
31248
  ];
31130
- function applyBankConfig(cfg, resolvedId) {
31249
+ function applyBankConfig(cfg, resolvedId, directory) {
31250
+ if (directory !== void 0 && !isOptedIn(cfg, directory))
31251
+ return { cfg: { ...cfg, disabled: true }, bankId: resolvedId };
31131
31252
  const section = cfg.banks[resolvedId];
31132
31253
  if (!section) return { cfg, bankId: resolvedId };
31133
31254
  const safe = { ...section };
@@ -31147,101 +31268,6 @@ function resolvePartial(cfg, patch) {
31147
31268
  return out;
31148
31269
  }
31149
31270
 
31150
- // src/core/bank.ts
31151
- import { execFileSync } from "child_process";
31152
- import { existsSync } from "fs";
31153
- import { homedir as homedir2 } from "os";
31154
- import { basename, dirname as dirname2, join as join3, normalize, sep } from "path";
31155
-
31156
- // src/core/template.ts
31157
- var PLACEHOLDER = /\{([a-zA-Z_]+)\}/g;
31158
- function applyTemplate(template, resolvers, what) {
31159
- return template.replace(PLACEHOLDER, (_, name) => {
31160
- const resolve2 = resolvers[name];
31161
- if (!resolve2) {
31162
- console.error(
31163
- `hindsight: unknown ${what} placeholder "{${name}}" \u2014 valid: ` + Object.keys(resolvers).sort().map((k) => `{${k}}`).join(", ")
31164
- );
31165
- return "unknown";
31166
- }
31167
- return resolve2();
31168
- });
31169
- }
31170
-
31171
- // src/core/bank.ts
31172
- var DEFAULT_BANK_NAME = "coding";
31173
- var DEFAULT_TEMPLATE = "coding-agent::{gitProject}";
31174
- function getProjectRootFromGit(directory) {
31175
- if (!directory) return null;
31176
- try {
31177
- const commonDir = execFileSync(
31178
- "git",
31179
- ["rev-parse", "--path-format=absolute", "--git-common-dir"],
31180
- { cwd: directory, encoding: "utf-8", stdio: ["ignore", "pipe", "ignore"], timeout: 1e3 }
31181
- ).trim();
31182
- if (!commonDir) return null;
31183
- return basename(commonDir) === ".git" ? dirname2(commonDir) : commonDir;
31184
- } catch {
31185
- return null;
31186
- }
31187
- }
31188
- function projectNameOf(directory) {
31189
- return gitProjectName(directory, true);
31190
- }
31191
- var PROJECT_ROOT_ENV = ["CLAUDE_PROJECT_DIR"];
31192
- function nearestExistingDir(directory) {
31193
- let current2 = directory;
31194
- while (current2) {
31195
- if (existsSync(current2)) return current2;
31196
- const parent = dirname2(current2);
31197
- if (parent === current2) return "";
31198
- current2 = parent;
31199
- }
31200
- return "";
31201
- }
31202
- function dirName(directory) {
31203
- return directory && basename(directory) || "unknown";
31204
- }
31205
- function gitProjectName(directory, resolveWorktrees) {
31206
- if (resolveWorktrees) {
31207
- const candidates = [
31208
- nearestExistingDir(directory),
31209
- ...PROJECT_ROOT_ENV.map((v) => process.env[v] || "")
31210
- ];
31211
- for (const candidate of candidates) {
31212
- const root = candidate ? getProjectRootFromGit(candidate) : null;
31213
- if (root) return basename(root);
31214
- }
31215
- }
31216
- return dirName(directory);
31217
- }
31218
- function mapLookup(map2, directory) {
31219
- const cwd = normalize(directory);
31220
- let best;
31221
- for (const [dir, bank] of Object.entries(map2)) {
31222
- const expanded = dir === "~" || dir.startsWith("~/") ? join3(homedir2(), dir.slice(1)) : dir;
31223
- const p = normalize(expanded).replace(new RegExp(`\\${sep}+$`), "");
31224
- if (cwd === p || cwd.startsWith(p + sep)) {
31225
- if (!best || p.length > best.len) best = { len: p.length, bank };
31226
- }
31227
- }
31228
- return best?.bank;
31229
- }
31230
- function deriveBankId(config2, directory, harness = "coding") {
31231
- const mapped = directory && config2.mapPathToBank ? mapLookup(config2.mapPathToBank, directory) : void 0;
31232
- if (mapped) return mapped;
31233
- const dynamic = config2.dynamicBankId ?? !config2.bankId;
31234
- if (!dynamic) return config2.bankId || DEFAULT_BANK_NAME;
31235
- const resolvers = {
31236
- harness: () => harness,
31237
- project: () => dirName(directory),
31238
- gitProject: () => gitProjectName(directory, config2.resolveWorktrees ?? true),
31239
- channel: () => process.env.HINDSIGHT_CHANNEL_ID || "default",
31240
- user: () => process.env.HINDSIGHT_USER_ID || "anonymous"
31241
- };
31242
- return applyTemplate(config2.bankIdTemplate || DEFAULT_TEMPLATE, resolvers, "bankIdTemplate");
31243
- }
31244
-
31245
31271
  // src/core/missions.ts
31246
31272
  var GIT_MISSION = "You are ingesting a single git commit: its message and its full diff. Extract the concrete technical DECISION and the CAUSE/INVARIANT it encodes, bound to the specific code entities (functions, methods, files) and behaviors it changes. Preserve exact identifiers, paths, and literal values verbatim. Preserve the 'REF-ID: <token>' marker verbatim in every fact. Capture both WHAT changed and WHY. Issue/PR references (#123, GH-123, PROJ-123) are load-bearing: keep them VERBATIM in the fact text and emit each as an ENTITY, so a later question about that issue or PR retrieves this decision.";
31247
31273
  var GITLOG_MISSION = "You are ingesting an aggregated block of git commit MESSAGES ONLY (no diffs) \u2014 the project's recent commit-message history, newest first. Extract the project's INITIATIVES, FEATURES, ENHANCEMENTS, and notable changes or THEMES over time \u2014 what the project has been working on and how it has evolved. Do NOT extract per-line code detail (there is no diff to draw it from). Group related commits into a coherent initiative/theme where the messages make that clear; preserve exact identifiers and literal values verbatim when quoting a subject line. Keep issue/PR references (#123, GH-123) VERBATIM and emit each as an ENTITY \u2014 they are how future sessions will ask about this work.";
@@ -32225,7 +32251,7 @@ async function main() {
32225
32251
  const cwd = process.env.HINDSIGHT_MCP_PROJECT_CWD || process.cwd();
32226
32252
  const harness = process.env.HINDSIGHT_MCP_HARNESS || "claude-code";
32227
32253
  const cfg0 = loadConfig({ harness });
32228
- const { cfg, bankId } = applyBankConfig(cfg0, deriveBankId(cfg0, cwd, harness));
32254
+ const { cfg, bankId } = applyBankConfig(cfg0, deriveBankId(cfg0, cwd, harness), cwd);
32229
32255
  const client = new HindsightClient({
32230
32256
  apiUrl: cfg.apiUrl,
32231
32257
  apiToken: cfg.apiToken,
@@ -14586,18 +14586,32 @@ function gitProjectName(directory, resolveWorktrees) {
14586
14586
  }
14587
14587
  return dirName(directory);
14588
14588
  }
14589
+ function configuredDir(dir) {
14590
+ const expanded = dir === "~" || dir.startsWith("~/") ? join(homedir(), dir.slice(1)) : dir;
14591
+ return normalize(expanded).replace(new RegExp(`\\${sep}+$`), "");
14592
+ }
14593
+ function isWithin(directory, configured) {
14594
+ return directory === configured || directory.startsWith(configured + sep);
14595
+ }
14589
14596
  function mapLookup(map2, directory) {
14590
14597
  const cwd = normalize(directory);
14591
14598
  let best;
14592
14599
  for (const [dir, bank] of Object.entries(map2)) {
14593
- const expanded = dir === "~" || dir.startsWith("~/") ? join(homedir(), dir.slice(1)) : dir;
14594
- const p = normalize(expanded).replace(new RegExp(`\\${sep}+$`), "");
14595
- if (cwd === p || cwd.startsWith(p + sep)) {
14600
+ const p = configuredDir(dir);
14601
+ if (isWithin(cwd, p)) {
14596
14602
  if (!best || p.length > best.len) best = { len: p.length, bank };
14597
14603
  }
14598
14604
  }
14599
14605
  return best?.bank;
14600
14606
  }
14607
+ function isOptedIn(config2, directory) {
14608
+ if (!config2.optInOnly) return true;
14609
+ if (!directory) return false;
14610
+ const cwd = normalize(directory);
14611
+ if ((config2.optInPaths ?? []).some((dir) => dir && isWithin(cwd, configuredDir(dir))))
14612
+ return true;
14613
+ return Boolean(config2.mapPathToBank && mapLookup(config2.mapPathToBank, directory));
14614
+ }
14601
14615
  function deriveBankId(config2, directory, harness = "coding") {
14602
14616
  const mapped = directory && config2.mapPathToBank ? mapLookup(config2.mapPathToBank, directory) : void 0;
14603
14617
  if (mapped) return mapped;
@@ -14675,6 +14689,9 @@ function resolveConfig(raw = {}) {
14675
14689
  bankIdTemplate: raw.bankIdTemplate,
14676
14690
  mapPathToBank: raw.mapPathToBank,
14677
14691
  resolveWorktrees: raw.resolveWorktrees,
14692
+ optInOnly: raw.optInOnly ?? false,
14693
+ // Same shape as retainTags: a config typo must not become a path that silently approves nothing.
14694
+ optInPaths: Array.isArray(raw.optInPaths) ? raw.optInPaths.filter((p) => typeof p === "string" && p.trim() !== "") : [],
14678
14695
  harness: raw.harness ?? "opencode",
14679
14696
  disabled: raw.disabled ?? false,
14680
14697
  retainSessions: raw.retainSessions ?? true,
@@ -14735,6 +14752,8 @@ var ENV_KEYS = {
14735
14752
  dynamicBankId: "HINDSIGHT_DYNAMIC_BANK_ID",
14736
14753
  bankIdTemplate: "HINDSIGHT_BANK_ID_TEMPLATE",
14737
14754
  resolveWorktrees: "HINDSIGHT_RESOLVE_WORKTREES",
14755
+ optInOnly: "HINDSIGHT_OPT_IN_ONLY",
14756
+ optInPaths: "HINDSIGHT_OPT_IN_PATHS",
14738
14757
  harness: "HINDSIGHT_HARNESS",
14739
14758
  disabled: "HINDSIGHT_DISABLED",
14740
14759
  retainSessions: "HINDSIGHT_RETAIN_SESSIONS",
@@ -14757,13 +14776,14 @@ var ENV_KEYS = {
14757
14776
  var ENV_BOOLEANS = /* @__PURE__ */ new Set([
14758
14777
  "dynamicBankId",
14759
14778
  "resolveWorktrees",
14779
+ "optInOnly",
14760
14780
  "disabled",
14761
14781
  "retainSessions",
14762
14782
  "autoReflect",
14763
14783
  "autoSeed",
14764
14784
  "codebaseSurvey"
14765
14785
  ]);
14766
- var ENV_LISTS = /* @__PURE__ */ new Set(["retainTags"]);
14786
+ var ENV_LISTS = /* @__PURE__ */ new Set(["retainTags", "optInPaths"]);
14767
14787
  var ENV_NUMBERS = /* @__PURE__ */ new Set([
14768
14788
  "apiPort",
14769
14789
  "daemonIdleTimeout",
@@ -14807,9 +14827,15 @@ var BANK_OVERRIDE_EXCLUDED = [
14807
14827
  "mapPathToBank",
14808
14828
  "dynamicBankId",
14809
14829
  "resolveWorktrees",
14830
+ // Approval is decided BEFORE the bank is resolved, so a `banks.<id>` section naming these could
14831
+ // only ever arrive too late to matter — strip them rather than let them read as effective.
14832
+ "optInOnly",
14833
+ "optInPaths",
14810
14834
  "harness"
14811
14835
  ];
14812
- function applyBankConfig(cfg, resolvedId) {
14836
+ function applyBankConfig(cfg, resolvedId, directory) {
14837
+ if (directory !== void 0 && !isOptedIn(cfg, directory))
14838
+ return { cfg: { ...cfg, disabled: true }, bankId: resolvedId };
14813
14839
  const section = cfg.banks[resolvedId];
14814
14840
  if (!section) return { cfg, bankId: resolvedId };
14815
14841
  const safe = { ...section };
@@ -16047,10 +16073,10 @@ async function supportsAppend(client) {
16047
16073
  return false;
16048
16074
  }
16049
16075
  }
16076
+ var DEFAULT_RETRY_WINDOW_MS = 6e4;
16050
16077
  var RETAIN_RETRY_ATTEMPTS = 2;
16051
- var RETAIN_RETRY_BUDGET_MS = 6e3;
16052
- async function submitWithRetry(send, isStillNewest) {
16053
- let spent = 0;
16078
+ var REQUEST_BUDGET_MS = 15e3;
16079
+ async function submitWithRetry(send, isStillNewest, retryUntil) {
16054
16080
  for (let attempt = 0; ; attempt++) {
16055
16081
  try {
16056
16082
  return await send();
@@ -16058,8 +16084,7 @@ async function submitWithRetry(send, isStillNewest) {
16058
16084
  const limited = e instanceof RateLimitedError;
16059
16085
  if (!limited || attempt >= RETAIN_RETRY_ATTEMPTS || !isStillNewest()) throw e;
16060
16086
  const wait = e.retryAfterMs || 1e3;
16061
- if (wait > RETAIN_RETRY_BUDGET_MS - spent) throw e;
16062
- spent += wait;
16087
+ if (Date.now() + wait + REQUEST_BUDGET_MS > retryUntil) throw e;
16063
16088
  await sleep(wait);
16064
16089
  }
16065
16090
  }
@@ -16079,14 +16104,24 @@ function serialize(cursors, sessionId, write2) {
16079
16104
  }
16080
16105
  async function retainLiveSession(client, sessionId, turns, startTs, harness, opts = {}) {
16081
16106
  const cursors = opts.cursors;
16082
- if (!cursors) return writeSession(client, sessionId, turns, startTs, harness, opts.stamp);
16107
+ if (!cursors)
16108
+ return writeSession(
16109
+ client,
16110
+ sessionId,
16111
+ turns,
16112
+ startTs,
16113
+ harness,
16114
+ opts.stamp,
16115
+ void 0,
16116
+ opts.retryUntil
16117
+ );
16083
16118
  return serialize(
16084
16119
  cursors,
16085
16120
  sessionId,
16086
- () => writeSession(client, sessionId, turns, startTs, harness, opts.stamp, cursors)
16121
+ () => writeSession(client, sessionId, turns, startTs, harness, opts.stamp, cursors, opts.retryUntil)
16087
16122
  );
16088
16123
  }
16089
- async function writeSession(client, sessionId, turns, startTs, harness, stamp, cursors) {
16124
+ async function writeSession(client, sessionId, turns, startTs, harness, stamp, cursors, retryUntil = Date.now() + DEFAULT_RETRY_WINDOW_MS) {
16090
16125
  const refId = `conversation:${sessionId}`;
16091
16126
  const appendSupported = Boolean(cursors) && await supportsAppend(client);
16092
16127
  const plan = planRetain(turns, cursors?.read(sessionId), { appendSupported, bank: client.bank });
@@ -16138,7 +16173,8 @@ ${content}`),
16138
16173
  if (!cursors) return true;
16139
16174
  const now = cursors.read(sessionId);
16140
16175
  return now?.turns === claimed.turns && now?.fingerprint === claimed.fingerprint;
16141
- }
16176
+ },
16177
+ retryUntil
16142
16178
  );
16143
16179
  cursors?.write(sessionId, next);
16144
16180
  }
@@ -16842,7 +16878,7 @@ ${injection}` };
16842
16878
  function createRuntime(repoPath) {
16843
16879
  let cfg = loadConfig({ harness: HARNESS2 });
16844
16880
  if (cfg.disabled) return void 0;
16845
- const resolved = applyBankConfig(cfg, deriveBankId(cfg, repoPath, HARNESS2));
16881
+ const resolved = applyBankConfig(cfg, deriveBankId(cfg, repoPath, HARNESS2), repoPath);
16846
16882
  cfg = resolved.cfg;
16847
16883
  if (cfg.disabled) return void 0;
16848
16884
  const client = new HindsightClient({