hippo-memory 1.56.0 → 1.57.0

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 (67) hide show
  1. package/dist/api.d.ts +16 -9
  2. package/dist/api.js +57 -24
  3. package/dist/card-detail.d.ts +1 -1
  4. package/dist/card-detail.js +1 -1
  5. package/dist/cli/shared.d.ts +137 -0
  6. package/dist/cli/shared.js +830 -0
  7. package/dist/cli/sleep.d.ts +10 -0
  8. package/dist/cli/sleep.js +171 -0
  9. package/dist/cli.d.ts +0 -7
  10. package/dist/cli.js +223 -1789
  11. package/dist/connectors/github/webhook.d.ts +19 -0
  12. package/dist/connectors/github/webhook.js +313 -0
  13. package/dist/connectors/slack/webhook.d.ts +22 -0
  14. package/dist/connectors/slack/webhook.js +203 -0
  15. package/dist/consolidate.js +3 -2
  16. package/dist/context-auto.d.ts +3 -0
  17. package/dist/context-auto.js +34 -0
  18. package/dist/customer-notes.js +2 -1
  19. package/dist/dashboard.js +2 -1
  20. package/dist/decisions.js +2 -1
  21. package/dist/eval-stats.d.ts +58 -0
  22. package/dist/eval-stats.js +111 -0
  23. package/dist/goals.d.ts +49 -25
  24. package/dist/goals.js +39 -22
  25. package/dist/graph-extract.js +1 -1
  26. package/dist/graph-recall.d.ts +1 -1
  27. package/dist/graph-recall.js +1 -1
  28. package/dist/graph.js +1 -1
  29. package/dist/hooks.d.ts +1 -3
  30. package/dist/hooks.js +2 -4
  31. package/dist/http-util.d.ts +31 -0
  32. package/dist/http-util.js +46 -0
  33. package/dist/incidents.js +2 -1
  34. package/dist/index.d.ts +5 -2
  35. package/dist/index.js +5 -2
  36. package/dist/mcp/server.js +173 -285
  37. package/dist/memory.d.ts +19 -0
  38. package/dist/memory.js +38 -0
  39. package/dist/policies.js +2 -1
  40. package/dist/predictions.js +2 -1
  41. package/dist/processes.js +2 -1
  42. package/dist/project-briefs.js +3 -1
  43. package/dist/prompt-recall.js +1 -1
  44. package/dist/recall-history.d.ts +5 -0
  45. package/dist/recall-history.js +9 -0
  46. package/dist/recall-pipeline.d.ts +101 -0
  47. package/dist/recall-pipeline.js +313 -0
  48. package/dist/recall-scope.d.ts +22 -0
  49. package/dist/recall-scope.js +27 -1
  50. package/dist/search.d.ts +0 -20
  51. package/dist/search.js +2 -49
  52. package/dist/server.js +1901 -2384
  53. package/dist/skills.js +2 -1
  54. package/dist/store-cards.d.ts +53 -0
  55. package/dist/store-cards.js +512 -0
  56. package/dist/store.d.ts +2 -89
  57. package/dist/store.js +6 -562
  58. package/dist/tenant.d.ts +22 -0
  59. package/dist/tenant.js +26 -0
  60. package/dist/tokenize.d.ts +2 -0
  61. package/dist/tokenize.js +8 -0
  62. package/dist/version.d.ts +1 -1
  63. package/dist/version.js +1 -1
  64. package/extensions/openclaw-plugin/openclaw.plugin.json +1 -1
  65. package/extensions/openclaw-plugin/package.json +1 -1
  66. package/openclaw.plugin.json +1 -1
  67. package/package.json +1 -1
package/dist/cli.js CHANGED
@@ -37,10 +37,11 @@ import * as path from 'path';
37
37
  import * as fs from 'fs';
38
38
  import * as os from 'os';
39
39
  import { fileURLToPath } from 'node:url';
40
- import { execFileSync, execSync, spawn } from 'child_process';
41
- import { installJsonHooks, uninstallJsonHooks, resolveJsonHookPaths, detectInstalledTools, defaultSleepLogPath, ensureCodexWrapperInstalled, installCodexWrapper, detectRealCodexPath, isCodexPresent, isCodexWrapperInstalled, CODEX_TRUST_LINE, repairCodexWrapperIfInstalled, uninstallCodexWrapper, resolveCodexSessionTranscript, resolveCodexWrapperPaths, installOpencodePlugin, uninstallOpencodePlugin, resolveOpencodePluginPath, } from './hooks.js';
40
+ import { execFileSync, spawn } from 'child_process';
41
+ import { installJsonHooks, uninstallJsonHooks, resolveJsonHookPaths, detectInstalledTools, defaultSleepLogPath, ensureCodexWrapperInstalled, installCodexWrapper, detectRealCodexPath, isCodexPresent, isCodexWrapperInstalled, repairCodexWrapperIfInstalled, uninstallCodexWrapper, resolveCodexSessionTranscript, resolveCodexWrapperPaths, installOpencodePlugin, uninstallOpencodePlugin, resolveOpencodePluginPath, } from './hooks.js';
42
42
  import { createMemory, createSuccessor, calculateStrength, calculateRewardFactor, deriveHalfLife, resolveConfidence, confidenceFacets, confidenceLabel, computeSchemaFit, Layer, } from './memory.js';
43
- import { getHippoRoot, isInitialized, initStore, writeEntry, strengthenRetrieved, readEntry, deleteEntry, loadAllEntries, loadSearchEntries, loadRecallSearchEntries, loadIndex, saveIndex, loadStats, updateStats, saveActiveTaskSnapshot, loadActiveTaskSnapshot, loadFreshActiveTaskSnapshot, closeTaskSnapshotsForSession, clearActiveTaskSnapshot, appendSessionEvent, listSessionEvents, listMemoryConflicts, resolveConflict, saveSessionHandoff, loadLatestHandoff, loadHandoffById, stampHandoffOutcome, writeSessionEndHandoff, createCard, loadCard, listCards, loadCardRuns, claimCard, heartbeatCard, blockCard, reviewCard, completeCard, reclaimExpiredCards, addCardComment, memoriesBackingObjects, } from './store.js';
43
+ import { getHippoRoot, isInitialized, initStore, writeEntry, strengthenRetrieved, readEntry, deleteEntry, loadAllEntries, loadSearchEntries, loadIndex, saveIndex, loadStats, updateStats, saveActiveTaskSnapshot, loadActiveTaskSnapshot, loadFreshActiveTaskSnapshot, closeTaskSnapshotsForSession, clearActiveTaskSnapshot, appendSessionEvent, listSessionEvents, listMemoryConflicts, resolveConflict, saveSessionHandoff, loadLatestHandoff, loadHandoffById, stampHandoffOutcome, writeSessionEndHandoff, memoriesBackingObjects, } from './store.js';
44
+ import { createCard, loadCard, listCards, loadCardRuns, claimCard, heartbeatCard, blockCard, reviewCard, completeCard, reclaimExpiredCards, addCardComment, } from './store-cards.js';
44
45
  import { rejectValue, unrejectValue, listRejectionsForTenant } from './reject-flow.js';
45
46
  import { RejectedValueError } from './rejection.js';
46
47
  import { isHandoffOutcome, formatHandoffEvidenceLine } from './handoff.js';
@@ -48,13 +49,12 @@ import { readSessionScan, recordSessionDigest } from './session-digest.js';
48
49
  import { isCardStatus } from './card.js';
49
50
  import { loadCardDetail } from './card-detail.js';
50
51
  import { passesScopeFilterForRecall } from './recall-scope.js';
51
- import { estimateTokens, fitBudget, hybridSearch, physicsSearch, explainMatch, textOverlap, tokenize as tokenizeQuery } from './search.js';
52
- import { compareEntryIdentity } from './compare.js';
52
+ import { estimateTokens, fitBudget, explainMatch } from './search.js';
53
53
  import { renderTraceContent, parseSteps } from './trace.js';
54
54
  import { writeDeliveryEventAtRoot, writeDeliveryEventOnHandle, writeRecallTraceAtRoot } from './recall-trace.js';
55
55
  import { createDeliveryRecorder } from './delivery-recorder.js';
56
56
  import { deduplicateStore } from './dedupe.js';
57
- import { isEmbeddingAvailable, embedAll, embedMemory, loadEmbeddingIndex, resolveEmbeddingModel, embeddingModelRequiresReindex, } from './embeddings.js';
57
+ import { embedAll, embedMemory, loadEmbeddingIndex, resolveEmbeddingModel, embeddingModelRequiresReindex, } from './embeddings.js';
58
58
  import { resolveEmbeddingProvider } from './embedding-provider.js';
59
59
  import { loadPhysicsState, resetAllPhysicsState } from './physics-state.js';
60
60
  import { computeSystemEnergy, vecNorm } from './physics.js';
@@ -64,26 +64,27 @@ import { runDoctor, formatDoctor } from './doctor.js';
64
64
  import { buildSupportBundle, TAIL_MAX_LINES } from './support-bundle.js';
65
65
  import { PACKAGE_VERSION } from './version.js';
66
66
  import { captureToolFailure } from './capture-error.js';
67
- import { blockHash, hookPayloadSessionId, isSubagentPayload, lastSentState, readApiCalls, recordRereads, recordTokenUse, shouldSkipUnchanged, } from './token-ledger.js';
67
+ import { blockHash, isSubagentPayload, lastSentState, readApiCalls, recordRereads, recordTokenUse, shouldSkipUnchanged, } from './token-ledger.js';
68
68
  import { FAILURE_LOG_RETENTION_DAYS } from './failure-log.js';
69
- import { pushGoal, getActiveGoals, completeGoal, suspendGoal, resumeGoal, applyGoalStackBoost } from './goals.js';
69
+ import { pushGoal, getActiveGoals, completeGoal, suspendGoal, resumeGoal, writeGoalRecallLog } from './goals.js';
70
70
  import { rowToGoal } from './goals.js';
71
- import { captureError, extractLessons, partitionLessons, runWatched, fetchGitLog, isGitRepo, } from './autolearn.js';
72
- import { dropHeldCopies, duplicateKey, storedTextKeys } from './same-text.js';
71
+ import { captureError, runWatched, isGitRepo, } from './autolearn.js';
72
+ import { dropHeldCopies } from './same-text.js';
73
73
  import { currentMachine, importAtCompaction, importAtSessionEnd, importForStore, importProjectMemories, importUserMemories, } from './agent-memories/sync.js';
74
74
  import { detailLines, emptyReport, mergeReports, summaryLine } from './agent-memories/report.js';
75
- import { extractInvalidationTarget, invalidateMatching, detectChurnStale } from './invalidation.js';
76
- import { deriveOriginProject, isGlobalStoreRoot, resolveProjectIdentity } from './project-identity.js';
75
+ import { invalidateMatching } from './invalidation.js';
76
+ import { deriveOriginProject, isGlobalStoreRoot } from './project-identity.js';
77
77
  import { extractPathTags } from './path-context.js';
78
+ import { autoDetectContext } from './context-auto.js';
78
79
  import { detectScope } from './scope.js';
79
- import { getGlobalRoot, initGlobal, shareMemory, listPeers, autoShare, transferScore, searchBothHybrid, syncGlobalToLocal, } from './shared.js';
80
- import { DAILY_TASK_NAME, buildDailyRunnerCommand, buildSchtasksCreateArgs, buildWindowsTaskRun, listRegisteredWorkspaces, registerWorkspace, runDailyMaintenance, } from './scheduler.js';
80
+ import { getGlobalRoot, initGlobal, shareMemory, listPeers, autoShare, transferScore, syncGlobalToLocal, } from './shared.js';
81
+ import { listRegisteredWorkspaces, registerWorkspace, runDailyMaintenance, } from './scheduler.js';
81
82
  import { importChatGPT, importClaude, importCursor, importGenericFile, importMarkdown, importVault, } from './importers.js';
82
- import { cmdCapture, cmdPreCompact, cmdPostCompact, resolveLastSessionTranscript, truncateCodePointSafe, sanitizeLogMessage, transcriptWorkingState } from './capture.js';
83
+ import { cmdCapture, cmdPreCompact, cmdPostCompact, resolveLastSessionTranscript, truncateCodePointSafe, transcriptWorkingState } from './capture.js';
83
84
  import { COMPACTION_DB_WAIT_MS, replayCompactionsAt } from './compaction-record.js';
84
85
  import { readStdinBounded } from './stdin.js';
85
- import { auditMemories, appendAuditEvent, auditQueryFields, AUDIT_OPS, } from './audit.js';
86
- import { listApiKeys, revokeApiKey } from './auth.js';
86
+ import { auditMemories, auditQueryFields, AUDIT_OPS, } from './audit.js';
87
+ import { listApiKeys } from './auth.js';
87
88
  import { buildProvenanceCoverage } from './provenance-coverage.js';
88
89
  import { buildCorrectionLatency } from './correction-latency.js';
89
90
  import * as api from './api.js';
@@ -98,8 +99,7 @@ import * as briefsModule from './project-briefs.js';
98
99
  import * as customerNotesModule from './customer-notes.js';
99
100
  import { extractGraph } from './graph-extract.js';
100
101
  import { buildGraphModel, renderGraphHtml, renderGraphCanvas, DEFAULT_VIEW_LIMIT } from './graph-view.js';
101
- import { createHash } from 'node:crypto';
102
- import { detectAnchoring, hashQueryText, buildSessionKey, getOrCreateRing, appendRecall, snapshotRing, } from './recall-history.js';
102
+ import { detectAnchoring, hashQueryText, biasHintEnabled, buildSessionKey, getOrCreateRing, appendRecall, snapshotRing, } from './recall-history.js';
103
103
  import { detectAvailabilityBias } from './availability.js';
104
104
  // v0.33 / J1 — Module-level per-(tenant, session) recall-history ring map.
105
105
  // Each CLI process maintains its OWN Map; no IPC / no cross-process sharing
@@ -124,16 +124,14 @@ export function __resetSessionRecallHistoryCli() {
124
124
  sessionRecallHistoryCli.clear();
125
125
  }
126
126
  import * as client from './client.js';
127
- import { detectServer, removePidfileIfOwned } from './server-detect.js';
128
127
  import { resolveTenantId } from './tenant.js';
129
128
  import { runEval, bootstrapCorpus, compareSummaries } from './eval.js';
130
129
  import { runFeatureEval, formatResult, resultToBaseline, detectRegressions } from './eval-suite.js';
131
130
  import { refineStore } from './refine-llm.js';
132
131
  import { wmPush, wmRead, wmClear, wmFlush } from './working-memory.js';
133
- import { multihopSearch } from './multihop.js';
134
- import { graphExpandRecall, MAX_HOPS, DEFAULT_MAX_NEIGHBORS } from './graph-recall.js';
135
- import { DEFAULT_GRAPH_STREAM_WEIGHT } from './graph-stream.js';
132
+ import { MAX_HOPS, DEFAULT_MAX_NEIGHBORS } from './graph-recall.js';
136
133
  import { getReranker } from './rerankers/index.js';
134
+ import { rankRecall } from './recall-pipeline.js';
137
135
  import { JEV_DEFAULT_TOP_K } from './rerankers/jev.js';
138
136
  import { computeSalience } from './salience.js';
139
137
  import { renderAmbientSummary } from './ambient.js';
@@ -145,145 +143,10 @@ import { backfillChannel } from './connectors/slack/backfill.js';
145
143
  import { slackHistoryFetcher } from './connectors/slack/web-client.js';
146
144
  import { addWorkspace as addSlackWorkspace, listWorkspaces as listSlackWorkspaces, removeWorkspace as removeSlackWorkspace, } from './connectors/slack/workspaces.js';
147
145
  import { cmdGithub, printGithubBackfillUsage } from './connectors/github/cli-impl.js';
146
+ import { parseLimitFlag, parseCountFlag, parseBudgetFlag, emitCliAudit, requireInit, runChurnStaleForRepo, runViaServerIfAvailable, fmt, recallEntryText, recallHeading, printAgentImport, hippoBlock, installCodexMemoryHooks, setupDailySchedule, parseAsOfFlag, engineFlags, collectHandoffEvidence, logSessionEndImport, appendSessionEndCloseLog, printActiveTaskSnapshot, printSessionEvents, printHandoff, cardStringFlag, hostSessionId, resetHookInjection, captureConsole, hookStoreRoot, withLedgerDb, learnFromRepo, HOOK_MARKERS, HOOKS, resolveAuthRoot, } from './cli/shared.js';
148
147
  // ---------------------------------------------------------------------------
149
148
  // Helpers
150
149
  // ---------------------------------------------------------------------------
151
- function parseLimitFlag(value) {
152
- if (!value)
153
- return Infinity;
154
- const parsed = parseInt(String(value), 10);
155
- return Number.isFinite(parsed) && parsed >= 1 ? parsed : Infinity;
156
- }
157
- function parseCountFlag(value) {
158
- if (!value || value === true || Array.isArray(value))
159
- return 0;
160
- const parsed = parseInt(String(value), 10);
161
- return Number.isFinite(parsed) && parsed >= 1 ? parsed : 0;
162
- }
163
- function parseBudgetFlag(value, fallback) {
164
- if (value === undefined)
165
- return fallback;
166
- // A value-less flag and a junk value are different typos; the --hops guard already splits them.
167
- if (typeof value !== 'string') {
168
- console.error('--budget requires an integer value (e.g. --budget 1500).');
169
- process.exit(1);
170
- }
171
- // Number(), like the --hops guard: parseInt('12abc') is 12, silently accepting what this message rejects.
172
- const parsed = Number(value);
173
- if (!Number.isInteger(parsed) || parsed < 0) {
174
- console.error(`Invalid --budget: "${value}". Must be a non-negative integer.`);
175
- process.exit(1);
176
- }
177
- return parsed;
178
- }
179
- /**
180
- * Emit an audit event against `hippoRoot`'s db. Opens its own short-lived
181
- * connection so callers don't have to thread a db handle. Swallows all errors
182
- * — audit must never crash a CLI command.
183
- */
184
- function emitCliAudit(hippoRoot, op, targetId, metadata) {
185
- try {
186
- const db = openHippoDb(hippoRoot);
187
- try {
188
- appendAuditEvent(db, {
189
- tenantId: resolveTenantId({}),
190
- actor: 'cli',
191
- op,
192
- targetId,
193
- metadata,
194
- });
195
- }
196
- finally {
197
- closeHippoDb(db);
198
- }
199
- }
200
- catch {
201
- // Audit is best-effort; surface failures only via missing rows.
202
- }
203
- }
204
- function requireInit(hippoRoot) {
205
- if (!isInitialized(hippoRoot)) {
206
- console.error(`No hippo store at ${hippoRoot} (searched ${process.cwd()} and its parents up to your home directory). Run \`hippo init\` first.`);
207
- process.exit(1);
208
- }
209
- }
210
- /** FE2: run detectChurnStale against every store this repo's memories can live in. */
211
- function runChurnStaleForRepo(hippoRoot, dryRun) {
212
- const repoRoot = execFileSync('git', ['rev-parse', '--show-toplevel'], { cwd: process.cwd(), encoding: 'utf8', windowsHide: true }).trim();
213
- const projectName = resolveProjectIdentity(process.cwd()).name;
214
- const globalRoot = getGlobalRoot();
215
- const roots = globalRoot !== hippoRoot && isInitialized(globalRoot) ? [hippoRoot, globalRoot] : [hippoRoot];
216
- const tenantId = resolveTenantId({});
217
- return roots.map((root) => {
218
- // One store failing must not abort sleep's later phases or skip the other store.
219
- try {
220
- return { root, result: detectChurnStale(root, repoRoot, { tenantId, projectName, dryRun }) };
221
- }
222
- catch (err) {
223
- const message = err instanceof Error ? err.message : String(err);
224
- return { root, result: { checked: 0, marked: 0, alreadyMarked: 0, skippedPinned: [], dryRun, preview: [], error: message } };
225
- }
226
- });
227
- }
228
- /**
229
- * H2: when HIPPO_REQUIRE_SERVER is set, the CLI must not silently fall back to
230
- * direct DB mode — a missing server then masks a real misconfiguration (the
231
- * configured HIPPO_API_KEY is also silently discarded on fallback). Throws a
232
- * clear error then. It guards only the routed writes (remember, forget, archive,
233
- * promote); every other command opens the store directly, knob or not.
234
- */
235
- function failIfServerRequired(reason) {
236
- if (process.env['HIPPO_REQUIRE_SERVER']) {
237
- throw new Error(`hippo: HIPPO_REQUIRE_SERVER is set but ${reason}. ` +
238
- `Start \`hippo serve\`, or unset HIPPO_REQUIRE_SERVER to allow direct-mode fallback.`);
239
- }
240
- }
241
- /**
242
- * Run an HTTP-routed command if a `hippo serve` instance is detected for
243
- * `hippoRoot`. Returns:
244
- * - true if the HTTP path ran (success OR a structured server error that
245
- * was already surfaced to stdout/stderr by `httpFn`),
246
- * - false if no server was detected, or if the detected pidfile turned out
247
- * to be stale (connection refused). On stale, the pidfile is removed
248
- * if it still names that dead server (a newer one may have replaced
249
- * it) and the caller should fall back to the direct path.
250
- *
251
- * Per the A1 plan footgun #1: stale pidfiles must self-heal, not crash.
252
- * H2: when HIPPO_REQUIRE_SERVER is set, both fallback paths throw instead of
253
- * returning false, so a missing server fails loudly rather than silently
254
- * degrading to direct mode.
255
- */
256
- async function runViaServerIfAvailable(hippoRoot, httpFn) {
257
- const info = await detectServer(hippoRoot);
258
- if (!info) {
259
- failIfServerRequired('no running server was detected for this hippoRoot');
260
- return false;
261
- }
262
- const apiKey = process.env['HIPPO_API_KEY'];
263
- try {
264
- await httpFn(info, apiKey);
265
- return true;
266
- }
267
- catch (err) {
268
- const failure = client.classifyTransportFailure(err);
269
- if (failure === 'never-sent') {
270
- failIfServerRequired('the server pidfile was stale (connection refused)');
271
- console.error('hippo: stale server pidfile detected, falling back to direct mode');
272
- // Clear the pidfile only if it still names the dead server we just
273
- // probed — a newer server may have rewritten it (removePidfileIfOwned).
274
- removePidfileIfOwned(hippoRoot, { pid: info.pid, startedAt: info.started_at });
275
- return false;
276
- }
277
- if (failure === 'delivery-unknown') {
278
- // Every caller of this helper is a non-idempotent write, so replaying on
279
- // the direct path would store a row the server may already have committed.
280
- // Leave the pidfile alone: the next command's connect-phase failure heals it.
281
- console.error(`hippo: the connection to ${info.url} dropped mid-request, so the write may already have been applied. Not retrying locally. Check with \`hippo recall\` before running this again.`);
282
- process.exit(1);
283
- }
284
- throw err;
285
- }
286
- }
287
150
  // Every switch the CLI reads. A value on one reads as on under Boolean() (`--fix=false` would fix)
288
151
  // and as off under === true (`--pin=true` would not pin), so parseArgs and main() refuse one.
289
152
  // tests/cli-parse-flag-equals.test.ts fails when a switch read is missing from this set.
@@ -410,51 +273,6 @@ export function parseArgs(argv) {
410
273
  }
411
274
  return { command, args, flags };
412
275
  }
413
- function fmt(n, digits = 2) {
414
- return n.toFixed(digits);
415
- }
416
- // What `hippo recall` prints for one result; the budget prices this same text.
417
- function recallEntryText(r, query, showWhy, isGlobal) {
418
- const e = r.entry;
419
- const label = confidenceLabel(e);
420
- const confLabel = label.warn ? `[${label.text}] ⚠️` : `[${label.text}]`;
421
- const bars = Math.round(e.strength * 10);
422
- const graphMark = r.graphVia ? ` [graph: ${r.graphVia.hops}hop ${r.graphVia.relType}]` : '';
423
- const lines = [
424
- `--- ${e.id} [${e.layer}] ${confLabel}${isGlobal ? ' [global]' : ''}${e.superseded_by ? ' [superseded]' : ''}${graphMark} score=${fmt(r.score, 3)} strength=${fmt(e.strength)}`,
425
- ` [${'█'.repeat(bars)}${'░'.repeat(10 - bars)}] tags: ${e.tags.join(', ') || 'none'} | retrieved: ${e.retrieval_count}x`,
426
- ];
427
- if (showWhy) {
428
- const explanation = explainMatch(query, r);
429
- lines.push(` source:${isGlobal ? ' [global]' : ' [local]'} | layer: [${e.layer}] | confidence: [${label.text}]`, ` reason: ${explanation.reason}`);
430
- const env = explanation.envelope;
431
- if (env) {
432
- lines.push(` kind: ${env.kind}`);
433
- if (env.scope)
434
- lines.push(` scope: ${env.scope}`);
435
- if (env.owner)
436
- lines.push(` owner: ${env.owner}`);
437
- if (env.artifact_ref)
438
- lines.push(` artifact_ref: ${env.artifact_ref}`);
439
- if (env.session_id)
440
- lines.push(` session_id: ${env.session_id}`);
441
- lines.push(` confidence: ${env.confidence}`);
442
- }
443
- // A7 recall-trace, e.g. "ranking: base 0.420 -> interference x0.30 -> 0.126 -> goal-boost x1.50 -> 0.189".
444
- if (r.rerankTrace && r.rerankTrace.length > 0) {
445
- const parts = [`base ${fmt(r.rerankTrace[0].scoreBefore, 3)}`];
446
- for (const step of r.rerankTrace) {
447
- parts.push(`${step.stage}${step.multiplier !== undefined ? ` x${fmt(step.multiplier, 2)}` : ''}`, fmt(step.scoreAfter, 3));
448
- }
449
- lines.push(` ranking: ${parts.join(' -> ')}`);
450
- }
451
- }
452
- lines.push('', e.content, '');
453
- return lines.join('\n');
454
- }
455
- function recallHeading(entries, tokens, query) {
456
- return `Found ${entries} memories (${tokens} tokens) for: "${query}"\n`;
457
- }
458
276
  // JSON.stringify keeps quotes or parens in the matched phrase from blurring the line.
459
277
  function planningLine(p) {
460
278
  if (p.hint)
@@ -633,14 +451,6 @@ function cmdInit(hippoRoot, flags) {
633
451
  if (!flags['no-learn'])
634
452
  printAgentImport(importForStore(hippoRoot, { machine: currentMachine() }));
635
453
  }
636
- /** One line when an agent memory import moved anything; its warnings go to stderr. */
637
- function printAgentImport(report, indent = ' ') {
638
- const line = summaryLine(report);
639
- if (line !== null)
640
- console.log(`${indent}${line}`);
641
- for (const warning of report.warnings)
642
- console.error(`hippo: agent memories: ${warning}`);
643
- }
644
454
  /** Every write init makes into agent config (instruction blocks, hooks, plugins) is an automatic integration, so one switch skips them all. */
645
455
  function initInstallsIntegrations(flags) {
646
456
  if (flags['no-hooks'])
@@ -698,19 +508,6 @@ function patchInstructionFiles(dir, agents) {
698
508
  console.log(` Auto-installed ${hook} hook in ${hookDef.file}`);
699
509
  }
700
510
  }
701
- /** The first hippo block in `text` and the agent whose current or shipped text it is; `owner` is undefined for an edited block. */
702
- function hippoBlock(text) {
703
- const at = text.indexOf(HOOK_MARKERS.start);
704
- const start = at + HOOK_MARKERS.start.length;
705
- const end = text.indexOf(HOOK_MARKERS.end, start);
706
- if (at < 0 || end < 0)
707
- return null;
708
- // git autocrlf checks these files out with CRLF: match as LF, write back in the file's own ending.
709
- const raw = text.slice(start, end);
710
- const inner = raw.replace(/\r\n/g, '\n').trim();
711
- const owner = Object.keys(HOOKS).find((k) => HOOKS[k].content === inner) ?? SHIPPED_HOOK_HASHES.get(createHash('sha256').update(inner).digest('hex'));
712
- return { start, end, eol: raw.includes('\r\n') ? '\r\n' : '\n', inner, owner };
713
- }
714
511
  /** Swap an unedited block from an earlier hippo for the current one; an edited block stays, with a hint. */
715
512
  function refreshShippedBlock(filePath, text, hook) {
716
513
  const block = hippoBlock(text);
@@ -725,22 +522,6 @@ function refreshShippedBlock(filePath, text, hook) {
725
522
  fs.writeFileSync(filePath, `${text.slice(0, start)}${eol}${HOOKS[owner].content.replace(/\n/g, eol)}${eol}${text.slice(end)}`, 'utf8');
726
523
  console.log(` Refreshed the ${owner} hippo block in ${name}`);
727
524
  }
728
- /** Adds hippo's two Codex hooks and says what changed; each install ends on the trust reminder, since Codex skips an untrusted hook. */
729
- function installCodexMemoryHooks(indent) {
730
- const result = installJsonHooks('codex');
731
- if (result.invalidJson) {
732
- console.log(`${indent}WARNING: ${result.settingsPath} is not a hooks file hippo can merge into; fix it, then run \`hippo hook install codex\`.`);
733
- return;
734
- }
735
- const added = [
736
- result.installedUserPromptSubmit ? 'UserPromptSubmit' : '',
737
- result.installedCompactResume ? 'SessionStart(compact)' : '',
738
- ].filter(Boolean);
739
- console.log(added.length > 0
740
- ? `${indent}Installed hippo's Codex memory hooks (${added.join(', ')}) in ${result.settingsPath}`
741
- : `${indent}hippo's Codex memory hooks already in ${result.settingsPath}`);
742
- console.log(`${indent}${CODEX_TRUST_LINE}`);
743
- }
744
525
  /** Claude Code settings hooks, Codex's hooks.json and the OpenCode plugin, under the home directory; idempotent, so re-running init adds newer hooks. */
745
526
  function installUserLevelHooks(agents, codexHint) {
746
527
  for (const hook of agents) {
@@ -804,66 +585,6 @@ function installUserLevelHooks(agents, codexHint) {
804
585
  }
805
586
  }
806
587
  }
807
- /**
808
- * Set up a machine-level daily runner that sweeps all registered Hippo
809
- * workspaces.
810
- * Linux/macOS: writes to user crontab.
811
- * Windows: creates a scheduled task.
812
- * Skips if already installed.
813
- */
814
- function setupDailySchedule(globalRoot) {
815
- const runnerDir = path.resolve(globalRoot);
816
- // Reject paths with characters that could break shell/crontab quoting
817
- // (backslash is normal on Windows, only dangerous in Unix shell/crontab)
818
- const unsafeChars = process.platform === 'win32' ? /["`$%\n\r]/ : /["`$\n\r\\]/;
819
- if (unsafeChars.test(runnerDir)) {
820
- console.log(` Skipping schedule: runner path contains unsafe characters.`);
821
- return;
822
- }
823
- const isWindows = process.platform === 'win32';
824
- const taskName = DAILY_TASK_NAME;
825
- const cmd = buildDailyRunnerCommand(runnerDir);
826
- if (isWindows) {
827
- // Check if task already exists
828
- try {
829
- const existing = execSync(`schtasks /query /tn "${taskName}" 2>nul`, { encoding: 'utf-8', windowsHide: true });
830
- if (existing.includes(taskName)) {
831
- return; // already scheduled
832
- }
833
- }
834
- catch {
835
- // Task doesn't exist, create it
836
- }
837
- try {
838
- execFileSync('schtasks', buildSchtasksCreateArgs(taskName, cmd), { stdio: 'pipe', windowsHide: true });
839
- console.log(` Scheduled machine-level daily runner (6:15am) via Task Scheduler: ${taskName}`);
840
- }
841
- catch {
842
- // No admin rights or schtasks unavailable, fall back to printing instructions
843
- console.log(` To schedule the machine-level daily runner, run:`);
844
- console.log(` schtasks /create /tn "${taskName}" /tr "${buildWindowsTaskRun(cmd).replace(/"/g, '\\"')}" /sc daily /st 06:15`);
845
- }
846
- }
847
- else {
848
- // Unix: check crontab for existing entry
849
- const marker = `# hippo:${taskName}`;
850
- try {
851
- const existing = execSync('crontab -l 2>/dev/null', { encoding: 'utf-8', windowsHide: true });
852
- if (existing.includes(marker)) {
853
- return; // already scheduled
854
- }
855
- const cronLine = `15 6 * * * ${cmd} ${marker}`;
856
- const newCrontab = existing.trimEnd() + '\n' + cronLine + '\n';
857
- execSync('crontab -', { input: newCrontab, stdio: ['pipe', 'pipe', 'pipe'], windowsHide: true });
858
- console.log(` Scheduled machine-level daily runner (6:15am) via crontab`);
859
- }
860
- catch {
861
- const cronLine = `15 6 * * * ${cmd}`;
862
- console.log(` To schedule the machine-level daily runner, add to crontab (crontab -e):`);
863
- console.log(` ${cronLine}`);
864
- }
865
- }
866
- }
867
588
  // Shared by the direct write and the routed request so both store the same tags.
868
589
  function rememberTags(flags, cwd) {
869
590
  const requested = Array.isArray(flags['tag']) ? [...flags['tag']] : [];
@@ -1050,669 +771,177 @@ function cmdSupersede(hippoRoot, oldId, newContent, flags) {
1050
771
  emitCliAudit(hippoRoot, 'supersede', oldId, { newId: newEntry.id });
1051
772
  console.log(`Superseded ${oldId} → ${newEntry.id}`);
1052
773
  }
774
+ function failWith(message) {
775
+ return () => {
776
+ console.error(message);
777
+ process.exit(1);
778
+ };
779
+ }
780
+ /** `--graph-stream` implies rrf fusion as well as the graph stream; the CLI fuses the local store only. */
781
+ function parseGraphStreamFlags(flags) {
782
+ let hops;
783
+ if (flags['graph-hops'] !== undefined) {
784
+ if (typeof flags['graph-hops'] === 'boolean')
785
+ failWith(`--graph-hops requires an integer value 1..${MAX_HOPS} (e.g. --graph-hops 2).`)();
786
+ const h = Number(flags['graph-hops']);
787
+ if (!Number.isInteger(h) || h < 1 || h > MAX_HOPS) {
788
+ failWith(`Invalid --graph-hops: "${String(flags['graph-hops'])}". Must be an integer 1..${MAX_HOPS}.`)();
789
+ }
790
+ hops = h;
791
+ }
792
+ let seeds;
793
+ if (flags['graph-seeds'] !== undefined) {
794
+ if (typeof flags['graph-seeds'] === 'boolean')
795
+ failWith('--graph-seeds requires a positive integer value (e.g. --graph-seeds 10).')();
796
+ const s = Number(flags['graph-seeds']);
797
+ if (!Number.isInteger(s) || s < 1)
798
+ failWith(`Invalid --graph-seeds: "${String(flags['graph-seeds'])}". Must be a positive integer.`)();
799
+ seeds = s;
800
+ }
801
+ return { hops, seeds };
802
+ }
803
+ function parseHopsFlags(flags) {
804
+ if (flags['hops'] === undefined)
805
+ return {};
806
+ // A value-less `--hops` parses as true, and Number(true) === 1 would silently run a 1-hop expansion.
807
+ if (typeof flags['hops'] === 'boolean')
808
+ return { fail: failWith(`--hops requires an integer value 0..${MAX_HOPS} (e.g. --hops 1).`) };
809
+ const hops = Number(flags['hops']);
810
+ if (!Number.isInteger(hops) || hops < 0 || hops > MAX_HOPS) {
811
+ return { fail: failWith(`Invalid --hops: "${String(flags['hops'])}". Must be an integer 0..${MAX_HOPS}.`) };
812
+ }
813
+ const raw = flags['max-neighbors'];
814
+ if (raw === undefined)
815
+ return { value: { hops, maxNeighbors: DEFAULT_MAX_NEIGHBORS } };
816
+ if (typeof raw === 'boolean')
817
+ return { fail: failWith(`--max-neighbors requires an integer value 1..200.`) };
818
+ const maxNeighbors = Number(raw);
819
+ if (!Number.isInteger(maxNeighbors) || maxNeighbors < 1 || maxNeighbors > 200) {
820
+ return { fail: failWith(`Invalid --max-neighbors: "${String(raw)}". Must be an integer 1..200.`) };
821
+ }
822
+ return { value: { hops, maxNeighbors } };
823
+ }
824
+ function parseRerankerFlag(flags) {
825
+ const name = flags['reranker'] !== undefined ? String(flags['reranker']).trim() : '';
826
+ let fn;
827
+ try {
828
+ fn = getReranker(name);
829
+ }
830
+ catch (err) {
831
+ // An unknown name throws to the top-level handler, as it did when the lookup sat mid-pipeline.
832
+ return { fail: () => { throw err; } };
833
+ }
834
+ if (!fn)
835
+ return {};
836
+ const topK = flags['reranker-top-k'] !== undefined
837
+ ? parseInt(String(flags['reranker-top-k']), 10)
838
+ : name === 'jev' ? JEV_DEFAULT_TOP_K : 50;
839
+ return { value: { fn, topK } };
840
+ }
841
+ function parseSalienceFlag(flags) {
842
+ const raw = flags['salience-threshold'];
843
+ if (raw === undefined)
844
+ return {};
845
+ const threshold = Number(raw);
846
+ if (!Number.isFinite(threshold) || threshold <= 0) {
847
+ return { fail: failWith(`Invalid --salience-threshold: "${String(raw)}". Must be a positive number.`) };
848
+ }
849
+ return { value: threshold };
850
+ }
851
+ function parseChoiceFlag(flags, name, valid) {
852
+ const value = flags[name] !== undefined ? String(flags[name]).trim() : '';
853
+ if (!value)
854
+ return {};
855
+ if (!valid.includes(value))
856
+ return { fail: failWith(`Invalid --${name}: "${value}". Must be one of: ${valid.join(', ')}.`) };
857
+ return { value };
858
+ }
859
+ /** Parses every flag a ranking stage reads; the first invalid one in pipeline order becomes `error`. */
860
+ function parseRecallLateFlags(flags) {
861
+ const graphHops = parseHopsFlags(flags);
862
+ const reranker = parseRerankerFlag(flags);
863
+ const salience = parseSalienceFlag(flags);
864
+ const outcome = parseChoiceFlag(flags, 'outcome', ['success', 'failure', 'partial']);
865
+ const layer = parseChoiceFlag(flags, 'layer', Object.values(Layer));
866
+ const staged = [
867
+ ['expand', graphHops.fail], ['rerank', reranker.fail], ['salience', salience.fail], ['outcome', outcome.fail], ['layer', layer.fail],
868
+ ];
869
+ let error;
870
+ for (const [stage, fail] of staged) {
871
+ if (fail) {
872
+ error = { stage, fail };
873
+ break;
874
+ }
875
+ }
876
+ return {
877
+ graphHops: graphHops.value,
878
+ reranker: reranker.value,
879
+ salienceThreshold: salience.value,
880
+ outcome: outcome.value,
881
+ layer: layer.value,
882
+ error,
883
+ };
884
+ }
1053
885
  async function cmdRecall(hippoRoot, query, flags) {
1054
886
  requireInit(hippoRoot);
1055
887
  const budget = parseBudgetFlag(flags['budget'], 4000);
1056
888
  const limit = parseLimitFlag(flags['limit']);
1057
889
  const asJson = Boolean(flags['json']);
1058
890
  const showWhy = Boolean(flags['why']);
1059
- const forcePhysics = Boolean(flags['physics']);
1060
- const forceClassic = Boolean(flags['classic']);
1061
891
  const includeSuperseded = Boolean(flags['include-superseded']);
1062
- const asOf = typeof flags['as-of'] === 'string' ? flags['as-of'] : undefined;
1063
- if (asOf !== undefined && Number.isNaN(new Date(asOf).getTime())) {
1064
- console.error(`Error: --as-of value "${asOf}" is not a valid ISO date (e.g. 2026-04-22 or 2026-04-22T12:00:00Z).`);
1065
- process.exit(1);
1066
- }
892
+ const asOf = parseAsOfFlag(flags);
1067
893
  const globalRoot = getGlobalRoot();
1068
894
  const primaryIsGlobal = isGlobalStoreRoot(hippoRoot);
1069
- // A5 stub auth: resolve the active tenant once and thread it through every
1070
- // recall-time SELECT against `memories`. Cross-tenant rows must never surface.
895
+ // Cross-tenant rows must never surface, so the tenant is resolved once and threaded through every load.
1071
896
  const tenantId = resolveTenantId({});
1072
- // v1.25.0 (v39 follow-up #1): the direct CLI recall path applies the recall
1073
- // scope rule. SQL half via loadRecallSearchEntries in 'additive' mode —
1074
- // default-deny (`unknown:legacy` quarantine) always, and an explicit
1075
- // `--scope X` UNLOCKS envelope scope X on top of the default-admitted set
1076
- // (the CLI flag is historically a tag-boost hint over scope-NULL rows, so
1077
- // api.recall's narrowing exact-match would empty every tag-scoped recall —
1078
- // see passesCliRecallScopeFilter in recall-scope.ts). The regex-only
1079
- // `<source>:private:*` half is the JS post-filter below. Hoisted here
1080
- // (it previously lived with the boost flags): recallExplicitScope is the
1081
- // FILTER input; recallActiveScope (which falls back to detectScope()) stays
1082
- // boost-only — auto-detection must never become a filter input or
1083
- // detected-project recalls would change shape.
897
+ // The explicit --scope is the filter input; the detected scope only boosts, so auto-detection never filters.
1084
898
  const recallExplicitScope = flags['scope'] !== undefined ? String(flags['scope']).trim() : null;
1085
- const requestedScopeForFilter = recallExplicitScope || undefined;
1086
- const loadSuperseded = includeSuperseded || Boolean(asOf);
1087
- let localEntries = loadRecallSearchEntries(hippoRoot, query, undefined, tenantId, requestedScopeForFilter, 'additive', loadSuperseded);
1088
- let globalEntries = globalRoot !== hippoRoot && isInitialized(globalRoot)
1089
- ? loadRecallSearchEntries(globalRoot, query, undefined, tenantId, requestedScopeForFilter, 'additive', loadSuperseded)
1090
- : [];
1091
- // v1.12.13 / C5 — WYSIATI counters. Track filter activity per the plan v3
1092
- // Task 3 mapping table. dropped_pre_rank is the SUM of all non-budget
1093
- // filter drops (pre-rank AND post-rank); its meaning is unchanged by C5.
1094
- //
1095
- // C5 (2026-08-24): search-engine internal drops (scored-to-zero rows that
1096
- // hybridSearch/physicsSearch returns fewer of than they were given) now
1097
- // count toward droppedByBudget, not "not counted at all" as the old v1
1098
- // convention had it. That old convention is exactly why the `Cutoff:` line
1099
- // never printed: cmdRecall measured droppedByBudget from `results.length -
1100
- // limit` (cli.ts ~1547) AFTER the search call, but `results` had already
1101
- // been ranked and truncated by the search engine to a handful of rows, so
1102
- // `limit < results.length` was almost always false and the counter stayed
1103
- // 0 while hundreds of candidates silently vanished (see the plan's measured
1104
- // table: 397 of 400 candidates gone, every counter reading 0). droppedByBudget
1105
- // is now derived as "everything not attributed to a named pre-rank filter",
1106
- // computed after the final `--limit` slice — see the definition near
1107
- // line ~1545 for the exact formula and the double-count argument.
1108
- // totalCandidates = post-SQL-predicate count (api.recall parity: measured
1109
- // after loadRecallSearchEntries, before the JS scope filter). NOTE the
1110
- // v1.12.13 accounting convention: SQL-excluded rows (quarantine + the
1111
- // v1.25.0 pre-window ':private:' exclusion) are pre-candidate and are NOT
1112
- // counted as drops; the JS half below normally drops 0 and exists as
1113
- // defense-in-depth (LIKE/regex divergence, exact-mode mismatch).
1114
- const totalCandidatesCountCmd = localEntries.length + globalEntries.length;
1115
- let droppedPreRankCountCmd = 0;
1116
- // Graph expansion adds candidates AFTER totalCandidatesCountCmd is taken,
1117
- // so they are folded back in before the budget residual is derived.
1118
- let graphAddedCountCmd = 0;
1119
- // v1.25.0: JS half of the recall scope rule (private-scope regex deny with
1120
- // explicit-request unlock), via the canonical helper — do not inline a
1121
- // fourth copy of this predicate.
1122
- const passesRecallScope = (e) => api.passesCliRecallScopeFilter(e.scope ?? null, requestedScopeForFilter);
1123
- const beforeScopeFilterCmd = localEntries.length + globalEntries.length;
1124
- localEntries = localEntries.filter(passesRecallScope);
1125
- globalEntries = globalEntries.filter(passesRecallScope);
1126
- droppedPreRankCountCmd += beforeScopeFilterCmd - (localEntries.length + globalEntries.length);
1127
- // Bi-temporal filtering for physics path (hybridSearch handles it internally)
1128
- if (asOf) {
1129
- const filterAsOf = (entries) => {
1130
- const asOfDate = new Date(asOf);
1131
- const successorValidFrom = new Map();
1132
- for (const e of entries) {
1133
- if (e.superseded_by) {
1134
- const successor = entries.find(s => s.id === e.superseded_by);
1135
- if (successor)
1136
- successorValidFrom.set(e.id, successor.valid_from);
1137
- }
1138
- }
1139
- return entries.filter(e => {
1140
- if (new Date(e.valid_from) > asOfDate)
1141
- return false;
1142
- if (!e.superseded_by)
1143
- return true;
1144
- const succVf = successorValidFrom.get(e.id);
1145
- return succVf ? new Date(succVf) > asOfDate : true;
1146
- });
1147
- };
1148
- const beforeAsOf = localEntries.length + globalEntries.length;
1149
- localEntries = filterAsOf(localEntries);
1150
- globalEntries = filterAsOf(globalEntries);
1151
- droppedPreRankCountCmd += beforeAsOf - (localEntries.length + globalEntries.length);
1152
- }
1153
- else if (!includeSuperseded) {
1154
- const beforeSupersededDrop = localEntries.length + globalEntries.length;
1155
- localEntries = localEntries.filter(e => !e.superseded_by);
1156
- globalEntries = globalEntries.filter(e => !e.superseded_by);
1157
- droppedPreRankCountCmd += beforeSupersededDrop - (localEntries.length + globalEntries.length);
1158
- }
1159
- const hasGlobal = globalEntries.length > 0;
1160
- // Determine search mode: --physics forces physics, --classic forces BM25+cosine,
1161
- // default uses physics if config.physics.enabled is not false
1162
899
  const config = loadConfig(hippoRoot);
1163
- const usePhysics = forcePhysics
1164
- || (!forceClassic && config.physics.enabled !== false);
1165
- const noMmr = Boolean(flags['no-mmr']);
1166
- const mmrLambda = flags['mmr-lambda'] !== undefined
1167
- ? parseFloat(String(flags['mmr-lambda']))
1168
- : config.mmr.lambda;
1169
- const mmrEnabled = !noMmr && config.mmr.enabled;
1170
- const localBump = flags['equal-sources']
1171
- ? 1.0
1172
- : flags['local-bump'] !== undefined
1173
- ? parseFloat(String(flags['local-bump']))
1174
- : config.search.localBump;
1175
900
  const minResults = flags['min-results'] !== undefined
1176
901
  ? parseInt(String(flags['min-results']), 10)
1177
902
  : undefined;
1178
- // recallExplicitScope hoisted above the candidate loads (v1.25.0) — see the
1179
- // scope-filter block near the top of cmdRecall.
1180
903
  const recallActiveScope = recallExplicitScope || detectScope();
1181
- const useMultihop = flags['multihop'] === true || config.multihop.enabled;
1182
- // L1 — graph-retrieval stream. `--graph-stream` forces rrf fusion + the graph stream
1183
- // (see src/graph-stream.ts). NOTE: it implies `scoring:'rrf'` (production default is
1184
- // 'blend'), so the flag bundles two behaviours by design. CLI surface is local-store
1185
- // only for now; the library API supports a globalRoot for callers who need it.
1186
- const useGraphStream = flags['graph-stream'] === true;
1187
- let graphStreamHops;
1188
- if (useGraphStream && flags['graph-hops'] !== undefined) {
1189
- if (typeof flags['graph-hops'] === 'boolean') {
1190
- console.error(`--graph-hops requires an integer value 1..${MAX_HOPS} (e.g. --graph-hops 2).`);
1191
- process.exit(1);
1192
- }
1193
- const h = Number(flags['graph-hops']);
1194
- if (!Number.isInteger(h) || h < 1 || h > MAX_HOPS) {
1195
- console.error(`Invalid --graph-hops: "${String(flags['graph-hops'])}". Must be an integer 1..${MAX_HOPS}.`);
1196
- process.exit(1);
1197
- }
1198
- graphStreamHops = h;
1199
- }
1200
- let graphStreamSeeds;
1201
- if (useGraphStream && flags['graph-seeds'] !== undefined) {
1202
- if (typeof flags['graph-seeds'] === 'boolean') {
1203
- console.error('--graph-seeds requires a positive integer value (e.g. --graph-seeds 10).');
1204
- process.exit(1);
1205
- }
1206
- const s = Number(flags['graph-seeds']);
1207
- if (!Number.isInteger(s) || s < 1) {
1208
- console.error(`Invalid --graph-seeds: "${String(flags['graph-seeds'])}". Must be a positive integer.`);
1209
- process.exit(1);
1210
- }
1211
- graphStreamSeeds = s;
1212
- }
904
+ const graphStream = flags['graph-stream'] === true ? parseGraphStreamFlags(flags) : undefined;
905
+ const late = parseRecallLateFlags(flags);
906
+ const goalTag = flags['goal'] !== undefined ? String(flags['goal']).trim() : '';
907
+ const sessionId = (flags['session-id'] !== undefined
908
+ ? String(flags['session-id'])
909
+ : process.env.HIPPO_SESSION_ID ?? '').trim();
1213
910
  // Engines spend the budget on the text each result prints as, less the header, so selection and print agree.
1214
911
  const localIndex = loadIndex(hippoRoot);
1215
912
  const globalOn = isInitialized(globalRoot);
1216
913
  const entryText = (r) => recallEntryText(r, query, showWhy, primaryIsGlobal || (globalOn && !localIndex.entries[r.entry.id]));
1217
914
  const printCost = (r) => printedTokens(entryText(r));
1218
915
  const entryBudget = Math.max(0, budget - printedTokens(recallHeading(budget, budget, query)));
1219
- let results;
1220
- if (useGraphStream) {
1221
- if (!isEmbeddingAvailable()) {
1222
- // The graph stream fuses inside the rrf path, which only runs with embeddings; without
1223
- // them hybridSearch falls back to BM25-only and the stream is inert. Say so plainly
1224
- // rather than silently no-op.
1225
- console.error('[note] --graph-stream needs embeddings (rrf fusion); none available, so the graph stream is inert. Run `hippo embed` first.');
1226
- }
1227
- if (hasGlobal) {
1228
- console.error('[note] --graph-stream searches the local store only; global graph fusion is a follow-up.');
1229
- }
1230
- // The stream anchors on the top-`seedCount` lexical hits and re-ranks the rank>seedCount
1231
- // tail; on a pool with <= seedCount candidates EVERY candidate is a seed and the stream
1232
- // is inert (it degrades to the 2-list fusion). Tune the anchor count with --graph-seeds.
1233
- results = await hybridSearch(query, localEntries, {
1234
- budget: entryBudget, cost: printCost, hippoRoot, mmr: mmrEnabled, mmrLambda, minResults, scope: recallActiveScope,
1235
- includeSuperseded, asOf,
1236
- scoring: 'rrf',
1237
- graphStream: { weight: DEFAULT_GRAPH_STREAM_WEIGHT, tenantId, hops: graphStreamHops, seedCount: graphStreamSeeds },
1238
- });
1239
- }
1240
- else if (useMultihop) {
1241
- // Unlike searchBothHybrid below, multihop ranks one pooled list, so a shared memory's two copies both compete.
1242
- const allEntries = api.oneCopyPerMemory(localEntries, globalEntries, evalNow()).flat();
1243
- results = multihopSearch(query, allEntries, {
1244
- budget: entryBudget,
1245
- cost: printCost,
1246
- hippoRoot,
1247
- minResults,
1248
- includeSuperseded,
1249
- asOf,
1250
- });
1251
- }
1252
- else if (usePhysics && !hasGlobal) {
1253
- results = await physicsSearch(query, localEntries, {
1254
- budget: entryBudget,
1255
- cost: printCost,
1256
- hippoRoot,
1257
- physicsConfig: config.physics,
1258
- minResults,
1259
- scope: recallActiveScope,
1260
- includeSuperseded,
1261
- asOf,
1262
- });
1263
- }
1264
- else if (hasGlobal) {
1265
- // Use searchBothHybrid for merged results with embedding support.
1266
- // recallScope (v1.25.0): searchBothHybrid re-loads candidates internally,
1267
- // so the scope rule must be plumbed in — the filtered localEntries /
1268
- // globalEntries above are NOT what this path ranks.
1269
- results = await searchBothHybrid(query, hippoRoot, globalRoot, {
1270
- budget: entryBudget, cost: printCost, mmr: mmrEnabled, mmrLambda, localBump, minResults, scope: recallActiveScope, tenantId,
1271
- includeSuperseded, asOf,
1272
- recallScope: recallExplicitScope
1273
- ? { requested: recallExplicitScope, additive: true }
1274
- : {},
1275
- });
1276
- }
1277
- else {
1278
- results = await hybridSearch(query, localEntries, {
1279
- budget: entryBudget, cost: printCost, hippoRoot, mmr: mmrEnabled, mmrLambda, minResults, scope: recallActiveScope,
1280
- includeSuperseded, asOf,
1281
- });
1282
- }
1283
- // E3.2 multi-hop graph recall. After the base branch produces `results`, optionally
1284
- // augment with memories reached by walking the entities/relations graph `--hops N` out
1285
- // from the lexical seeds. Runs BEFORE the opt-in re-rankers below so graph-reached
1286
- // results are first-class candidates in any downstream re-ranking / --why. Default OFF
1287
- // (absent or 0 = no-op). Reached memories are loaded directly by id (NOT via the
1288
- // lexical candidate set, which would exclude the orthogonal neighbours graph recall
1289
- // exists to surface); the engine re-applies the same superseded/asOf hard filters.
1290
- if (flags['hops'] !== undefined) {
1291
- // Reject a value-less `--hops` (parseArgs stores boolean true): Number(true) === 1
1292
- // would otherwise silently run a 1-hop expansion when the user fat-fingered the value.
1293
- if (typeof flags['hops'] === 'boolean') {
1294
- console.error(`--hops requires an integer value 0..${MAX_HOPS} (e.g. --hops 1).`);
1295
- process.exit(1);
1296
- }
1297
- const hops = Number(flags['hops']);
1298
- if (!Number.isInteger(hops) || hops < 0 || hops > MAX_HOPS) {
1299
- console.error(`Invalid --hops: "${String(flags['hops'])}". Must be an integer 0..${MAX_HOPS}.`);
1300
- process.exit(1);
1301
- }
1302
- let maxNeighbors = DEFAULT_MAX_NEIGHBORS;
1303
- if (flags['max-neighbors'] !== undefined) {
1304
- if (typeof flags['max-neighbors'] === 'boolean') {
1305
- console.error(`--max-neighbors requires an integer value 1..200.`);
1306
- process.exit(1);
1307
- }
1308
- maxNeighbors = Number(flags['max-neighbors']);
1309
- if (!Number.isInteger(maxNeighbors) || maxNeighbors < 1 || maxNeighbors > 200) {
1310
- console.error(`Invalid --max-neighbors: "${String(flags['max-neighbors'])}". Must be an integer 1..200.`);
1311
- process.exit(1);
1312
- }
1313
- }
1314
- if (hops > 0) {
1315
- // graphExpandRecall can SURFACE rows that were never in the lexical
1316
- // candidate pool (a graph neighbour reached by entity edge, not by
1317
- // query match), and totalCandidatesCountCmd was snapshotted before the
1318
- // search. Without this the derived budget count goes negative, clamps
1319
- // to 0, and the accounting silently breaks: 1 candidate, 2 returned,
1320
- // 0 drops. Found independently by two reviewers. Count the additions
1321
- // so the invariant holds on graph-expanded recalls too.
1322
- // GROSS, not net. graphExpandRecall both adds neighbours AND evicts weak
1323
- // base rows in one call (graph-recall.ts:285), so a net delta of 0 hides
1324
- // 3 added + 3 evicted: the additions escape the candidate total and the
1325
- // evictions escape the drop count, and the Cutoff line goes silent again.
1326
- // Compare ID sets so both directions are counted.
1327
- const beforeGraphIds = new Set(results.map((r) => r.entry.id));
1328
- results = graphExpandRecall(results, {
1329
- hops,
1330
- maxNeighbors,
1331
- hippoRoot,
1332
- globalRoot: isInitialized(globalRoot) && globalRoot !== hippoRoot ? globalRoot : undefined,
1333
- tenantId,
1334
- includeSuperseded,
1335
- asOf,
1336
- budget: entryBudget,
1337
- cost: printCost,
1338
- minResults: minResults ?? 1,
1339
- recallScope: recallExplicitScope
1340
- ? { requested: recallExplicitScope, additive: true }
1341
- : {},
1342
- });
1343
- // Rows the graph surfaced that the lexical pool never held.
1344
- for (const r of results) {
1345
- if (!beforeGraphIds.has(r.entry.id))
1346
- graphAddedCountCmd++;
1347
- }
1348
- }
1349
- }
1350
- // ACC EVC-adaptive recall (RESEARCH.md §PFC.ACC). When the initial top-K is
1351
- // dominated by lexically similar but distinct memories (high pairwise token
1352
- // overlap = same topic, different facts = conflict), allocate extra retrieval
1353
- // effort: take a wider candidate pool, drop low-relevance distractors, and
1354
- // re-rank by recency to surface the most up-to-date item from the cluster.
1355
- // Default off; opt-in via --evc-adaptive.
1356
- if (flags['evc-adaptive'] && results.length >= 2) {
1357
- const sliceSize = Math.min(3, results.length);
1358
- const slice = results.slice(0, sliceSize);
1359
- let pairs = 0;
1360
- let overlapSum = 0;
1361
- for (let i = 0; i < slice.length; i++) {
1362
- for (let j = i + 1; j < slice.length; j++) {
1363
- overlapSum += textOverlap(slice[i].entry.content, slice[j].entry.content);
1364
- pairs++;
1365
- }
1366
- }
1367
- const avgOverlap = pairs > 0 ? overlapSum / pairs : 0;
1368
- if (avgOverlap >= 0.4) {
1369
- const poolSize = Math.min(results.length, Math.max(sliceSize * 3, 9));
1370
- const pool = results.slice(0, poolSize);
1371
- const tail = results.slice(poolSize);
1372
- const maxScore = pool.reduce((m, r) => Math.max(m, r.score), 0);
1373
- const scoreFloor = maxScore * 0.5;
1374
- // On-topic test: score floor OR query coverage. The score floor alone
1375
- // proxied topicality via ranking score, but the disambiguating update
1376
- // this mechanic exists to surface is BY NATURE phrased differently
1377
- // (weaker lexical/embedding overlap), so under the #t2 embed-text
1378
- // format (docs/plans/2026-07-09-recall-determinism.md T1, which
1379
- // de-compressed similarity gaps) it fell below any sane floor
1380
- // (measured 0.33x max on the acc-evc micro fixture). Query coverage —
1381
- // the fraction of query tokens present in the candidate — is the
1382
- // mechanic's own definition of "same topic, different fact" applied
1383
- // to the query, and is score-scale-independent. Principled EVC
1384
- // calibration remains roadmapped as B1 depth.
1385
- const queryTokens = new Set(tokenizeQuery(query));
1386
- const onTopic = [];
1387
- const offTopic = [];
1388
- for (const r of pool) {
1389
- let hits = 0;
1390
- if (queryTokens.size > 0) {
1391
- const candTokens = new Set(tokenizeQuery(r.entry.content));
1392
- for (const t of queryTokens)
1393
- if (candTokens.has(t))
1394
- hits++;
1395
- }
1396
- const queryCoverage = queryTokens.size > 0 ? hits / queryTokens.size : 0;
1397
- (r.score >= scoreFloor || queryCoverage >= 0.6 ? onTopic : offTopic).push(r);
1398
- }
1399
- // Recency is the true primary key (unchanged) — that's the whole
1400
- // point of --evc-adaptive. compareEntryIdentity is only a TAIL for
1401
- // entries created at the exact same timestamp, which previously fell
1402
- // to array/scan order (T2, deterministic tie keys).
1403
- onTopic.sort((a, b) => {
1404
- const ta = new Date(a.entry.created).getTime();
1405
- const tb = new Date(b.entry.created).getTime();
1406
- return tb !== ta ? tb - ta : compareEntryIdentity(a.entry, b.entry);
1407
- });
1408
- results = [...onTopic, ...offTopic, ...tail];
1409
- }
1410
- }
1411
- // vlPFC interference filter (RESEARCH.md §PFC.vlPFC). Suppress task-irrelevant
1412
- // memories using *recorded* supersession + conflict structure only. Default
1413
- // off; opt-in via --filter-conflicts. Two effects, both surgical:
1414
- // 1. Drop entries with `superseded_by` set. (No-op under default recall,
1415
- // which already filters them; matters when `--include-superseded` was
1416
- // passed. The flag re-asserts the gate.)
1417
- // 2. Apply a 0.3x score multiplier to entries whose `conflicts_with` list
1418
- // references another entry that ALSO appears in the result set. The
1419
- // multiplier is conservative — we never delete on conflict, only
1420
- // down-rank, so the user can still surface the loser via --include-*.
1421
- // We never infer conflicts from lexical overlap. The v1 salience gate did
1422
- // that and destroyed LoCoMo (0.28 → 0.02). Recorded structure only.
1423
- if (flags['filter-conflicts']) {
1424
- const beforeFilterConflicts = results.length;
1425
- results = results.filter((r) => !r.entry.superseded_by);
1426
- droppedPreRankCountCmd += beforeFilterConflicts - results.length;
1427
- const presentIds = new Set(results.map((r) => r.entry.id));
1428
- results = results.map((r) => {
1429
- const peers = r.entry.conflicts_with || [];
1430
- const hasPeerInResults = peers.some((peerId) => presentIds.has(peerId));
1431
- if (!hasPeerInResults)
1432
- return r;
1433
- const next = { ...r, score: r.score * 0.3 };
1434
- // A7 recall-trace: interference (vlPFC) down-rank. Only when --why.
1435
- if (showWhy) {
1436
- next.rerankTrace = [
1437
- ...(r.rerankTrace ?? []),
1438
- { stage: 'interference', multiplier: 0.3, scoreBefore: r.score, scoreAfter: next.score },
1439
- ];
1440
- }
1441
- return next;
1442
- });
1443
- // T2 note: PLAIN stable score sort on purpose (here and in the rerank
1444
- // blocks below) -- these are RE-SORTS of an already deterministically-
1445
- // ordered ranking, so sort stability inherits the upstream content-tail
1446
- // determinism, and ties preserve the prior rank rather than reordering
1447
- // by content (a no-signal boost must not shuffle its input).
1448
- results.sort((a, b) => b.score - a.score);
1449
- }
1450
- // vmPFC continuous value attribution (RESEARCH.md §PFC.vmPFC). Continuous
1451
- // value scoring per memory based on cumulative outcome attribution. Memories
1452
- // with positive cumulative outcomes are boosted; those with negative outcomes
1453
- // are demoted. The multiplier is a tanh-shaped function clamped to [0.7, 1.3]
1454
- // — wider than the always-on outcomeBoost (which clamps [0.85, 1.15]) so this
1455
- // flag has additional decisive effect when value attribution should drive
1456
- // ranking. Default off; opt-in via --value-aware. Reuses outcome_positive /
1457
- // outcome_negative columns; no schema change.
1458
- if (flags['value-aware'] && results.length >= 1) {
1459
- results = results.map((r) => {
1460
- const pos = r.entry.outcome_positive ?? 0;
1461
- const neg = r.entry.outcome_negative ?? 0;
1462
- if (pos === 0 && neg === 0)
1463
- return r;
1464
- const raw = 1 + 0.3 * Math.tanh(pos - neg);
1465
- const valueMult = Math.max(0.7, Math.min(1.3, raw));
1466
- const next = { ...r, score: r.score * valueMult };
1467
- // A7 recall-trace: vmPFC continuous value attribution. Only when --why.
1468
- if (showWhy) {
1469
- next.rerankTrace = [
1470
- ...(r.rerankTrace ?? []),
1471
- { stage: 'value', multiplier: valueMult, scoreBefore: r.score, scoreAfter: next.score },
1472
- ];
1473
- }
1474
- return next;
1475
- });
1476
- results.sort((a, b) => b.score - a.score); // T2: plain stable re-sort, see --filter-conflicts note
1477
- }
1478
- // OFC option-value re-ranker MVP (RESEARCH.md §PFC.OFC). Combine relevance,
1479
- // strength, and integration cost into a single utility score and re-sort.
1480
- // OFC neurons encode a "common currency" across heterogeneous attributes
1481
- // (Rangel et al., 2008); this is the simplest demonstration of that mechanism.
1482
- // Default off; opt-in via --rerank-utility.
1483
- //
1484
- // utility = score * (0.5 + 0.5 * strength) * (1 - cost_factor)
1485
- // cost_factor = min(0.3, tokens / 10000)
1486
- //
1487
- // The full OFC spec (option_valuation table in RESEARCH.md) decomposes value
1488
- // into reward / cost / risk / confidence components. The MVP collapses these
1489
- // to: score (relevance proxy), strength (persistence proxy), tokens (cost).
1490
- // CAVEAT: cost penalty is monotone with token count; LoCoMo's harder QAs
1491
- // often live in long evidence-rich memories. Default off — needs LoCoMo
1492
- // eval before enabling broadly.
1493
- if (flags['rerank-utility']) {
1494
- results = results
1495
- .map((r) => {
1496
- const strength = typeof r.entry.strength === 'number' ? r.entry.strength : 1.0;
1497
- const costFactor = Math.min(0.3, (r.tokens || 0) / 10000);
1498
- const utilityMult = (0.5 + 0.5 * strength) * (1 - costFactor);
1499
- const utility = r.score * utilityMult;
1500
- const next = { ...r, score: utility };
1501
- // A7 recall-trace: OFC option-value re-rank. Only when --why.
1502
- if (showWhy) {
1503
- next.rerankTrace = [
1504
- ...(r.rerankTrace ?? []),
1505
- { stage: 'utility', multiplier: utilityMult, scoreBefore: r.score, scoreAfter: utility },
1506
- ];
1507
- }
1508
- return next;
1509
- })
1510
- .sort((a, b) => b.score - a.score); // T2: plain stable re-sort, see --filter-conflicts note
1511
- }
1512
- // F6 reranker pass (docs/plans/2026-05-10-f6-reranker-hardening.md). When
1513
- // --reranker <name> is set, look up the reranker fn from the registry
1514
- // (src/rerankers/index.ts) and apply it to the top-K candidates. The
1515
- // reranker reorders (and may rescale) results; the post-budget set is
1516
- // returned. Default off; opt-in via --reranker <cross-encoder|jev|llm>. The
1517
- // structurally similar --rerank-utility block above is the OFC MVP and is
1518
- // independent — both can run in the same recall, with --rerank-utility
1519
- // applied first. Available rerankers: cross-encoder, jev, llm (see
1520
- // src/rerankers/index.ts). The Track 1 `features` reranker was removed in
1521
- // v1.9.1 per the F10 HARD RETRACTION; it is no longer a valid value.
1522
- const rerankerName = flags['reranker'] !== undefined ? String(flags['reranker']).trim() : '';
1523
- if (rerankerName) {
1524
- const rerankerFn = getReranker(rerankerName);
1525
- if (rerankerFn) {
1526
- const topK = flags['reranker-top-k'] !== undefined
1527
- ? parseInt(String(flags['reranker-top-k']), 10)
1528
- : rerankerName === 'jev' ? JEV_DEFAULT_TOP_K : 50;
1529
- const head = results.slice(0, topK);
1530
- const tail = results.slice(topK);
1531
- const rerankInput = head.map((r, i) => ({ ...r, preRerankRank: i + 1 }));
1532
- const reranked = await rerankerFn(query, rerankInput, { topK });
1533
- // Copy rerankScore into score so downstream blocks (--goal, goal-stack,
1534
- // salience) that sort by `r.score` honor the reranker's order rather
1535
- // than unwinding it. Original score is preserved on rerankScore's
1536
- // input, but downstream sorters key on `score`.
1537
- const withPostRank = reranked.map((r, i) => {
1538
- const next = {
1539
- ...r,
1540
- score: r.rerankScore,
1541
- postRerankRank: i + 1,
1542
- };
1543
- // A7 recall-trace: F6 reranker pass (reorder + rescale; not a scalar
1544
- // multiply, so no multiplier field). Only when --why.
1545
- if (showWhy) {
1546
- next.rerankTrace = [
1547
- ...(r.rerankTrace ?? []),
1548
- { stage: 'reranker', scoreBefore: r.score, scoreAfter: r.rerankScore },
1549
- ];
1550
- }
1551
- return next;
1552
- });
1553
- results = [...withPostRank, ...tail];
1554
- }
1555
- }
1556
- // dlPFC goal-conditioned recall MVP (RESEARCH.md §PFC.dlPFC). When --goal
1557
- // <tag> is set, memories whose `tags` array contains the goal tag receive
1558
- // a 1.5x score boost and results are re-sorted. The full dlPFC spec
1559
- // (goal_stack + retrieval_policy tables) maintains a hierarchical task
1560
- // stack with weighted retrieval policies; this MVP collapses that to a
1561
- // single-tag boost — the smallest demonstrable goal-conditioning signal.
1562
- // Default off; opt-in via --goal <tag>. No schema change.
1563
- const goalTag = flags['goal'] !== undefined ? String(flags['goal']).trim() : '';
1564
- if (goalTag) {
1565
- results = results
1566
- .map((r) => {
1567
- if (!r.entry.tags?.includes(goalTag))
1568
- return r;
1569
- const boosted = { ...r, score: r.score * 1.5 };
1570
- // A7 recall-trace: the explicit `--goal <tag>` boost is a public CLI
1571
- // re-ranker (score *= 1.5) and is mutually exclusive with the session
1572
- // goal-stack boost below, so it must record its OWN step or `--why
1573
- // --goal` produces no ranking line (codex review). Stage `goal`
1574
- // (explicit flag) is distinct from `goal-boost` (session stack).
1575
- if (showWhy) {
1576
- boosted.rerankTrace = [
1577
- ...(r.rerankTrace ?? []),
1578
- { stage: 'goal', multiplier: 1.5, scoreBefore: r.score, scoreAfter: r.score * 1.5, note: `--goal ${goalTag}` },
1579
- ];
1580
- }
1581
- return boosted;
1582
- })
1583
- .sort((a, b) => b.score - a.score); // T2: plain stable re-sort, see --filter-conflicts note
1584
- }
1585
- // dlPFC depth (B3, v0.38; lifted v1.7.4 into applyGoalStackBoost). When
1586
- // HIPPO_SESSION_ID is set (env or --session-id flag) and the
1587
- // (tenant, session) has active goals, the helper boosts memories whose tags
1588
- // overlap any active goal's name and logs (memory, goal) pairs into
1589
- // goal_recall_log. Runs AFTER the explicit `--goal <tag>` block so an
1590
- // explicit flag always wins (gated on `goalTag === ''`).
1591
- const sessionId = (flags['session-id'] !== undefined
1592
- ? String(flags['session-id'])
1593
- : process.env.HIPPO_SESSION_ID ?? '').trim();
1594
- if (sessionId && goalTag === '') {
916
+ const rank = await rankRecall({ hippoRoot, globalRoot: globalRoot !== hippoRoot && globalOn ? globalRoot : undefined, tenantId, note: (line) => console.error(line) }, {
917
+ query, budget: entryBudget, cost: printCost, limit, why: showWhy, includeSuperseded, asOf,
918
+ explicitScope: recallExplicitScope, activeScope: recallActiveScope,
919
+ search: { ...engineFlags(flags, config), multihop: flags['multihop'] === true || config.multihop.enabled, graphStream, minResults, explain: false },
920
+ graphHops: late.graphHops,
921
+ evcAdaptive: Boolean(flags['evc-adaptive']),
922
+ filterConflicts: Boolean(flags['filter-conflicts']),
923
+ valueAware: Boolean(flags['value-aware']),
924
+ rerankUtility: Boolean(flags['rerank-utility']),
925
+ reranker: late.reranker,
926
+ goalTag,
927
+ sessionId,
928
+ salienceThreshold: late.salienceThreshold,
929
+ outcome: late.outcome,
930
+ layer: late.layer,
931
+ haltBefore: late.error?.stage,
932
+ });
933
+ if (rank.goalRecallLog.length > 0) {
1595
934
  const dbForGoals = openHippoDb(hippoRoot);
1596
- // A7 recall-trace: goal-boost is the shared helper, not an inline map. It
1597
- // writes its steps into this SEPARATE accumulator (keyed by entry id), NOT
1598
- // onto the row (the helper strips internal markers on re-spread). Only
1599
- // allocated under --why.
1600
- const goalBoostTrace = showWhy ? new Map() : undefined;
1601
935
  try {
1602
- results = applyGoalStackBoost(dbForGoals, results, {
1603
- sessionId,
1604
- tenantId,
1605
- limit,
1606
- ...(goalBoostTrace ? { trace: goalBoostTrace } : {}),
1607
- });
936
+ writeGoalRecallLog(dbForGoals, rank.goalRecallLog);
1608
937
  }
1609
938
  finally {
1610
939
  closeHippoDb(dbForGoals);
1611
940
  }
1612
- // Merge the accumulated goal-boost steps onto the matching SearchResult.
1613
- if (goalBoostTrace && goalBoostTrace.size > 0) {
1614
- results = results.map((r) => {
1615
- const step = goalBoostTrace.get(r.entry.id);
1616
- if (!step)
1617
- return r;
1618
- return { ...r, rerankTrace: [...(r.rerankTrace ?? []), step] };
1619
- });
1620
- }
1621
- }
1622
- // Pineal salience MVP (RESEARCH.md §"AI Pineal Gland — Intuition and Awareness
1623
- // Module"). When --salience-threshold T is set (T > 0), memories whose
1624
- // retrieval_count is below T are downweighted: score *= max(0.5, count / T).
1625
- // At or above T, no change. This makes salience emerge from USE — high-recall
1626
- // memories earn full ranking weight, low-recall memories are softly demoted.
1627
- //
1628
- // CRITICAL HISTORY: The v1 salience gate (60% lexical-overlap gate at memory
1629
- // CREATION time) destroyed LoCoMo recall (0.28 -> 0.02) by dropping same-
1630
- // session relevant turns at intake. See MEMORY.md "Hippo salience gate
1631
- // destroys benchmark recall". This v2 is the inverse:
1632
- // - retrieval-side only (no creation-time gating)
1633
- // - retrieval_count signal only (no lexical overlap, no novelty heuristic)
1634
- // - default OFF, opt-in via the flag (no behaviour change without it)
1635
- // - 0.5 floor so non-salient entries stay reachable, never dropped
1636
- // Reuses the existing retrieval_count column; no schema change.
1637
- const salienceThresholdRaw = flags['salience-threshold'];
1638
- if (salienceThresholdRaw !== undefined) {
1639
- const T = Number(salienceThresholdRaw);
1640
- if (!Number.isFinite(T) || T <= 0) {
1641
- console.error(`Invalid --salience-threshold: "${salienceThresholdRaw}". Must be a positive number.`);
1642
- process.exit(1);
1643
- }
1644
- results = results
1645
- .map((r) => {
1646
- const count = r.entry.retrieval_count ?? 0;
1647
- if (count >= T)
1648
- return r;
1649
- const mult = Math.max(0.5, count / T);
1650
- const next = { ...r, score: r.score * mult };
1651
- // A7 recall-trace: pineal salience (retrieval_count) down-weight. The
1652
- // stage names the ACTUAL transform (retrieval_count, not goal-stack).
1653
- // Only when --why.
1654
- if (showWhy) {
1655
- next.rerankTrace = [
1656
- ...(r.rerankTrace ?? []),
1657
- { stage: 'retrieval-count-downweight', multiplier: mult, scoreBefore: r.score, scoreAfter: next.score },
1658
- ];
1659
- }
1660
- return next;
1661
- })
1662
- .sort((a, b) => b.score - a.score); // T2: plain stable re-sort, see --filter-conflicts note
1663
- }
1664
- // --outcome filter: drop trace entries whose trace_outcome !== target.
1665
- // Non-trace entries pass through unaffected (traces are the only layer with
1666
- // a meaningful outcome; filtering non-traces by outcome would be incoherent).
1667
- const outcomeFilter = flags['outcome'] !== undefined ? String(flags['outcome']).trim() : '';
1668
- if (outcomeFilter) {
1669
- const validOutcomes = ['success', 'failure', 'partial'];
1670
- if (!validOutcomes.includes(outcomeFilter)) {
1671
- console.error(`Invalid --outcome: "${outcomeFilter}". Must be one of: ${validOutcomes.join(', ')}.`);
1672
- process.exit(1);
1673
- }
1674
- const beforeOutcomeFilter = results.length;
1675
- results = results.filter((r) => {
1676
- if (r.entry.layer !== Layer.Trace)
1677
- return true;
1678
- return r.entry.trace_outcome === outcomeFilter;
1679
- });
1680
- droppedPreRankCountCmd += beforeOutcomeFilter - results.length;
1681
- }
1682
- // --layer filter: strict, drops entries whose layer does not match.
1683
- const layerFilter = flags['layer'] !== undefined ? String(flags['layer']).trim() : '';
1684
- if (layerFilter) {
1685
- const validLayers = Object.values(Layer);
1686
- if (!validLayers.includes(layerFilter)) {
1687
- console.error(`Invalid --layer: "${layerFilter}". Must be one of: ${validLayers.join(', ')}.`);
1688
- process.exit(1);
1689
- }
1690
- const beforeLayerFilter = results.length;
1691
- results = results.filter((r) => r.entry.layer === layerFilter);
1692
- droppedPreRankCountCmd += beforeLayerFilter - results.length;
1693
- }
1694
- // v1.12.13 / C5 — WYSIATI dropped_by_budget counter. Apply the final
1695
- // `--limit` slice first, then derive the count ARITHMETICALLY as
1696
- // "everything lost that droppedPreRank did not already claim":
1697
- //
1698
- // droppedByBudget = totalCandidates - droppedPreRank - returned
1699
- //
1700
- // This is the invariant the plan requires (totalCandidates == droppedPreRank
1701
- // + droppedByBudget + returned) restated as an assignment, so it holds by
1702
- // construction rather than by two counters happening to agree. It also
1703
- // cannot double-count the post-search droppedPreRank sites (--filter-
1704
- // conflicts, --outcome, --layer, ~1270/1528/1541): those are subtracted
1705
- // once here, not re-counted, because this line does not re-walk any filter
1706
- // — it only compares the two totals already tracked above. Everything left
1707
- // over — search-engine internal rank-step drops AND the `--limit` slice
1708
- // itself — lands in droppedByBudget, per the C5 accounting change in the
1709
- // comment near line ~976. Clamped at 0 as a defensive floor: if a future
1710
- // pipeline change ever returns MORE rows than totalCandidates minus
1711
- // droppedPreRank (should not happen), report "nothing dropped" rather than
1712
- // a negative count.
1713
- if (limit < results.length) {
1714
- results = results.slice(0, limit);
1715
941
  }
942
+ late.error?.fail();
943
+ const { localEntries, globalEntries, totalCandidates: totalCandidatesCountCmd, droppedPreRank: droppedPreRankCountCmd, graphAdded: graphAddedCountCmd, } = rank;
944
+ let results = rank.results;
1716
945
  // Continuity assembly (--continuity). Lives BEFORE the zero-result branch
1717
946
  // so a no-match query with active continuity state still returns a useful
1718
947
  // resume packet. Same three tenant-scoped store helpers as api.recall.
@@ -1768,11 +997,11 @@ async function cmdRecall(hippoRoot, query, flags) {
1768
997
  results = shown(kept);
1769
998
  // J1, J2 and C5: each pipeline computes its hints over the list it returns, so they follow the list as it shrinks.
1770
999
  // HIPPO_ANCHORING=off and HIPPO_AVAILABILITY=off skip the work entirely.
1771
- const anchorRing = process.env.HIPPO_ANCHORING !== 'off' && sessionId
1000
+ const anchorRing = biasHintEnabled('anchoring') && sessionId
1772
1001
  ? getOrCreateRing(sessionRecallHistoryCli, buildSessionKey(tenantId, sessionId))
1773
1002
  : null;
1774
1003
  const queryHash = hashQueryText(query);
1775
- const availabilityPool = process.env.HIPPO_AVAILABILITY !== 'off'
1004
+ const availabilityPool = biasHintEnabled('availability')
1776
1005
  ? [...localEntries, ...globalEntries].map((e) => ({ id: e.id, created: e.created }))
1777
1006
  : null;
1778
1007
  const hintsFor = (list, held) => {
@@ -1852,7 +1081,7 @@ async function cmdRecall(hippoRoot, query, flags) {
1852
1081
  // Appended after every detect: anchoredOn feeds the cooldown for the next recall on this session.
1853
1082
  appendRecall(anchorRing, queryHash, results[0]?.entry.id ?? null, cmdAnchoringHint?.memoryId);
1854
1083
  }
1855
- else if (process.env.HIPPO_ANCHORING !== 'off') {
1084
+ else if (biasHintEnabled('anchoring')) {
1856
1085
  // SHA-256/16 per the recall-audit convention; hashQueryText is FNV-1a and brute-forceable on short queries.
1857
1086
  emitCliAudit(hippoRoot, 'recall_anchor_skipped_no_session', undefined, auditQueryFields(query));
1858
1087
  }
@@ -2039,131 +1268,49 @@ async function cmdRecall(hippoRoot, query, flags) {
2039
1268
  }
2040
1269
  emit(recallText);
2041
1270
  }
1271
+ /** The SQL predicate drops denied rows before the window, so an unscoped probe counts what the policy hides. */
1272
+ function noteScopeHidden(hippoRoot, globalRoot, query, tenantId, requested) {
1273
+ const probe = [
1274
+ ...loadSearchEntries(hippoRoot, query, undefined, tenantId),
1275
+ ...(globalRoot ? loadSearchEntries(globalRoot, query, undefined, tenantId) : []),
1276
+ ];
1277
+ // Window-capped, so the count is a floor on large stores; fine for a "why is my row missing" hint.
1278
+ const hidden = probe.filter((e) => !api.passesCliRecallScopeFilter(e.scope ?? null, requested)).length;
1279
+ if (hidden > 0) {
1280
+ console.error(`[note] ${hidden} candidate${hidden === 1 ? '' : 's'} hidden by recall scope policy (pass an explicit --scope to inspect).`);
1281
+ }
1282
+ }
2042
1283
  async function cmdExplain(hippoRoot, query, flags) {
2043
1284
  requireInit(hippoRoot);
2044
1285
  const budget = parseBudgetFlag(flags['budget'], 4000);
2045
1286
  const limit = parseLimitFlag(flags['limit']);
2046
1287
  const asJson = Boolean(flags['json']);
2047
- const forcePhysics = Boolean(flags['physics']);
2048
- const forceClassic = Boolean(flags['classic']);
2049
- const explainIncludeSuperseded = Boolean(flags['include-superseded']);
2050
- const explainAsOf = typeof flags['as-of'] === 'string' ? flags['as-of'] : undefined;
2051
- if (explainAsOf !== undefined && Number.isNaN(new Date(explainAsOf).getTime())) {
2052
- console.error(`Error: --as-of value "${explainAsOf}" is not a valid ISO date (e.g. 2026-04-22 or 2026-04-22T12:00:00Z).`);
2053
- process.exit(1);
2054
- }
1288
+ const includeSuperseded = Boolean(flags['include-superseded']);
1289
+ const asOf = parseAsOfFlag(flags);
2055
1290
  const globalRoot = getGlobalRoot();
2056
- // A5: scope explain results to the active tenant.
2057
1291
  const tenantId = resolveTenantId({});
2058
- // v1.25.0 (v39 follow-up #1): cmdExplain shares cmdRecall's leak class —
2059
- // same unscoped loads, same fix, same semantics (explain explains what
2060
- // recall would see). Flag hoisted above the loads; the note below keeps the
2061
- // command honest for an operator debugging a hidden row.
2062
- const explainExplicitScope = flags['scope'] !== undefined ? String(flags['scope']).trim() : null;
2063
- const explainRequestedScope = explainExplicitScope || undefined;
2064
- const explainLoadSuperseded = explainIncludeSuperseded || Boolean(explainAsOf);
2065
- let explainLocalEntries = loadRecallSearchEntries(hippoRoot, query, undefined, tenantId, explainRequestedScope, 'additive', explainLoadSuperseded);
2066
- let explainGlobalEntries = isInitialized(globalRoot) ? loadRecallSearchEntries(globalRoot, query, undefined, tenantId, explainRequestedScope, 'additive', explainLoadSuperseded) : [];
2067
- const passesExplainScope = (e) => api.passesCliRecallScopeFilter(e.scope ?? null, explainRequestedScope);
2068
- explainLocalEntries = explainLocalEntries.filter(passesExplainScope);
2069
- explainGlobalEntries = explainGlobalEntries.filter(passesExplainScope);
2070
- // Honesty note (grill finding #2): the SQL predicate excludes denied rows
2071
- // BEFORE the candidate window (codex P2 fix), so the pipeline never sees
2072
- // them. For the diagnostic note only, probe the unscoped window and count
2073
- // what the scope policy hides — window-capped, so the count is a floor on
2074
- // large stores, which is fine for a "why is my row missing" hint.
2075
- const explainUnscopedProbe = [
2076
- ...loadSearchEntries(hippoRoot, query, undefined, tenantId),
2077
- ...(isInitialized(globalRoot) ? loadSearchEntries(globalRoot, query, undefined, tenantId) : []),
2078
- ];
2079
- const explainScopeDropped = explainUnscopedProbe.filter((e) => !passesExplainScope(e)).length;
2080
- if (explainScopeDropped > 0) {
2081
- console.error(`[note] ${explainScopeDropped} candidate${explainScopeDropped === 1 ? '' : 's'} hidden by recall scope policy (pass an explicit --scope to inspect).`);
2082
- }
2083
- // Bi-temporal filtering
2084
- if (explainAsOf) {
2085
- const filterAsOfExplain = (entries) => {
2086
- const asOfDate = new Date(explainAsOf);
2087
- const successorValidFrom = new Map();
2088
- for (const e of entries) {
2089
- if (e.superseded_by) {
2090
- const successor = entries.find(s => s.id === e.superseded_by);
2091
- if (successor)
2092
- successorValidFrom.set(e.id, successor.valid_from);
2093
- }
2094
- }
2095
- return entries.filter(e => {
2096
- if (new Date(e.valid_from) > asOfDate)
2097
- return false;
2098
- if (!e.superseded_by)
2099
- return true;
2100
- const succVf = successorValidFrom.get(e.id);
2101
- return succVf ? new Date(succVf) > asOfDate : true;
2102
- });
2103
- };
2104
- explainLocalEntries = filterAsOfExplain(explainLocalEntries);
2105
- explainGlobalEntries = filterAsOfExplain(explainGlobalEntries);
2106
- }
2107
- else if (!explainIncludeSuperseded) {
2108
- explainLocalEntries = explainLocalEntries.filter(e => !e.superseded_by);
2109
- explainGlobalEntries = explainGlobalEntries.filter(e => !e.superseded_by);
2110
- }
2111
- const hasGlobal = explainGlobalEntries.length > 0;
1292
+ // Explain shows what recall would see, so it applies the same scope rule.
1293
+ const explicitScope = flags['scope'] !== undefined ? String(flags['scope']).trim() : null;
1294
+ // Unlike recall, explain reads the global store whenever it exists, even when it is the local root.
1295
+ const explainGlobalOn = isInitialized(globalRoot);
1296
+ noteScopeHidden(hippoRoot, explainGlobalOn ? globalRoot : undefined, query, tenantId, explicitScope || undefined);
2112
1297
  const config = loadConfig(hippoRoot);
2113
- const usePhysics = forcePhysics
2114
- || (!forceClassic && config.physics.enabled !== false);
2115
- const noMmr = Boolean(flags['no-mmr']);
2116
- const mmrLambda = flags['mmr-lambda'] !== undefined
2117
- ? parseFloat(String(flags['mmr-lambda']))
2118
- : config.mmr.lambda;
2119
- const mmrEnabled = !noMmr && config.mmr.enabled;
2120
- const localBump = flags['equal-sources']
2121
- ? 1.0
2122
- : flags['local-bump'] !== undefined
2123
- ? parseFloat(String(flags['local-bump']))
2124
- : config.search.localBump;
2125
- // explainExplicitScope hoisted above the candidate loads (v1.25.0).
2126
- const explainActiveScope = explainExplicitScope || detectScope();
1298
+ const engine = engineFlags(flags, config);
2127
1299
  // Priced as recall prints each result, so explain returns what recall's engines would.
2128
1300
  const explainIndex = loadIndex(hippoRoot);
2129
- const explainGlobalOn = isInitialized(globalRoot);
2130
1301
  const cost = (r) => printedTokens(recallEntryText(r, query, false, explainGlobalOn && !explainIndex.entries[r.entry.id]));
2131
1302
  const entryBudget = Math.max(0, budget - printedTokens(recallHeading(budget, budget, query)));
2132
- let results;
2133
- let modeUsed;
2134
- if (usePhysics && !hasGlobal) {
2135
- results = await physicsSearch(query, explainLocalEntries, {
2136
- budget: entryBudget,
2137
- cost,
2138
- hippoRoot,
2139
- physicsConfig: config.physics,
2140
- explain: true,
2141
- scope: explainActiveScope,
2142
- });
2143
- modeUsed = 'physics';
2144
- }
2145
- else if (hasGlobal) {
2146
- results = await searchBothHybrid(query, hippoRoot, globalRoot, {
2147
- budget: entryBudget, cost, explain: true, mmr: mmrEnabled, mmrLambda, localBump, scope: explainActiveScope,
2148
- includeSuperseded: explainIncludeSuperseded, asOf: explainAsOf, tenantId,
2149
- recallScope: explainExplicitScope
2150
- ? { requested: explainExplicitScope, additive: true }
2151
- : {},
2152
- });
2153
- modeUsed = 'searchBothHybrid';
2154
- }
2155
- else {
2156
- results = await hybridSearch(query, explainLocalEntries, {
2157
- budget: entryBudget, cost, hippoRoot, explain: true, mmr: mmrEnabled, mmrLambda, scope: explainActiveScope,
2158
- includeSuperseded: explainIncludeSuperseded, asOf: explainAsOf,
2159
- });
2160
- modeUsed = 'hybrid';
2161
- }
2162
- if (limit < results.length) {
2163
- results = results.slice(0, limit);
2164
- }
2165
- results = dropHeldCopies(results, (r) => r.entry);
2166
- const candidates = explainLocalEntries.length + explainGlobalEntries.length;
1303
+ const rank = await rankRecall({ hippoRoot, globalRoot: explainGlobalOn ? globalRoot : undefined, tenantId }, {
1304
+ query, budget: entryBudget, cost, limit, includeSuperseded, asOf,
1305
+ explicitScope, activeScope: explicitScope || detectScope(),
1306
+ search: { ...engine, multihop: false, explain: true },
1307
+ });
1308
+ const hasGlobal = rank.globalEntries.length > 0;
1309
+ const modeUsed = engine.usePhysics && !hasGlobal
1310
+ ? 'physics'
1311
+ : hasGlobal ? 'searchBothHybrid' : 'hybrid';
1312
+ const results = dropHeldCopies(rank.results, (r) => r.entry);
1313
+ const candidates = rank.localEntries.length + rank.globalEntries.length;
2167
1314
  if (asJson) {
2168
1315
  const output = results.map((r, rank) => ({
2169
1316
  rank: rank + 1,
@@ -2666,164 +1813,6 @@ function cmdDedup(hippoRoot, flags) {
2666
1813
  console.log(` ... and ${result.pairs.length - 15} more (run with --dry-run to see all)`);
2667
1814
  }
2668
1815
  }
2669
- async function cmdSleep(hippoRoot, flags) {
2670
- // Tee stdout/stderr to a log file when --log-file is set. The SessionEnd
2671
- // hook uses this so the output is captured somewhere the SessionStart hook
2672
- // can re-display it next time the agent UI starts.
2673
- const logFile = typeof flags['log-file'] === 'string' ? flags['log-file'] : null;
2674
- let restoreStdout = null;
2675
- if (logFile) {
2676
- try {
2677
- fs.mkdirSync(path.dirname(logFile), { recursive: true });
2678
- fs.writeFileSync(logFile, `[hippo] ${new Date().toISOString()} consolidating memory...\n`, 'utf8');
2679
- const origStdoutWrite = process.stdout.write.bind(process.stdout);
2680
- const origStderrWrite = process.stderr.write.bind(process.stderr);
2681
- const tee = (chunk) => {
2682
- try {
2683
- const buf = typeof chunk === 'string' ? chunk : Buffer.isBuffer(chunk) ? chunk.toString('utf8') : String(chunk);
2684
- fs.appendFileSync(logFile, buf, 'utf8');
2685
- }
2686
- catch {
2687
- // log failures are non-fatal — still write to the real stream
2688
- }
2689
- };
2690
- process.stdout.write = ((chunk, enc, cb) => {
2691
- tee(chunk);
2692
- return origStdoutWrite(chunk, enc, cb);
2693
- });
2694
- process.stderr.write = ((chunk, enc, cb) => {
2695
- tee(chunk);
2696
- return origStderrWrite(chunk, enc, cb);
2697
- });
2698
- restoreStdout = () => {
2699
- process.stdout.write = origStdoutWrite;
2700
- process.stderr.write = origStderrWrite;
2701
- };
2702
- }
2703
- catch (err) {
2704
- console.error(`[hippo] warning: could not open log file ${logFile}: ${err.message}`);
2705
- }
2706
- }
2707
- try {
2708
- await cmdSleepCore(hippoRoot, flags);
2709
- if (logFile)
2710
- console.log('[hippo] sleep complete');
2711
- }
2712
- catch (err) {
2713
- if (logFile)
2714
- console.log(`[hippo] sleep failed: ${err.message}`);
2715
- throw err;
2716
- }
2717
- finally {
2718
- if (restoreStdout)
2719
- restoreStdout();
2720
- }
2721
- }
2722
- /**
2723
- * Render an api.sleep result as console output, byte-identical to the
2724
- * pre-extraction inline implementation in cmdSleepCore.
2725
- */
2726
- /** @internal — exported for snapshot tests (tests/cli-context-render-snapshot.test.ts). NOT a stable public API. */
2727
- export function renderSleepResult(result) {
2728
- console.log(`Running consolidation${result.dryRun ? ' (dry run)' : ''}...`);
2729
- console.log(`\nResults:`);
2730
- console.log(` Active memories: ${result.active}`);
2731
- console.log(` Removed (decayed): ${result.removed}`);
2732
- // Only when dormant.enabled moved something, so every other render stays
2733
- // byte-identical (tests/cli-context-render-snapshot.test.ts).
2734
- if (result.dormant !== undefined && result.dormant > 0) {
2735
- console.log(` Kept dormant: ${result.dormant} (hippo dormant to list)`);
2736
- }
2737
- if (result.dormantExpired !== undefined && result.dormantExpired > 0) {
2738
- console.log(` Expired dormant: ${result.dormantExpired} (past dormant.retentionDays)`);
2739
- }
2740
- console.log(` Merged episodic: ${result.mergedEpisodic}`);
2741
- console.log(` New semantic: ${result.newSemantic}`);
2742
- if (result.details && result.details.length > 0) {
2743
- console.log('\nDetails:');
2744
- for (const d of result.details) {
2745
- console.log(d);
2746
- }
2747
- }
2748
- if (result.dryRun)
2749
- console.log('\n(dry run - nothing written)');
2750
- if (result.deduped && result.deduped.removed > 0) {
2751
- const { removed, semDups, epiDups, crossDups } = result.deduped;
2752
- const parts = [];
2753
- if (semDups > 0)
2754
- parts.push(`${semDups} redundant semantic patterns`);
2755
- if (epiDups > 0)
2756
- parts.push(`${epiDups} duplicate episodic lessons`);
2757
- if (crossDups > 0)
2758
- parts.push(`${crossDups} cross-layer duplicates`);
2759
- console.log(`\n${result.dryRun ? 'Would dedupe' : 'Deduped'} ${removed} duplicates (${parts.join(', ')}). ${result.dryRun ? 'Would keep' : 'Kept'} stronger copies.`);
2760
- }
2761
- if (result.audit) {
2762
- if (result.audit.errorsRemoved > 0) {
2763
- console.log(`\nAudit: ${result.dryRun ? 'would remove' : 'removed'} ${result.audit.errorsRemoved} junk memories (too short/empty).`);
2764
- }
2765
- if (result.audit.warningCount > 0) {
2766
- console.log(`Audit: ${result.audit.warningCount} low-quality memories detected (run \`hippo audit\` for details).`);
2767
- }
2768
- }
2769
- if (result.shared !== undefined && result.shared > 0) {
2770
- console.log(`\nAuto-shared ${result.shared} high-value memories to global store.`);
2771
- }
2772
- if (result.secretSkipped !== undefined && result.secretSkipped > 0) {
2773
- // v1.25.0 (v39 follow-up #2): the secret veto is no longer silent.
2774
- console.log(`\nAuto-share: withheld ${result.secretSkipped} secret-flagged ${result.secretSkipped === 1 ? 'memory' : 'memories'} (secret veto).`);
2775
- }
2776
- if (result.ambient) {
2777
- console.log(`\n${renderAmbientSummary(result.ambient)}`);
2778
- }
2779
- if (result.graph && result.graph.tenants > 0) {
2780
- const { tenants, entities, relations } = result.graph;
2781
- console.log(`\nGraph: rebuilt ${tenants} tenant${tenants === 1 ? '' : 's'} (${entities} entities, ${relations} relations).`);
2782
- }
2783
- }
2784
- async function cmdSleepCore(hippoRoot, flags) {
2785
- requireInit(hippoRoot);
2786
- // Phase 1: Auto-learn from git and every coding agent's own memories (CLI-only, uses process.cwd() / os.homedir()).
2787
- // Stays in cli.ts; api.sleep covers Phase 2-6 only.
2788
- if (!flags['no-learn'] && flags['dry-run']) {
2789
- console.log("Dry run: skipped learning from git commits and coding agents' own memories (`hippo import --agents --dry-run` previews those).");
2790
- }
2791
- else if (!flags['no-learn']) {
2792
- const config = loadConfig(hippoRoot);
2793
- if (config.autoLearnOnSleep && isGitRepo(process.cwd())) {
2794
- const { added } = learnFromRepo(hippoRoot, process.cwd(), 1);
2795
- if (added > 0)
2796
- console.log(`Auto-learned ${added} lessons from today's git commits.`);
2797
- }
2798
- // FE2: opt-in code-churn staleness, off by default (config.churnStaleness.enabled).
2799
- if (config.churnStaleness.enabled && isGitRepo(process.cwd())) {
2800
- for (const { root, result } of runChurnStaleForRepo(hippoRoot, false)) {
2801
- if (result.marked > 0)
2802
- console.log(`Tagged ${result.marked} memories churn-stale in ${root}.`);
2803
- if (result.error)
2804
- console.error(`Churn-staleness check failed for ${root}: ${result.error}`);
2805
- }
2806
- }
2807
- printAgentImport(importForStore(hippoRoot, { machine: currentMachine() }), '');
2808
- }
2809
- // Finishes compactions a killed or busy post-compact hook left; never throws, and a dry run writes nothing.
2810
- if (!flags['dry-run']) {
2811
- const finished = replayCompactionsAt(hippoRoot, (message) => console.error(`compaction replay: ${message}`));
2812
- if (finished > 0)
2813
- console.log(`Finished saving ${finished} compaction${finished === 1 ? '' : 's'} left over from earlier sessions.`);
2814
- }
2815
- // Phase 2-6: Pure-storage pipeline (consolidate + dedup + audit + share + ambient).
2816
- const ctx = {
2817
- hippoRoot,
2818
- tenantId: resolveTenantId({}),
2819
- actor: api.adminActor('cli'),
2820
- };
2821
- const result = await api.sleep(ctx, {
2822
- dryRun: Boolean(flags['dry-run']),
2823
- noShare: Boolean(flags['no-share']),
2824
- });
2825
- renderSleepResult(result);
2826
- }
2827
1816
  /** Prints the SessionEnd sleep log, then clears it. Stderr, because Claude Code adds
2828
1817
  * SessionStart stdout to the model's context and this log is for the user. */
2829
1818
  function cmdLastSleep(flags) {
@@ -3024,49 +2013,6 @@ async function cmdSessionEnd(hippoRoot, flags) {
3024
2013
  return;
3025
2014
  }
3026
2015
  }
3027
- /**
3028
- * Detached worker that counts re-reads, runs sleep, then capture. Invoked via the internal
3029
- * `__session-end-worker` subcommand (not user-facing). Failures in one stage
3030
- * do not block the other.
3031
- */
3032
- // Best-effort git state; a missing git, non-repo cwd, or the timeout all
3033
- // yield null fields rather than throw (autolearn.ts execFileSync shape).
3034
- function collectHandoffEvidence(cwd, testStatus) {
3035
- let gitRef = null;
3036
- try {
3037
- gitRef = execFileSync('git', ['rev-parse', 'HEAD'], {
3038
- cwd, encoding: 'utf8', timeout: 2000, stdio: ['ignore', 'pipe', 'ignore'], windowsHide: true,
3039
- }).trim() || null;
3040
- }
3041
- catch {
3042
- gitRef = null;
3043
- }
3044
- let dirtyTree = null;
3045
- try {
3046
- const status = execFileSync('git', ['status', '--porcelain'], {
3047
- cwd, encoding: 'utf8', timeout: 2000, stdio: ['ignore', 'pipe', 'ignore'], windowsHide: true,
3048
- });
3049
- dirtyTree = status.trim().length > 0;
3050
- }
3051
- catch {
3052
- dirtyTree = null;
3053
- }
3054
- return { gitRef, dirtyTree, testStatus };
3055
- }
3056
- /** A folder without its own store never sleeps at session end, so its project's agent notes go to the global store here. */
3057
- function logSessionEndImport(logFile, transcriptPath) {
3058
- try {
3059
- const report = importAtSessionEnd(process.cwd(), transcriptPath, { machine: currentMachine() });
3060
- const line = summaryLine(report);
3061
- if (line !== null)
3062
- appendSessionEndCloseLog(logFile, line);
3063
- for (const warning of report.warnings)
3064
- appendSessionEndCloseLog(logFile, `agent memories: ${warning}`);
3065
- }
3066
- catch (err) {
3067
- appendSessionEndCloseLog(logFile, `agent memory import failed: ${err instanceof Error ? err.message : String(err)}`);
3068
- }
3069
- }
3070
2016
  async function cmdSessionEndWorker(hippoRoot, flags) {
3071
2017
  const transcriptPath = typeof flags['transcript'] === 'string' ? flags['transcript'] : undefined;
3072
2018
  const closeLogFile = typeof flags['log-file'] === 'string' ? flags['log-file'] : null;
@@ -3087,7 +2033,7 @@ async function cmdSessionEndWorker(hippoRoot, flags) {
3087
2033
  // Sleeping the global store from here would learn this folder's git commits into it; it has its own daily sleep.
3088
2034
  if (isInitialized(hippoRoot)) {
3089
2035
  try {
3090
- await cmdSleep(hippoRoot, flags);
2036
+ await (await import('./cli/sleep.js')).cmdSleep(hippoRoot, flags);
3091
2037
  }
3092
2038
  catch {
3093
2039
  // sleep errors are already tee'd to the log file via cmdSleep's
@@ -3206,28 +2152,6 @@ async function bookSessionRereads(hippoRoot, transcriptPath, sessionId) {
3206
2152
  lines.push(`re-read ${tokens} tokens over ${read.calls.length} model calls for session ${sessionId}${skipped}`);
3207
2153
  return lines;
3208
2154
  }
3209
- /**
3210
- * Best-effort log line for the DF1 T3 snapshot-close step in
3211
- * `cmdSessionEndWorker`. `cmdSleep`/`cmdCapture` each tee console output to
3212
- * `logFile` only for their own duration (the tee is restored before this
3213
- * runs), so a plain `console.log` here would be silently discarded under
3214
- * the detached worker's `stdio: 'ignore'` — write straight to the file
3215
- * instead, matching capture.ts's `appendPreCompactLog` convention.
3216
- */
3217
- function appendSessionEndCloseLog(logFile, message, opts = {}) {
3218
- if (!logFile)
3219
- return;
3220
- try {
3221
- fs.mkdirSync(path.dirname(logFile), { recursive: true });
3222
- // sanitizeLogMessage: `message` interpolates the payload-controlled
3223
- // session_id — same log-forgery guard appendPreCompactLog applies.
3224
- const write = opts.startFresh ? fs.writeFileSync : fs.appendFileSync;
3225
- write(logFile, `[hippo] ${new Date().toISOString()} ${sanitizeLogMessage(message)}\n`, 'utf8');
3226
- }
3227
- catch {
3228
- // Best-effort only — never let a log-write failure surface as an error.
3229
- }
3230
- }
3231
2155
  function loadCodexWrapperMetadata() {
3232
2156
  const { metadataPath } = resolveCodexWrapperPaths();
3233
2157
  if (!fs.existsSync(metadataPath)) {
@@ -3257,7 +2181,8 @@ function spawnRealCodex(realCodexPath, forwardArgs, cwd) {
3257
2181
  function cmdCodexRun(hippoRoot, args) {
3258
2182
  const metadata = loadCodexWrapperMetadata();
3259
2183
  const startedAtMs = Date.now();
3260
- const historyPath = metadata.historyPath;
2184
+ // Codex reads CODEX_HOME at each launch, so resolve it now, not from the install-time metadata.
2185
+ const { historyPath } = resolveCodexWrapperPaths();
3261
2186
  const startOffsetBytes = fs.existsSync(historyPath) ? fs.statSync(historyPath).size : 0;
3262
2187
  try {
3263
2188
  cmdLastSleep({ path: metadata.logFile });
@@ -3332,7 +2257,7 @@ async function cmdCodexSessionEndWorker(hippoRoot, flags) {
3332
2257
  // Sleeping the global store from here would learn this folder's git commits into it; it has its own daily sleep.
3333
2258
  if (isInitialized(hippoRoot)) {
3334
2259
  try {
3335
- await cmdSleep(hippoRoot, logFile ? { 'log-file': logFile } : {});
2260
+ await (await import('./cli/sleep.js')).cmdSleep(hippoRoot, logFile ? { 'log-file': logFile } : {});
3336
2261
  }
3337
2262
  catch {
3338
2263
  // sleep errors are already written via cmdSleep
@@ -3345,7 +2270,7 @@ async function cmdCodexSessionEndWorker(hippoRoot, flags) {
3345
2270
  try {
3346
2271
  const codexHome = typeof flags['codex-home'] === 'string'
3347
2272
  ? flags['codex-home']
3348
- : path.join(os.homedir(), '.codex');
2273
+ : resolveCodexWrapperPaths().codexHome;
3349
2274
  const historyPath = typeof flags['history-path'] === 'string'
3350
2275
  ? flags['history-path']
3351
2276
  : path.join(codexHome, 'history.jsonl');
@@ -3703,12 +2628,6 @@ function cmdInspect(hippoRoot, id) {
3703
2628
  console.log('-'.repeat(40));
3704
2629
  console.log(entry.content);
3705
2630
  }
3706
- function printActiveTaskSnapshot(snapshot) {
3707
- console.log(snapshotText(snapshot));
3708
- }
3709
- function printSessionEvents(events) {
3710
- console.log(events.length === 0 ? 'No session events found.' : sessionTrailText(events));
3711
- }
3712
2631
  function cmdConflicts(hippoRoot, flags) {
3713
2632
  requireInit(hippoRoot);
3714
2633
  const conflicts = listMemoryConflicts(hippoRoot, String(flags['status'] ?? 'open'));
@@ -4290,9 +3209,6 @@ function cmdSession(hippoRoot, args, flags) {
4290
3209
  console.error('Usage: hippo session <log|show|latest|resume|complete>');
4291
3210
  process.exit(1);
4292
3211
  }
4293
- function printHandoff(handoff) {
4294
- console.log(handoffText(handoff));
4295
- }
4296
3212
  function cmdHandoff(hippoRoot, args, flags) {
4297
3213
  requireInit(hippoRoot);
4298
3214
  const subcommand = args[0] ?? 'latest';
@@ -4458,18 +3374,6 @@ function printCard(detail) {
4458
3374
  }
4459
3375
  console.log('');
4460
3376
  }
4461
- // parseArgs turns a value-less flag into `true`; refuse rather than silently
4462
- // stringifying it (String(true) === 'true'), mirroring cmdHandoff's guard.
4463
- function cardStringFlag(flags, key) {
4464
- const v = flags[key];
4465
- if (v === undefined)
4466
- return undefined;
4467
- if (v === true || v === false || Array.isArray(v)) {
4468
- console.error(`--${key} requires a value`);
4469
- process.exit(1);
4470
- }
4471
- return v.trim();
4472
- }
4473
3377
  // A too-large --run would silently round to a different id (mirrors parsePositiveIncidentId).
4474
3378
  function cardRunFlag(flags) {
4475
3379
  const raw = cardStringFlag(flags, 'run');
@@ -6287,10 +5191,6 @@ function cmdCurrent(hippoRoot, args, flags) {
6287
5191
  console.error('Usage: hippo current <show>');
6288
5192
  process.exit(1);
6289
5193
  }
6290
- // Claude Code exports its own session var, not ours; without the fallback agent-run recalls trace with no session.
6291
- function hostSessionId() {
6292
- return process.env.HIPPO_SESSION_ID?.trim() || process.env.CLAUDE_CODE_SESSION_ID?.trim() || undefined;
6293
- }
6294
5194
  async function cmdContext(hippoRoot, args, flags, stdinText) {
6295
5195
  const rec = startDeliveryRecorder(hippoRoot, flags, stdinText);
6296
5196
  // No try/finally: a render throw keeps its own exit code and writes no event.
@@ -6594,83 +5494,6 @@ async function renderContext(hippoRoot, args, flags, stdinText, rec) {
6594
5494
  });
6595
5495
  }
6596
5496
  }
6597
- /**
6598
- * TE2: compaction drops the pinned blocks the per-prompt hook injected
6599
- * earlier, so record a `reset` for the payload's session and the next prompt
6600
- * injects again even if nothing changed. `requiredSource` limits it to hook
6601
- * payloads with that `source` (SessionStart fires for other reasons too).
6602
- * Best-effort and silent: a malformed payload records nothing.
6603
- */
6604
- function resetHookInjection(hippoRoot, stdinText, requiredSource) {
6605
- const sessionId = hookPayloadSessionId(stdinText, requiredSource);
6606
- // A sub-agent's compaction leaves its parent's context, and the blocks in it, as they were.
6607
- if (sessionId === null || isSubagentPayload(stdinText))
6608
- return;
6609
- withLedgerDb(hippoRoot, (db) => recordTokenUse(db, {
6610
- tenantId: resolveTenantId({}), sessionId, surface: 'hook', event: 'reset', items: 0, tokens: 0,
6611
- }));
6612
- }
6613
- /**
6614
- * Run `fn` with console.log captured; returns the captured lines joined by
6615
- * newlines (what the same calls would have printed, minus the final newline).
6616
- */
6617
- function captureConsole(fn) {
6618
- const lines = [];
6619
- const realLog = console.log;
6620
- console.log = (...parts) => { lines.push(parts.map(String).join(' ')); };
6621
- try {
6622
- fn();
6623
- }
6624
- finally {
6625
- console.log = realLog;
6626
- }
6627
- return lines.join('\n');
6628
- }
6629
- /**
6630
- * The store a Claude Code hook writes to: the project store when there is
6631
- * one, else an existing global store, else the project path (which the hook
6632
- * then skips, since hooks fire in every directory and must not create one).
6633
- * Pre-compact and compact-resume must agree, or a snapshot saved to one store
6634
- * is looked for in the other.
6635
- */
6636
- function hookStoreRoot(hippoRoot) {
6637
- if (isInitialized(hippoRoot))
6638
- return hippoRoot;
6639
- const globalRoot = getGlobalRoot();
6640
- return isInitialized(globalRoot) ? globalRoot : hippoRoot;
6641
- }
6642
- /**
6643
- * Run `fn` against the token ledger's store: the local store when it is
6644
- * initialized, else the global one (the per-prompt hook runs in directories
6645
- * without a local store). Best-effort: returns undefined and never throws,
6646
- * because a ledger failure must not break context or recall.
6647
- */
6648
- function withLedgerDb(hippoRoot, fn) {
6649
- let root = null;
6650
- try {
6651
- if (isInitialized(hippoRoot))
6652
- root = hippoRoot;
6653
- else if (isInitialized(getGlobalRoot()))
6654
- root = getGlobalRoot();
6655
- }
6656
- catch {
6657
- return undefined;
6658
- }
6659
- if (root === null)
6660
- return undefined;
6661
- let db;
6662
- try {
6663
- db = openHippoDb(root);
6664
- return fn(db);
6665
- }
6666
- catch {
6667
- return undefined;
6668
- }
6669
- finally {
6670
- if (db)
6671
- closeHippoDb(db);
6672
- }
6673
- }
6674
5497
  /**
6675
5498
  * v39: render cross-project inclusions under an explicit header so agents
6676
5499
  * (and humans) can tell borrowed context from project memory. Only ever
@@ -6691,39 +5514,6 @@ export function printContextMarkdown(items, totalTokens, framing = 'observe', op
6691
5514
  for (const item of items)
6692
5515
  console.log(contextLine(item, framing, showStrength, now));
6693
5516
  }
6694
- function autoDetectContext() {
6695
- // Try git diff --name-only for changed files
6696
- try {
6697
- const diff = execSync('git diff --name-only HEAD 2>&1', {
6698
- encoding: 'utf8',
6699
- timeout: 3000,
6700
- windowsHide: true,
6701
- }).trim();
6702
- if (diff) {
6703
- // Extract meaningful terms from file paths
6704
- const terms = diff
6705
- .split('\n')
6706
- .flatMap((f) => f.replace(/[\/\\\.]/g, ' ').split(/\s+/))
6707
- .filter((t) => t.length > 2 && !['src', 'dist', 'test', 'tests', 'node_modules', 'index'].includes(t))
6708
- .slice(0, 10);
6709
- if (terms.length > 0)
6710
- return terms.join(' ');
6711
- }
6712
- // Try branch name
6713
- const branch = execSync('git branch --show-current 2>&1', {
6714
- encoding: 'utf8',
6715
- timeout: 3000,
6716
- windowsHide: true,
6717
- }).trim();
6718
- if (branch && branch !== 'main' && branch !== 'master') {
6719
- return branch.replace(/[-_\/]/g, ' ');
6720
- }
6721
- }
6722
- catch {
6723
- // Not a git repo or git not available, fall through
6724
- }
6725
- return '';
6726
- }
6727
5517
  // ---------------------------------------------------------------------------
6728
5518
  // Embed command
6729
5519
  // ---------------------------------------------------------------------------
@@ -6859,115 +5649,6 @@ async function cmdWatch(command, hippoRoot) {
6859
5649
  // ---------------------------------------------------------------------------
6860
5650
  // Learn command
6861
5651
  // ---------------------------------------------------------------------------
6862
- function learnFromRepo(hippoRoot, repoPath, days, label) {
6863
- const prefix = label ? `[${label}] ` : '';
6864
- if (!isGitRepo(repoPath)) {
6865
- console.log(`${prefix}No git history found (or not a git repository).`);
6866
- return { added: 0, skipped: 0, lowInfo: 0 };
6867
- }
6868
- const gitLog = fetchGitLog(repoPath, days);
6869
- if (!gitLog.trim()) {
6870
- console.log(`${prefix}No fix/revert/bug commits found in the specified period.`);
6871
- return { added: 0, skipped: 0, lowInfo: 0 };
6872
- }
6873
- // Same patterns as MCP hippo_learn: config.gitLearnPatterns (whose default
6874
- // equals extractLessons' built-in list) so a custom list applies everywhere.
6875
- const config = loadConfig(hippoRoot);
6876
- const parsedLessons = extractLessons(gitLog, config.gitLearnPatterns);
6877
- if (parsedLessons.length === 0) {
6878
- console.log(`${prefix}No fix/revert/bug commits found in the specified period.`);
6879
- return { added: 0, skipped: 0, lowInfo: 0 };
6880
- }
6881
- // DF4: admission gate lives at the write path, not in extractLessons
6882
- // (a published API surface that only parses). Bare subjects like "fixed
6883
- // signals" are dropped here, before they ever become a memory.
6884
- // The gate filters the loop INPUT, so a dropped lesson neither stores nor
6885
- // invalidates. That is deliberate, and it was argued both ways.
6886
- //
6887
- // Round 1 of review called the lost invalidation a P1: a migration subject
6888
- // too thin to store ("replace webpack with vite") would stop weakening
6889
- // stale webpack memories. True. So the loop was widened to walk every
6890
- // parsed lesson with the gate on the write alone.
6891
- //
6892
- // Round 2 then found the cure was worse. STORAGE is what makes invalidation
6893
- // idempotent here: a stored lesson is recognised by its same-text key on
6894
- // the next scan and short-circuits before invalidating again. A lesson that
6895
- // invalidates but is never stored has no such record, so every rescan
6896
- // re-invalidates, and invalidateMatching halves half_life_days each time.
6897
- // Measured: 7 -> 3 -> 1 over two runs. That is compounding data damage.
6898
- //
6899
- // Measured frequency decided it. Across 413 real auto-learn rows in 4
6900
- // stores, 24 are gated and ZERO of those carry an invalidation target; the
6901
- // 45 lessons that do carry targets all pass the gate and are unaffected
6902
- // either way. Both failure modes are empty on real data, so the tie breaks
6903
- // on which one is benign if it ever fires: not invalidating is a missed
6904
- // improvement, re-invalidating forever is damage.
6905
- //
6906
- // Documented limitation, pinned by test: a migration subject too thin to
6907
- // store also does not invalidate. Making invalidateMatching idempotent
6908
- // would allow both, and is backlogged - it is a latent issue for the manual
6909
- // `hippo invalidate` path too, not just this one.
6910
- const { kept: lessons, dropped } = partitionLessons(parsedLessons);
6911
- const lowInfo = dropped.length;
6912
- let added = 0;
6913
- let skipped = 0;
6914
- // AT1 (plan §3 containment): per-lesson refusal must not abort the rest
6915
- // of the git-log scan. No signature change (added/skipped return shape
6916
- // used by cmdLearn + cmdSleepCore callers) — counted locally, folded into
6917
- // the existing summary line.
6918
- let rejected = 0;
6919
- const gitLearnTags = ['error', 'git-learned'];
6920
- const existingForSchema = loadAllEntries(hippoRoot, resolveTenantId({}));
6921
- const keys = storedTextKeys(existingForSchema);
6922
- for (const lesson of lessons) {
6923
- if (keys.has(duplicateKey(lesson))) {
6924
- skipped++;
6925
- continue;
6926
- }
6927
- const target = extractInvalidationTarget(lesson);
6928
- if (target) {
6929
- const invResult = invalidateMatching(hippoRoot, target, resolveTenantId({}));
6930
- if (invResult.invalidated > 0) {
6931
- console.log(`${prefix} Invalidated ${invResult.invalidated} memories referencing "${target.from}"`);
6932
- }
6933
- }
6934
- const schemaFitVal = computeSchemaFit(lesson, gitLearnTags, existingForSchema);
6935
- const entry = createMemory(lesson, {
6936
- layer: Layer.Episodic,
6937
- tags: [...gitLearnTags],
6938
- source: 'git-learn',
6939
- confidence: 'observed',
6940
- schema_fit: schemaFitVal,
6941
- tenantId: resolveTenantId({}),
6942
- baseHalfLifeDays: config.defaultHalfLifeDays,
6943
- });
6944
- // Auto-tag with path context from the repo being learned
6945
- const learnPathTags = extractPathTags(repoPath);
6946
- for (const pt of learnPathTags) {
6947
- if (!entry.tags.includes(pt))
6948
- entry.tags.push(pt);
6949
- }
6950
- try {
6951
- writeEntry(hippoRoot, entry);
6952
- }
6953
- catch (err) {
6954
- if (err instanceof RejectedValueError) {
6955
- rejected++;
6956
- continue;
6957
- }
6958
- throw err;
6959
- }
6960
- updateStats(hippoRoot, { remembered: 1 });
6961
- keys.add(duplicateKey(lesson));
6962
- void embedMemory(hippoRoot, entry);
6963
- added++;
6964
- }
6965
- console.log(`${prefix}${added} new lessons added, ${skipped} duplicates skipped` +
6966
- (rejected > 0 ? `, ${rejected} rejected value(s) skipped` : '') +
6967
- (lowInfo > 0 ? `, ${lowInfo} low-information subject(s) dropped` : '') +
6968
- '.');
6969
- return { added, skipped, lowInfo };
6970
- }
6971
5652
  function cmdLearn(hippoRoot, flags) {
6972
5653
  requireInit(hippoRoot);
6973
5654
  if (!flags['git']) {
@@ -7206,223 +5887,6 @@ function cmdSync(hippoRoot, flags = {}) {
7206
5887
  // ---------------------------------------------------------------------------
7207
5888
  // Hook install/uninstall
7208
5889
  // ---------------------------------------------------------------------------
7209
- const HOOK_MARKERS = {
7210
- start: '<!-- hippo:start -->',
7211
- end: '<!-- hippo:end -->',
7212
- };
7213
- const HOOKS = {
7214
- 'claude-code': {
7215
- file: 'CLAUDE.md',
7216
- description: 'Claude Code',
7217
- content: `
7218
- ## Project Memory (Hippo)
7219
-
7220
- Pinned rules and recent writes auto-inject at every prompt via the installed
7221
- UserPromptSubmit hook; never re-run that part manually. At the START of a
7222
- task (not per prompt), additionally load task-specific context: git-aware
7223
- recall over the full store that per-prompt injection does not cover. Also
7224
- run it if the hook is not installed:
7225
- \`\`\`bash
7226
- hippo context --auto --budget 1500
7227
- \`\`\`
7228
-
7229
- When you find out why something failed, record it right then, while you
7230
- work, never as a closing step:
7231
- \`\`\`bash
7232
- hippo remember "<what went wrong and why>" --error
7233
- \`\`\`
7234
-
7235
- The installed hooks store failed tool calls and capture the session when it
7236
- ends, so there is nothing to run before you finish.
7237
- `.trim(),
7238
- },
7239
- 'codex': {
7240
- file: 'AGENTS.md',
7241
- description: 'OpenAI Codex',
7242
- content: `
7243
- ## Project Memory (Hippo)
7244
-
7245
- At the start of every task, run:
7246
- \`\`\`bash
7247
- hippo context --auto --budget 1500
7248
- \`\`\`
7249
- Read the output before writing any code.
7250
-
7251
- On errors or unexpected behaviour, record it right then, while you work,
7252
- never as a closing step:
7253
- \`\`\`bash
7254
- hippo remember "<description of what went wrong>" --error
7255
- \`\`\`
7256
-
7257
- When you learn something that should outlive this session (a decision and
7258
- its reason, a user preference, a lesson), record it right then, while you
7259
- work, never as a closing step. Leave out secrets and personal details:
7260
- \`\`\`bash
7261
- hippo remember "<what you learned and why>"
7262
- \`\`\`
7263
-
7264
- When Hippo's Codex wrapper is installed, session-end capture runs automatically.
7265
- If the wrapper is not installed, capture a brief summary manually:
7266
- \`\`\`bash
7267
- hippo capture --stdin <<< '<decisions, errors, lessons: 2-5 bullets>'
7268
- \`\`\`
7269
- `.trim(),
7270
- },
7271
- 'cursor': {
7272
- file: 'AGENTS.md',
7273
- description: 'Cursor',
7274
- content: `
7275
- ## Project Memory (Hippo)
7276
-
7277
- At the start of every task, run:
7278
- \`\`\`bash
7279
- hippo context --auto --budget 1500
7280
- \`\`\`
7281
- Read the output before writing any code.
7282
-
7283
- On errors or unexpected behaviour, record it right then, while you work,
7284
- never as a closing step:
7285
- \`\`\`bash
7286
- hippo remember "<description of what went wrong>" --error
7287
- \`\`\`
7288
-
7289
- When you learn something that should outlive this session (a decision and
7290
- its reason, a user preference, a lesson), record it right then, while you
7291
- work, never as a closing step. Leave out secrets and personal details:
7292
- \`\`\`bash
7293
- hippo remember "<what you learned and why>"
7294
- \`\`\`
7295
-
7296
- When ending a session, capture a brief summary:
7297
- \`\`\`bash
7298
- hippo capture --stdin <<< '<decisions, errors, lessons: 2-5 bullets>'
7299
- \`\`\`
7300
- `.trim(),
7301
- },
7302
- 'openclaw': {
7303
- file: 'AGENTS.md',
7304
- description: 'OpenClaw',
7305
- content: `
7306
- ## Project Memory (Hippo)
7307
-
7308
- At the start of every session, run:
7309
- \`\`\`bash
7310
- hippo context --auto --budget 1500
7311
- \`\`\`
7312
- Read the output before writing any code.
7313
-
7314
- On errors or unexpected behaviour, record it right then, while you work,
7315
- never as a closing step:
7316
- \`\`\`bash
7317
- hippo remember "<description of what went wrong>" --error
7318
- \`\`\`
7319
-
7320
- When you learn something that should outlive this session (a decision and
7321
- its reason, a user preference, a lesson), record it right then, while you
7322
- work, never as a closing step. Leave out secrets and personal details:
7323
- \`\`\`bash
7324
- hippo remember "<what you learned and why>"
7325
- \`\`\`
7326
-
7327
- When ending a session, capture a brief summary:
7328
- \`\`\`bash
7329
- hippo capture --stdin <<< '<decisions, errors, lessons: 2-5 bullets>'
7330
- \`\`\`
7331
- `.trim(),
7332
- },
7333
- 'opencode': {
7334
- file: 'AGENTS.md',
7335
- description: 'OpenCode',
7336
- content: `
7337
- ## Project Memory (Hippo)
7338
-
7339
- At the start of every task, run:
7340
- \`\`\`bash
7341
- hippo context --auto --budget 1500
7342
- \`\`\`
7343
- Read the output before writing any code.
7344
-
7345
- On errors or unexpected behaviour, record it right then, while you work,
7346
- never as a closing step:
7347
- \`\`\`bash
7348
- hippo remember "<description of what went wrong>" --error
7349
- \`\`\`
7350
-
7351
- When you learn something that should outlive this session (a decision and
7352
- its reason, a user preference, a lesson), record it right then, while you
7353
- work, never as a closing step. Leave out secrets and personal details:
7354
- \`\`\`bash
7355
- hippo remember "<what you learned and why>"
7356
- \`\`\`
7357
-
7358
- When stuck or repeating yourself, check if this happened before:
7359
- \`\`\`bash
7360
- hippo recall "<what's going wrong>" --budget 2000
7361
- \`\`\`
7362
-
7363
- When ending a session, capture a brief summary:
7364
- \`\`\`bash
7365
- hippo capture --stdin <<< '<decisions, errors, lessons: 2-5 bullets>'
7366
- \`\`\`
7367
- `.trim(),
7368
- },
7369
- 'pi': {
7370
- file: 'AGENTS.md',
7371
- description: 'Pi',
7372
- content: `
7373
- ## Project Memory (Hippo)
7374
-
7375
- At the start of every session, run:
7376
- \`\`\`bash
7377
- hippo context --auto --budget 1500
7378
- \`\`\`
7379
- Read the output before writing any code.
7380
-
7381
- On errors or unexpected behaviour, record it right then, while you work,
7382
- never as a closing step:
7383
- \`\`\`bash
7384
- hippo remember "<description of what went wrong>" --error
7385
- \`\`\`
7386
-
7387
- When you learn something that should outlive this session (a decision and
7388
- its reason, a user preference, a lesson), record it right then, while you
7389
- work, never as a closing step. Leave out secrets and personal details:
7390
- \`\`\`bash
7391
- hippo remember "<what you learned and why>"
7392
- \`\`\`
7393
-
7394
- When ending a session, capture a brief summary:
7395
- \`\`\`bash
7396
- hippo capture --stdin <<< '<decisions, errors, lessons: 2-5 bullets>'
7397
- \`\`\`
7398
-
7399
- For full integration, copy the hippo-memory Pi extension to \`~/.pi/agent/extensions/hippo-memory/\`.
7400
- `.trim(),
7401
- },
7402
- };
7403
- // sha256 of each trimmed block an earlier hippo wrote, so init refreshes only blocks nobody edited. Add the old hash when a block changes.
7404
- const SHIPPED_HOOK_HASHES = new Map([
7405
- ['c04e48f2896a4fee9ae98f8f832e2d26a3910269df3beb5bcd6baee3cd9db68e', 'claude-code'],
7406
- ['e6b12bd8983c032e5ca8e95a97aeff4178a5a05026d10acad5b2e1b25d5656dd', 'claude-code'],
7407
- ['4c64e11d3e5be68fa547c9248d7553feb645a02f7cf13ba02f7275e1854baf44', 'claude-code'],
7408
- ['293bd319bbc86225a0ee027490a3322a0336257f5832f7fade65e4ffb2530654', 'claude-code'],
7409
- ['15abcece9712279fb4721f7a8f0ba117457400278977beb5cf5b5d7ba49f7b1a', 'codex'],
7410
- ['0c81a6b2c21473313001f624b80ea870e661aecbfda9bfe8503febc0d5f34533', 'codex'],
7411
- ['88e45358aba4f17912f113221c991dc758275991335d1daa4aa1974a69c46769', 'codex'],
7412
- ['e61632fe177450a06541c148a9a4f9182530d8df667806927a99792825903298', 'codex'],
7413
- ['a1415ecda9b2f8f317c233738e4a5ac16e6b2cc385a017c0c8ecfbfacbcab6a3', 'cursor'],
7414
- ['a38c428bbdfc14ec50f6f7b9183785170a4eae1ce9cde60257cca6efc7206b3a', 'cursor'],
7415
- ['0ec9f556abfd55e94f9e6fb47ece0fc5acb841977d144b35a2371e03645d8636', 'cursor'],
7416
- ['40524c3bd5a2eb04036567cc761451961d950995768bccd93a9900b0f75eafea', 'openclaw'],
7417
- ['7b3518e8c0feaa7b8b454cde7743f7598ad14cd9979e1680d0954484e2464aae', 'openclaw'],
7418
- ['1137dcf04568caf011e41db77bc55324faee88bc29c3a5fcc98ab687cd952a16', 'openclaw'],
7419
- ['4601c67c31f41cd5b1324cfccdb1afc66872b7fb0bc1e7c5789ecabb1f6bd942', 'opencode'],
7420
- ['90d9e21d8d1ecbe99a0fc7b7f2d9f8af7b5315a6b4b0203df4f7a9bdc0699b98', 'opencode'],
7421
- ['ca4e00284f1397ed2f2fcc53210c27f63b90edf6b37fd66dad5ee58b94ea3eee', 'opencode'],
7422
- ['8b8f5986d7f7ed15f06e68720d8913c3cab23d94366b411935ca2bbaa334553b', 'pi'],
7423
- ['37767b355e18beac726b05b9e2b898dab8c6135fd7b98f3aa52edc734d5dd283', 'pi'],
7424
- ['6e85a5cccb3cfeaa9a080713754936db730f96376f94cc9a9888a746149c7268', 'pi'],
7425
- ]);
7426
5890
  function cmdHook(args, flags) {
7427
5891
  const subcommand = args[0];
7428
5892
  const target = args[1];
@@ -8001,14 +6465,6 @@ function cmdDrillDown(hippoRoot, summaryId, flags) {
8001
6465
  // ---------------------------------------------------------------------------
8002
6466
  // Auth subcommands (A5 stub auth)
8003
6467
  // ---------------------------------------------------------------------------
8004
- function resolveAuthRoot(hippoRoot, flags) {
8005
- if (flags['global']) {
8006
- initGlobal();
8007
- return getGlobalRoot();
8008
- }
8009
- requireInit(hippoRoot);
8010
- return hippoRoot;
8011
- }
8012
6468
  function cmdAuthCreate(hippoRoot, flags) {
8013
6469
  const root = resolveAuthRoot(hippoRoot, flags);
8014
6470
  const tenantFlag = typeof flags['tenant'] === 'string' ? flags['tenant'] : undefined;
@@ -8088,53 +6544,31 @@ function cmdAuthList(hippoRoot, flags) {
8088
6544
  }
8089
6545
  function cmdAuthRevoke(hippoRoot, keyId, flags) {
8090
6546
  const root = resolveAuthRoot(hippoRoot, flags);
8091
- const asJson = Boolean(flags['json']);
6547
+ // The local CLI owns every tenant, so the revoke runs in the key's own tenant.
8092
6548
  const db = openHippoDb(root);
8093
- let exists = false;
8094
- let alreadyRevoked = false;
8095
- let revokedAt = null;
8096
- let keyTenantId = null;
6549
+ let keyTenant;
8097
6550
  try {
8098
- const row = db.prepare(`SELECT key_id, tenant_id, revoked_at FROM api_keys WHERE key_id = ?`).get(keyId);
8099
- if (!row) {
8100
- // Let the finally{} block close the db. M4: avoid manual close before
8101
- // process.exit() — the finally already handles it on every path.
8102
- console.error(`Unknown key_id: ${keyId}`);
8103
- process.exit(1);
8104
- }
8105
- exists = true;
8106
- keyTenantId = row.tenant_id;
8107
- if (row.revoked_at) {
8108
- alreadyRevoked = true;
8109
- revokedAt = row.revoked_at;
8110
- }
8111
- else {
8112
- revokeApiKey(db, keyId);
8113
- const updated = db.prepare(`SELECT revoked_at FROM api_keys WHERE key_id = ?`).get(keyId);
8114
- revokedAt = updated?.revoked_at ?? null;
8115
- }
8116
- // M1: emit auth_revoke audit event. Skip on no-op revoke (already revoked)
8117
- // so re-running the command doesn't pad the audit log with duplicates.
8118
- if (!alreadyRevoked && keyTenantId) {
8119
- try {
8120
- appendAuditEvent(db, {
8121
- tenantId: keyTenantId,
8122
- actor: 'cli',
8123
- op: 'auth_revoke',
8124
- targetId: keyId,
8125
- });
8126
- }
8127
- catch {
8128
- // Audit must not crash a successful revoke.
8129
- }
8130
- }
6551
+ // SAFETY: row's shape matches the single tenant_id column in the SELECT.
6552
+ const row = db.prepare(`SELECT tenant_id FROM api_keys WHERE key_id = ?`).get(keyId);
6553
+ keyTenant = row?.tenant_id;
8131
6554
  }
8132
6555
  finally {
8133
6556
  closeHippoDb(db);
8134
6557
  }
8135
- if (!exists)
8136
- return;
8137
- if (asJson) {
6558
+ if (keyTenant === undefined) {
6559
+ console.error(`Unknown key_id: ${keyId}`);
6560
+ process.exit(1);
6561
+ }
6562
+ const ctx = { hippoRoot: root, tenantId: keyTenant, actor: api.adminActor('cli') };
6563
+ let revokedAt;
6564
+ try {
6565
+ revokedAt = api.authRevoke(ctx, keyId).revokedAt;
6566
+ }
6567
+ catch (err) {
6568
+ console.error(`Error: ${err instanceof Error ? err.message : String(err)}`);
6569
+ process.exit(1);
6570
+ }
6571
+ if (flags['json']) {
8138
6572
  console.log(JSON.stringify({ keyId, revokedAt }));
8139
6573
  return;
8140
6574
  }
@@ -9564,7 +7998,7 @@ async function main(command, args, flags, hippoRoot) {
9564
7998
  await cmdRefine(hippoRoot, flags);
9565
7999
  break;
9566
8000
  case 'sleep':
9567
- await cmdSleep(hippoRoot, flags);
8001
+ await (await import('./cli/sleep.js')).cmdSleep(hippoRoot, flags);
9568
8002
  break;
9569
8003
  case 'last-sleep':
9570
8004
  cmdLastSleep(flags);