haansi 0.1.116 → 0.1.118

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/haansi.js +201 -11
  2. package/package.json +1 -1
package/dist/haansi.js CHANGED
@@ -95,7 +95,7 @@ var require_package = __commonJS({
95
95
  "package.json"(exports2, module2) {
96
96
  module2.exports = {
97
97
  name: "haansi",
98
- version: "0.1.116",
98
+ version: "0.1.118",
99
99
  description: "Haansi CLI - Session collector and MCP server for Claude Code & OpenAI Codex",
100
100
  bin: {
101
101
  haansi: "./dist/haansi.js"
@@ -89922,7 +89922,7 @@ var init_replay_context = __esm({
89922
89922
  init_replay_diff();
89923
89923
  init_replay_scale();
89924
89924
  init_replay_spec();
89925
- CONTEXT_CHECK_VERSION = 6;
89925
+ CONTEXT_CHECK_VERSION = 7;
89926
89926
  QUERY_CHARS2 = 300;
89927
89927
  ERROR_CHARS = 1e3;
89928
89928
  MAX_PATHS = 20;
@@ -89935,6 +89935,149 @@ var init_replay_context = __esm({
89935
89935
  }
89936
89936
  });
89937
89937
 
89938
+ // src/commands/replay-inject.ts
89939
+ function itemsOfReturnedText(text) {
89940
+ const marks = [...text.matchAll(ITEM_MARK)];
89941
+ return marks.map((m, i) => ({
89942
+ id: m[1],
89943
+ text: text.slice(
89944
+ m.index + m[0].length,
89945
+ i + 1 < marks.length ? marks[i + 1].index : text.length
89946
+ ).trim()
89947
+ }));
89948
+ }
89949
+ function textsById(calls) {
89950
+ const out = /* @__PURE__ */ new Map();
89951
+ for (const c of calls) {
89952
+ if (c.refused || !c.returnedText) continue;
89953
+ for (const item of itemsOfReturnedText(c.returnedText)) {
89954
+ const known = out.get(item.id);
89955
+ if (!known || item.text.length > known.length)
89956
+ out.set(item.id, item.text);
89957
+ }
89958
+ }
89959
+ return out;
89960
+ }
89961
+ async function readWriteUps(session, ids, fetchImpl = fetch) {
89962
+ const base = session.apiUrl.replace(/\/+$/, "");
89963
+ await Promise.allSettled(
89964
+ ids.filter((id) => id.startsWith("solution:")).map(
89965
+ (id) => fetchImpl(
89966
+ `${base}/api/v1/memory/solutions/${encodeURIComponent(id.slice("solution:".length))}`,
89967
+ {
89968
+ method: "GET",
89969
+ headers: {
89970
+ Authorization: `Bearer ${session.token}`,
89971
+ "X-Haansi-Replay-Session": session.sessionId
89972
+ }
89973
+ }
89974
+ )
89975
+ )
89976
+ );
89977
+ }
89978
+ async function contextToInject(deps, input, fetchImpl) {
89979
+ const { task, label, targetText } = input;
89980
+ if (!deps.searchHaansi || targetText === null || !input.prompt) return null;
89981
+ const session = {
89982
+ ...await deps.openSession(task.taskId),
89983
+ apiUrl: deps.apiUrl,
89984
+ token: deps.token
89985
+ };
89986
+ await deps.searchHaansi(session, visibleAsk(task)).catch(() => void 0);
89987
+ const reached = (await deps.getCalls(session.sessionId)).calls;
89988
+ const reachedIds = new Set(reached.flatMap((c) => c.returnedIds));
89989
+ if (reachedIds.size === 0)
89990
+ return {
89991
+ onPoint: [],
89992
+ nearby: [],
89993
+ sessionId: session.sessionId,
89994
+ judgeTag: null
89995
+ };
89996
+ const judgeOnce = (n) => judgeRelevance(deps, {
89997
+ label,
89998
+ taskId: `${task.taskId}-inject-${n}`,
89999
+ agentCmd: input.agentCmd,
90000
+ timeoutMs: input.timeoutMs,
90001
+ card: input.card,
90002
+ targetChange: targetText,
90003
+ reached,
90004
+ recall: [],
90005
+ prompt: input.prompt
90006
+ });
90007
+ const judged = agreeOn(await judgeOnce(1), await judgeOnce(2), reachedIds);
90008
+ if (!judged.relevantIds) return null;
90009
+ const onPointIds = judged.relevantIds.filter((id) => reachedIds.has(id)).slice(0, MAX_INJECTED_PER_TIER);
90010
+ const nearbyIds = (judged.nearbyIds ?? []).filter((id) => reachedIds.has(id) && !onPointIds.includes(id)).slice(0, MAX_INJECTED_PER_TIER);
90011
+ await readWriteUps(session, onPointIds, fetchImpl);
90012
+ const texts = textsById((await deps.getCalls(session.sessionId)).calls);
90013
+ const item = (id) => ({
90014
+ id,
90015
+ text: (texts.get(id) ?? "(only its id came back)").slice(
90016
+ 0,
90017
+ MAX_INJECTED_ITEM_CHARS
90018
+ )
90019
+ });
90020
+ return {
90021
+ onPoint: onPointIds.map(item),
90022
+ nearby: nearbyIds.map(item),
90023
+ sessionId: session.sessionId,
90024
+ judgeTag: judged.judgeTag
90025
+ };
90026
+ }
90027
+ function injectionSection(injection) {
90028
+ if (!injection || !injection.onPoint.length && !injection.nearby.length)
90029
+ return [];
90030
+ const block = (items) => items.flatMap((i) => [`[${i.id}]`, i.text, ""]);
90031
+ return [
90032
+ "## Context Haansi found for this task",
90033
+ "",
90034
+ "Your organization's knowledge base was searched for this task before you started, as it stood when the task began. Read these before you start, and say which items you used.",
90035
+ "",
90036
+ ...injection.onPoint.length ? [
90037
+ "### On point",
90038
+ "",
90039
+ "These were judged to bear directly on this task: what to change or check.",
90040
+ "",
90041
+ ...block(injection.onPoint)
90042
+ ] : [],
90043
+ ...injection.nearby.length ? [
90044
+ "### Possibly similar",
90045
+ "",
90046
+ "These are related problems, not necessarily this one: take an idea, step or check only where it fits, and check it still applies.",
90047
+ "",
90048
+ ...block(injection.nearby)
90049
+ ] : []
90050
+ ];
90051
+ }
90052
+ function injectionCall(injection) {
90053
+ if (!injection) return [];
90054
+ const items = [...injection.onPoint, ...injection.nearby];
90055
+ if (!items.length) return [];
90056
+ return [
90057
+ {
90058
+ server: "haansi-replay",
90059
+ tool: "injected_context",
90060
+ returnedIds: items.map((i) => i.id),
90061
+ refused: false,
90062
+ returnedText: items.map((i) => `[${i.id}] ${i.text}`).join("\n\n")
90063
+ }
90064
+ ];
90065
+ }
90066
+ function experimentLine(result) {
90067
+ return `experiment-result ${JSON.stringify(result)}`;
90068
+ }
90069
+ var MAX_INJECTED_PER_TIER, MAX_INJECTED_ITEM_CHARS, ITEM_MARK;
90070
+ var init_replay_inject = __esm({
90071
+ "src/commands/replay-inject.ts"() {
90072
+ "use strict";
90073
+ init_replay_context();
90074
+ init_replay_relevance();
90075
+ MAX_INJECTED_PER_TIER = 5;
90076
+ MAX_INJECTED_ITEM_CHARS = 4e3;
90077
+ ITEM_MARK = /\[([a-z][a-z_]{0,40}:[A-Za-z0-9._:/-]{1,200})\]/g;
90078
+ }
90079
+ });
90080
+
89938
90081
  // src/commands/replay-tasks.ts
89939
90082
  var replay_tasks_exports = {};
89940
90083
  __export(replay_tasks_exports, {
@@ -90025,7 +90168,7 @@ function haansiSection(tools, hint = null) {
90025
90168
  ""
90026
90169
  ];
90027
90170
  }
90028
- function cardPrompt(task, feedback, arm = "baseline", tools = []) {
90171
+ function cardPrompt(task, feedback, arm = "baseline", tools = [], injection = null) {
90029
90172
  const failing = task.kind === "recovery" ? task.failing : null;
90030
90173
  return [
90031
90174
  "# Task",
@@ -90054,6 +90197,7 @@ function cardPrompt(task, feedback, arm = "baseline", tools = []) {
90054
90197
  ...task.card.area ? ["", "## Part of the code", "", task.card.area] : [],
90055
90198
  "",
90056
90199
  ...interfaceSection(task.testInterface ?? []),
90200
+ ...arm === "haansi" ? injectionSection(injection) : [],
90057
90201
  ...arm === "haansi" ? tools.length ? haansiSection(
90058
90202
  tools,
90059
90203
  failing?.tests.length ? "bug-fix" : task.kind !== "recovery" && task.testInterface?.length ? "feature" : null
@@ -90370,7 +90514,7 @@ async function runStoredReplays(options, deps) {
90370
90514
  let targetSent = false;
90371
90515
  let baselineChange = null;
90372
90516
  for (const arm of arms) {
90373
- if (!options.force && alreadyMeasured(
90517
+ if (!options.force && !options.injectContext && alreadyMeasured(
90374
90518
  task.measured,
90375
90519
  arm,
90376
90520
  options.agentName,
@@ -90414,6 +90558,26 @@ async function runStoredReplays(options, deps) {
90414
90558
  }
90415
90559
  }
90416
90560
  const armTools = session && deps.listTools ? haansiTools ??= await deps.listTools(session).catch(() => []) : [];
90561
+ let injection = null;
90562
+ if (session && options.injectContext) {
90563
+ injection = await contextToInject(deps, {
90564
+ task,
90565
+ label,
90566
+ card: cardPrompt(task, null),
90567
+ targetText,
90568
+ agentCmd: options.judgeCmd || options.agentCmd,
90569
+ timeoutMs: options.timeoutMs,
90570
+ prompt: await promptForRelevance()
90571
+ }).catch((err2) => {
90572
+ deps.log(
90573
+ `\u2026 ${label}: context not injected (${err2 instanceof Error ? err2.message : String(err2)})`
90574
+ );
90575
+ return null;
90576
+ });
90577
+ deps.log(
90578
+ injection ? `\u2026 ${label} [haansi]: injected ${injection.onPoint.length} on point (${injection.onPoint.map((i) => i.id).join(", ") || "none"}), ${injection.nearby.length} possibly similar (${injection.nearby.map((i) => i.id).join(", ") || "none"})` : `\u2026 ${label} [haansi]: nothing injected (Haansi could not be searched or judged)`
90579
+ );
90580
+ }
90417
90581
  const configPath = `${deps.workDir()}/${task.taskId}-${arm}.mcp.json`;
90418
90582
  const config6 = mcpConfig(arm, session);
90419
90583
  deps.writeFile(configPath, config6);
@@ -90426,7 +90590,7 @@ async function runStoredReplays(options, deps) {
90426
90590
  { ...options, setupCmd, testCmd },
90427
90591
  asTask,
90428
90592
  arm,
90429
- (feedback) => cardPrompt(task, feedback, arm, armTools),
90593
+ (feedback) => cardPrompt(task, feedback, arm, armTools, injection),
90430
90594
  async (dir) => {
90431
90595
  changed = await changedFiles(deps, dir, task.baseSha);
90432
90596
  agentDiff = await agentDiffOf(deps, dir, task.baseSha);
@@ -90443,10 +90607,14 @@ async function runStoredReplays(options, deps) {
90443
90607
  deps.removeDir(codexHome2);
90444
90608
  }
90445
90609
  let haansi = null;
90610
+ let askedIds = [];
90446
90611
  if (session) {
90447
- const { calls } = await deps.getCalls(session.sessionId);
90612
+ const own = (await deps.getCalls(session.sessionId)).calls;
90613
+ askedIds = [...new Set(own.flatMap((c) => c.returnedIds))];
90614
+ const calls = [...injectionCall(injection), ...own];
90448
90615
  haansi = {
90449
- ...haansiUseOf(calls),
90616
+ // Its own calls only: the injection is not a call it made.
90617
+ ...haansiUseOf(own),
90450
90618
  ...await judgeUse(deps, {
90451
90619
  label,
90452
90620
  taskId: task.taskId,
@@ -90463,20 +90631,20 @@ async function runStoredReplays(options, deps) {
90463
90631
  if (relevancePromptNow && targetText !== null) {
90464
90632
  let recall = [];
90465
90633
  if (deps.searchHaansi) {
90466
- const own = {
90634
+ const own2 = {
90467
90635
  ...await deps.openSession(task.taskId),
90468
90636
  apiUrl: deps.apiUrl,
90469
90637
  token: deps.token
90470
90638
  };
90471
90639
  await deps.searchHaansi(
90472
- own,
90640
+ own2,
90473
90641
  recallAsk(
90474
90642
  task.card.problem,
90475
90643
  targetText,
90476
90644
  filesOfDiff(targetText)
90477
90645
  )
90478
90646
  ).catch(() => void 0);
90479
- recall = (await deps.getCalls(own.sessionId)).calls;
90647
+ recall = (await deps.getCalls(own2.sessionId)).calls;
90480
90648
  }
90481
90649
  const judged = await judgeRelevance(deps, {
90482
90650
  label,
@@ -90538,6 +90706,26 @@ async function runStoredReplays(options, deps) {
90538
90706
  specGaps: task.specGaps ? task.specGaps.length : null
90539
90707
  });
90540
90708
  if (sendTarget) targetSent = true;
90709
+ if (options.injectContext)
90710
+ deps.log(
90711
+ experimentLine({
90712
+ task: label,
90713
+ taskId: task.taskId,
90714
+ kind: task.kind,
90715
+ tier: gateTierOf(task.context?.state, storedScoreOf(task)),
90716
+ arm,
90717
+ passed,
90718
+ attempts: r.attempts,
90719
+ tests: r.testCounts ?? [],
90720
+ fileRecall: overlap.fileRecall,
90721
+ injectedOnPoint: injection?.onPoint.map((i) => i.id) ?? [],
90722
+ injectedNearby: injection?.nearby.map((i) => i.id) ?? [],
90723
+ askedIds,
90724
+ used: haansi?.used ?? null,
90725
+ usedIds: haansi?.usedIds ?? [],
90726
+ knowledge: haansi?.knowledge ?? null
90727
+ })
90728
+ );
90541
90729
  deps.log(
90542
90730
  `${passed ? "pass" : "fail"} ${arm.padEnd(8)} ${label} (${r.attempts}/${r.maxAttempts} attempts; files ${pct(overlap.fileRecall)} of the target's, ${pct(overlap.filePrecision)} of its own on target; lines ${pct(lines.lineRecall)} of the target's, ${pct(lines.linePrecision)} of its own${haansi ? `; ${haansiText(haansi)}` : ""})`
90543
90731
  );
@@ -90548,7 +90736,7 @@ async function runStoredReplays(options, deps) {
90548
90736
  }
90549
90737
  }
90550
90738
  results.push(...done);
90551
- if (options.dryRun || done.length === 0) return;
90739
+ if (options.dryRun || options.injectContext || done.length === 0) return;
90552
90740
  try {
90553
90741
  await report(done);
90554
90742
  } catch (err2) {
@@ -90593,6 +90781,7 @@ var init_replay_tasks = __esm({
90593
90781
  init_replay_scale();
90594
90782
  init_replay_codex();
90595
90783
  init_replay_spec_gaps();
90784
+ init_replay_inject();
90596
90785
  init_replay_interface();
90597
90786
  init_replay_spec();
90598
90787
  init_replay_context();
@@ -90685,6 +90874,7 @@ function parseReplayArgs(argv) {
90685
90874
  else if (takeScaleArg(a, next, scale, errors)) continue;
90686
90875
  else if (a === "--force") options.force = true;
90687
90876
  else if (a === "--any-context") options.anyContext = true;
90877
+ else if (a === "--inject-context") options.injectContext = true;
90688
90878
  else if (a === "--check-only") options.checkOnly = true;
90689
90879
  else if (a === "--from-checks") options.fromChecks = true;
90690
90880
  else if (a === "--max-hours") options.maxHours = Number(next());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "haansi",
3
- "version": "0.1.116",
3
+ "version": "0.1.118",
4
4
  "description": "Haansi CLI - Session collector and MCP server for Claude Code & OpenAI Codex",
5
5
  "bin": {
6
6
  "haansi": "./dist/haansi.js"