@cortexkit/opencode-magic-context 0.49.35 → 0.49.36
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 +1 -1
- package/dist/auto-search-worker.js +163 -160
- package/dist/chunk-embedding-write-worker.js +58 -56
- package/dist/{compaction-marker-candidates-g0p67ced.js → compaction-marker-candidates-5p1qkve9.js} +3 -1
- package/dist/compaction-marker-candidates-worker.js +24 -23
- package/dist/{compartment-chunk-embedding-y4136are.js → compartment-chunk-embedding-ypktm0tk.js} +5 -4
- package/dist/{conflict-warning-hook-hhw8z5ak.js → conflict-warning-hook-b75jn458.js} +6 -5
- package/dist/{context-fk8e3h9p.js → context-969z683s.js} +485 -91
- package/dist/{embedding-backfill-q6n72sy0.js → embedding-backfill-rma0v2ct.js} +9 -8
- package/dist/embedding-worker.js +2 -2
- package/dist/features/magic-context/compaction-marker-candidates.d.ts.map +1 -1
- package/dist/features/magic-context/compaction-marker.d.ts +5 -3
- package/dist/features/magic-context/compaction-marker.d.ts.map +1 -1
- package/dist/features/magic-context/dreamer/task-gates.d.ts.map +1 -1
- package/dist/features/magic-context/dreamer/verify-gate.d.ts +2 -0
- package/dist/features/magic-context/dreamer/verify-gate.d.ts.map +1 -1
- package/dist/features/magic-context/dreamer/verify-state.d.ts +9 -0
- package/dist/features/magic-context/dreamer/verify-state.d.ts.map +1 -0
- package/dist/features/magic-context/dreamer/verify.d.ts +2 -1
- package/dist/features/magic-context/dreamer/verify.d.ts.map +1 -1
- package/dist/features/magic-context/migration-worker-client.d.ts +3 -0
- package/dist/features/magic-context/migration-worker-client.d.ts.map +1 -1
- package/dist/features/magic-context/migration-worker.d.ts +15 -0
- package/dist/features/magic-context/migration-worker.d.ts.map +1 -1
- package/dist/features/magic-context/overflow-detection.d.ts +2 -2
- package/dist/features/magic-context/overflow-detection.d.ts.map +1 -1
- package/dist/features/magic-context/search-rerank.d.ts.map +1 -1
- package/dist/features/magic-context/storage-meta-persisted.d.ts +1 -0
- package/dist/features/magic-context/storage-meta-persisted.d.ts.map +1 -1
- package/dist/features/magic-context/storage-tags.d.ts +4 -0
- package/dist/features/magic-context/storage-tags.d.ts.map +1 -1
- package/dist/features/magic-context/work-metrics-carry-storage.d.ts +10 -0
- package/dist/features/magic-context/work-metrics-carry-storage.d.ts.map +1 -0
- 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/compaction-marker-publication.d.ts.map +1 -1
- package/dist/hooks/magic-context/compartment-runner-historian.d.ts.map +1 -1
- package/dist/hooks/magic-context/issue-423-fixture.d.ts +1 -1
- package/dist/hooks/magic-context/issue-423-fixture.d.ts.map +1 -1
- package/dist/hooks/magic-context/issue-424-fixture.d.ts +1 -1
- package/dist/hooks/magic-context/issue-424-fixture.d.ts.map +1 -1
- package/dist/hooks/magic-context/project-structural-sentinels.d.ts +25 -0
- package/dist/hooks/magic-context/project-structural-sentinels.d.ts.map +1 -0
- package/dist/hooks/magic-context/protected-tail-boundary.d.ts +8 -1
- package/dist/hooks/magic-context/protected-tail-boundary.d.ts.map +1 -1
- package/dist/hooks/magic-context/raw-ordinal-warmup.d.ts.map +1 -1
- package/dist/hooks/magic-context/read-session-true-raw-tokens.d.ts +2 -0
- package/dist/hooks/magic-context/read-session-true-raw-tokens.d.ts.map +1 -1
- package/dist/hooks/magic-context/release-recovery-prefix.d.ts +9 -0
- package/dist/hooks/magic-context/release-recovery-prefix.d.ts.map +1 -0
- package/dist/hooks/magic-context/strip-structural-noise.d.ts +6 -3
- package/dist/hooks/magic-context/strip-structural-noise.d.ts.map +1 -1
- package/dist/hooks/magic-context/transform-context-state.d.ts +3 -0
- package/dist/hooks/magic-context/transform-context-state.d.ts.map +1 -1
- package/dist/hooks/magic-context/transform-postprocess-phase.d.ts +3 -0
- 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-cpjk5jd2.js → index-1d042x6w.js} +16 -14
- package/dist/{index-jn0ddrhp.js → index-21fgrfrf.js} +8 -6
- package/dist/{index-pq0dpwrc.js → index-2rs7912w.js} +2 -2
- package/dist/{index-ezv6dz3y.js → index-4ks89h5a.js} +4 -4
- package/dist/{index-vzhhr1ht.js → index-4y35msh5.js} +1 -2
- package/dist/{index-f297z1n8.js → index-72czy45z.js} +5 -5
- package/dist/index-7v05d9b7.js +58 -0
- package/dist/{index-f9b3vtt1.js → index-850vthn3.js} +8 -2
- package/dist/{index-ksv0a31x.js → index-8jjjmhab.js} +12 -1
- package/dist/index-96ztjvkn.js +204 -0
- package/dist/{index-gyaa159j.js → index-9fgmkfaf.js} +6 -3
- package/dist/{index-4bgk5ztz.js → index-9tdvbdv1.js} +187 -90
- package/dist/{index-2v6nvy3k.js → index-ae1ar4v1.js} +1 -1
- package/dist/{index-faxb7mck.js → index-b1ydbp95.js} +1 -1
- package/dist/{index-nxrsns5x.js → index-c5dzns43.js} +75 -75
- package/dist/{index-zhqwda72.js → index-dcjkw41m.js} +1 -1
- package/dist/{index-x2mrq5xv.js → index-dfdg2cgg.js} +179 -49
- package/dist/{index-87ctnzz9.js → index-dz48h1wx.js} +2 -2
- package/dist/{index-b6xz3qbe.js → index-gnmhbyyc.js} +366 -83
- package/dist/{index-qx8j6kds.js → index-j105mf16.js} +2 -2
- package/dist/{index-p56dszfp.js → index-jggt5gf2.js} +1 -1
- package/dist/{index-xwwsbsd3.js → index-pp8vdn01.js} +5 -5
- package/dist/{index-dy7w6wm4.js → index-prw8apnc.js} +5 -5
- package/dist/{index-qncw9hey.js → index-r2epb000.js} +33 -3
- package/dist/{index-brg2w6qc.js → index-tn9drm1a.js} +16 -14
- package/dist/{index-pswsyf1w.js → index-ty4vhe4n.js} +3 -3
- package/dist/{index-pztqab6k.js → index-vd2ywmfe.js} +1 -1
- package/dist/{index-3j9t1v4n.js → index-wcwhhtdh.js} +10 -9
- package/dist/{index-1rhgxqs8.js → index-xd2p95m7.js} +1 -1
- package/dist/{index-0c7p8mq5.js → index-xnm0wq1r.js} +1 -1
- package/dist/{index-3d3872c6.js → index-ys7j6679.js} +4 -4
- package/dist/index.js +38 -37
- package/dist/{message-fts-rowid-map-xg7hr0mk.js → message-fts-rowid-map-t6qsyg9s.js} +4 -3
- package/dist/{message-index-bounded-narw7bnz.js → message-index-bounded-wfk6aqxk.js} +9 -8
- package/dist/migration-worker.js +45 -40
- package/dist/plugin/rpc-handlers.d.ts.map +1 -1
- package/dist/{project-embedding-registry-tdz7985n.js → project-embedding-registry-3yyegjma.js} +8 -7
- package/dist/raw-ordinal-warmup-worker.js +114 -118
- package/dist/{read-session-chunk-vmg1zjg0.js → read-session-chunk-26yrrhyz.js} +7 -6
- package/dist/{read-session-raw-mncmzbm9.js → read-session-raw-j7c9w57v.js} +3 -2
- package/dist/{recomp-orchestrator-qyyhnc9b.js → recomp-orchestrator-wk509qkp.js} +20 -19
- package/dist/{render-c5p0k846.js → render-h1qarr8a.js} +13 -12
- package/dist/{search-f6hwvcxk.js → search-5h9fydyq.js} +16 -15
- package/dist/{search-archive-0ny7p3bw.js → search-archive-q79hyqxs.js} +9 -8
- package/dist/{send-session-notification-0gwcz3fw.js → send-session-notification-14p3yje4.js} +5 -4
- package/dist/shared/model-suggestion-retry.d.ts +7 -0
- package/dist/shared/model-suggestion-retry.d.ts.map +1 -1
- package/dist/shared/prompt-async-transport.d.ts.map +1 -1
- package/dist/shared/reusable-worker-entry.d.ts +4 -0
- package/dist/shared/reusable-worker-entry.d.ts.map +1 -0
- package/dist/shared/reusable-worker.d.ts +6 -0
- package/dist/shared/reusable-worker.d.ts.map +1 -0
- package/dist/shared/reusable-worker.fixture.d.ts +2 -0
- package/dist/shared/reusable-worker.fixture.d.ts.map +1 -0
- package/dist/shared/worker-shutdown.d.ts +1 -1
- package/dist/shared/worker-shutdown.d.ts.map +1 -1
- package/dist/{storage-tags-zx1023km.js → storage-tags-vj0yj7ed.js} +1 -1
- package/dist/{store-620rjzt9.js → store-s3eykdjt.js} +1 -1
- package/dist/{transform-postprocess-phase-807ff6t2.js → transform-postprocess-phase-4gj0mtcg.js} +21 -20
- package/dist/v2/auto-search-worker.js +163 -160
- package/dist/v2/chunk-embedding-write-worker.js +59 -57
- package/dist/v2/compaction-marker-candidates-worker.js +24 -23
- package/dist/v2/{compaction-marker-candidates-t1aq6gsj.js → compaction-marker-candidates-yeb2ky46.js} +4 -2
- package/dist/v2/{compartment-chunk-embedding-ncsk0r9e.js → compartment-chunk-embedding-qfk791bd.js} +6 -5
- package/dist/v2/{context-znqfkap4.js → context-bxr039be.js} +1011 -209
- package/dist/v2/{embedding-backfill-11j425e0.js → embedding-backfill-00tw59rx.js} +10 -9
- package/dist/v2/embedding-worker.js +2 -2
- package/dist/v2/fold/code-mode-catalog.d.ts +3 -0
- package/dist/v2/fold/code-mode-catalog.d.ts.map +1 -0
- package/dist/v2/fold/idle.d.ts +1 -0
- package/dist/v2/fold/idle.d.ts.map +1 -1
- package/dist/v2/fold/manual-baseline.d.ts +8 -0
- package/dist/v2/fold/manual-baseline.d.ts.map +1 -0
- package/dist/v2/fold/native-replay-v3.d.ts +7 -0
- package/dist/v2/fold/native-replay-v3.d.ts.map +1 -1
- package/dist/v2/fold/native-replay.d.ts +24 -7
- package/dist/v2/fold/native-replay.d.ts.map +1 -1
- package/dist/v2/fold/restore.d.ts.map +1 -1
- package/dist/v2/fold/summary.d.ts +4 -0
- package/dist/v2/fold/summary.d.ts.map +1 -0
- package/dist/v2/hidden-completion.d.ts.map +1 -1
- package/dist/v2/hooks/channel2.d.ts +6 -0
- package/dist/v2/hooks/channel2.d.ts.map +1 -1
- package/dist/v2/hooks/context.d.ts.map +1 -1
- package/dist/v2/hooks/restore-rows.d.ts +12 -2
- package/dist/v2/hooks/restore-rows.d.ts.map +1 -1
- package/dist/v2/hooks/synthetic-admission.d.ts +7 -0
- package/dist/v2/hooks/synthetic-admission.d.ts.map +1 -0
- package/dist/v2/hooks/usage-persist.d.ts.map +1 -1
- package/dist/v2/{message-fts-rowid-map-rmk571mb.js → message-fts-rowid-map-z86avy0n.js} +5 -4
- package/dist/v2/{message-index-bounded-620bppw7.js → message-index-bounded-w14496s9.js} +9 -8
- package/dist/v2/migration-worker.js +46 -41
- package/dist/v2/{project-embedding-registry-bm5fe1a8.js → project-embedding-registry-jqq2qeqb.js} +9 -8
- package/dist/v2/raw-ordinal-warmup-worker.js +114 -118
- package/dist/v2/{read-session-chunk-qfwv1k71.js → read-session-chunk-tqnh1ret.js} +7 -6
- package/dist/v2/{read-session-raw-q294t8xs.js → read-session-raw-qpp6x9kg.js} +4 -3
- package/dist/v2/{recomp-orchestrator-fcnjref7.js → recomp-orchestrator-9cjk6aej.js} +19 -18
- package/dist/v2/{render-98a1qv0b.js → render-5dz1knf2.js} +13 -12
- package/dist/v2/{search-z1zqzsmh.js → search-8yfet25y.js} +15 -14
- package/dist/v2/{search-archive-r0y1bafr.js → search-archive-z6z9jqwf.js} +9 -8
- package/dist/v2/{send-session-notification-fst6hkg7.js → send-session-notification-kxp47tsw.js} +4 -3
- package/dist/v2/{server-cpjk5jd2.js → server-1d042x6w.js} +16 -14
- package/dist/v2/{server-fqj6ddpr.js → server-5by3vdzs.js} +186 -89
- package/dist/v2/{server-5jp38p98.js → server-6emy0v9a.js} +1 -2
- package/dist/v2/{server-rktnzs89.js → server-6vzh6wej.js} +1 -1
- package/dist/v2/{server-qa74bj91.js → server-7094pk3w.js} +9 -8
- package/dist/v2/{server-bxpz2dcn.js → server-7acq7815.js} +8 -6
- package/dist/v2/server-7v05d9b7.js +58 -0
- package/dist/v2/{server-24g8r2d0.js → server-7zacmty5.js} +75 -75
- package/dist/v2/server-96ztjvkn.js +204 -0
- package/dist/v2/{server-hnknwaz8.js → server-a9w5f0yw.js} +3 -3
- package/dist/v2/{server-9k0mjef7.js → server-avdveykk.js} +1 -1
- package/dist/v2/{server-hsf0qsn5.js → server-cp1a03vc.js} +1 -1
- package/dist/v2/{server-r3chnxs5.js → server-dfq5vb8j.js} +2 -2
- package/dist/v2/{server-x4m6xcn4.js → server-e51xmr4d.js} +6 -6
- package/dist/v2/{server-7ccxt4nq.js → server-g3gqq4ac.js} +6 -3
- package/dist/v2/{server-kkxzwa6w.js → server-gkse1wyz.js} +3 -3
- package/dist/v2/{server-1xesqm5h.js → server-jd7vzwn4.js} +15 -13
- package/dist/v2/{server-ws5exe6q.js → server-nmyganc8.js} +1 -1
- package/dist/v2/{server-q0z2q2dx.js → server-nq3cb7be.js} +1 -1
- package/dist/v2/{server-55v5hpth.js → server-qjyp27vf.js} +2 -2
- package/dist/v2/{server-xbtw0p9t.js → server-r4430x9m.js} +9 -3
- package/dist/v2/{server-qf2kzsh2.js → server-t8y64xj7.js} +4 -4
- package/dist/v2/{server-s9xgyyj9.js → server-tnzbk64q.js} +34 -4
- package/dist/v2/{server-51779zs2.js → server-v0p37hm2.js} +4 -1
- package/dist/v2/{server-bpvsvtph.js → server-xfjjbrkt.js} +12 -1
- package/dist/v2/server.js +4 -4
- package/dist/v2/{storage-tags-19s0ebf0.js → storage-tags-cw07ky67.js} +1 -1
- package/dist/v2/{store-vdm8v3jd.js → store-z3qe67cg.js} +1 -1
- package/dist/v2/{v2-archive-reader-t2c0eypn.js → v2-archive-reader-z7wr42ry.js} +3 -3
- package/dist/v2/{wrapup-orchestrator-aqpqsveg.js → wrapup-orchestrator-j1v62bkj.js} +19 -18
- package/dist/{v2-archive-reader-tm2eayp2.js → v2-archive-reader-028z6kck.js} +2 -2
- package/dist/{wrapup-orchestrator-k5yp19a6.js → wrapup-orchestrator-xg9avt2p.js} +21 -20
- package/package.json +1 -1
- package/src/shared/model-suggestion-retry.ts +1 -1
- package/src/shared/prompt-async-transport.test.ts +64 -3
- package/src/shared/prompt-async-transport.ts +61 -16
- package/src/shared/reusable-worker-entry.ts +58 -0
- package/src/shared/reusable-worker-reset.test.ts +108 -0
- package/src/shared/reusable-worker.fixture.ts +35 -0
- package/src/shared/reusable-worker.test.ts +253 -0
- package/src/shared/reusable-worker.ts +240 -0
- package/src/shared/worker-shutdown.ts +21 -17
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
resolveLegacyConfigSources,
|
|
8
8
|
getProtectedTokensTierOverrides,
|
|
9
9
|
loadPluginConfigDetailed
|
|
10
|
-
} from "./server-
|
|
10
|
+
} from "./server-avdveykk.js";
|
|
11
11
|
import {
|
|
12
12
|
getHarness2,
|
|
13
13
|
harnessOwnsOpenCodeStore2
|
|
@@ -38,6 +38,13 @@ import {
|
|
|
38
38
|
getModelsDevCacheState
|
|
39
39
|
} from "./server-qxzbmtqt.js";
|
|
40
40
|
import {
|
|
41
|
+
string,
|
|
42
|
+
number,
|
|
43
|
+
boolean,
|
|
44
|
+
array,
|
|
45
|
+
object,
|
|
46
|
+
tuple,
|
|
47
|
+
literal,
|
|
41
48
|
isValidLanguageCode,
|
|
42
49
|
withContentLanguageDirective,
|
|
43
50
|
parseCron,
|
|
@@ -49,7 +56,7 @@ import {
|
|
|
49
56
|
DEFAULT_HISTORIAN_TIMEOUT_MS,
|
|
50
57
|
DEFAULT_LOCAL_EMBEDDING_MODEL,
|
|
51
58
|
DREAM_TASK_PROMOTION_DEFAULTS
|
|
52
|
-
} from "./server-
|
|
59
|
+
} from "./server-7zacmty5.js";
|
|
53
60
|
import {
|
|
54
61
|
CANONICAL_DREAM_TASKS,
|
|
55
62
|
DREAM_TASK_CAPABILITIES,
|
|
@@ -202,7 +209,7 @@ import {
|
|
|
202
209
|
startCompartmentAgent,
|
|
203
210
|
setRecompStarting2,
|
|
204
211
|
setRecompTerminal2
|
|
205
|
-
} from "./server-
|
|
212
|
+
} from "./server-5by3vdzs.js";
|
|
206
213
|
import {
|
|
207
214
|
resolveHistorianModel2
|
|
208
215
|
} from "./server-5kym00ag.js";
|
|
@@ -231,7 +238,7 @@ import {
|
|
|
231
238
|
beginBootQuietPeriod,
|
|
232
239
|
scheduleAfterBootQuiet
|
|
233
240
|
} from "./server-69m30b21.js";
|
|
234
|
-
import"./server-
|
|
241
|
+
import"./server-v0p37hm2.js";
|
|
235
242
|
import {
|
|
236
243
|
inRootAsyncContext2,
|
|
237
244
|
getTagIdentityWriteGeneration2,
|
|
@@ -246,9 +253,12 @@ import {
|
|
|
246
253
|
SqliteAcquisitionBusyError2,
|
|
247
254
|
withAsyncPrivilegedWriter2
|
|
248
255
|
} from "./server-gzy5w8ya.js";
|
|
256
|
+
import {
|
|
257
|
+
reuseWorker
|
|
258
|
+
} from "./server-96ztjvkn.js";
|
|
249
259
|
import {
|
|
250
260
|
createWorkerShutdown
|
|
251
|
-
} from "./server-
|
|
261
|
+
} from "./server-1d042x6w.js";
|
|
252
262
|
import {
|
|
253
263
|
areRustHistorianSyntheticParts2,
|
|
254
264
|
isRawCompactionSummaryInfo2,
|
|
@@ -256,7 +266,7 @@ import {
|
|
|
256
266
|
buildInMemoryTailRawMessages2,
|
|
257
267
|
RawOrdinalsNotReady2,
|
|
258
268
|
runSessionWarmupWorker
|
|
259
|
-
} from "./server-
|
|
269
|
+
} from "./server-tnzbk64q.js";
|
|
260
270
|
import {
|
|
261
271
|
resolveModelCalibration,
|
|
262
272
|
calibrateBuckets,
|
|
@@ -351,7 +361,7 @@ import {
|
|
|
351
361
|
adoptNullOwnerToolTag2,
|
|
352
362
|
getCandidateToolOwners2,
|
|
353
363
|
pickNearestPriorOwner2
|
|
354
|
-
} from "./server-
|
|
364
|
+
} from "./server-xfjjbrkt.js";
|
|
355
365
|
import {
|
|
356
366
|
textMentionsRecentCommit,
|
|
357
367
|
hasMeaningfulUserText2,
|
|
@@ -499,7 +509,6 @@ import {
|
|
|
499
509
|
getPendingPiCompactionMarkerState,
|
|
500
510
|
clearPendingPiCompactionMarkerStateIf,
|
|
501
511
|
logPiCompactionMarkerClear,
|
|
502
|
-
setSessionWorkMetrics,
|
|
503
512
|
getObservedEpochFloor,
|
|
504
513
|
resolveEpochFloorForPass,
|
|
505
514
|
getReasoningRemovalState,
|
|
@@ -581,7 +590,7 @@ import {
|
|
|
581
590
|
readRawSessionMessageOrdinalById2,
|
|
582
591
|
readSessionChunk2,
|
|
583
592
|
readRawSessionSeedTail2
|
|
584
|
-
} from "./server-
|
|
593
|
+
} from "./server-jd7vzwn4.js";
|
|
585
594
|
import {
|
|
586
595
|
BoundedSessionMap,
|
|
587
596
|
recordToolDefinitionSet,
|
|
@@ -592,8 +601,8 @@ import {
|
|
|
592
601
|
cleanSearchText,
|
|
593
602
|
matchedSearchSnippet,
|
|
594
603
|
getLastIndexedOrdinal
|
|
595
|
-
} from "./server-
|
|
596
|
-
import"./server-
|
|
604
|
+
} from "./server-dfq5vb8j.js";
|
|
605
|
+
import"./server-nmyganc8.js";
|
|
597
606
|
import {
|
|
598
607
|
getMessageIndexEpoch,
|
|
599
608
|
isMessageIndexSearchable,
|
|
@@ -619,7 +628,7 @@ import {
|
|
|
619
628
|
resolveMemoriesByIdsForSearch2,
|
|
620
629
|
unifiedSearch2,
|
|
621
630
|
formatSearchResults2
|
|
622
|
-
} from "./server-
|
|
631
|
+
} from "./server-7094pk3w.js";
|
|
623
632
|
import {
|
|
624
633
|
embedPauseBySession,
|
|
625
634
|
embedRunStateBySession,
|
|
@@ -641,7 +650,7 @@ import {
|
|
|
641
650
|
} from "./server-zzgtgqmd.js";
|
|
642
651
|
import {
|
|
643
652
|
deleteChunkEmbedBackoffForSession2
|
|
644
|
-
} from "./server-
|
|
653
|
+
} from "./server-cp1a03vc.js";
|
|
645
654
|
import {
|
|
646
655
|
registerOwnRpcServerInstance,
|
|
647
656
|
rpcPortDir,
|
|
@@ -752,7 +761,7 @@ import {
|
|
|
752
761
|
saveSourceContent,
|
|
753
762
|
replaceSourceContent,
|
|
754
763
|
getSourceContents
|
|
755
|
-
} from "./server-
|
|
764
|
+
} from "./server-t8y64xj7.js";
|
|
756
765
|
import {
|
|
757
766
|
startBootDeadline,
|
|
758
767
|
getUnconfirmedMigrationHolders,
|
|
@@ -768,7 +777,7 @@ import {
|
|
|
768
777
|
openDatabaseAsync,
|
|
769
778
|
isDatabasePersisted,
|
|
770
779
|
getDatabasePersistenceError
|
|
771
|
-
} from "./server-
|
|
780
|
+
} from "./server-g3gqq4ac.js";
|
|
772
781
|
import {
|
|
773
782
|
V2_MEMORY_CATEGORIES,
|
|
774
783
|
CATEGORY_PRIORITY
|
|
@@ -824,7 +833,7 @@ import {
|
|
|
824
833
|
resolveHistorianProducerLimits2,
|
|
825
834
|
resolveHistorianContextLimit2
|
|
826
835
|
} from "./server-sd8hvzte.js";
|
|
827
|
-
import"./server-
|
|
836
|
+
import"./server-r4430x9m.js";
|
|
828
837
|
import {
|
|
829
838
|
isEmbeddingHostBusy2
|
|
830
839
|
} from "./server-qt1qqasc.js";
|
|
@@ -848,7 +857,7 @@ import {
|
|
|
848
857
|
embedSessionCompartmentChunks2,
|
|
849
858
|
getEmbeddingCoverageStatus2,
|
|
850
859
|
getAutoEmbeddingSessionCoverage2
|
|
851
|
-
} from "./server-
|
|
860
|
+
} from "./server-7acq7815.js";
|
|
852
861
|
import {
|
|
853
862
|
GIT_SWEEP_LEASE_RENEWAL_MS,
|
|
854
863
|
acquireGitSweepLease,
|
|
@@ -868,10 +877,10 @@ import {
|
|
|
868
877
|
getCachedLocalEmbeddingPooling,
|
|
869
878
|
preflightLocalEmbeddingPooling,
|
|
870
879
|
getLocalEmbeddingNativeMemoryStats
|
|
871
|
-
} from "./server-
|
|
880
|
+
} from "./server-6vzh6wej.js";
|
|
872
881
|
import {
|
|
873
882
|
cosineSimilarity
|
|
874
|
-
} from "./server-
|
|
883
|
+
} from "./server-gkse1wyz.js";
|
|
875
884
|
import {
|
|
876
885
|
computeCueContentHash,
|
|
877
886
|
hasMuralCueColumns,
|
|
@@ -891,6 +900,7 @@ import {
|
|
|
891
900
|
getMemoryById2,
|
|
892
901
|
getMemoriesByIds2,
|
|
893
902
|
updateMemorySeenCount2,
|
|
903
|
+
updateMemoryVerification2,
|
|
894
904
|
setMemoryClassification2,
|
|
895
905
|
supersededMemory2,
|
|
896
906
|
mergeMemoryStats2,
|
|
@@ -915,7 +925,7 @@ import {
|
|
|
915
925
|
readGitFileChangeTimesSince,
|
|
916
926
|
verificationFileExists,
|
|
917
927
|
normalizeVerificationFiles
|
|
918
|
-
} from "./server-
|
|
928
|
+
} from "./server-nq3cb7be.js";
|
|
919
929
|
import {
|
|
920
930
|
registerNotificationSink2,
|
|
921
931
|
pushNotification2,
|
|
@@ -944,7 +954,7 @@ import {
|
|
|
944
954
|
readV2RawMessagePagesForConversion2,
|
|
945
955
|
createV2RawMessageReader2,
|
|
946
956
|
createV2RawMessageProvider2
|
|
947
|
-
} from "./server-
|
|
957
|
+
} from "./server-6emy0v9a.js";
|
|
948
958
|
import {
|
|
949
959
|
resolvePromptContext2
|
|
950
960
|
} from "./server-fmsr7v69.js";
|
|
@@ -952,14 +962,14 @@ import {
|
|
|
952
962
|
renderMessageByOrdinal2,
|
|
953
963
|
renderVerboseRangeForTool2,
|
|
954
964
|
renderItemByTag2
|
|
955
|
-
} from "./server-
|
|
965
|
+
} from "./server-qjyp27vf.js";
|
|
956
966
|
import"./server-kaqs6h31.js";
|
|
957
967
|
import {
|
|
958
968
|
readV2ArchiveOffThread2
|
|
959
|
-
} from "./server-
|
|
969
|
+
} from "./server-e51xmr4d.js";
|
|
960
970
|
import {
|
|
961
971
|
renderSearchArchiveOffThread2
|
|
962
|
-
} from "./server-
|
|
972
|
+
} from "./server-a9w5f0yw.js";
|
|
963
973
|
import {
|
|
964
974
|
markCoveredInstructionRow,
|
|
965
975
|
isCoveredInstructionRow,
|
|
@@ -1099,11 +1109,11 @@ __export(exports_external, {
|
|
|
1099
1109
|
_default: () => _default2,
|
|
1100
1110
|
_function: () => _function,
|
|
1101
1111
|
any: () => any,
|
|
1102
|
-
array: () =>
|
|
1112
|
+
array: () => array2,
|
|
1103
1113
|
base64: () => base642,
|
|
1104
1114
|
base64url: () => base64url2,
|
|
1105
1115
|
bigint: () => bigint2,
|
|
1106
|
-
boolean: () =>
|
|
1116
|
+
boolean: () => boolean3,
|
|
1107
1117
|
catch: () => _catch2,
|
|
1108
1118
|
check: () => check,
|
|
1109
1119
|
cidrv4: () => cidrv42,
|
|
@@ -1157,7 +1167,7 @@ __export(exports_external, {
|
|
|
1157
1167
|
ksuid: () => ksuid2,
|
|
1158
1168
|
lazy: () => lazy,
|
|
1159
1169
|
length: () => _length,
|
|
1160
|
-
literal: () =>
|
|
1170
|
+
literal: () => literal2,
|
|
1161
1171
|
locales: () => exports_locales,
|
|
1162
1172
|
looseObject: () => looseObject,
|
|
1163
1173
|
lowercase: () => _lowercase,
|
|
@@ -1182,8 +1192,8 @@ __export(exports_external, {
|
|
|
1182
1192
|
null: () => _null3,
|
|
1183
1193
|
nullable: () => nullable,
|
|
1184
1194
|
nullish: () => nullish2,
|
|
1185
|
-
number: () =>
|
|
1186
|
-
object: () =>
|
|
1195
|
+
number: () => number3,
|
|
1196
|
+
object: () => object3,
|
|
1187
1197
|
optional: () => optional,
|
|
1188
1198
|
overwrite: () => _overwrite,
|
|
1189
1199
|
parse: () => parse3,
|
|
@@ -1213,7 +1223,7 @@ __export(exports_external, {
|
|
|
1213
1223
|
size: () => _size,
|
|
1214
1224
|
startsWith: () => _startsWith,
|
|
1215
1225
|
strictObject: () => strictObject,
|
|
1216
|
-
string: () =>
|
|
1226
|
+
string: () => string3,
|
|
1217
1227
|
stringFormat: () => stringFormat,
|
|
1218
1228
|
stringbool: () => stringbool,
|
|
1219
1229
|
success: () => success,
|
|
@@ -1226,7 +1236,7 @@ __export(exports_external, {
|
|
|
1226
1236
|
transform: () => transform,
|
|
1227
1237
|
treeifyError: () => treeifyError,
|
|
1228
1238
|
trim: () => _trim,
|
|
1229
|
-
tuple: () =>
|
|
1239
|
+
tuple: () => tuple2,
|
|
1230
1240
|
uint32: () => uint32,
|
|
1231
1241
|
uint64: () => uint64,
|
|
1232
1242
|
ulid: () => ulid2,
|
|
@@ -1579,7 +1589,7 @@ __export(exports_regexes, {
|
|
|
1579
1589
|
base64: () => base64,
|
|
1580
1590
|
base64url: () => base64url,
|
|
1581
1591
|
bigint: () => bigint,
|
|
1582
|
-
boolean: () =>
|
|
1592
|
+
boolean: () => boolean2,
|
|
1583
1593
|
browserEmail: () => browserEmail,
|
|
1584
1594
|
cidrv4: () => cidrv4,
|
|
1585
1595
|
cidrv6: () => cidrv6,
|
|
@@ -1608,7 +1618,7 @@ __export(exports_regexes, {
|
|
|
1608
1618
|
md5_hex: () => md5_hex,
|
|
1609
1619
|
nanoid: () => nanoid,
|
|
1610
1620
|
null: () => _null,
|
|
1611
|
-
number: () =>
|
|
1621
|
+
number: () => number2,
|
|
1612
1622
|
rfc5322Email: () => rfc5322Email,
|
|
1613
1623
|
sha1_base64: () => sha1_base64,
|
|
1614
1624
|
sha1_base64url: () => sha1_base64url,
|
|
@@ -1622,7 +1632,7 @@ __export(exports_regexes, {
|
|
|
1622
1632
|
sha512_base64: () => sha512_base64,
|
|
1623
1633
|
sha512_base64url: () => sha512_base64url,
|
|
1624
1634
|
sha512_hex: () => sha512_hex,
|
|
1625
|
-
string: () =>
|
|
1635
|
+
string: () => string2,
|
|
1626
1636
|
time: () => time,
|
|
1627
1637
|
ulid: () => ulid,
|
|
1628
1638
|
undefined: () => _undefined,
|
|
@@ -1706,10 +1716,10 @@ __export(exports_iso, {
|
|
|
1706
1716
|
var exports_coerce = {};
|
|
1707
1717
|
__export(exports_coerce, {
|
|
1708
1718
|
bigint: () => bigint3,
|
|
1709
|
-
boolean: () =>
|
|
1719
|
+
boolean: () => boolean4,
|
|
1710
1720
|
date: () => date4,
|
|
1711
|
-
number: () =>
|
|
1712
|
-
string: () =>
|
|
1721
|
+
number: () => number4,
|
|
1722
|
+
string: () => string4
|
|
1713
1723
|
});
|
|
1714
1724
|
|
|
1715
1725
|
// src/v2/hooks/context.ts
|
|
@@ -4222,7 +4232,7 @@ async function acquireSearchWorker(db, key, entry, input, signal) {
|
|
|
4222
4232
|
}
|
|
4223
4233
|
const fresh = !slot;
|
|
4224
4234
|
if (!slot) {
|
|
4225
|
-
const worker = inRootAsyncContext2(() => new Worker(entry, { workerData: { ...input, persistent: true } }));
|
|
4235
|
+
const worker = inRootAsyncContext2(() => input.job === "backfill" && entry.href === defaultAutoSearchWorkerEntry().href ? reuseWorker(entry, { workerData: input }) : new Worker(entry, { workerData: { ...input, persistent: true } }));
|
|
4226
4236
|
const shutdown = createWorkerShutdown(worker, "auto-search", AUTO_SEARCH_WORKER_CLOSE_DEADLINE_MS);
|
|
4227
4237
|
slot = {
|
|
4228
4238
|
worker,
|
|
@@ -4300,6 +4310,7 @@ function queueAutoSearchBackfill(db, sessionId, projectPath, query) {
|
|
|
4300
4310
|
pending.set(key, work);
|
|
4301
4311
|
return work;
|
|
4302
4312
|
}
|
|
4313
|
+
var nextSearchJobId = 0;
|
|
4303
4314
|
async function executeAutoSearchWorker(db, sessionId, projectPath, query, options, entry, job) {
|
|
4304
4315
|
if (options.signal?.aborted)
|
|
4305
4316
|
return [];
|
|
@@ -4320,6 +4331,7 @@ async function executeAutoSearchWorker(db, sessionId, projectPath, query, option
|
|
|
4320
4331
|
const signal = deadlineSignal ? AbortSignal.any([deadlineSignal, lifecycle.signal]) : lifecycle.signal;
|
|
4321
4332
|
const snapshot = getProjectEmbeddingSnapshot2(projectPath);
|
|
4322
4333
|
const input = {
|
|
4334
|
+
jobId: ++nextSearchJobId,
|
|
4323
4335
|
job,
|
|
4324
4336
|
path,
|
|
4325
4337
|
harness: getHarness2(),
|
|
@@ -4439,6 +4451,8 @@ async function executeAutoSearchWorker(db, sessionId, projectPath, query, option
|
|
|
4439
4451
|
const onMessage = async (reply) => {
|
|
4440
4452
|
if (released)
|
|
4441
4453
|
return;
|
|
4454
|
+
if (entry.href === defaultAutoSearchWorkerEntry().href && reply.jobId !== input.jobId)
|
|
4455
|
+
return;
|
|
4442
4456
|
if (reply.kind === "diagnostics") {
|
|
4443
4457
|
if (!settled && options.autoSearchDiagnostics) {
|
|
4444
4458
|
const workerStarted = options.autoSearchDiagnostics.workerStarted;
|
|
@@ -4466,7 +4480,7 @@ async function executeAutoSearchWorker(db, sessionId, projectPath, query, option
|
|
|
4466
4480
|
}
|
|
4467
4481
|
if (reply.kind === "messages" || reply.kind === "accepted")
|
|
4468
4482
|
return;
|
|
4469
|
-
const response = { id: reply.id };
|
|
4483
|
+
const response = { id: reply.id, jobId: input.jobId };
|
|
4470
4484
|
if (!signal.aborted) {
|
|
4471
4485
|
const requestedAt = performance.now();
|
|
4472
4486
|
try {
|
|
@@ -4595,7 +4609,7 @@ function searchMessageHistoryOffThread(db, request, entry = defaultAutoSearchWor
|
|
|
4595
4609
|
deadline.unref?.();
|
|
4596
4610
|
};
|
|
4597
4611
|
try {
|
|
4598
|
-
worker = inRootAsyncContext2(() => new Worker(entry, { workerData: input }));
|
|
4612
|
+
worker = inRootAsyncContext2(() => entry.href === defaultAutoSearchWorkerEntry().href ? reuseWorker(entry, { workerData: input }) : new Worker(entry, { workerData: input }));
|
|
4599
4613
|
shutdown = createWorkerShutdown(worker, "message-search", MESSAGE_SEARCH_WORKER_CLOSE_DEADLINE_MS);
|
|
4600
4614
|
} catch (error) {
|
|
4601
4615
|
finish(null, "start-failed", error);
|
|
@@ -5484,12 +5498,17 @@ function countCompartmentsSince(db, projectPath, since) {
|
|
|
5484
5498
|
return row?.cnt ?? 0;
|
|
5485
5499
|
}
|
|
5486
5500
|
function countProjectSessionsSince(db, projectPath, since) {
|
|
5487
|
-
const
|
|
5488
|
-
|
|
5489
|
-
|
|
5490
|
-
|
|
5491
|
-
|
|
5492
|
-
|
|
5501
|
+
const scope = `FROM session_projects sp
|
|
5502
|
+
LEFT JOIN session_meta m ON m.session_id = sp.session_id
|
|
5503
|
+
WHERE sp.project_path = ? AND sp.harness = ?
|
|
5504
|
+
AND COALESCE(m.is_subagent, 0) = 0`;
|
|
5505
|
+
const row = since === null ? db.prepare(`SELECT COUNT(*) AS cnt ${scope}`).get(projectPath, getHarness2()) : db.prepare(`SELECT COUNT(*) AS cnt ${scope}
|
|
5506
|
+
AND COALESCE(
|
|
5507
|
+
(SELECT CAST(value AS INTEGER) FROM schema_migrations_meta
|
|
5508
|
+
WHERE key = 'retrospective_activity:' || sp.session_id),
|
|
5509
|
+
(SELECT updated_at FROM message_history_index
|
|
5510
|
+
WHERE session_id = sp.session_id AND harness = sp.harness)
|
|
5511
|
+
) > ?`).get(projectPath, getHarness2(), since);
|
|
5493
5512
|
return row?.cnt ?? 0;
|
|
5494
5513
|
}
|
|
5495
5514
|
function countMappedMemories(db, projectPath) {
|
|
@@ -5502,6 +5521,10 @@ function countMappedMemories(db, projectPath) {
|
|
|
5502
5521
|
AND v.file_path <> ''`).get(projectPath, Date.now());
|
|
5503
5522
|
return row?.cnt ?? 0;
|
|
5504
5523
|
}
|
|
5524
|
+
var CURRENT_VERIFY_SKIP_SQL = `COALESCE(CASE WHEN json_valid(m.metadata_json) THEN
|
|
5525
|
+
json_extract(m.metadata_json, '$.dreamerVerifySkip.contentHash') = m.normalized_hash
|
|
5526
|
+
AND json_type(m.metadata_json, '$.dreamerVerifySkip.fingerprint') = 'text'
|
|
5527
|
+
ELSE 0 END, 0)`;
|
|
5505
5528
|
function countUnverifiedMappedMemories(db, projectPath) {
|
|
5506
5529
|
const row = db.prepare(`SELECT COUNT(DISTINCT m.id) AS cnt
|
|
5507
5530
|
FROM memories m
|
|
@@ -5510,7 +5533,8 @@ function countUnverifiedMappedMemories(db, projectPath) {
|
|
|
5510
5533
|
AND m.status IN ('active','permanent')
|
|
5511
5534
|
AND (m.expires_at IS NULL OR m.expires_at > ?)
|
|
5512
5535
|
AND v.file_path <> ''
|
|
5513
|
-
AND v.verified_at = 0
|
|
5536
|
+
AND v.verified_at = 0
|
|
5537
|
+
AND NOT (${CURRENT_VERIFY_SKIP_SQL})`).get(projectPath, Date.now());
|
|
5514
5538
|
return row?.cnt ?? 0;
|
|
5515
5539
|
}
|
|
5516
5540
|
function countBroadCycleCandidates(db, projectPath, cycleStartAt) {
|
|
@@ -5519,6 +5543,7 @@ function countBroadCycleCandidates(db, projectPath, cycleStartAt) {
|
|
|
5519
5543
|
WHERE m.project_path = ?
|
|
5520
5544
|
AND m.status IN ('active','permanent')
|
|
5521
5545
|
AND (m.expires_at IS NULL OR m.expires_at > ?)
|
|
5546
|
+
AND NOT (${CURRENT_VERIFY_SKIP_SQL})
|
|
5522
5547
|
AND (
|
|
5523
5548
|
SELECT MAX(v.verified_at)
|
|
5524
5549
|
FROM memory_verifications v
|
|
@@ -5627,7 +5652,7 @@ function getDreamTaskBacklog(db, projectPath, task, options = {}) {
|
|
|
5627
5652
|
case "verify-broad": {
|
|
5628
5653
|
const total = countMappedMemories(db, projectPath);
|
|
5629
5654
|
const cycleStartAt = getTaskScheduleState(db, projectPath, "verify-broad")?.lastBroadRunAt;
|
|
5630
|
-
const pending =
|
|
5655
|
+
const pending = countBroadCycleCandidates(db, projectPath, cycleStartAt ?? Number.MAX_SAFE_INTEGER);
|
|
5631
5656
|
return { pending, total };
|
|
5632
5657
|
}
|
|
5633
5658
|
case "curate": {
|
|
@@ -5754,6 +5779,7 @@ function getDreamTaskBacklogs(db, projectPath, tasks = CANONICAL_DREAM_TASKS, op
|
|
|
5754
5779
|
}
|
|
5755
5780
|
const optionKey = (value) => value === undefined ? "omitted" : value === null ? "null" : `number:${value}`;
|
|
5756
5781
|
const key = JSON.stringify([
|
|
5782
|
+
getHarness2(),
|
|
5757
5783
|
projectPath,
|
|
5758
5784
|
tasks,
|
|
5759
5785
|
optionKey(options.lastRunAt),
|
|
@@ -8584,6 +8610,130 @@ function isVisibleNoteReadPart(part) {
|
|
|
8584
8610
|
return false;
|
|
8585
8611
|
}
|
|
8586
8612
|
|
|
8613
|
+
// src/hooks/magic-context/strip-structural-noise.ts
|
|
8614
|
+
var STRUCTURAL_PART_TYPES = new Set(["meta", "step-start", "step-finish", "reasoning"]);
|
|
8615
|
+
var structuralSentinels = new WeakSet;
|
|
8616
|
+
function isStructuralNoiseSentinel(part) {
|
|
8617
|
+
return isRecord(part) && structuralSentinels.has(part);
|
|
8618
|
+
}
|
|
8619
|
+
function isStructuralNoisePart(part) {
|
|
8620
|
+
if (!isRecord(part) || typeof part.type !== "string") {
|
|
8621
|
+
return false;
|
|
8622
|
+
}
|
|
8623
|
+
if (!STRUCTURAL_PART_TYPES.has(part.type)) {
|
|
8624
|
+
return false;
|
|
8625
|
+
}
|
|
8626
|
+
if (part.type === "reasoning" && typeof part.text === "string" && part.text !== "[cleared]") {
|
|
8627
|
+
return false;
|
|
8628
|
+
}
|
|
8629
|
+
return true;
|
|
8630
|
+
}
|
|
8631
|
+
function stripStructuralNoise(messages) {
|
|
8632
|
+
let strippedParts = 0;
|
|
8633
|
+
for (const message of messages) {
|
|
8634
|
+
if (!Array.isArray(message.parts)) {
|
|
8635
|
+
continue;
|
|
8636
|
+
}
|
|
8637
|
+
for (let i = 0;i < message.parts.length; i++) {
|
|
8638
|
+
const part = message.parts[i];
|
|
8639
|
+
if (isSentinel(part))
|
|
8640
|
+
continue;
|
|
8641
|
+
if (!isStructuralNoisePart(part))
|
|
8642
|
+
continue;
|
|
8643
|
+
message.parts[i] = makeSentinel(part);
|
|
8644
|
+
if (isRecord(part) && part.type !== "reasoning") {
|
|
8645
|
+
structuralSentinels.add(message.parts[i]);
|
|
8646
|
+
}
|
|
8647
|
+
strippedParts++;
|
|
8648
|
+
}
|
|
8649
|
+
}
|
|
8650
|
+
return strippedParts;
|
|
8651
|
+
}
|
|
8652
|
+
|
|
8653
|
+
// src/hooks/magic-context/project-structural-sentinels.ts
|
|
8654
|
+
function readOmissions(value) {
|
|
8655
|
+
if (value === undefined)
|
|
8656
|
+
return new Set;
|
|
8657
|
+
if (!Array.isArray(value) || value.some((id) => typeof id !== "string" || !id))
|
|
8658
|
+
throw new Error("Invalid structural-sentinel replay decisions");
|
|
8659
|
+
return new Set(value);
|
|
8660
|
+
}
|
|
8661
|
+
function hasSignedReasoning(message) {
|
|
8662
|
+
return message.info.role === "assistant" && message.parts.some((part) => {
|
|
8663
|
+
if (!isRecord(part) || part.type !== "reasoning" || !isRecord(part.metadata))
|
|
8664
|
+
return false;
|
|
8665
|
+
const anthropic = part.metadata.anthropic;
|
|
8666
|
+
return isRecord(anthropic) && typeof anthropic.signature === "string";
|
|
8667
|
+
});
|
|
8668
|
+
}
|
|
8669
|
+
function readKeepIds(document) {
|
|
8670
|
+
const value = document.structuralSentinelUpgrade;
|
|
8671
|
+
if (value === undefined)
|
|
8672
|
+
return;
|
|
8673
|
+
if (!isRecord(value) || !Array.isArray(value.keep) || value.keep.some((id) => typeof id !== "string" || !id))
|
|
8674
|
+
throw new Error("Invalid structural-sentinel upgrade");
|
|
8675
|
+
return new Set(value.keep);
|
|
8676
|
+
}
|
|
8677
|
+
function prepareStructuralSentinelChoices(args) {
|
|
8678
|
+
const document = readReplayEnvelope(args.db, args.sessionId);
|
|
8679
|
+
if (readKeepIds(document))
|
|
8680
|
+
return document;
|
|
8681
|
+
const prior = !!args.db.prepare(`SELECT 1 WHERE
|
|
8682
|
+
EXISTS (SELECT 1 FROM session_meta WHERE session_id = ? AND trailing_blank_decisions <> '')
|
|
8683
|
+
OR EXISTS (SELECT 1 FROM session_replay_decisions WHERE session_id = ?)
|
|
8684
|
+
OR EXISTS (SELECT 1 FROM tags WHERE session_id = ?)
|
|
8685
|
+
OR EXISTS (SELECT 1 FROM lkg_slots WHERE session_id = ?)`).get(args.sessionId, args.sessionId, args.sessionId, args.sessionId);
|
|
8686
|
+
try {
|
|
8687
|
+
const persisted = updateReplayDocument(args.db, args.sessionId, (current) => {
|
|
8688
|
+
if (readKeepIds(current))
|
|
8689
|
+
return false;
|
|
8690
|
+
current.version = 2;
|
|
8691
|
+
current.structuralSentinelUpgrade = {
|
|
8692
|
+
keep: prior ? args.messages.flatMap((message) => message.info.id && hasSignedReasoning(message) ? [message.info.id] : []) : []
|
|
8693
|
+
};
|
|
8694
|
+
return true;
|
|
8695
|
+
});
|
|
8696
|
+
if (!persisted)
|
|
8697
|
+
throw new Error("Cannot persist structural-sentinel upgrade");
|
|
8698
|
+
} catch (error) {
|
|
8699
|
+
sessionLog2(args.sessionId, `structural-sentinel upgrade deferred: ${String(error)}`);
|
|
8700
|
+
return document;
|
|
8701
|
+
}
|
|
8702
|
+
return readReplayEnvelope(args.db, args.sessionId);
|
|
8703
|
+
}
|
|
8704
|
+
function projectStructuralSentinels(args) {
|
|
8705
|
+
const structuralMessages = args.messages.filter((message) => message.parts.some(isStructuralNoiseSentinel));
|
|
8706
|
+
if (structuralMessages.length === 0)
|
|
8707
|
+
return 0;
|
|
8708
|
+
const document = args.deferUpgrade ? readReplayEnvelope(args.db, args.sessionId) : prepareStructuralSentinelChoices(args);
|
|
8709
|
+
const keepIds = args.deferUpgrade ? undefined : readKeepIds(document);
|
|
8710
|
+
let omittedIds = readOmissions(document.structuralSentinelOmissions);
|
|
8711
|
+
const candidates = structuralMessages.filter((message) => typeof message.info.id === "string" && message.info.id !== "" && hasSignedReasoning(message));
|
|
8712
|
+
const selected = keepIds && args.rebuilding ? candidates.filter((message) => keepIds.has(message.info.id ?? "") && !omittedIds.has(message.info.id ?? "")) : [];
|
|
8713
|
+
if (selected.length) {
|
|
8714
|
+
if (!updateReplayDocument(args.db, args.sessionId, (document) => {
|
|
8715
|
+
const ids = readOmissions(document.structuralSentinelOmissions);
|
|
8716
|
+
for (const message of selected) {
|
|
8717
|
+
ids.add(message.info.id);
|
|
8718
|
+
}
|
|
8719
|
+
document.version = 2;
|
|
8720
|
+
document.structuralSentinelOmissions = [...ids];
|
|
8721
|
+
return true;
|
|
8722
|
+
}))
|
|
8723
|
+
throw new Error("Cannot persist structural-sentinel omissions");
|
|
8724
|
+
omittedIds = readOmissions(readReplayEnvelope(args.db, args.sessionId).structuralSentinelOmissions);
|
|
8725
|
+
}
|
|
8726
|
+
let removed = 0;
|
|
8727
|
+
for (const message of candidates) {
|
|
8728
|
+
if (!omittedIds.has(message.info.id ?? "") && (!keepIds || keepIds.has(message.info.id ?? "")))
|
|
8729
|
+
continue;
|
|
8730
|
+
const parts = message.parts.filter((part) => !isStructuralNoiseSentinel(part));
|
|
8731
|
+
removed += message.parts.length - parts.length;
|
|
8732
|
+
message.parts = parts;
|
|
8733
|
+
}
|
|
8734
|
+
return removed;
|
|
8735
|
+
}
|
|
8736
|
+
|
|
8587
8737
|
// src/hooks/magic-context/strict-thinking-prefix.ts
|
|
8588
8738
|
function servedPrefixMatches(args) {
|
|
8589
8739
|
const heads = args.messages?.slice(0, 2);
|
|
@@ -8930,38 +9080,6 @@ ${typeof state?.output === "string" ? state.output : JSON.stringify(state?.outpu
|
|
|
8930
9080
|
text: typeof part.text === "string" ? part.text : typeof part.thinking === "string" ? part.thinking : undefined
|
|
8931
9081
|
};
|
|
8932
9082
|
}
|
|
8933
|
-
// src/hooks/magic-context/strip-structural-noise.ts
|
|
8934
|
-
var STRUCTURAL_PART_TYPES = new Set(["meta", "step-start", "step-finish", "reasoning"]);
|
|
8935
|
-
function isStructuralNoisePart(part) {
|
|
8936
|
-
if (!isRecord(part) || typeof part.type !== "string") {
|
|
8937
|
-
return false;
|
|
8938
|
-
}
|
|
8939
|
-
if (!STRUCTURAL_PART_TYPES.has(part.type)) {
|
|
8940
|
-
return false;
|
|
8941
|
-
}
|
|
8942
|
-
if (part.type === "reasoning" && typeof part.text === "string" && part.text !== "[cleared]") {
|
|
8943
|
-
return false;
|
|
8944
|
-
}
|
|
8945
|
-
return true;
|
|
8946
|
-
}
|
|
8947
|
-
function stripStructuralNoise(messages) {
|
|
8948
|
-
let strippedParts = 0;
|
|
8949
|
-
for (const message of messages) {
|
|
8950
|
-
if (!Array.isArray(message.parts)) {
|
|
8951
|
-
continue;
|
|
8952
|
-
}
|
|
8953
|
-
for (let i = 0;i < message.parts.length; i++) {
|
|
8954
|
-
const part = message.parts[i];
|
|
8955
|
-
if (isSentinel(part))
|
|
8956
|
-
continue;
|
|
8957
|
-
if (!isStructuralNoisePart(part))
|
|
8958
|
-
continue;
|
|
8959
|
-
message.parts[i] = makeSentinel(part);
|
|
8960
|
-
strippedParts++;
|
|
8961
|
-
}
|
|
8962
|
-
}
|
|
8963
|
-
return strippedParts;
|
|
8964
|
-
}
|
|
8965
9083
|
// src/hooks/magic-context/foreign-text-parts.ts
|
|
8966
9084
|
function hasStringId(part) {
|
|
8967
9085
|
return part !== null && typeof part === "object" && typeof part.id === "string";
|
|
@@ -10432,6 +10550,9 @@ async function runPostTransformPhase(args) {
|
|
|
10432
10550
|
}
|
|
10433
10551
|
};
|
|
10434
10552
|
const trailingBlankSourceDecisions = args.trailingBlankSourceDecisions ?? snapshotTrailingBlankSourceDecisions(args.messages);
|
|
10553
|
+
let structuralSentinelUpgradeFailed = args.structuralSentinelUpgradeFailed ?? false;
|
|
10554
|
+
if (!structuralSentinelUpgradeFailed && args.opencode1 && !compactionOff && modelAcceptsEmptyContent(args.resolvedProviderID))
|
|
10555
|
+
structuralSentinelUpgradeFailed = prepareStructuralSentinelChoices(args).structuralSentinelUpgrade === undefined;
|
|
10435
10556
|
if (!modelAcceptsEmptyContent(args.resolvedProviderID) && !compactionOff)
|
|
10436
10557
|
captureInlineThinkingRepairs(args.messages, args.messageTagNumbers, args.sessionMeta?.clearedReasoningThroughTag ?? 0);
|
|
10437
10558
|
const servedShapeRepair = prepareServedShapeRepair(args.db, args.sessionId, args.messages, args.readLastServedMessages, compactionOff ? undefined : postprocessReplaySnapshot(args.db, args.sessionId));
|
|
@@ -10550,6 +10671,8 @@ async function runPostTransformPhase(args) {
|
|
|
10550
10671
|
const servedBeforeFirstRender = firstRenderBust ? args.readLastServedMessages?.() : null;
|
|
10551
10672
|
let foldExecutedThisPass = false;
|
|
10552
10673
|
let foldBustsServedPrefixThisPass = false;
|
|
10674
|
+
let foldChangesHeadThisPass = false;
|
|
10675
|
+
let recoveryExternalFoldCause = null;
|
|
10553
10676
|
let publishedM1RefreshedThisPass = false;
|
|
10554
10677
|
let prefixPreflightFailed = false;
|
|
10555
10678
|
const softRefreshOpportunity = !freezeM0M1 && (args.schedulerDecision === "execute" || deferredMaterialize || servedShapeRepair.needsRebuild);
|
|
@@ -10628,9 +10751,16 @@ async function runPostTransformPhase(args) {
|
|
|
10628
10751
|
prefixPreflightFailed = foldResult.materializationContentionRetryExhausted === true;
|
|
10629
10752
|
foldExecutedThisPass = foldExecutesThisPass(foldDueDecision.value || softRefreshOpportunity, foldResult.m0RematerializedThisPass);
|
|
10630
10753
|
foldBustsServedPrefixThisPass = foldExecutedThisPass && (committedFoldBustsServedPrefix ?? true);
|
|
10754
|
+
foldChangesHeadThisPass = foldExecutedThisPass && committedFoldChangesHead === true;
|
|
10631
10755
|
publishedM1RefreshedThisPass = !foldResult.materializationContentionRetryExhausted && previousM1 !== args.sessionMeta.cachedM1Bytes?.toString("utf8");
|
|
10632
10756
|
m0RematerializedThisPass = foldResult.m0RematerializedThisPass;
|
|
10633
10757
|
m0MaterializeReason = foldResult.decision.reason;
|
|
10758
|
+
if (!prefixPreflightFailed && foldExecutedThisPass) {
|
|
10759
|
+
const decision = foldResult.decision;
|
|
10760
|
+
if (decision.reason === "system_hash" && decision.systemHashPrev && decision.systemHashPrev !== "0" || decision.reason === "model_change" && decision.m0ModelKeyPrev || decision.reason === "tool_definitions") {
|
|
10761
|
+
recoveryExternalFoldCause = decision.reason;
|
|
10762
|
+
}
|
|
10763
|
+
}
|
|
10634
10764
|
if (foldResult.m0RematerializedThisPass && convertedToolSkeletons.size > 0) {
|
|
10635
10765
|
convertedToolSkeletonsDidMutate = renderConvertedToolSkeletons(args.targets, convertedToolSkeletons);
|
|
10636
10766
|
sessionLog2(args.sessionId, `HARD fold converted ${convertedToolSkeletons.size} legacy dropped-tool skeleton(s) to real-or-absent`);
|
|
@@ -10733,8 +10863,14 @@ async function runPostTransformPhase(args) {
|
|
|
10733
10863
|
newTargets = args.targets;
|
|
10734
10864
|
}
|
|
10735
10865
|
}
|
|
10866
|
+
const recoveryHasHistoryHead = m0M1EnabledForFold || (args.pendingCompartmentInjection?.compartmentCount ?? 0) > 0;
|
|
10867
|
+
const recoveryHeadChangedThisPass = !prefixPreflightFailed && (foldChangesHeadThisPass || !firstRenderBust && publishedM1RefreshedThisPass || !m0M1EnabledForFold && legacyPublishedHeadChanged(args));
|
|
10868
|
+
const recoveryHeadlessRebuild = !recoveryHasHistoryHead && (args.schedulerDecision === "execute" || isExplicitFlush || forceMaterialization || emergencyDropEligible);
|
|
10869
|
+
const recoveredShapeRewriteProof = recoveryHeadChangedThisPass ? "head_bytes" : recoveryExternalFoldCause ?? (recoveryHeadlessRebuild ? "headless_rebuild" : null);
|
|
10870
|
+
const canNormalizeRecoveredShape = isCacheBustingPass && !activeThinkingPrefixHold && recoveredShapeRewriteProof !== null;
|
|
10871
|
+
sessionLog2(args.sessionId, `rewrite permission: allowed=${isCacheBustingPass} ${reclaimRideLabel(rideSignals)} served_shape_repair=${servedShapeRepair.needsRebuild} signed_prefix_release=${rewritingSignedPrefix} fold_prefix_loss=${foldBustsServedPrefixThisPass} first_render=${firstRenderBust} idle_expiry=${idleExpiryRebuild} thinking_hold=${activeThinkingPrefixHold} recovery_has_head=${recoveryHasHistoryHead} recovery_head_changed=${recoveryHeadChangedThisPass} recovery_headless_rebuild=${recoveryHeadlessRebuild} proven_external_fold=${recoveryExternalFoldCause ?? "none"} recovered_shape_proof=${recoveredShapeRewriteProof ?? "none"}`);
|
|
10736
10872
|
let recoveryTailNormalizedThisPass = false;
|
|
10737
|
-
if (args.normalizeRecoveryTail &&
|
|
10873
|
+
if (args.normalizeRecoveryTail && canNormalizeRecoveredShape) {
|
|
10738
10874
|
args.normalizeRecoveryTail();
|
|
10739
10875
|
recoveryTailNormalizedThisPass = true;
|
|
10740
10876
|
args.tags = getTagsBySession2(args.db, args.sessionId);
|
|
@@ -11852,6 +11988,19 @@ async function runPostTransformPhase(args) {
|
|
|
11852
11988
|
if (reasoningRemovalEnabled)
|
|
11853
11989
|
removeReasoningParts(args.messages, removedReasoningIds, args.resolvedProviderID);
|
|
11854
11990
|
}
|
|
11991
|
+
const releaseRecoveryThisPass = canEditWholePrefix;
|
|
11992
|
+
if (releaseRecoveryThisPass)
|
|
11993
|
+
args.releaseRecoveryPrefix?.();
|
|
11994
|
+
sessionLog2(args.sessionId, `recovery projection: case=${canNormalizeRecoveredShape ? recoveredShapeRewriteProof : releaseRecoveryThisPass ? "conversation_only" : "defer"} released=${releaseRecoveryThisPass} normalized=${recoveryTailNormalizedThisPass}`);
|
|
11995
|
+
if (canUseEmptySentinels && args.opencode1) {
|
|
11996
|
+
projectStructuralSentinels({
|
|
11997
|
+
db: args.db,
|
|
11998
|
+
sessionId: args.sessionId,
|
|
11999
|
+
messages: args.messages,
|
|
12000
|
+
rebuilding: canNormalizeRecoveredShape && bustedThisPass,
|
|
12001
|
+
deferUpgrade: structuralSentinelUpgradeFailed
|
|
12002
|
+
});
|
|
12003
|
+
}
|
|
11855
12004
|
sessionLog2(args.sessionId, `final representation: clearedParts=${finalRepresentation.clearedParts} mergedReasoningParts=${finalRepresentation.mergedReasoningParts} removedReasoningParts=${removedReasoningParts} settledDroppedReasoning=${settledDroppedReasoning}`);
|
|
11856
12005
|
logTransformTiming(args.sessionId, "finalizeMessageRepresentation", tFinalRepresentation, `clearedParts=${finalRepresentation.clearedParts} mergedReasoningParts=${finalRepresentation.mergedReasoningParts}`);
|
|
11857
12006
|
const tTailBaseline = performance.now();
|
|
@@ -12715,6 +12864,44 @@ function createPassOutcome() {
|
|
|
12715
12864
|
};
|
|
12716
12865
|
}
|
|
12717
12866
|
|
|
12867
|
+
// src/hooks/magic-context/release-recovery-prefix.ts
|
|
12868
|
+
function groups(messages) {
|
|
12869
|
+
const result = new Map;
|
|
12870
|
+
for (const message of messages) {
|
|
12871
|
+
if (!message.info.id)
|
|
12872
|
+
continue;
|
|
12873
|
+
const group = result.get(message.info.id) ?? [];
|
|
12874
|
+
group.push(message);
|
|
12875
|
+
result.set(message.info.id, group);
|
|
12876
|
+
}
|
|
12877
|
+
return result;
|
|
12878
|
+
}
|
|
12879
|
+
function visibleGroup(group) {
|
|
12880
|
+
return JSON.stringify(group.map((message) => ({
|
|
12881
|
+
role: message.info.role,
|
|
12882
|
+
parts: message.parts.filter((part) => !isStructuralNoiseSentinel(part) && !(isRecord(part) && (part.type === "step-start" || part.type === "step-finish" || part.type === "meta"))).map(providerVisiblePart)
|
|
12883
|
+
})));
|
|
12884
|
+
}
|
|
12885
|
+
function snapshotRecoveryMessages(messages) {
|
|
12886
|
+
return new Map([...groups(messages)].map(([id, group]) => [id, visibleGroup(group)]));
|
|
12887
|
+
}
|
|
12888
|
+
function releaseRecoveryPrefix(args) {
|
|
12889
|
+
const frozen = groups(args.frozen);
|
|
12890
|
+
const rendered = groups(args.after);
|
|
12891
|
+
const emitted = new Set;
|
|
12892
|
+
return args.after.flatMap((message) => {
|
|
12893
|
+
const id = message.info.id;
|
|
12894
|
+
if (!id)
|
|
12895
|
+
return [message];
|
|
12896
|
+
if (emitted.has(id))
|
|
12897
|
+
return [];
|
|
12898
|
+
emitted.add(id);
|
|
12899
|
+
const group = rendered.get(id) ?? [message];
|
|
12900
|
+
const previous = frozen.get(id);
|
|
12901
|
+
return previous && args.before.get(id) === visibleGroup(group) ? previous : group;
|
|
12902
|
+
});
|
|
12903
|
+
}
|
|
12904
|
+
|
|
12718
12905
|
// src/hooks/magic-context/rust-mode-transform.ts
|
|
12719
12906
|
import { createHash as createHash5, randomUUID as randomUUID2 } from "node:crypto";
|
|
12720
12907
|
|
|
@@ -15394,6 +15581,32 @@ function releaseRustServedRaw(db, sessionId, output, rebuiltIdentity) {
|
|
|
15394
15581
|
}
|
|
15395
15582
|
|
|
15396
15583
|
// src/hooks/magic-context/transform-context-state.ts
|
|
15584
|
+
var usageRefusals = new WeakMap;
|
|
15585
|
+
function recordUsageRefusal(db, sessionId, refusedAt = Date.now()) {
|
|
15586
|
+
let sessions = usageRefusals.get(db);
|
|
15587
|
+
if (!sessions) {
|
|
15588
|
+
sessions = new Map;
|
|
15589
|
+
usageRefusals.set(db, sessions);
|
|
15590
|
+
}
|
|
15591
|
+
if (!sessions.has(sessionId))
|
|
15592
|
+
sessions.set(sessionId, refusedAt);
|
|
15593
|
+
}
|
|
15594
|
+
function shouldResetRefusedUsage(db, sessionId, meta, liveReading) {
|
|
15595
|
+
const refusals = usageRefusals.get(db);
|
|
15596
|
+
const refusedAt = refusals?.get(sessionId);
|
|
15597
|
+
if (refusedAt === undefined)
|
|
15598
|
+
return false;
|
|
15599
|
+
const readingAt = liveReading?.hasUsageTokens ? liveReading.updatedAt : meta.lastResponseTime;
|
|
15600
|
+
if (readingAt > refusedAt) {
|
|
15601
|
+
refusals?.delete(sessionId);
|
|
15602
|
+
return false;
|
|
15603
|
+
}
|
|
15604
|
+
const now = Date.now();
|
|
15605
|
+
const foldedAt = meta.cachedM0MaterializedAt ?? 0;
|
|
15606
|
+
if (foldedAt > refusedAt && foldedAt <= now)
|
|
15607
|
+
return true;
|
|
15608
|
+
return !!db.prepare("SELECT 1 FROM compartments WHERE session_id = ? AND harness = ? AND created_at > ? AND created_at <= ? LIMIT 1").get(sessionId, getHarness2(), refusedAt, now);
|
|
15609
|
+
}
|
|
15397
15610
|
function loadPersistedUsageWatermark(db, sessionId) {
|
|
15398
15611
|
const result = db.prepare("SELECT last_response_time FROM session_meta WHERE session_id = ?").get(sessionId);
|
|
15399
15612
|
if (result === null || typeof result !== "object")
|
|
@@ -19335,6 +19548,7 @@ function createTransform(deps) {
|
|
|
19335
19548
|
return;
|
|
19336
19549
|
throw degradedPassError(site, error);
|
|
19337
19550
|
};
|
|
19551
|
+
const structuralSentinelUpgradeFailed = deps.storeGeneration === "v1" && !deps.compactionOff && deps.transformMode !== "rust" && prepareStructuralSentinelChoices({ db, sessionId, messages }).structuralSentinelUpgrade === undefined;
|
|
19338
19552
|
if (deps.storeGeneration !== undefined) {
|
|
19339
19553
|
try {
|
|
19340
19554
|
if (rustModeTransform && readCoordinateGeneration(db, sessionId) !== deps.storeGeneration) {
|
|
@@ -19556,10 +19770,13 @@ function createTransform(deps) {
|
|
|
19556
19770
|
loadedSessions.add(sessionId);
|
|
19557
19771
|
const earlyStateSnapshot = loadTransformPassStateSnapshot(db, sessionId);
|
|
19558
19772
|
const historianFailureState = earlyStateSnapshot.historianFailure;
|
|
19559
|
-
|
|
19773
|
+
const liveUsageBeforeReset = deps.contextUsageMap.get(sessionId);
|
|
19774
|
+
const usageResetAfterRefusal = shouldResetRefusedUsage(db, sessionId, sessionMeta, liveUsageBeforeReset);
|
|
19775
|
+
if ((isFirstTransformPassForSession || usageResetAfterRefusal) && sessionMeta) {
|
|
19560
19776
|
const persistedPct = sessionMeta.lastContextPercentage ?? 0;
|
|
19561
|
-
if (persistedPct > 0) {
|
|
19562
|
-
|
|
19777
|
+
if (persistedPct > 0 || usageResetAfterRefusal && (sessionMeta.lastInputTokens > 0 || deps.contextUsageMap.has(sessionId))) {
|
|
19778
|
+
const resetPct = persistedPct || liveUsageBeforeReset?.usage.percentage || 0;
|
|
19779
|
+
sessionLog2(sessionId, `transform: ${isFirstTransformPassForSession ? "first pass" : "refused turn recovery"} reset — percentage=${resetPct.toFixed(1)}% — clearing stale usage state`);
|
|
19563
19780
|
updateSessionMeta(db, sessionId, {
|
|
19564
19781
|
lastContextPercentage: 0,
|
|
19565
19782
|
lastInputTokens: 0
|
|
@@ -19968,10 +20185,15 @@ Historian previously failed ${historianFailureState.failureCount} time(s), so Ma
|
|
|
19968
20185
|
let taggingSucceeded = false;
|
|
19969
20186
|
let recoveryBatch = null;
|
|
19970
20187
|
let recoveryNormalized = false;
|
|
20188
|
+
let recoveryReleased = false;
|
|
19971
20189
|
const untaggedReplayTailServed = !!recovery.prefix || untaggedReplayIds.size > 0 && messages.some((message) => untaggedReplayIds.has(message.info.id ?? ""));
|
|
20190
|
+
const releaseRecoveryReplay = untaggedReplayTailServed ? () => {
|
|
20191
|
+
recoveryReleased = true;
|
|
20192
|
+
} : undefined;
|
|
19972
20193
|
const normalizeRecoveryTail = untaggedReplayTailServed ? () => {
|
|
19973
20194
|
if (recoveryNormalized)
|
|
19974
20195
|
return;
|
|
20196
|
+
recoveryReleased = true;
|
|
19975
20197
|
const tail = messages.filter((message) => untaggedReplayIds.has(message.info.id ?? ""));
|
|
19976
20198
|
const tagged = tagMessages(sessionId, tail, deps.tagger, db, {
|
|
19977
20199
|
skipPrefixInjection: !ctxReduceCallable,
|
|
@@ -20047,6 +20269,7 @@ Historian previously failed ${historianFailureState.failureCount} time(s), so Ma
|
|
|
20047
20269
|
const targetTagNumbers = [...targets.keys()];
|
|
20048
20270
|
const targetsSliceTags = compactionOff ? [] : getDroppedTagsByNumbers2(db, sessionId, targetTagNumbers);
|
|
20049
20271
|
logTransformTiming(sessionId, "getDroppedTagsByNumbers", t1b, `targets=${targetTagNumbers.length} fetched=${targetsSliceTags.length}`);
|
|
20272
|
+
const recoveryBeforePostprocess = recovery.prefix ? snapshotRecoveryMessages(messages) : undefined;
|
|
20050
20273
|
let didMutateFromFlushedStatuses = false;
|
|
20051
20274
|
if (taggingSucceeded) {
|
|
20052
20275
|
try {
|
|
@@ -20191,7 +20414,7 @@ Historian previously failed ${historianFailureState.failureCount} time(s), so Ma
|
|
|
20191
20414
|
let contextUsageFromEstimate = hostOverWindow;
|
|
20192
20415
|
if (contextUsage.inputTokens <= 0 && protectionCacheBustingPass) {
|
|
20193
20416
|
const overflowRecoveryPass = usagePercentageSynthetic || emergencyRecoveryArmed;
|
|
20194
|
-
const savedProviderReading = overflowRecoveryPass ? null : savedProviderReadingForPass({
|
|
20417
|
+
const savedProviderReading = overflowRecoveryPass || usageResetAfterRefusal ? null : savedProviderReadingForPass({
|
|
20195
20418
|
persisted: persistedUsageBeforeResets,
|
|
20196
20419
|
currentModelKey: currentModelKeyForBoundary ?? (hardModelKey || undefined),
|
|
20197
20420
|
contextLimit: resolvedContextLimit,
|
|
@@ -20258,7 +20481,10 @@ Historian previously failed ${historianFailureState.failureCount} time(s), so Ma
|
|
|
20258
20481
|
const tPostProcess = performance.now();
|
|
20259
20482
|
dropProjectionEmergency = contextUsage.percentage >= 95;
|
|
20260
20483
|
const postTransformResult = await runPostTransformPhase({
|
|
20484
|
+
opencode1: deps.storeGeneration === "v1",
|
|
20261
20485
|
opencode2: deps.storeGeneration === "v2",
|
|
20486
|
+
structuralSentinelUpgradeFailed,
|
|
20487
|
+
releaseRecoveryPrefix: releaseRecoveryReplay,
|
|
20262
20488
|
normalizeRecoveryTail,
|
|
20263
20489
|
compactionMarkerStrategy: deps.compactionMarkerStrategy ?? defaultCompactionMarkerStrategy,
|
|
20264
20490
|
sessionId,
|
|
@@ -20353,7 +20579,17 @@ Historian previously failed ${historianFailureState.failureCount} time(s), so Ma
|
|
|
20353
20579
|
}
|
|
20354
20580
|
});
|
|
20355
20581
|
compactionModeTransitionPending.delete(sessionId);
|
|
20356
|
-
if (recovery.prefix && !recoveryNormalized) {
|
|
20582
|
+
if (recovery.prefix && recoveryReleased && !recoveryNormalized) {
|
|
20583
|
+
if (!recoveryBeforePostprocess)
|
|
20584
|
+
throw new Error("Cannot release recovery without its pre-edit snapshot");
|
|
20585
|
+
const released = releaseRecoveryPrefix({
|
|
20586
|
+
frozen: JSON.parse(recovery.prefix.json),
|
|
20587
|
+
before: recoveryBeforePostprocess,
|
|
20588
|
+
after: messages
|
|
20589
|
+
});
|
|
20590
|
+
messages.splice(0, messages.length, ...released);
|
|
20591
|
+
}
|
|
20592
|
+
if (recovery.prefix && !recoveryReleased) {
|
|
20357
20593
|
const renderedById = new Map;
|
|
20358
20594
|
for (const message of messages) {
|
|
20359
20595
|
if (!message.info.id)
|
|
@@ -20365,7 +20601,7 @@ Historian previously failed ${historianFailureState.failureCount} time(s), so Ma
|
|
|
20365
20601
|
const suffix = recoveryTailSources.flatMap(({ identity, pristineSystem }) => pristineSystem ? [pristineSystem] : renderedById.get(identity) ?? []);
|
|
20366
20602
|
messages.splice(0, messages.length, ...JSON.parse(recovery.prefix.json), ...suffix);
|
|
20367
20603
|
}
|
|
20368
|
-
const recoveryReleaseReason = recovery.rebuildReason ?? (recoveryNormalized ? "recovery_tail_normalized" : undefined);
|
|
20604
|
+
const recoveryReleaseReason = recovery.rebuildReason ?? (recoveryNormalized ? "recovery_tail_normalized" : recoveryReleased ? "recovery_prefix_released" : undefined);
|
|
20369
20605
|
if (recoveryReleaseReason)
|
|
20370
20606
|
postTransformResult.bustedThisPass = true;
|
|
20371
20607
|
passOutcome.markFinalized();
|
|
@@ -20500,6 +20736,7 @@ Historian previously failed ${historianFailureState.failureCount} time(s), so Ma
|
|
|
20500
20736
|
}
|
|
20501
20737
|
try {
|
|
20502
20738
|
await host.hostRefuse(deps.client, sessionId);
|
|
20739
|
+
recordUsageRefusal(db, sessionId);
|
|
20503
20740
|
} catch (error) {
|
|
20504
20741
|
sessionLog2(sessionId, "transform: emergency fail-closed abort failed; refusing to return a sendable prompt:", getErrorMessage(error));
|
|
20505
20742
|
throw new EmergencyFailClosedError("Emergency recovery abort failed", {
|
|
@@ -20747,7 +20984,16 @@ Historian previously failed ${historianFailureState.failureCount} time(s), so Ma
|
|
|
20747
20984
|
});
|
|
20748
20985
|
deps.onPassPermission?.(sessionId, postTransformResult.prefixMutationPermitted);
|
|
20749
20986
|
};
|
|
20750
|
-
const timedTransform = (input, output) => withTransformTimingSummary(() =>
|
|
20987
|
+
const timedTransform = (input, output) => withTransformTimingSummary(async () => {
|
|
20988
|
+
const sessionId = findSessionId(output.messages);
|
|
20989
|
+
try {
|
|
20990
|
+
await transform(input, output);
|
|
20991
|
+
} catch (error) {
|
|
20992
|
+
if (sessionId && !deps.compactionOff && error instanceof EmergencyFailClosedError)
|
|
20993
|
+
recordUsageRefusal(deps.db, sessionId);
|
|
20994
|
+
throw error;
|
|
20995
|
+
}
|
|
20996
|
+
});
|
|
20751
20997
|
return Object.assign(timedTransform, {
|
|
20752
20998
|
clearSessionLogState,
|
|
20753
20999
|
invalidateRustWireState(sessionId) {
|
|
@@ -22433,6 +22679,78 @@ function computeOpenCodeWorkMetricsIncremental(openCodeDb, sessionId, carry, gen
|
|
|
22433
22679
|
return { carry, metrics: metricsFromCarry(view) };
|
|
22434
22680
|
}
|
|
22435
22681
|
|
|
22682
|
+
// src/features/magic-context/work-metrics-carry-storage.ts
|
|
22683
|
+
var STATE_KEY = "magicContextWorkMetrics";
|
|
22684
|
+
var storedCarry = object({
|
|
22685
|
+
version: literal(1),
|
|
22686
|
+
source: string(),
|
|
22687
|
+
newWorkSum: number(),
|
|
22688
|
+
lastTimeCreated: number(),
|
|
22689
|
+
lastId: string(),
|
|
22690
|
+
perAgent: array(tuple([
|
|
22691
|
+
string(),
|
|
22692
|
+
object({
|
|
22693
|
+
prevPrompt: number(),
|
|
22694
|
+
phaseId: number(),
|
|
22695
|
+
phasePeak: number(),
|
|
22696
|
+
phaseHasQualifying: boolean(),
|
|
22697
|
+
closedPhaseSum: number(),
|
|
22698
|
+
lastOutput: number(),
|
|
22699
|
+
seen: boolean()
|
|
22700
|
+
})
|
|
22701
|
+
]))
|
|
22702
|
+
});
|
|
22703
|
+
function readState2(db, sessionId) {
|
|
22704
|
+
const row = db.prepare("SELECT deferred_execute_state, new_work_tokens, total_input_tokens FROM session_meta WHERE session_id = ?").get(sessionId);
|
|
22705
|
+
let root = {};
|
|
22706
|
+
try {
|
|
22707
|
+
const value = JSON.parse(row?.deferred_execute_state ?? "null");
|
|
22708
|
+
if (value !== null && typeof value === "object" && !Array.isArray(value))
|
|
22709
|
+
root = value;
|
|
22710
|
+
} catch {}
|
|
22711
|
+
return { root, newWorkTokens: row?.new_work_tokens, totalInputTokens: row?.total_input_tokens };
|
|
22712
|
+
}
|
|
22713
|
+
function matchesStoredState(state, source, carry, metrics) {
|
|
22714
|
+
const parsed = storedCarry.safeParse(state.root[STATE_KEY]);
|
|
22715
|
+
if (!parsed.success)
|
|
22716
|
+
return false;
|
|
22717
|
+
const previous = parsed.data;
|
|
22718
|
+
if (previous.source !== source || previous.newWorkSum !== carry.newWorkSum || previous.lastTimeCreated !== carry.lastTimeCreated || previous.lastId !== carry.lastId || previous.perAgent.length !== carry.perAgent.size || state.newWorkTokens !== metrics.newWorkTokens || state.totalInputTokens !== metrics.totalInputTokens)
|
|
22719
|
+
return false;
|
|
22720
|
+
return previous.perAgent.every(([key, agent]) => {
|
|
22721
|
+
const current = carry.perAgent.get(key);
|
|
22722
|
+
return current !== undefined && agent.prevPrompt === current.prevPrompt && agent.phaseId === current.phaseId && agent.phasePeak === current.phasePeak && agent.phaseHasQualifying === current.phaseHasQualifying && agent.closedPhaseSum === current.closedPhaseSum && agent.lastOutput === current.lastOutput && agent.seen === current.seen;
|
|
22723
|
+
});
|
|
22724
|
+
}
|
|
22725
|
+
function readSessionWorkMetricsCarry(db, sessionId, source) {
|
|
22726
|
+
const parsed = storedCarry.safeParse(readState2(db, sessionId).root[STATE_KEY]);
|
|
22727
|
+
if (!parsed.success || parsed.data.source !== source)
|
|
22728
|
+
return;
|
|
22729
|
+
return {
|
|
22730
|
+
perAgent: new Map(parsed.data.perAgent),
|
|
22731
|
+
newWorkSum: parsed.data.newWorkSum,
|
|
22732
|
+
lastTimeCreated: parsed.data.lastTimeCreated,
|
|
22733
|
+
lastId: parsed.data.lastId
|
|
22734
|
+
};
|
|
22735
|
+
}
|
|
22736
|
+
function setSessionWorkMetricsCarry(db, sessionId, source, carry, metrics) {
|
|
22737
|
+
const normalized = {
|
|
22738
|
+
newWorkTokens: Math.max(0, Math.floor(metrics.newWorkTokens)),
|
|
22739
|
+
totalInputTokens: Math.max(0, Math.floor(metrics.totalInputTokens))
|
|
22740
|
+
};
|
|
22741
|
+
if (matchesStoredState(readState2(db, sessionId), source, carry, normalized))
|
|
22742
|
+
return;
|
|
22743
|
+
db.transaction(() => {
|
|
22744
|
+
const state = readState2(db, sessionId);
|
|
22745
|
+
if (matchesStoredState(state, source, carry, normalized))
|
|
22746
|
+
return;
|
|
22747
|
+
ensureSessionMetaRow(db, sessionId);
|
|
22748
|
+
const { root } = state;
|
|
22749
|
+
root[STATE_KEY] = { ...carry, version: 1, source, perAgent: [...carry.perAgent] };
|
|
22750
|
+
db.prepare("UPDATE session_meta SET deferred_execute_state = ?, new_work_tokens = ?, total_input_tokens = ? WHERE session_id = ?").run(JSON.stringify(root), normalized.newWorkTokens, normalized.totalInputTokens, sessionId);
|
|
22751
|
+
}).immediate();
|
|
22752
|
+
}
|
|
22753
|
+
|
|
22436
22754
|
// src/plugin/embedding-bootstrap.ts
|
|
22437
22755
|
import { readFileSync, statSync } from "node:fs";
|
|
22438
22756
|
|
|
@@ -23060,7 +23378,6 @@ function pluginPackageVersion(moduleUrl = import.meta.url) {
|
|
|
23060
23378
|
}
|
|
23061
23379
|
|
|
23062
23380
|
// src/plugin/rpc-handlers.ts
|
|
23063
|
-
var workMetricsCarryBySession = new Map;
|
|
23064
23381
|
async function executeRustRecompRpc(moduleClient, sessionId, projectRoot) {
|
|
23065
23382
|
if (!moduleClient)
|
|
23066
23383
|
return { ok: false, error: "Rust module client is unavailable" };
|
|
@@ -23100,12 +23417,11 @@ function resolveSidebarWorkMetrics(db, sessionId, persistedNewWork, persistedTot
|
|
|
23100
23417
|
return { newWorkTokens: persistedNewWork, totalInputTokens: persistedTotalInput };
|
|
23101
23418
|
}
|
|
23102
23419
|
try {
|
|
23103
|
-
const
|
|
23104
|
-
const carry =
|
|
23420
|
+
const source = `${generation}:${resolveOpenCodeDbPath(generation).path}`;
|
|
23421
|
+
const carry = readSessionWorkMetricsCarry(db, sessionId, source) ?? emptyWorkMetricsCarry();
|
|
23105
23422
|
const { carry: nextCarry, metrics } = withReadOnlySessionDb((openCodeDb) => computeOpenCodeWorkMetricsIncremental(openCodeDb, sessionId, carry, generation), generation);
|
|
23106
|
-
workMetricsCarryBySession.set(carryKey, nextCarry);
|
|
23107
23423
|
try {
|
|
23108
|
-
|
|
23424
|
+
setSessionWorkMetricsCarry(db, sessionId, source, nextCarry, metrics);
|
|
23109
23425
|
} catch {}
|
|
23110
23426
|
return metrics;
|
|
23111
23427
|
} catch {
|
|
@@ -24004,8 +24320,8 @@ function registerRpcHandlers(rpcServer, args) {
|
|
|
24004
24320
|
const db = readDatabase();
|
|
24005
24321
|
if (!db)
|
|
24006
24322
|
return { ok: false, error: "db unavailable" };
|
|
24007
|
-
const { runManagedRecomp } = await importPluginModule(() => import("./recomp-orchestrator-
|
|
24008
|
-
const { sendIgnoredMessage } = await importPluginModule(() => import("./send-session-notification-
|
|
24323
|
+
const { runManagedRecomp } = await importPluginModule(() => import("./recomp-orchestrator-9cjk6aej.js"));
|
|
24324
|
+
const { sendIgnoredMessage } = await importPluginModule(() => import("./send-session-notification-kxp47tsw.js"));
|
|
24009
24325
|
log2(`[rpc] recomp requested for session ${sessionId}`);
|
|
24010
24326
|
if (!args.client && !args.hiddenCompletionExecutor) {
|
|
24011
24327
|
return {
|
|
@@ -24089,7 +24405,7 @@ function registerRpcHandlers(rpcServer, args) {
|
|
|
24089
24405
|
const db = readDatabase();
|
|
24090
24406
|
if (!db)
|
|
24091
24407
|
return { ok: false, error: "db unavailable" };
|
|
24092
|
-
const { runManagedWrapup } = await importPluginModule(() => import("./wrapup-orchestrator-
|
|
24408
|
+
const { runManagedWrapup } = await importPluginModule(() => import("./wrapup-orchestrator-j1v62bkj.js"));
|
|
24093
24409
|
const model = liveSessionState.liveModelBySession.get(sessionId);
|
|
24094
24410
|
const contextLimit = model ? resolveContextLimit(model.providerID, model.modelID, { db, sessionID: sessionId }) : 128000;
|
|
24095
24411
|
const ctx = {
|
|
@@ -25158,6 +25474,7 @@ class HostFoldIdle {
|
|
|
25158
25474
|
now;
|
|
25159
25475
|
timers;
|
|
25160
25476
|
windows = new Map;
|
|
25477
|
+
attempted = new Map;
|
|
25161
25478
|
constructor(fold, now = Date.now, timers = { setTimeout, clearTimeout }) {
|
|
25162
25479
|
this.fold = fold;
|
|
25163
25480
|
this.now = now;
|
|
@@ -25179,6 +25496,9 @@ class HostFoldIdle {
|
|
|
25179
25496
|
}
|
|
25180
25497
|
if (!Number.isFinite(ttl) || lastResponseTime <= 0)
|
|
25181
25498
|
return;
|
|
25499
|
+
const attempted = this.attempted.get(sessionID);
|
|
25500
|
+
if (attempted?.cacheTtl === cacheTtl && attempted.lastResponseTime === lastResponseTime)
|
|
25501
|
+
return;
|
|
25182
25502
|
const window = { cacheTtl, lastResponseTime };
|
|
25183
25503
|
this.windows.set(sessionID, window);
|
|
25184
25504
|
const eligible = () => this.windows.get(sessionID) === window && computeHardCacheExpired(window.cacheTtl, window.lastResponseTime, this.now());
|
|
@@ -25187,6 +25507,7 @@ class HostFoldIdle {
|
|
|
25187
25507
|
return;
|
|
25188
25508
|
if (eligible()) {
|
|
25189
25509
|
window.timer = undefined;
|
|
25510
|
+
this.attempted.set(sessionID, { cacheTtl, lastResponseTime });
|
|
25190
25511
|
const release = () => {
|
|
25191
25512
|
if (this.windows.get(sessionID) === window)
|
|
25192
25513
|
this.windows.delete(sessionID);
|
|
@@ -25203,9 +25524,37 @@ class HostFoldIdle {
|
|
|
25203
25524
|
dispose() {
|
|
25204
25525
|
for (const sessionID of this.windows.keys())
|
|
25205
25526
|
this.active(sessionID);
|
|
25527
|
+
this.attempted.clear();
|
|
25206
25528
|
}
|
|
25207
25529
|
}
|
|
25208
25530
|
|
|
25531
|
+
// src/v2/fold/manual-baseline.ts
|
|
25532
|
+
function stageManualBaseline(db, sessionID, render) {
|
|
25533
|
+
const read = () => db.prepare("SELECT * FROM session_meta WHERE session_id = ?").get(sessionID);
|
|
25534
|
+
const before = read();
|
|
25535
|
+
const columns = Object.keys(before).filter((key) => key.startsWith("cached_m0_") || key.startsWith("cached_m1_") || key === "memory_block_count" || key === "memory_block_ids");
|
|
25536
|
+
const update = db.prepare(`UPDATE session_meta SET ${columns.map((key) => `${key} = ?`).join(", ")} WHERE session_id = ?`);
|
|
25537
|
+
const restore = (row) => update.run(...columns.map((key) => row[key] ?? null), sessionID);
|
|
25538
|
+
let summary;
|
|
25539
|
+
let staged;
|
|
25540
|
+
try {
|
|
25541
|
+
summary = render();
|
|
25542
|
+
staged = read();
|
|
25543
|
+
} finally {
|
|
25544
|
+
restore(before);
|
|
25545
|
+
}
|
|
25546
|
+
const commit = db.prepare(`UPDATE session_meta SET ${columns.map((key) => `${key} = ?`).join(", ")} WHERE session_id = ? AND ${columns.map((key) => `${key} IS ?`).join(" AND ")}`);
|
|
25547
|
+
return {
|
|
25548
|
+
summary,
|
|
25549
|
+
boundaryID: staged.cached_m0_last_baseline_end_message_id,
|
|
25550
|
+
commit() {
|
|
25551
|
+
const result = commit.run(...columns.map((key) => staged[key] ?? null), sessionID, ...columns.map((key) => before[key] ?? null));
|
|
25552
|
+
if (result.changes !== 1)
|
|
25553
|
+
throw new Error("Manual compaction baseline changed during admission");
|
|
25554
|
+
}
|
|
25555
|
+
};
|
|
25556
|
+
}
|
|
25557
|
+
|
|
25209
25558
|
// src/v2/fold/memory-cache.ts
|
|
25210
25559
|
import { randomUUID as randomUUID4 } from "node:crypto";
|
|
25211
25560
|
|
|
@@ -25992,6 +26341,30 @@ function adaptPayload(draft, admittedIDs = new Set, messageTimes = new Map, hidd
|
|
|
25992
26341
|
};
|
|
25993
26342
|
}
|
|
25994
26343
|
|
|
26344
|
+
// src/v2/fold/code-mode-catalog.ts
|
|
26345
|
+
var changed = "The Code Mode tool catalog has changed.";
|
|
26346
|
+
var replacement = `${changed} This catalog supersedes the previous Code Mode tool catalog.`;
|
|
26347
|
+
function codeModeCatalogKind(data) {
|
|
26348
|
+
const text = data.text ?? "";
|
|
26349
|
+
const metadata = data.metadata;
|
|
26350
|
+
const keyed = typeof metadata === "object" && metadata !== null && "instructionSources" in metadata;
|
|
26351
|
+
if (keyed) {
|
|
26352
|
+
const sources = metadata.instructionSources;
|
|
26353
|
+
if (!Array.isArray(sources) || !sources.includes("core/codemode"))
|
|
26354
|
+
return;
|
|
26355
|
+
if (sources.length !== 1)
|
|
26356
|
+
return text.startsWith(replacement) ? "combined-replacement" : undefined;
|
|
26357
|
+
}
|
|
26358
|
+
if (text.startsWith(replacement))
|
|
26359
|
+
return "replacement";
|
|
26360
|
+
if (text.startsWith(changed))
|
|
26361
|
+
return "incremental";
|
|
26362
|
+
if (keyed || text.startsWith(`# Code Mode
|
|
26363
|
+
`) || text.startsWith("No Code Mode tools are currently available."))
|
|
26364
|
+
return "initial";
|
|
26365
|
+
return;
|
|
26366
|
+
}
|
|
26367
|
+
|
|
25995
26368
|
// src/v2/fold/native-codec.ts
|
|
25996
26369
|
class NativeReplayUnavailable extends Error {
|
|
25997
26370
|
}
|
|
@@ -26040,19 +26413,32 @@ var efforts = new Set(["none", "minimal", "low", "medium", "high", "xhigh", "max
|
|
|
26040
26413
|
|
|
26041
26414
|
class ControlMetadataUnavailable extends Error {
|
|
26042
26415
|
}
|
|
26043
|
-
var
|
|
26416
|
+
var object2 = (value) => !!value && typeof value === "object" && !Array.isArray(value);
|
|
26044
26417
|
var keys = (value, allowed) => Object.keys(value).every((key) => allowed.includes(key));
|
|
26045
26418
|
var identity = (value) => typeof value === "string" && value.length > 0 && Buffer.byteLength(value) <= 1024;
|
|
26046
26419
|
var digest = (value) => typeof value === "string" && /^[a-f0-9]{64}$/.test(value);
|
|
26047
26420
|
function validReplayState(value) {
|
|
26048
|
-
if (!
|
|
26421
|
+
if (!object2(value) || !keys(value, [
|
|
26422
|
+
"floor",
|
|
26423
|
+
"controls",
|
|
26424
|
+
"blocked",
|
|
26425
|
+
"upgrade",
|
|
26426
|
+
"withoutRecentContext",
|
|
26427
|
+
"retiredInstructionThrough",
|
|
26428
|
+
"retiredInstructionIDs"
|
|
26429
|
+
]))
|
|
26049
26430
|
return false;
|
|
26050
26431
|
const floor = value.floor;
|
|
26051
26432
|
const upgrade = value.upgrade;
|
|
26052
|
-
return
|
|
26433
|
+
return object2(floor) && keys(floor, ["kind", "id", "seq", "after", "digest"]) && ["start", "summary", "checkpoint", "host-checkpoint"].includes(floor.kind) && identity(floor.id) && typeof floor.seq === "number" && typeof floor.after === "number" && Number.isSafeInteger(floor.seq) && Number.isSafeInteger(floor.after) && floor.after === (floor.kind === "start" || floor.kind === "host-checkpoint" ? floor.seq - 1 : floor.seq) && digest(floor.digest) && validControls(value.controls) && (value.blocked === undefined || typeof value.blocked === "boolean") && (value.withoutRecentContext === undefined || typeof value.withoutRecentContext === "boolean") && (value.retiredInstructionThrough === undefined || Number.isSafeInteger(value.retiredInstructionThrough) && value.retiredInstructionThrough >= 0) && (value.retiredInstructionIDs === undefined || Array.isArray(value.retiredInstructionIDs) && value.retiredInstructionIDs.every(identity)) && (upgrade === undefined || object2(upgrade) && keys(upgrade, ["admissionID", "mode"]) && identity(upgrade.admissionID) && ["bounded", "host-checkpoint", "source-repair"].includes(upgrade.mode));
|
|
26434
|
+
}
|
|
26435
|
+
function instructionRetirement(replay) {
|
|
26436
|
+
const ids = new Set(replay?.retiredInstructionIDs);
|
|
26437
|
+
const through = replay?.retiredInstructionThrough ?? -1;
|
|
26438
|
+
return (row) => row.type === "system" && (row.seq <= through || ids.has(row.id));
|
|
26053
26439
|
}
|
|
26054
26440
|
function validAdmissionV3(value) {
|
|
26055
|
-
if (!
|
|
26441
|
+
if (!object2(value) || !keys(value, ["version", "proof", "replay"]) || value.version !== 3 || !object2(value.proof))
|
|
26056
26442
|
return false;
|
|
26057
26443
|
const proof = value.proof;
|
|
26058
26444
|
return keys(proof, [
|
|
@@ -26069,7 +26455,7 @@ function encodeAdmissionV3(value) {
|
|
|
26069
26455
|
return JSON.stringify(value);
|
|
26070
26456
|
}
|
|
26071
26457
|
function validCapturedReplayV3(value) {
|
|
26072
|
-
return
|
|
26458
|
+
return object2(value) && keys(value, ["version", "sessionID", "sourceDigest", "through", "replay"]) && value.version === 3 && identity(value.sessionID) && digest(value.sourceDigest) && Number.isSafeInteger(value.through) && validReplayState(value.replay);
|
|
26073
26459
|
}
|
|
26074
26460
|
function floorFor(row, kind) {
|
|
26075
26461
|
return {
|
|
@@ -26208,11 +26594,17 @@ function controlsThrough(controls, reader, sessionID, through) {
|
|
|
26208
26594
|
|
|
26209
26595
|
// src/v2/fold/native-replay.ts
|
|
26210
26596
|
var coordinates = new WeakMap;
|
|
26597
|
+
var sequences = new WeakMap;
|
|
26598
|
+
function nativeRowSequence(message) {
|
|
26599
|
+
return sequences.get(message);
|
|
26600
|
+
}
|
|
26211
26601
|
function nativeRowID(message) {
|
|
26212
26602
|
return coordinates.get(message) ?? message.id;
|
|
26213
26603
|
}
|
|
26214
|
-
function bindNativeRowID(message, id) {
|
|
26604
|
+
function bindNativeRowID(message, id, seq) {
|
|
26215
26605
|
coordinates.set(message, id);
|
|
26606
|
+
if (seq !== undefined)
|
|
26607
|
+
sequences.set(message, seq);
|
|
26216
26608
|
}
|
|
26217
26609
|
function copyNativeInput(messages) {
|
|
26218
26610
|
const copies = reviveNative(encodeNative(messages));
|
|
@@ -26221,6 +26613,9 @@ function copyNativeInput(messages) {
|
|
|
26221
26613
|
const id = original && nativeRowID(original);
|
|
26222
26614
|
if (id !== undefined)
|
|
26223
26615
|
coordinates.set(copy, id);
|
|
26616
|
+
const seq = original && sequences.get(original);
|
|
26617
|
+
if (seq !== undefined)
|
|
26618
|
+
sequences.set(copy, seq);
|
|
26224
26619
|
});
|
|
26225
26620
|
return copies;
|
|
26226
26621
|
}
|
|
@@ -26332,6 +26727,48 @@ class NativeFoldReplay {
|
|
|
26332
26727
|
async baseline(sessionID) {
|
|
26333
26728
|
return this.tail(sessionID)?.baseline;
|
|
26334
26729
|
}
|
|
26730
|
+
async retiredInstructionThrough(sessionID, reader) {
|
|
26731
|
+
if (!reader.latestCompaction(sessionID))
|
|
26732
|
+
return -1;
|
|
26733
|
+
return (await this.floorState(sessionID, reader))?.retiredInstructionThrough ?? -1;
|
|
26734
|
+
}
|
|
26735
|
+
async instructionRetirement(sessionID, reader) {
|
|
26736
|
+
if (!reader.latestCompaction(sessionID))
|
|
26737
|
+
return instructionRetirement();
|
|
26738
|
+
return instructionRetirement(await this.floorState(sessionID, reader));
|
|
26739
|
+
}
|
|
26740
|
+
async prepareManualFold(sessionID, reader, boundaryID, summary) {
|
|
26741
|
+
const running = reader.latestRunningCompaction(sessionID);
|
|
26742
|
+
if (!running || this.options.protocol !== 3)
|
|
26743
|
+
throw new NativeReplayUnavailable("Manual compaction has no running host checkpoint");
|
|
26744
|
+
const previous = await this.floorState(sessionID, reader);
|
|
26745
|
+
if (this.unprovenControls.has(sessionID) || previous?.blocked)
|
|
26746
|
+
throw new NativeReplayUnavailable("Manual compaction controls have not been captured");
|
|
26747
|
+
const seq = boundaryID ? reader.sequenceForId(sessionID, boundaryID) : undefined;
|
|
26748
|
+
const row = seq === undefined ? undefined : reader.range(sessionID, seq - 1, seq)[0];
|
|
26749
|
+
const first = reader.page(sessionID, { limit: 1 }).rows[0];
|
|
26750
|
+
let floor = row ? floorFor(row, "summary") : undefined;
|
|
26751
|
+
floor ??= first ? floorFor(first, "start") : undefined;
|
|
26752
|
+
if (!floor)
|
|
26753
|
+
throw new NativeReplayUnavailable("Manual compaction has no history boundary");
|
|
26754
|
+
const instructions = reader.instructionRowsThrough(sessionID, running.seq - 1);
|
|
26755
|
+
const retiredInstructionIDs = [];
|
|
26756
|
+
let newerFullCatalog = false;
|
|
26757
|
+
for (const instruction of [...instructions].reverse()) {
|
|
26758
|
+
const text = instruction.data.text ?? "";
|
|
26759
|
+
const catalog = codeModeCatalogKind(instruction.data);
|
|
26760
|
+
if (instruction.seq > floor.after && (catalog && catalog !== "combined-replacement" && newerFullCatalog || text.length > 0 && summary.includes(text)))
|
|
26761
|
+
retiredInstructionIDs.push(instruction.id);
|
|
26762
|
+
if (catalog === "replacement" || catalog === "combined-replacement")
|
|
26763
|
+
newerFullCatalog = true;
|
|
26764
|
+
}
|
|
26765
|
+
return {
|
|
26766
|
+
floor,
|
|
26767
|
+
controls: previous?.controls ?? [],
|
|
26768
|
+
...floor.after >= 0 ? { retiredInstructionThrough: floor.after } : {},
|
|
26769
|
+
retiredInstructionIDs
|
|
26770
|
+
};
|
|
26771
|
+
}
|
|
26335
26772
|
admissionKey(sessionID) {
|
|
26336
26773
|
return this.options.protocol === 3 ? `native-fold/v3/${encodeURIComponent(sessionID)}` : admissionKey(sessionID);
|
|
26337
26774
|
}
|
|
@@ -26551,7 +26988,7 @@ ${cached}
|
|
|
26551
26988
|
return pending.rows.filter((row) => row.seq > pending.value.replay.floor.after).flatMap((row) => {
|
|
26552
26989
|
const messages = this.rendered(structuredClone(row), model);
|
|
26553
26990
|
for (const message of messages)
|
|
26554
|
-
|
|
26991
|
+
bindNativeRowID(message, row.id, row.seq);
|
|
26555
26992
|
return messages;
|
|
26556
26993
|
});
|
|
26557
26994
|
}
|
|
@@ -26778,6 +27215,7 @@ ${cached}
|
|
|
26778
27215
|
if (!isLocalCheckpoint(cut) || cut.session_id !== sessionID)
|
|
26779
27216
|
return;
|
|
26780
27217
|
let after = -1;
|
|
27218
|
+
const retired = instructionRetirement(replayOverride ?? this.tail(sessionID)?.replay);
|
|
26781
27219
|
if (this.options.protocol === 3) {
|
|
26782
27220
|
const replay = replayOverride ?? this.tail(sessionID)?.replay;
|
|
26783
27221
|
if (!replay || !validFloor(reader, sessionID, replay.floor))
|
|
@@ -26792,7 +27230,7 @@ ${cached}
|
|
|
26792
27230
|
}
|
|
26793
27231
|
const through = cut.seq - 1;
|
|
26794
27232
|
const metadata = (this.metadataThrough.get(sessionID) ?? -1) >= through ? this.metadata.get(sessionID) : undefined;
|
|
26795
|
-
const required = (metadata ?? reader.rowStampsThrough(sessionID, through, this.options.protocol === 3 ? after : undefined)).filter(conversational).filter((row) => row.seq > after && row.seq <= through);
|
|
27233
|
+
const required = (metadata ?? reader.rowStampsThrough(sessionID, through, this.options.protocol === 3 ? after : undefined)).filter(conversational).filter((row) => !retired(row)).filter((row) => row.seq > after && row.seq <= through);
|
|
26796
27234
|
const tail = this.tail(sessionID);
|
|
26797
27235
|
let records;
|
|
26798
27236
|
if (!this.dirty.has(sessionID) && tail?.model === `${model.providerID}/${model.id}` && this.storage.containsRows(sessionID, required.map((row) => row.id))) {
|
|
@@ -26803,10 +27241,10 @@ ${cached}
|
|
|
26803
27241
|
if (required.some((row) => !restored.has(row.id)))
|
|
26804
27242
|
return;
|
|
26805
27243
|
}
|
|
26806
|
-
const projected = records.flatMap((row) => {
|
|
27244
|
+
const projected = records.filter((row) => !retired(row)).flatMap((row) => {
|
|
26807
27245
|
const messages = reviveNative(JSON.parse(row.data));
|
|
26808
27246
|
for (const message of messages)
|
|
26809
|
-
|
|
27247
|
+
bindNativeRowID(message, row.id, row.seq);
|
|
26810
27248
|
return messages;
|
|
26811
27249
|
});
|
|
26812
27250
|
return this.options.protocol === 3 ? insertControls(projected, controlsThrough((replayOverride ?? this.tail(sessionID)?.replay)?.controls ?? [], reader, sessionID, through), nativeRowID) : projected;
|
|
@@ -26888,7 +27326,7 @@ ${cached}
|
|
|
26888
27326
|
if (!row)
|
|
26889
27327
|
throw new NativeReplayUnavailable("Native carrier has no source row coordinate");
|
|
26890
27328
|
cursor = row.seq;
|
|
26891
|
-
|
|
27329
|
+
bindNativeRowID(message, row.id, row.seq);
|
|
26892
27330
|
if (message.role !== "tool")
|
|
26893
27331
|
owner = row.id;
|
|
26894
27332
|
const messages = grouped.get(row) ?? [];
|
|
@@ -26913,13 +27351,13 @@ ${cached}
|
|
|
26913
27351
|
replay = { ...replay, blocked: false };
|
|
26914
27352
|
const replayAfter = replay.floor.after;
|
|
26915
27353
|
const replayStart = replay.floor.kind === "start";
|
|
27354
|
+
const baseline = this.servedBaseline(draft);
|
|
26916
27355
|
captured = native.filter((message) => {
|
|
26917
27356
|
if (effortPart(message))
|
|
26918
27357
|
return false;
|
|
26919
27358
|
const id = nativeRowID(message);
|
|
26920
27359
|
if (HEAD_IDS.some((head) => head === id))
|
|
26921
27360
|
return false;
|
|
26922
|
-
const baseline = this.servedBaseline(draft);
|
|
26923
27361
|
if (!id)
|
|
26924
27362
|
return !(baseline && message.role === "user" && message.content.some((part) => part.type === "text" && part.text === baseline));
|
|
26925
27363
|
if (replayStart)
|
|
@@ -27033,21 +27471,23 @@ ${cached}
|
|
|
27033
27471
|
if (await this.admissions?.get(admissionKey(draft.sessionID)) && !await this.readV3(draft.sessionID))
|
|
27034
27472
|
return false;
|
|
27035
27473
|
const running = reader.latestRunningCompaction(draft.sessionID);
|
|
27036
|
-
const replay = await this.floorState(draft.sessionID, reader, undefined, undefined, true);
|
|
27474
|
+
const replay = args.manual?.replay ?? await this.floorState(draft.sessionID, reader, undefined, undefined, true);
|
|
27037
27475
|
if (!running || !replay || replay.blocked) {
|
|
27038
27476
|
this.report(draft.sessionID, "fold refused: no usable host checkpoint boundary");
|
|
27039
27477
|
return false;
|
|
27040
27478
|
}
|
|
27041
27479
|
const model = `${draft.model.providerID}/${draft.model.id}`;
|
|
27042
|
-
if (this.tail(draft.sessionID)?.model !== model)
|
|
27480
|
+
if (!args.manual && this.tail(draft.sessionID)?.model !== model)
|
|
27043
27481
|
this.storage.replaceRows(draft.sessionID, reader.latestSequence(draft.sessionID), []);
|
|
27044
|
-
|
|
27045
|
-
|
|
27046
|
-
|
|
27047
|
-
|
|
27048
|
-
|
|
27049
|
-
|
|
27050
|
-
|
|
27482
|
+
if (!args.manual)
|
|
27483
|
+
await this.retainReplay(draft.sessionID, replay, model, reader.latestSequence(draft.sessionID));
|
|
27484
|
+
if (!args.manual)
|
|
27485
|
+
await this.storage.supply(draft.sessionID, running.id, JSON.stringify({
|
|
27486
|
+
source: this.storage.sourceID,
|
|
27487
|
+
sessionID: draft.sessionID,
|
|
27488
|
+
admissionID: running.id,
|
|
27489
|
+
summary
|
|
27490
|
+
}));
|
|
27051
27491
|
const value = {
|
|
27052
27492
|
version: 3,
|
|
27053
27493
|
replay,
|
|
@@ -27060,6 +27500,57 @@ ${cached}
|
|
|
27060
27500
|
}
|
|
27061
27501
|
};
|
|
27062
27502
|
const payload = encodeAdmissionV3(value);
|
|
27503
|
+
if (args.manual) {
|
|
27504
|
+
const keys = [
|
|
27505
|
+
capturedReplayKey(draft.sessionID),
|
|
27506
|
+
this.admissionKey(draft.sessionID)
|
|
27507
|
+
];
|
|
27508
|
+
const old = await Promise.all(keys.map((key) => this.admissions?.get(key)));
|
|
27509
|
+
const capture = {
|
|
27510
|
+
version: 3,
|
|
27511
|
+
sessionID: draft.sessionID,
|
|
27512
|
+
sourceDigest: foldDigest(reader.hostIdentity()),
|
|
27513
|
+
through: reader.latestSequence(draft.sessionID),
|
|
27514
|
+
replay
|
|
27515
|
+
};
|
|
27516
|
+
const capturedPayload = JSON.stringify(capture);
|
|
27517
|
+
let written = 0;
|
|
27518
|
+
try {
|
|
27519
|
+
for (const [index, key] of keys.entries()) {
|
|
27520
|
+
const data = index === 0 ? capturedPayload : payload;
|
|
27521
|
+
await this.admissions?.set(key, {
|
|
27522
|
+
payload: data,
|
|
27523
|
+
digest: foldDigest(data)
|
|
27524
|
+
});
|
|
27525
|
+
written++;
|
|
27526
|
+
}
|
|
27527
|
+
args.manual.commit();
|
|
27528
|
+
} catch (error) {
|
|
27529
|
+
for (let index = written - 1;index >= 0; index--) {
|
|
27530
|
+
const key = keys[index];
|
|
27531
|
+
if (!key)
|
|
27532
|
+
continue;
|
|
27533
|
+
if (old[index] !== undefined)
|
|
27534
|
+
await this.admissions?.set(key, old[index]);
|
|
27535
|
+
else if (this.admissions?.remove)
|
|
27536
|
+
await this.admissions.remove(key);
|
|
27537
|
+
else
|
|
27538
|
+
await this.admissions?.set(key, null);
|
|
27539
|
+
}
|
|
27540
|
+
throw error;
|
|
27541
|
+
}
|
|
27542
|
+
this.invalidate(draft.sessionID);
|
|
27543
|
+
this.cancelLegacyUpgrade(draft.sessionID);
|
|
27544
|
+
this.cancelEditedAdmission(draft.sessionID);
|
|
27545
|
+
await this.retainReplay(draft.sessionID, replay, model, running.seq - 1);
|
|
27546
|
+
await this.storage.supply(draft.sessionID, running.id, JSON.stringify({
|
|
27547
|
+
source: this.storage.sourceID,
|
|
27548
|
+
sessionID: draft.sessionID,
|
|
27549
|
+
admissionID: running.id,
|
|
27550
|
+
summary
|
|
27551
|
+
}));
|
|
27552
|
+
return true;
|
|
27553
|
+
}
|
|
27063
27554
|
await this.admissions?.set(this.admissionKey(draft.sessionID), {
|
|
27064
27555
|
payload,
|
|
27065
27556
|
digest: foldDigest(payload)
|
|
@@ -27128,7 +27619,13 @@ ${cached}
|
|
|
27128
27619
|
this.report(sessionID, "replay refused: missing usable checkpoint boundary");
|
|
27129
27620
|
throw new NativeReplayUnavailable("Host checkpoint replay boundary is unavailable");
|
|
27130
27621
|
}
|
|
27131
|
-
options = {
|
|
27622
|
+
options = {
|
|
27623
|
+
...options,
|
|
27624
|
+
bounded: false,
|
|
27625
|
+
after: replay.floor.after,
|
|
27626
|
+
retiredInstructionThrough: replay.retiredInstructionThrough,
|
|
27627
|
+
retiredInstructionIDs: replay.retiredInstructionIDs
|
|
27628
|
+
};
|
|
27132
27629
|
}
|
|
27133
27630
|
const release = this.storage.hold(sessionID);
|
|
27134
27631
|
let result;
|
|
@@ -27194,10 +27691,13 @@ ${cached}
|
|
|
27194
27691
|
reader?.close();
|
|
27195
27692
|
}
|
|
27196
27693
|
const messages = [];
|
|
27694
|
+
const retired = instructionRetirement(options);
|
|
27197
27695
|
for (const row of this.storage.rows(sessionID, after, cut.seq - 1)) {
|
|
27696
|
+
if (retired(row))
|
|
27697
|
+
continue;
|
|
27198
27698
|
const revived = reviveNative(JSON.parse(row.data));
|
|
27199
27699
|
for (const message of revived)
|
|
27200
|
-
|
|
27700
|
+
bindNativeRowID(message, row.id, row.seq);
|
|
27201
27701
|
messages.push(...revived);
|
|
27202
27702
|
}
|
|
27203
27703
|
return messages;
|
|
@@ -27210,10 +27710,11 @@ ${cached}
|
|
|
27210
27710
|
this.report(sessionID, "replay refused: missing usable checkpoint boundary");
|
|
27211
27711
|
throw new NativeReplayUnavailable("Host checkpoint replay boundary is unavailable");
|
|
27212
27712
|
}
|
|
27213
|
-
const
|
|
27713
|
+
const retired = instructionRetirement(replay);
|
|
27714
|
+
const messages = reader.range(sessionID, replay?.floor.after ?? -1, cut.seq - 1).filter(conversational).filter((row) => !retired(row)).flatMap((row) => {
|
|
27214
27715
|
const messages = this.rendered(row, model);
|
|
27215
27716
|
for (const message of messages)
|
|
27216
|
-
|
|
27717
|
+
bindNativeRowID(message, row.id, row.seq);
|
|
27217
27718
|
return messages;
|
|
27218
27719
|
});
|
|
27219
27720
|
return replay ? insertControls(messages, controlsThrough(replay.controls, reader, sessionID, cut.seq - 1), nativeRowID) : messages;
|
|
@@ -27348,6 +27849,12 @@ class HostFoldPolicy {
|
|
|
27348
27849
|
}
|
|
27349
27850
|
}
|
|
27350
27851
|
|
|
27852
|
+
// src/v2/fold/summary.ts
|
|
27853
|
+
var RAW_HISTORY_ACCOUNT = "Magic Context retains the conversation as raw messages; no summarized history is available yet.";
|
|
27854
|
+
function hasFoldSummary(summary) {
|
|
27855
|
+
return typeof summary === "string" && summary.replace(/<[^>]*>/g, "").trim().length > 0;
|
|
27856
|
+
}
|
|
27857
|
+
|
|
27351
27858
|
// src/v2/fork-inheritance.ts
|
|
27352
27859
|
var RAW_TYPES = new Set(RAW_MESSAGE_TYPES2);
|
|
27353
27860
|
function forkBoundarySeq(origin, sequenceForId) {
|
|
@@ -28342,6 +28849,26 @@ async function createV2HiddenCompletionExecutor(host, options) {
|
|
|
28342
28849
|
const budgetStopped = new Promise((_resolve, reject) => {
|
|
28343
28850
|
budgetReject = reject;
|
|
28344
28851
|
});
|
|
28852
|
+
const chargeBudgetRows = (rows) => {
|
|
28853
|
+
if (!budget)
|
|
28854
|
+
return;
|
|
28855
|
+
for (const row of rows) {
|
|
28856
|
+
if (row.seq <= usageSeq)
|
|
28857
|
+
continue;
|
|
28858
|
+
if (!row.data.finish)
|
|
28859
|
+
break;
|
|
28860
|
+
usageSeq = row.seq;
|
|
28861
|
+
const tokens = row.data.tokens;
|
|
28862
|
+
const decision = budget.charge(Math.max(0, tokens?.input ?? 0), Math.max(0, tokens?.cache?.read ?? 0), Math.max(0, tokens?.cache?.write ?? 0), row.data.finish === "stop" && !(row.data.content ?? []).some((part) => part.type === "tool-call"), false);
|
|
28863
|
+
const onBudgetUpdate = run.identity.metadata?.onBudgetUpdate;
|
|
28864
|
+
if (typeof onBudgetUpdate === "function")
|
|
28865
|
+
onBudgetUpdate({ ...budget.snapshot(), sessionId: run.child.id });
|
|
28866
|
+
if (decision !== "continue") {
|
|
28867
|
+
attempt.budgetExceeded = new DreamTokenBudgetExceeded(run.child.id, budget.snapshot().spent);
|
|
28868
|
+
return attempt.budgetExceeded;
|
|
28869
|
+
}
|
|
28870
|
+
}
|
|
28871
|
+
};
|
|
28345
28872
|
if (budget) {
|
|
28346
28873
|
budgetPoll = inRootAsyncContext2(() => setInterval(() => {
|
|
28347
28874
|
try {
|
|
@@ -28349,25 +28876,12 @@ async function createV2HiddenCompletionExecutor(host, options) {
|
|
|
28349
28876
|
const latest = reader.latestAssistant(run.child.id);
|
|
28350
28877
|
return latest ? [latest] : [];
|
|
28351
28878
|
})());
|
|
28352
|
-
|
|
28353
|
-
|
|
28354
|
-
|
|
28355
|
-
|
|
28356
|
-
|
|
28357
|
-
|
|
28358
|
-
const onBudgetUpdate = run.identity.metadata?.onBudgetUpdate;
|
|
28359
|
-
if (typeof onBudgetUpdate === "function")
|
|
28360
|
-
onBudgetUpdate({
|
|
28361
|
-
...budget.snapshot(),
|
|
28362
|
-
sessionId: run.child.id
|
|
28363
|
-
});
|
|
28364
|
-
if (decision !== "continue") {
|
|
28365
|
-
if (budgetPoll)
|
|
28366
|
-
clearInterval(budgetPoll);
|
|
28367
|
-
attempt.budgetExceeded = new DreamTokenBudgetExceeded(run.child.id, budget.snapshot().spent);
|
|
28368
|
-
interruptAndRetire(run, "token-budget").finally(() => budgetReject(attempt.budgetExceeded));
|
|
28369
|
-
return;
|
|
28370
|
-
}
|
|
28879
|
+
const exceeded = chargeBudgetRows(fresh);
|
|
28880
|
+
if (exceeded) {
|
|
28881
|
+
if (budgetPoll)
|
|
28882
|
+
clearInterval(budgetPoll);
|
|
28883
|
+
interruptAndRetire(run, "token-budget").finally(() => budgetReject(exceeded));
|
|
28884
|
+
return;
|
|
28371
28885
|
}
|
|
28372
28886
|
} catch (error) {
|
|
28373
28887
|
if (budgetPoll)
|
|
@@ -28464,6 +28978,11 @@ async function createV2HiddenCompletionExecutor(host, options) {
|
|
|
28464
28978
|
const tokens = row.data.tokens;
|
|
28465
28979
|
const tokenNumber = (value) => typeof value === "number" && Number.isFinite(value) ? value : undefined;
|
|
28466
28980
|
const steps = withReader(options.openReader, (reader) => reader.assistantSince?.(run.child.id, baseline) ?? [row]).filter((step) => step.seq > baseline && step.seq <= row.seq);
|
|
28981
|
+
const exceeded = chargeBudgetRows(steps);
|
|
28982
|
+
if (exceeded) {
|
|
28983
|
+
await interruptAndRetire(run, "token-budget");
|
|
28984
|
+
throw exceeded;
|
|
28985
|
+
}
|
|
28467
28986
|
const reportedUsage = steps.some((step) => tokenNumber(step.data.tokens?.input) !== undefined || tokenNumber(step.data.tokens?.output) !== undefined);
|
|
28468
28987
|
const usage = sumRowUsage(steps);
|
|
28469
28988
|
if (request.signal?.aborted || run.retired || Date.now() >= deadline) {
|
|
@@ -28683,6 +29202,100 @@ class V2MessageTimesCache {
|
|
|
28683
29202
|
|
|
28684
29203
|
// src/v2/hooks/channel2.ts
|
|
28685
29204
|
import { randomBytes as randomBytes2 } from "node:crypto";
|
|
29205
|
+
|
|
29206
|
+
// src/v2/hooks/synthetic-admission.ts
|
|
29207
|
+
var memos = new WeakMap;
|
|
29208
|
+
var MAX_SESSIONS = 64;
|
|
29209
|
+
var MAX_IDS = 50000;
|
|
29210
|
+
function admissionSession(key) {
|
|
29211
|
+
if (!key.startsWith("synthetic/"))
|
|
29212
|
+
return;
|
|
29213
|
+
const end = key.indexOf("/", "synthetic/".length);
|
|
29214
|
+
return end < 0 ? undefined : key.slice("synthetic/".length, end);
|
|
29215
|
+
}
|
|
29216
|
+
function memoFor(storage) {
|
|
29217
|
+
const prior = memos.get(storage);
|
|
29218
|
+
if (prior)
|
|
29219
|
+
return prior;
|
|
29220
|
+
const sessions = new Map;
|
|
29221
|
+
const set = storage.set;
|
|
29222
|
+
const remove = storage.remove;
|
|
29223
|
+
const invalidate = (key) => {
|
|
29224
|
+
const sessionID = admissionSession(key);
|
|
29225
|
+
if (sessionID !== undefined) {
|
|
29226
|
+
sessions.delete(sessionID);
|
|
29227
|
+
const current = memos.get(storage);
|
|
29228
|
+
if (current) {
|
|
29229
|
+
current.sessions.delete(sessionID);
|
|
29230
|
+
current.revision++;
|
|
29231
|
+
}
|
|
29232
|
+
}
|
|
29233
|
+
};
|
|
29234
|
+
const wrappedSet = async (key, value) => {
|
|
29235
|
+
invalidate(key);
|
|
29236
|
+
try {
|
|
29237
|
+
await set.call(storage, key, value);
|
|
29238
|
+
} finally {
|
|
29239
|
+
invalidate(key);
|
|
29240
|
+
}
|
|
29241
|
+
};
|
|
29242
|
+
const wrappedRemove = remove && (async (key) => {
|
|
29243
|
+
invalidate(key);
|
|
29244
|
+
try {
|
|
29245
|
+
await remove.call(storage, key);
|
|
29246
|
+
} finally {
|
|
29247
|
+
invalidate(key);
|
|
29248
|
+
}
|
|
29249
|
+
});
|
|
29250
|
+
const memo = { sessions, revision: 0, set, remove, wrappedSet, wrappedRemove };
|
|
29251
|
+
storage.set = wrappedSet;
|
|
29252
|
+
if (wrappedRemove)
|
|
29253
|
+
storage.remove = wrappedRemove;
|
|
29254
|
+
memos.set(storage, memo);
|
|
29255
|
+
return memo;
|
|
29256
|
+
}
|
|
29257
|
+
function forgetSyntheticAdmission(storage, sessionID) {
|
|
29258
|
+
const memo = memoFor(storage);
|
|
29259
|
+
memo.sessions.delete(sessionID);
|
|
29260
|
+
memo.revision++;
|
|
29261
|
+
}
|
|
29262
|
+
function clearSyntheticAdmissions(storage) {
|
|
29263
|
+
const memo = memos.get(storage);
|
|
29264
|
+
if (!memo)
|
|
29265
|
+
return;
|
|
29266
|
+
memo.sessions.clear();
|
|
29267
|
+
if (storage.set === memo.wrappedSet)
|
|
29268
|
+
storage.set = memo.set;
|
|
29269
|
+
if (storage.remove === memo.wrappedRemove)
|
|
29270
|
+
storage.remove = memo.remove;
|
|
29271
|
+
memos.delete(storage);
|
|
29272
|
+
}
|
|
29273
|
+
async function admittedSynthetic(storage, sessionID, id) {
|
|
29274
|
+
for (;; ) {
|
|
29275
|
+
const memo = memoFor(storage);
|
|
29276
|
+
const session = memo.sessions.get(sessionID) ?? new Map;
|
|
29277
|
+
memo.sessions.delete(sessionID);
|
|
29278
|
+
memo.sessions.set(sessionID, session);
|
|
29279
|
+
if (memo.sessions.size > MAX_SESSIONS)
|
|
29280
|
+
memo.sessions.delete(memo.sessions.keys().next().value);
|
|
29281
|
+
const known = session.get(id);
|
|
29282
|
+
if (known !== undefined)
|
|
29283
|
+
return known;
|
|
29284
|
+
const revision = memo.revision;
|
|
29285
|
+
const record = await storage.get(`synthetic/${sessionID}/${id}`);
|
|
29286
|
+
if (memos.get(storage) !== memo || memo.revision !== revision)
|
|
29287
|
+
continue;
|
|
29288
|
+
const admitted = record !== null && typeof record === "object" && "id" in record && record.id === id;
|
|
29289
|
+
if (memo.sessions.get(sessionID) !== session)
|
|
29290
|
+
return admitted;
|
|
29291
|
+
if (session.size >= MAX_IDS)
|
|
29292
|
+
session.delete(session.keys().next().value);
|
|
29293
|
+
session.set(id, admitted);
|
|
29294
|
+
return admitted;
|
|
29295
|
+
}
|
|
29296
|
+
}
|
|
29297
|
+
|
|
29298
|
+
// src/v2/hooks/channel2.ts
|
|
28686
29299
|
var pendingCandidates = new WeakMap;
|
|
28687
29300
|
function syntheticCandidates(context, sessionID, stored) {
|
|
28688
29301
|
const pending = pendingCandidates.get(context.storage);
|
|
@@ -28698,9 +29311,17 @@ function syntheticCandidates(context, sessionID, stored) {
|
|
|
28698
29311
|
}
|
|
28699
29312
|
function clearSyntheticCandidates(context) {
|
|
28700
29313
|
pendingCandidates.delete(context.storage);
|
|
29314
|
+
clearSyntheticAdmissions(context.storage);
|
|
29315
|
+
}
|
|
29316
|
+
function observeSyntheticAdmissionEvent(context, event) {
|
|
29317
|
+
const sessionID = event.data?.sessionID;
|
|
29318
|
+
if (!sessionID || !(event.type === "session.deleted" || event.type?.startsWith("session.revert.")))
|
|
29319
|
+
return;
|
|
29320
|
+
forgetSyntheticAdmission(context.storage, sessionID);
|
|
28701
29321
|
}
|
|
28702
29322
|
async function deliverSynthetic(context, sessionID, text) {
|
|
28703
29323
|
const id = `msg_${randomBytes2(12).toString("hex")}`;
|
|
29324
|
+
forgetSyntheticAdmission(context.storage, sessionID);
|
|
28704
29325
|
await context.storage.set(`synthetic/${sessionID}/${id}`, { id });
|
|
28705
29326
|
let candidates = pendingCandidates.get(context.storage);
|
|
28706
29327
|
if (!candidates) {
|
|
@@ -28712,8 +29333,7 @@ async function deliverSynthetic(context, sessionID, text) {
|
|
|
28712
29333
|
return id;
|
|
28713
29334
|
}
|
|
28714
29335
|
async function isAdmittedSynthetic(context, sessionID, id) {
|
|
28715
|
-
|
|
28716
|
-
return record !== null && typeof record === "object" && "id" in record && record.id === id;
|
|
29336
|
+
return admittedSynthetic(context.storage, sessionID, id);
|
|
28717
29337
|
}
|
|
28718
29338
|
async function deliverPendingChannel2(context, db, sessionID, baseline) {
|
|
28719
29339
|
if (getChannel2NudgeState(db, sessionID) !== "pending")
|
|
@@ -28915,7 +29535,7 @@ class DeletedSessionTombstones {
|
|
|
28915
29535
|
}
|
|
28916
29536
|
|
|
28917
29537
|
// src/features/magic-context/dreamer/task-executor.ts
|
|
28918
|
-
import { createHash as
|
|
29538
|
+
import { createHash as createHash15 } from "node:crypto";
|
|
28919
29539
|
import { existsSync as existsSync7, readFileSync as readFileSync8 } from "node:fs";
|
|
28920
29540
|
|
|
28921
29541
|
// src/features/magic-context/mural/compress-cues.ts
|
|
@@ -34658,7 +35278,7 @@ function insertDreamRun(db, run) {
|
|
|
34658
35278
|
}
|
|
34659
35279
|
|
|
34660
35280
|
// src/features/magic-context/dreamer/verify.ts
|
|
34661
|
-
import { createHash as
|
|
35281
|
+
import { createHash as createHash14 } from "node:crypto";
|
|
34662
35282
|
|
|
34663
35283
|
// src/features/magic-context/dreamer/verify-diff.ts
|
|
34664
35284
|
import { execFile as execFile3 } from "node:child_process";
|
|
@@ -34779,7 +35399,56 @@ Read only these changed line ranges if they may affect the claim.`
|
|
|
34779
35399
|
}
|
|
34780
35400
|
|
|
34781
35401
|
// src/features/magic-context/dreamer/verify-gate.ts
|
|
35402
|
+
import path6 from "node:path";
|
|
35403
|
+
|
|
35404
|
+
// src/features/magic-context/dreamer/verify-state.ts
|
|
35405
|
+
import { createHash as createHash13 } from "node:crypto";
|
|
35406
|
+
import { readFile, realpath as realpath2 } from "node:fs/promises";
|
|
34782
35407
|
import path5 from "node:path";
|
|
35408
|
+
function verificationMetadata(memory) {
|
|
35409
|
+
try {
|
|
35410
|
+
const parsed = JSON.parse(memory.metadataJson ?? "{}");
|
|
35411
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed))
|
|
35412
|
+
return parsed;
|
|
35413
|
+
} catch {}
|
|
35414
|
+
return {};
|
|
35415
|
+
}
|
|
35416
|
+
async function verificationFilesFingerprint(directory, files) {
|
|
35417
|
+
const root = await resolveGitTopLevel(directory) ?? path5.resolve(directory);
|
|
35418
|
+
const hash = createHash13("sha256");
|
|
35419
|
+
for (const file of [...new Set(files)].sort()) {
|
|
35420
|
+
const absolute = path5.resolve(root, file);
|
|
35421
|
+
const withinRoot = (candidate) => {
|
|
35422
|
+
const relative = path5.relative(root, candidate);
|
|
35423
|
+
return relative !== ".." && !relative.startsWith(`..${path5.sep}`) && !path5.isAbsolute(relative);
|
|
35424
|
+
};
|
|
35425
|
+
if (!withinRoot(absolute))
|
|
35426
|
+
return null;
|
|
35427
|
+
hash.update(JSON.stringify(file));
|
|
35428
|
+
try {
|
|
35429
|
+
if (!withinRoot(await realpath2(absolute)))
|
|
35430
|
+
return null;
|
|
35431
|
+
hash.update(createHash13("sha256").update(await readFile(absolute)).digest("hex"));
|
|
35432
|
+
} catch (error) {
|
|
35433
|
+
if (error.code !== "ENOENT")
|
|
35434
|
+
return null;
|
|
35435
|
+
hash.update("missing");
|
|
35436
|
+
}
|
|
35437
|
+
}
|
|
35438
|
+
return hash.digest("hex");
|
|
35439
|
+
}
|
|
35440
|
+
function recordVerificationVerdict(db, memory, verdict, at, fingerprint = null) {
|
|
35441
|
+
const metadata = verificationMetadata(memory);
|
|
35442
|
+
delete metadata.dreamerVerifySkip;
|
|
35443
|
+
if (verdict === "skip" && fingerprint) {
|
|
35444
|
+
metadata.dreamerVerifySkip = { contentHash: memory.normalizedHash, fingerprint, at };
|
|
35445
|
+
}
|
|
35446
|
+
metadata.dreamerVerifyVerdict = verdict;
|
|
35447
|
+
metadata.dreamerVerifyAttemptedAt = at;
|
|
35448
|
+
db.prepare("UPDATE memories SET metadata_json = ? WHERE id = ?").run(JSON.stringify(metadata), memory.id);
|
|
35449
|
+
}
|
|
35450
|
+
|
|
35451
|
+
// src/features/magic-context/dreamer/verify-gate.ts
|
|
34783
35452
|
function minOf(values) {
|
|
34784
35453
|
return values.reduce((acc, v) => v < acc ? v : acc, Number.POSITIVE_INFINITY);
|
|
34785
35454
|
}
|
|
@@ -34811,7 +35480,22 @@ async function partitionVerifyScope(args) {
|
|
|
34811
35480
|
const runStartedAt = args.now ?? Date.now();
|
|
34812
35481
|
const active = getMemoriesByProject2(args.db, args.projectIdentity);
|
|
34813
35482
|
const verById = getMemoryVerifications(args.db, active.map((m) => m.id));
|
|
34814
|
-
const candidates =
|
|
35483
|
+
const candidates = [];
|
|
35484
|
+
const unchangedSkippedIds = [];
|
|
35485
|
+
const changedSkippedIds = new Set;
|
|
35486
|
+
for (const memory of active) {
|
|
35487
|
+
const files = verById.get(memory.id)?.files ?? [];
|
|
35488
|
+
if (!files.length)
|
|
35489
|
+
continue;
|
|
35490
|
+
const skip = verificationMetadata(memory).dreamerVerifySkip;
|
|
35491
|
+
if (skip && skip.contentHash === memory.normalizedHash && typeof skip.fingerprint === "string" && skip.fingerprint === await verificationFilesFingerprint(args.projectDirectory, files)) {
|
|
35492
|
+
unchangedSkippedIds.push(memory.id);
|
|
35493
|
+
} else {
|
|
35494
|
+
candidates.push(memory);
|
|
35495
|
+
if (skip)
|
|
35496
|
+
changedSkippedIds.add(memory.id);
|
|
35497
|
+
}
|
|
35498
|
+
}
|
|
34815
35499
|
const toPrompt = (m) => {
|
|
34816
35500
|
const verifiedAt = verById.get(m.id)?.verifiedAt ?? 0;
|
|
34817
35501
|
let verifiedCommit = null;
|
|
@@ -34838,7 +35522,7 @@ async function partitionVerifyScope(args) {
|
|
|
34838
35522
|
leaseKey: args.leaseKey,
|
|
34839
35523
|
runStartedAt
|
|
34840
35524
|
});
|
|
34841
|
-
const broadCandidates = candidates.filter((m) => (verById.get(m.id)?.verifiedAt ?? 0) < broadCycleStartAt).sort((a, b) => {
|
|
35525
|
+
const broadCandidates = candidates.filter((m) => changedSkippedIds.has(m.id) || (verById.get(m.id)?.verifiedAt ?? 0) < broadCycleStartAt).sort((a, b) => {
|
|
34842
35526
|
const verifiedAtA = verById.get(a.id)?.verifiedAt ?? 0;
|
|
34843
35527
|
const verifiedAtB = verById.get(b.id)?.verifiedAt ?? 0;
|
|
34844
35528
|
return verifiedAtA - verifiedAtB || a.id - b.id;
|
|
@@ -34849,7 +35533,10 @@ async function partitionVerifyScope(args) {
|
|
|
34849
35533
|
mode: "broad",
|
|
34850
35534
|
inScope: broadCandidates.map(toPrompt),
|
|
34851
35535
|
inScopeIds: broadCandidates.map((m) => m.id),
|
|
34852
|
-
skippedIds:
|
|
35536
|
+
skippedIds: [
|
|
35537
|
+
...unchangedSkippedIds,
|
|
35538
|
+
...candidates.filter((m) => !broadIds.has(m.id)).map((m) => m.id)
|
|
35539
|
+
],
|
|
34853
35540
|
broadCycleStartAt,
|
|
34854
35541
|
reason: `broad cycle (${broadCandidates.length} remain; started ${broadCycleStartAt})`
|
|
34855
35542
|
};
|
|
@@ -34860,7 +35547,7 @@ async function partitionVerifyScope(args) {
|
|
|
34860
35547
|
mode: "incremental",
|
|
34861
35548
|
inScope: [],
|
|
34862
35549
|
inScopeIds: [],
|
|
34863
|
-
skippedIds:
|
|
35550
|
+
skippedIds: unchangedSkippedIds,
|
|
34864
35551
|
reason: "no file-mapped memories in scope"
|
|
34865
35552
|
};
|
|
34866
35553
|
}
|
|
@@ -34869,11 +35556,11 @@ async function partitionVerifyScope(args) {
|
|
|
34869
35556
|
mode,
|
|
34870
35557
|
inScope: candidates.map(toPrompt),
|
|
34871
35558
|
inScopeIds: candidates.map((m) => m.id),
|
|
34872
|
-
skippedIds:
|
|
35559
|
+
skippedIds: unchangedSkippedIds,
|
|
34873
35560
|
reason
|
|
34874
35561
|
});
|
|
34875
35562
|
const resolvedGitRoot = await resolveGitTopLevel(args.projectDirectory);
|
|
34876
|
-
const gitRoot = resolvedGitRoot ??
|
|
35563
|
+
const gitRoot = resolvedGitRoot ?? path6.resolve(args.projectDirectory);
|
|
34877
35564
|
const verifiedTimes = candidates.map((m) => verById.get(m.id)?.verifiedAt ?? 0).filter((t) => t > 0);
|
|
34878
35565
|
const sinceMs = verifiedTimes.length > 0 ? minOf(verifiedTimes) : runStartedAt;
|
|
34879
35566
|
const changeTimes = await readGitFileChangeTimesSince(args.projectDirectory, sinceMs, resolvedGitRoot ?? undefined);
|
|
@@ -34883,11 +35570,11 @@ async function partitionVerifyScope(args) {
|
|
|
34883
35570
|
const head = await readGitHead(args.projectDirectory);
|
|
34884
35571
|
const uncommitted = head ? await readGitChangedFilesSince(args.projectDirectory, head, resolvedGitRoot ?? undefined) ?? new Set : new Set;
|
|
34885
35572
|
const inScope = [];
|
|
34886
|
-
const skippedIds = [];
|
|
35573
|
+
const skippedIds = [...unchangedSkippedIds];
|
|
34887
35574
|
for (const m of candidates) {
|
|
34888
35575
|
const v = verById.get(m.id);
|
|
34889
35576
|
const verifiedAt = v?.verifiedAt ?? 0;
|
|
34890
|
-
if (verifiedAt === 0) {
|
|
35577
|
+
if (verifiedAt === 0 || changedSkippedIds.has(m.id)) {
|
|
34891
35578
|
inScope.push(toPrompt(m));
|
|
34892
35579
|
continue;
|
|
34893
35580
|
}
|
|
@@ -35257,6 +35944,7 @@ async function verifyOneBatch(args, batch, sliceMs, signal) {
|
|
|
35257
35944
|
let budgetFinalized = false;
|
|
35258
35945
|
let failedCompletion;
|
|
35259
35946
|
let headAtPrompt = null;
|
|
35947
|
+
const skipSnapshots = new Map;
|
|
35260
35948
|
const onBudgetUpdate = (state) => {
|
|
35261
35949
|
budgetFinalized ||= state.finalizeFired;
|
|
35262
35950
|
args.onBudgetUpdate?.({
|
|
@@ -35273,6 +35961,11 @@ async function verifyOneBatch(args, batch, sliceMs, signal) {
|
|
|
35273
35961
|
});
|
|
35274
35962
|
};
|
|
35275
35963
|
try {
|
|
35964
|
+
if (!args.moduleRoute) {
|
|
35965
|
+
for (const memory of batch) {
|
|
35966
|
+
skipSnapshots.set(memory.id, await raceWithSignal(verificationFilesFingerprint(args.sessionDirectory, memory.mappedFiles), batchSignal));
|
|
35967
|
+
}
|
|
35968
|
+
}
|
|
35276
35969
|
headAtPrompt = await raceWithSignal(readGitHead(args.sessionDirectory), batchSignal);
|
|
35277
35970
|
const evidence = !args.forceBroad && batch.every((m) => m.verifiedAt) ? await raceWithSignal(buildVerifyDiffEvidence(args.sessionDirectory, batch), batchSignal) : null;
|
|
35278
35971
|
const prompt = buildVerifyPrompt(args.projectIdentity, await raceWithSignal(seedVerifyCandidates(batch, args.sessionDirectory), batchSignal), evidence);
|
|
@@ -35314,7 +36007,7 @@ async function verifyOneBatch(args, batch, sliceMs, signal) {
|
|
|
35314
36007
|
tokens: run.completion.usage
|
|
35315
36008
|
});
|
|
35316
36009
|
reportUsage(run.completion.messages ?? [], run.childSessionId, run.completion.usage.input + (run.completion.usage.cacheRead ?? 0) + (run.completion.usage.cacheWrite ?? 0));
|
|
35317
|
-
return await applyParsedVerifyManifest(args, batch, run.validated, evidence?.head ?? headAtPrompt, budgetFinalized);
|
|
36010
|
+
return await applyParsedVerifyManifest(args, batch, run.validated, evidence?.head ?? headAtPrompt, budgetFinalized, skipSnapshots);
|
|
35318
36011
|
}
|
|
35319
36012
|
const client = args.client;
|
|
35320
36013
|
if (!client)
|
|
@@ -35374,7 +36067,7 @@ async function verifyOneBatch(args, batch, sliceMs, signal) {
|
|
|
35374
36067
|
promptSettled = true;
|
|
35375
36068
|
recordInvocation4(args, startedAt, { status: "completed", messages: run.output });
|
|
35376
36069
|
reportUsage(run.output, agentSessionId);
|
|
35377
|
-
return await applyParsedVerifyManifest(args, batch, run.validated, evidence?.head ?? headAtPrompt, budgetFinalized);
|
|
36070
|
+
return await applyParsedVerifyManifest(args, batch, run.validated, evidence?.head ?? headAtPrompt, budgetFinalized, skipSnapshots);
|
|
35378
36071
|
} catch (error) {
|
|
35379
36072
|
const desc = describeError(error);
|
|
35380
36073
|
const providerFailure = error instanceof DreamerProviderOutputFailureError ? error : undefined;
|
|
@@ -35394,7 +36087,7 @@ async function verifyOneBatch(args, batch, sliceMs, signal) {
|
|
|
35394
36087
|
if (interrupted) {
|
|
35395
36088
|
const text = verifyAssistantText(messages) ?? failedCompletion?.text ?? "";
|
|
35396
36089
|
const recovered = parsePartialVerifyManifest(text, new Set(batch.map((m) => m.id)));
|
|
35397
|
-
const counts = await applyParsedVerifyManifest(args, batch, recovered, headAtPrompt, true);
|
|
36090
|
+
const counts = await applyParsedVerifyManifest(args, batch, recovered, headAtPrompt, true, skipSnapshots);
|
|
35398
36091
|
return {
|
|
35399
36092
|
...counts,
|
|
35400
36093
|
timedOut: !(error instanceof DreamTokenBudgetExceeded),
|
|
@@ -35426,7 +36119,7 @@ async function verifyOneBatch(args, batch, sliceMs, signal) {
|
|
|
35426
36119
|
signal.removeEventListener("abort", abort);
|
|
35427
36120
|
}
|
|
35428
36121
|
}
|
|
35429
|
-
async function applyParsedVerifyManifest(args, batch, parsed, headAtPrompt, budgetFinalized = false) {
|
|
36122
|
+
async function applyParsedVerifyManifest(args, batch, parsed, headAtPrompt, budgetFinalized = false, skipSnapshots) {
|
|
35430
36123
|
const batchIds = new Set(batch.map((m) => m.id));
|
|
35431
36124
|
const batchById = new Map(batch.map((memory) => [memory.id, memory]));
|
|
35432
36125
|
const valid = {
|
|
@@ -35505,6 +36198,15 @@ async function applyParsedVerifyManifest(args, batch, parsed, headAtPrompt, budg
|
|
|
35505
36198
|
}
|
|
35506
36199
|
writes.push({ kind: "archive", id: a.id, reason: a.reason });
|
|
35507
36200
|
}
|
|
36201
|
+
if (!args.moduleRoute) {
|
|
36202
|
+
for (const entry of valid.skipped) {
|
|
36203
|
+
const original = batchById.get(entry.id);
|
|
36204
|
+
if (!original)
|
|
36205
|
+
continue;
|
|
36206
|
+
const fingerprint = skipSnapshots ? skipSnapshots.get(entry.id) ?? null : await verificationFilesFingerprint(args.sessionDirectory, original.mappedFiles);
|
|
36207
|
+
writes.push({ kind: "skip", id: entry.id, fingerprint });
|
|
36208
|
+
}
|
|
36209
|
+
}
|
|
35508
36210
|
let verified = 0;
|
|
35509
36211
|
let updated = 0;
|
|
35510
36212
|
let archived = 0;
|
|
@@ -35534,7 +36236,7 @@ async function applyParsedVerifyManifest(args, batch, parsed, headAtPrompt, budg
|
|
|
35534
36236
|
name: "memory.set_verification",
|
|
35535
36237
|
arguments: {
|
|
35536
36238
|
memory_project: args.projectIdentity,
|
|
35537
|
-
command_id: `${args.moduleRoute.moduleCommandId}:${
|
|
36239
|
+
command_id: `${args.moduleRoute.moduleCommandId}:${createHash14("sha256").update(rows.map((row) => row.memory_id).join(",")).digest("hex").slice(0, 16)}`,
|
|
35538
36240
|
rows
|
|
35539
36241
|
}
|
|
35540
36242
|
}
|
|
@@ -35566,9 +36268,17 @@ async function applyParsedVerifyManifest(args, batch, parsed, headAtPrompt, budg
|
|
|
35566
36268
|
continue;
|
|
35567
36269
|
if (w.kind === "verify") {
|
|
35568
36270
|
recordMemoryVerifications(args.db, w.id, w.files, now);
|
|
35569
|
-
|
|
35570
|
-
|
|
36271
|
+
updateMemoryVerification2(args.db, w.id, "verified");
|
|
36272
|
+
recordVerificationVerdict(args.db, memory, "verified", now);
|
|
36273
|
+
const verifiedMemory = getMemoryById2(args.db, w.id);
|
|
36274
|
+
if (verifiedHead && verifiedMemory)
|
|
36275
|
+
recordVerifiedCommit(args.db, verifiedMemory, now, verifiedHead);
|
|
35571
36276
|
verified += 1;
|
|
36277
|
+
} else if (w.kind === "skip") {
|
|
36278
|
+
if (memory.content !== batchById.get(w.id)?.content)
|
|
36279
|
+
continue;
|
|
36280
|
+
updateMemoryVerification2(args.db, w.id, "unverified");
|
|
36281
|
+
recordVerificationVerdict(args.db, memory, "skip", now, w.fingerprint);
|
|
35572
36282
|
} else if (w.kind === "update") {
|
|
35573
36283
|
const duplicate = getMemoryByHash2(args.db, memory.projectPath, memory.category, w.hash);
|
|
35574
36284
|
if (duplicate && duplicate.id !== memory.id) {
|
|
@@ -35577,6 +36287,8 @@ async function applyParsedVerifyManifest(args, batch, parsed, headAtPrompt, budg
|
|
|
35577
36287
|
continue;
|
|
35578
36288
|
}
|
|
35579
36289
|
rewriteMemoryContent(args.db, memory, w.content, w.hash);
|
|
36290
|
+
updateMemoryVerification2(args.db, w.id, "unverified");
|
|
36291
|
+
recordVerificationVerdict(args.db, memory, "update", now);
|
|
35580
36292
|
queueMemoryMutation(args.db, {
|
|
35581
36293
|
projectPath: args.projectIdentity,
|
|
35582
36294
|
mutationType: "update",
|
|
@@ -35587,6 +36299,8 @@ async function applyParsedVerifyManifest(args, batch, parsed, headAtPrompt, budg
|
|
|
35587
36299
|
updated += 1;
|
|
35588
36300
|
} else {
|
|
35589
36301
|
archiveMemory2(args.db, w.id, w.reason);
|
|
36302
|
+
updateMemoryVerification2(args.db, w.id, "stale");
|
|
36303
|
+
recordVerificationVerdict(args.db, memory, "archive", now);
|
|
35590
36304
|
queueMemoryMutation(args.db, {
|
|
35591
36305
|
projectPath: args.projectIdentity,
|
|
35592
36306
|
mutationType: "archive",
|
|
@@ -36380,7 +37094,7 @@ function parseFrictionGateVerdict(verdict) {
|
|
|
36380
37094
|
}
|
|
36381
37095
|
function computeRetrospectiveWindowKey(flagged) {
|
|
36382
37096
|
const anchors = flagged.map((message) => `${message.sessionId}:${message.ts}`).sort().join("|");
|
|
36383
|
-
return
|
|
37097
|
+
return createHash15("sha256").update(anchors).digest("hex").slice(0, 32);
|
|
36384
37098
|
}
|
|
36385
37099
|
function renderFrictionWindow(messages, flaggedOrdinals, radius = 2) {
|
|
36386
37100
|
const flagged = new Set(flaggedOrdinals);
|
|
@@ -38485,9 +39199,9 @@ function hiddenTerminalError(event) {
|
|
|
38485
39199
|
}
|
|
38486
39200
|
|
|
38487
39201
|
// src/v2/hooks/lkg-system.ts
|
|
38488
|
-
import { createHash as
|
|
39202
|
+
import { createHash as createHash16 } from "node:crypto";
|
|
38489
39203
|
function identity2(slot) {
|
|
38490
|
-
return
|
|
39204
|
+
return createHash16("sha256").update(JSON.stringify({
|
|
38491
39205
|
capturedAt: slot.capturedAt,
|
|
38492
39206
|
captureSequence: slot.captureSequence,
|
|
38493
39207
|
rowVersion: slot.rowVersion,
|
|
@@ -38704,16 +39418,31 @@ async function interruptBeforeProvider(session, sessionID, timers = systemTimers
|
|
|
38704
39418
|
}
|
|
38705
39419
|
|
|
38706
39420
|
// src/v2/hooks/restore-rows.ts
|
|
39421
|
+
var DEFAULT_LIMITS = {
|
|
39422
|
+
maxSessions: 16,
|
|
39423
|
+
sessionRows: 40000,
|
|
39424
|
+
totalRows: 80000,
|
|
39425
|
+
sessionBytes: 192 * 1024 * 1024,
|
|
39426
|
+
totalBytes: 256 * 1024 * 1024
|
|
39427
|
+
};
|
|
39428
|
+
|
|
38707
39429
|
class RestoredRowCache {
|
|
38708
|
-
capacity;
|
|
38709
39430
|
sessions = new Map;
|
|
38710
|
-
|
|
38711
|
-
|
|
39431
|
+
limits;
|
|
39432
|
+
totalRows = 0;
|
|
39433
|
+
totalBytes = 0;
|
|
39434
|
+
constructor(limits = {}) {
|
|
39435
|
+
this.limits = {
|
|
39436
|
+
...DEFAULT_LIMITS,
|
|
39437
|
+
...typeof limits === "number" ? { maxSessions: limits } : limits
|
|
39438
|
+
};
|
|
38712
39439
|
}
|
|
38713
39440
|
rows(reader, sessionID, after, through) {
|
|
38714
|
-
if (through <= after)
|
|
39441
|
+
if (through <= after) {
|
|
39442
|
+
this.forget(sessionID);
|
|
38715
39443
|
return [];
|
|
38716
|
-
|
|
39444
|
+
}
|
|
39445
|
+
const kept = this.sessions.get(sessionID)?.rows ?? new Map;
|
|
38717
39446
|
const stamps = reader.spanRowStamps(sessionID, after, through);
|
|
38718
39447
|
const runs = [];
|
|
38719
39448
|
let previous = after;
|
|
@@ -38735,28 +39464,44 @@ class RestoredRowCache {
|
|
|
38735
39464
|
fresh.set(row.seq, row);
|
|
38736
39465
|
const next = new Map;
|
|
38737
39466
|
const rows = [];
|
|
39467
|
+
let bytes = 0;
|
|
38738
39468
|
for (const [seq, stamp] of stamps) {
|
|
38739
39469
|
const row = fresh.get(seq) ?? kept.get(seq)?.row;
|
|
38740
39470
|
if (!row)
|
|
38741
39471
|
continue;
|
|
38742
|
-
next.set(seq, { stamp, row });
|
|
38743
39472
|
rows.push(row);
|
|
39473
|
+
const rowBytes = Number(stamp.slice(stamp.lastIndexOf("\x00") + 1));
|
|
39474
|
+
if (!Number.isSafeInteger(rowBytes) || rowBytes < 0 || next.size >= Math.min(this.limits.sessionRows, this.limits.totalRows) || bytes + rowBytes > Math.min(this.limits.sessionBytes, this.limits.totalBytes))
|
|
39475
|
+
continue;
|
|
39476
|
+
next.set(seq, { stamp, row });
|
|
39477
|
+
bytes += rowBytes;
|
|
38744
39478
|
}
|
|
38745
|
-
this.
|
|
38746
|
-
|
|
38747
|
-
|
|
39479
|
+
this.forget(sessionID);
|
|
39480
|
+
if (next.size) {
|
|
39481
|
+
this.sessions.set(sessionID, { rows: next, bytes });
|
|
39482
|
+
this.totalRows += next.size;
|
|
39483
|
+
this.totalBytes += bytes;
|
|
39484
|
+
}
|
|
39485
|
+
while (this.sessions.size > this.limits.maxSessions || this.totalRows > this.limits.totalRows || this.totalBytes > this.limits.totalBytes) {
|
|
38748
39486
|
const oldest = this.sessions.keys().next().value;
|
|
38749
39487
|
if (oldest === undefined)
|
|
38750
39488
|
break;
|
|
38751
|
-
this.
|
|
39489
|
+
this.forget(oldest);
|
|
38752
39490
|
}
|
|
38753
39491
|
return structuredClone(rows);
|
|
38754
39492
|
}
|
|
38755
39493
|
forget(sessionID) {
|
|
39494
|
+
const prior = this.sessions.get(sessionID);
|
|
39495
|
+
if (prior) {
|
|
39496
|
+
this.totalRows -= prior.rows.size;
|
|
39497
|
+
this.totalBytes -= prior.bytes;
|
|
39498
|
+
}
|
|
38756
39499
|
this.sessions.delete(sessionID);
|
|
38757
39500
|
}
|
|
38758
39501
|
clear() {
|
|
38759
39502
|
this.sessions.clear();
|
|
39503
|
+
this.totalRows = 0;
|
|
39504
|
+
this.totalBytes = 0;
|
|
38760
39505
|
}
|
|
38761
39506
|
}
|
|
38762
39507
|
function readPages(reader, sessionID, after, through) {
|
|
@@ -40340,14 +41085,14 @@ function datetime(args) {
|
|
|
40340
41085
|
const timeRegex = `${time2}(?:${opts.join("|")})`;
|
|
40341
41086
|
return new RegExp(`^${dateSource}T(?:${timeRegex})$`);
|
|
40342
41087
|
}
|
|
40343
|
-
var
|
|
41088
|
+
var string2 = (params) => {
|
|
40344
41089
|
const regex = params ? `[\\s\\S]{${params?.minimum ?? 0},${params?.maximum ?? ""}}` : `[\\s\\S]*`;
|
|
40345
41090
|
return new RegExp(`^${regex}$`);
|
|
40346
41091
|
};
|
|
40347
41092
|
var bigint = /^-?\d+n?$/;
|
|
40348
41093
|
var integer = /^-?\d+$/;
|
|
40349
|
-
var
|
|
40350
|
-
var
|
|
41094
|
+
var number2 = /^-?\d+(?:\.\d+)?/;
|
|
41095
|
+
var boolean2 = /^(?:true|false)$/i;
|
|
40351
41096
|
var _null = /^null$/i;
|
|
40352
41097
|
var _undefined = /^undefined$/i;
|
|
40353
41098
|
var lowercase = /^[^A-Z]*$/;
|
|
@@ -41073,7 +41818,7 @@ var $ZodType = /* @__PURE__ */ $constructor("$ZodType", (inst, def) => {
|
|
|
41073
41818
|
});
|
|
41074
41819
|
var $ZodString = /* @__PURE__ */ $constructor("$ZodString", (inst, def) => {
|
|
41075
41820
|
$ZodType.init(inst, def);
|
|
41076
|
-
inst._zod.pattern = [...inst?._zod.bag?.patterns ?? []].pop() ??
|
|
41821
|
+
inst._zod.pattern = [...inst?._zod.bag?.patterns ?? []].pop() ?? string2(inst._zod.bag);
|
|
41077
41822
|
inst._zod.parse = (payload, _) => {
|
|
41078
41823
|
if (def.coerce)
|
|
41079
41824
|
try {
|
|
@@ -41387,7 +42132,7 @@ var $ZodCustomStringFormat = /* @__PURE__ */ $constructor("$ZodCustomStringForma
|
|
|
41387
42132
|
});
|
|
41388
42133
|
var $ZodNumber = /* @__PURE__ */ $constructor("$ZodNumber", (inst, def) => {
|
|
41389
42134
|
$ZodType.init(inst, def);
|
|
41390
|
-
inst._zod.pattern = inst._zod.bag.pattern ??
|
|
42135
|
+
inst._zod.pattern = inst._zod.bag.pattern ?? number2;
|
|
41391
42136
|
inst._zod.parse = (payload, _ctx) => {
|
|
41392
42137
|
if (def.coerce)
|
|
41393
42138
|
try {
|
|
@@ -41414,7 +42159,7 @@ var $ZodNumberFormat = /* @__PURE__ */ $constructor("$ZodNumber", (inst, def) =>
|
|
|
41414
42159
|
});
|
|
41415
42160
|
var $ZodBoolean = /* @__PURE__ */ $constructor("$ZodBoolean", (inst, def) => {
|
|
41416
42161
|
$ZodType.init(inst, def);
|
|
41417
|
-
inst._zod.pattern =
|
|
42162
|
+
inst._zod.pattern = boolean2;
|
|
41418
42163
|
inst._zod.parse = (payload, _ctx) => {
|
|
41419
42164
|
if (def.coerce)
|
|
41420
42165
|
try {
|
|
@@ -50069,7 +50814,7 @@ var ZodType = /* @__PURE__ */ $constructor("ZodType", (inst, def) => {
|
|
|
50069
50814
|
inst.nullable = () => nullable(inst);
|
|
50070
50815
|
inst.nullish = () => optional(nullable(inst));
|
|
50071
50816
|
inst.nonoptional = (params) => nonoptional(inst, params);
|
|
50072
|
-
inst.array = () =>
|
|
50817
|
+
inst.array = () => array2(inst);
|
|
50073
50818
|
inst.or = (arg) => union([inst, arg]);
|
|
50074
50819
|
inst.and = (arg) => intersection(inst, arg);
|
|
50075
50820
|
inst.transform = (tx) => pipe(inst, transform(tx));
|
|
@@ -50154,7 +50899,7 @@ var ZodString = /* @__PURE__ */ $constructor("ZodString", (inst, def) => {
|
|
|
50154
50899
|
inst.time = (params) => inst.check(time2(params));
|
|
50155
50900
|
inst.duration = (params) => inst.check(duration2(params));
|
|
50156
50901
|
});
|
|
50157
|
-
function
|
|
50902
|
+
function string3(params) {
|
|
50158
50903
|
return _string(ZodString, params);
|
|
50159
50904
|
}
|
|
50160
50905
|
var ZodStringFormat = /* @__PURE__ */ $constructor("ZodStringFormat", (inst, def) => {
|
|
@@ -50356,7 +51101,7 @@ var ZodNumber = /* @__PURE__ */ $constructor("ZodNumber", (inst, def) => {
|
|
|
50356
51101
|
inst.isFinite = true;
|
|
50357
51102
|
inst.format = bag.format ?? null;
|
|
50358
51103
|
});
|
|
50359
|
-
function
|
|
51104
|
+
function number3(params) {
|
|
50360
51105
|
return _number(ZodNumber, params);
|
|
50361
51106
|
}
|
|
50362
51107
|
var ZodNumberFormat = /* @__PURE__ */ $constructor("ZodNumberFormat", (inst, def) => {
|
|
@@ -50382,7 +51127,7 @@ var ZodBoolean = /* @__PURE__ */ $constructor("ZodBoolean", (inst, def) => {
|
|
|
50382
51127
|
$ZodBoolean.init(inst, def);
|
|
50383
51128
|
ZodType.init(inst, def);
|
|
50384
51129
|
});
|
|
50385
|
-
function
|
|
51130
|
+
function boolean3(params) {
|
|
50386
51131
|
return _boolean(ZodBoolean, params);
|
|
50387
51132
|
}
|
|
50388
51133
|
var ZodBigInt = /* @__PURE__ */ $constructor("ZodBigInt", (inst, def) => {
|
|
@@ -50490,7 +51235,7 @@ var ZodArray = /* @__PURE__ */ $constructor("ZodArray", (inst, def) => {
|
|
|
50490
51235
|
inst.length = (len, params) => inst.check(_length(len, params));
|
|
50491
51236
|
inst.unwrap = () => inst.element;
|
|
50492
51237
|
});
|
|
50493
|
-
function
|
|
51238
|
+
function array2(element, params) {
|
|
50494
51239
|
return _array(ZodArray, element, params);
|
|
50495
51240
|
}
|
|
50496
51241
|
function keyof(schema) {
|
|
@@ -50519,7 +51264,7 @@ var ZodObject = /* @__PURE__ */ $constructor("ZodObject", (inst, def) => {
|
|
|
50519
51264
|
inst.partial = (...args) => partial(ZodOptional, inst, args[0]);
|
|
50520
51265
|
inst.required = (...args) => required(ZodNonOptional, inst, args[0]);
|
|
50521
51266
|
});
|
|
50522
|
-
function
|
|
51267
|
+
function object3(shape, params) {
|
|
50523
51268
|
const def = {
|
|
50524
51269
|
type: "object",
|
|
50525
51270
|
get shape() {
|
|
@@ -50595,7 +51340,7 @@ var ZodTuple = /* @__PURE__ */ $constructor("ZodTuple", (inst, def) => {
|
|
|
50595
51340
|
rest
|
|
50596
51341
|
});
|
|
50597
51342
|
});
|
|
50598
|
-
function
|
|
51343
|
+
function tuple2(items, _paramsOrRest, _params) {
|
|
50599
51344
|
const hasRest = _paramsOrRest instanceof $ZodType;
|
|
50600
51345
|
const params = hasRest ? _params : _paramsOrRest;
|
|
50601
51346
|
const rest = hasRest ? _paramsOrRest : null;
|
|
@@ -50724,7 +51469,7 @@ var ZodLiteral = /* @__PURE__ */ $constructor("ZodLiteral", (inst, def) => {
|
|
|
50724
51469
|
}
|
|
50725
51470
|
});
|
|
50726
51471
|
});
|
|
50727
|
-
function
|
|
51472
|
+
function literal2(value, params) {
|
|
50728
51473
|
return new ZodLiteral({
|
|
50729
51474
|
type: "literal",
|
|
50730
51475
|
values: Array.isArray(value) ? value : [value],
|
|
@@ -50952,7 +51697,7 @@ var ZodFunction = /* @__PURE__ */ $constructor("ZodFunction", (inst, def) => {
|
|
|
50952
51697
|
function _function(params) {
|
|
50953
51698
|
return new ZodFunction({
|
|
50954
51699
|
type: "function",
|
|
50955
|
-
input: Array.isArray(params?.input) ?
|
|
51700
|
+
input: Array.isArray(params?.input) ? tuple2(params?.input) : params?.input ?? array2(unknown()),
|
|
50956
51701
|
output: params?.output ?? unknown()
|
|
50957
51702
|
});
|
|
50958
51703
|
}
|
|
@@ -50996,7 +51741,7 @@ var stringbool = (...args) => _stringbool({
|
|
|
50996
51741
|
}, ...args);
|
|
50997
51742
|
function json2(params) {
|
|
50998
51743
|
const jsonSchema = lazy(() => {
|
|
50999
|
-
return union([
|
|
51744
|
+
return union([string3(params), number3(), boolean3(), _null3(), array2(jsonSchema), record(string3(), jsonSchema)]);
|
|
51000
51745
|
});
|
|
51001
51746
|
return jsonSchema;
|
|
51002
51747
|
}
|
|
@@ -51028,13 +51773,13 @@ function getErrorMap() {
|
|
|
51028
51773
|
var ZodFirstPartyTypeKind;
|
|
51029
51774
|
(function(ZodFirstPartyTypeKind) {})(ZodFirstPartyTypeKind || (ZodFirstPartyTypeKind = {}));
|
|
51030
51775
|
// ../../node_modules/.bun/zod@4.1.8/node_modules/zod/v4/classic/coerce.js
|
|
51031
|
-
function
|
|
51776
|
+
function string4(params) {
|
|
51032
51777
|
return _coercedString(ZodString, params);
|
|
51033
51778
|
}
|
|
51034
|
-
function
|
|
51779
|
+
function number4(params) {
|
|
51035
51780
|
return _coercedNumber(ZodNumber, params);
|
|
51036
51781
|
}
|
|
51037
|
-
function
|
|
51782
|
+
function boolean4(params) {
|
|
51038
51783
|
return _coercedBoolean(ZodBoolean, params);
|
|
51039
51784
|
}
|
|
51040
51785
|
function bigint3(params) {
|
|
@@ -51967,7 +52712,7 @@ import { existsSync as existsSync9 } from "node:fs";
|
|
|
51967
52712
|
import { isAbsolute as isAbsolute2 } from "node:path";
|
|
51968
52713
|
|
|
51969
52714
|
// ../retina-local-fs/src/path-fence.ts
|
|
51970
|
-
import { lstat as lstat2, realpath as
|
|
52715
|
+
import { lstat as lstat2, realpath as realpath3 } from "node:fs/promises";
|
|
51971
52716
|
import { homedir as homedir4 } from "node:os";
|
|
51972
52717
|
import { basename, dirname as dirname2, isAbsolute, join as join11, relative, resolve as resolve2, sep } from "node:path";
|
|
51973
52718
|
|
|
@@ -51997,7 +52742,7 @@ async function resolveFenceRoots(options) {
|
|
|
51997
52742
|
const configuredHomePath = resolve2(options.homeDirectory ?? process.env.HOME ?? homedir4());
|
|
51998
52743
|
let home;
|
|
51999
52744
|
try {
|
|
52000
|
-
home = await
|
|
52745
|
+
home = await realpath3(configuredHomePath);
|
|
52001
52746
|
} catch (error) {
|
|
52002
52747
|
throw fsError(configuredHomePath, error);
|
|
52003
52748
|
}
|
|
@@ -52027,7 +52772,7 @@ function fenceCandidates(canonical, aliases) {
|
|
|
52027
52772
|
}
|
|
52028
52773
|
async function canonicalPath(path, allowMissing) {
|
|
52029
52774
|
try {
|
|
52030
|
-
return await
|
|
52775
|
+
return await realpath3(path);
|
|
52031
52776
|
} catch (error) {
|
|
52032
52777
|
if (!allowMissing || !isMissingError(error)) {
|
|
52033
52778
|
throw fsError(path, error);
|
|
@@ -52052,7 +52797,7 @@ async function canonicalPath(path, allowMissing) {
|
|
|
52052
52797
|
suffix.unshift(basename(candidate));
|
|
52053
52798
|
candidate = parent;
|
|
52054
52799
|
try {
|
|
52055
|
-
return join11(await
|
|
52800
|
+
return join11(await realpath3(candidate), ...suffix);
|
|
52056
52801
|
} catch (parentError) {
|
|
52057
52802
|
if (!isMissingError(parentError)) {
|
|
52058
52803
|
throw fsError(path, parentError);
|
|
@@ -52742,7 +53487,7 @@ function formatCtxReduceReply(stampedCount, selfStampCount = 0) {
|
|
|
52742
53487
|
return parts.join(" ");
|
|
52743
53488
|
}
|
|
52744
53489
|
// src/tools/ctx-reduce/tools.ts
|
|
52745
|
-
import { createHash as
|
|
53490
|
+
import { createHash as createHash17 } from "node:crypto";
|
|
52746
53491
|
|
|
52747
53492
|
// src/features/magic-context/range-parser.ts
|
|
52748
53493
|
function parseRangeString(input) {
|
|
@@ -52805,11 +53550,11 @@ function createCtxReduceTool(deps) {
|
|
|
52805
53550
|
const stableId = `oc-${sessionId}-${callId}`;
|
|
52806
53551
|
if (Buffer.byteLength(stableId) <= 128)
|
|
52807
53552
|
return stableId;
|
|
52808
|
-
return `oc-${
|
|
53553
|
+
return `oc-${createHash17("sha256").update(stableId).digest("hex")}`;
|
|
52809
53554
|
}
|
|
52810
53555
|
fallbackCommandSequence += 1;
|
|
52811
53556
|
const monotonicId = `oc-${sessionId}-${fallbackCommandSequence}`;
|
|
52812
|
-
return Buffer.byteLength(monotonicId) <= 128 ? monotonicId : `oc-${
|
|
53557
|
+
return Buffer.byteLength(monotonicId) <= 128 ? monotonicId : `oc-${createHash17("sha256").update(monotonicId).digest("hex")}`;
|
|
52813
53558
|
};
|
|
52814
53559
|
return tool({
|
|
52815
53560
|
description: CTX_REDUCE_DESCRIPTION,
|
|
@@ -53452,7 +54197,7 @@ function persistV2UsageReading(args) {
|
|
|
53452
54197
|
args.contextUsageMap.set(sessionID, {
|
|
53453
54198
|
usage: { inputTokens: reading.inputTokens, percentage },
|
|
53454
54199
|
hasUsageTokens: true,
|
|
53455
|
-
updatedAt: Date.now()
|
|
54200
|
+
updatedAt: reading.completed || Date.now()
|
|
53456
54201
|
});
|
|
53457
54202
|
} else {
|
|
53458
54203
|
args.contextUsageMap.delete(sessionID);
|
|
@@ -53500,7 +54245,7 @@ function bindKeptInstructionCoordinates(messages, incoming, reader, sessionID) {
|
|
|
53500
54245
|
selected.forEach((row, index) => {
|
|
53501
54246
|
const message = pending[index];
|
|
53502
54247
|
if (message)
|
|
53503
|
-
bindNativeRowID(message, row.id);
|
|
54248
|
+
bindNativeRowID(message, row.id, row.seq);
|
|
53504
54249
|
});
|
|
53505
54250
|
}
|
|
53506
54251
|
pending = [];
|
|
@@ -53794,10 +54539,10 @@ async function applyV2SystemPrompt(systemPrompt, draft) {
|
|
|
53794
54539
|
text
|
|
53795
54540
|
})));
|
|
53796
54541
|
}
|
|
53797
|
-
function coveredInstructionRows(reader, sessionID, throughSeq, model) {
|
|
54542
|
+
function coveredInstructionRows(reader, sessionID, throughSeq, model, retired = () => false) {
|
|
53798
54543
|
if (throughSeq < 0)
|
|
53799
54544
|
return [];
|
|
53800
|
-
return reader.instructionRowsThrough(sessionID, throughSeq).flatMap((row) => restoreRow(row, model)).map((message) => markCoveredInstructionRow(message));
|
|
54545
|
+
return reader.instructionRowsThrough(sessionID, throughSeq).filter((row) => !retired(row)).flatMap((row) => restoreRow(row, model)).map((message) => markCoveredInstructionRow(message));
|
|
53801
54546
|
}
|
|
53802
54547
|
async function registerContext(context) {
|
|
53803
54548
|
const directory = context.location.directory;
|
|
@@ -54276,7 +55021,15 @@ async function registerContext(context) {
|
|
|
54276
55021
|
storage: nativeFoldMemory,
|
|
54277
55022
|
openReader: openStoreReader,
|
|
54278
55023
|
canReplay: (sessionID, reader) => nativeFoldReplay.canReplay(sessionID, reader),
|
|
54279
|
-
canFold: (sessionID, reader) =>
|
|
55024
|
+
canFold: async (sessionID, reader) => {
|
|
55025
|
+
const meta = getOrCreateSessionMeta(storage.require(), sessionID);
|
|
55026
|
+
const summary = [
|
|
55027
|
+
servedModuleM0Text(sessionID),
|
|
55028
|
+
await nativeFoldReplay.baseline(sessionID),
|
|
55029
|
+
meta.cachedM0Bytes?.toString("utf8")
|
|
55030
|
+
].find((value) => value !== RAW_HISTORY_ACCOUNT && hasFoldSummary(value));
|
|
55031
|
+
return !meta.isSubagent && summary !== RAW_HISTORY_ACCOUNT && hasFoldSummary(summary) && nativeFoldReplay.canFold(sessionID, reader);
|
|
55032
|
+
},
|
|
54280
55033
|
threshold: Number(process.env.MC_OC2_HOST_FOLD_ROWS ?? 1000),
|
|
54281
55034
|
log: sessionLog2
|
|
54282
55035
|
}) : undefined;
|
|
@@ -54302,6 +55055,7 @@ async function registerContext(context) {
|
|
|
54302
55055
|
if (!event.data?.sessionID)
|
|
54303
55056
|
continue;
|
|
54304
55057
|
const sessionID = event.data.sessionID;
|
|
55058
|
+
observeSyntheticAdmissionEvent(context, event);
|
|
54305
55059
|
overflowRecovery.observeTerminal(event, (id, messageID) => {
|
|
54306
55060
|
const reader = openStoreReader();
|
|
54307
55061
|
try {
|
|
@@ -54483,22 +55237,46 @@ async function registerContext(context) {
|
|
|
54483
55237
|
await storage.probe();
|
|
54484
55238
|
const watermark = reader.latestSequenceForIds(draft.sessionID, draft.messages.flatMap((message) => message.id ? [message.id] : []));
|
|
54485
55239
|
const running = reader.latestRunningCompaction(draft.sessionID);
|
|
55240
|
+
const attempt = running && nativeFoldMemory?.admission(draft.sessionID, running.id);
|
|
55241
|
+
const manual = running?.data.reason === "manual" && !(attempt?.status === "pending" && attempt.owner_token);
|
|
54486
55242
|
const moduleBaseline = rustModeModuleClient ? servedModuleM0Text(draft.sessionID) : undefined;
|
|
54487
55243
|
const source = !rustModeModuleClient ? "typescript" : moduleBaseline === null ? "typescript_fallback" : "module";
|
|
54488
55244
|
if (nativeFoldReplay) {
|
|
54489
55245
|
try {
|
|
54490
55246
|
const state = getOrCreateSessionMeta(storage.require(), draft.sessionID);
|
|
54491
|
-
const
|
|
54492
|
-
|
|
55247
|
+
const staged = manual && moduleBaseline == null ? stageManualBaseline(storage.require(), draft.sessionID, () => materialize(draft)) : undefined;
|
|
55248
|
+
const publishedHistory = Boolean(storage.require().prepare("SELECT 1 FROM compartments WHERE session_id = ? LIMIT 1").get(draft.sessionID));
|
|
55249
|
+
const candidate = [
|
|
55250
|
+
moduleBaseline,
|
|
55251
|
+
await nativeFoldReplay.baseline(draft.sessionID),
|
|
55252
|
+
state.cachedM0Bytes?.toString("utf8"),
|
|
55253
|
+
(await folds.read(draft.sessionID))?.submitted
|
|
55254
|
+
].find((value) => value !== RAW_HISTORY_ACCOUNT && (publishedHistory && value?.includes("<session-history>") || hasFoldSummary(value)));
|
|
55255
|
+
const manualHistory = manual && hasFoldSummary(staged?.summary ?? moduleBaseline);
|
|
55256
|
+
const summary = manualHistory ? staged?.summary ?? candidate ?? RAW_HISTORY_ACCOUNT : candidate ?? RAW_HISTORY_ACCOUNT;
|
|
55257
|
+
const manualReplay = manualHistory && summary ? await nativeFoldReplay.prepareManualFold(draft.sessionID, reader, staged ? staged.boundaryID : storage.require().prepare("SELECT cached_m0_last_baseline_end_message_id AS id FROM session_meta WHERE session_id = ?").get(draft.sessionID).id, summary) : undefined;
|
|
55258
|
+
if ((hasFoldSummary(summary) || publishedHistory) && await nativeFoldReplay.supply({
|
|
54493
55259
|
draft,
|
|
54494
55260
|
reader,
|
|
54495
55261
|
previousCut: reader.latestCompaction(draft.sessionID),
|
|
54496
|
-
summary
|
|
55262
|
+
summary,
|
|
55263
|
+
manual: manualReplay ? { replay: manualReplay, commit: () => staged?.commit() } : undefined
|
|
54497
55264
|
})) {
|
|
55265
|
+
if (manualHistory) {
|
|
55266
|
+
dropSlot(draft.sessionID, "manual host compaction");
|
|
55267
|
+
generateReplay.forget(draft.sessionID);
|
|
55268
|
+
restoredRows.forget(draft.sessionID);
|
|
55269
|
+
historyRefreshSessions.add(draft.sessionID);
|
|
55270
|
+
requestPendingMaterialization(pendingMaterializationSessions, draft.sessionID, "host_compaction");
|
|
55271
|
+
}
|
|
54498
55272
|
draft.result = { summary, metadata: { keepID: undefined } };
|
|
54499
55273
|
return;
|
|
54500
55274
|
}
|
|
55275
|
+
if (manual)
|
|
55276
|
+
throw new V2ContextRefusal("Magic Context could not admit the manual fold.");
|
|
54501
55277
|
} catch (error) {
|
|
55278
|
+
if (error instanceof V2ContextRefusal || manual)
|
|
55279
|
+
throw error;
|
|
54502
55280
|
nativeFoldReplay.invalidate(draft.sessionID, true);
|
|
54503
55281
|
sessionLog2(draft.sessionID, `v2 host fold: legacy compaction reason=${JSON.stringify(String(error))}`);
|
|
54504
55282
|
}
|
|
@@ -54507,7 +55285,10 @@ async function registerContext(context) {
|
|
|
54507
55285
|
sessionID: draft.sessionID,
|
|
54508
55286
|
watermark,
|
|
54509
55287
|
runningCut: running?.seq,
|
|
54510
|
-
materialize: () =>
|
|
55288
|
+
materialize: () => {
|
|
55289
|
+
const summary = moduleBaseline ?? materialize(draft);
|
|
55290
|
+
return hasFoldSummary(summary) ? summary : RAW_HISTORY_ACCOUNT;
|
|
55291
|
+
}
|
|
54511
55292
|
});
|
|
54512
55293
|
sessionLog2(draft.sessionID, `v2 compaction hook: fired answered=true source=${source}`);
|
|
54513
55294
|
draft.result = { summary: fold.submitted, metadata: { keepID: undefined } };
|
|
@@ -54522,6 +55303,19 @@ async function registerContext(context) {
|
|
|
54522
55303
|
});
|
|
54523
55304
|
const temporalTimes = config.temporal_awareness === true;
|
|
54524
55305
|
const anchorTimesOnly = !temporalTimes && !rustModeModuleClient;
|
|
55306
|
+
const retireInstructionMessages = async (sessionID, reader, messages) => {
|
|
55307
|
+
const retired = await nativeFoldReplay?.instructionRetirement(sessionID, reader);
|
|
55308
|
+
if (!retired)
|
|
55309
|
+
return messages;
|
|
55310
|
+
return messages.filter((message) => {
|
|
55311
|
+
const id = nativeRowID(message);
|
|
55312
|
+
return message.role !== "system" || !id || !retired({
|
|
55313
|
+
type: "system",
|
|
55314
|
+
id,
|
|
55315
|
+
seq: nativeRowSequence(message) ?? reader.sequenceForId(sessionID, id) ?? Infinity
|
|
55316
|
+
});
|
|
55317
|
+
});
|
|
55318
|
+
};
|
|
54525
55319
|
const timeIds = (messages) => {
|
|
54526
55320
|
if (temporalTimes)
|
|
54527
55321
|
return messages.flatMap((message) => message.id ? [message.id] : []);
|
|
@@ -54543,16 +55337,17 @@ async function registerContext(context) {
|
|
|
54543
55337
|
const boundaryID = db.prepare("SELECT cached_m0_last_baseline_end_message_id AS id FROM session_meta WHERE session_id = ?").get(draft.sessionID)?.id ?? (boundaryRepair?.kind === "repaired" || boundaryRepair?.kind === "baseline-reset" ? boundaryRepair.anchorEndMessageId : null);
|
|
54544
55338
|
const servedBoundary = boundaryID ? servedBoundaryRow2(reader, draft.sessionID, boundaryID) : null;
|
|
54545
55339
|
const nativeFloor = await nativeFoldReplay?.restorationFloor(draft.sessionID, cut, reader);
|
|
55340
|
+
const retiredInstructions = await nativeFoldReplay?.instructionRetirement(draft.sessionID, reader) ?? (() => false);
|
|
54546
55341
|
const boundary = Math.max(nativeFloor ?? -Infinity, (nativeFoldReplay?.sourceChanged(draft.sessionID) && !rustModeModuleClient ? (reader.earliestSequence(draft.sessionID) ?? 0) - 1 : undefined) ?? (moduleBoundarySeq !== undefined ? moduleBoundarySeq - 1 : undefined) ?? servedBoundary?.seq ?? reader.sequenceForId(draft.sessionID, boundaryID) ?? (reader.earliestSequence(draft.sessionID) ?? 0) - 1);
|
|
54547
55342
|
if (moduleBoundarySeq !== undefined)
|
|
54548
55343
|
sessionLog2(draft.sessionID, `v2 restore: from the module boundary ${moduleBoundaryID} to the host checkpoint`);
|
|
54549
55344
|
const present = new Set(args.messages.map((message) => message.id));
|
|
54550
|
-
const restored = restoredRows.rows(reader, draft.sessionID, boundary, cut.seq).filter((row) => !present.has(row.id)).flatMap((row) => restoreRow(row, draft.model, hostUsesMediaAssets() ? {
|
|
55345
|
+
const restored = restoredRows.rows(reader, draft.sessionID, boundary, cut.seq).filter((row) => !retiredInstructions(row)).filter((row) => !present.has(row.id)).flatMap((row) => restoreRow(row, draft.model, hostUsesMediaAssets() ? {
|
|
54551
55346
|
asset: hostMediaAsset,
|
|
54552
55347
|
unavailable: (detail) => sessionLog2(draft.sessionID, `v2 restore: attachment replaced by a note row=${detail.rowID} name=${JSON.stringify(detail.name ?? null)} mediaType=${detail.mediaType} reason=${JSON.stringify(detail.reason)}`)
|
|
54553
55348
|
} : undefined));
|
|
54554
55349
|
return [
|
|
54555
|
-
...rustModeModuleClient ? [] : coveredInstructionRows(reader, draft.sessionID, Math.min(boundary, cut.seq), draft.model),
|
|
55350
|
+
...rustModeModuleClient ? [] : coveredInstructionRows(reader, draft.sessionID, Math.min(boundary, cut.seq), draft.model, retiredInstructions),
|
|
54556
55351
|
...restored,
|
|
54557
55352
|
...args.messages.filter((message) => message !== args.incoming)
|
|
54558
55353
|
];
|
|
@@ -54785,6 +55580,7 @@ async function registerContext(context) {
|
|
|
54785
55580
|
}
|
|
54786
55581
|
} else if (localCut)
|
|
54787
55582
|
return;
|
|
55583
|
+
messages = await retireInstructionMessages(draft.sessionID, reader, messages);
|
|
54788
55584
|
times = readTimes(reader, messages);
|
|
54789
55585
|
}
|
|
54790
55586
|
} finally {
|
|
@@ -55014,6 +55810,7 @@ async function registerContext(context) {
|
|
|
55014
55810
|
boundaryRepair
|
|
55015
55811
|
}));
|
|
55016
55812
|
}
|
|
55813
|
+
draft.messages = await retireInstructionMessages(draft.sessionID, reader, draft.messages);
|
|
55017
55814
|
messageTimes = readTimes(reader, draft.messages);
|
|
55018
55815
|
} finally {
|
|
55019
55816
|
reader.close();
|
|
@@ -55194,6 +55991,11 @@ async function registerContext(context) {
|
|
|
55194
55991
|
if (!deletedSessions.has(draft.sessionID) && admissionRebuildPasses.has(draft.sessionID)) {
|
|
55195
55992
|
await nativeFoldReplay?.commitLegacyUpgrade(draft.sessionID, true);
|
|
55196
55993
|
await nativeFoldReplay?.commitEditedAdmission(draft.sessionID);
|
|
55994
|
+
if (db && hostFoldIdle) {
|
|
55995
|
+
const meta = getOrCreateSessionMeta(db, draft.sessionID);
|
|
55996
|
+
if (computeHardCacheExpired(meta.cacheTtl, meta.lastResponseTime, Date.now()))
|
|
55997
|
+
hostFoldIdle.idle(draft.sessionID, meta.cacheTtl, meta.lastResponseTime);
|
|
55998
|
+
}
|
|
55197
55999
|
}
|
|
55198
56000
|
} finally {
|
|
55199
56001
|
admissionRebuildPasses.delete(draft.sessionID);
|