agent-recall-core 3.4.48 → 3.4.50
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.
- package/README.md +25 -0
- package/dist/digest/store.d.ts +15 -6
- package/dist/digest/store.d.ts.map +1 -1
- package/dist/digest/store.js +33 -20
- package/dist/digest/store.js.map +1 -1
- package/dist/embeddings/chunker.d.ts +102 -0
- package/dist/embeddings/chunker.d.ts.map +1 -0
- package/dist/embeddings/chunker.js +260 -0
- package/dist/embeddings/chunker.js.map +1 -0
- package/dist/embeddings/config.d.ts +93 -0
- package/dist/embeddings/config.d.ts.map +1 -0
- package/dist/embeddings/config.js +174 -0
- package/dist/embeddings/config.js.map +1 -0
- package/dist/embeddings/index-store.d.ts +61 -0
- package/dist/embeddings/index-store.d.ts.map +1 -0
- package/dist/embeddings/index-store.js +158 -0
- package/dist/embeddings/index-store.js.map +1 -0
- package/dist/embeddings/indexer.d.ts +75 -0
- package/dist/embeddings/indexer.d.ts.map +1 -0
- package/dist/embeddings/indexer.js +196 -0
- package/dist/embeddings/indexer.js.map +1 -0
- package/dist/embeddings/runtime.d.ts +66 -0
- package/dist/embeddings/runtime.d.ts.map +1 -0
- package/dist/embeddings/runtime.js +197 -0
- package/dist/embeddings/runtime.js.map +1 -0
- package/dist/helpers/associative-link.d.ts.map +1 -1
- package/dist/helpers/associative-link.js +26 -1
- package/dist/helpers/associative-link.js.map +1 -1
- package/dist/helpers/tokenize.d.ts +1 -0
- package/dist/helpers/tokenize.d.ts.map +1 -1
- package/dist/helpers/tokenize.js +18 -0
- package/dist/helpers/tokenize.js.map +1 -1
- package/dist/index.d.ts +27 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +42 -5
- package/dist/index.js.map +1 -1
- package/dist/palace/awareness.d.ts +56 -10
- package/dist/palace/awareness.d.ts.map +1 -1
- package/dist/palace/awareness.js +279 -182
- package/dist/palace/awareness.js.map +1 -1
- package/dist/palace/consolidate.d.ts +1 -1
- package/dist/palace/consolidate.d.ts.map +1 -1
- package/dist/palace/consolidate.js +9 -9
- package/dist/palace/consolidate.js.map +1 -1
- package/dist/palace/decay-pass.d.ts +1 -1
- package/dist/palace/decay-pass.d.ts.map +1 -1
- package/dist/palace/decay-pass.js +2 -2
- package/dist/palace/decay-pass.js.map +1 -1
- package/dist/palace/fan-out.d.ts +1 -1
- package/dist/palace/fan-out.d.ts.map +1 -1
- package/dist/palace/fan-out.js +5 -5
- package/dist/palace/fan-out.js.map +1 -1
- package/dist/palace/index-manager.d.ts +1 -1
- package/dist/palace/index-manager.d.ts.map +1 -1
- package/dist/palace/index-manager.js +1 -1
- package/dist/palace/index-manager.js.map +1 -1
- package/dist/palace/insights-index.d.ts +19 -6
- package/dist/palace/insights-index.d.ts.map +1 -1
- package/dist/palace/insights-index.js +36 -13
- package/dist/palace/insights-index.js.map +1 -1
- package/dist/palace/keystone.d.ts +1 -1
- package/dist/palace/keystone.d.ts.map +1 -1
- package/dist/palace/keystone.js +2 -2
- package/dist/palace/keystone.js.map +1 -1
- package/dist/palace/rooms.d.ts +3 -3
- package/dist/palace/rooms.d.ts.map +1 -1
- package/dist/palace/rooms.js +34 -6
- package/dist/palace/rooms.js.map +1 -1
- package/dist/prompts/consolidation-prompt.d.ts.map +1 -1
- package/dist/prompts/consolidation-prompt.js +5 -0
- package/dist/prompts/consolidation-prompt.js.map +1 -1
- package/dist/retrieval/candidates.d.ts.map +1 -1
- package/dist/retrieval/candidates.js +17 -2
- package/dist/retrieval/candidates.js.map +1 -1
- package/dist/retrieval/query-memory.d.ts +65 -7
- package/dist/retrieval/query-memory.d.ts.map +1 -1
- package/dist/retrieval/query-memory.js +518 -78
- package/dist/retrieval/query-memory.js.map +1 -1
- package/dist/retrieval/semantic-leg.d.ts +74 -0
- package/dist/retrieval/semantic-leg.d.ts.map +1 -0
- package/dist/retrieval/semantic-leg.js +180 -0
- package/dist/retrieval/semantic-leg.js.map +1 -0
- package/dist/storage/archive-write.d.ts.map +1 -1
- package/dist/storage/archive-write.js +29 -8
- package/dist/storage/archive-write.js.map +1 -1
- package/dist/storage/capture-router.d.ts +1 -1
- package/dist/storage/capture-router.d.ts.map +1 -1
- package/dist/storage/capture-router.js +26 -9
- package/dist/storage/capture-router.js.map +1 -1
- package/dist/storage/consolidation-queue.d.ts +11 -2
- package/dist/storage/consolidation-queue.d.ts.map +1 -1
- package/dist/storage/consolidation-queue.js +45 -16
- package/dist/storage/consolidation-queue.js.map +1 -1
- package/dist/storage/corrections.d.ts +53 -5
- package/dist/storage/corrections.d.ts.map +1 -1
- package/dist/storage/corrections.js +412 -124
- package/dist/storage/corrections.js.map +1 -1
- package/dist/storage/dream-health.d.ts +56 -8
- package/dist/storage/dream-health.d.ts.map +1 -1
- package/dist/storage/dream-health.js +130 -35
- package/dist/storage/dream-health.js.map +1 -1
- package/dist/storage/dream-yield.d.ts +81 -0
- package/dist/storage/dream-yield.d.ts.map +1 -0
- package/dist/storage/dream-yield.js +77 -0
- package/dist/storage/dream-yield.js.map +1 -0
- package/dist/storage/durable-intent.d.ts.map +1 -1
- package/dist/storage/durable-intent.js +24 -1
- package/dist/storage/durable-intent.js.map +1 -1
- package/dist/storage/filelock.d.ts +92 -12
- package/dist/storage/filelock.d.ts.map +1 -1
- package/dist/storage/filelock.js +317 -47
- package/dist/storage/filelock.js.map +1 -1
- package/dist/storage/fs-utils.d.ts +20 -0
- package/dist/storage/fs-utils.d.ts.map +1 -1
- package/dist/storage/fs-utils.js +27 -0
- package/dist/storage/fs-utils.js.map +1 -1
- package/dist/storage/paths.d.ts +34 -0
- package/dist/storage/paths.d.ts.map +1 -1
- package/dist/storage/paths.js +71 -0
- package/dist/storage/paths.js.map +1 -1
- package/dist/storage/pending.d.ts +154 -0
- package/dist/storage/pending.d.ts.map +1 -0
- package/dist/storage/pending.js +454 -0
- package/dist/storage/pending.js.map +1 -0
- package/dist/storage/project.d.ts.map +1 -1
- package/dist/storage/project.js +68 -16
- package/dist/storage/project.js.map +1 -1
- package/dist/storage/session-card.d.ts +13 -0
- package/dist/storage/session-card.d.ts.map +1 -1
- package/dist/storage/session-card.js +34 -3
- package/dist/storage/session-card.js.map +1 -1
- package/dist/storage/unclaimed.d.ts +165 -0
- package/dist/storage/unclaimed.d.ts.map +1 -0
- package/dist/storage/unclaimed.js +442 -0
- package/dist/storage/unclaimed.js.map +1 -0
- package/dist/storage/working-memory.d.ts.map +1 -1
- package/dist/storage/working-memory.js +19 -1
- package/dist/storage/working-memory.js.map +1 -1
- package/dist/supabase/recall-backend.d.ts +28 -2
- package/dist/supabase/recall-backend.d.ts.map +1 -1
- package/dist/supabase/recall-backend.js +80 -23
- package/dist/supabase/recall-backend.js.map +1 -1
- package/dist/supabase/sync.d.ts.map +1 -1
- package/dist/supabase/sync.js +13 -0
- package/dist/supabase/sync.js.map +1 -1
- package/dist/tools-logic/alignment-check.js +1 -1
- package/dist/tools-logic/alignment-check.js.map +1 -1
- package/dist/tools-logic/awareness-update.d.ts.map +1 -1
- package/dist/tools-logic/awareness-update.js +25 -16
- package/dist/tools-logic/awareness-update.js.map +1 -1
- package/dist/tools-logic/check-action.js +1 -1
- package/dist/tools-logic/check-action.js.map +1 -1
- package/dist/tools-logic/check.d.ts +55 -1
- package/dist/tools-logic/check.d.ts.map +1 -1
- package/dist/tools-logic/check.js +230 -57
- package/dist/tools-logic/check.js.map +1 -1
- package/dist/tools-logic/confidence.d.ts +11 -6
- package/dist/tools-logic/confidence.d.ts.map +1 -1
- package/dist/tools-logic/confidence.js +11 -6
- package/dist/tools-logic/confidence.js.map +1 -1
- package/dist/tools-logic/context-synthesize.js +2 -2
- package/dist/tools-logic/context-synthesize.js.map +1 -1
- package/dist/tools-logic/digest-read.js +2 -2
- package/dist/tools-logic/digest-read.js.map +1 -1
- package/dist/tools-logic/digest-recall.js +1 -1
- package/dist/tools-logic/digest-recall.js.map +1 -1
- package/dist/tools-logic/dream-admission.d.ts +144 -0
- package/dist/tools-logic/dream-admission.d.ts.map +1 -0
- package/dist/tools-logic/dream-admission.js +537 -0
- package/dist/tools-logic/dream-admission.js.map +1 -0
- package/dist/tools-logic/insight-promotion.d.ts +19 -1
- package/dist/tools-logic/insight-promotion.d.ts.map +1 -1
- package/dist/tools-logic/insight-promotion.js +48 -14
- package/dist/tools-logic/insight-promotion.js.map +1 -1
- package/dist/tools-logic/journal-capture.js +3 -3
- package/dist/tools-logic/journal-capture.js.map +1 -1
- package/dist/tools-logic/journal-write.js +3 -3
- package/dist/tools-logic/journal-write.js.map +1 -1
- package/dist/tools-logic/knowledge-write.js +2 -2
- package/dist/tools-logic/knowledge-write.js.map +1 -1
- package/dist/tools-logic/palace-lint.js +1 -1
- package/dist/tools-logic/palace-lint.js.map +1 -1
- package/dist/tools-logic/palace-read.js +1 -1
- package/dist/tools-logic/palace-read.js.map +1 -1
- package/dist/tools-logic/palace-search.js +1 -1
- package/dist/tools-logic/palace-search.js.map +1 -1
- package/dist/tools-logic/palace-walk.js +3 -3
- package/dist/tools-logic/palace-walk.js.map +1 -1
- package/dist/tools-logic/palace-write.js +4 -4
- package/dist/tools-logic/palace-write.js.map +1 -1
- package/dist/tools-logic/predict-correction.js +1 -1
- package/dist/tools-logic/predict-correction.js.map +1 -1
- package/dist/tools-logic/recall-backend.d.ts +11 -2
- package/dist/tools-logic/recall-backend.d.ts.map +1 -1
- package/dist/tools-logic/recall-backend.js +2 -2
- package/dist/tools-logic/recall-backend.js.map +1 -1
- package/dist/tools-logic/resurrect.d.ts.map +1 -1
- package/dist/tools-logic/resurrect.js +9 -1
- package/dist/tools-logic/resurrect.js.map +1 -1
- package/dist/tools-logic/safety-consolidation.d.ts +15 -0
- package/dist/tools-logic/safety-consolidation.d.ts.map +1 -1
- package/dist/tools-logic/safety-consolidation.js +44 -31
- package/dist/tools-logic/safety-consolidation.js.map +1 -1
- package/dist/tools-logic/session-end-reflect.d.ts.map +1 -1
- package/dist/tools-logic/session-end-reflect.js +4 -1
- package/dist/tools-logic/session-end-reflect.js.map +1 -1
- package/dist/tools-logic/session-end.d.ts +8 -0
- package/dist/tools-logic/session-end.d.ts.map +1 -1
- package/dist/tools-logic/session-end.js +51 -13
- package/dist/tools-logic/session-end.js.map +1 -1
- package/dist/tools-logic/session-start.d.ts +23 -0
- package/dist/tools-logic/session-start.d.ts.map +1 -1
- package/dist/tools-logic/session-start.js +44 -1
- package/dist/tools-logic/session-start.js.map +1 -1
- package/dist/tools-logic/smart-recall.d.ts +185 -10
- package/dist/tools-logic/smart-recall.d.ts.map +1 -1
- package/dist/tools-logic/smart-recall.js +363 -85
- package/dist/tools-logic/smart-recall.js.map +1 -1
- package/dist/tools-logic/store-doctor.d.ts.map +1 -1
- package/dist/tools-logic/store-doctor.js +14 -5
- package/dist/tools-logic/store-doctor.js.map +1 -1
- package/dist/tools-logic/store-repair.js +1 -1
- package/dist/tools-logic/store-repair.js.map +1 -1
- package/dist/tools-logic/supersession.d.ts +1 -1
- package/dist/tools-logic/supersession.d.ts.map +1 -1
- package/dist/tools-logic/supersession.js +2 -2
- package/dist/tools-logic/supersession.js.map +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/types.js +1 -1
- package/dist/vector/embedding.d.ts +8 -0
- package/dist/vector/embedding.d.ts.map +1 -1
- package/dist/vector/embedding.js +8 -0
- package/dist/vector/embedding.js.map +1 -1
- package/package.json +1 -1
|
@@ -106,7 +106,7 @@
|
|
|
106
106
|
import * as fs from "node:fs";
|
|
107
107
|
import * as path from "node:path";
|
|
108
108
|
import { getRoot } from "../types.js";
|
|
109
|
-
import { ensureDir } from "../storage/fs-utils.js";
|
|
109
|
+
import { ensureDir, writeTextAtomic } from "../storage/fs-utils.js";
|
|
110
110
|
import { expandQuery } from "../helpers/normalize.js";
|
|
111
111
|
import { tokenizeWords } from "../helpers/tokenize.js";
|
|
112
112
|
import { getConnectedRooms } from "../palace/graph.js";
|
|
@@ -114,6 +114,7 @@ import { palaceDir } from "../storage/paths.js";
|
|
|
114
114
|
import { calibratedConfidence, CONFIDENCE_FLOOR } from "./confidence.js";
|
|
115
115
|
import { fetchVerbatim } from "./drill-down.js";
|
|
116
116
|
import { resolveProject } from "../storage/project.js";
|
|
117
|
+
import { withLock, LockContentionError } from "../storage/filelock.js";
|
|
117
118
|
import { queryMemory, queryArchiveFallback } from "../retrieval/query-memory.js";
|
|
118
119
|
/** Compute both the human label and the stored calibrated value for a score. */
|
|
119
120
|
function label(score, scale) {
|
|
@@ -154,23 +155,46 @@ function readFeedbackLog() {
|
|
|
154
155
|
return [];
|
|
155
156
|
}
|
|
156
157
|
}
|
|
157
|
-
function processFeedback(feedback, query) {
|
|
158
|
+
async function processFeedback(feedback, query) {
|
|
158
159
|
ensureDir(path.dirname(feedbackLogPath()));
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
//
|
|
163
|
-
//
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
160
|
+
try {
|
|
161
|
+
// fix6-locks: the read→push→write span runs as ONE locked critical section.
|
|
162
|
+
// feedback-log.json is GLOBAL and written by every live session that
|
|
163
|
+
// submits recall feedback — the old unlocked read-modify-write dropped
|
|
164
|
+
// concurrent sessions' entries (feedback-log-concurrency.test.mjs
|
|
165
|
+
// reproduced 46/120 lost on main HEAD).
|
|
166
|
+
return await withLock("feedback-log", () => {
|
|
167
|
+
const log = readFeedbackLog();
|
|
168
|
+
const date = new Date().toISOString().slice(0, 10);
|
|
169
|
+
for (const f of feedback) {
|
|
170
|
+
// Only deduplicate when a stable ID is present. Without an ID there's no
|
|
171
|
+
// reliable key, so always log the entry (allows accumulation across calls).
|
|
172
|
+
const isDuplicate = f.id
|
|
173
|
+
? log.some((existing) => existing.query === query && existing.id === f.id && existing.date === date)
|
|
174
|
+
: false;
|
|
175
|
+
if (!isDuplicate) {
|
|
176
|
+
log.push({ query, id: f.id, title: f.title ?? "", useful: f.useful, date });
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
const updated = log.slice(-1000);
|
|
180
|
+
// Atomic: readers (readFeedbackLog in every smartRecall call) are
|
|
181
|
+
// lock-free — never let them observe a truncated file.
|
|
182
|
+
writeTextAtomic(feedbackLogPath(), JSON.stringify(updated, null, 2));
|
|
183
|
+
return { log: updated, persistenceSkipped: false };
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
catch (err) {
|
|
187
|
+
if (err instanceof LockContentionError) {
|
|
188
|
+
// Advisory ranking data: never fail (or stall) a recall because the
|
|
189
|
+
// feedback log is contended past the timeout — log the skip explicitly
|
|
190
|
+
// and rank with the current on-disk log. The entries are lost, loudly:
|
|
191
|
+
// stderr for the host log AND persistenceSkipped for the caller
|
|
192
|
+
// (surfaced as SmartRecallResult.feedback_log_skipped — review LOW-3).
|
|
193
|
+
console.error(`[agent-recall] smart_recall feedback: ${err.message} — this call's feedback entries were NOT persisted.`);
|
|
194
|
+
return { log: readFeedbackLog(), persistenceSkipped: true };
|
|
169
195
|
}
|
|
196
|
+
throw err;
|
|
170
197
|
}
|
|
171
|
-
const updated = log.slice(-1000);
|
|
172
|
-
fs.writeFileSync(feedbackLogPath(), JSON.stringify(updated, null, 2), "utf-8");
|
|
173
|
-
return updated;
|
|
174
198
|
}
|
|
175
199
|
/** Count positive and negative feedback for a result item. Query-aware. */
|
|
176
200
|
function getFeedbackCounts(id, title, queryWords, log) {
|
|
@@ -205,30 +229,6 @@ function verbatimKeyFor(item) {
|
|
|
205
229
|
return { kind: "archive", date: item.date, file: item.file };
|
|
206
230
|
return undefined;
|
|
207
231
|
}
|
|
208
|
-
/**
|
|
209
|
-
* v4 W3 (2026-09-08) type-compat shim, NOT a behavior change: `queryMemory`'s
|
|
210
|
-
* `QueryMemorySource` union grew a `"corrections"` member this wave (a new
|
|
211
|
-
* competing RRF tier — see retrieval/candidates.ts/query-memory.ts), but
|
|
212
|
-
* `SmartRecallResultItem.source`'s own external contract (this file's own
|
|
213
|
-
* header comment, "kept singular for backward compatibility with existing
|
|
214
|
-
* consumers") deliberately stays `"palace" | "journal" | "insight" |
|
|
215
|
-
* "archive"` — smart_recall does NOT opt into the corrections tier this wave
|
|
216
|
-
* (its own `tiers: ["palace", "journal", "insight"]` call below is
|
|
217
|
-
* unchanged), so widening this exported type would be a false, unrequested
|
|
218
|
-
* contract change for every existing consumer. This narrows the (now wider)
|
|
219
|
-
* `QueryMemorySource` back down at the one call site that needs it, and
|
|
220
|
-
* FAILS LOUDLY (never silently mislabels) if a future edit ever adds
|
|
221
|
-
* `"corrections"` to the `tiers` array below without also updating
|
|
222
|
-
* `SmartRecallResultItem.source`'s contract first.
|
|
223
|
-
*/
|
|
224
|
-
function excludeCorrectionsSource(source) {
|
|
225
|
-
if (source === "corrections") {
|
|
226
|
-
throw new Error("localRecallSearch: unexpected 'corrections' source — this file's own " +
|
|
227
|
-
"tiers list must not request it without first updating " +
|
|
228
|
-
"SmartRecallResultItem.source's external contract");
|
|
229
|
-
}
|
|
230
|
-
return source;
|
|
231
|
-
}
|
|
232
232
|
/**
|
|
233
233
|
* localRecallSearch — the core local search logic (palace + journal + insight).
|
|
234
234
|
*
|
|
@@ -252,7 +252,7 @@ function excludeCorrectionsSource(source) {
|
|
|
252
252
|
* side-channel contract are UNCHANGED, since those are real, direct external
|
|
253
253
|
* callers, not just smartRecall()'s own internals.
|
|
254
254
|
*/
|
|
255
|
-
export async function localRecallSearch(query, project, limit, since) {
|
|
255
|
+
export async function localRecallSearch(query, project, limit, since, freshnessBias) {
|
|
256
256
|
let resolvedProject;
|
|
257
257
|
try {
|
|
258
258
|
resolvedProject = await resolveProject(project);
|
|
@@ -263,14 +263,38 @@ export async function localRecallSearch(query, project, limit, since) {
|
|
|
263
263
|
const result = await queryMemory({
|
|
264
264
|
query,
|
|
265
265
|
project: resolvedProject,
|
|
266
|
-
// Order matters: RRF/fuseCanonical's "primary/display source"
|
|
267
|
-
// whichever source's items were inserted into the fusion map FIRST
|
|
268
|
-
// iteration = insertion order)
|
|
269
|
-
//
|
|
270
|
-
//
|
|
271
|
-
|
|
266
|
+
// Order matters TWICE: (a) RRF/fuseCanonical's "primary/display source"
|
|
267
|
+
// is whichever source's items were inserted into the fusion map FIRST
|
|
268
|
+
// (Map iteration = insertion order), and (b) the final fused sort is
|
|
269
|
+
// stable, so EXACT fused-score ties resolve in insertion order too.
|
|
270
|
+
// The ORIGINAL localRecallSearch queried palace, then journal, then
|
|
271
|
+
// insight — that relative order is preserved exactly
|
|
272
|
+
// (audit-retrieval-accounting.test.mjs asserts on it directly).
|
|
273
|
+
// fix4 S1 (2026-09-11) added "corrections" as the 4th competing tier;
|
|
274
|
+
// the v4 W3 shim (`excludeCorrectionsSource`) that guarded this exact
|
|
275
|
+
// wiring was retired in the same change that widened
|
|
276
|
+
// `SmartRecallResultItem.source`'s contract — the ordering its doc
|
|
277
|
+
// comment mandated ("update the contract first").
|
|
278
|
+
// fix4 S1-refinement (same day): corrections moved FIRST. With
|
|
279
|
+
// one-doc-one-vote scoring, single-source fused scores cluster at
|
|
280
|
+
// exactly 1/(60+rank), so cross-tier ties are the COMMON case — and a
|
|
281
|
+
// last-place insertion order systematically ranked the OWNER'S OWN
|
|
282
|
+
// CAPTURED RULE below every same-evidence derivative mention of it
|
|
283
|
+
// (palace notes, journal transcript lines). Authority order matches the
|
|
284
|
+
// product's existing doctrine (session_start P0 always-load, check()'s
|
|
285
|
+
// authoritative-override gate): at equal rank evidence, ground truth
|
|
286
|
+
// wins the tie. Fusion SCORES are order-independent (applyRRF sums per
|
|
287
|
+
// tier); only tie-break order and duplicate display-source change.
|
|
288
|
+
tiers: ["corrections", "palace", "journal", "insight"],
|
|
272
289
|
limit,
|
|
273
290
|
since,
|
|
291
|
+
// fix4 S4-completion: on THIS competitive surface one journal
|
|
292
|
+
// (date, section) gets one slot — see QueryMemoryInput.journal's own
|
|
293
|
+
// doc comment; journalSearch's per-line contract is unaffected.
|
|
294
|
+
journal: { perSectionDedupe: true },
|
|
295
|
+
// fix4b (2026-09-12): legacy boost opt-in, default OFF — see
|
|
296
|
+
// SmartRecallInput.freshnessBias.
|
|
297
|
+
...(freshnessBias ? { freshnessBias: true } : {}),
|
|
274
298
|
});
|
|
275
299
|
// Final materialization: rrf-local confidence label (matches the ORIGINAL
|
|
276
300
|
// dedup loop's `...label(score, "rrf-local")` — the only labeling that
|
|
@@ -280,9 +304,9 @@ export async function localRecallSearch(query, project, limit, since) {
|
|
|
280
304
|
// correctly dropped, not a behavior change).
|
|
281
305
|
const deduped = result.items.map((item) => ({
|
|
282
306
|
id: item.id,
|
|
283
|
-
source:
|
|
307
|
+
source: item.source,
|
|
284
308
|
...(item.alsoFoundIn && item.alsoFoundIn.length > 0
|
|
285
|
-
? { alsoFoundIn: item.alsoFoundIn
|
|
309
|
+
? { alsoFoundIn: item.alsoFoundIn }
|
|
286
310
|
: {}),
|
|
287
311
|
title: item.title,
|
|
288
312
|
excerpt: item.excerpt,
|
|
@@ -298,8 +322,20 @@ export async function localRecallSearch(query, project, limit, since) {
|
|
|
298
322
|
// though they had already, invisibly, changed this item's `score`/rank.
|
|
299
323
|
...(item.supersededBy ? { supersededBy: item.supersededBy } : {}),
|
|
300
324
|
...(item.conflictsWith && item.conflictsWith.length > 0 ? { conflictsWith: item.conflictsWith } : {}),
|
|
325
|
+
// fix7: semantic-origin marker (see SmartRecallResultItem.foundBySemantic).
|
|
326
|
+
...(item.semantic ? { foundBySemantic: true } : {}),
|
|
301
327
|
}));
|
|
302
|
-
// Graph walk —
|
|
328
|
+
// Graph walk — fix4 S2 (2026-09-11): the 1-hop graph signal is now
|
|
329
|
+
// METADATA on its parent result (`alsoLinked` on the top hit), never a
|
|
330
|
+
// competing result row. The old form pushed synthetic "↳ linked: <room>"
|
|
331
|
+
// stub items at 0.6× the top score, which the global re-sort landed at
|
|
332
|
+
// ranks 2-4 — the S2-standard golden eval measured them burning 24/100
|
|
333
|
+
// top-5 slots (a pure precision loss: a stub carries no retrievable
|
|
334
|
+
// content, no verbatimKey, no excerpt beyond a template line). The graph
|
|
335
|
+
// SIGNAL is preserved verbatim — same source (getConnectedRooms on the
|
|
336
|
+
// top result's room), same 2-room cap — an agent that wants the linked
|
|
337
|
+
// rooms' content follows up with a room-scoped query, exactly what it had
|
|
338
|
+
// to do with the stub rows anyway.
|
|
303
339
|
// Uses the RESOLVED project (a characterized fix over the original, which
|
|
304
340
|
// used the raw, possibly-unresolved `project` parameter here — a latent
|
|
305
341
|
// H1-class inconsistency for the "auto"-literal edge case; every existing
|
|
@@ -307,25 +343,36 @@ export async function localRecallSearch(query, project, limit, since) {
|
|
|
307
343
|
// difference for the common case and a strict improvement otherwise).
|
|
308
344
|
if (deduped.length > 0 && resolvedProject) {
|
|
309
345
|
const pd = palaceDir(resolvedProject);
|
|
310
|
-
const resultIds = new Set(deduped.map((r) => r.id));
|
|
311
346
|
const topRoom = deduped[0].room;
|
|
312
347
|
if (topRoom) {
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
348
|
+
// Rooms already visible among the substantive results carry their own
|
|
349
|
+
// slot — advertising them again as a link is redundant. (The OLD stub
|
|
350
|
+
// code's `resultIds.has(linkedRoom)` check compared room slugs against
|
|
351
|
+
// stableId hashes and so never excluded anything but its own earlier
|
|
352
|
+
// stubs; matching on the items' real `room` field is the check that
|
|
353
|
+
// comment always described.)
|
|
354
|
+
const visibleRooms = new Set(deduped.map((r) => r.room).filter(Boolean));
|
|
355
|
+
const linked = getConnectedRooms(pd, topRoom)
|
|
356
|
+
.filter((room) => !visibleRooms.has(room))
|
|
357
|
+
// Review M2-adjacent fix (2026-09-11): graph edge targets are not
|
|
358
|
+
// always room slugs — linkToSimilar historically minted edges to
|
|
359
|
+
// journal/correction item IDS (its `candidate.room ? room/id : id`
|
|
360
|
+
// target shape), and getConnectedRooms's `split("/")[0]` hands the
|
|
361
|
+
// bare id back as a pseudo-room. The old stub rows advertised those
|
|
362
|
+
// hashes verbatim ("↳ linked: k3f9a2"); alsoLinked only names
|
|
363
|
+
// targets that are REAL rooms on disk (_room.json exists — the same
|
|
364
|
+
// existence notion listRooms uses).
|
|
365
|
+
.filter((room) => {
|
|
366
|
+
try {
|
|
367
|
+
return fs.existsSync(path.join(pd, "rooms", room, "_room.json"));
|
|
368
|
+
}
|
|
369
|
+
catch {
|
|
370
|
+
return false;
|
|
328
371
|
}
|
|
372
|
+
})
|
|
373
|
+
.slice(0, 2);
|
|
374
|
+
if (linked.length > 0) {
|
|
375
|
+
deduped[0] = { ...deduped[0], alsoLinked: linked };
|
|
329
376
|
}
|
|
330
377
|
}
|
|
331
378
|
}
|
|
@@ -336,8 +383,15 @@ export async function localRecallSearch(query, project, limit, since) {
|
|
|
336
383
|
palace: result.candidatesBySource.palace ?? 0,
|
|
337
384
|
journal: result.candidatesBySource.journal ?? 0,
|
|
338
385
|
insight: result.candidatesBySource.insight ?? 0,
|
|
386
|
+
corrections: result.candidatesBySource.corrections ?? 0,
|
|
339
387
|
};
|
|
340
388
|
deduped[RAW_CANDIDATE_COUNTS] = rawCandidateCounts;
|
|
389
|
+
// fix7: semantic-leg diagnostics ride the same hidden-side-channel pattern
|
|
390
|
+
// (invisible to JSON/Object.keys — flag-off arrays carry NO new symbol
|
|
391
|
+
// because queryMemory only sets semanticLeg under the opt-in).
|
|
392
|
+
if (result.semanticLeg) {
|
|
393
|
+
deduped[SEMANTIC_LEG_NOTE] = result.semanticLeg;
|
|
394
|
+
}
|
|
341
395
|
return deduped;
|
|
342
396
|
}
|
|
343
397
|
/**
|
|
@@ -348,11 +402,28 @@ export async function localRecallSearch(query, project, limit, since) {
|
|
|
348
402
|
* existing tests and recall-backend.ts depend on that exact shape).
|
|
349
403
|
*/
|
|
350
404
|
const RAW_CANDIDATE_COUNTS = Symbol("rawCandidateCounts");
|
|
405
|
+
/**
|
|
406
|
+
* fix7: second hidden side channel — the semantic-leg note (set by
|
|
407
|
+
* localRecallSearch ONLY when queryMemory ran under the embeddings opt-in),
|
|
408
|
+
* surfaced by smartRecall() as `SmartRecallResult.semantic_leg`. Same
|
|
409
|
+
* pattern and rationale as RAW_CANDIDATE_COUNTS immediately above.
|
|
410
|
+
*/
|
|
411
|
+
const SEMANTIC_LEG_NOTE = Symbol("semanticLegNote");
|
|
351
412
|
/**
|
|
352
413
|
* Budget for the semantic (remote) backend in ms.
|
|
353
|
-
* Overridable via AGENT_RECALL_RECALL_BUDGET_MS for tuning / tests.
|
|
414
|
+
* Overridable via AGENT_RECALL_RECALL_BUDGET_MS for tuning / tests. Read
|
|
415
|
+
* PER-CALL, not cached at module load — this used to be a module-level
|
|
416
|
+
* `const` (frozen at first import), which silently made a per-test env-var
|
|
417
|
+
* override a no-op whenever the module had already been imported earlier in
|
|
418
|
+
* the same process (caught by independent code review, build #24,
|
|
419
|
+
* 2026-09-09 — the exact hazard `fusionEnabled()` below was already written
|
|
420
|
+
* to avoid for its own env var; this constant just hadn't been swept yet).
|
|
421
|
+
* `parseInt` on every call is negligible cost against an already-async
|
|
422
|
+
* network race.
|
|
354
423
|
*/
|
|
355
|
-
|
|
424
|
+
function recallBudgetMs() {
|
|
425
|
+
return parseInt(process.env.AGENT_RECALL_RECALL_BUDGET_MS ?? "2500", 10);
|
|
426
|
+
}
|
|
356
427
|
/**
|
|
357
428
|
* Wrap a promise with a wall-clock timeout.
|
|
358
429
|
* Resolves to null (never throws) when the deadline passes.
|
|
@@ -363,6 +434,168 @@ function withTimeout(promise, ms) {
|
|
|
363
434
|
promise.then((value) => { clearTimeout(timer); resolve(value); }, () => { clearTimeout(timer); resolve(null); });
|
|
364
435
|
});
|
|
365
436
|
}
|
|
437
|
+
// ---------------------------------------------------------------------------
|
|
438
|
+
// Remote+local fusion (#24, 2026-09-09) — feature-flagged.
|
|
439
|
+
// ---------------------------------------------------------------------------
|
|
440
|
+
/**
|
|
441
|
+
* Kill-switch / enable-switch for remote+local fusion. Mirrors
|
|
442
|
+
* `recallBudgetMs()`'s own `AGENT_RECALL_RECALL_BUDGET_MS` env convention —
|
|
443
|
+
* read PER-CALL, not cached, for the identical reason: this flag's own
|
|
444
|
+
* equivalence test (flag OFF ⇒ byte-identical to pre-wave) and dedup tests
|
|
445
|
+
* (flag ON) both need to toggle it within the SAME test file/process.
|
|
446
|
+
*/
|
|
447
|
+
function fusionEnabled() {
|
|
448
|
+
return process.env.AGENT_RECALL_RECALL_FUSION === "1";
|
|
449
|
+
}
|
|
450
|
+
/** RRF constant for the local<->remote merge below — same constant every
|
|
451
|
+
* other RRF pass in this codebase uses (query-memory.ts's `RRF_K`,
|
|
452
|
+
* supabase/recall-backend.ts's `RRF_K`). */
|
|
453
|
+
const FUSION_RRF_K = 60;
|
|
454
|
+
/** Normalize an excerpt for identity comparison — BYTE-IDENTICAL to
|
|
455
|
+
* query-memory.ts's `normalizeExcerpt` and supabase/recall-backend.ts's own
|
|
456
|
+
* inline dedup key (`item.excerpt.toLowerCase().replace(/\s+/g, " ").trim()`)
|
|
457
|
+
* — the SAME normalization every existing RRF/dedup pass in this codebase
|
|
458
|
+
* already uses, not a fourth reinvention. */
|
|
459
|
+
function normalizeExcerptForFusion(excerpt) {
|
|
460
|
+
return excerpt.toLowerCase().replace(/\s+/g, " ").trim();
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* Cross-origin fusion identity for one item. Prefers a `deriveSlug()`-shaped
|
|
464
|
+
* identity (EXACT — collapses a local item and a remote item that are
|
|
465
|
+
* genuinely the same on-disk file) and falls back to normalized-excerpt
|
|
466
|
+
* identity (the same fallback every existing RRF/dedup pass in this codebase
|
|
467
|
+
* already uses) when no slug-comparable identity can be produced:
|
|
468
|
+
*
|
|
469
|
+
* - Remote-origin items: `item.slug` is already populated verbatim by
|
|
470
|
+
* `supabase/recall-backend.ts`'s `mapSemanticRows`/`mapFtsRows` (INC1
|
|
471
|
+
* enrichment) — used directly.
|
|
472
|
+
* - Local-origin PALACE items: reconstructed from `verbatimKey.room`/
|
|
473
|
+
* `.file` — `palace--${room}--${file}`, matching `sync.ts`'s
|
|
474
|
+
* `deriveSlug()` output EXACTLY, since both derive from the same
|
|
475
|
+
* underlying room/file-basename pair for the same on-disk file.
|
|
476
|
+
* - INSIGHT items (either origin): `ar_insights.title` is UNIQUE at the DB
|
|
477
|
+
* level (migration.sql) and the LOCAL insight tier mints its own item id
|
|
478
|
+
* via `stableId("insight", i.title)` (query-memory.ts's
|
|
479
|
+
* `scoreInsightTier`) — `title` is therefore a genuinely precise,
|
|
480
|
+
* cross-origin-stable identity for this tier on BOTH sides, unlike its
|
|
481
|
+
* `excerpt` (`[${severity}] ${applies_when...}` locally, `[${severity}]
|
|
482
|
+
* confirmed ${n}x` remotely — exactly the low-entropy shape this file's
|
|
483
|
+
* own header (Fix 5b) already documents as UNSAFE as a fusion identity:
|
|
484
|
+
* two DIFFERENT insights sharing a severity + confirmation-count would
|
|
485
|
+
* falsely collapse under excerpt identity). Used in preference to the
|
|
486
|
+
* excerpt fallback below.
|
|
487
|
+
* - Every other local-origin item (journal, archive, graph-walk): CANNOT
|
|
488
|
+
* reconstruct an exact slug. Journal in particular carries only the bare
|
|
489
|
+
* authored `date` (`YYYY-MM-DD`), not the slug-suffixed filename
|
|
490
|
+
* `deriveSlug()` needs (`journal--${date}-${suffix}`) — two DIFFERENT
|
|
491
|
+
* journal entries from the same day would collapse under a date-only
|
|
492
|
+
* identity, a FALSE dedup that is worse than missing a true one. These
|
|
493
|
+
* fall back to normalized-excerpt identity instead.
|
|
494
|
+
*/
|
|
495
|
+
function fusionIdentity(item) {
|
|
496
|
+
if (item.slug)
|
|
497
|
+
return item.slug;
|
|
498
|
+
if (item.source === "palace" && item.verbatimKey?.kind === "palace" && item.verbatimKey.room && item.verbatimKey.file) {
|
|
499
|
+
return `palace--${item.verbatimKey.room}--${item.verbatimKey.file}`;
|
|
500
|
+
}
|
|
501
|
+
if (item.source === "insight" && item.title) {
|
|
502
|
+
return `insight::${item.title.toLowerCase().trim()}`;
|
|
503
|
+
}
|
|
504
|
+
return normalizeExcerptForFusion(item.excerpt);
|
|
505
|
+
}
|
|
506
|
+
/**
|
|
507
|
+
* remote-fusion wave #24 (2026-09-09) — rank-based RRF fusion of the local
|
|
508
|
+
* keyword-search results and the remote (Supabase) semantic-search results.
|
|
509
|
+
*
|
|
510
|
+
* THE BUG THIS FIXES: today, `smartRecall()`'s remote path is
|
|
511
|
+
* `results = remoteResults.length > 0 ? remoteResults : localResults` — the
|
|
512
|
+
* moment the remote backend answers with ANY non-empty result set, the ENTIRE
|
|
513
|
+
* local result set is discarded, even a genuine local-only hit the remote
|
|
514
|
+
* backend simply missed (e.g. a very recent journal entry not yet
|
|
515
|
+
* embedded/synced to Supabase — sync is fire-and-forget, see `sync.ts`'s
|
|
516
|
+
* `syncToSupabase()`). This function merges instead of replacing.
|
|
517
|
+
*
|
|
518
|
+
* RANK-BASED, NEVER RAW-SCORE (see this file's header, Fix 1, and
|
|
519
|
+
* confidence.ts's own header for the bug class this avoids): local's native
|
|
520
|
+
* score scale (~0..0.12, a 3-tier internal RRF) and remote's (~0..0.049, a
|
|
521
|
+
* 3-leg internal RRF, itself already re-labeled through a THIRD "cosine"/
|
|
522
|
+
* "rrf-supabase" scale) are mutually incompatible — summing them directly
|
|
523
|
+
* would let one origin systematically dominate, exactly the
|
|
524
|
+
* cross-source-raw-score-averaging bug this codebase has already fixed once.
|
|
525
|
+
* So neither list's `.score` is read here — only each item's RANK (its
|
|
526
|
+
* position within its OWN already-sorted list) feeds the RRF contribution
|
|
527
|
+
* `1/(FUSION_RRF_K+rank)`, the identical formula `query-memory.ts`'s
|
|
528
|
+
* `applyRRF` uses one layer down (there: fusing TIERS; here: fusing
|
|
529
|
+
* BACKENDS).
|
|
530
|
+
*
|
|
531
|
+
* ON A DUP (same `fusionIdentity()`): the LOCAL item's fields ALWAYS win —
|
|
532
|
+
* richer (real trust-filtered annotations: `supersededBy`/`conflictsWith`,
|
|
533
|
+
* match-anchored excerpt, `verbatimKey` for the Bridge) — only `.score` is
|
|
534
|
+
* replaced (the summed RRF contribution, used for this array's own sort
|
|
535
|
+
* order) and `foundInRemote` is set. `.confidence`/`.calibrated` are NEVER
|
|
536
|
+
* recomputed here: they stay exactly whatever the SURVIVING item's own
|
|
537
|
+
* origin already computed them as at ITS OWN scoring time (local items:
|
|
538
|
+
* "rrf-local" scale, set in `localRecallSearch`; remote-only items: "cosine"/
|
|
539
|
+
* "rrf-supabase", set in `recall-backend.ts`'s row mappers) — this is the
|
|
540
|
+
* safest possible way to keep `calibrated` correctly scaled to each item's
|
|
541
|
+
* ORIGIN: never re-deriving it from the new blended rank-score at all means
|
|
542
|
+
* it can never be accidentally run through the WRONG scale (Risk #8,
|
|
543
|
+
* confidence.ts's own header — the exact hazard this satisfies rather than
|
|
544
|
+
* reintroduces).
|
|
545
|
+
*
|
|
546
|
+
* Both input lists are assumed already rank-sorted (best first) — exactly
|
|
547
|
+
* what `localRecallSearch()`'s and `SupabaseRecallBackend.search()`'s own
|
|
548
|
+
* return values already are. Each input list is ALSO assumed to already be
|
|
549
|
+
* internally deduped (unique `fusionIdentity()` within its own list) —
|
|
550
|
+
* true today for both origins (`SupabaseRecallBackend.search()` already
|
|
551
|
+
* dedupes by id then by normalized excerpt before returning;
|
|
552
|
+
* `localRecallSearch()`'s own pipeline dedupes via `fuseCanonical()`). If
|
|
553
|
+
* that precondition were ever violated, a second same-identity item WITHIN
|
|
554
|
+
* one origin's own list would fold into the FIRST-seen item's map entry
|
|
555
|
+
* (same-origin collision, not a cross-origin one) — for a remote-side
|
|
556
|
+
* collision this would incorrectly leave `foundInRemote` at its default
|
|
557
|
+
* `false` (the field's own contract is "found in the OTHER origin", not
|
|
558
|
+
* "found more than once"); not a live bug given the precondition above, but
|
|
559
|
+
* worth knowing if either origin's own de-dup is ever relaxed.
|
|
560
|
+
*/
|
|
561
|
+
export function fuseRemoteWithLocal(localResults, remoteResults) {
|
|
562
|
+
const map = new Map();
|
|
563
|
+
// LOCAL pass first — a local item always seeds/owns its identity slot, so
|
|
564
|
+
// a later remote match folds INTO it rather than the reverse.
|
|
565
|
+
localResults.forEach((item, idx) => {
|
|
566
|
+
const contribution = 1 / (FUSION_RRF_K + (idx + 1));
|
|
567
|
+
const key = fusionIdentity(item);
|
|
568
|
+
const existing = map.get(key);
|
|
569
|
+
if (existing) {
|
|
570
|
+
existing.score += contribution;
|
|
571
|
+
}
|
|
572
|
+
else {
|
|
573
|
+
map.set(key, { score: contribution, item, foundInRemote: false });
|
|
574
|
+
}
|
|
575
|
+
});
|
|
576
|
+
// REMOTE pass — a match folds its rank contribution into the LOCAL entry
|
|
577
|
+
// (local item's own fields kept, `foundInRemote` marked); a remote-only
|
|
578
|
+
// identity becomes its own new entry (remote item's fields kept as-is).
|
|
579
|
+
remoteResults.forEach((item, idx) => {
|
|
580
|
+
const contribution = 1 / (FUSION_RRF_K + (idx + 1));
|
|
581
|
+
const key = fusionIdentity(item);
|
|
582
|
+
const existing = map.get(key);
|
|
583
|
+
if (existing) {
|
|
584
|
+
existing.score += contribution;
|
|
585
|
+
existing.foundInRemote = true;
|
|
586
|
+
}
|
|
587
|
+
else {
|
|
588
|
+
map.set(key, { score: contribution, item, foundInRemote: false });
|
|
589
|
+
}
|
|
590
|
+
});
|
|
591
|
+
const fused = [...map.values()].map(({ score, item, foundInRemote }) => ({
|
|
592
|
+
...item,
|
|
593
|
+
score,
|
|
594
|
+
...(foundInRemote ? { foundInRemote: true } : {}),
|
|
595
|
+
}));
|
|
596
|
+
fused.sort((a, b) => b.score - a.score);
|
|
597
|
+
return fused;
|
|
598
|
+
}
|
|
366
599
|
export async function smartRecall(input) {
|
|
367
600
|
// H1 fix (continuity wave review, 2026-07-31): resolve `project` ONCE here,
|
|
368
601
|
// the same way journalSearch/palaceSearch already resolve it internally on
|
|
@@ -384,9 +617,16 @@ export async function smartRecall(input) {
|
|
|
384
617
|
resolvedProject = input.project ?? "auto";
|
|
385
618
|
}
|
|
386
619
|
// Process feedback first; reuse the returned log to avoid a second disk read
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
620
|
+
let feedbackLogSkipped = false;
|
|
621
|
+
let feedbackLog;
|
|
622
|
+
if (input.feedback && input.feedback.length > 0) {
|
|
623
|
+
const fb = await processFeedback(input.feedback, input.query);
|
|
624
|
+
feedbackLog = fb.log;
|
|
625
|
+
feedbackLogSkipped = fb.persistenceSkipped;
|
|
626
|
+
}
|
|
627
|
+
else {
|
|
628
|
+
feedbackLog = readFeedbackLog();
|
|
629
|
+
}
|
|
390
630
|
const limit = input.limit ?? 10;
|
|
391
631
|
// CJK-aware (P0-b): shared tokenizer — feeds getFeedbackCounts' relevance
|
|
392
632
|
// weighting below with real word-segmented tokens instead of one giant
|
|
@@ -394,9 +634,14 @@ export async function smartRecall(input) {
|
|
|
394
634
|
const queryWords = expandQuery(tokenizeWords(input.query));
|
|
395
635
|
let results;
|
|
396
636
|
let degraded;
|
|
637
|
+
// remote-fusion wave #24 (2026-09-09) — additive observability only; see
|
|
638
|
+
// `SmartRecallResult.recall_path`'s own doc comment for why this stays
|
|
639
|
+
// `undefined` (never added to the returned object) whenever the fusion
|
|
640
|
+
// flag is off, which is what keeps flag-off output byte-identical.
|
|
641
|
+
let recallPath;
|
|
397
642
|
if (input.since) {
|
|
398
643
|
// `since` filter is only supported by localRecallSearch — always use local.
|
|
399
|
-
results = await localRecallSearch(input.query, input.project, limit, input.since);
|
|
644
|
+
results = await localRecallSearch(input.query, input.project, limit, input.since, input.freshnessBias);
|
|
400
645
|
}
|
|
401
646
|
else {
|
|
402
647
|
const { getRecallBackend, recordRemoteFailure, recordRemoteSuccess } = await import("./recall-backend.js");
|
|
@@ -405,32 +650,56 @@ export async function smartRecall(input) {
|
|
|
405
650
|
const isRemote = backendName === "SupabaseRecallBackend";
|
|
406
651
|
if (!isRemote) {
|
|
407
652
|
// Pure-local path: no budget needed.
|
|
408
|
-
results = await backend.search(input.query, input.project, limit);
|
|
653
|
+
results = await backend.search(input.query, input.project, limit, input.freshnessBias ? { freshnessBias: true } : undefined);
|
|
409
654
|
// If the vector backend returned nothing (index not yet populated), fall back to keyword search.
|
|
410
655
|
if (results.length === 0 && backendName === "LocalVectorRecallBackend") {
|
|
411
|
-
results = await localRecallSearch(input.query, input.project, limit);
|
|
656
|
+
results = await localRecallSearch(input.query, input.project, limit, undefined, input.freshnessBias);
|
|
412
657
|
}
|
|
413
658
|
}
|
|
414
659
|
else {
|
|
415
660
|
// Remote path: run local keyword search in parallel from the start.
|
|
416
|
-
// Use semantic results if they arrive within
|
|
417
|
-
//
|
|
418
|
-
const localPromise = localRecallSearch(input.query, input.project, limit);
|
|
419
|
-
|
|
661
|
+
// Use semantic results if they arrive within the budget; otherwise use
|
|
662
|
+
// local results (already computed — zero extra wait).
|
|
663
|
+
const localPromise = localRecallSearch(input.query, input.project, limit, undefined, input.freshnessBias);
|
|
664
|
+
// fix4b review MEDIUM-1: the remote backend's own scoring ignores the
|
|
665
|
+
// flag, but its INTERNAL local fallbacks (missing client / embed()
|
|
666
|
+
// failure return local results AS the "remote" result and record a
|
|
667
|
+
// remote success) must carry it — see SupabaseRecallBackend.search.
|
|
668
|
+
const remotePromise = backend.search(input.query, input.project, limit, input.freshnessBias ? { freshnessBias: true } : undefined);
|
|
420
669
|
const [localResults, remoteResults] = await Promise.all([
|
|
421
670
|
localPromise,
|
|
422
|
-
withTimeout(remotePromise,
|
|
671
|
+
withTimeout(remotePromise, recallBudgetMs()),
|
|
423
672
|
]);
|
|
424
673
|
if (remoteResults !== null) {
|
|
425
674
|
// Semantic results arrived in time — use them.
|
|
426
675
|
recordRemoteSuccess();
|
|
427
|
-
|
|
676
|
+
// #24 fusion gate: ONLY when the flag is on AND both sides answered
|
|
677
|
+
// non-empty — every other case (flag off, or either side empty)
|
|
678
|
+
// falls through to the pre-existing ternary UNCHANGED, so this
|
|
679
|
+
// `if` can only ever ADD a new branch, never alter the existing
|
|
680
|
+
// one's condition or result.
|
|
681
|
+
if (fusionEnabled() && localResults.length > 0 && remoteResults.length > 0) {
|
|
682
|
+
results = fuseRemoteWithLocal(localResults, remoteResults);
|
|
683
|
+
recallPath = "fused";
|
|
684
|
+
}
|
|
685
|
+
else {
|
|
686
|
+
results = remoteResults.length > 0 ? remoteResults : localResults;
|
|
687
|
+
// F-1 fix (pre-ship gate review, 2026-09-09): the label must match
|
|
688
|
+
// which side's data is actually in `results` — remote only when it
|
|
689
|
+
// genuinely answered non-empty; otherwise "local" (covers both
|
|
690
|
+
// "remote resolved empty, local had data" and "both empty"), never
|
|
691
|
+
// a hardcoded "remote" regardless of outcome.
|
|
692
|
+
if (fusionEnabled())
|
|
693
|
+
recallPath = remoteResults.length > 0 ? "remote" : "local";
|
|
694
|
+
}
|
|
428
695
|
}
|
|
429
696
|
else {
|
|
430
697
|
// Timed out (or errored inside withTimeout) — fall back to local.
|
|
431
698
|
recordRemoteFailure();
|
|
432
699
|
degraded = { reason: "timeout", backend: backendName };
|
|
433
700
|
results = localResults;
|
|
701
|
+
if (fusionEnabled())
|
|
702
|
+
recallPath = "local-timeout";
|
|
434
703
|
}
|
|
435
704
|
}
|
|
436
705
|
}
|
|
@@ -443,8 +712,10 @@ export async function smartRecall(input) {
|
|
|
443
712
|
const multiplier = betaUtility(positives, negatives) * 2;
|
|
444
713
|
item.score *= multiplier;
|
|
445
714
|
// Update the human-readable label only. `calibrated` stays the
|
|
446
|
-
// SCORING-TIME value so the bridge gate is not fooled by the
|
|
447
|
-
//
|
|
715
|
+
// SCORING-TIME value so the bridge gate is not fooled by the boost
|
|
716
|
+
// (Risk #8; since fix4b 2026-09-12 this Beta ×≤2 is the only remaining
|
|
717
|
+
// post-RRF score mutation — the hot-window no longer multiplies).
|
|
718
|
+
// Backends without `calibrated` (defensive) get one.
|
|
448
719
|
item.confidence = calibratedConfidence(item.score, "rrf-local").label;
|
|
449
720
|
if (typeof item.calibrated !== "number") {
|
|
450
721
|
item.calibrated = calibratedConfidence(item.score, "rrf-local").calibrated;
|
|
@@ -485,7 +756,7 @@ export async function smartRecall(input) {
|
|
|
485
756
|
// fused top-confidence of palace/journal/insight". This adds brand-new
|
|
486
757
|
// result items sourced from journal/archive/raw/, so it must never compete
|
|
487
758
|
// for rank inside the palace/journal/insight RRF fusion — it only steps in
|
|
488
|
-
// once those
|
|
759
|
+
// once those 4 competing sources have already failed to produce a confident #1
|
|
489
760
|
// answer. Placed AFTER the Bridge above so the Bridge's own `low` filter
|
|
490
761
|
// (which also matches any verbatimKey-bearing item) only ever considers
|
|
491
762
|
// genuine palace/journal/insight items — an archive item is already a raw
|
|
@@ -535,14 +806,18 @@ export async function smartRecall(input) {
|
|
|
535
806
|
// survivor count and can legitimately be smaller). The raw counts side
|
|
536
807
|
// channel is only present when `results` came straight from
|
|
537
808
|
// localRecallSearch's local multi-source pipeline; remote/vector-backend
|
|
538
|
-
// results have no "before fusion across
|
|
809
|
+
// results have no "before fusion across 4 sources" notion, so fall back to
|
|
539
810
|
// results.length for those (unchanged prior behavior). The archive source
|
|
540
811
|
// is intentionally excluded from this count — it is not part of the
|
|
541
|
-
//
|
|
812
|
+
// 4-source fan-out this diagnostic describes.
|
|
542
813
|
const rawCandidateCounts = results[RAW_CANDIDATE_COUNTS];
|
|
543
814
|
const totalSearched = rawCandidateCounts
|
|
544
|
-
? rawCandidateCounts.palace + rawCandidateCounts.journal + rawCandidateCounts.insight
|
|
815
|
+
? rawCandidateCounts.palace + rawCandidateCounts.journal + rawCandidateCounts.insight + rawCandidateCounts.corrections
|
|
545
816
|
: results.length;
|
|
817
|
+
// fix7: only ever set when the embeddings opt-in was on (see the side
|
|
818
|
+
// channel's own doc comment) — absent otherwise, keeping flag-off output
|
|
819
|
+
// byte-identical.
|
|
820
|
+
const semanticLegNote = results[SEMANTIC_LEG_NOTE];
|
|
546
821
|
const sourcesQueried = [...new Set(results.map((r) => r.source))];
|
|
547
822
|
// "archive" is reported whenever the gate ran, regardless of hit count —
|
|
548
823
|
// matches the existing convention in localRecallSearch (a source is
|
|
@@ -554,8 +829,11 @@ export async function smartRecall(input) {
|
|
|
554
829
|
results: finalResults,
|
|
555
830
|
total_searched: totalSearched,
|
|
556
831
|
sources_queried: sourcesQueried,
|
|
832
|
+
...(feedbackLogSkipped ? { feedback_log_skipped: true } : {}),
|
|
557
833
|
...(rawCandidateCounts ? { candidates_by_source: rawCandidateCounts } : {}),
|
|
558
834
|
...(degraded ? { degraded } : {}),
|
|
835
|
+
...(recallPath ? { recall_path: recallPath } : {}),
|
|
836
|
+
...(semanticLegNote ? { semantic_leg: semanticLegNote } : {}),
|
|
559
837
|
...(bridged ? { bridged } : {}),
|
|
560
838
|
...(finalResults.length === 0
|
|
561
839
|
? { guidance: "No results found. Try `session_start` to initialize this project, or `bootstrap_scan` to import existing context." }
|