pi-ghost-autocomplete 0.3.1 → 0.3.2

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 (155) hide show
  1. package/README.md +101 -6
  2. package/dist/bench-cli.js +81 -5
  3. package/dist/bench-cli.js.map +1 -1
  4. package/dist/bench.d.ts +130 -2
  5. package/dist/bench.d.ts.map +1 -1
  6. package/dist/bench.js +285 -4
  7. package/dist/bench.js.map +1 -1
  8. package/dist/cache.d.ts +124 -0
  9. package/dist/cache.d.ts.map +1 -0
  10. package/dist/cache.js +173 -0
  11. package/dist/cache.js.map +1 -0
  12. package/dist/candidate-rerank.d.ts +57 -0
  13. package/dist/candidate-rerank.d.ts.map +1 -0
  14. package/dist/candidate-rerank.js +78 -0
  15. package/dist/candidate-rerank.js.map +1 -0
  16. package/dist/config.d.ts +43 -0
  17. package/dist/config.d.ts.map +1 -1
  18. package/dist/config.js +148 -18
  19. package/dist/config.js.map +1 -1
  20. package/dist/context-aggregator.d.ts +14 -5
  21. package/dist/context-aggregator.d.ts.map +1 -1
  22. package/dist/context-aggregator.js +38 -9
  23. package/dist/context-aggregator.js.map +1 -1
  24. package/dist/context-selector.d.ts +128 -0
  25. package/dist/context-selector.d.ts.map +1 -0
  26. package/dist/context-selector.js +300 -0
  27. package/dist/context-selector.js.map +1 -0
  28. package/dist/cycle-manager.d.ts +102 -0
  29. package/dist/cycle-manager.d.ts.map +1 -0
  30. package/dist/cycle-manager.js +168 -0
  31. package/dist/cycle-manager.js.map +1 -0
  32. package/dist/edit-history-ring.d.ts +148 -0
  33. package/dist/edit-history-ring.d.ts.map +1 -0
  34. package/dist/edit-history-ring.js +262 -0
  35. package/dist/edit-history-ring.js.map +1 -0
  36. package/dist/entropy-stop.d.ts +102 -0
  37. package/dist/entropy-stop.d.ts.map +1 -0
  38. package/dist/entropy-stop.js +141 -0
  39. package/dist/entropy-stop.js.map +1 -0
  40. package/dist/eval-dataset.d.ts +66 -0
  41. package/dist/eval-dataset.d.ts.map +1 -0
  42. package/dist/eval-dataset.js +127 -0
  43. package/dist/eval-dataset.js.map +1 -0
  44. package/dist/fast-path-engine.d.ts +286 -0
  45. package/dist/fast-path-engine.d.ts.map +1 -0
  46. package/dist/fast-path-engine.js +341 -0
  47. package/dist/fast-path-engine.js.map +1 -0
  48. package/dist/fuzzy-matcher.d.ts +8 -0
  49. package/dist/fuzzy-matcher.d.ts.map +1 -1
  50. package/dist/fuzzy-matcher.js +5 -3
  51. package/dist/fuzzy-matcher.js.map +1 -1
  52. package/dist/ghost-editor.d.ts +321 -4
  53. package/dist/ghost-editor.d.ts.map +1 -1
  54. package/dist/ghost-editor.js +1190 -144
  55. package/dist/ghost-editor.js.map +1 -1
  56. package/dist/index.d.ts +17 -0
  57. package/dist/index.d.ts.map +1 -1
  58. package/dist/index.js +81 -2
  59. package/dist/index.js.map +1 -1
  60. package/dist/keystroke-timer.d.ts +59 -0
  61. package/dist/keystroke-timer.d.ts.map +1 -0
  62. package/dist/keystroke-timer.js +94 -0
  63. package/dist/keystroke-timer.js.map +1 -0
  64. package/dist/metrics-builder.d.ts +71 -0
  65. package/dist/metrics-builder.d.ts.map +1 -0
  66. package/dist/metrics-builder.js +71 -0
  67. package/dist/metrics-builder.js.map +1 -0
  68. package/dist/metrics.d.ts +99 -2
  69. package/dist/metrics.d.ts.map +1 -1
  70. package/dist/metrics.js +131 -17
  71. package/dist/metrics.js.map +1 -1
  72. package/dist/path-matcher.d.ts +9 -0
  73. package/dist/path-matcher.d.ts.map +1 -1
  74. package/dist/path-matcher.js +5 -2
  75. package/dist/path-matcher.js.map +1 -1
  76. package/dist/profile-store.d.ts +82 -0
  77. package/dist/profile-store.d.ts.map +1 -0
  78. package/dist/profile-store.js +199 -0
  79. package/dist/profile-store.js.map +1 -0
  80. package/dist/profile.d.ts +185 -0
  81. package/dist/profile.d.ts.map +1 -0
  82. package/dist/profile.js +434 -0
  83. package/dist/profile.js.map +1 -0
  84. package/dist/providers/cloud-provider.d.ts +10 -1
  85. package/dist/providers/cloud-provider.d.ts.map +1 -1
  86. package/dist/providers/cloud-provider.js +71 -14
  87. package/dist/providers/cloud-provider.js.map +1 -1
  88. package/dist/providers/local-provider.d.ts +8 -1
  89. package/dist/providers/local-provider.d.ts.map +1 -1
  90. package/dist/providers/local-provider.js +15 -3
  91. package/dist/providers/local-provider.js.map +1 -1
  92. package/dist/providers/openai-compat-provider.d.ts +11 -1
  93. package/dist/providers/openai-compat-provider.d.ts.map +1 -1
  94. package/dist/providers/openai-compat-provider.js +93 -6
  95. package/dist/providers/openai-compat-provider.js.map +1 -1
  96. package/dist/providers/openai-completions-runner.d.ts +25 -1
  97. package/dist/providers/openai-completions-runner.d.ts.map +1 -1
  98. package/dist/providers/openai-completions-runner.js +109 -14
  99. package/dist/providers/openai-completions-runner.js.map +1 -1
  100. package/dist/providers/race.d.ts +58 -1
  101. package/dist/providers/race.d.ts.map +1 -1
  102. package/dist/providers/race.js +317 -4
  103. package/dist/providers/race.js.map +1 -1
  104. package/dist/providers/truncate.d.ts.map +1 -1
  105. package/dist/providers/truncate.js +20 -2
  106. package/dist/providers/truncate.js.map +1 -1
  107. package/dist/providers/types.d.ts +96 -1
  108. package/dist/providers/types.d.ts.map +1 -1
  109. package/dist/providers/types.js +59 -1
  110. package/dist/providers/types.js.map +1 -1
  111. package/dist/provisional-manager.d.ts +93 -0
  112. package/dist/provisional-manager.d.ts.map +1 -0
  113. package/dist/provisional-manager.js +98 -0
  114. package/dist/provisional-manager.js.map +1 -0
  115. package/dist/provisional.d.ts +150 -0
  116. package/dist/provisional.d.ts.map +1 -0
  117. package/dist/provisional.js +238 -0
  118. package/dist/provisional.js.map +1 -0
  119. package/dist/regret-tracker.d.ts +84 -0
  120. package/dist/regret-tracker.d.ts.map +1 -0
  121. package/dist/regret-tracker.js +145 -0
  122. package/dist/regret-tracker.js.map +1 -0
  123. package/dist/replay-cli.d.ts +13 -0
  124. package/dist/replay-cli.d.ts.map +1 -0
  125. package/dist/replay-cli.js +167 -0
  126. package/dist/replay-cli.js.map +1 -0
  127. package/dist/replay.d.ts +107 -0
  128. package/dist/replay.d.ts.map +1 -0
  129. package/dist/replay.js +225 -0
  130. package/dist/replay.js.map +1 -0
  131. package/dist/reranker.d.ts +181 -0
  132. package/dist/reranker.d.ts.map +1 -0
  133. package/dist/reranker.js +324 -0
  134. package/dist/reranker.js.map +1 -0
  135. package/dist/session-trie.d.ts +103 -0
  136. package/dist/session-trie.d.ts.map +1 -0
  137. package/dist/session-trie.js +280 -0
  138. package/dist/session-trie.js.map +1 -0
  139. package/dist/speculative.d.ts +74 -0
  140. package/dist/speculative.d.ts.map +1 -0
  141. package/dist/speculative.js +107 -0
  142. package/dist/speculative.js.map +1 -0
  143. package/dist/tool-summarize.d.ts +40 -0
  144. package/dist/tool-summarize.d.ts.map +1 -0
  145. package/dist/tool-summarize.js +245 -0
  146. package/dist/tool-summarize.js.map +1 -0
  147. package/dist/trie-sanitize.d.ts +22 -0
  148. package/dist/trie-sanitize.d.ts.map +1 -0
  149. package/dist/trie-sanitize.js +142 -0
  150. package/dist/trie-sanitize.js.map +1 -0
  151. package/dist/versions.d.ts +23 -0
  152. package/dist/versions.d.ts.map +1 -0
  153. package/dist/versions.js +31 -0
  154. package/dist/versions.js.map +1 -0
  155. package/package.json +3 -2
@@ -0,0 +1,127 @@
1
+ /**
2
+ * Offline-eval dataset — capture + load. PRD §M5c.
3
+ *
4
+ * One JSONL example per committed prompt sample. The shape captures
5
+ * everything a replay run needs to reproduce the original ghost call:
6
+ * `(context, prefix, expected continuation, provider fingerprint)`.
7
+ *
8
+ * Privacy: examples DO contain raw prompt text. Capture is gated on the
9
+ * `PI_GHOST_EVAL_CAPTURE=1` env (not a default behavior — opt-in only).
10
+ * Sanitization still applies through `containsSensitive` so messages
11
+ * with API keys are dropped at capture time. Downstream tooling (CLI
12
+ * replay) ships a `--redact` flag that emits aggregate metrics without
13
+ * raw examples for publication.
14
+ */
15
+ import { appendFile, mkdir, readFile } from "node:fs/promises";
16
+ import { dirname, join } from "node:path";
17
+ import { containsSensitive } from "./trie-sanitize.js";
18
+ export const EVAL_REL = ".pi/ghost-autocomplete/eval.jsonl";
19
+ /** Char positions at which to sample prefixes from a committed line. */
20
+ export const PREFIX_SAMPLE_POSITIONS = [3, 5, 8, 12, 20];
21
+ /** Beyond `LONG_LINE_START`, sample every `LONG_LINE_STEP` chars. */
22
+ export const LONG_LINE_START = 20;
23
+ export const LONG_LINE_STEP = 10;
24
+ /**
25
+ * Generate sample (prefix, expected) splits from a committed line. PRD
26
+ * §M5c: positions `[3, 5, 8, 12, 20]` plus every 10 chars beyond 20.
27
+ * Returns empty when the committed text is too short for any sample.
28
+ */
29
+ export function samplePrefixes(committed) {
30
+ if (!committed)
31
+ return [];
32
+ const out = [];
33
+ const seen = new Set();
34
+ const len = committed.length;
35
+ for (const pos of PREFIX_SAMPLE_POSITIONS) {
36
+ if (pos >= len)
37
+ break;
38
+ seen.add(pos);
39
+ out.push({ prefixLen: pos, prefix: committed.slice(0, pos), expected: committed.slice(pos) });
40
+ }
41
+ for (let pos = LONG_LINE_START + LONG_LINE_STEP; pos < len; pos += LONG_LINE_STEP) {
42
+ if (seen.has(pos))
43
+ continue;
44
+ out.push({ prefixLen: pos, prefix: committed.slice(0, pos), expected: committed.slice(pos) });
45
+ }
46
+ return out;
47
+ }
48
+ /**
49
+ * Build eval examples for a single commit. Drops sensitive messages
50
+ * entirely. `context` should already be rendered by the caller (the
51
+ * provider's recent-messages block joined into a single string) — keeps
52
+ * this module decoupled from provider context shape.
53
+ */
54
+ export function buildExamplesFromCommit(committed, meta, ts = Date.now()) {
55
+ if (!committed)
56
+ return [];
57
+ // Skip multi-line commits — sampling assumes a single source line.
58
+ // First-line-only would distort expected continuations across newlines.
59
+ if (committed.includes("\n"))
60
+ return [];
61
+ if (containsSensitive(committed))
62
+ return [];
63
+ if (containsSensitive(meta.context))
64
+ return [];
65
+ return samplePrefixes(committed).map((s) => ({
66
+ ts,
67
+ committed,
68
+ prefixLen: s.prefixLen,
69
+ prefix: s.prefix,
70
+ expected: s.expected,
71
+ ...meta,
72
+ }));
73
+ }
74
+ export async function appendExamples(cwd, examples) {
75
+ if (examples.length === 0)
76
+ return;
77
+ const path = join(cwd, EVAL_REL);
78
+ try {
79
+ await mkdir(dirname(path), { recursive: true });
80
+ const body = examples.map((e) => JSON.stringify(e)).join("\n") + "\n";
81
+ await appendFile(path, body, "utf8");
82
+ }
83
+ catch {
84
+ // best-effort, never break completions
85
+ }
86
+ }
87
+ export async function loadExamples(cwd) {
88
+ const path = join(cwd, EVAL_REL);
89
+ let body;
90
+ try {
91
+ body = await readFile(path, "utf8");
92
+ }
93
+ catch {
94
+ return [];
95
+ }
96
+ return parseExamples(body);
97
+ }
98
+ export function parseExamples(body) {
99
+ const out = [];
100
+ for (const line of body.split("\n")) {
101
+ if (!line)
102
+ continue;
103
+ try {
104
+ const e = JSON.parse(line);
105
+ if (typeof e.ts === "number" &&
106
+ typeof e.committed === "string" &&
107
+ typeof e.prefixLen === "number" &&
108
+ typeof e.prefix === "string" &&
109
+ typeof e.expected === "string" &&
110
+ typeof e.context === "string" &&
111
+ typeof e.providerFingerprint === "string" &&
112
+ typeof e.versionFingerprint === "string" &&
113
+ typeof e.providerName === "string") {
114
+ out.push(e);
115
+ }
116
+ }
117
+ catch {
118
+ // partial trailing write — ignore
119
+ }
120
+ }
121
+ return out;
122
+ }
123
+ /** Parse `PI_GHOST_EVAL_CAPTURE`. Returns true only when set to "1". */
124
+ export function parseEvalCaptureEnv(raw) {
125
+ return raw === "1";
126
+ }
127
+ //# sourceMappingURL=eval-dataset.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"eval-dataset.js","sourceRoot":"","sources":["../src/eval-dataset.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC/D,OAAO,EAAE,OAAO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;gBACF,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;YACb,CAAC;QACF,CAAC;QAAC,MAAM,CAAC;YACR,kCAAkC;QACnC,CAAC;IACF,CAAC;IACD,OAAO,GAAG,CAAC;AACZ,CAAC;AAED,wEAAwE;AACxE,MAAM,UAAU,mBAAmB,CAAC,GAAuB;IAC1D,OAAO,GAAG,KAAK,GAAG,CAAC;AACpB,CAAC"}
@@ -0,0 +1,286 @@
1
+ /**
2
+ * Fast-path engine: owns the three sync ghost sources (session trie,
3
+ * completion cache, speculative-warm ring) and exposes a single lookup
4
+ * that returns the winning hit or null. PRD §M1 + §M2.
5
+ *
6
+ * Also exposes `decideDeterministic` — a pure function that resolves
7
+ * the path-prefix / fuzzy-filename decision used by the editor's
8
+ * deterministic ghost path (PRD §M4 deterministic matchers).
9
+ *
10
+ * Refactor #57 slices 1 + 2. Extracted from `ghost-editor.ts` to reduce
11
+ * the editor's responsibility surface. The engine is state-only — it
12
+ * does not touch the editor's render or metric pipeline; callers feed
13
+ * it fingerprints and read the result.
14
+ *
15
+ * Mutation methods (set/delete/insertFromAccept/penalize/...) are left
16
+ * on the inner instances (`engine.cache`, `engine.trie`,
17
+ * `engine.lastGhosts`) and accessed by the editor directly. Migrating
18
+ * those into named methods is a follow-up slice; the win here is
19
+ * consolidating the lookup ordering in one place so it can be unit
20
+ * tested without spinning up the editor.
21
+ *
22
+ * Remaining #57 slices: extract `applyDeterministic` mutation,
23
+ * ProvisionalManager, CycleManager, MetricsBuilder. ghost-editor.ts
24
+ * target: <800 LOC.
25
+ */
26
+ import { CompletionCache, type CacheValue } from "./cache.js";
27
+ import { SessionTrie } from "./session-trie.js";
28
+ import { LastGhostsRing, type LastGhost } from "./speculative.js";
29
+ import type { CompletionMode } from "./metrics.js";
30
+ import { type PathIndex } from "./path-index.js";
31
+ import { type FuzzyBand } from "./fuzzy-matcher.js";
32
+ import { EditHistoryRing, PreEditTextCache } from "./edit-history-ring.js";
33
+ import { type ProvisionalPick, type ReconcileResult } from "./provisional-manager.js";
34
+ import type { ProfileStore } from "./profile-store.js";
35
+ /**
36
+ * Result of a successful fast-path lookup. Shape intentionally mirrors
37
+ * the legacy inline-construction in `GhostEditor.handleTextChange` so
38
+ * the editor's `applyServedGhost` call site is unchanged.
39
+ *
40
+ * `cacheKey` is `undefined` for trie hits (the trie keys on raw prefix,
41
+ * not on the cache key) and a hex digest for cache/speculative hits.
42
+ */
43
+ export interface FastPathHit {
44
+ ghost: string;
45
+ mode: CompletionMode;
46
+ providerName: string;
47
+ cacheKey: string | undefined;
48
+ }
49
+ export interface FastPathLookupContext {
50
+ contextFingerprint: string;
51
+ providerFingerprint: string;
52
+ versionFingerprint: string;
53
+ /** Engine config fingerprint folded into the cache key + warm-path
54
+ * match. Optional — when omitted, the engine substitutes its own
55
+ * `getConfigFingerprint()` so callers get knob-change invalidation
56
+ * by default. */
57
+ configFingerprint?: string;
58
+ cooldownActive: boolean;
59
+ /** Optional `Date.now()` override for deterministic tests. */
60
+ now?: number;
61
+ }
62
+ /**
63
+ * Bundled view of every `PI_GHOST_*` env knob that affects lookup,
64
+ * provisional, rerank, or context selection. Captured once at engine
65
+ * construction so downstream consumers can migrate off per-call
66
+ * `process.env` reads incrementally.
67
+ *
68
+ * Phase 2 contract: this is **additive**. Existing call sites that still
69
+ * read `process.env.PI_GHOST_*` continue to work — the values here are
70
+ * the same strings the env exposed at construction time. Tests that
71
+ * mutate `process.env` between cases keep working against those call
72
+ * sites; once a consumer is migrated to read from `engine.config`, that
73
+ * test must construct a fresh engine.
74
+ *
75
+ * `editHistory` lives here too, surfaced from the same single env read
76
+ * the ring already does. Other modules (config.ts provider envs,
77
+ * metrics.ts logging envs, eval-dataset capture env) are out of scope —
78
+ * they don't influence lookup ordering.
79
+ */
80
+ export interface FastPathEngineConfig {
81
+ /** Raw `PI_GHOST_RERANK_WEIGHTS` (PRD §M3c). Parser in `reranker.ts`. */
82
+ rerankWeightsRaw: string | undefined;
83
+ /** Raw `PI_GHOST_MIN_CONFIDENCE` (PRD §M3c.1). Parser in `reranker.ts`. */
84
+ minConfidenceRaw: string | undefined;
85
+ /** Raw `PI_GHOST_PROVISIONAL` (PRD §M3.5b). Parser in `provisional.ts`. */
86
+ provisionalRaw: string | undefined;
87
+ /** Raw `PI_GHOST_PROVISIONAL_PREFER` (#6 swap-margin override). */
88
+ provisionalPreferRaw: string | undefined;
89
+ /** Raw `PI_GHOST_CONTEXT_BM25_WEIGHT` (PRD §M6b). */
90
+ contextBm25WeightRaw: string | undefined;
91
+ /** Raw `PI_GHOST_CONTEXT_EDIT_LOCALITY_WEIGHT` (#1 opt-in). */
92
+ contextEditLocalityWeightRaw: string | undefined;
93
+ /** Raw `PI_GHOST_CONTEXT_TOOL_MAX` (PRD §M6c). */
94
+ contextToolMaxRaw: string | undefined;
95
+ /** Raw `PI_GHOST_SHORT_HOVER_MS` (#3 short-hover-dismiss threshold). */
96
+ shortHoverMsRaw: string | undefined;
97
+ /** Raw `PI_GHOST_REGRET_WINDOW_MS` (PRD §M5a regret window). */
98
+ regretWindowMsRaw: string | undefined;
99
+ /** Raw `PI_GHOST_EVAL_CAPTURE` (PRD §M5c eval capture opt-in). */
100
+ evalCaptureRaw: string | undefined;
101
+ /** Edit-history (#1) — pre-parsed because the engine itself owns the ring. */
102
+ editHistory: {
103
+ enabled: boolean;
104
+ capacity: number;
105
+ maxAgeMs: number;
106
+ halfLifeMs: number;
107
+ };
108
+ }
109
+ /**
110
+ * Optional env source. Defaults to `process.env`. Tests can pass a fixed
111
+ * `Record<string, string | undefined>` to construct an engine with a
112
+ * specific knob state, no global mutation required.
113
+ */
114
+ export type EngineEnvSource = Record<string, string | undefined>;
115
+ /**
116
+ * Build the bundled config snapshot. Reads each knob once. Exported so
117
+ * tests + diag can compute the same fingerprint deterministically.
118
+ */
119
+ export declare function buildFastPathEngineConfig(env?: EngineEnvSource): FastPathEngineConfig;
120
+ /**
121
+ * Stable fingerprint over the bundled config. Suitable for folding into
122
+ * cache keys so a knob change (e.g. swapping rerank weights mid-session)
123
+ * invalidates affected entries. Field ordering is the JSON ordering
124
+ * above; do not reorder without bumping the fingerprint version.
125
+ */
126
+ export declare function fingerprintFastPathEngineConfig(cfg: FastPathEngineConfig): string;
127
+ export declare class FastPathEngine {
128
+ readonly cache: CompletionCache;
129
+ readonly trie: SessionTrie;
130
+ readonly lastGhosts: LastGhostsRing;
131
+ /**
132
+ * Enhancement #1 — edit-history ring + pre-edit text cache. Capacity /
133
+ * decay parameters read once at construction from
134
+ * `PI_GHOST_EDIT_HISTORY*` env. The `enabled` flag is the master
135
+ * switch: when false the editor skips `record()` (zero overhead) and
136
+ * skips injecting an `editProximityScorer` into the reranker.
137
+ */
138
+ readonly editHistory: EditHistoryRing;
139
+ readonly preEditCache: PreEditTextCache;
140
+ readonly editHistoryEnabled: boolean;
141
+ /**
142
+ * PRD §M4 — workspace path index used by the deterministic matchers
143
+ * (`decideDeterministic`). `null` until the editor's async indexer
144
+ * resolves; the deterministic step is skipped in that window.
145
+ */
146
+ private pathIndex;
147
+ /** Phase 2 — bundled env snapshot. Consumers migrate to read from here
148
+ * instead of `process.env` over time. Immutable for an engine's lifetime. */
149
+ readonly config: FastPathEngineConfig;
150
+ private readonly configFingerprintCached;
151
+ /**
152
+ * PRD §M3.5b — provisional ghost manager. Owned by the engine so the
153
+ * `picker` closure can be built from the engine's own trie + lastGhosts
154
+ * without the editor having to reach in. The editor still supplies the
155
+ * `canRender` predicate (editor-state) and `onShow` callback (UI mutator).
156
+ */
157
+ private readonly provisional;
158
+ constructor(env?: EngineEnvSource);
159
+ /**
160
+ * Stable fingerprint over the bundled env config. Suitable for folding
161
+ * into cache keys so a knob change invalidates affected entries. See
162
+ * `fingerprintFastPathEngineConfig` for the field-ordering contract.
163
+ */
164
+ getConfigFingerprint(): string;
165
+ /**
166
+ * Install or replace the workspace path index. Called once at editor
167
+ * startup after the async indexer resolves; may be re-called with
168
+ * `null` to disable deterministic matching (test / shutdown path).
169
+ */
170
+ setPathIndex(pi: PathIndex | null): void;
171
+ getPathIndex(): PathIndex | null;
172
+ /** PRD §M2 — seed (prefix, ghost) into the session trie after an accept. */
173
+ recordAccept(prefix: string, ghost: string, weight: number): void;
174
+ /** PRD §M2 / #5 — apply a negative-signal penalty to a (prefix, ghost) pair. */
175
+ penalize(prefix: string, ghost: string, magnitude: number): void;
176
+ /** PRD §M1a — write a completed ghost into the cache keyed on `cacheKey`. */
177
+ setCacheEntry(cacheKey: string, value: CacheValue): void;
178
+ /** PRD §M1a — evict a single cache entry on a non-accept outcome. */
179
+ deleteCacheEntry(cacheKey: string): void;
180
+ /** PRD §M1b — record a shown ghost into the speculative-warm ring. */
181
+ recordRecentGhost(entry: LastGhost): void;
182
+ /**
183
+ * PRD §M3.5b — arm a provisional ghost. Engine reads
184
+ * `PI_GHOST_PROVISIONAL` from the bundled config; no-op when disabled.
185
+ * The engine builds the `picker` from its own `trie` + `lastGhosts` +
186
+ * caller's profile. Editor supplies the `canRender` predicate
187
+ * (editor-state) and `onShow` callback (UI mutator).
188
+ */
189
+ armProvisional(input: {
190
+ prefix: string;
191
+ debounceMs: number;
192
+ profile: ProfileStore | null;
193
+ canRender: () => boolean;
194
+ onShow: (pick: ProvisionalPick) => void;
195
+ }): void;
196
+ /**
197
+ * PRD §M3.5b — reconcile an LLM result against any displayed
198
+ * provisional. Engine reads `PI_GHOST_PROVISIONAL_PREFER` from the
199
+ * bundled config.
200
+ */
201
+ reconcileProvisional(input: {
202
+ llm: {
203
+ text: string;
204
+ score: number;
205
+ } | null;
206
+ now: number;
207
+ }): ReconcileResult;
208
+ /** True iff a provisional pick is currently displayed. */
209
+ isProvisionalShown(): boolean;
210
+ /** Cancel a pending provisional timer without touching the displayed pick. */
211
+ cancelProvisionalTimer(): void;
212
+ /** Reset provisional state (timer + displayed pick). */
213
+ clearProvisional(): void;
214
+ /**
215
+ * Try, in order: session trie → completion cache → speculative warm.
216
+ * Returns null when nothing matches. Lookup is sync and allocation-light;
217
+ * fits inside a keystroke handler.
218
+ *
219
+ * `prefix` is the full pre-cursor text (multiline). The trie consults
220
+ * only the last line (committed messages + accepts are seeded against
221
+ * single-line prefixes). The cache/warm path consults the full prefix.
222
+ */
223
+ lookup(prefix: string, ctx: FastPathLookupContext): FastPathHit | null;
224
+ /**
225
+ * Deterministic-aware lookup. Runs `decideDeterministic` first (when
226
+ * a `deterministic` input is supplied) and falls back to the standard
227
+ * trie/cache/warm lookup. Returns a discriminated union so the caller
228
+ * can route deterministic decisions through its `applyDeterministic`
229
+ * mutator (different metrics path) while still using a single entry
230
+ * point. Pass `deterministic: null` to skip the path-prefix/fuzzy
231
+ * matchers (e.g., when an autocomplete popup is open).
232
+ *
233
+ * No editor state inside the engine — `deterministic` is supplied as a
234
+ * fully-formed `DeterministicInput`, and the editor owns the eventual
235
+ * mutation (`applyServedGhost` / `applyDeterministic`).
236
+ */
237
+ lookupWithDeterministic(prefix: string, ctx: FastPathLookupContext, deterministic: DeterministicInput | null): {
238
+ kind: "deterministic";
239
+ decision: DeterministicDecision;
240
+ } | {
241
+ kind: "fast-path";
242
+ hit: FastPathHit;
243
+ } | null;
244
+ }
245
+ /**
246
+ * Result of a successful deterministic-matcher decision. The editor
247
+ * applies this via `applyDeterministic()`. Pure — caller owns timing
248
+ * (`startedAt`) and editor-state mutation.
249
+ *
250
+ * `replaceFrom` is non-null for fuzzy matches that replace a typed token
251
+ * (e.g. `pkg.j` → `package.json`); null for path-prefix completions that
252
+ * append to the cursor.
253
+ */
254
+ export interface DeterministicDecision {
255
+ ghost: string;
256
+ replaceFrom: {
257
+ line: number;
258
+ col: number;
259
+ } | null;
260
+ providerName: string;
261
+ matchedPath: string;
262
+ }
263
+ export interface DeterministicInput {
264
+ pathIndex: PathIndex;
265
+ lineText: string;
266
+ cursorLine: number;
267
+ cursorCol: number;
268
+ recentPaths: Set<string>;
269
+ /** PRD §M4 — bounded boost from profile path-frequency. Null when no profile. */
270
+ profileBoost: ((path: string) => number) | null;
271
+ }
272
+ /**
273
+ * Resolve a deterministic ghost candidate from the active path index.
274
+ * Tries path-prefix first (e.g. `src/co|` → `src/config.ts`), then
275
+ * fuzzy-filename (e.g. `cfg|` → `config.ts` with replace).
276
+ *
277
+ * Pure function — does not consult `this`, autocomplete state, or
278
+ * cursor mutation. The editor's `tryDeterministic` wraps this with the
279
+ * `isShowingAutocomplete()` guard and the `applyDeterministic` mutation.
280
+ *
281
+ * Returns null when no candidate clears the score gates (≥70 fuzzy
282
+ * baseline; ≥85 for non-action-trigger contexts).
283
+ */
284
+ export declare function decideDeterministic(input: DeterministicInput): DeterministicDecision | null;
285
+ export declare function bandToProvider(band: FuzzyBand): string;
286
+ //# sourceMappingURL=fast-path-engine.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fast-path-engine.d.ts","sourceRoot":"","sources":["../src/fast-path-engine.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAGH,OAAO,EAAE,eAAe,EAAkB,KAAK,UAAU,EAAE,MAAM,YAAY,CAAC;AAC9E,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,cAAc,EAAE,KAAK,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAClE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AACnD,OAAO,EAAE,KAAK,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAEjD,OAAO,EAAqB,KAAK,SAAS,EAAqB,MAAM,oBAAoB,CAAC;AAC1F,OAAO,EACN,eAAe,EACf,gBAAgB,EAEhB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAIN,KAAK,eAAe,EACpB,KAAK,eAAe,EACpB,MAAM,0BAA0B,CAAC;AAElC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAEvD;;;;;;;GAOG;AACH,MAAM,WAAW,WAAW;IAC3B,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,cAAc,CAA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