@cortexkit/opencode-magic-context 0.49.34 → 0.49.35
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/dist/auto-search-worker.js +11 -6
- package/dist/chunk-embedding-write-worker.js +16 -14
- package/dist/{compaction-marker-candidates-31h86ph0.js → compaction-marker-candidates-g0p67ced.js} +3 -2
- package/dist/compaction-marker-candidates-worker.js +1 -1
- package/dist/{compartment-chunk-embedding-5q5gj67r.js → compartment-chunk-embedding-y4136are.js} +8 -7
- package/dist/{conflict-warning-hook-xgskea1v.js → conflict-warning-hook-hhw8z5ak.js} +9 -8
- package/dist/{context-p3xrarzt.js → context-fk8e3h9p.js} +1400 -701
- package/dist/{derive-budgets-pqybnx09.js → derive-budgets-htj16n4x.js} +3 -2
- package/dist/embedding-backfill-q6n72sy0.js +27 -0
- package/dist/embedding-worker-process.js +2 -1
- package/dist/embedding-worker.js +5 -4
- package/dist/expand-attachments-h33jbgyz.js +21 -0
- package/dist/features/magic-context/auto-search-diagnostics.d.ts +21 -0
- package/dist/features/magic-context/auto-search-diagnostics.d.ts.map +1 -0
- package/dist/features/magic-context/compartment-chunk-embedding.d.ts +1 -1
- package/dist/features/magic-context/compartment-chunk-embedding.d.ts.map +1 -1
- package/dist/features/magic-context/search.d.ts +4 -0
- package/dist/features/magic-context/search.d.ts.map +1 -1
- package/dist/features/magic-context/store-generation-rebase-labels.d.ts +6 -0
- package/dist/features/magic-context/store-generation-rebase-labels.d.ts.map +1 -0
- package/dist/features/magic-context/store-generation-rebase.d.ts.map +1 -1
- package/dist/{history-owner-36wp3mkb.js → history-owner-0z620wgz.js} +3 -2
- package/dist/hooks/magic-context/apply-operations.d.ts +20 -3
- package/dist/hooks/magic-context/apply-operations.d.ts.map +1 -1
- package/dist/hooks/magic-context/auto-search-observation.d.ts +13 -0
- package/dist/hooks/magic-context/auto-search-observation.d.ts.map +1 -0
- package/dist/hooks/magic-context/auto-search-runner.d.ts.map +1 -1
- package/dist/hooks/magic-context/auto-search-worker-client.d.ts +5 -1
- package/dist/hooks/magic-context/auto-search-worker-client.d.ts.map +1 -1
- package/dist/hooks/magic-context/compartment-commit-hashes.d.ts +4 -0
- package/dist/hooks/magic-context/compartment-commit-hashes.d.ts.map +1 -0
- package/dist/hooks/magic-context/compartment-runner-validation.d.ts +1 -0
- package/dist/hooks/magic-context/compartment-runner-validation.d.ts.map +1 -1
- package/dist/hooks/magic-context/event-resolvers.d.ts.map +1 -1
- package/dist/hooks/magic-context/final-wire-completeness.d.ts +15 -0
- package/dist/hooks/magic-context/final-wire-completeness.d.ts.map +1 -0
- package/dist/hooks/magic-context/final-wire-token-estimate.d.ts.map +1 -1
- package/dist/hooks/magic-context/raw-ordinal-warmup.d.ts +1 -0
- package/dist/hooks/magic-context/raw-ordinal-warmup.d.ts.map +1 -1
- package/dist/hooks/magic-context/read-session-chunk.d.ts +11 -0
- package/dist/hooks/magic-context/read-session-chunk.d.ts.map +1 -1
- package/dist/hooks/magic-context/read-session-formatting.d.ts +2 -0
- package/dist/hooks/magic-context/read-session-formatting.d.ts.map +1 -1
- package/dist/hooks/magic-context/transform-postprocess-phase.d.ts.map +1 -1
- package/dist/hooks/magic-context/transform.d.ts.map +1 -1
- package/dist/{index-as2amvn3.js → index-0c7p8mq5.js} +6 -531
- package/dist/{index-51rdvtkz.js → index-1rhgxqs8.js} +2 -2
- package/dist/index-2amn2s73.js +123 -0
- package/dist/{index-c7phnbvk.js → index-2p37txes.js} +1 -1
- package/dist/index-2qbtqg1w.js +529 -0
- package/dist/{index-waktfbjy.js → index-2v6nvy3k.js} +4 -4
- package/dist/{index-ftr84kkv.js → index-3d3872c6.js} +7 -5
- package/dist/{index-0677w9sd.js → index-3j9t1v4n.js} +55 -21
- package/dist/{index-atekmqef.js → index-4bgk5ztz.js} +363 -5499
- package/dist/{index-b88dwwrh.js → index-4xyjr62e.js} +1 -1
- package/dist/index-66a50f3e.js +777 -0
- package/dist/{index-04z754rd.js → index-87ctnzz9.js} +33 -5
- package/dist/{index-xrnsygfq.js → index-9wnbnx8f.js} +1 -1
- package/dist/{index-97spytxq.js → index-b6xz3qbe.js} +246 -420
- package/dist/{index-7g9dr8gy.js → index-brg2w6qc.js} +113 -133
- package/dist/{index-spwjbkc7.js → index-dnqqqc5f.js} +1 -1
- package/dist/index-dy7w6wm4.js +194 -0
- package/dist/{index-m1x9ghk5.js → index-ezv6dz3y.js} +37 -7
- package/dist/index-f297z1n8.js +4557 -0
- package/dist/{index-qgh1qwjj.js → index-f9b3vtt1.js} +1 -1
- package/dist/{index-v15ne3a3.js → index-faxb7mck.js} +2 -2
- package/dist/{index-rrv26qjz.js → index-fw42ms3m.js} +1 -1
- package/dist/{index-t5pck3ss.js → index-fxbk7gk3.js} +1 -1
- package/dist/{index-wfybj1dw.js → index-g4t7850m.js} +2 -2
- package/dist/{index-fm36bjv4.js → index-gyaa159j.js} +18 -276
- package/dist/{index-4pnsgvfk.js → index-hacf2tyx.js} +1 -1
- package/dist/{index-s1n8dd2n.js → index-jkanybgq.js} +2 -2
- package/dist/{index-2743x98y.js → index-jn0ddrhp.js} +29 -138
- package/dist/{index-h7tgn49j.js → index-jrfre9ah.js} +5 -222
- package/dist/{index-kfyb3k6n.js → index-jz5bk7xd.js} +2 -2
- package/dist/{index-np3rgfw4.js → index-ksv0a31x.js} +3 -3
- package/dist/{index-7kkphb6c.js → index-nj360hcs.js} +4 -4
- package/dist/{index-gkee6p4g.js → index-p56dszfp.js} +3 -3
- package/dist/{index-9gpw8hrm.js → index-pq0dpwrc.js} +4 -4
- package/dist/{index-rfxzazzy.js → index-pswsyf1w.js} +4 -4
- package/dist/{index-y1qwzvxn.js → index-pztqab6k.js} +2 -2
- package/dist/{index-rverk2vz.js → index-qncw9hey.js} +1 -1
- package/dist/index-qx8j6kds.js +270 -0
- package/dist/{index-kq5w68dp.js → index-txs8tce6.js} +1 -1
- package/dist/index-vzhhr1ht.js +532 -0
- package/dist/index-wh9fxds5.js +265 -0
- package/dist/{index-hbx7m8he.js → index-x2mrq5xv.js} +170 -48
- package/dist/{index-07vskjn8.js → index-xwwsbsd3.js} +15 -13
- package/dist/index-z4p0ry94.js +222 -0
- package/dist/index-z9zygwse.js +89 -0
- package/dist/{index-v7287tzq.js → index-zhqwda72.js} +19 -8
- package/dist/index.js +83 -72
- package/dist/{logger-94kew5wm.js → logger-qwnbcfj9.js} +2 -1
- package/dist/{message-fts-rowid-map-va36knh4.js → message-fts-rowid-map-xg7hr0mk.js} +5 -4
- package/dist/{message-index-bounded-qh91fdr3.js → message-index-bounded-narw7bnz.js} +12 -11
- package/dist/{message-time-backfill-4qva0b4f.js → message-time-backfill-39bm498k.js} +3 -2
- package/dist/migration-worker.js +13 -11
- package/dist/{project-embedding-registry-6yex0r9f.js → project-embedding-registry-tdz7985n.js} +13 -11
- package/dist/raw-ordinal-warmup-worker.js +12 -7
- package/dist/{read-session-chunk-kaghtr75.js → read-session-chunk-vmg1zjg0.js} +15 -10
- package/dist/{read-session-raw-b2sd67v4.js → read-session-raw-mncmzbm9.js} +3 -2
- package/dist/recomp-orchestrator-qyyhnc9b.js +68 -0
- package/dist/render-c5p0k846.js +43 -0
- package/dist/{safe-notification-target-mkr9yzvr.js → safe-notification-target-wztd7bgk.js} +3 -2
- package/dist/{search-archive-1kw2rp19.js → search-archive-0ny7p3bw.js} +13 -11
- package/dist/{search-p684j8ww.js → search-f6hwvcxk.js} +26 -23
- package/dist/{send-session-notification-vz86nt79.js → send-session-notification-0gwcz3fw.js} +7 -6
- package/dist/{storage-tags-6hvkk01c.js → storage-tags-zx1023km.js} +5 -4
- package/dist/store-620rjzt9.js +21 -0
- package/dist/store-reader-yh19h38c.js +14 -0
- package/dist/tools/ctx-expand/render.d.ts +5 -3
- package/dist/tools/ctx-expand/render.d.ts.map +1 -1
- package/dist/tools/ctx-expand/search-archive.d.ts.map +1 -1
- package/dist/tools/ctx-expand/tools.d.ts +2 -0
- package/dist/tools/ctx-expand/tools.d.ts.map +1 -1
- package/dist/tools/ctx-expand/v2-archive-reader.d.ts +33 -0
- package/dist/tools/ctx-expand/v2-archive-reader.d.ts.map +1 -0
- package/dist/{transform-postprocess-phase-ph38mhrs.js → transform-postprocess-phase-807ff6t2.js} +36 -30
- package/dist/v2/auto-search-worker.js +11 -6
- package/dist/v2/chunk-embedding-write-worker.js +16 -14
- package/dist/v2/{compaction-marker-candidates-m4vmye3w.js → compaction-marker-candidates-t1aq6gsj.js} +3 -2
- package/dist/v2/compaction-marker-candidates-worker.js +1 -1
- package/dist/v2/{compartment-chunk-embedding-rc31e6md.js → compartment-chunk-embedding-ncsk0r9e.js} +8 -7
- package/dist/v2/{context-s60hcatx.js → context-znqfkap4.js} +1725 -1095
- package/dist/v2/{derive-budgets-knf763tt.js → derive-budgets-sa0v7bz0.js} +4 -3
- package/dist/v2/embedding-backfill-11j425e0.js +26 -0
- package/dist/v2/embedding-worker-process.js +2 -1
- package/dist/v2/embedding-worker.js +5 -4
- package/dist/v2/expand-attachments-h33jbgyz.js +21 -0
- package/dist/v2/fold/idle.d.ts +15 -0
- package/dist/v2/fold/idle.d.ts.map +1 -0
- package/dist/v2/fold/native-replay-v3.d.ts +68 -0
- package/dist/v2/fold/native-replay-v3.d.ts.map +1 -0
- package/dist/v2/fold/native-replay.d.ts +58 -2
- package/dist/v2/fold/native-replay.d.ts.map +1 -1
- package/dist/v2/fold/policy.d.ts +2 -1
- package/dist/v2/fold/policy.d.ts.map +1 -1
- package/dist/v2/{history-owner-ad0crnrn.js → history-owner-fpka43cn.js} +3 -2
- package/dist/v2/hooks/context.d.ts.map +1 -1
- package/dist/v2/hooks/expand-attachments.d.ts +8 -0
- package/dist/v2/hooks/expand-attachments.d.ts.map +1 -0
- package/dist/v2/hooks/overflow-recovery.d.ts +45 -0
- package/dist/v2/hooks/overflow-recovery.d.ts.map +1 -0
- package/dist/v2/hooks/store.d.ts +7 -2
- package/dist/v2/hooks/store.d.ts.map +1 -1
- package/dist/v2/{logger-p34mjtja.js → logger-4cj6d03m.js} +2 -1
- package/dist/v2/{message-fts-rowid-map-78awh03v.js → message-fts-rowid-map-rmk571mb.js} +5 -4
- package/dist/v2/{message-index-bounded-4htdr1j6.js → message-index-bounded-620bppw7.js} +12 -11
- package/dist/v2/{message-time-backfill-zvthw6xb.js → message-time-backfill-0c4h7sg6.js} +3 -2
- package/dist/v2/migration-worker.js +13 -11
- package/dist/v2/ordinal-range.fixture.d.ts +7 -0
- package/dist/v2/ordinal-range.fixture.d.ts.map +1 -0
- package/dist/v2/{project-embedding-registry-1x5qe2fy.js → project-embedding-registry-bm5fe1a8.js} +13 -11
- package/dist/v2/raw-ordinal-warmup-worker.js +12 -7
- package/dist/v2/{read-session-chunk-1fa9qzwb.js → read-session-chunk-qfwv1k71.js} +15 -10
- package/dist/v2/{read-session-raw-5x088amc.js → read-session-raw-q294t8xs.js} +3 -2
- package/dist/v2/recomp-orchestrator-fcnjref7.js +67 -0
- package/dist/v2/render-98a1qv0b.js +42 -0
- package/dist/v2/{safe-notification-target-khnzg57c.js → safe-notification-target-gphhast7.js} +3 -2
- package/dist/v2/{search-archive-xfp51s0g.js → search-archive-r0y1bafr.js} +13 -11
- package/dist/v2/{search-wf8rqxhm.js → search-z1zqzsmh.js} +25 -22
- package/dist/v2/{send-session-notification-x5nzy34v.js → send-session-notification-fst6hkg7.js} +7 -6
- package/dist/v2/{server-qvy4qavp.js → server-1xesqm5h.js} +113 -133
- package/dist/v2/server-2amn2s73.js +123 -0
- package/dist/v2/{server-6g98c227.js → server-3schkz95.js} +4 -4
- package/dist/v2/server-468mya5v.js +209 -0
- package/dist/v2/server-55v5hpth.js +270 -0
- package/dist/v2/server-5jp38p98.js +532 -0
- package/dist/v2/{server-qjvk4a9t.js → server-69m30b21.js} +1 -1
- package/dist/v2/{server-jw5qe9y8.js → server-7ccxt4nq.js} +14 -216
- package/dist/v2/{server-3mz2qh5a.js → server-969cdpmg.js} +1 -1
- package/dist/v2/{server-835q39kd.js → server-9k0mjef7.js} +2 -2
- package/dist/v2/{server-qc915kw2.js → server-bez34e60.js} +4 -4
- package/dist/v2/{server-nxas8v1d.js → server-bpvsvtph.js} +3 -3
- package/dist/v2/{server-ab8017gw.js → server-bxpz2dcn.js} +29 -138
- package/dist/v2/{server-bsgd4dk1.js → server-dcws5831.js} +2 -2
- package/dist/v2/server-ef1mskas.js +777 -0
- package/dist/v2/{server-1bw1wcbp.js → server-f0j864ma.js} +1 -1
- package/dist/v2/{server-z2wft9zv.js → server-fqj6ddpr.js} +402 -5497
- package/dist/v2/{server-grakwy1v.js → server-hnknwaz8.js} +37 -7
- package/dist/v2/{server-h1byrhwx.js → server-hsf0qsn5.js} +19 -8
- package/dist/v2/{server-6495nmn9.js → server-jb6tjyeb.js} +5 -222
- package/dist/v2/{server-2aasmpsp.js → server-kkxzwa6w.js} +4 -4
- package/dist/v2/{server-a7x00tw8.js → server-mfn1yf9d.js} +1 -1
- package/dist/v2/{server-tkdabx8q.js → server-q0z2q2dx.js} +5 -530
- package/dist/v2/{server-ajxhrgjv.js → server-qa74bj91.js} +77 -17
- package/dist/v2/server-qf2kzsh2.js +4513 -0
- package/dist/v2/{server-gyy4k6mf.js → server-qmmkhz2z.js} +1 -1
- package/dist/v2/{server-29qp8nz4.js → server-qxzbmtqt.js} +1 -1
- package/dist/v2/{server-m1pfg2d4.js → server-r3chnxs5.js} +4 -4
- package/dist/v2/{server-vr4szghm.js → server-rktnzs89.js} +2 -2
- package/dist/v2/{server-6vd2n183.js → server-s9xgyyj9.js} +1 -1
- package/dist/v2/{server-53eh1r8r.js → server-sd8hvzte.js} +2 -2
- package/dist/v2/{server-k3t06fxr.js → server-sefxj8k1.js} +1 -1
- package/dist/v2/{server-v77xa5wj.js → server-tpt7bwq8.js} +1 -1
- package/dist/v2/{server-awr8qa4c.js → server-ws5exe6q.js} +2 -2
- package/dist/v2/server-x4m6xcn4.js +194 -0
- package/dist/v2/{server-806rzhs6.js → server-xbtw0p9t.js} +1 -1
- package/dist/v2/{server-cqjvxyrp.js → server-xy7w7hrd.js} +2 -2
- package/dist/v2/server-ypf1ap0v.js +529 -0
- package/dist/v2/server-z4p0ry94.js +222 -0
- package/dist/v2/server-z9zygwse.js +89 -0
- package/dist/v2/{server-ab1g0wn9.js → server-zzgtgqmd.js} +2 -2
- package/dist/v2/server.js +6 -5
- package/dist/v2/{storage-tags-18nbaanf.js → storage-tags-19s0ebf0.js} +5 -4
- package/dist/v2/store-reader-cgx47zkp.js +14 -0
- package/dist/v2/store-reader.d.ts +9 -2
- package/dist/v2/store-reader.d.ts.map +1 -1
- package/dist/v2/store-vdm8v3jd.js +21 -0
- package/dist/v2/v2-archive-reader-t2c0eypn.js +11 -0
- package/dist/v2/{wrapup-orchestrator-wxf7n5k6.js → wrapup-orchestrator-aqpqsveg.js} +46 -39
- package/dist/v2-archive-reader-tm2eayp2.js +11 -0
- package/dist/wrapup-orchestrator-k5yp19a6.js +55 -0
- package/package.json +1 -1
- package/src/shared/sqlite-transaction-mode-guard.test.ts +2 -0
- package/dist/embedding-backfill-0jgtqxen.js +0 -25
- package/dist/recomp-orchestrator-0y983w7h.js +0 -62
- package/dist/v2/embedding-backfill-f43hd8y6.js +0 -24
- package/dist/v2/recomp-orchestrator-7q9nvxte.js +0 -61
- package/dist/wrapup-orchestrator-ys2nrkkb.js +0 -49
|
@@ -0,0 +1,529 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getHarness2
|
|
3
|
+
} from "./server-pavzsdc6.js";
|
|
4
|
+
import {
|
|
5
|
+
isTransientSqliteError2
|
|
6
|
+
} from "./server-gzy5w8ya.js";
|
|
7
|
+
import {
|
|
8
|
+
managedAuthorityNoteRow
|
|
9
|
+
} from "./server-3schkz95.js";
|
|
10
|
+
|
|
11
|
+
// src/features/magic-context/storage-notes.ts
|
|
12
|
+
var NOTE_TYPES = new Set(["session", "smart"]);
|
|
13
|
+
var NOTE_STATUSES = new Set(["active", "pending", "ready", "dismissed"]);
|
|
14
|
+
var NOTE_CHECK_STATUSES = new Set([
|
|
15
|
+
"uncompiled",
|
|
16
|
+
"compiled",
|
|
17
|
+
"failing",
|
|
18
|
+
"fallback",
|
|
19
|
+
"parked"
|
|
20
|
+
]);
|
|
21
|
+
var CONDITION_COMPILE_STATUSES = new Set([
|
|
22
|
+
"compiled",
|
|
23
|
+
"plain",
|
|
24
|
+
"refused"
|
|
25
|
+
]);
|
|
26
|
+
var DEFAULT_SMART_STATUSES = ["pending", "ready"];
|
|
27
|
+
function toNullableString(value) {
|
|
28
|
+
return typeof value === "string" && value.length > 0 ? value : null;
|
|
29
|
+
}
|
|
30
|
+
function toNullableNumber(value) {
|
|
31
|
+
return typeof value === "number" ? value : null;
|
|
32
|
+
}
|
|
33
|
+
function isNoteRow(row) {
|
|
34
|
+
if (row === null || typeof row !== "object")
|
|
35
|
+
return false;
|
|
36
|
+
const candidate = row;
|
|
37
|
+
return typeof candidate.id === "number" && typeof candidate.type === "string" && NOTE_TYPES.has(candidate.type) && typeof candidate.status === "string" && NOTE_STATUSES.has(candidate.status) && typeof candidate.content === "string" && (candidate.session_id === null || typeof candidate.session_id === "string") && (candidate.project_path === null || typeof candidate.project_path === "string") && (candidate.surface_condition === null || typeof candidate.surface_condition === "string") && typeof candidate.created_at === "number" && typeof candidate.updated_at === "number" && (candidate.last_checked_at === null || typeof candidate.last_checked_at === "number") && (candidate.ready_at === null || typeof candidate.ready_at === "number") && (candidate.ready_reason === null || typeof candidate.ready_reason === "string");
|
|
38
|
+
}
|
|
39
|
+
function toNote(row) {
|
|
40
|
+
return {
|
|
41
|
+
id: row.id,
|
|
42
|
+
type: row.type,
|
|
43
|
+
status: row.status,
|
|
44
|
+
content: row.content,
|
|
45
|
+
sessionId: toNullableString(row.session_id),
|
|
46
|
+
projectPath: toNullableString(row.project_path),
|
|
47
|
+
surfaceCondition: toNullableString(row.surface_condition),
|
|
48
|
+
compiledProvider: toNullableString(row.compiled_provider),
|
|
49
|
+
compiledConfig: toNullableString(row.compiled_config),
|
|
50
|
+
compiledAt: toNullableNumber(row.compiled_at),
|
|
51
|
+
compileStatus: typeof row.compile_status === "string" && CONDITION_COMPILE_STATUSES.has(row.compile_status) ? row.compile_status : null,
|
|
52
|
+
createdAt: row.created_at,
|
|
53
|
+
updatedAt: row.updated_at,
|
|
54
|
+
lastCheckedAt: toNullableNumber(row.last_checked_at),
|
|
55
|
+
readyAt: toNullableNumber(row.ready_at),
|
|
56
|
+
readyReason: toNullableString(row.ready_reason),
|
|
57
|
+
anchorOrdinal: toNullableNumber(row.anchor_ordinal),
|
|
58
|
+
compiledCheck: toNullableString(row.compiled_check),
|
|
59
|
+
manifestJson: toNullableString(row.manifest_json),
|
|
60
|
+
checkHash: toNullableString(row.check_hash),
|
|
61
|
+
checkCron: toNullableString(row.check_cron),
|
|
62
|
+
checkVersion: toNullableNumber(row.check_version),
|
|
63
|
+
checkStatus: typeof row.check_status === "string" && NOTE_CHECK_STATUSES.has(row.check_status) ? row.check_status : null,
|
|
64
|
+
checkFailureCount: toNullableNumber(row.check_failure_count) ?? 0,
|
|
65
|
+
checkNetworkFailureCount: toNullableNumber(row.check_network_failure_count) ?? 0,
|
|
66
|
+
checkQuarantinedUntil: toNullableNumber(row.check_quarantined_until),
|
|
67
|
+
checkNextDueAt: toNullableNumber(row.check_next_due_at),
|
|
68
|
+
checkCompiledAt: toNullableNumber(row.check_compiled_at),
|
|
69
|
+
checkFalseSinceAt: toNullableNumber(row.check_false_since_at),
|
|
70
|
+
checkLastLivenessAt: toNullableNumber(row.check_last_liveness_at),
|
|
71
|
+
policyVersion: toNullableNumber(row.policy_version)
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
function noteCheckColumnsExist(db) {
|
|
75
|
+
try {
|
|
76
|
+
const rows = db.prepare("PRAGMA table_info(notes)").all();
|
|
77
|
+
return rows.some((row) => row.name === "compiled_check");
|
|
78
|
+
} catch {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
var PENDING_SESSION_NOTE_PREDICATE = "type = 'session' AND status = 'pending' AND surface_condition IS NOT NULL";
|
|
83
|
+
function healPendingSessionNotes(db) {
|
|
84
|
+
let predicate = `${PENDING_SESSION_NOTE_PREDICATE} AND NOT ${managedAuthorityNoteRow("notes")}`;
|
|
85
|
+
let healable;
|
|
86
|
+
try {
|
|
87
|
+
healable = db.prepare(`SELECT 1 FROM notes WHERE ${predicate} LIMIT 1`).get();
|
|
88
|
+
} catch (error) {
|
|
89
|
+
if (!(error instanceof Error) || !error.message.includes("no such table"))
|
|
90
|
+
throw error;
|
|
91
|
+
predicate = PENDING_SESSION_NOTE_PREDICATE;
|
|
92
|
+
healable = db.prepare(`SELECT 1 FROM notes WHERE ${predicate} LIMIT 1`).get();
|
|
93
|
+
}
|
|
94
|
+
if (healable == null)
|
|
95
|
+
return;
|
|
96
|
+
try {
|
|
97
|
+
db.prepare(`UPDATE notes SET status = 'active', surface_condition = NULL WHERE ${predicate}`).run();
|
|
98
|
+
} catch (error) {
|
|
99
|
+
if (!isTransientSqliteError2(error))
|
|
100
|
+
throw error;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
var SESSION_NOTE_CONDITION_ERROR = "Only a note created with a condition can have one. Write a new note with surface_condition, and dismiss this one.";
|
|
104
|
+
function getNoteById(db, noteId) {
|
|
105
|
+
healPendingSessionNotes(db);
|
|
106
|
+
const row = db.prepare("SELECT * FROM notes WHERE id = ?").get(noteId);
|
|
107
|
+
return isNoteRow(row) ? toNote(row) : null;
|
|
108
|
+
}
|
|
109
|
+
function getNoteByIdInScope(db, noteId, scope) {
|
|
110
|
+
const note = getNoteById(db, noteId);
|
|
111
|
+
return note && noteBelongsToScope(note, scope) ? note : null;
|
|
112
|
+
}
|
|
113
|
+
function noteBelongsToScope(note, scope) {
|
|
114
|
+
if (note.type === "session") {
|
|
115
|
+
return note.sessionId === scope.sessionId;
|
|
116
|
+
}
|
|
117
|
+
return note.projectPath === scope.projectPath;
|
|
118
|
+
}
|
|
119
|
+
function buildStatusClause(status) {
|
|
120
|
+
if (status === undefined) {
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
const statuses = Array.isArray(status) ? status : [status];
|
|
124
|
+
if (statuses.length === 0) {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
const placeholders = statuses.map(() => "?").join(", ");
|
|
128
|
+
return {
|
|
129
|
+
sql: `status IN (${placeholders})`,
|
|
130
|
+
params: statuses
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
function getNotes(db, options = {}) {
|
|
134
|
+
healPendingSessionNotes(db);
|
|
135
|
+
const clauses = [];
|
|
136
|
+
const params = [];
|
|
137
|
+
if (options.sessionId !== undefined) {
|
|
138
|
+
clauses.push("session_id = ?");
|
|
139
|
+
params.push(options.sessionId);
|
|
140
|
+
}
|
|
141
|
+
if (options.projectPath !== undefined) {
|
|
142
|
+
clauses.push("project_path = ?");
|
|
143
|
+
params.push(options.projectPath);
|
|
144
|
+
}
|
|
145
|
+
if (options.type !== undefined) {
|
|
146
|
+
clauses.push("type = ?");
|
|
147
|
+
params.push(options.type);
|
|
148
|
+
}
|
|
149
|
+
const statusClause = buildStatusClause(options.status);
|
|
150
|
+
if (statusClause) {
|
|
151
|
+
clauses.push(statusClause.sql);
|
|
152
|
+
params.push(...statusClause.params);
|
|
153
|
+
}
|
|
154
|
+
const where = clauses.length > 0 ? ` WHERE ${clauses.join(" AND ")}` : "";
|
|
155
|
+
return db.prepare(`SELECT * FROM notes${where} ORDER BY created_at ASC, id ASC`).all(...params).filter(isNoteRow).map(toNote);
|
|
156
|
+
}
|
|
157
|
+
function addNote(db, type, options) {
|
|
158
|
+
const now = Date.now();
|
|
159
|
+
const result = type === "session" ? db.prepare("INSERT INTO notes (type, status, content, session_id, created_at, updated_at, harness, anchor_ordinal) VALUES ('session', 'active', ?, ?, ?, ?, ?, ?) RETURNING *").get(options.content, options.sessionId, now, now, getHarness2(), options.anchorOrdinal ?? null) : db.prepare("INSERT INTO notes (type, status, content, session_id, project_path, surface_condition, compiled_provider, compiled_config, compiled_at, compile_status, created_at, updated_at, harness, anchor_ordinal) VALUES ('smart', 'pending', ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING *").get(options.content, options.sessionId ?? null, options.projectPath, options.surfaceCondition, options.compiledProvider ?? null, options.compiledConfig ?? null, options.compiledAt ?? null, options.compileStatus ?? null, now, now, getHarness2(), options.anchorOrdinal ?? null);
|
|
160
|
+
if (!isNoteRow(result)) {
|
|
161
|
+
throw new Error("[notes] failed to insert note");
|
|
162
|
+
}
|
|
163
|
+
return toNote(result);
|
|
164
|
+
}
|
|
165
|
+
function getSessionNotes(db, sessionId) {
|
|
166
|
+
return getNotes(db, { sessionId, type: "session", status: "active" });
|
|
167
|
+
}
|
|
168
|
+
function getSmartNotes(db, projectPath, status) {
|
|
169
|
+
return getNotes(db, {
|
|
170
|
+
projectPath,
|
|
171
|
+
type: "smart",
|
|
172
|
+
status: status ?? DEFAULT_SMART_STATUSES
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
function getPendingSmartNotes(db, projectPath) {
|
|
176
|
+
return getSmartNotes(db, projectPath, "pending");
|
|
177
|
+
}
|
|
178
|
+
function getReadySmartNotes(db, projectPath) {
|
|
179
|
+
return getSmartNotes(db, projectPath, "ready");
|
|
180
|
+
}
|
|
181
|
+
function updateNote(db, noteId, updates, scope) {
|
|
182
|
+
const existing = getNoteByIdInScope(db, noteId, scope);
|
|
183
|
+
if (!existing) {
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
if (updates.surfaceCondition !== undefined && existing.type !== "smart") {
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
const now = Date.now();
|
|
190
|
+
const sets = ["updated_at = ?"];
|
|
191
|
+
const params = [now];
|
|
192
|
+
if (updates.content !== undefined) {
|
|
193
|
+
sets.push("content = ?");
|
|
194
|
+
params.push(updates.content);
|
|
195
|
+
}
|
|
196
|
+
if (updates.sessionId !== undefined) {
|
|
197
|
+
sets.push("session_id = ?");
|
|
198
|
+
params.push(updates.sessionId);
|
|
199
|
+
}
|
|
200
|
+
const smartConditionChanged = existing.type === "smart" && updates.surfaceCondition !== undefined && updates.surfaceCondition !== existing.surfaceCondition;
|
|
201
|
+
if (updates.status !== undefined && !smartConditionChanged) {
|
|
202
|
+
sets.push("status = ?");
|
|
203
|
+
params.push(updates.status);
|
|
204
|
+
}
|
|
205
|
+
if (existing.type === "smart") {
|
|
206
|
+
if (updates.projectPath !== undefined) {
|
|
207
|
+
sets.push("project_path = ?");
|
|
208
|
+
params.push(updates.projectPath);
|
|
209
|
+
}
|
|
210
|
+
if (updates.surfaceCondition !== undefined) {
|
|
211
|
+
sets.push("surface_condition = ?");
|
|
212
|
+
params.push(updates.surfaceCondition);
|
|
213
|
+
}
|
|
214
|
+
if (smartConditionChanged) {
|
|
215
|
+
sets.push("status = 'pending'", "last_checked_at = NULL", "ready_at = NULL", "ready_reason = NULL");
|
|
216
|
+
sets.push("compiled_provider = ?", "compiled_config = ?", "compiled_at = ?", "compile_status = ?");
|
|
217
|
+
params.push(updates.compiledProvider ?? null, updates.compiledConfig ?? null, updates.compiledAt ?? null, updates.compileStatus ?? null);
|
|
218
|
+
if (noteCheckColumnsExist(db)) {
|
|
219
|
+
sets.push("compiled_check = NULL", "manifest_json = NULL", "check_hash = NULL", "check_cron = NULL", "check_version = 0", "check_status = 'uncompiled'", "check_failure_count = 0", "check_network_failure_count = 0", "check_quarantined_until = NULL", "check_next_due_at = NULL", "check_compiled_at = NULL", "check_false_since_at = NULL", "check_last_liveness_at = NULL");
|
|
220
|
+
}
|
|
221
|
+
} else {
|
|
222
|
+
if (updates.lastCheckedAt !== undefined) {
|
|
223
|
+
sets.push("last_checked_at = ?");
|
|
224
|
+
params.push(updates.lastCheckedAt);
|
|
225
|
+
}
|
|
226
|
+
if (updates.readyAt !== undefined) {
|
|
227
|
+
sets.push("ready_at = ?");
|
|
228
|
+
params.push(updates.readyAt);
|
|
229
|
+
}
|
|
230
|
+
if (updates.readyReason !== undefined) {
|
|
231
|
+
sets.push("ready_reason = ?");
|
|
232
|
+
params.push(updates.readyReason);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
if (sets.length === 1) {
|
|
237
|
+
return null;
|
|
238
|
+
}
|
|
239
|
+
params.push(noteId);
|
|
240
|
+
const result = db.prepare(`UPDATE notes SET ${sets.join(", ")} WHERE id = ? RETURNING *`).get(...params);
|
|
241
|
+
return isNoteRow(result) ? toNote(result) : null;
|
|
242
|
+
}
|
|
243
|
+
function dismissNotes(db, noteIds, scope) {
|
|
244
|
+
const now = Date.now();
|
|
245
|
+
return db.transaction(() => noteIds.map((noteId) => {
|
|
246
|
+
const existing = getNoteById(db, noteId);
|
|
247
|
+
if (!existing)
|
|
248
|
+
return { noteId, outcome: "not_found" };
|
|
249
|
+
if (!noteBelongsToScope(existing, scope)) {
|
|
250
|
+
return { noteId, outcome: "not_owned" };
|
|
251
|
+
}
|
|
252
|
+
if (existing.status === "dismissed") {
|
|
253
|
+
return { noteId, outcome: "already_dismissed" };
|
|
254
|
+
}
|
|
255
|
+
const result = db.prepare("UPDATE notes SET status = 'dismissed', updated_at = ? WHERE id = ? AND status != 'dismissed'").run(now, noteId);
|
|
256
|
+
return {
|
|
257
|
+
noteId,
|
|
258
|
+
outcome: result.changes > 0 ? "dismissed" : "already_dismissed"
|
|
259
|
+
};
|
|
260
|
+
})).immediate();
|
|
261
|
+
}
|
|
262
|
+
function dismissNote(db, noteId, scope) {
|
|
263
|
+
return dismissNotes(db, [noteId], scope)[0]?.outcome === "dismissed";
|
|
264
|
+
}
|
|
265
|
+
function markNoteReady(db, noteId, reason) {
|
|
266
|
+
const now = Date.now();
|
|
267
|
+
db.prepare("UPDATE notes SET status = 'ready', ready_at = ?, ready_reason = ?, updated_at = ?, last_checked_at = ? WHERE id = ? AND type = 'smart'").run(now, reason ?? null, now, now, noteId);
|
|
268
|
+
}
|
|
269
|
+
function markNoteChecked(db, noteId) {
|
|
270
|
+
const now = Date.now();
|
|
271
|
+
db.prepare("UPDATE notes SET last_checked_at = ?, updated_at = ? WHERE id = ? AND type = 'smart'").run(now, now, noteId);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// src/features/magic-context/storage-primers.ts
|
|
275
|
+
var PRIMER_CANDIDATE_TTL_MS = 90 * 24 * 60 * 60 * 1000;
|
|
276
|
+
var PRIMER_CANDIDATE_MAX_AGE_MS = 180 * 24 * 60 * 60 * 1000;
|
|
277
|
+
function normalizePrimerQuestion(question) {
|
|
278
|
+
return question.trim().toLowerCase().replace(/[“”]/g, '"').replace(/[’]/g, "'").replace(/\s+/g, " ").replace(/[?.!]+$/g, "").trim();
|
|
279
|
+
}
|
|
280
|
+
function primerOccurrenceKey(candidate) {
|
|
281
|
+
return [
|
|
282
|
+
candidate.projectPath,
|
|
283
|
+
candidate.harness,
|
|
284
|
+
candidate.sessionId,
|
|
285
|
+
candidate.sourceStartMessageId,
|
|
286
|
+
candidate.sourceEndMessageId
|
|
287
|
+
].join("\x1F");
|
|
288
|
+
}
|
|
289
|
+
function primerOccurrenceUtcDay(sourceMessageTime) {
|
|
290
|
+
return new Date(sourceMessageTime).toISOString().slice(0, 10);
|
|
291
|
+
}
|
|
292
|
+
function vectorBlob(vector) {
|
|
293
|
+
if (!vector)
|
|
294
|
+
return null;
|
|
295
|
+
return new Uint8Array(vector.buffer, vector.byteOffset, vector.byteLength);
|
|
296
|
+
}
|
|
297
|
+
function blobToFloat32Array(value) {
|
|
298
|
+
if (!value)
|
|
299
|
+
return null;
|
|
300
|
+
const bytes = value instanceof ArrayBuffer ? new Uint8Array(value) : value;
|
|
301
|
+
return new Float32Array(bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength));
|
|
302
|
+
}
|
|
303
|
+
function parseCandidateIds(raw) {
|
|
304
|
+
if (!raw)
|
|
305
|
+
return [];
|
|
306
|
+
try {
|
|
307
|
+
const parsed = JSON.parse(raw);
|
|
308
|
+
return Array.isArray(parsed) ? parsed.filter((id) => typeof id === "number" && Number.isFinite(id)) : [];
|
|
309
|
+
} catch {
|
|
310
|
+
return [];
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
function parsePrimerSourceProvenance(raw) {
|
|
314
|
+
if (raw === null)
|
|
315
|
+
return null;
|
|
316
|
+
try {
|
|
317
|
+
const parsed = JSON.parse(raw);
|
|
318
|
+
if (!Array.isArray(parsed))
|
|
319
|
+
return null;
|
|
320
|
+
const provenance = [];
|
|
321
|
+
for (const value of parsed) {
|
|
322
|
+
if (!value || typeof value !== "object")
|
|
323
|
+
return null;
|
|
324
|
+
const source = value;
|
|
325
|
+
if (typeof source.candidate_id !== "number" || !Number.isFinite(source.candidate_id) || typeof source.project_path !== "string" || typeof source.harness !== "string" || typeof source.session_id !== "string" || source.source_compartment_start !== null && typeof source.source_compartment_start !== "number" || source.source_compartment_end !== null && typeof source.source_compartment_end !== "number" || typeof source.source_start_message_id !== "string" || typeof source.source_end_message_id !== "string") {
|
|
326
|
+
return null;
|
|
327
|
+
}
|
|
328
|
+
provenance.push({
|
|
329
|
+
candidateId: source.candidate_id,
|
|
330
|
+
projectPath: source.project_path,
|
|
331
|
+
harness: source.harness,
|
|
332
|
+
sessionId: source.session_id,
|
|
333
|
+
sourceCompartmentStart: source.source_compartment_start,
|
|
334
|
+
sourceCompartmentEnd: source.source_compartment_end,
|
|
335
|
+
sourceStartMessageId: source.source_start_message_id,
|
|
336
|
+
sourceEndMessageId: source.source_end_message_id
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
return provenance;
|
|
340
|
+
} catch {
|
|
341
|
+
return null;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
function loadPrimerSourceProvenance(db, candidateIds) {
|
|
345
|
+
const ids = [...new Set(candidateIds)].sort((a, b) => a - b);
|
|
346
|
+
if (ids.length === 0)
|
|
347
|
+
return [];
|
|
348
|
+
const placeholders = ids.map(() => "?").join(",");
|
|
349
|
+
const rows = db.prepare(`SELECT id, project_path, harness, session_id,
|
|
350
|
+
source_compartment_start, source_compartment_end,
|
|
351
|
+
source_start_message_id, source_end_message_id
|
|
352
|
+
FROM primer_candidates
|
|
353
|
+
WHERE id IN (${placeholders})
|
|
354
|
+
ORDER BY id ASC`).all(...ids);
|
|
355
|
+
return rows.map((row) => ({
|
|
356
|
+
candidateId: row.id,
|
|
357
|
+
projectPath: row.project_path,
|
|
358
|
+
harness: row.harness,
|
|
359
|
+
sessionId: row.session_id,
|
|
360
|
+
sourceCompartmentStart: row.source_compartment_start,
|
|
361
|
+
sourceCompartmentEnd: row.source_compartment_end,
|
|
362
|
+
sourceStartMessageId: row.source_start_message_id,
|
|
363
|
+
sourceEndMessageId: row.source_end_message_id
|
|
364
|
+
}));
|
|
365
|
+
}
|
|
366
|
+
function serializePrimerSourceProvenance(provenance, sourceCandidateIds) {
|
|
367
|
+
if (provenance.length === 0 && sourceCandidateIds.length > 0)
|
|
368
|
+
return null;
|
|
369
|
+
return JSON.stringify(provenance.map((source) => ({
|
|
370
|
+
candidate_id: source.candidateId,
|
|
371
|
+
project_path: source.projectPath,
|
|
372
|
+
harness: source.harness,
|
|
373
|
+
session_id: source.sessionId,
|
|
374
|
+
source_compartment_start: source.sourceCompartmentStart,
|
|
375
|
+
source_compartment_end: source.sourceCompartmentEnd,
|
|
376
|
+
source_start_message_id: source.sourceStartMessageId,
|
|
377
|
+
source_end_message_id: source.sourceEndMessageId
|
|
378
|
+
})));
|
|
379
|
+
}
|
|
380
|
+
function normalizedCandidateIds(candidateIds) {
|
|
381
|
+
return [...new Set(candidateIds)].sort((a, b) => a - b);
|
|
382
|
+
}
|
|
383
|
+
function toCandidate(row) {
|
|
384
|
+
return {
|
|
385
|
+
id: row.id,
|
|
386
|
+
projectPath: row.project_path,
|
|
387
|
+
harness: row.harness,
|
|
388
|
+
sessionId: row.session_id,
|
|
389
|
+
question: row.question,
|
|
390
|
+
normalizedQuestion: row.normalized_question,
|
|
391
|
+
sourceCompartmentStart: row.source_compartment_start,
|
|
392
|
+
sourceCompartmentEnd: row.source_compartment_end,
|
|
393
|
+
sourceStartMessageId: row.source_start_message_id,
|
|
394
|
+
sourceEndMessageId: row.source_end_message_id,
|
|
395
|
+
sourceMessageTime: row.source_message_time,
|
|
396
|
+
questionEmbedding: blobToFloat32Array(row.question_embedding),
|
|
397
|
+
questionEmbeddingModelId: row.question_embedding_model_id,
|
|
398
|
+
createdAt: row.created_at
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
function toPrimer(row) {
|
|
402
|
+
const status = row.status === "archived" ? "archived" : "active";
|
|
403
|
+
return {
|
|
404
|
+
id: row.id,
|
|
405
|
+
projectPath: row.project_path,
|
|
406
|
+
question: row.question,
|
|
407
|
+
questionEmbedding: blobToFloat32Array(row.question_embedding),
|
|
408
|
+
questionEmbeddingModelId: row.question_embedding_model_id,
|
|
409
|
+
answer: row.answer,
|
|
410
|
+
status,
|
|
411
|
+
totalSupport: row.total_support,
|
|
412
|
+
lastObservedAt: row.last_observed_at,
|
|
413
|
+
answerRefreshedAt: row.answer_refreshed_at,
|
|
414
|
+
sourceCandidateIds: parseCandidateIds(row.source_candidate_ids),
|
|
415
|
+
sourceProvenance: parsePrimerSourceProvenance(row.source_candidate_provenance),
|
|
416
|
+
createdAt: row.created_at,
|
|
417
|
+
updatedAt: row.updated_at
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
function insertPrimerCandidates(db, candidates) {
|
|
421
|
+
const ids = [];
|
|
422
|
+
const stmt = db.prepare(`
|
|
423
|
+
INSERT INTO primer_candidates (
|
|
424
|
+
project_path, harness, session_id, question, normalized_question,
|
|
425
|
+
source_compartment_start, source_compartment_end,
|
|
426
|
+
source_start_message_id, source_end_message_id, source_message_time,
|
|
427
|
+
question_embedding, question_embedding_model_id, created_at
|
|
428
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
429
|
+
ON CONFLICT(project_path, harness, session_id, source_start_message_id, source_end_message_id)
|
|
430
|
+
DO UPDATE SET
|
|
431
|
+
question = excluded.question,
|
|
432
|
+
normalized_question = excluded.normalized_question,
|
|
433
|
+
source_compartment_start = excluded.source_compartment_start,
|
|
434
|
+
source_compartment_end = excluded.source_compartment_end,
|
|
435
|
+
source_message_time = excluded.source_message_time,
|
|
436
|
+
question_embedding = COALESCE(excluded.question_embedding, primer_candidates.question_embedding),
|
|
437
|
+
question_embedding_model_id = COALESCE(excluded.question_embedding_model_id, primer_candidates.question_embedding_model_id),
|
|
438
|
+
created_at = MIN(primer_candidates.created_at, excluded.created_at)
|
|
439
|
+
`);
|
|
440
|
+
const select = db.prepare(`
|
|
441
|
+
SELECT id FROM primer_candidates
|
|
442
|
+
WHERE project_path = ? AND harness = ? AND session_id = ?
|
|
443
|
+
AND source_start_message_id = ? AND source_end_message_id = ?
|
|
444
|
+
`);
|
|
445
|
+
db.transaction(() => {
|
|
446
|
+
for (const candidate of candidates) {
|
|
447
|
+
const question = candidate.question.trim();
|
|
448
|
+
if (!question)
|
|
449
|
+
continue;
|
|
450
|
+
const normalized = candidate.normalizedQuestion ?? normalizePrimerQuestion(question);
|
|
451
|
+
stmt.run(candidate.projectPath, candidate.harness || "opencode", candidate.sessionId, question, normalized, candidate.sourceCompartmentStart ?? null, candidate.sourceCompartmentEnd ?? null, candidate.sourceStartMessageId, candidate.sourceEndMessageId, candidate.sourceMessageTime, vectorBlob(candidate.questionEmbedding), candidate.questionEmbeddingModelId ?? null, candidate.createdAt ?? Date.now());
|
|
452
|
+
const row = select.get(candidate.projectPath, candidate.harness || "opencode", candidate.sessionId, candidate.sourceStartMessageId, candidate.sourceEndMessageId);
|
|
453
|
+
if (typeof row?.id === "number")
|
|
454
|
+
ids.push(row.id);
|
|
455
|
+
}
|
|
456
|
+
}).immediate();
|
|
457
|
+
return ids;
|
|
458
|
+
}
|
|
459
|
+
function updatePrimerCandidateEmbedding(db, candidateId, vector, modelId) {
|
|
460
|
+
db.prepare("UPDATE primer_candidates SET question_embedding = ?, question_embedding_model_id = ? WHERE id = ?").run(vectorBlob(vector), modelId, candidateId);
|
|
461
|
+
}
|
|
462
|
+
function getPrimerCandidatesByIds(db, ids) {
|
|
463
|
+
if (ids.length === 0)
|
|
464
|
+
return [];
|
|
465
|
+
const placeholders = ids.map(() => "?").join(",");
|
|
466
|
+
const rows = db.prepare(`SELECT * FROM primer_candidates WHERE id IN (${placeholders})`).all(...ids);
|
|
467
|
+
return rows.map(toCandidate);
|
|
468
|
+
}
|
|
469
|
+
function getPrimerCandidatesForPromotion(db, projectPath, now = Date.now(), ttlMs = PRIMER_CANDIDATE_TTL_MS) {
|
|
470
|
+
const cutoff = now - ttlMs;
|
|
471
|
+
const rows = db.prepare(`SELECT * FROM primer_candidates
|
|
472
|
+
WHERE project_path = ? AND source_message_time >= ?
|
|
473
|
+
ORDER BY project_path ASC, harness ASC, session_id ASC, source_start_message_id ASC, source_end_message_id ASC, id ASC`).all(projectPath, cutoff);
|
|
474
|
+
return rows.map(toCandidate);
|
|
475
|
+
}
|
|
476
|
+
function countPrimerCandidatesForProject(db, projectPath) {
|
|
477
|
+
const row = db.prepare("SELECT COUNT(*) AS count FROM primer_candidates WHERE project_path = ?").get(projectPath);
|
|
478
|
+
return row?.count ?? 0;
|
|
479
|
+
}
|
|
480
|
+
function getActivePrimers(db, projectPath) {
|
|
481
|
+
const rows = db.prepare(`SELECT * FROM primers
|
|
482
|
+
WHERE project_path = ? AND status = 'active'
|
|
483
|
+
ORDER BY COALESCE(last_observed_at, created_at) DESC, id ASC`).all(projectPath);
|
|
484
|
+
return rows.map(toPrimer);
|
|
485
|
+
}
|
|
486
|
+
function createPrimer(db, input) {
|
|
487
|
+
return db.transaction(() => {
|
|
488
|
+
const now = input.now ?? Date.now();
|
|
489
|
+
const sourceCandidateIds = normalizedCandidateIds(input.sourceCandidateIds);
|
|
490
|
+
const sourceProvenance = loadPrimerSourceProvenance(db, sourceCandidateIds);
|
|
491
|
+
const info = db.prepare(`INSERT INTO primers (
|
|
492
|
+
project_path, question, question_embedding, question_embedding_model_id, answer,
|
|
493
|
+
status, total_support, last_observed_at, answer_refreshed_at,
|
|
494
|
+
source_candidate_ids, source_candidate_provenance, created_at, updated_at
|
|
495
|
+
) VALUES (?, ?, ?, ?, ?, 'active', ?, ?, NULL, ?, ?, ?, ?)`).run(input.projectPath, input.question, vectorBlob(input.questionEmbedding), input.questionEmbeddingModelId ?? null, input.answer ?? "", input.totalSupport, input.lastObservedAt, JSON.stringify(sourceCandidateIds), serializePrimerSourceProvenance(sourceProvenance, sourceCandidateIds), now, now);
|
|
496
|
+
return Number(info.lastInsertRowid);
|
|
497
|
+
}).immediate();
|
|
498
|
+
}
|
|
499
|
+
function updatePrimerSupport(db, input) {
|
|
500
|
+
db.transaction(() => {
|
|
501
|
+
const sourceCandidateIds = normalizedCandidateIds(input.sourceCandidateIds);
|
|
502
|
+
const allowedIds = new Set(sourceCandidateIds);
|
|
503
|
+
const existingRow = db.prepare("SELECT source_candidate_provenance FROM primers WHERE id = ?").get(input.primerId);
|
|
504
|
+
const provenanceByCandidateId = new Map;
|
|
505
|
+
for (const source of parsePrimerSourceProvenance(existingRow?.source_candidate_provenance ?? null) ?? []) {
|
|
506
|
+
if (allowedIds.has(source.candidateId)) {
|
|
507
|
+
provenanceByCandidateId.set(source.candidateId, source);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
for (const source of loadPrimerSourceProvenance(db, sourceCandidateIds)) {
|
|
511
|
+
provenanceByCandidateId.set(source.candidateId, source);
|
|
512
|
+
}
|
|
513
|
+
const sourceProvenance = [...provenanceByCandidateId.values()].sort((a, b) => a.candidateId - b.candidateId);
|
|
514
|
+
db.prepare(`UPDATE primers
|
|
515
|
+
SET question_embedding = COALESCE(?, question_embedding),
|
|
516
|
+
question_embedding_model_id = COALESCE(?, question_embedding_model_id),
|
|
517
|
+
total_support = ?,
|
|
518
|
+
last_observed_at = ?,
|
|
519
|
+
source_candidate_ids = ?,
|
|
520
|
+
source_candidate_provenance = ?,
|
|
521
|
+
updated_at = ?
|
|
522
|
+
WHERE id = ?`).run(vectorBlob(input.questionEmbedding), input.questionEmbeddingModelId ?? null, input.totalSupport, input.lastObservedAt, JSON.stringify(sourceCandidateIds), serializePrimerSourceProvenance(sourceProvenance, sourceCandidateIds), input.now ?? Date.now(), input.primerId);
|
|
523
|
+
}).immediate();
|
|
524
|
+
}
|
|
525
|
+
function updatePrimerAnswer(db, primerId, answer, refreshedAt = Date.now()) {
|
|
526
|
+
db.prepare("UPDATE primers SET answer = ?, answer_refreshed_at = ?, updated_at = ? WHERE id = ?").run(answer, refreshedAt, refreshedAt, primerId);
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
export { SESSION_NOTE_CONDITION_ERROR, getNoteByIdInScope, getNotes, addNote, getSessionNotes, getPendingSmartNotes, getReadySmartNotes, updateNote, dismissNotes, dismissNote, markNoteReady, markNoteChecked, PRIMER_CANDIDATE_TTL_MS, PRIMER_CANDIDATE_MAX_AGE_MS, primerOccurrenceKey, primerOccurrenceUtcDay, insertPrimerCandidates, updatePrimerCandidateEmbedding, getPrimerCandidatesByIds, getPrimerCandidatesForPromotion, countPrimerCandidatesForProject, getActivePrimers, createPrimer, updatePrimerSupport, updatePrimerAnswer };
|