@vectorize-io/hindsight-coding-agents 0.3.4 → 0.4.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.
@@ -3,7 +3,7 @@
3
3
  // src/core/config.ts
4
4
  import { readFileSync } from "fs";
5
5
  import { homedir as homedir2 } from "os";
6
- import { join as join3 } from "path";
6
+ import { join as join4 } from "path";
7
7
 
8
8
  // src/core/seed.ts
9
9
  import { spawn as realSpawn } from "child_process";
@@ -87,30 +87,36 @@ function getProjectRootFromGit(directory) {
87
87
  }
88
88
  var PROJECT_ROOT_ENV = ["CLAUDE_PROJECT_DIR"];
89
89
  function nearestExistingDir(directory) {
90
- let current = directory;
91
- while (current) {
92
- if (existsSync(current)) return current;
93
- const parent = dirname2(current);
94
- if (parent === current) return "";
95
- current = parent;
90
+ let current2 = directory;
91
+ while (current2) {
92
+ if (existsSync(current2)) return current2;
93
+ const parent = dirname2(current2);
94
+ if (parent === current2) return "";
95
+ current2 = parent;
96
96
  }
97
97
  return "";
98
98
  }
99
99
  function dirName(directory) {
100
100
  return directory && basename(directory) || "unknown";
101
101
  }
102
- function gitProjectName(directory, resolveWorktrees) {
102
+ function mainWorktreeRoot(directory, sessionRoot = "") {
103
+ const candidates = [
104
+ nearestExistingDir(directory),
105
+ sessionRoot,
106
+ ...PROJECT_ROOT_ENV.map((v) => process.env[v] || "")
107
+ ];
108
+ for (const candidate of candidates) {
109
+ const root = candidate ? getProjectRootFromGit(candidate) : null;
110
+ if (root) return root;
111
+ }
112
+ return null;
113
+ }
114
+ function gitProjectName(directory, resolveWorktrees, sessionRoot = "") {
103
115
  if (resolveWorktrees) {
104
- const candidates = [
105
- nearestExistingDir(directory),
106
- ...PROJECT_ROOT_ENV.map((v) => process.env[v] || "")
107
- ];
108
- for (const candidate of candidates) {
109
- const root = candidate ? getProjectRootFromGit(candidate) : null;
110
- if (root) return basename(root);
111
- }
116
+ const root = mainWorktreeRoot(directory, sessionRoot);
117
+ if (root) return basename(root);
112
118
  }
113
- return dirName(directory);
119
+ return dirName(sessionRoot || directory);
114
120
  }
115
121
  function configuredDir(dir) {
116
122
  const expanded = dir === "~" || dir.startsWith("~/") ? join2(homedir(), dir.slice(1)) : dir;
@@ -130,26 +136,102 @@ function mapLookup(map, directory) {
130
136
  }
131
137
  return best?.bank;
132
138
  }
133
- function deriveBankId(config, directory, harness = "coding") {
134
- const mapped = directory && config.mapPathToBank ? mapLookup(config.mapPathToBank, directory) : void 0;
139
+ function lookupDirectories(config, directory, sessionRoot = "") {
140
+ const directories = [normalize(directory)];
141
+ if (config.resolveWorktrees ?? true) {
142
+ const root = mainWorktreeRoot(directory, sessionRoot);
143
+ const normalizedRoot = root ? normalize(root) : "";
144
+ if (normalizedRoot && normalizedRoot !== directories[0]) directories.push(normalizedRoot);
145
+ }
146
+ return directories;
147
+ }
148
+ function deriveBankId(config, directory, harness = "coding", sessionRoot) {
149
+ const pathMap = config.mapPathToBank;
150
+ const mapped = directory && pathMap ? lookupDirectories(config, directory, sessionRoot).map((candidate) => mapLookup(pathMap, candidate)).find((bank) => bank !== void 0) : void 0;
135
151
  if (mapped) return mapped;
136
152
  const dynamic = config.dynamicBankId ?? !config.bankId;
137
153
  if (!dynamic) return config.bankId || DEFAULT_BANK_NAME;
138
154
  const resolvers = {
139
155
  harness: () => harness,
140
156
  project: () => dirName(directory),
141
- gitProject: () => gitProjectName(directory, config.resolveWorktrees ?? true),
157
+ gitProject: () => gitProjectName(directory, config.resolveWorktrees ?? true, sessionRoot),
142
158
  channel: () => process.env.HINDSIGHT_CHANNEL_ID || "default",
143
159
  user: () => process.env.HINDSIGHT_USER_ID || "anonymous"
144
160
  };
145
161
  return applyTemplate(config.bankIdTemplate || DEFAULT_TEMPLATE, resolvers, "bankIdTemplate");
146
162
  }
147
163
 
164
+ // src/core/log.ts
165
+ import { appendFileSync, mkdirSync } from "fs";
166
+ import { tmpdir } from "os";
167
+ import { dirname as dirname3, join as join3 } from "path";
168
+ var WEIGHT = { debug: 10, info: 20, warn: 30, error: 40 };
169
+ var current = ["debug", "info", "warn", "error"].find(
170
+ (l) => l === process.env.HINDSIGHT_LOG_LEVEL
171
+ ) ?? "info";
172
+ function logFilePath() {
173
+ return process.env.HINDSIGHT_LOG_FILE || join3(tmpdir(), "hindsight-coding-agent", "plugin.log");
174
+ }
175
+ function write(level, scope, msg, extra) {
176
+ if (WEIGHT[level] < WEIGHT[current]) return;
177
+ try {
178
+ const file = logFilePath();
179
+ mkdirSync(dirname3(file), { recursive: true });
180
+ appendFileSync(
181
+ file,
182
+ `${(/* @__PURE__ */ new Date()).toISOString()} ${level.toUpperCase().padEnd(5)} [${scope}] ${msg}` + (extra && Object.keys(extra).length ? ` ${JSON.stringify(extra)}` : "") + "\n"
183
+ );
184
+ } catch {
185
+ }
186
+ }
187
+ var log = {
188
+ debug: (scope, msg, extra) => write("debug", scope, msg, extra),
189
+ info: (scope, msg, extra) => write("info", scope, msg, extra),
190
+ warn: (scope, msg, extra) => write("warn", scope, msg, extra),
191
+ error: (scope, msg, extra) => write("error", scope, msg, extra)
192
+ };
193
+
194
+ // src/core/hindsight.ts
195
+ var DEFAULT_OBSERVATION_SCOPES = "shared";
196
+ var RETRY_AFTER_FLOOR_MS = 10 * 1e3;
197
+ var RETRY_AFTER_CEILING_MS = 60 * 1e3;
198
+
148
199
  // src/core/config.ts
149
- var CONFIG_PATH = process.env.HINDSIGHT_CONFIG || join3(homedir2(), ".hindsight", "coding-agent.json");
200
+ var CONFIG_PATH = process.env.HINDSIGHT_CONFIG || join4(homedir2(), ".hindsight", "coding-agent.json");
150
201
  var DEFAULT_DAEMON_PORT = 9077;
151
- var DEFAULT_DAEMON_IDLE_TIMEOUT = 300;
152
202
  var DEFAULT_DAEMON_PROFILE = "coding-agent";
203
+ function resolvePageTriggerType(raw) {
204
+ if (raw.pageTriggerType === "manual") return "manual";
205
+ if (raw.pageTriggerType === "cron") {
206
+ if (raw.pageTriggerCron?.trim()) return "cron";
207
+ log.warn(
208
+ "config",
209
+ 'pageTriggerType "cron" needs pageTriggerCron (UTC 5-field, e.g. "0 3 * * *") \u2014 falling back to "auto-refresh"'
210
+ );
211
+ }
212
+ return "auto-refresh";
213
+ }
214
+ var DEFAULT_REFLECT_TOOL_TIMEOUT_MS = 33e4;
215
+ var REFLECT_BUDGETS = ["low", "mid", "high"];
216
+ function resolveReflectBudget(raw) {
217
+ const value = raw.reflectBudget;
218
+ if (value === void 0) return "high";
219
+ if (typeof value === "string" && REFLECT_BUDGETS.includes(value))
220
+ return value;
221
+ log.warn("config", `ignoring reflectBudget=${JSON.stringify(value)} \u2014 expected low|mid|high`);
222
+ return "high";
223
+ }
224
+ var OBSERVATION_SCOPE_MODES = ["shared", "combined", "per_tag", "all_combinations"];
225
+ function resolveObservationScopes(raw) {
226
+ const value = raw;
227
+ if (typeof value === "string")
228
+ return OBSERVATION_SCOPE_MODES.includes(value) ? value : DEFAULT_OBSERVATION_SCOPES;
229
+ if (Array.isArray(value)) {
230
+ const scopes = value.filter((scope) => Array.isArray(scope)).map((scope) => scope.filter((t) => typeof t === "string" && t.trim() !== ""));
231
+ if (scopes.length) return scopes;
232
+ }
233
+ return DEFAULT_OBSERVATION_SCOPES;
234
+ }
153
235
  function resolveConfig(raw = {}) {
154
236
  const serverMode = ["cloud", "self-hosted", "daemon"].includes(raw.serverMode) ? raw.serverMode : "cloud";
155
237
  const apiPort = raw.apiPort || DEFAULT_DAEMON_PORT;
@@ -161,7 +243,7 @@ function resolveConfig(raw = {}) {
161
243
  apiUrl: serverMode === "daemon" ? `http://127.0.0.1:${apiPort}` : raw.apiUrl ?? "https://api.hindsight.vectorize.io",
162
244
  apiToken: raw.apiToken || void 0,
163
245
  apiPort,
164
- daemonIdleTimeout: raw.daemonIdleTimeout ?? DEFAULT_DAEMON_IDLE_TIMEOUT,
246
+ daemonIdleTimeout: raw.daemonIdleTimeout,
165
247
  daemonProfile: raw.daemonProfile || DEFAULT_DAEMON_PROFILE,
166
248
  embedVersion: raw.embedVersion || void 0,
167
249
  embedPackagePath: raw.embedPackagePath || void 0,
@@ -176,11 +258,18 @@ function resolveConfig(raw = {}) {
176
258
  harness: raw.harness ?? "opencode",
177
259
  disabled: raw.disabled ?? false,
178
260
  retainSessions: raw.retainSessions ?? true,
179
- // opencode: write back by default (parity with hook-harness Stop)
261
+ // write sessions back by default, every harness
180
262
  maxParallelRetains: raw.maxParallelRetains || 10,
181
263
  reflectTimeoutMs: raw.reflectTimeoutMs || 12e4,
264
+ // Inherit an explicitly-raised reflectTimeoutMs (that is what users reaching for a longer
265
+ // reflect already set), but never let it LOWER the tool below the default — a short window is
266
+ // set to bound the automatic hook, not to cut off a call the agent is waiting on.
267
+ reflectToolTimeoutMs: raw.reflectToolTimeoutMs || Math.max(raw.reflectTimeoutMs || 0, DEFAULT_REFLECT_TOOL_TIMEOUT_MS),
268
+ reflectBudget: resolveReflectBudget(raw),
182
269
  autoReflect: raw.autoReflect ?? true,
183
270
  pageRefreshEveryTurns: raw.pageRefreshEveryTurns || 10,
271
+ pageTriggerType: resolvePageTriggerType(raw),
272
+ pageTriggerCron: raw.pageTriggerCron?.trim() || void 0,
184
273
  autoSeed: raw.autoSeed ?? true,
185
274
  seedLimit: raw.seedLimit || DEFAULT_SEED_LIMIT,
186
275
  codebaseSurvey: raw.codebaseSurvey ?? true,
@@ -194,6 +283,7 @@ function resolveConfig(raw = {}) {
194
283
  retainMetadata: raw.retainMetadata && typeof raw.retainMetadata === "object" ? Object.fromEntries(
195
284
  Object.entries(raw.retainMetadata).filter(([, v]) => typeof v === "string")
196
285
  ) : {},
286
+ observationScopes: resolveObservationScopes(raw.observationScopes),
197
287
  banks: raw.banks && typeof raw.banks === "object" ? raw.banks : {},
198
288
  logLevel: ["debug", "info", "warn", "error"].includes(raw.logLevel) ? raw.logLevel : "info"
199
289
  };
@@ -240,8 +330,12 @@ var ENV_KEYS = {
240
330
  retainSessions: "HINDSIGHT_RETAIN_SESSIONS",
241
331
  maxParallelRetains: "HINDSIGHT_MAX_PARALLEL_RETAINS",
242
332
  reflectTimeoutMs: "HINDSIGHT_REFLECT_TIMEOUT_MS",
333
+ reflectToolTimeoutMs: "HINDSIGHT_REFLECT_TOOL_TIMEOUT_MS",
334
+ reflectBudget: "HINDSIGHT_REFLECT_BUDGET",
243
335
  autoReflect: "HINDSIGHT_AUTO_REFLECT",
244
336
  pageRefreshEveryTurns: "HINDSIGHT_PAGE_REFRESH_EVERY_TURNS",
337
+ pageTriggerType: "HINDSIGHT_PAGE_TRIGGER_TYPE",
338
+ pageTriggerCron: "HINDSIGHT_PAGE_TRIGGER_CRON",
245
339
  autoSeed: "HINDSIGHT_AUTO_SEED",
246
340
  seedLimit: "HINDSIGHT_SEED_LIMIT",
247
341
  codebaseSurvey: "HINDSIGHT_CODEBASE_SURVEY",
@@ -250,6 +344,9 @@ var ENV_KEYS = {
250
344
  surveyRefreshCommits: "HINDSIGHT_SURVEY_REFRESH_COMMITS",
251
345
  logLevel: "HINDSIGHT_LOG_LEVEL",
252
346
  gitIngest: "HINDSIGHT_GIT_INGEST",
347
+ // Scalar modes only ("shared", "combined", "per_tag", "all_combinations"). An explicit scope
348
+ // list is a list OF lists, which does not survive flattening into one variable — file-only.
349
+ observationScopes: "HINDSIGHT_OBSERVATION_SCOPES",
253
350
  // Comma-separated, e.g. HINDSIGHT_RETAIN_TAGS="project:{gitProject},env:work". A LIST rather than
254
351
  // a map, so it flattens cleanly; its sibling retainMetadata stays file-only for the reason above.
255
352
  retainTags: "HINDSIGHT_RETAIN_TAGS"
@@ -270,6 +367,7 @@ var ENV_NUMBERS = /* @__PURE__ */ new Set([
270
367
  "daemonIdleTimeout",
271
368
  "maxParallelRetains",
272
369
  "reflectTimeoutMs",
370
+ "reflectToolTimeoutMs",
273
371
  "pageRefreshEveryTurns",
274
372
  "seedLimit",
275
373
  "surveyBudgetUsd",