haansi 0.1.104 → 0.1.106

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 +285 -96
  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.104",
98
+ version: "0.1.106",
99
99
  description: "Haansi CLI - Session collector and MCP server for Claude Code & OpenAI Codex",
100
100
  bin: {
101
101
  haansi: "./dist/haansi.js"
@@ -547,10 +547,10 @@ __export(setup_mcp_exports, {
547
547
  });
548
548
  function parseTarget(arg) {
549
549
  if (!arg || arg === "all") return "all";
550
- if (arg === "solutions" || arg === "exchange" || arg === "digests" || arg === "onboarding" || arg === "skills")
550
+ if (arg === "solutions" || arg === "exchange" || arg === "digests" || arg === "onboarding" || arg === "skills" || arg === "incidents")
551
551
  return arg;
552
552
  throw new Error(
553
- `Unknown target: ${arg}. Use one of: solutions, exchange, digests, onboarding, skills, all (default).`
553
+ `Unknown target: ${arg}. Use one of: solutions, exchange, digests, onboarding, skills, incidents, all (default).`
554
554
  );
555
555
  }
556
556
  function httpServerUrl(path2) {
@@ -792,6 +792,13 @@ var init_setup_mcp = __esm({
792
792
  target: "skills",
793
793
  name: "haansi-skills",
794
794
  path: "/api/v1/memory/skills/mcp"
795
+ },
796
+ // Past incidents: what happened, how it was resolved, and what resembles
797
+ // the problem at hand (served as of a replay's start in a replay).
798
+ {
799
+ target: "incidents",
800
+ name: "haansi-incidents",
801
+ path: "/api/v1/memory/processes/incidents/mcp"
795
802
  }
796
803
  ];
797
804
  HSK_BEARER = /Bearer hsk_[A-Za-z0-9_-]+/;
@@ -60235,6 +60242,38 @@ var init_search_solutions_tool = __esm({
60235
60242
  }
60236
60243
  });
60237
60244
 
60245
+ // ../service-capture/agent-sessions/get-solution-tool.ts
60246
+ function getSolutionTool(contextSchema2) {
60247
+ return {
60248
+ name: "get_solution",
60249
+ description: "Read one solution from your organization's knowledge base in full: the problem, the solution, every step with why it was needed, and the files it changed. Pass the id a search_solutions result names (solution:<id>). Search first; open the results that look like your problem.",
60250
+ inputSchema: {
60251
+ type: "object",
60252
+ properties: {
60253
+ id: {
60254
+ type: "string",
60255
+ description: "The solution's id, as a search result names it"
60256
+ },
60257
+ _context: contextSchema2
60258
+ },
60259
+ required: ["id"]
60260
+ }
60261
+ };
60262
+ }
60263
+ function getSolutionPath(id) {
60264
+ const match = typeof id === "string" ? ID.exec(id.trim()) : null;
60265
+ return match ? `/memory/solutions/${match[1].toLowerCase()}` : null;
60266
+ }
60267
+ var ID, NOT_A_SOLUTION_ID, NO_SUCH_SOLUTION;
60268
+ var init_get_solution_tool = __esm({
60269
+ "../service-capture/agent-sessions/get-solution-tool.ts"() {
60270
+ "use strict";
60271
+ ID = /^(?:solution:)?([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})$/i;
60272
+ NOT_A_SOLUTION_ID = "Pass the id a search_solutions result names, e.g. solution:1b2c3d4e-\u2026.";
60273
+ NO_SUCH_SOLUTION = "No such solution: it may have been removed, or it is not one you can read.";
60274
+ }
60275
+ });
60276
+
60238
60277
  // ../../node_modules/depd/index.js
60239
60278
  var require_depd = __commonJS({
60240
60279
  "../../node_modules/depd/index.js"(exports2, module2) {
@@ -84401,6 +84440,7 @@ var init_mcp_server2 = __esm({
84401
84440
  init_stdio2();
84402
84441
  init_entrypoint();
84403
84442
  init_search_solutions_tool();
84443
+ init_get_solution_tool();
84404
84444
  init_types4();
84405
84445
  (0, import_dotenv3.config)({ path: (0, import_path5.resolve)(__dirname, "../../../.env") });
84406
84446
  DEFAULT_API_URL3 = SERVICE_URL_DEFAULTS.haansiApiBaseUrl;
@@ -84438,6 +84478,7 @@ var init_mcp_server2 = __esm({
84438
84478
  server.setRequestHandler(ListToolsRequestSchema, async () => ({
84439
84479
  tools: [
84440
84480
  searchSolutionsTool(contextSchema),
84481
+ getSolutionTool(contextSchema),
84441
84482
  {
84442
84483
  name: "list_recent_solutions",
84443
84484
  description: "List the most recently added distilled paths from the knowledge base.",
@@ -84577,6 +84618,34 @@ var init_mcp_server2 = __esm({
84577
84618
  ]
84578
84619
  };
84579
84620
  }
84621
+ case "get_solution": {
84622
+ const { id, _context } = args;
84623
+ const path2 = getSolutionPath(id);
84624
+ if (!path2) {
84625
+ return {
84626
+ content: [{ type: "text", text: NOT_A_SOLUTION_ID }],
84627
+ isError: true
84628
+ };
84629
+ }
84630
+ const extraHeaders = {};
84631
+ if (_context?.userId) {
84632
+ extraHeaders["X-Session-User-Id"] = String(_context.userId);
84633
+ }
84634
+ if (_context?.orgId) {
84635
+ extraHeaders["X-Session-Org-Id"] = String(_context.orgId);
84636
+ }
84637
+ try {
84638
+ const data = await apiGet2(path2, { extraHeaders });
84639
+ return {
84640
+ content: [{ type: "text", text: data.text ?? NO_SUCH_SOLUTION }]
84641
+ };
84642
+ } catch (err2) {
84643
+ if (err2 instanceof ApiError && err2.status === 404) {
84644
+ return { content: [{ type: "text", text: NO_SUCH_SOLUTION }] };
84645
+ }
84646
+ throw err2;
84647
+ }
84648
+ }
84580
84649
  case "list_recent_solutions": {
84581
84650
  const { limit = 10, _context } = args;
84582
84651
  const n = Math.min(Math.max(1, limit), 50);
@@ -86485,14 +86554,14 @@ var init_replay_focus = __esm({
86485
86554
 
86486
86555
  // src/commands/replay-preflight.ts
86487
86556
  function refusalOf(status, body) {
86488
- let said = body.trim();
86557
+ let said2 = body.trim();
86489
86558
  try {
86490
86559
  const parsed = JSON.parse(body);
86491
86560
  const m = parsed.message ?? parsed.error;
86492
- if (typeof m === "string" && m.trim()) said = m.trim();
86561
+ if (typeof m === "string" && m.trim()) said2 = m.trim();
86493
86562
  } catch {
86494
86563
  }
86495
- const short = said.replace(/\s+/g, " ").slice(0, 120);
86564
+ const short = said2.replace(/\s+/g, " ").slice(0, 120);
86496
86565
  return short ? `${status}: ${short}` : String(status);
86497
86566
  }
86498
86567
  async function check2(fetchImpl, name, url2, init2) {
@@ -86713,6 +86782,18 @@ var init_replay_preflight = __esm({
86713
86782
  });
86714
86783
 
86715
86784
  // src/commands/replay-fallback.ts
86785
+ function solutionsSearchQuery(q, hints) {
86786
+ const params = new URLSearchParams();
86787
+ params.set("q", q);
86788
+ params.set("top_k", String(SOLUTIONS_PER_QUERY));
86789
+ for (const t of clipList2(hints?.failingTests))
86790
+ params.append("failing_tests", t);
86791
+ const error2 = hints?.error?.trim();
86792
+ if (error2) params.set("error", error2.slice(0, MAX_HINT_ERROR_CHARS2));
86793
+ for (const f of clipList2(hints?.files)) params.append("files", f);
86794
+ for (const n of clipList2(hints?.names)) params.append("names", n);
86795
+ return params.toString();
86796
+ }
86716
86797
  async function searchHaansi(session, ask, fetchImpl = fetch) {
86717
86798
  const base = session.apiUrl.replace(/\/+$/, "");
86718
86799
  const headers = {
@@ -86721,22 +86802,20 @@ async function searchHaansi(session, ask, fetchImpl = fetch) {
86721
86802
  };
86722
86803
  const solutions = ask.queries.map(
86723
86804
  (q) => fetchImpl(
86724
- `${base}/api/v1/memory/solutions/search?q=${encodeURIComponent(q)}&top_k=${SOLUTIONS_PER_QUERY}`,
86805
+ `${base}/api/v1/memory/solutions/search?${solutionsSearchQuery(q, ask.hints)}`,
86725
86806
  { method: "GET", headers }
86726
86807
  )
86727
86808
  );
86728
- const skillsPath = HTTP_SERVERS.find((s) => s.name === "haansi-skills")?.path;
86729
- const skills2 = skillsPath ? (async () => {
86730
- const url2 = `${base}${skillsPath}`;
86731
- const mcpHeaders = {
86732
- ...headers,
86733
- "Content-Type": "application/json",
86734
- Accept: "application/json, text/event-stream"
86735
- };
86809
+ const mcp = async (server2, calls) => {
86810
+ const path2 = HTTP_SERVERS.find((s) => s.name === server2)?.path;
86811
+ if (!path2 || calls.length === 0) return;
86812
+ const url2 = `${base}${path2}`;
86736
86813
  const rpc = (id, method, params, sid2) => fetchImpl(url2, {
86737
86814
  method: "POST",
86738
86815
  headers: {
86739
- ...mcpHeaders,
86816
+ ...headers,
86817
+ "Content-Type": "application/json",
86818
+ Accept: "application/json, text/event-stream",
86740
86819
  ...sid2 ? { "Mcp-Session-Id": sid2 } : {}
86741
86820
  },
86742
86821
  body: JSON.stringify({ jsonrpc: "2.0", id, method, params })
@@ -86748,57 +86827,54 @@ async function searchHaansi(session, ask, fetchImpl = fetch) {
86748
86827
  });
86749
86828
  if (!init2.ok) return;
86750
86829
  const sid = init2.headers?.get("mcp-session-id") ?? void 0;
86751
- await Promise.allSettled([
86752
- rpc(
86753
- 2,
86754
- "tools/call",
86755
- {
86756
- name: "match_playbook",
86757
- arguments: {
86758
- task: ask.task,
86759
- ...ask.error ? { error: ask.error } : {}
86760
- }
86761
- },
86762
- sid
86763
- ).then((r) => r.text()),
86764
- ...ask.queries[0] ? [
86765
- rpc(
86766
- 3,
86767
- "tools/call",
86768
- {
86769
- name: "search_skills",
86770
- arguments: { query: ask.queries[0], limit: 5 }
86771
- },
86772
- sid
86773
- ).then((r) => r.text().then(rpcResult))
86774
- ] : [],
86775
- ...ask.paths?.length ? [
86776
- rpc(
86777
- 4,
86778
- "tools/call",
86779
- {
86780
- name: "get_prior_attempts",
86781
- arguments: {
86782
- paths: ask.paths,
86783
- ...ask.queries[0] ? { query: ask.queries[0] } : {},
86784
- limit: PRIOR_ATTEMPTS_LIMIT
86785
- }
86786
- },
86787
- sid
86788
- ).then((r) => r.text().then(rpcResult))
86789
- ] : []
86790
- ]);
86791
- })() : Promise.resolve();
86792
- await Promise.allSettled([...solutions, skills2]);
86830
+ await Promise.allSettled(
86831
+ calls.map(
86832
+ (call, i) => rpc(i + 2, "tools/call", call, sid).then(
86833
+ (r) => r.text().then(rpcResult)
86834
+ )
86835
+ )
86836
+ );
86837
+ };
86838
+ const first = ask.queries[0];
86839
+ const searchWith = (name) => first ? [{ name, arguments: { query: first, limit: OTHER_SOURCE_LIMIT } }] : [];
86840
+ const skills2 = mcp("haansi-skills", [
86841
+ {
86842
+ name: "match_playbook",
86843
+ arguments: { task: ask.task, ...ask.error ? { error: ask.error } : {} }
86844
+ },
86845
+ ...first ? [{ name: "search_skills", arguments: { query: first, limit: 5 } }] : [],
86846
+ ...ask.paths?.length ? [
86847
+ {
86848
+ name: "get_prior_attempts",
86849
+ arguments: {
86850
+ paths: ask.paths,
86851
+ ...first ? { query: first } : {},
86852
+ limit: PRIOR_ATTEMPTS_LIMIT
86853
+ }
86854
+ }
86855
+ ] : []
86856
+ ]);
86857
+ const others = [
86858
+ mcp("haansi-exchange", searchWith("search_exchange")),
86859
+ mcp("haansi-digests", searchWith("search_digests")),
86860
+ mcp("haansi-onboarding", searchWith("search_guides")),
86861
+ mcp("haansi-incidents", searchWith("search_prior_incidents"))
86862
+ ];
86863
+ await Promise.allSettled([...solutions, skills2, ...others]);
86793
86864
  }
86794
- var SOLUTIONS_PER_QUERY, PRIOR_ATTEMPTS_LIMIT;
86865
+ var SOLUTIONS_PER_QUERY, MAX_HINT_ITEMS2, MAX_HINT_ITEM_CHARS2, MAX_HINT_ERROR_CHARS2, clipList2, PRIOR_ATTEMPTS_LIMIT, OTHER_SOURCE_LIMIT;
86795
86866
  var init_replay_fallback = __esm({
86796
86867
  "src/commands/replay-fallback.ts"() {
86797
86868
  "use strict";
86798
86869
  init_setup_mcp();
86799
86870
  init_replay_preflight();
86800
86871
  SOLUTIONS_PER_QUERY = 5;
86872
+ MAX_HINT_ITEMS2 = 10;
86873
+ MAX_HINT_ITEM_CHARS2 = 300;
86874
+ MAX_HINT_ERROR_CHARS2 = 1e3;
86875
+ clipList2 = (items) => (items ?? []).map((v) => v.trim().slice(0, MAX_HINT_ITEM_CHARS2)).filter(Boolean).slice(0, MAX_HINT_ITEMS2);
86801
86876
  PRIOR_ATTEMPTS_LIMIT = 3;
86877
+ OTHER_SOURCE_LIMIT = 5;
86802
86878
  }
86803
86879
  });
86804
86880
 
@@ -86858,9 +86934,9 @@ function modelUnreachableOf(output, exitCode) {
86858
86934
  const objects = jsonLines(output);
86859
86935
  const result = [...objects].reverse().find((o) => o.type === "result");
86860
86936
  if (result && result.is_error === true) {
86861
- const said = typeof result.result === "string" ? result.result : "";
86862
- if (result.terminal_reason === "api_error" || MODEL_REFUSAL.test(said))
86863
- return clipReason(said || "the model's API returned an error");
86937
+ const said2 = typeof result.result === "string" ? result.result : "";
86938
+ if (result.terminal_reason === "api_error" || MODEL_REFUSAL.test(said2))
86939
+ return clipReason(said2 || "the model's API returned an error");
86864
86940
  }
86865
86941
  const used = usageOf(output);
86866
86942
  if ((used.outputTokens ?? 0) > 0) return null;
@@ -86907,12 +86983,12 @@ function agentFailureLines(output, max2, scrub) {
86907
86983
  }
86908
86984
  if (event && typeof event === "object") {
86909
86985
  if (event.type !== "result") continue;
86910
- const said = [
86986
+ const said2 = [
86911
86987
  typeof event.terminal_reason === "string" ? `terminal_reason=${event.terminal_reason}` : "",
86912
86988
  event.is_error === true ? "is_error" : "",
86913
86989
  typeof event.result === "string" ? event.result : ""
86914
86990
  ].filter(Boolean).join(" ");
86915
- if (said) out.push(said);
86991
+ if (said2) out.push(said2);
86916
86992
  continue;
86917
86993
  }
86918
86994
  }
@@ -87954,13 +88030,20 @@ function parseRelevance(text, shown) {
87954
88030
  return null;
87955
88031
  }
87956
88032
  if (!Array.isArray(v.items)) return null;
87957
- const relevant = /* @__PURE__ */ new Set();
88033
+ const onPoint = /* @__PURE__ */ new Set();
88034
+ const nearby = /* @__PURE__ */ new Set();
87958
88035
  for (const item of v.items) {
87959
88036
  const it = item ?? {};
87960
88037
  if (typeof it.id !== "string" || !shown.has(it.id)) continue;
87961
- if (it.relevant === true) relevant.add(it.id);
88038
+ if (it.grade === "on_point") {
88039
+ const e = it.evidence ?? {};
88040
+ if (said(e.quote) && said(e.change)) onPoint.add(it.id);
88041
+ else nearby.add(it.id);
88042
+ } else if (it.grade === "nearby") nearby.add(it.id);
88043
+ else if (it.grade === void 0 && it.relevant === true) onPoint.add(it.id);
87962
88044
  }
87963
- return [...relevant].sort();
88045
+ for (const id of onPoint) nearby.delete(id);
88046
+ return { onPoint: [...onPoint].sort(), nearby: [...nearby].sort() };
87964
88047
  }
87965
88048
  function knowledgeOf(relevantIds, reachedAgent) {
87966
88049
  if (relevantIds.some((id) => reachedAgent.has(id))) return "returned";
@@ -87986,6 +88069,7 @@ async function judgeRelevance(deps, input) {
87986
88069
  return {
87987
88070
  relevantIds: [],
87988
88071
  recallIds: [],
88072
+ nearbyIds: [],
87989
88073
  knowledge: "none",
87990
88074
  judgeTag: null
87991
88075
  };
@@ -87993,6 +88077,7 @@ async function judgeRelevance(deps, input) {
87993
88077
  return {
87994
88078
  relevantIds: null,
87995
88079
  recallIds: null,
88080
+ nearbyIds: null,
87996
88081
  knowledge: null,
87997
88082
  judgeTag: null
87998
88083
  };
@@ -88028,21 +88113,24 @@ async function judgeRelevance(deps, input) {
88028
88113
  },
88029
88114
  input.timeoutMs
88030
88115
  );
88031
- const relevant = parseRelevance(deps.readFile(verdictFile) ?? "", shown) ?? parseRelevance(ran.output, shown);
88032
- if (!relevant) {
88116
+ const grades = parseRelevance(deps.readFile(verdictFile) ?? "", shown) ?? parseRelevance(ran.output, shown);
88117
+ if (!grades) {
88033
88118
  deps.log(
88034
88119
  `\u2026 ${input.label} [haansi]: the relevance judge gave no verdict`
88035
88120
  );
88036
88121
  return {
88037
88122
  relevantIds: null,
88038
88123
  recallIds: null,
88124
+ nearbyIds: null,
88039
88125
  knowledge: null,
88040
88126
  judgeTag: tag
88041
88127
  };
88042
88128
  }
88129
+ const relevant = grades.onPoint;
88043
88130
  return {
88044
88131
  relevantIds: relevant,
88045
88132
  recallIds: relevant.filter((id) => !reachedIds.has(id)),
88133
+ nearbyIds: grades.nearby,
88046
88134
  knowledge: knowledgeOf(relevant, reachedIds),
88047
88135
  judgeTag: tag
88048
88136
  };
@@ -88050,7 +88138,7 @@ async function judgeRelevance(deps, input) {
88050
88138
  deps.removeDir(dir);
88051
88139
  }
88052
88140
  }
88053
- var import_node_path14, MAX_CHANGE_CHARS2, MAX_ITEMS_CHARS, QUERY_CHARS, TERMS, clip2, COMMON;
88141
+ var import_node_path14, MAX_CHANGE_CHARS2, MAX_ITEMS_CHARS, QUERY_CHARS, TERMS, clip2, COMMON, said;
88054
88142
  var init_replay_relevance = __esm({
88055
88143
  "src/commands/replay-relevance.ts"() {
88056
88144
  "use strict";
@@ -88065,6 +88153,7 @@ var init_replay_relevance = __esm({
88065
88153
  COMMON = new Set(
88066
88154
  "this that with from have will when then than they them their there what which while where return const function export import class true false null undefined async await string number value values type types test tests expect should data item items list name names error errors".split(" ")
88067
88155
  );
88156
+ said = (v) => typeof v === "string" && v.trim() !== "";
88068
88157
  }
88069
88158
  });
88070
88159
 
@@ -89231,6 +89320,17 @@ var init_replay_spec = __esm({
89231
89320
  });
89232
89321
 
89233
89322
  // src/commands/replay-context.ts
89323
+ function storedScoreOf(task) {
89324
+ return {
89325
+ onPoint: task.context?.onPoint ?? 0,
89326
+ nearby: task.context?.nearby ?? 0
89327
+ };
89328
+ }
89329
+ function rankFound(tasks, scoreOf) {
89330
+ return tasks.map((task) => ({ task, score: scoreOf(task) })).sort(
89331
+ (a, b) => b.score.onPoint - a.score.onPoint || b.score.nearby - a.score.nearby || a.task.place - b.task.place
89332
+ ).map((r) => r.task);
89333
+ }
89234
89334
  function stateOf(knowledge) {
89235
89335
  if (knowledge === "returned") return "found";
89236
89336
  return knowledge;
@@ -89261,6 +89361,7 @@ function visibleAsk(task) {
89261
89361
  ].join("\n");
89262
89362
  let paths;
89263
89363
  let queries;
89364
+ let hints;
89264
89365
  if (failing?.tests.length) {
89265
89366
  const named = failing.tests.map(fileOfTest).filter((f) => !!f);
89266
89367
  paths = [
@@ -89270,11 +89371,17 @@ function visibleAsk(task) {
89270
89371
  ])
89271
89372
  ].slice(0, MAX_FAILING_PATHS);
89272
89373
  const files = [...new Set(paths.map(baseNameOf))];
89374
+ const errorLine = errorLineOf(failing.output);
89273
89375
  queries = [
89274
89376
  ...failing.tests.slice(0, FAILING_QUERIES).map(titleOfTest),
89275
- errorLineOf(failing.output) ?? "",
89377
+ errorLine ?? "",
89276
89378
  files.slice(0, 6).join(" ")
89277
89379
  ];
89380
+ hints = {
89381
+ failingTests: failing.tests,
89382
+ ...errorLine ? { error: errorLine } : {},
89383
+ files: paths
89384
+ };
89278
89385
  } else {
89279
89386
  const iface = task.testInterface ?? [];
89280
89387
  paths = [
@@ -89286,6 +89393,7 @@ function visibleAsk(task) {
89286
89393
  ...(task.kind !== "recovery" ? task.testTitles ?? [] : []).slice(0, TITLE_QUERIES).map(titleOfTest),
89287
89394
  names.join(" ")
89288
89395
  ];
89396
+ hints = { files: paths, names: [...new Set(names)] };
89289
89397
  }
89290
89398
  queries = queries.map((q) => flat2(q).slice(0, QUERY_CHARS2).trim()).filter((q, i, all) => q !== "" && all.indexOf(q) === i).slice(0, MAX_QUERIES);
89291
89399
  if (!queries.length && head) queries = [head];
@@ -89297,7 +89405,8 @@ function visibleAsk(task) {
89297
89405
  task: [task.card.problem, task.card.acceptance].join("\n\n").slice(0, 1e3),
89298
89406
  error: error2,
89299
89407
  queries,
89300
- ...paths.length ? { paths } : {}
89408
+ ...paths.length ? { paths } : {},
89409
+ hints
89301
89410
  };
89302
89411
  }
89303
89412
  async function checkContext(deps, input) {
@@ -89339,24 +89448,30 @@ async function checkContext(deps, input) {
89339
89448
  });
89340
89449
  const state = stateOf(judged.knowledge);
89341
89450
  if (!state || !judged.relevantIds) return null;
89451
+ const visible = new Set(reached.flatMap((c) => c.returnedIds));
89452
+ const count4 = (ids) => (ids ?? []).filter((id) => visible.has(id)).length;
89342
89453
  return {
89343
89454
  taskId: task.taskId,
89344
89455
  inputsFingerprint: task.inputsFingerprint,
89345
89456
  state,
89346
89457
  relevantIds: judged.relevantIds,
89347
89458
  judgeTag: judged.judgeTag,
89348
- checkVersion: CONTEXT_CHECK_VERSION
89459
+ checkVersion: CONTEXT_CHECK_VERSION,
89460
+ onPoint: count4(judged.relevantIds),
89461
+ nearby: count4(judged.nearbyIds)
89349
89462
  };
89350
89463
  }
89351
89464
  async function gateOnContext(deps, tasks, options) {
89352
89465
  const states = /* @__PURE__ */ new Map();
89466
+ const scores = /* @__PURE__ */ new Map();
89353
89467
  const checks = [];
89354
89468
  for (const t of tasks)
89355
- if (!needsCheck(t)) states.set(t.taskId, t.context.state);
89356
- const found = () => [...states.values()].filter((s) => s === "found").length;
89469
+ if (!needsCheck(t)) {
89470
+ states.set(t.taskId, t.context.state);
89471
+ scores.set(t.taskId, storedScoreOf(t));
89472
+ }
89357
89473
  const toCheck = tasks.filter((t) => needsCheck(t));
89358
89474
  await runPool(toCheck, options.concurrency, async (task) => {
89359
- if (options.limit && found() >= options.limit) return;
89360
89475
  const label = task.name ?? task.taskKey;
89361
89476
  try {
89362
89477
  await options.prepare?.(task);
@@ -89377,9 +89492,10 @@ async function gateOnContext(deps, tasks, options) {
89377
89492
  return;
89378
89493
  }
89379
89494
  checks.push(check3);
89495
+ scores.set(task.taskId, { onPoint: check3.onPoint, nearby: check3.nearby });
89380
89496
  const n = check3.relevantIds.length;
89381
89497
  deps.log(
89382
- `context ${check3.state.padEnd(6)} ${label}${n ? ` (${n} relevant item${n === 1 ? "" : "s"})` : ""}`
89498
+ `context ${check3.state.padEnd(6)} ${label}${n ? ` (${n} on-point item${n === 1 ? "" : "s"}; returned: ${check3.onPoint} on point, ${check3.nearby} nearby)` : check3.nearby ? ` (${check3.nearby} nearby only)` : ""}`
89383
89499
  );
89384
89500
  } catch (err2) {
89385
89501
  states.set(task.taskId, null);
@@ -89403,21 +89519,33 @@ async function gateOnContext(deps, tasks, options) {
89403
89519
  `The context checks could not be reported (${err2 instanceof Error ? err2.message : String(err2)}); the next run checks them again.`
89404
89520
  );
89405
89521
  }
89406
- const replay2 = [];
89522
+ const found = [];
89407
89523
  let noContext = 0;
89408
89524
  let unknown2 = 0;
89409
89525
  for (const t of tasks) {
89410
89526
  const label = t.name ?? t.taskKey;
89411
- if (!states.has(t.taskId)) continue;
89412
89527
  const s = states.get(t.taskId);
89413
- if (s === "found") replay2.push(t);
89528
+ if (s === "found") found.push(t);
89414
89529
  else if (s === "missed" || s === "none") {
89415
89530
  noContext += 1;
89416
- deps.log(`skip ${label}: no relevant context Haansi can return (${s})`);
89531
+ deps.log(`skip ${label}: no on-point context Haansi can return (${s})`);
89417
89532
  } else unknown2 += 1;
89418
89533
  }
89534
+ const replay2 = rankFound(
89535
+ found,
89536
+ (t) => scores.get(t.taskId) ?? { onPoint: 0, nearby: 0 }
89537
+ );
89419
89538
  return { replay: replay2, noContext, unknown: unknown2 };
89420
89539
  }
89540
+ function fromChecks(tasks, limit, shard) {
89541
+ const ready = tasks.filter(
89542
+ (t) => !needsCheck(t) && t.context?.state === "found" && t.calibration?.state === "valid" && t.calibration.inputsFingerprint === t.inputsFingerprint
89543
+ );
89544
+ const ranked = rankFound(ready, storedScoreOf);
89545
+ const top = limit ? ranked.slice(0, limit) : ranked;
89546
+ const mine = shard && shard.count > 1 ? top.filter((_, i) => i % shard.count === shard.index - 1) : top;
89547
+ return { found: ready.length, top, mine };
89548
+ }
89421
89549
  var CONTEXT_CHECK_VERSION, QUERY_CHARS2, ERROR_CHARS, MAX_PATHS, HEAD_CHARS, MAX_QUERIES, FAILING_QUERIES, TITLE_QUERIES, CONTEXT_REPORT_CHUNK, flat2;
89422
89550
  var init_replay_context = __esm({
89423
89551
  "src/commands/replay-context.ts"() {
@@ -89426,7 +89554,7 @@ var init_replay_context = __esm({
89426
89554
  init_replay_diff();
89427
89555
  init_replay_scale();
89428
89556
  init_replay_spec();
89429
- CONTEXT_CHECK_VERSION = 2;
89557
+ CONTEXT_CHECK_VERSION = 4;
89430
89558
  QUERY_CHARS2 = 200;
89431
89559
  ERROR_CHARS = 1e3;
89432
89560
  MAX_PATHS = 20;
@@ -89506,9 +89634,17 @@ function haansiSection(tools, hint = null) {
89506
89634
  return [
89507
89635
  "## Your organization's knowledge (Haansi)",
89508
89636
  "",
89509
- "These MCP servers hold what your organization learned before this task began: solutions to past problems, team skills and playbooks, earlier attempts at the same code, digests, onboarding guides and Exchange posts. They are read-only here.",
89637
+ "These MCP servers hold what your organization learned before this task began. They are read-only here.",
89638
+ "",
89639
+ "Start here, before you read or change any code, and search again when you get stuck:",
89510
89640
  "",
89511
- "Start here, before you read or change any code: search them for this problem (search_solutions with the problem and its error, match_playbook and search_skills for a team playbook, get_prior_attempts with the files you expect to change), and read what applies. Search again when you get stuck.",
89641
+ "- Past fixes: search_solutions with a sentence about the problem (its error line in error, failing tests in failing_tests, the files you expect to change in files, identifiers in names), then get_solution to read the matching results in full.",
89642
+ "- Team procedures: match_playbook with the task, and search_skills; get_skill loads one.",
89643
+ "- Earlier attempts at the same code: get_prior_attempts with the files you expect to change.",
89644
+ "- Incidents like this one: search_prior_incidents.",
89645
+ "- Recent work, conventions and shared answers: search_digests, search_guides and search_exchange.",
89646
+ "",
89647
+ "A test name, an identifier or a list of file names alone rarely finds anything; ask with what is wrong, and pass those in the structured inputs. Read what applies and say which items you used.",
89512
89648
  "",
89513
89649
  ...hint ? [HAANSI_HINTS[hint], ""] : [],
89514
89650
  ...tools.flatMap((s) => [
@@ -89667,13 +89803,26 @@ async function runStoredReplays(options, deps) {
89667
89803
  );
89668
89804
  return { results: [], notValid: 0, recorded: null };
89669
89805
  }
89670
- const mine = set2.tasks.filter(
89671
- (t) => inShard(t.taskId, options.shard ?? null)
89672
- );
89673
- if (options.shard && options.shard.count > 1)
89806
+ let mine;
89807
+ if (options.fromChecks) {
89808
+ const picked = fromChecks(set2.tasks, options.limit, options.shard ?? null);
89809
+ mine = picked.mine;
89674
89810
  deps.log(
89675
- `Shard ${options.shard.index}/${options.shard.count}: ${mine.length} of ${set2.tasks.length} task(s).`
89811
+ `From stored checks: ${picked.found} found task(s) in the set; the run takes ${picked.top.length}${options.limit ? ` (--limit ${options.limit})` : ""}, best scored first.`
89676
89812
  );
89813
+ if (options.shard && options.shard.count > 1)
89814
+ deps.log(
89815
+ `Shard ${options.shard.index}/${options.shard.count}: ${mine.length} of the run's ${picked.top.length} task(s).`
89816
+ );
89817
+ if (mine.length === 0)
89818
+ return { results: [], notValid: 0, noContext: 0, recorded: null };
89819
+ } else {
89820
+ mine = set2.tasks.filter((t) => inShard(t.taskId, options.shard ?? null));
89821
+ if (options.shard && options.shard.count > 1)
89822
+ deps.log(
89823
+ `Shard ${options.shard.index}/${options.shard.count}: ${mine.length} of ${set2.tasks.length} task(s).`
89824
+ );
89825
+ }
89677
89826
  const { states } = await runCalibration(
89678
89827
  // With the context gate, --limit caps the tasks replayed (found ones),
89679
89828
  // not the ones calibrated: calibrating only the first n left the rest
@@ -89753,7 +89902,6 @@ async function runStoredReplays(options, deps) {
89753
89902
  agentCmd: options.judgeCmd || options.agentCmd,
89754
89903
  timeoutMs: options.timeoutMs,
89755
89904
  concurrency: options.concurrency ?? 1,
89756
- limit: options.limit,
89757
89905
  dryRun: options.dryRun,
89758
89906
  cardOf: (task) => cardPrompt(task, null),
89759
89907
  targetOf,
@@ -89764,6 +89912,19 @@ async function runStoredReplays(options, deps) {
89764
89912
  noContext = gate.noContext;
89765
89913
  contextUnknown = gate.unknown;
89766
89914
  }
89915
+ if (options.checkOnly) {
89916
+ deps.log(
89917
+ `Checked: ${gated.length} found \xB7 ${noContext} with no on-point context \xB7 ${contextUnknown} unknown \xB7 ${notValid} not valid.`
89918
+ );
89919
+ return {
89920
+ results: [],
89921
+ notValid,
89922
+ noContext,
89923
+ contextUnknown,
89924
+ found: gated.length,
89925
+ recorded: null
89926
+ };
89927
+ }
89767
89928
  const chosen = options.limit ? gated.slice(0, options.limit) : gated;
89768
89929
  const results = [];
89769
89930
  let recorded = 0;
@@ -90120,6 +90281,8 @@ function parseReplayArgs(argv) {
90120
90281
  else if (takeScaleArg(a, next, scale, errors)) continue;
90121
90282
  else if (a === "--force") options.force = true;
90122
90283
  else if (a === "--any-context") options.anyContext = true;
90284
+ else if (a === "--check-only") options.checkOnly = true;
90285
+ else if (a === "--from-checks") options.fromChecks = true;
90123
90286
  else if (a === "--max-hours") options.maxHours = Number(next());
90124
90287
  else if (takeContainerArg(a, next, options.container, errors)) continue;
90125
90288
  else if (a === "--arms")
@@ -90141,6 +90304,16 @@ function parseReplayArgs(argv) {
90141
90304
  if (options.arms.length === 0 || options.arms.some((a) => !ARMS.includes(a)))
90142
90305
  errors.push("--arms must list baseline and/or haansi.");
90143
90306
  errors.push(...containerErrors(options.container));
90307
+ if (options.checkOnly && options.fromChecks)
90308
+ errors.push("--check-only and --from-checks are the two phases: pass one.");
90309
+ if ((options.checkOnly || options.fromChecks) && options.anyContext)
90310
+ errors.push(
90311
+ "--any-context skips the context check: not with --check-only or --from-checks."
90312
+ );
90313
+ if (options.checkOnly && options.limit !== null)
90314
+ errors.push(
90315
+ "--check-only checks every task: --limit belongs to the --from-checks phase."
90316
+ );
90144
90317
  if (options.maxHours !== null && options.maxHours !== void 0 && (!Number.isFinite(options.maxHours) || options.maxHours <= 0))
90145
90318
  errors.push("--max-hours must be a positive number.");
90146
90319
  Object.assign(
@@ -90588,7 +90761,7 @@ async function replayStored(options, token2, apiUrl2) {
90588
90761
  runCommand
90589
90762
  );
90590
90763
  stop = contained.stop;
90591
- const { results, notValid, noContext, contextUnknown, recorded } = await runStoredReplays2(options, {
90764
+ const { results, notValid, noContext, contextUnknown, found, recorded } = await runStoredReplays2(options, {
90592
90765
  ...contained.deps,
90593
90766
  getReplaySet: (repository) => apiGet3(
90594
90767
  `/memory/replays/tasks?repository=${encodeURIComponent(repository)}`,
@@ -90636,6 +90809,13 @@ async function replayStored(options, token2, apiUrl2) {
90636
90809
  apiUrl: apiUrl2,
90637
90810
  token: token2
90638
90811
  });
90812
+ if (options.checkOnly) {
90813
+ console.log(
90814
+ `
90815
+ Context checked: ${found ?? 0} task(s) found \xB7 ${noContext ?? 0} with no on-point context${contextUnknown ? ` \xB7 ${contextUnknown} could not be checked` : ""} \xB7 ${notValid} not valid to replay`
90816
+ );
90817
+ return;
90818
+ }
90639
90819
  const arm = (a) => {
90640
90820
  const mine = results.filter((r) => r.arm === a);
90641
90821
  return `${mine.filter((r) => r.passed).length}/${mine.length}`;
@@ -90685,6 +90865,7 @@ var init_replay = __esm({
90685
90865
  [--judge-cmd "<cmd>"]
90686
90866
  [--run-ref <url>] [--dry-run] [--repo-commands] [--no-diffs]
90687
90867
  [--shard i/n] [--concurrency k] [--force] [--max-hours h] [--any-context]
90868
+ [--check-only | --from-checks]
90688
90869
  [--setup-cache <files> --setup-cache-path <paths> [--setup-cache-dir <dir>]]
90689
90870
  [--container <image> [--container-setup "<cmd>"] [--container-env NAME]...]
90690
90871
 
@@ -90715,10 +90896,18 @@ files they are and import, and the files changed by the commits that wrote
90715
90896
  the lines its stack frames name, or by those files' own last commits before
90716
90897
  the start; a feature's problem, its tests' titles and the
90717
90898
  names they use; the paths the card names), and the relevance judge (your agent
90718
- command, once per task checked) says whether anything relevant came back.
90719
- A task with nothing relevant Haansi can return is skipped, and checked again
90720
- once the organization gains new Haansi content. --any-context replays every
90721
- valid task without the check (diagnostics).
90899
+ command, once per task checked) grades each item that came back on point (it
90900
+ says part of what to change or check), nearby (same area, another problem)
90901
+ or unrelated. A task is replayed only when an on-point item came back; one
90902
+ without is skipped, and checked again once the organization gains new Haansi
90903
+ content. Found tasks are replayed most on-point items first (then most
90904
+ nearby), and --limit takes the best. --any-context replays every valid task
90905
+ without the check (diagnostics).
90906
+
90907
+ Across shards, run two phases so --limit takes the best of the whole set:
90908
+ --check-only --shard i/n calibrates and checks each share (no agent runs);
90909
+ then --from-checks --limit N --shard i/n ranks every stored found task, takes
90910
+ the top N for the run, and replays this shard's share of them.
90722
90911
 
90723
90912
  --judge-cmd runs the judges (whether the agent used what Haansi returned,
90724
90913
  which of it was relevant, and the context check before replaying) with their
@@ -90737,7 +90926,7 @@ Run from a clone of the repository. Example (GitHub Actions):
90737
90926
  haansi replay run --setup-cmd 'npm ci' \\
90738
90927
  --container node:22 --container-env ANTHROPIC_API_KEY \\
90739
90928
  --container-setup 'npm install -g haansi @anthropic-ai/claude-code' \\
90740
- --agent-cmd 'claude -p "$HAANSI_REPLAY_PROMPT" --output-format stream-json --verbose --permission-mode acceptEdits --mcp-config "$HAANSI_REPLAY_MCP_CONFIG" --allowedTools mcp__haansi-solutions,mcp__haansi-exchange,mcp__haansi-digests,mcp__haansi-onboarding,mcp__haansi-skills' \\
90929
+ --agent-cmd 'claude -p "$HAANSI_REPLAY_PROMPT" --output-format stream-json --verbose --permission-mode acceptEdits --mcp-config "$HAANSI_REPLAY_MCP_CONFIG" --allowedTools mcp__haansi-solutions,mcp__haansi-exchange,mcp__haansi-digests,mcp__haansi-onboarding,mcp__haansi-skills,mcp__haansi-incidents' \\
90741
90930
  --test-cmd 'npx vitest run $HAANSI_REPLAY_TEST_FILES -t "$HAANSI_REPLAY_TEST_PATTERN"' \\
90742
90931
  --agent-name claude-code --model claude-sonnet-5-5`;
90743
90932
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "haansi",
3
- "version": "0.1.104",
3
+ "version": "0.1.106",
4
4
  "description": "Haansi CLI - Session collector and MCP server for Claude Code & OpenAI Codex",
5
5
  "bin": {
6
6
  "haansi": "./dist/haansi.js"