hippo-memory 3.0.0 → 3.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. package/README.md +1 -0
  2. package/dist/api/context/context-plan.js +2 -4
  3. package/dist/api/context/context-select.js +12 -7
  4. package/dist/api/detached.d.ts +6 -0
  5. package/dist/api/detached.js +16 -0
  6. package/dist/api/embeddings.d.ts +4 -3
  7. package/dist/api/embeddings.js +4 -4
  8. package/dist/api/memories/promote.js +2 -1
  9. package/dist/api/memories/remember-local.d.ts +5 -4
  10. package/dist/api/memories/remember-local.js +13 -10
  11. package/dist/api/recall/rank-stages.js +2 -2
  12. package/dist/api/session-end.js +1 -0
  13. package/dist/api/sharing.js +9 -7
  14. package/dist/api/sleep/auto-sleep.d.ts +1 -1
  15. package/dist/api/sleep/auto-sleep.js +5 -3
  16. package/dist/api/sleep/learn.js +5 -2
  17. package/dist/api/sleep/sleep-run.js +4 -2
  18. package/dist/api/types.d.ts +4 -0
  19. package/dist/base/memory/entry.js +2 -2
  20. package/dist/base/memory/markdown-delimiters.d.ts +15 -0
  21. package/dist/base/memory/markdown-delimiters.js +94 -0
  22. package/dist/base/memory/memory-audit.js +4 -4
  23. package/dist/base/memory/memory-quality.d.ts +12 -0
  24. package/dist/base/memory/memory-quality.js +92 -3
  25. package/dist/base/ports/store/types.d.ts +2 -1
  26. package/dist/base/rank/prompt-recall.d.ts +16 -3
  27. package/dist/base/rank/prompt-recall.js +47 -7
  28. package/dist/base/util/detached-set.d.ts +8 -0
  29. package/dist/base/util/detached-set.js +35 -0
  30. package/dist/base/util/tokenize.d.ts +29 -0
  31. package/dist/base/util/tokenize.js +68 -5
  32. package/dist/base/util/version.d.ts +1 -1
  33. package/dist/base/util/version.js +1 -1
  34. package/dist/db/meta.d.ts +1 -0
  35. package/dist/db/meta.js +1 -0
  36. package/dist/db/store-runtime.d.ts +10 -3
  37. package/dist/db/store-runtime.js +19 -3
  38. package/dist/domain/consolidate/conflicts.js +2 -2
  39. package/dist/domain/learn/forward-claim-detector.js +4 -2
  40. package/dist/domain/learn/invalidation.js +5 -2
  41. package/dist/domain/search/bm25.js +3 -3
  42. package/dist/domain/search/detached-work.d.ts +6 -0
  43. package/dist/domain/search/detached-work.js +2 -0
  44. package/dist/domain/search/embed-memories.d.ts +4 -4
  45. package/dist/domain/search/embed-memories.js +4 -5
  46. package/dist/domain/sharing/global-sync.d.ts +8 -2
  47. package/dist/domain/sharing/global-sync.js +5 -5
  48. package/dist/domain/sharing/share.d.ts +4 -0
  49. package/dist/domain/sharing/share.js +2 -2
  50. package/dist/entry/eval-scripts.d.ts +1 -0
  51. package/dist/entry/sleep.d.ts +1 -0
  52. package/dist/index.d.ts +1 -1
  53. package/dist/server.d.ts +1 -0
  54. package/dist/store/rules/windows.d.ts +3 -0
  55. package/dist/store/rules/windows.js +3 -0
  56. package/dist/store/sqlite/audit/audit-maintenance.js +16 -13
  57. package/dist/store/sqlite/recall/candidates.js +1 -0
  58. package/dist/store/sqlite/recall/search-rows.d.ts +2 -2
  59. package/dist/store/sqlite/recall/search-rows.js +62 -26
  60. package/dist/store/sqlite/runtime/server-runtime.d.ts +6 -1
  61. package/dist/store/sqlite/runtime/server-runtime.js +15 -3
  62. package/dist/store/sqlite/runtime/worker-jobs.js +2 -2
  63. package/dist/store/sqlite/sharing/copy-rows.d.ts +1 -0
  64. package/dist/store/sqlite/sharing/copy-rows.js +2 -1
  65. package/dist/surface/capture/command.d.ts +8 -1
  66. package/dist/surface/capture/command.js +38 -8
  67. package/dist/surface/capture/compact.d.ts +3 -3
  68. package/dist/surface/capture/compact.js +4 -4
  69. package/dist/surface/capture/extract.d.ts +10 -0
  70. package/dist/surface/capture/extract.js +199 -26
  71. package/dist/surface/capture/session-texts.js +11 -11
  72. package/dist/surface/cli/api-context.d.ts +5 -0
  73. package/dist/surface/cli/api-context.js +12 -0
  74. package/dist/surface/cli/goals.js +6 -8
  75. package/dist/surface/cli/hook-runtime.js +2 -2
  76. package/dist/surface/cli/memory-import.js +20 -15
  77. package/dist/surface/mcp/protocol.d.ts +6 -0
  78. package/dist/surface/mcp/protocol.js +1 -0
  79. package/dist/surface/mcp/stdio.js +11 -5
  80. package/dist/surface/open-store.d.ts +2 -2
  81. package/dist/surface/open-store.js +11 -7
  82. package/dist/surface/server/auth.d.ts +1 -1
  83. package/dist/surface/server/auth.js +13 -2
  84. package/dist/surface/server/boot.js +3 -2
  85. package/dist/surface/server/dashboard-listener.js +2 -2
  86. package/dist/surface/server/lifecycle.d.ts +2 -0
  87. package/dist/surface/server/lifecycle.js +4 -2
  88. package/dist/surface/server/mcp-http.js +1 -0
  89. package/dist/surface/server/types.d.ts +3 -0
  90. package/extensions/openclaw-plugin/openclaw.plugin.json +1 -1
  91. package/extensions/openclaw-plugin/package.json +1 -1
  92. package/openclaw.plugin.json +1 -1
  93. package/package.json +1 -1
package/README.md CHANGED
@@ -56,6 +56,7 @@ Every measurement we have ever published is indexed in [`docs/evals/`](docs/eval
56
56
  pre-registrations kept next to their results, including the runs that failed and the one
57
57
  claim we retracted.
58
58
 
59
+ - **Z0 round 2, 16 repositories, Claude Code with hippo against its built-in memory: no extra cost (ratio 0.999 [0.962, 1.037]) and about 12% less spent on teach and correction turns. A drop in repeat mistakes is not shown (-8.9 points [-18.5, +0.7], under the pre-registered 15-point minimum).** ([result](docs/evals/2026-11-01-z0-round2-result.md))
59
60
  - **Sequential Learning Benchmark.** [benchmarks/sequential-learning/](benchmarks/sequential-learning/). 50 tasks, 10 buried traps. Measures whether agents learn from past mistakes, not just retrieve text. v0.11.0 informal magnitude RETRACTED v1.7.9; mechanism remains shipped. See [CHANGELOG.md](./CHANGELOG.md) v1.7.9 entry.
60
61
  - **LongMemEval oracle split, all 500 questions in one pooled store, BM25 only, no embeddings, v0.11: R@5 = 74.0%** ([benchmarks/README.md](benchmarks/README.md), scripts in [benchmarks/longmemeval/](benchmarks/longmemeval/)). A different setup from the per-haystack results under [Benchmarks](#benchmarks), so the two are not a before and after.
61
62
  - **On a private 300-query developer store, R@1 0.41 to 0.62 with `hippo recall "<query>" --reranker jev`**, the free local cross-encoder against Jev ([full eval](docs/evals/2026-09-19-jev-reranker.md)). The opt-in [TypeSafe Jev](https://typesafe.ai) reranker, off by default, about 0.0004 USD a recall. 2000-draw paired bootstrap; the margin held in 20 of 20 seeds and a permutation null reached it in 0 of 200 runs. Ranking only: three graded tests on one 150-question LongMemEval set did **not** show a better answer rate than the free local cross-encoder, and that negative result is in the same doc. What it buys today is a shorter context: on that set, 2 memories ranked by Jev answered as well as 5 ranked by the cross-encoder.
@@ -3,7 +3,7 @@ import { BadRequestError } from '../../base/errors/api-errors.js';
3
3
  import { log } from '../../base/util/log.js';
4
4
  import { classifyOriginProject, projectId, projectNames, } from '../../base/scope/project-identity.js';
5
5
  import { DEFAULT_CONFIG } from '../../base/config/config.js';
6
- import { promptTokens } from '../../base/rank/prompt-recall.js';
6
+ import { promptSearchTerms } from '../../base/rank/prompt-recall.js';
7
7
  import { personalScopeOf } from '../../base/scope/recall-scope.js';
8
8
  import { finiteOr } from '../../domain/context/select-policy.js';
9
9
  import { strengthOf } from '../types.js';
@@ -125,9 +125,7 @@ export function planContext(ctx, opts, local) {
125
125
  /** The prompt-recall candidate request the ambient loads carry, when the prompt has any terms. */
126
126
  export function promptRecallRequest(opts, plan) {
127
127
  const { pinnedInject } = plan.config;
128
- const promptRecallTerms = plan.promptRecallPending && pinnedInject.enabled
129
- ? Array.from(promptTokens(opts.prompt ?? ''))
130
- : [];
128
+ const promptRecallTerms = plan.promptRecallPending && pinnedInject.enabled ? promptSearchTerms(opts.prompt ?? '') : [];
131
129
  return promptRecallTerms.length > 0
132
130
  ? {
133
131
  terms: promptRecallTerms,
@@ -5,7 +5,7 @@ import { DEFAULT_LOCAL_BUMP } from '../../base/rank/search-types.js';
5
5
  import { compareScoredResults } from '../../base/rank/compare.js';
6
6
  import { scopeMatch } from '../../base/rank/active-scope.js';
7
7
  import { scopeBoostFor } from '../../domain/search/boosts.js';
8
- import { promptTokens, contentTokens, gatePromptRecall } from '../../base/rank/prompt-recall.js';
8
+ import { promptTokens, memoryTokens, gatePromptRecall } from '../../base/rank/prompt-recall.js';
9
9
  import { admitWithinBudget, newestWorthSurfacing, promptRecallGateOf, reservePinned, strongestWithin, } from '../../domain/context/select-policy.js';
10
10
  const GLOBAL_DISCOUNT = 1 / DEFAULT_LOCAL_BUMP;
11
11
  // Share and promote copy a memory to the global store under a new id, so equal content is the only
@@ -53,7 +53,7 @@ export function selectPinned({ opts, plan, left, pools, admission, }) {
53
53
  const recentBudget = Math.max(0, effBudget - reservePinned(rankedPinned, effBudget));
54
54
  // Prompt recall gates the backfill on the prompt instead of recency.
55
55
  if (plan.promptRecallPending) {
56
- const candidates = () => promptRecallCandidates({ plan, pools, admit: admission.admit, rankedPinned, nowP });
56
+ const candidates = (prompt) => promptRecallCandidates({ plan, pools, admit: admission.admit, rankedPinned, nowP, prompt });
57
57
  backfillFromPrompt(opts, plan, { pinnedCfg, candidates, picked, recentBudget });
58
58
  }
59
59
  else if (plan.includeRecent > 0) {
@@ -100,7 +100,7 @@ function backfillFromPrompt(opts, plan, options) {
100
100
  const p = promptTokens(opts.prompt ?? '');
101
101
  if (p.size === 0)
102
102
  return;
103
- const candidateItems = candidates();
103
+ const candidateItems = candidates(p);
104
104
  const gated = gatePromptRecall(p, candidateItems, gate);
105
105
  plan.obs?.gated(p, candidateItems, gate, gated);
106
106
  const rows = gated.map((g) => ({
@@ -113,7 +113,7 @@ function backfillFromPrompt(opts, plan, options) {
113
113
  admitWithinBudget(rows, picked, recentBudget, rejectOverBudget(plan.obs));
114
114
  }
115
115
  // Candidates came off the ambient load's own connection (the recall request), not a fresh open.
116
- function promptRecallCandidates({ plan, pools, admit, rankedPinned, nowP, }) {
116
+ function promptRecallCandidates({ plan, pools, admit, rankedPinned, nowP, prompt, }) {
117
117
  const { obs, primaryIsGlobal } = plan;
118
118
  // A candidate carrying a pin's text would inject that memory a second time.
119
119
  const pinnedText = new Set(rankedPinned.map((r) => r.entry.content));
@@ -139,9 +139,9 @@ function promptRecallCandidates({ plan, pools, admit, rankedPinned, nowP, }) {
139
139
  const { strength } = plan;
140
140
  const [localCandidates, globalCandidates] = oneCopyPerMemory(localEligible, globalEligible, nowP, strength);
141
141
  obs?.dropMissing([...localEligible, ...globalEligible], [...localCandidates, ...globalCandidates], 'eligible', 'duplicate');
142
- return promptCandidateItems(localCandidates, globalCandidates, primaryIsGlobal);
142
+ return promptCandidateItems(localCandidates, globalCandidates, primaryIsGlobal, prompt);
143
143
  }
144
- function promptCandidateItems(localCandidates, globalCandidates, primaryIsGlobal) {
144
+ function promptCandidateItems(localCandidates, globalCandidates, primaryIsGlobal, prompt) {
145
145
  const seenCandidateIds = new Set();
146
146
  const candidateItems = [];
147
147
  // Local wins the id collision (a global row synced into the local store).
@@ -153,7 +153,12 @@ function promptCandidateItems(localCandidates, globalCandidates, primaryIsGlobal
153
153
  if (seenCandidateIds.has(e.id))
154
154
  continue;
155
155
  seenCandidateIds.add(e.id);
156
- candidateItems.push({ id: e.id, tokens: contentTokens(e.content), entry: e, isGlobal });
156
+ candidateItems.push({
157
+ id: e.id,
158
+ tokens: memoryTokens(e.content, prompt),
159
+ entry: e,
160
+ isGlobal,
161
+ });
157
162
  }
158
163
  }
159
164
  return candidateItems;
@@ -0,0 +1,6 @@
1
+ import type { DetachedWork } from '../domain/search/detached-work.js';
2
+ import type { Context } from './types.js';
3
+ /** Runs `start` with the root's keeper of detached work; a context with none gets one of the
4
+ * call's own, which settles before the call returns, so no work outlives the call unowned. */
5
+ export declare function withDetached<T>(ctx: Pick<Context, 'detached'>, start: (detached: DetachedWork) => T | Promise<T>): Promise<T>;
6
+ //# sourceMappingURL=detached.d.ts.map
@@ -0,0 +1,16 @@
1
+ // Work an api call starts and does not wait on goes to the keeper its root set on the Context.
2
+ import { DetachedSet } from '../base/util/detached-set.js';
3
+ /** Runs `start` with the root's keeper of detached work; a context with none gets one of the
4
+ * call's own, which settles before the call returns, so no work outlives the call unowned. */
5
+ export async function withDetached(ctx, start) {
6
+ if (ctx.detached !== undefined)
7
+ return start(ctx.detached);
8
+ const own = new DetachedSet();
9
+ try {
10
+ return await start(own);
11
+ }
12
+ finally {
13
+ await own.settle();
14
+ }
15
+ }
16
+ //# sourceMappingURL=detached.js.map
@@ -2,12 +2,13 @@ import type { ConfiguredFolder } from '../base/config/configured-folder.js';
2
2
  import type { MemoryEntry } from '../base/memory/entry.js';
3
3
  import type { EmbeddingProvider } from '../base/ports/embedder.js';
4
4
  import { type EmbedRun } from '../domain/search/embed-memories.js';
5
+ import type { DetachedWork } from '../domain/search/detached-work.js';
5
6
  import type { RequiredStore, VectorCoverage } from '../base/ports/store.js';
6
7
  import { type Context, type ContextWith } from './types.js';
7
8
  /** Embeds each row a capture writes into `folder`'s index without holding the capture up; a
8
- * failure is a warning. A capture runs outside any Context, so its root hands the eval switches
9
- * and clock as `run`. */
10
- export declare function detachedEmbedder(folder: ConfiguredFolder<RequiredStore>, run: EmbedRun): (entry: MemoryEntry) => void;
9
+ * failure is a warning. A capture runs outside any Context, so its root hands the eval switches,
10
+ * the clock and the embeds' keeper. */
11
+ export declare function detachedEmbedder(folder: ConfiguredFolder<RequiredStore>, run: EmbedRun, detached: DetachedWork): (entry: MemoryEntry) => void;
11
12
  /** Embed every memory that has no vector yet; resolves to how many it embedded. */
12
13
  export declare function memoryEmbedMissing(ctx: Context, provider?: EmbeddingProvider): Promise<number>;
13
14
  /** How many vectors the store holds, live memory or not. */
@@ -5,11 +5,11 @@ import { embedAll, embedMemoryDetached, folderEmbedTarget, } from '../domain/sea
5
5
  import { indexStep } from '../domain/search/embed-index-target.js';
6
6
  import { requireHostAdmin } from './types.js';
7
7
  /** Embeds each row a capture writes into `folder`'s index without holding the capture up; a
8
- * failure is a warning. A capture runs outside any Context, so its root hands the eval switches
9
- * and clock as `run`. */
10
- export function detachedEmbedder(folder, run) {
8
+ * failure is a warning. A capture runs outside any Context, so its root hands the eval switches,
9
+ * the clock and the embeds' keeper. */
10
+ export function detachedEmbedder(folder, run, detached) {
11
11
  const target = folderEmbedTarget(folder, run);
12
- return (entry) => embedMemoryDetached(target, entry);
12
+ return (entry) => embedMemoryDetached(target, entry, detached);
13
13
  }
14
14
  /** Embed every memory that has no vector yet; resolves to how many it embedded. */
15
15
  export async function memoryEmbedMissing(ctx, provider) {
@@ -4,6 +4,7 @@ import { createSuccessor, generateId, } from '../../base/memory/entry.js';
4
4
  import { detectSecret } from '../../base/util/secret-detect.js';
5
5
  import { embedMemoryDetached, folderEmbedTarget } from '../../domain/search/embed-memories.js';
6
6
  import { globalTargetOf } from '../infra.js';
7
+ import { withDetached } from '../detached.js';
7
8
  import { sharedStoreOf } from '../types.js';
8
9
  import { assertWriteRight, canReadScope, isPersonalScope, isQuarantineScope, scopeReaderOf, } from '../../base/scope/recall-scope.js';
9
10
  export async function promote(ctx, id) {
@@ -29,7 +30,7 @@ export async function promote(ctx, id) {
29
30
  auditAs: { tenantId: ctx.tenantId, actor: ctx.actor.subject },
30
31
  });
31
32
  // A promote may create the global store, which only the write target names before it exists.
32
- embedMemoryDetached(folderEmbedTarget(target, ctx.infra), globalEntry);
33
+ await withDetached(ctx, (detached) => embedMemoryDetached(folderEmbedTarget(target, ctx.infra), globalEntry, detached));
33
34
  return { ok: true, sourceId: id, globalId: globalEntry.id };
34
35
  }
35
36
  /** The copy of a local memory, with a new `g_` id so it cannot collide. A project store's NULL
@@ -29,8 +29,9 @@ export interface StartedWeak {
29
29
  readonly strength: number;
30
30
  }
31
31
  /** `stored` carries the row as the store holds it after the write, and `embedding`, which
32
- * settles with whether the row reached the vector index (a caller may ignore it; a failure is
33
- * also logged). `skipped` is the gate dropping the write, with nothing stored or counted. */
32
+ * settles with whether the row reached the vector index (the Context's keeper also holds it, so a
33
+ * caller may drop it; a failure is logged). `skipped` is the gate dropping the write, with nothing
34
+ * stored or counted. */
34
35
  export type LocalRememberOutcome = {
35
36
  readonly status: 'stored';
36
37
  readonly entry: MemoryEntry;
@@ -43,8 +44,8 @@ export type LocalRememberOutcome = {
43
44
  readonly reason: string;
44
45
  readonly score: number;
45
46
  };
46
- /** One local write on hippo.db: gate, write, read-back, counter, then the embedding, started and
47
- * never awaited. A refused value throws as `remember` does. */
47
+ /** One local write on hippo.db: gate, write, read-back, counter, then the embedding, which the
48
+ * Context's keeper owns; with none, the call waits for it. A refused value throws as `remember` does. */
48
49
  export declare function rememberLocally(ctx: Context, input: LocalRememberInput): Promise<LocalRememberOutcome>;
49
50
  /** What a caller asks of fact extraction; the model is the one its Context carries. */
50
51
  export interface FactExtractionRequest {
@@ -1,11 +1,12 @@
1
1
  // The local remember pipeline for a caller in this process (`hippo remember`, `hippo watch`):
2
2
  // schema fit, the salience gate, the write, the read-back, the `remembered` counter and the
3
- // embedding kick-off, in that order. Fact extraction is a second step, which the caller runs after
4
- // it has reported the write. Nothing here prints; the caller reports from what comes back.
3
+ // embedding, which the Context's keeper owns, in that order. Fact extraction is a second step,
4
+ // which the caller runs after it has reported the write. Nothing here prints.
5
5
  import { embedMemoryInto, folderEmbedTarget, } from '../../domain/search/embed-memories.js';
6
6
  import { captureError } from '../../domain/learn/autolearn.js';
7
7
  import { extractFacts, storeExtractedFacts } from '../../domain/learn/extract.js';
8
8
  import { errorFields, errorMessage, log } from '../../base/util/log.js';
9
+ import { withDetached } from '../detached.js';
9
10
  import { memoryGet } from './memories.js';
10
11
  import { remember } from './remember.js';
11
12
  // The tags a caller in this process adds from the folder it runs in.
@@ -20,8 +21,7 @@ async function storedEntry(ctx, id) {
20
21
  throw new Error(`memory ${id} was written but cannot be read back`);
21
22
  return entry;
22
23
  }
23
- /** Best-effort and slow (a model load or a network call), so the caller is not held for it; a
24
- * row left out of the index is logged. */
24
+ /** Best-effort and slow (a model load or a network call); a row left out of the index is logged. */
25
25
  function indexInBackground(ctx, entry) {
26
26
  const folder = { store: ctx.store, config: ctx.config, embedderFor: ctx.infra.embedderFor };
27
27
  const target = folderEmbedTarget(folder, ctx.infra);
@@ -32,8 +32,8 @@ function indexInBackground(ctx, entry) {
32
32
  return outcome;
33
33
  });
34
34
  }
35
- /** One local write on hippo.db: gate, write, read-back, counter, then the embedding, started and
36
- * never awaited. A refused value throws as `remember` does. */
35
+ /** One local write on hippo.db: gate, write, read-back, counter, then the embedding, which the
36
+ * Context's keeper owns; with none, the call waits for it. A refused value throws as `remember` does. */
37
37
  export async function rememberLocally(ctx, input) {
38
38
  const schemaFit = await ctx.store.contextReads.schemaFit(ctx.tenantId, input.content, input.fitTags ?? input.tags);
39
39
  const written = await remember(ctx, {
@@ -57,10 +57,13 @@ export async function rememberLocally(ctx, input) {
57
57
  const { id, warnings = [], startedWeak } = written;
58
58
  const entry = await storedEntry(ctx, id);
59
59
  await ctx.store.bumpRemembered(1);
60
- const embedding = indexInBackground(ctx, entry);
61
- return startedWeak
62
- ? { status: 'stored', entry, embedding, warnings, startedWeak }
63
- : { status: 'stored', entry, embedding, warnings };
60
+ return withDetached(ctx, (detached) => {
61
+ const embedding = indexInBackground(ctx, entry);
62
+ detached.detach(embedding);
63
+ return startedWeak
64
+ ? { status: 'stored', entry, embedding, warnings, startedWeak }
65
+ : { status: 'stored', entry, embedding, warnings };
66
+ });
64
67
  }
65
68
  /** Extracts facts from a row just stored and stores them beside it.
66
69
  * Best-effort: a failure is named in the result, never thrown. */
@@ -7,7 +7,7 @@ import { outcomeNudge, STRENGTH_RANK_FLOOR, STRENGTH_RANK_SPAN, } from '../../do
7
7
  import { applyReranker } from '../../domain/search/rerank.js';
8
8
  import { CONFLICT_PEER_MULTIPLIER, SALIENCE_MIN_MULTIPLIER, UTILITY_LENGTH_PENALTY_CAP, UTILITY_LENGTH_TOKENS, VALUE_MULT_MAX, VALUE_MULT_MIN, VALUE_MULT_SLOPE, } from '../../base/rank/search-types.js';
9
9
  import { isJsonNumber } from '../../base/util/json.js';
10
- import { textOverlap, tokenize as tokenizeQuery } from '../../base/util/tokenize.js';
10
+ import { indexTokenize, textOverlap, tokenize as tokenizeQuery } from '../../base/util/tokenize.js';
11
11
  import { poolScope } from './rank-search.js';
12
12
  /** The stages after the search, in order: graph expansion, re-rank, goal boost, salience, then
13
13
  * the outcome and layer filters. */
@@ -156,7 +156,7 @@ function newestOnTopicFirst(query, results) {
156
156
  for (const r of pool) {
157
157
  let hits = 0;
158
158
  if (queryTokens.size > 0) {
159
- const candTokens = new Set(tokenizeQuery(r.entry.content));
159
+ const candTokens = new Set(indexTokenize(r.entry.content));
160
160
  for (const t of queryTokens)
161
161
  if (candTokens.has(t))
162
162
  hits++;
@@ -160,6 +160,7 @@ function endStore(ctx) {
160
160
  store: global.store,
161
161
  config: global.config,
162
162
  infra: ctx.infra,
163
+ detached: ctx.detached,
163
164
  };
164
165
  }
165
166
  /** Sleeps the folder's own store: a reply's close only at the auto-sleep threshold, a session's
@@ -5,6 +5,7 @@ import { ALL_TENANTS } from '../base/util/tenant.js';
5
5
  import { syncGlobalToLocal } from '../domain/sharing/global-sync.js';
6
6
  import { autoShare, listPeers, shareMemory } from '../domain/sharing/share.js';
7
7
  import { globalTargetOf } from './infra.js';
8
+ import { withDetached } from './detached.js';
8
9
  import { requireHostAdmin } from './types.js';
9
10
  export { AUTO_SHARE_MIN_SCORE, transferScore } from '../domain/sharing/share.js';
10
11
  // A root's reach sets it for a store kept in a folder, so only a context built without a reach
@@ -19,17 +20,16 @@ function requireGlobalTarget(ctx, action) {
19
20
  /** Copies one of the caller's memories to the global store; null when its transfer score is too
20
21
  * low and `force` is off. */
21
22
  export async function shareToGlobal(ctx, id, opts) {
23
+ const target = requireGlobalTarget(ctx, 'share');
22
24
  const options = { force: opts.force, tenantId: ctx.tenantId, embed: ctx.infra };
23
- return shareMemory(ctx, requireGlobalTarget(ctx, 'share'), id, options);
25
+ return withDetached(ctx, (detached) => shareMemory(ctx, target, id, { ...options, detached }));
24
26
  }
25
27
  /** The caller's memories whose transfer score reaches `minScore`, copied to the global store
26
28
  * unless `dryRun`. */
27
29
  export async function autoShareToGlobal(ctx, opts) {
28
- return autoShare(ctx, requireGlobalTarget(ctx, 'auto-share'), {
29
- ...opts,
30
- tenantId: ctx.tenantId,
31
- embed: ctx.infra,
32
- });
30
+ const target = requireGlobalTarget(ctx, 'auto-share');
31
+ const options = { ...opts, tenantId: ctx.tenantId, embed: ctx.infra };
32
+ return withDetached(ctx, (detached) => autoShare(ctx, target, { ...options, detached }));
33
33
  }
34
34
  /** The projects that shared into the global store: the caller's tenant, or every tenant for the
35
35
  * host's operator. */
@@ -42,6 +42,8 @@ export async function sharePeers(ctx, opts = {}) {
42
42
  * `includeCrossProject`. */
43
43
  export async function syncFromGlobal(ctx, opts) {
44
44
  const local = { store: ctx.store, config: ctx.config, embedderFor: ctx.infra.embedderFor };
45
- return syncGlobalToLocal(local, requireGlobalTarget(ctx, 'sync'), { ...opts, embed: ctx.infra });
45
+ const target = requireGlobalTarget(ctx, 'sync');
46
+ const options = { ...opts, embed: ctx.infra };
47
+ return withDetached(ctx, (detached) => syncGlobalToLocal(local, target, { ...options, detached }));
46
48
  }
47
49
  //# sourceMappingURL=sharing.js.map
@@ -20,7 +20,7 @@ export declare function autoSleepDue(newSince: (tenantId: string, now: Date) =>
20
20
  * only the host tenant's writes start one, one per store at a time. Never rejects: the write it
21
21
  * follows has landed.
22
22
  */
23
- export declare function autoSleepAfterWrite(ctx: Pick<Context, 'tenantId' | 'config' | 'callers' | 'infra'>): Promise<void>;
23
+ export declare function autoSleepAfterWrite(ctx: Pick<Context, 'tenantId' | 'config' | 'callers' | 'infra' | 'detached'>): Promise<void>;
24
24
  /** What one auto-sleep runs: the consolidation alone, its skipped passes logged by the process
25
25
  * that ran it. */
26
26
  export declare function autoSleepConsolidate(ctx: Pick<ContextWith<'consolidation' | 'learning' | 'lessonWrites' | 'auditMaintenance' | 'vectors'>, 'store' | 'config' | 'tenantId' | 'infra'>): Promise<ConsolidationResult>;
@@ -4,6 +4,7 @@ import { errorFields, errorMessage, log } from '../../base/util/log.js';
4
4
  import { logPhaseFailures } from '../../domain/consolidate/run.js';
5
5
  import { runConsolidation } from '../../domain/consolidate/sleep.js';
6
6
  import { strengthOf } from '../types.js';
7
+ import { withDetached } from '../detached.js';
7
8
  /** Counts only when enabled: the count reads the store. */
8
9
  export async function autoSleepDue(newSince, tenantId, config, now) {
9
10
  const { enabled, threshold } = config;
@@ -37,9 +38,9 @@ export async function autoSleepAfterWrite(ctx) {
37
38
  }
38
39
  if (!due)
39
40
  return;
40
- // Fire-and-forget (never block the response); an unhandled rejection would kill the server, so
41
- // log it.
42
- host
41
+ // The root's keeper holds the sleep so its stores outlast it; a context with no keeper waits for
42
+ // it. A failure is logged here once, with the tenant.
43
+ const sleep = host
43
44
  .run()
44
45
  .catch((err) => {
45
46
  log.error(`auto-sleep consolidate failed (tenant ${tenantId}): ${errorMessage(err)}`, errorFields(err));
@@ -47,6 +48,7 @@ export async function autoSleepAfterWrite(ctx) {
47
48
  .finally(() => {
48
49
  host.latch.held = false;
49
50
  });
51
+ await withDetached(ctx, (detached) => detached.detach(sleep));
50
52
  }
51
53
  /** What one auto-sleep runs: the consolidation alone, its skipped passes logged by the process
52
54
  * that ran it. */
@@ -4,6 +4,7 @@ import { ForbiddenError } from '../../base/errors/api-errors.js';
4
4
  import { fetchGitLog, extractLessons, partitionLessons } from '../../domain/learn/autolearn.js';
5
5
  import { isGitRepo } from '../../base/ports/git.js';
6
6
  import { embedMemoryDetached } from '../../domain/search/embed-memories.js';
7
+ import { withDetached } from '../detached.js';
7
8
  import { extractInvalidationTarget, invalidateMatching, invalidationMatcher, invalidateMatchingAmong, } from '../../domain/learn/invalidation.js';
8
9
  import { createMemory, ERROR_TAG, Layer, schemaFitFrom, } from '../../base/memory/entry.js';
9
10
  import { extractPathTags } from '../../domain/search/path-context.js';
@@ -85,8 +86,10 @@ async function landLessons(batch, queued) {
85
86
  strength: strengthOf(ctx),
86
87
  clock,
87
88
  };
88
- for (const entry of landed)
89
- embedMemoryDetached(target, entry);
89
+ await withDetached(ctx, (detached) => {
90
+ for (const entry of landed)
91
+ embedMemoryDetached(target, entry, detached);
92
+ });
90
93
  }
91
94
  /** The keys of the rows that may hold a lesson, the only rows that can be its copy. */
92
95
  async function knownKeys(ctx, writes, lessons) {
@@ -10,6 +10,7 @@ import { extractGraphChunked } from '../../domain/graph/extract.js';
10
10
  import { strengthOf } from '../types.js';
11
11
  import { auditRemoveErrors } from '../auth/audit.js';
12
12
  import { globalTargetOf } from '../infra.js';
13
+ import { withDetached } from '../detached.js';
13
14
  import { errorFields, errorMessage, log } from '../../base/util/log.js';
14
15
  import { ALL_TENANTS } from '../../base/util/tenant.js';
15
16
  import { failedUnitOf } from '../../base/errors/failed-unit.js';
@@ -186,7 +187,7 @@ async function shareOnSleep(ctx, phases, result) {
186
187
  // Both skip counters are surfaced so the secret veto and the rejection tombstone are observable,
187
188
  // not silent.
188
189
  const autoShareStats = { secretSkipped: 0, rejectedSkipped: 0 };
189
- const shared = await phases.autoShare({
190
+ const shared = await withDetached(ctx, (detached) => phases.autoShare({
190
191
  store: {
191
192
  sharing,
192
193
  folderFiles,
@@ -197,7 +198,8 @@ async function shareOnSleep(ctx, phases, result) {
197
198
  minScore: AUTO_SHARE_MIN_SCORE,
198
199
  stats: autoShareStats,
199
200
  embed: ctx.infra,
200
- });
201
+ detached,
202
+ }));
201
203
  if (shared.length > 0) {
202
204
  result.shared = shared.length;
203
205
  }
@@ -8,6 +8,7 @@ import type { InfraPorts } from '../base/ports/infra.js';
8
8
  import type { FailureNotices } from '../base/util/failure-notices.js';
9
9
  import type { StrengthModel } from '../base/memory/strength.js';
10
10
  import type { EmbedderFor } from '../base/ports/embedder.js';
11
+ import type { DetachedWork } from '../domain/search/detached-work.js';
11
12
  /** Actor identity + role for a Context: the audit-log subject (pass `ctx.actor.subject` to audit helpers) and the role
12
13
  * for admin gating.
13
14
  * Role checks happen at the request boundary (e.g. /v1/sleep), except in authCreate and authRevoke (ForbiddenError).
@@ -74,6 +75,9 @@ export interface Context {
74
75
  * for. Set by a server or stdio root; the deadline itself rides in the store, whose worker
75
76
  * calls wait on it. */
76
77
  readonly signal?: AbortSignal | undefined;
78
+ /** Where the root keeps work a call starts and does not wait on, such as an embed, and settles it
79
+ * before closing its stores. Unset, the call waits for that work itself before it returns. */
80
+ readonly detached?: DetachedWork | undefined;
77
81
  }
78
82
  /** The strength model `ctx` scores by: its root's eval switches with its config's loss-aversion
79
83
  * ratio. */
@@ -4,7 +4,7 @@ import { DEFAULT_TENANT_ID } from '../util/tenant.js';
4
4
  import { BadRequestError } from '../errors/api-errors.js';
5
5
  import { randomUUID } from 'node:crypto';
6
6
  import { isJsonString } from '../util/json.js';
7
- import { tokenize } from '../util/tokenize.js';
7
+ import { tokenizeMin } from '../util/tokenize.js';
8
8
  import { ID_SUFFIX_CHARS } from '../util/token-text.js';
9
9
  import { calculateStrength, netWrong, PRODUCTION_STRENGTH, } from './strength.js';
10
10
  export const DEFAULT_SCHEMA_FIT = 0.5;
@@ -168,7 +168,7 @@ const TAG_OVERLAP_SCALE = 2;
168
168
  // hit.
169
169
  const CONTENT_ENOUGH_MIN_MATCHES = 5;
170
170
  function significantTokens(text) {
171
- return new Set(tokenize(text).filter((t) => t.length > FILLER_TOKEN_MAX_LENGTH));
171
+ return new Set(tokenizeMin(text, FILLER_TOKEN_MAX_LENGTH + 1));
172
172
  }
173
173
  /** computeSchemaFit against `source`, for a caller that holds counts and texts but no rows. */
174
174
  export function schemaFitFrom(content, tags, source) {
@@ -0,0 +1,15 @@
1
+ /** `text` with each code span replaced by `fill`, same length; an opener with no closer is text. */
2
+ export declare function blankCodeSpans(text: string, fill?: string): string;
3
+ /** One `**` delimiter run and the roles CommonMark gives it. */
4
+ export interface StrongMarker {
5
+ readonly index: number;
6
+ /** Left-flanking: it can open a span. */
7
+ readonly open: boolean;
8
+ /** Right-flanking: it can close a span. */
9
+ readonly close: boolean;
10
+ /** A word character on both sides, as in "2**10" and "foo**bar". */
11
+ readonly inWord: boolean;
12
+ }
13
+ /** Each `**` of `text` (code spans blanked) in order; a run after `/`, `*` or a backslash, or before `*`, is a glob. */
14
+ export declare function strongMarkers(text: string): StrongMarker[];
15
+ //# sourceMappingURL=markdown-delimiters.d.ts.map
@@ -0,0 +1,94 @@
1
+ // CommonMark's code span and "**" rules, read once for the quality gate and the capture extractor.
2
+ const PUNCTUATION = /[\p{P}\p{S}]/u;
3
+ const WORD = /[\p{L}\p{N}_]/u;
4
+ /** Every maximal backtick run of `text` as [start, end), grouped by its length, in text order. */
5
+ function runsByLength(text) {
6
+ const byLength = new Map();
7
+ for (const { 0: run, index } of text.matchAll(/`+/g)) {
8
+ const same = byLength.get(run.length) ?? [];
9
+ same.push([index, index + run.length]);
10
+ byLength.set(run.length, same);
11
+ }
12
+ return byLength;
13
+ }
14
+ /** `text` with each code span replaced by `fill`, same length; an opener with no closer is text. */
15
+ export function blankCodeSpans(text, fill = 'x') {
16
+ const byLength = runsByLength(text);
17
+ // Openers only move forward, so each length keeps a cursor that never goes back.
18
+ const cursor = new Map();
19
+ const closerEnd = (length, from) => {
20
+ const same = byLength.get(length) ?? [];
21
+ let k = cursor.get(length) ?? 0;
22
+ while (k < same.length && same[k][0] < from)
23
+ k++;
24
+ cursor.set(length, k);
25
+ return k < same.length ? same[k][1] : -1;
26
+ };
27
+ let out = '';
28
+ let copied = 0;
29
+ let at = 0;
30
+ while (at < text.length) {
31
+ if (text[at] === '\\') {
32
+ at += 2;
33
+ continue;
34
+ }
35
+ if (text[at] !== '`') {
36
+ at++;
37
+ continue;
38
+ }
39
+ let end = at;
40
+ while (text[end] === '`')
41
+ end++;
42
+ const close = closerEnd(end - at, end);
43
+ if (close < 0) {
44
+ at = end;
45
+ continue;
46
+ }
47
+ out += text.slice(copied, at) + fill.repeat(close - at);
48
+ copied = at = close;
49
+ }
50
+ return out + text.slice(copied);
51
+ }
52
+ function charBefore(text, i) {
53
+ if (i === 0)
54
+ return '';
55
+ const unit = text.charCodeAt(i - 1);
56
+ const pair = unit >= 0xdc00 && unit <= 0xdfff && i >= 2 && text.charCodeAt(i - 2) >= 0xd800;
57
+ return pair ? text.slice(i - 2, i) : text[i - 1];
58
+ }
59
+ function charAfter(text, i) {
60
+ const point = text.codePointAt(i);
61
+ return point === undefined ? '' : String.fromCodePoint(point);
62
+ }
63
+ const isSpace = (char) => char === '' || /\s/u.test(char);
64
+ const isPunctuation = (char) => char !== '' && PUNCTUATION.test(char);
65
+ /** Each `**` of `text` (code spans blanked) in order; a run after `/`, `*` or a backslash, or before `*`, is a glob. */
66
+ export function strongMarkers(text) {
67
+ const found = [];
68
+ for (const { index } of text.matchAll(/\*\*/g)) {
69
+ const before = charBefore(text, index);
70
+ const after = charAfter(text, index + 2);
71
+ if (['/', '*', '\\'].includes(before) || after === '*')
72
+ continue;
73
+ const [spaceBefore, spaceAfter] = [isSpace(before), isSpace(after)];
74
+ const [markBefore, markAfter] = [isPunctuation(before), isPunctuation(after)];
75
+ const marker = {
76
+ index,
77
+ open: !spaceAfter && (!markAfter || spaceBefore || markBefore),
78
+ close: !spaceBefore && (!markBefore || spaceAfter || markAfter),
79
+ inWord: WORD.test(before) && WORD.test(after),
80
+ };
81
+ found.push({ marker, slashed: after === '/' });
82
+ }
83
+ // "**/etc/hosts**" is bold when the next marker closes it with no space between; else it is "**/".
84
+ const bold = ({ marker, slashed }, i) => {
85
+ const next = found[i + 1];
86
+ if (!slashed)
87
+ return true;
88
+ if (next === undefined || next.slashed || !next.marker.close)
89
+ return false;
90
+ return !/\s/.test(text.slice(marker.index + 2, next.marker.index));
91
+ };
92
+ return found.filter(bold).map(({ marker }) => marker);
93
+ }
94
+ //# sourceMappingURL=markdown-delimiters.js.map
@@ -1,6 +1,6 @@
1
1
  // The memory-quality audit rules: pure functions of a row, so any store can be audited by them.
2
2
  import { canAutoDelete, isKeptForGood } from './keep-rules.js';
3
- import { assessAutomaticMemory, automaticDefect, BUNDLE_HEADER, isAutomaticEntry, isCertainReason, isFragment, isReleaseCommitNoise, isShortAndUnspecific, MIN_MEMORY_CHARS, MIN_SUBSTANTIVE_WORDS, substantiveWordCount, } from './memory-quality.js';
3
+ import { automaticDefect, BUNDLE_HEADER, isAutomaticEntry, isCertainReason, isFragment, isReleaseCommitNoise, isShortAndUnspecific, MIN_MEMORY_CHARS, MIN_SUBSTANTIVE_WORDS, storedDefect, substantiveWordCount, } from './memory-quality.js';
4
4
  import { heldTexts } from '../util/same-text.js';
5
5
  export function auditMemory(entry, backsObject = false) {
6
6
  const issue = classifyMemory(entry);
@@ -112,7 +112,7 @@ export function repairDefect(entry, parents) {
112
112
  return null;
113
113
  // Only a bundle header marks parts; a refined row keeps its lead line, so it is judged whole.
114
114
  if (!BUNDLE_HEADER.test(entry.content)) {
115
- const { reason } = assessAutomaticMemory(entry.content);
115
+ const reason = storedDefect(entry.content);
116
116
  return reason === null
117
117
  ? null
118
118
  : { reason, disposition: isCertainReason(reason) ? 'set-aside' : 'review' };
@@ -122,7 +122,7 @@ export function repairDefect(entry, parents) {
122
122
  return { reason: 'derived bundle has no safely parsed constituents', disposition: 'review' };
123
123
  }
124
124
  const reasons = texts
125
- .map((text) => assessAutomaticMemory(text).reason)
125
+ .map(storedDefect)
126
126
  .filter((reason) => reason !== null);
127
127
  if (reasons.length === 0)
128
128
  return null;
@@ -137,6 +137,6 @@ export function repairDefect(entry, parents) {
137
137
  /** The defect a store too old to repair can only report: an automatic row's text alone, for a
138
138
  * person to review. */
139
139
  export function previewDefect(row) {
140
- return isAutomaticEntry(row) ? assessAutomaticMemory(row.content).reason : null;
140
+ return isAutomaticEntry(row) ? storedDefect(row.content) : null;
141
141
  }
142
142
  //# sourceMappingURL=memory-audit.js.map
@@ -1,5 +1,7 @@
1
1
  import { type MemoryEntry } from './entry.js';
2
2
  export type AutomaticMemoryDefect = 'too-short' | 'release-activity' | 'sentence-fragment' | 'possible-fragment' | 'subjectless-outcome' | 'raw-output' | 'too-vague' | 'no-specificity';
3
+ /** Bump when a certain defect is added, so the next daily run or sleep cleans each store once more. */
4
+ export declare const QUALITY_GATE_VERSION = 2;
3
5
  /** Text shorter than this holds no fact worth keeping. */
4
6
  export declare const MIN_MEMORY_CHARS = 10;
5
7
  /** Fewer substantive words than this leave a memory too vague to act on. */
@@ -14,12 +16,22 @@ export interface AutomaticMemoryAssessment {
14
16
  export declare function substantiveWordCount(text: string): number;
15
17
  /** Preserves the auditor's existing destructive release-noise classification. */
16
18
  export declare function isReleaseCommitNoise(text: string): boolean;
19
+ export declare const QUOTE_MARK: RegExp;
20
+ /** The offset of the ")" that closes an aside the text began inside, or -1: a ")" before any "(" or
21
+ * open "[". Code spans do not count. */
22
+ export declare function asideCloseOffset(text: string): number;
23
+ /** Whether `text` starts inside a bold span or an aside, as rows the old capture segmenter cut do.
24
+ * Repair reads this; admission does not, since capture drops a sentence the split cut out of an aside. */
25
+ export declare function startsInsideCut(text: string): boolean;
17
26
  /** Names automatic-capture defects without inventing missing context. */
18
27
  export declare function assessAutomaticMemory(content: string): AutomaticMemoryAssessment;
19
28
  export declare function isCertainReason(reason: AutomaticMemoryDefect | null): boolean;
20
29
  /** The bar for sleep's derived memories and for repair: a defect the checks are sure of, else
21
30
  * null. */
22
31
  export declare function certainDefect(content: string): AutomaticMemoryDefect | null;
32
+ /** The defect repair and the audit find in a stored text: admission's, but a start inside a bold span
33
+ * or an aside outranks an uncertain reason. */
34
+ export declare function storedDefect(text: string): AutomaticMemoryDefect | null;
23
35
  export declare const BUNDLE_HEADER: RegExp;
24
36
  type Provenance = Pick<MemoryEntry, 'source' | 'confidence' | 'content' | 'extractedFrom' | 'dagLevel' | 'tags'>;
25
37
  /**