@cortexkit/opencode-magic-context 0.49.90 → 0.49.91
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/auto-search-worker.js +4 -4
- package/dist/chunk-embedding-write-worker.js +6 -6
- package/dist/{compartment-chunk-embedding-fey2bsyf.js → compartment-chunk-embedding-rp4asvk0.js} +3 -3
- package/dist/config/index.d.ts.map +1 -1
- package/dist/config/migrate-dreamer-v2.d.ts +1 -0
- package/dist/config/migrate-dreamer-v2.d.ts.map +1 -1
- package/dist/{context-cdds0gjj.js → context-6qq5e1ry.js} +519 -197
- package/dist/{embedding-backfill-qjkaee9s.js → embedding-backfill-w0yq71q2.js} +6 -6
- package/dist/embedding-worker.js +3 -3
- package/dist/features/magic-context/dreamer/refresh-primers.d.ts.map +1 -1
- package/dist/features/magic-context/dreamer/task-scheduler.d.ts +2 -2
- package/dist/features/magic-context/dreamer/task-scheduler.d.ts.map +1 -1
- package/dist/features/magic-context/memory/single-store-embedding-drain.d.ts.map +1 -1
- package/dist/features/magic-context/project-embedding-registry.d.ts +2 -1
- package/dist/features/magic-context/project-embedding-registry.d.ts.map +1 -1
- package/dist/hooks/magic-context/compartment-runner-types.d.ts +2 -2
- package/dist/hooks/magic-context/compartment-runner-types.d.ts.map +1 -1
- package/dist/hooks/magic-context/historian-host-runner.d.ts +1 -1
- package/dist/hooks/magic-context/historian-host-runner.d.ts.map +1 -1
- package/dist/hooks/magic-context/read-session-formatting.d.ts +2 -0
- package/dist/hooks/magic-context/read-session-formatting.d.ts.map +1 -1
- package/dist/{index-3pdknr07.js → index-0twg41mh.js} +2 -2
- package/dist/{index-2pdgy88n.js → index-3n376ann.js} +19 -7
- package/dist/{index-wmb2mcdw.js → index-3yw525kb.js} +5 -5
- package/dist/{index-mrjat4eq.js → index-5wgdbj2h.js} +3 -3
- package/dist/{index-pxnzyvjc.js → index-6r99v53h.js} +1 -1
- package/dist/{index-9d3qgwq0.js → index-6t7jvp76.js} +3 -3
- package/dist/{index-jzfsnbmm.js → index-7rgnh11t.js} +5 -5
- package/dist/{index-chpzb6xy.js → index-capcna4r.js} +2 -2
- package/dist/{index-1y4y98r1.js → index-czz0w2c4.js} +14 -2
- package/dist/{index-zxhev8ze.js → index-dva352qe.js} +1 -1
- package/dist/{index-dk3hy2a2.js → index-e8my7q6m.js} +10 -10
- package/dist/{index-w51n3576.js → index-f25ha337.js} +1 -1
- package/dist/{index-q7msfgwz.js → index-febyth5n.js} +94 -27
- package/dist/{index-kknjvmpx.js → index-fqth7850.js} +51 -60
- package/dist/{index-60wnwcwz.js → index-gqhebzxx.js} +2 -2
- package/dist/{index-n1ty0vd3.js → index-m6vhxsce.js} +58 -56
- package/dist/{index-m6dnydf8.js → index-mg9s2rc3.js} +1 -1
- package/dist/{index-2qsca7h7.js → index-mq97qmb8.js} +3 -3
- package/dist/{index-78t37mcq.js → index-nmkv8yvs.js} +3 -3
- package/dist/{index-tt0vs4x5.js → index-qna2ra0s.js} +1 -1
- package/dist/{index-qfcjrnpe.js → index-r6r2eh90.js} +4 -4
- package/dist/{index-g0mb2kb4.js → index-ran2b3fn.js} +10 -10
- package/dist/{index-5vsk2cpp.js → index-s7460efs.js} +12 -1
- package/dist/{index-08jfpny9.js → index-sr8dmp7p.js} +1 -1
- package/dist/{index-h9hybzr5.js → index-wfahebsc.js} +5 -5
- package/dist/index-x3rdza40.js +63 -0
- package/dist/{index-60x5d68t.js → index-xn9efn18.js} +1 -1
- package/dist/{index-39rzef89.js → index-ydhymf0b.js} +3 -3
- package/dist/{index-61knrt52.js → index-ywgv1xcb.js} +1 -1
- package/dist/index.js +40 -40
- package/dist/{message-index-bounded-r1qys471.js → message-index-bounded-wvnp6fcp.js} +5 -5
- package/dist/migration-worker.js +4 -4
- package/dist/plugin/hooks/create-session-hooks.d.ts.map +1 -1
- package/dist/{project-embedding-registry-m3fp1pa4.js → project-embedding-registry-8je0xg18.js} +7 -5
- package/dist/raw-ordinal-warmup-worker.js +4 -4
- package/dist/{read-session-chunk-xv9f0wgb.js → read-session-chunk-j4hjc1pk.js} +4 -4
- package/dist/{recomp-orchestrator-dgyb394j.js → recomp-orchestrator-ps1c8c5h.js} +19 -18
- package/dist/{render-d014e603.js → render-nfj7280n.js} +13 -13
- package/dist/{search-9gceftb9.js → search-3r8b0jfa.js} +15 -15
- package/dist/{search-archive-gybn6q7s.js → search-archive-1etwnve4.js} +6 -6
- package/dist/shared/model-suggestion-retry.d.ts +18 -0
- package/dist/shared/model-suggestion-retry.d.ts.map +1 -1
- package/dist/{storage-tags-f1ftvjpw.js → storage-tags-nh2ftqhx.js} +3 -3
- package/dist/{store-n4yz3n66.js → store-019k3ek7.js} +2 -2
- package/dist/{store-reader-nf8z86n9.js → store-reader-a2v34xnh.js} +1 -1
- package/dist/{task-config-qt6n4bxm.js → task-config-vrebjxyy.js} +2 -1
- package/dist/{transform-postprocess-phase-34k1zwsy.js → transform-postprocess-phase-tqp968w4.js} +20 -19
- package/dist/v2/auto-search-worker.js +4 -4
- package/dist/v2/chunk-embedding-write-worker.js +6 -6
- package/dist/v2/{compartment-chunk-embedding-knt2468v.js → compartment-chunk-embedding-w7ht0t9v.js} +3 -3
- package/dist/v2/{context-satgm752.js → context-d57v8c10.js} +557 -234
- package/dist/v2/{embedding-backfill-0ar1bm97.js → embedding-backfill-95m72gxa.js} +6 -6
- package/dist/v2/embedding-worker.js +3 -3
- package/dist/v2/fold/native-replay-v3.d.ts.map +1 -1
- package/dist/v2/hidden-child-native.d.ts +1 -1
- package/dist/v2/hidden-child-native.d.ts.map +1 -1
- package/dist/v2/hidden-child-record.d.ts +11 -2
- package/dist/v2/hidden-child-record.d.ts.map +1 -1
- package/dist/v2/hidden-child-sweep.d.ts +3 -0
- package/dist/v2/hidden-child-sweep.d.ts.map +1 -1
- package/dist/v2/hidden-completion.d.ts +4 -0
- package/dist/v2/hidden-completion.d.ts.map +1 -1
- package/dist/v2/hidden-run-holder.d.ts +2 -2
- package/dist/v2/hidden-run-holder.d.ts.map +1 -1
- package/dist/v2/hooks/context.d.ts +2 -2
- package/dist/v2/hooks/context.d.ts.map +1 -1
- package/dist/v2/hooks/hidden-child.d.ts +2 -0
- package/dist/v2/hooks/hidden-child.d.ts.map +1 -1
- package/dist/v2/{message-index-bounded-b07r9cjv.js → message-index-bounded-2nhbwry6.js} +5 -5
- package/dist/v2/migration-worker.js +4 -4
- package/dist/v2/{project-embedding-registry-gx2waee8.js → project-embedding-registry-fcxf4qht.js} +7 -5
- package/dist/v2/raw-ordinal-warmup-worker.js +4 -4
- package/dist/v2/{read-session-chunk-38wdy8nd.js → read-session-chunk-jrrhwxgx.js} +4 -4
- package/dist/v2/{recomp-orchestrator-wq7gbkqa.js → recomp-orchestrator-n79hdd7n.js} +19 -18
- package/dist/v2/{render-2dan17xc.js → render-7emcjdt1.js} +13 -13
- package/dist/v2/{search-archive-89hdy0a2.js → search-archive-fpjbmxcx.js} +6 -6
- package/dist/v2/{search-nyqykx7q.js → search-vz2cpgr0.js} +14 -14
- package/dist/v2/{server-ytqps2xa.js → server-0h875f56.js} +1 -1
- package/dist/v2/server-3gz9k20t.js +63 -0
- package/dist/v2/{server-ta4h0fch.js → server-3z8rhacq.js} +9 -9
- package/dist/v2/{server-dtcwkdqj.js → server-4axv6kq6.js} +63 -23
- package/dist/v2/{server-c7awk5s2.js → server-4bfk1sky.js} +3 -3
- package/dist/v2/{server-dgjqvpgd.js → server-72k2r3kt.js} +19 -7
- package/dist/v2/{server-pjk9spbf.js → server-7e89a2kf.js} +1 -1
- package/dist/v2/{server-add0j5n3.js → server-8ps3717t.js} +3 -3
- package/dist/v2/{server-a85j3kam.js → server-8r32etq0.js} +1 -1
- package/dist/v2/{server-f7tzvmzs.js → server-b4efn08m.js} +1 -1
- package/dist/v2/{server-y17r5ryz.js → server-barj0mjz.js} +2 -2
- package/dist/v2/{server-yx0nyj1q.js → server-hgfd6c3e.js} +3 -3
- package/dist/v2/{server-qyazxy92.js → server-j9cm1czj.js} +14 -2
- package/dist/v2/{server-9n2qgk29.js → server-kwsqytwj.js} +2 -2
- package/dist/v2/{server-fb5hvvfc.js → server-n431vzn5.js} +3 -3
- package/dist/v2/{server-2kswss3d.js → server-n5wn7d9b.js} +48 -60
- package/dist/v2/{server-x6fn9x8q.js → server-ps6zrzwj.js} +1 -1
- package/dist/v2/{server-pmws7we9.js → server-q104czn6.js} +2 -2
- package/dist/v2/{server-vy0beygw.js → server-q2ap5cty.js} +3 -3
- package/dist/v2/{server-y8xjr8rx.js → server-qb3s0mnc.js} +12 -1
- package/dist/v2/{server-stjy5dxv.js → server-qntmqaca.js} +1 -1
- package/dist/v2/{server-09h2vyvs.js → server-r4816kwv.js} +5 -5
- package/dist/v2/{server-b0a9h3ds.js → server-vfa7byfa.js} +5 -5
- package/dist/v2/{server-tj65pgvg.js → server-vsqmkvxc.js} +32 -5
- package/dist/v2/{server-fn4ky6mn.js → server-xmgvy3cq.js} +1 -1
- package/dist/v2/server.js +4 -3
- package/dist/v2/{storage-tags-pmw33chf.js → storage-tags-t0f9njdp.js} +3 -3
- package/dist/v2/{store-y56y034s.js → store-q3ef5rvz.js} +2 -2
- package/dist/v2/{store-reader-2vhx33n6.js → store-reader-wc01y2rc.js} +1 -1
- package/dist/v2/store-reader.d.ts +2 -0
- package/dist/v2/store-reader.d.ts.map +1 -1
- package/dist/v2/{task-config-ex2vajr2.js → task-config-4nqq2s0w.js} +2 -1
- package/dist/v2/{v2-archive-reader-gh1jghf7.js → v2-archive-reader-9p84yzk5.js} +1 -1
- package/dist/v2/{wrapup-orchestrator-2h7ayss9.js → wrapup-orchestrator-acebazx8.js} +19 -18
- package/dist/{v2-archive-reader-8158kyfd.js → v2-archive-reader-w1bbaqe8.js} +1 -1
- package/dist/{wrapup-orchestrator-ct59br0c.js → wrapup-orchestrator-fwbe0mvm.js} +20 -19
- package/package.json +1 -1
- package/src/config/index.test.ts +58 -0
- package/src/config/index.ts +4 -1
- package/src/config/migrate-dreamer-v2.ts +42 -0
- package/src/shared/model-suggestion-retry.test.ts +37 -0
- package/src/shared/model-suggestion-retry.ts +38 -4
- package/src/shared/startup-maintenance-policy.test.ts +11 -6
- package/dist/index-ksdyrrav.js +0 -52
- package/dist/v2/server-0s95v3en.js +0 -49
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
hiddenToolLoop,
|
|
18
18
|
HIDDEN_RUN_DISPOSED_REASON,
|
|
19
19
|
noteChildOfDisposedInstance,
|
|
20
|
+
noteTimedOutHiddenChild,
|
|
20
21
|
hiddenChildPermissions,
|
|
21
22
|
registerHiddenChildAgents,
|
|
22
23
|
hiddenToolCallRefusal,
|
|
@@ -25,8 +26,11 @@ import {
|
|
|
25
26
|
parseProviderModel,
|
|
26
27
|
toModelEntry,
|
|
27
28
|
ModelProducedNoOutputError,
|
|
29
|
+
promptTransportBudget,
|
|
30
|
+
notePromptSetupTimeout,
|
|
28
31
|
noteModelActivityMonitored,
|
|
29
32
|
noteModelOutputReceived,
|
|
33
|
+
isPromptTimeoutError,
|
|
30
34
|
annotateSubagentInvocationError,
|
|
31
35
|
recordChildInvocation,
|
|
32
36
|
CTX_NOTE_LIGHT_DESCRIPTION,
|
|
@@ -44,7 +48,7 @@ import {
|
|
|
44
48
|
resolveOpenCodeProtectedTailBoundary,
|
|
45
49
|
nonNarrativeStoredGapRanges,
|
|
46
50
|
getActiveCompartmentRun
|
|
47
|
-
} from "./index-
|
|
51
|
+
} from "./index-febyth5n.js";
|
|
48
52
|
import"./index-sd3egckz.js";
|
|
49
53
|
import {
|
|
50
54
|
getHarness2
|
|
@@ -69,16 +73,16 @@ import {
|
|
|
69
73
|
resolveKnownHistorianContextLimit2,
|
|
70
74
|
resolveHistorianContextLimit2
|
|
71
75
|
} from "./index-xsqw8kbd.js";
|
|
72
|
-
import"./index-6nqz8hsz.js";
|
|
73
76
|
import {
|
|
77
|
+
isDreamerRunnable,
|
|
78
|
+
isSmartNoteSurfaceAvailable,
|
|
79
|
+
isCompactionEnabled,
|
|
74
80
|
CANONICAL_DREAM_TASKS,
|
|
75
81
|
DREAM_TASK_CAPABILITIES,
|
|
76
82
|
formatUnsupportedDreamTasks,
|
|
77
|
-
isCanonicalDreamTask
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
userObservationsRequested2
|
|
81
|
-
} from "./index-kknjvmpx.js";
|
|
83
|
+
isCanonicalDreamTask
|
|
84
|
+
} from "./index-fqth7850.js";
|
|
85
|
+
import"./index-6nqz8hsz.js";
|
|
82
86
|
import {
|
|
83
87
|
logConfigDependencyNotesOnce,
|
|
84
88
|
runSessionProjectBackfill,
|
|
@@ -136,7 +140,7 @@ import {
|
|
|
136
140
|
applyJsonSchemaParameterDescriptions,
|
|
137
141
|
MagicContextRpcServer,
|
|
138
142
|
subagentTemperature
|
|
139
|
-
} from "./index-
|
|
143
|
+
} from "./index-m6vhxsce.js";
|
|
140
144
|
import {
|
|
141
145
|
invalidateAutoEmbedSession,
|
|
142
146
|
clearEmbedSessionState
|
|
@@ -145,8 +149,8 @@ import {
|
|
|
145
149
|
hasMeaningfulUserText2,
|
|
146
150
|
preloadTokenizer,
|
|
147
151
|
estimateTokens
|
|
148
|
-
} from "./index-
|
|
149
|
-
import"./index-
|
|
152
|
+
} from "./index-s7460efs.js";
|
|
153
|
+
import"./index-6r99v53h.js";
|
|
150
154
|
import {
|
|
151
155
|
inRootAsyncContext2,
|
|
152
156
|
withSqliteTransformPass2,
|
|
@@ -161,7 +165,7 @@ import"./index-q7q6qp1f.js";
|
|
|
161
165
|
import"./index-s32f8y67.js";
|
|
162
166
|
import {
|
|
163
167
|
deleteChunkEmbedBackoffForSession2
|
|
164
|
-
} from "./index-
|
|
168
|
+
} from "./index-0twg41mh.js";
|
|
165
169
|
import {
|
|
166
170
|
advanceSessionActivity,
|
|
167
171
|
backfillSessionActivity,
|
|
@@ -199,8 +203,8 @@ import {
|
|
|
199
203
|
getSlot,
|
|
200
204
|
dropSlot,
|
|
201
205
|
copySessionStateForClone
|
|
202
|
-
} from "./index-
|
|
203
|
-
import"./index-
|
|
206
|
+
} from "./index-wfahebsc.js";
|
|
207
|
+
import"./index-capcna4r.js";
|
|
204
208
|
import {
|
|
205
209
|
stripPersistedAssistantText,
|
|
206
210
|
currentReclaimableToolOutputCount,
|
|
@@ -242,16 +246,21 @@ import {
|
|
|
242
246
|
estimateFinalWireInputTokens,
|
|
243
247
|
getMaxTagNumberBySession2,
|
|
244
248
|
getTagsBySession2
|
|
245
|
-
} from "./index-
|
|
249
|
+
} from "./index-gqhebzxx.js";
|
|
246
250
|
import {
|
|
247
251
|
BoundedSessionMap,
|
|
248
252
|
getTableColumnNames,
|
|
249
253
|
recordToolDefinitionSet,
|
|
250
254
|
getCurrentToolSetHash
|
|
251
|
-
} from "./index-
|
|
255
|
+
} from "./index-mg9s2rc3.js";
|
|
252
256
|
import {
|
|
253
257
|
resolveHistorianModel2
|
|
254
258
|
} from "./index-5kym00ag.js";
|
|
259
|
+
import {
|
|
260
|
+
buildDreamTaskRuntimeConfigs2,
|
|
261
|
+
userObservationCollectionEnabled2,
|
|
262
|
+
userObservationsRequested2
|
|
263
|
+
} from "./index-x3rdza40.js";
|
|
255
264
|
import {
|
|
256
265
|
formatFailClosedBlockingMessage,
|
|
257
266
|
formatFailClosedBlockingSummary,
|
|
@@ -280,10 +289,10 @@ import {
|
|
|
280
289
|
cleanUserText2,
|
|
281
290
|
readRawSessionMessageIdOrdinalsForRange2,
|
|
282
291
|
readRawSessionMessageOrdinalById2
|
|
283
|
-
} from "./index-
|
|
292
|
+
} from "./index-mq97qmb8.js";
|
|
284
293
|
import {
|
|
285
294
|
scheduleClearAndReindex
|
|
286
|
-
} from "./index-
|
|
295
|
+
} from "./index-dva352qe.js";
|
|
287
296
|
import {
|
|
288
297
|
startBootDeadline,
|
|
289
298
|
getSchemaFenceRejection,
|
|
@@ -293,17 +302,17 @@ import {
|
|
|
293
302
|
openDatabaseAsync,
|
|
294
303
|
isDatabasePersisted,
|
|
295
304
|
getDatabasePersistenceError
|
|
296
|
-
} from "./index-
|
|
305
|
+
} from "./index-5wgdbj2h.js";
|
|
297
306
|
import"./index-fr0wbhdn.js";
|
|
298
|
-
import"./index-
|
|
307
|
+
import"./index-qna2ra0s.js";
|
|
299
308
|
import {
|
|
300
309
|
gaDatabasePath2,
|
|
301
310
|
RAW_MESSAGE_TYPES2,
|
|
302
311
|
V2StoreReaderPool2,
|
|
303
312
|
V2StoreReader2
|
|
304
|
-
} from "./index-
|
|
313
|
+
} from "./index-czz0w2c4.js";
|
|
305
314
|
import"./index-fb3zxn06.js";
|
|
306
|
-
import"./index-
|
|
315
|
+
import"./index-ywgv1xcb.js";
|
|
307
316
|
import {
|
|
308
317
|
resolveContextLimit,
|
|
309
318
|
resolveModelKey,
|
|
@@ -321,20 +330,20 @@ import {
|
|
|
321
330
|
servedHeadChanged,
|
|
322
331
|
noteHostCheckpointCommit,
|
|
323
332
|
isSyntheticHeadMessage2
|
|
324
|
-
} from "./index-
|
|
333
|
+
} from "./index-ran2b3fn.js";
|
|
325
334
|
import"./index-832w615x.js";
|
|
326
335
|
import"./index-qt1qqasc.js";
|
|
327
336
|
import {
|
|
328
337
|
retainProjectEmbedding2
|
|
329
|
-
} from "./index-
|
|
338
|
+
} from "./index-3n376ann.js";
|
|
330
339
|
import"./index-r4y0dczp.js";
|
|
331
340
|
import"./index-qjwpqvnf.js";
|
|
332
|
-
import"./index-
|
|
341
|
+
import"./index-xn9efn18.js";
|
|
333
342
|
import {
|
|
334
343
|
renderUserFacingFailure,
|
|
335
344
|
userFacingFailureCode
|
|
336
|
-
} from "./index-
|
|
337
|
-
import"./index-
|
|
345
|
+
} from "./index-ydhymf0b.js";
|
|
346
|
+
import"./index-f25ha337.js";
|
|
338
347
|
import"./index-9emdjcb5.js";
|
|
339
348
|
import {
|
|
340
349
|
pushLiveNotification2,
|
|
@@ -343,8 +352,8 @@ import {
|
|
|
343
352
|
} from "./index-y14pawe1.js";
|
|
344
353
|
import"./index-e6ncz7a6.js";
|
|
345
354
|
import"./index-6zewd2n5.js";
|
|
346
|
-
import"./index-
|
|
347
|
-
import"./index-
|
|
355
|
+
import"./index-e8my7q6m.js";
|
|
356
|
+
import"./index-r6r2eh90.js";
|
|
348
357
|
import"./index-cr483vx6.js";
|
|
349
358
|
import {
|
|
350
359
|
detectConflicts
|
|
@@ -374,17 +383,12 @@ import {
|
|
|
374
383
|
readV2RawMessagePagesForConversion2,
|
|
375
384
|
createV2RawMessageReader2,
|
|
376
385
|
createV2RawMessageProvider2
|
|
377
|
-
} from "./index-
|
|
386
|
+
} from "./index-sr8dmp7p.js";
|
|
378
387
|
import"./index-fmsr7v69.js";
|
|
379
|
-
import"./index-
|
|
388
|
+
import"./index-nmkv8yvs.js";
|
|
380
389
|
import"./index-52x68mj4.js";
|
|
381
|
-
import"./index-
|
|
382
|
-
import"./index-
|
|
383
|
-
import {
|
|
384
|
-
isDreamerRunnable,
|
|
385
|
-
isSmartNoteSurfaceAvailable,
|
|
386
|
-
isCompactionEnabled
|
|
387
|
-
} from "./index-ksdyrrav.js";
|
|
390
|
+
import"./index-7rgnh11t.js";
|
|
391
|
+
import"./index-6t7jvp76.js";
|
|
388
392
|
import {
|
|
389
393
|
markHostInstructionRow,
|
|
390
394
|
hostInstructionRow,
|
|
@@ -2159,7 +2163,13 @@ function importedFloor(reader, sessionID, through = reader.latestSequence(sessio
|
|
|
2159
2163
|
let after = -1;
|
|
2160
2164
|
let found;
|
|
2161
2165
|
while (true) {
|
|
2162
|
-
const page = reader.page(sessionID, {
|
|
2166
|
+
const page = reader.page(sessionID, {
|
|
2167
|
+
after,
|
|
2168
|
+
through,
|
|
2169
|
+
type: "compaction",
|
|
2170
|
+
limit: 1000,
|
|
2171
|
+
preferTypeIndex: true
|
|
2172
|
+
});
|
|
2163
2173
|
if (!page.rows.length)
|
|
2164
2174
|
break;
|
|
2165
2175
|
for (const row of page.rows) {
|
|
@@ -3881,6 +3891,10 @@ var SWEEP_BUDGET_MS = 30000;
|
|
|
3881
3891
|
function recordHiddenChild(db, child) {
|
|
3882
3892
|
db.prepare("INSERT INTO schema_migrations_meta (key, value) VALUES (?, ?)").run(`${PREFIX}${child.id}`, JSON.stringify(child));
|
|
3883
3893
|
}
|
|
3894
|
+
function recordRetiringHiddenChild(db, child) {
|
|
3895
|
+
db.prepare(`INSERT INTO schema_migrations_meta (key, value) VALUES (?, ?)
|
|
3896
|
+
ON CONFLICT(key) DO UPDATE SET value = json_set(value, '$.retiring', json('true'))`).run(`${PREFIX}${child.id}`, JSON.stringify({ ...child, retiring: true }));
|
|
3897
|
+
}
|
|
3884
3898
|
function forgetHiddenChild(db, id) {
|
|
3885
3899
|
db.prepare("DELETE FROM schema_migrations_meta WHERE key = ?").run(`${PREFIX}${id}`);
|
|
3886
3900
|
}
|
|
@@ -3987,6 +4001,20 @@ function isSessionNotFound(error) {
|
|
|
3987
4001
|
function createNativeHiddenChildren(host, removeSession, options) {
|
|
3988
4002
|
const quiet = supportsQuietHiddenChildren(options.hostVersion);
|
|
3989
4003
|
const removing = new Map;
|
|
4004
|
+
const removeBounded = async (id, timeoutMs = options.removalTimeoutMs ?? 5000) => {
|
|
4005
|
+
let timer;
|
|
4006
|
+
try {
|
|
4007
|
+
await Promise.race([
|
|
4008
|
+
removeSession({ sessionID: id }),
|
|
4009
|
+
new Promise((_, reject) => {
|
|
4010
|
+
timer = setTimeout(() => reject(new Error("session.remove timed out")), timeoutMs);
|
|
4011
|
+
})
|
|
4012
|
+
]);
|
|
4013
|
+
} finally {
|
|
4014
|
+
if (timer)
|
|
4015
|
+
clearTimeout(timer);
|
|
4016
|
+
}
|
|
4017
|
+
};
|
|
3990
4018
|
const remove = (child) => {
|
|
3991
4019
|
const inFlight = removing.get(child.id);
|
|
3992
4020
|
if (inFlight)
|
|
@@ -3994,18 +4022,7 @@ function createNativeHiddenChildren(host, removeSession, options) {
|
|
|
3994
4022
|
options.log(`[magic-context] hidden child ${child.id} removing`);
|
|
3995
4023
|
const done = (async () => {
|
|
3996
4024
|
try {
|
|
3997
|
-
|
|
3998
|
-
try {
|
|
3999
|
-
await Promise.race([
|
|
4000
|
-
removeSession({ sessionID: child.id }),
|
|
4001
|
-
new Promise((_, reject) => {
|
|
4002
|
-
timer = setTimeout(() => reject(new Error("session.remove timed out")), options.removalTimeoutMs ?? 5000);
|
|
4003
|
-
})
|
|
4004
|
-
]);
|
|
4005
|
-
} finally {
|
|
4006
|
-
if (timer)
|
|
4007
|
-
clearTimeout(timer);
|
|
4008
|
-
}
|
|
4025
|
+
await removeBounded(child.id);
|
|
4009
4026
|
forgetHiddenChild(options.db, child.id);
|
|
4010
4027
|
options.log(`[magic-context] hidden child ${child.id} removed`);
|
|
4011
4028
|
} catch (error) {
|
|
@@ -4026,18 +4043,52 @@ function createNativeHiddenChildren(host, removeSession, options) {
|
|
|
4026
4043
|
removing.set(child.id, done);
|
|
4027
4044
|
return done;
|
|
4028
4045
|
};
|
|
4029
|
-
const
|
|
4046
|
+
const retireLate = async (id, directory, createdAt, cleanupMs) => {
|
|
4047
|
+
noteTimedOutHiddenChild(id);
|
|
4048
|
+
liveChildren.delete(id);
|
|
4049
|
+
try {
|
|
4050
|
+
recordRetiringHiddenChild(options.db, { id, directory, created_at: createdAt });
|
|
4051
|
+
} catch (error) {
|
|
4052
|
+
const kind = error instanceof Error ? error.constructor.name : typeof error;
|
|
4053
|
+
options.log(`[magic-context] late hidden child ${id} record failed (${kind})`);
|
|
4054
|
+
}
|
|
4055
|
+
try {
|
|
4056
|
+
await removeBounded(id, cleanupMs);
|
|
4057
|
+
} catch (error) {
|
|
4058
|
+
if (!isSessionNotFound(error)) {
|
|
4059
|
+
options.log(`[magic-context] late hidden child ${id} could not be removed`);
|
|
4060
|
+
return;
|
|
4061
|
+
}
|
|
4062
|
+
}
|
|
4063
|
+
try {
|
|
4064
|
+
forgetHiddenChild(options.db, id);
|
|
4065
|
+
} catch {}
|
|
4066
|
+
};
|
|
4067
|
+
const create = async (identity, role, model, budget) => {
|
|
4030
4068
|
const input = childCreateInput(identity, role, model);
|
|
4031
4069
|
const directory = identity.directory;
|
|
4032
4070
|
if (quiet) {
|
|
4033
4071
|
if (!host.createHolder)
|
|
4034
4072
|
throw new Error("OpenCode 2 background holder API is not wired");
|
|
4035
|
-
const placement = await hiddenRunPlacement(host.createHolder, directory);
|
|
4073
|
+
const placement = await hiddenRunPlacement(host.createHolder, directory, budget);
|
|
4036
4074
|
input.id = placement.id;
|
|
4037
4075
|
input.parentID = placement.parentID;
|
|
4038
4076
|
delete input.location;
|
|
4039
4077
|
}
|
|
4040
|
-
|
|
4078
|
+
budget?.setStage("creating the child session");
|
|
4079
|
+
const start = () => host.create(input);
|
|
4080
|
+
const removeLate = ({ id }) => {
|
|
4081
|
+
if (!id || !budget)
|
|
4082
|
+
return;
|
|
4083
|
+
retireLate(id, directory, Date.now(), budget.cleanupMs);
|
|
4084
|
+
};
|
|
4085
|
+
const created = await (budget?.work(start, removeLate) ?? start());
|
|
4086
|
+
try {
|
|
4087
|
+
budget?.check();
|
|
4088
|
+
} catch (error) {
|
|
4089
|
+
removeLate(created);
|
|
4090
|
+
throw error;
|
|
4091
|
+
}
|
|
4041
4092
|
if (!created.id)
|
|
4042
4093
|
throw new Error("OpenCode 2 did not return a child session id");
|
|
4043
4094
|
const createdAt = Date.now();
|
|
@@ -4052,20 +4103,12 @@ function createNativeHiddenChildren(host, removeSession, options) {
|
|
|
4052
4103
|
});
|
|
4053
4104
|
} catch (storeError) {
|
|
4054
4105
|
let removed = false;
|
|
4055
|
-
let timer;
|
|
4056
4106
|
try {
|
|
4057
|
-
await
|
|
4058
|
-
removeSession({ sessionID: created.id }),
|
|
4059
|
-
new Promise((_, reject) => {
|
|
4060
|
-
timer = setTimeout(() => reject(new Error("session.remove timed out")), options.removalTimeoutMs ?? 5000);
|
|
4061
|
-
})
|
|
4062
|
-
]);
|
|
4107
|
+
await removeBounded(created.id);
|
|
4063
4108
|
removed = true;
|
|
4064
4109
|
} catch (error) {
|
|
4065
4110
|
removed = isSessionNotFound(error);
|
|
4066
4111
|
} finally {
|
|
4067
|
-
if (timer)
|
|
4068
|
-
clearTimeout(timer);
|
|
4069
4112
|
liveChildren.delete(created.id);
|
|
4070
4113
|
}
|
|
4071
4114
|
options.log(`[magic-context] hidden child ${created.id} record failed; removed=${removed}: ${errorText(storeError)}`);
|
|
@@ -4093,6 +4136,9 @@ function createNativeHiddenChildren(host, removeSession, options) {
|
|
|
4093
4136
|
return {
|
|
4094
4137
|
open: create,
|
|
4095
4138
|
create,
|
|
4139
|
+
retireLate(child, cleanupMs) {
|
|
4140
|
+
retireLate(child.id, child.directory ?? "", child.created_at, cleanupMs);
|
|
4141
|
+
},
|
|
4096
4142
|
retire(child) {
|
|
4097
4143
|
if (kept(child)) {
|
|
4098
4144
|
liveChildren.delete(child.id);
|
|
@@ -4191,17 +4237,83 @@ async function cleanupLegacyHiddenChildren(db, remove, note, options = {}) {
|
|
|
4191
4237
|
|
|
4192
4238
|
// src/v2/hidden-completion.ts
|
|
4193
4239
|
var DEFAULT_DISPOSE_DRAIN_MS = 2000;
|
|
4240
|
+
|
|
4241
|
+
class HiddenWorkTimeout extends Error {
|
|
4242
|
+
constructor(timeoutMs) {
|
|
4243
|
+
super(`prompt timed out after ${timeoutMs}ms`);
|
|
4244
|
+
}
|
|
4245
|
+
}
|
|
4246
|
+
|
|
4247
|
+
class HiddenSetupTimeout extends HiddenCompletionRefusal {
|
|
4248
|
+
constructor(stage) {
|
|
4249
|
+
super("hidden_setup_timeout", `Hidden completion setup timed out while ${stage}`, true);
|
|
4250
|
+
}
|
|
4251
|
+
}
|
|
4252
|
+
|
|
4253
|
+
class HiddenWorkAborted extends Error {
|
|
4254
|
+
constructor() {
|
|
4255
|
+
super("Hidden completion prompt aborted");
|
|
4256
|
+
}
|
|
4257
|
+
}
|
|
4258
|
+
function setupBudget(deadline, timeout, signal) {
|
|
4259
|
+
let stage = "preparing the attempt";
|
|
4260
|
+
let lost;
|
|
4261
|
+
const check = () => {
|
|
4262
|
+
if (lost)
|
|
4263
|
+
throw lost;
|
|
4264
|
+
if (Date.now() >= deadline)
|
|
4265
|
+
throw lost = timeout(stage);
|
|
4266
|
+
if (signal?.aborted)
|
|
4267
|
+
throw lost = new HiddenWorkAborted;
|
|
4268
|
+
};
|
|
4269
|
+
return {
|
|
4270
|
+
check,
|
|
4271
|
+
setStage: (value) => {
|
|
4272
|
+
stage = value;
|
|
4273
|
+
},
|
|
4274
|
+
get stage() {
|
|
4275
|
+
return stage;
|
|
4276
|
+
},
|
|
4277
|
+
cleanupMs: DEFAULT_DISPOSE_DRAIN_MS,
|
|
4278
|
+
async work(start, onLate) {
|
|
4279
|
+
check();
|
|
4280
|
+
let timer;
|
|
4281
|
+
let abort;
|
|
4282
|
+
try {
|
|
4283
|
+
return await Promise.race([
|
|
4284
|
+
start().then((result) => {
|
|
4285
|
+
try {
|
|
4286
|
+
check();
|
|
4287
|
+
} catch (error) {
|
|
4288
|
+
onLate?.(result);
|
|
4289
|
+
throw error;
|
|
4290
|
+
}
|
|
4291
|
+
return result;
|
|
4292
|
+
}),
|
|
4293
|
+
new Promise((_, reject) => {
|
|
4294
|
+
timer = setTimeout(() => reject(lost ??= timeout(stage)), Math.max(0, deadline - Date.now()));
|
|
4295
|
+
abort = () => reject(lost ??= new HiddenWorkAborted);
|
|
4296
|
+
if (signal?.aborted)
|
|
4297
|
+
abort();
|
|
4298
|
+
else
|
|
4299
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
4300
|
+
})
|
|
4301
|
+
]);
|
|
4302
|
+
} finally {
|
|
4303
|
+
clearTimeout(timer);
|
|
4304
|
+
if (abort)
|
|
4305
|
+
signal?.removeEventListener("abort", abort);
|
|
4306
|
+
}
|
|
4307
|
+
}
|
|
4308
|
+
};
|
|
4309
|
+
}
|
|
4194
4310
|
async function settleWithin(work, ms) {
|
|
4195
4311
|
let timer;
|
|
4196
4312
|
try {
|
|
4197
|
-
await Promise.race([
|
|
4198
|
-
work.then(() =>
|
|
4199
|
-
return;
|
|
4200
|
-
}, () => {
|
|
4201
|
-
return;
|
|
4202
|
-
}),
|
|
4313
|
+
return await Promise.race([
|
|
4314
|
+
work.then(() => true, () => true),
|
|
4203
4315
|
new Promise((resolve) => {
|
|
4204
|
-
timer = setTimeout(resolve, Math.max(0, ms));
|
|
4316
|
+
timer = setTimeout(() => resolve(false), Math.max(0, ms));
|
|
4205
4317
|
})
|
|
4206
4318
|
]);
|
|
4207
4319
|
} finally {
|
|
@@ -4398,8 +4510,9 @@ function isProviderFailure(error) {
|
|
|
4398
4510
|
const type = error.type;
|
|
4399
4511
|
return typeof type === "string" && (type.startsWith("provider.") || type.toLowerCase().includes("provider"));
|
|
4400
4512
|
}
|
|
4401
|
-
async function awaitAssistantRow(openReader, readSessionError, localFailure, childID, afterSeq, deadline, signal) {
|
|
4513
|
+
async function awaitAssistantRow(openReader, readSessionError, localFailure, childID, afterSeq, deadline, signal, checkDeadline) {
|
|
4402
4514
|
for (;; ) {
|
|
4515
|
+
checkDeadline?.();
|
|
4403
4516
|
const refusedBefore = localFailure();
|
|
4404
4517
|
if (refusedBefore)
|
|
4405
4518
|
throw refusedBefore;
|
|
@@ -4455,9 +4568,6 @@ async function awaitAssistantRow(openReader, readSessionError, localFailure, chi
|
|
|
4455
4568
|
return { row: newAssistant };
|
|
4456
4569
|
}
|
|
4457
4570
|
}
|
|
4458
|
-
if (Date.now() >= deadline) {
|
|
4459
|
-
throw new Error("Hidden completion timed out waiting for a persisted assistant row");
|
|
4460
|
-
}
|
|
4461
4571
|
await sleepUntilPoll(signal, deadline);
|
|
4462
4572
|
}
|
|
4463
4573
|
}
|
|
@@ -4522,11 +4632,11 @@ async function createV2HiddenCompletionExecutor(host, options) {
|
|
|
4522
4632
|
};
|
|
4523
4633
|
};
|
|
4524
4634
|
const warnedVariants = new Set;
|
|
4525
|
-
const validateVariant = async (model) => {
|
|
4635
|
+
const validateVariant = async (model, work) => {
|
|
4526
4636
|
if (!model.variant || !options.modelCatalog)
|
|
4527
4637
|
return model;
|
|
4528
4638
|
try {
|
|
4529
|
-
const listed = await options.modelCatalog
|
|
4639
|
+
const listed = await work(options.modelCatalog);
|
|
4530
4640
|
const rows = Array.isArray(listed) ? listed : listed && typeof listed === "object" && Array.isArray(listed.data) ? listed.data : [];
|
|
4531
4641
|
const entry = rows.find((row) => row && typeof row === "object" && row.providerID === model.providerID && row.id === model.modelID);
|
|
4532
4642
|
if (!entry)
|
|
@@ -4539,18 +4649,21 @@ async function createV2HiddenCompletionExecutor(host, options) {
|
|
|
4539
4649
|
note(`[magic-context] ${recordHiddenVariantWarning(model.providerID, model.modelID, model.variant)}`);
|
|
4540
4650
|
}
|
|
4541
4651
|
return { providerID: model.providerID, modelID: model.modelID };
|
|
4542
|
-
} catch {
|
|
4652
|
+
} catch (error) {
|
|
4653
|
+
if (error instanceof HiddenSetupTimeout || error instanceof HiddenWorkAborted)
|
|
4654
|
+
throw error;
|
|
4543
4655
|
return model;
|
|
4544
4656
|
}
|
|
4545
4657
|
};
|
|
4546
|
-
const resolveHead = async (identity) => {
|
|
4658
|
+
const resolveHead = async (identity, work) => {
|
|
4547
4659
|
const configured = configuredHead(identity);
|
|
4548
4660
|
if (configured)
|
|
4549
|
-
return validateVariant(configured);
|
|
4550
|
-
|
|
4661
|
+
return validateVariant(configured, work);
|
|
4662
|
+
const parentSessionId = identity.parentSessionId;
|
|
4663
|
+
if (!parentSessionId) {
|
|
4551
4664
|
throw new HiddenCompletionRefusal("hidden_model_unsupported", "Hidden completion requires a configured model or an existing parent session model", true);
|
|
4552
4665
|
}
|
|
4553
|
-
const parent = await host.get({ sessionID:
|
|
4666
|
+
const parent = await work(() => host.get({ sessionID: parentSessionId }));
|
|
4554
4667
|
if (!parent.model) {
|
|
4555
4668
|
throw new HiddenCompletionRefusal("hidden_model_unsupported", "Hidden completion could not resolve the parent session model", true);
|
|
4556
4669
|
}
|
|
@@ -4560,17 +4673,18 @@ async function createV2HiddenCompletionExecutor(host, options) {
|
|
|
4560
4673
|
...parent.model.variant ? { variant: parent.model.variant } : {}
|
|
4561
4674
|
};
|
|
4562
4675
|
};
|
|
4563
|
-
const switchChildModel = async (run, requested) => {
|
|
4676
|
+
const switchChildModel = async (run, requested, setup) => {
|
|
4564
4677
|
if (sameModel(run.child.model, requested))
|
|
4565
4678
|
return;
|
|
4566
|
-
await host.switchModel({
|
|
4679
|
+
await setup.work(() => host.switchModel({
|
|
4567
4680
|
sessionID: run.child.id,
|
|
4568
4681
|
model: {
|
|
4569
4682
|
providerID: requested.providerID,
|
|
4570
4683
|
id: requested.modelID,
|
|
4571
4684
|
...requested.variant ? { variant: requested.variant } : {}
|
|
4572
4685
|
}
|
|
4573
|
-
});
|
|
4686
|
+
}));
|
|
4687
|
+
setup.check();
|
|
4574
4688
|
run.child = lifecycle.updateModel(run.child, requested);
|
|
4575
4689
|
};
|
|
4576
4690
|
const partialCompletion = (run) => {
|
|
@@ -4602,30 +4716,63 @@ async function createV2HiddenCompletionExecutor(host, options) {
|
|
|
4602
4716
|
lifecycle.retire(run.child, reason);
|
|
4603
4717
|
run.retired = true;
|
|
4604
4718
|
};
|
|
4605
|
-
const interruptAndRetire =
|
|
4606
|
-
|
|
4607
|
-
|
|
4608
|
-
|
|
4609
|
-
|
|
4610
|
-
|
|
4719
|
+
const interruptAndRetire = (run, reason) => {
|
|
4720
|
+
if (run.retired)
|
|
4721
|
+
return run.retirement ?? Promise.resolve();
|
|
4722
|
+
return run.retirement ??= (async () => {
|
|
4723
|
+
const id = run.child.id;
|
|
4724
|
+
const warn = (error) => {
|
|
4725
|
+
const kind = error instanceof Error ? error.constructor.name : typeof error;
|
|
4726
|
+
const message = `[magic-context] hidden child ${id} could not be interrupted (${kind})`;
|
|
4727
|
+
if (options.log)
|
|
4728
|
+
options.log(message);
|
|
4729
|
+
else
|
|
4730
|
+
log2(message);
|
|
4731
|
+
};
|
|
4732
|
+
try {
|
|
4733
|
+
await settleWithin(host.interrupt({ sessionID: id }).catch(warn), DEFAULT_DISPOSE_DRAIN_MS);
|
|
4734
|
+
} catch (error) {
|
|
4735
|
+
warn(error);
|
|
4736
|
+
} finally {
|
|
4737
|
+
retire(run, reason);
|
|
4738
|
+
}
|
|
4739
|
+
})();
|
|
4611
4740
|
};
|
|
4612
4741
|
return {
|
|
4613
4742
|
capabilities: V2_HIDDEN_EXECUTOR_CAPABILITIES,
|
|
4614
4743
|
async open(identity) {
|
|
4615
4744
|
if (disposed)
|
|
4616
4745
|
throw disposed;
|
|
4746
|
+
const roleWait = setupBudget(Date.now() + identity.timeoutMs, (stage) => new HiddenSetupTimeout(stage));
|
|
4747
|
+
roleWait.setStage("waiting for its role");
|
|
4617
4748
|
const role = roleFor(identity);
|
|
4618
4749
|
const releaseStore = options.holdStore?.() ?? (() => {});
|
|
4619
4750
|
let releaseRole;
|
|
4620
4751
|
let openedChild;
|
|
4621
4752
|
let opened;
|
|
4622
4753
|
try {
|
|
4623
|
-
releaseRole = await acquireRole(role);
|
|
4754
|
+
releaseRole = await roleWait.work(() => acquireRole(role), (release) => release());
|
|
4624
4755
|
if (disposed)
|
|
4625
4756
|
throw disposed;
|
|
4626
|
-
|
|
4627
|
-
const
|
|
4628
|
-
const
|
|
4757
|
+
const deadline = Date.now() + identity.timeoutMs;
|
|
4758
|
+
const setup = setupBudget(deadline, (stage) => new HiddenSetupTimeout(stage));
|
|
4759
|
+
const work = setup.work;
|
|
4760
|
+
const ensureAgent = options.ensureAgent;
|
|
4761
|
+
if (ensureAgent) {
|
|
4762
|
+
setup.setStage("reloading the hidden agent");
|
|
4763
|
+
let pending;
|
|
4764
|
+
try {
|
|
4765
|
+
await work(() => pending = ensureAgent.call(options));
|
|
4766
|
+
} catch (error) {
|
|
4767
|
+
if (pending)
|
|
4768
|
+
options.forgetAgent?.(pending);
|
|
4769
|
+
throw error;
|
|
4770
|
+
}
|
|
4771
|
+
}
|
|
4772
|
+
setup.setStage("resolving the hidden model");
|
|
4773
|
+
const head = await resolveHead(identity, work);
|
|
4774
|
+
setup.setStage("creating the child session");
|
|
4775
|
+
const active = await work(() => lifecycle.open(identity, role, head, setup), (child) => lifecycle.retireLate(child, setup.cleanupMs));
|
|
4629
4776
|
openedChild = active;
|
|
4630
4777
|
const configuredModel = toModelEntry(identity.model);
|
|
4631
4778
|
const handle = {
|
|
@@ -4642,6 +4789,7 @@ async function createV2HiddenCompletionExecutor(host, options) {
|
|
|
4642
4789
|
child: active,
|
|
4643
4790
|
releaseRole,
|
|
4644
4791
|
releaseStore,
|
|
4792
|
+
firstDeadline: deadline,
|
|
4645
4793
|
failed: false,
|
|
4646
4794
|
unsettledFailure: false,
|
|
4647
4795
|
retired: false
|
|
@@ -4651,15 +4799,23 @@ async function createV2HiddenCompletionExecutor(host, options) {
|
|
|
4651
4799
|
openRuns.add(run);
|
|
4652
4800
|
if (disposed)
|
|
4653
4801
|
noteChildOfDisposedInstance(active.id);
|
|
4654
|
-
|
|
4802
|
+
setup.setStage("switching the child model");
|
|
4803
|
+
await switchChildModel(run, head, setup);
|
|
4804
|
+
setup.check();
|
|
4655
4805
|
return handle;
|
|
4656
4806
|
} catch (error) {
|
|
4657
|
-
|
|
4658
|
-
|
|
4659
|
-
|
|
4660
|
-
|
|
4661
|
-
|
|
4662
|
-
|
|
4807
|
+
try {
|
|
4808
|
+
if (openedChild) {
|
|
4809
|
+
if (error instanceof HiddenSetupTimeout)
|
|
4810
|
+
noteTimedOutHiddenChild(openedChild.id);
|
|
4811
|
+
lifecycle.retire(openedChild, "hidden-run-open-failed");
|
|
4812
|
+
}
|
|
4813
|
+
} finally {
|
|
4814
|
+
if (opened)
|
|
4815
|
+
openRuns.delete(opened);
|
|
4816
|
+
releaseRole?.();
|
|
4817
|
+
releaseStore();
|
|
4818
|
+
}
|
|
4663
4819
|
throw error;
|
|
4664
4820
|
}
|
|
4665
4821
|
},
|
|
@@ -4668,6 +4824,35 @@ async function createV2HiddenCompletionExecutor(host, options) {
|
|
|
4668
4824
|
const run = runs.get(handle);
|
|
4669
4825
|
if (!run)
|
|
4670
4826
|
throw new Error("Unknown hidden completion run");
|
|
4827
|
+
const timeoutMs = run.identity.timeoutMs;
|
|
4828
|
+
const deadline = run.firstDeadline ?? startedAt + timeoutMs;
|
|
4829
|
+
run.firstDeadline = undefined;
|
|
4830
|
+
let dispatched = false;
|
|
4831
|
+
const setup = setupBudget(deadline, (stage) => dispatched ? new HiddenWorkTimeout(timeoutMs) : new HiddenSetupTimeout(stage), request.signal);
|
|
4832
|
+
const work = setup.work;
|
|
4833
|
+
const sharedBudget = promptTransportBudget(request.signal);
|
|
4834
|
+
const refuseSliver = () => {
|
|
4835
|
+
const now = Date.now();
|
|
4836
|
+
if (deadline - now < timeoutMs * 0.05 || sharedBudget && sharedBudget.deadline - now < sharedBudget.timeoutMs * 0.05)
|
|
4837
|
+
throw new HiddenSetupTimeout("preparing to send the prompt");
|
|
4838
|
+
};
|
|
4839
|
+
const retireFailedSetup = (error) => {
|
|
4840
|
+
if (error instanceof HiddenSetupTimeout) {
|
|
4841
|
+
run.deadlineExpired = true;
|
|
4842
|
+
noteTimedOutHiddenChild(run.child.id);
|
|
4843
|
+
}
|
|
4844
|
+
run.failed = true;
|
|
4845
|
+
run.unsettledFailure = true;
|
|
4846
|
+
if (!run.retirement)
|
|
4847
|
+
retire(run, "hidden-attempt-setup-cancelled");
|
|
4848
|
+
};
|
|
4849
|
+
const stopWithinWork = async (reason) => {
|
|
4850
|
+
const retirement = interruptAndRetire(run, reason);
|
|
4851
|
+
if (Date.now() < deadline)
|
|
4852
|
+
await settleWithin(retirement, deadline - Date.now());
|
|
4853
|
+
if (Date.now() >= deadline)
|
|
4854
|
+
run.deadlineExpired = true;
|
|
4855
|
+
};
|
|
4671
4856
|
const refuseDisposed = () => {
|
|
4672
4857
|
if (!disposed)
|
|
4673
4858
|
return;
|
|
@@ -4678,46 +4863,78 @@ async function createV2HiddenCompletionExecutor(host, options) {
|
|
|
4678
4863
|
};
|
|
4679
4864
|
refuseDisposed();
|
|
4680
4865
|
if (request.signal?.aborted) {
|
|
4681
|
-
await
|
|
4866
|
+
await stopWithinWork("aborted-before-prompt");
|
|
4682
4867
|
throw new Error("Hidden completion prompt aborted");
|
|
4683
4868
|
}
|
|
4684
|
-
|
|
4685
|
-
|
|
4686
|
-
|
|
4687
|
-
|
|
4688
|
-
|
|
4689
|
-
|
|
4690
|
-
|
|
4691
|
-
|
|
4692
|
-
|
|
4693
|
-
|
|
4694
|
-
|
|
4695
|
-
|
|
4696
|
-
|
|
4697
|
-
|
|
4698
|
-
|
|
4699
|
-
|
|
4700
|
-
|
|
4701
|
-
|
|
4702
|
-
|
|
4703
|
-
|
|
4704
|
-
|
|
4705
|
-
|
|
4706
|
-
|
|
4707
|
-
|
|
4708
|
-
|
|
4709
|
-
|
|
4710
|
-
|
|
4711
|
-
|
|
4712
|
-
|
|
4713
|
-
|
|
4714
|
-
|
|
4715
|
-
|
|
4716
|
-
|
|
4717
|
-
|
|
4718
|
-
|
|
4869
|
+
let requested;
|
|
4870
|
+
let requestedRef;
|
|
4871
|
+
notePromptSetupTimeout(request.signal, () => new HiddenSetupTimeout(setup.stage));
|
|
4872
|
+
try {
|
|
4873
|
+
const previousRetirement = run.retirement;
|
|
4874
|
+
setup.setStage("waiting for the previous child to retire");
|
|
4875
|
+
if (previousRetirement)
|
|
4876
|
+
await work(() => previousRetirement);
|
|
4877
|
+
setup.setStage("resolving the hidden model");
|
|
4878
|
+
requested = await validateVariant(requestModel(request, run.child.model), work);
|
|
4879
|
+
requestedRef = modelKey(requested);
|
|
4880
|
+
const configuredModel = [
|
|
4881
|
+
run.identity.model,
|
|
4882
|
+
...run.identity.configuredModels ?? []
|
|
4883
|
+
].map(toModelEntry).find((entry) => entry?.model === requestedRef);
|
|
4884
|
+
handle.attemptedModel = requestedRef;
|
|
4885
|
+
handle.modelConfigKey = configuredModel?.sourceConfigKey ?? handle.modelConfigKey;
|
|
4886
|
+
if (run.identity.kind !== "dreamer-task" && request.body.temperature === undefined) {
|
|
4887
|
+
const configured = options.historianTemperature?.();
|
|
4888
|
+
if (typeof configured === "number" && Number.isFinite(configured))
|
|
4889
|
+
request = {
|
|
4890
|
+
...request,
|
|
4891
|
+
body: { ...request.body, temperature: configured }
|
|
4892
|
+
};
|
|
4893
|
+
}
|
|
4894
|
+
if (typeof request.body.temperature === "number") {
|
|
4895
|
+
let metadata;
|
|
4896
|
+
try {
|
|
4897
|
+
const listed = options.modelCatalog ? await work(options.modelCatalog) : undefined;
|
|
4898
|
+
const rows = Array.isArray(listed) ? listed : listed?.data;
|
|
4899
|
+
metadata = rows?.find((row) => row?.providerID === requested.providerID && (row?.id === requested.modelID || row?.modelID === requested.modelID));
|
|
4900
|
+
} catch (error) {
|
|
4901
|
+
if (error instanceof HiddenSetupTimeout || error instanceof HiddenWorkAborted)
|
|
4902
|
+
throw error;
|
|
4903
|
+
}
|
|
4904
|
+
const temperature = subagentTemperature(modelKey(requested), metadata, request.body.temperature, (message) => note(message));
|
|
4905
|
+
const body = { ...request.body };
|
|
4906
|
+
if (temperature === undefined)
|
|
4907
|
+
delete body.temperature;
|
|
4908
|
+
else
|
|
4909
|
+
body.temperature = temperature;
|
|
4910
|
+
request = { ...request, body };
|
|
4911
|
+
}
|
|
4912
|
+
if (needsBlockedRead(run.identity.agent, run.child.model.providerID) !== needsBlockedRead(run.identity.agent, requested.providerID)) {
|
|
4913
|
+
retire(run, "hidden-run-tool-surface-changed");
|
|
4914
|
+
}
|
|
4915
|
+
if (run.retired) {
|
|
4916
|
+
setup.setStage("creating the replacement child session");
|
|
4917
|
+
run.child = await work(() => lifecycle.create(run.identity, run.role, requested, setup), (child) => lifecycle.retireLate(child, setup.cleanupMs));
|
|
4918
|
+
run.retirement = undefined;
|
|
4919
|
+
run.deadlineExpired = false;
|
|
4920
|
+
run.failed = false;
|
|
4921
|
+
run.unsettledFailure = false;
|
|
4922
|
+
run.retired = false;
|
|
4923
|
+
handle.id = run.child.id;
|
|
4924
|
+
handle.childSessionId = run.child.id;
|
|
4925
|
+
}
|
|
4926
|
+
setup.setStage("switching the child model");
|
|
4927
|
+
await switchChildModel(run, requested, setup);
|
|
4928
|
+
setup.check();
|
|
4929
|
+
setup.setStage("preparing to send the prompt");
|
|
4930
|
+
refuseSliver();
|
|
4931
|
+
} catch (error) {
|
|
4932
|
+
if (error instanceof HiddenSetupTimeout || error instanceof HiddenWorkAborted)
|
|
4933
|
+
retireFailedSetup(error);
|
|
4934
|
+
throw error;
|
|
4935
|
+
} finally {
|
|
4936
|
+
notePromptSetupTimeout(request.signal);
|
|
4719
4937
|
}
|
|
4720
|
-
await switchChildModel(run, requested);
|
|
4721
4938
|
refuseDisposed();
|
|
4722
4939
|
const baseline = withReader(options.openReader, (reader) => reader.latestSequence(run.child.id));
|
|
4723
4940
|
const marker = `mc:hidden:${crypto.randomUUID()}:${crypto.randomUUID()}`;
|
|
@@ -4732,7 +4949,6 @@ async function createV2HiddenCompletionExecutor(host, options) {
|
|
|
4732
4949
|
run.partialCompletion = undefined;
|
|
4733
4950
|
run.attempt = { child: attempt, baseline };
|
|
4734
4951
|
options.hook.registerAttempt(marker, attempt);
|
|
4735
|
-
const deadline = Date.now() + run.identity.timeoutMs;
|
|
4736
4952
|
lifecycle.extendDeadline(run.child, deadline);
|
|
4737
4953
|
const budget = run.budget;
|
|
4738
4954
|
if (budget?.snapshot().finalizeFired) {
|
|
@@ -4791,6 +5007,8 @@ async function createV2HiddenCompletionExecutor(host, options) {
|
|
|
4791
5007
|
});
|
|
4792
5008
|
const onAbort = () => {
|
|
4793
5009
|
const reason = request.signal?.reason;
|
|
5010
|
+
if (dispatched)
|
|
5011
|
+
noteTimedOutHiddenChild(run.child.id);
|
|
4794
5012
|
if (reason instanceof ModelProducedNoOutputError && run.identity.kind === "dreamer-task") {
|
|
4795
5013
|
recordChildInvocation({
|
|
4796
5014
|
db: options.db,
|
|
@@ -4808,9 +5026,6 @@ async function createV2HiddenCompletionExecutor(host, options) {
|
|
|
4808
5026
|
}
|
|
4809
5027
|
interruptAndRetire(run, "prompt-aborted").finally(() => abortReject(new Error("Hidden completion prompt aborted")));
|
|
4810
5028
|
};
|
|
4811
|
-
const deadlineTimer = setTimeout(() => {
|
|
4812
|
-
interruptAndRetire(run, "prompt-timeout").finally(() => abortReject(new Error("Hidden completion prompt timed out")));
|
|
4813
|
-
}, Math.max(0, deadline - Date.now()));
|
|
4814
5029
|
request.signal?.addEventListener("abort", onAbort, { once: true });
|
|
4815
5030
|
let settle;
|
|
4816
5031
|
const entry = {
|
|
@@ -4819,8 +5034,6 @@ async function createV2HiddenCompletionExecutor(host, options) {
|
|
|
4819
5034
|
noteChildOfDisposedInstance(run.child.id);
|
|
4820
5035
|
try {
|
|
4821
5036
|
await interruptAndRetire(run, "plugin-instance-disposed");
|
|
4822
|
-
} catch (error) {
|
|
4823
|
-
note(`[magic-context] hidden child ${run.child.id} could not be interrupted on dispose: ${errorText(error)}`);
|
|
4824
5037
|
} finally {
|
|
4825
5038
|
abortReject(refusal);
|
|
4826
5039
|
}
|
|
@@ -4839,28 +5052,32 @@ async function createV2HiddenCompletionExecutor(host, options) {
|
|
|
4839
5052
|
noteModelActivityMonitored(request.signal);
|
|
4840
5053
|
try {
|
|
4841
5054
|
await Promise.race([
|
|
4842
|
-
|
|
5055
|
+
work(() => {
|
|
5056
|
+
refuseSliver();
|
|
5057
|
+
dispatched = true;
|
|
5058
|
+
return host.prompt({ sessionID: run.child.id, text: marker });
|
|
5059
|
+
}),
|
|
4843
5060
|
aborted,
|
|
4844
5061
|
...budget ? [budgetStopped] : []
|
|
4845
5062
|
]);
|
|
4846
5063
|
await Promise.race([
|
|
4847
|
-
host.wait({ sessionID: run.child.id }),
|
|
5064
|
+
work(() => host.wait({ sessionID: run.child.id })),
|
|
4848
5065
|
aborted,
|
|
4849
5066
|
...budget ? [budgetStopped] : []
|
|
4850
5067
|
]);
|
|
4851
5068
|
const { row, outputLimit } = await Promise.race([
|
|
4852
|
-
awaitAssistantRow(options.openReader, async () => {
|
|
5069
|
+
work(() => awaitAssistantRow(options.openReader, async () => {
|
|
4853
5070
|
try {
|
|
4854
|
-
const
|
|
4855
|
-
|
|
4856
|
-
});
|
|
5071
|
+
const id = run.child.id;
|
|
5072
|
+
const terminalError = host.terminalError;
|
|
5073
|
+
const eventError = terminalError ? await work(() => terminalError.call(host, { sessionID: id })) : undefined;
|
|
4857
5074
|
if (eventError !== undefined)
|
|
4858
5075
|
return eventError;
|
|
4859
|
-
return (await host.get({ sessionID:
|
|
5076
|
+
return (await work(() => host.get({ sessionID: id }))).error;
|
|
4860
5077
|
} catch {
|
|
4861
5078
|
return;
|
|
4862
5079
|
}
|
|
4863
|
-
}, () => attempt.refusal ?? attempt.stepLimit, run.child.id, baseline, deadline, request.signal),
|
|
5080
|
+
}, () => attempt.refusal ?? attempt.stepLimit, run.child.id, baseline, deadline, request.signal, setup.check)),
|
|
4864
5081
|
aborted,
|
|
4865
5082
|
...budget ? [budgetStopped] : []
|
|
4866
5083
|
]);
|
|
@@ -4876,15 +5093,17 @@ async function createV2HiddenCompletionExecutor(host, options) {
|
|
|
4876
5093
|
const steps = attemptRows.filter((step) => step.type === "assistant" && step.seq > baseline && step.seq <= row.seq);
|
|
4877
5094
|
const exceeded = chargeBudgetRows(steps);
|
|
4878
5095
|
if (exceeded) {
|
|
4879
|
-
await
|
|
5096
|
+
await stopWithinWork("token-budget");
|
|
4880
5097
|
throw exceeded;
|
|
4881
5098
|
}
|
|
4882
5099
|
const cappedChainStart = outputLimit ? outputLimitChainStart(attemptRows, row.seq) : row.seq;
|
|
4883
5100
|
const usageRows = cappedChainStart < row.seq ? steps.filter((step) => step.seq < cappedChainStart || step.seq === row.seq) : steps;
|
|
4884
5101
|
const reportedUsage = usageRows.some((step) => tokenNumber(step.data.tokens?.input) !== undefined || tokenNumber(step.data.tokens?.output) !== undefined);
|
|
4885
5102
|
const usage = sumRowUsage(usageRows);
|
|
4886
|
-
if (
|
|
4887
|
-
|
|
5103
|
+
if (Date.now() >= deadline)
|
|
5104
|
+
throw new HiddenWorkTimeout(timeoutMs);
|
|
5105
|
+
if (request.signal?.aborted || run.retired) {
|
|
5106
|
+
await stopWithinWork("prompt-aborted-or-timeout");
|
|
4888
5107
|
throw new Error(request.signal?.aborted ? "Hidden completion prompt aborted" : "Hidden completion prompt timed out");
|
|
4889
5108
|
}
|
|
4890
5109
|
run.completion = {
|
|
@@ -4913,17 +5132,25 @@ async function createV2HiddenCompletionExecutor(host, options) {
|
|
|
4913
5132
|
if (!(error instanceof HiddenProviderError)) {
|
|
4914
5133
|
run.unsettledFailure = true;
|
|
4915
5134
|
}
|
|
4916
|
-
|
|
4917
|
-
|
|
4918
|
-
|
|
4919
|
-
|
|
5135
|
+
const dispatchedTimeout = dispatched && isPromptTimeoutError(error);
|
|
5136
|
+
if (error instanceof HiddenSetupTimeout) {
|
|
5137
|
+
retireFailedSetup(error);
|
|
5138
|
+
} else if (dispatched && (caught instanceof HiddenWorkTimeout || Date.now() >= deadline)) {
|
|
5139
|
+
run.deadlineExpired = true;
|
|
5140
|
+
noteTimedOutHiddenChild(run.child.id);
|
|
5141
|
+
interruptAndRetire(run, "prompt-timeout");
|
|
5142
|
+
} else if (request.signal?.aborted && !run.retired) {
|
|
5143
|
+
await stopWithinWork("prompt-aborted");
|
|
5144
|
+
} else if (!run.retired && (dispatchedTimeout || error instanceof Error && error.message.includes("timed out"))) {
|
|
5145
|
+
if (dispatchedTimeout)
|
|
5146
|
+
noteTimedOutHiddenChild(run.child.id);
|
|
5147
|
+
await stopWithinWork("prompt-timeout");
|
|
4920
5148
|
} else if ((error instanceof HiddenAgentStepLimit || error instanceof HiddenProviderError && error.settled) && !run.retired) {
|
|
4921
|
-
await
|
|
5149
|
+
await stopWithinWork(error instanceof HiddenAgentStepLimit ? "hidden-run-step-limit" : "hidden-run-provider-error");
|
|
4922
5150
|
}
|
|
4923
5151
|
throw error;
|
|
4924
5152
|
} finally {
|
|
4925
5153
|
stopWatching?.();
|
|
4926
|
-
clearTimeout(deadlineTimer);
|
|
4927
5154
|
if (budgetPoll)
|
|
4928
5155
|
clearInterval(budgetPoll);
|
|
4929
5156
|
request.signal?.removeEventListener("abort", onAbort);
|
|
@@ -4950,7 +5177,18 @@ async function createV2HiddenCompletionExecutor(host, options) {
|
|
|
4950
5177
|
const run = runs.get(handle);
|
|
4951
5178
|
if (!run)
|
|
4952
5179
|
return;
|
|
5180
|
+
if (run.retirement && (!run.retired || run.deadlineExpired)) {
|
|
5181
|
+
runs.delete(handle);
|
|
5182
|
+
run.retirement.finally(() => {
|
|
5183
|
+
openRuns.delete(run);
|
|
5184
|
+
run.releaseRole();
|
|
5185
|
+
run.releaseStore();
|
|
5186
|
+
});
|
|
5187
|
+
return;
|
|
5188
|
+
}
|
|
4953
5189
|
try {
|
|
5190
|
+
if (run.retirement)
|
|
5191
|
+
await run.retirement;
|
|
4954
5192
|
const reusable = !run.retired && run.failed && !run.unsettledFailure;
|
|
4955
5193
|
if (hiddenToolLoop(run.identity)) {
|
|
4956
5194
|
retire(run, settlement.promptSettled ? "tool-loop-settled" : "tool-loop-failed");
|
|
@@ -4963,7 +5201,9 @@ async function createV2HiddenCompletionExecutor(host, options) {
|
|
|
4963
5201
|
run.releaseRole();
|
|
4964
5202
|
run.releaseStore();
|
|
4965
5203
|
}
|
|
4966
|
-
|
|
5204
|
+
const finished = lifecycle.finish(run.child, run.retired);
|
|
5205
|
+
if (!run.deadlineExpired)
|
|
5206
|
+
await finished;
|
|
4967
5207
|
},
|
|
4968
5208
|
async dispose() {
|
|
4969
5209
|
disposed ??= new HiddenCompletionRefusal("hidden_instance_disposed", HIDDEN_RUN_DISPOSED_REASON, true);
|
|
@@ -6893,9 +7133,23 @@ function servableCarrier(stored, head) {
|
|
|
6893
7133
|
}
|
|
6894
7134
|
function bindKeptInstructionCoordinates(messages, incoming, reader, sessionID) {
|
|
6895
7135
|
const rows = reader.instructionRowsThrough(sessionID, reader.latestSequence(sessionID));
|
|
7136
|
+
const visible = (message) => message !== incoming && !effortPart(message);
|
|
7137
|
+
const ids = new Set;
|
|
7138
|
+
for (const message of messages) {
|
|
7139
|
+
if (!visible(message))
|
|
7140
|
+
continue;
|
|
7141
|
+
const id = nativeRowID(message);
|
|
7142
|
+
if (id)
|
|
7143
|
+
ids.add(id);
|
|
7144
|
+
}
|
|
7145
|
+
const sequences = reader.sequencesForIds(sessionID, [...ids]);
|
|
6896
7146
|
let previous;
|
|
6897
7147
|
let pending = [];
|
|
6898
7148
|
const matches = (message, row) => message?.role === "system" && message.content.length === 1 && message.content[0]?.type === "text" && message.content[0].text === row?.data.text;
|
|
7149
|
+
const bindRow = (message, row) => {
|
|
7150
|
+
bindNativeRowID(message, row.id, row.seq);
|
|
7151
|
+
sequences.set(row.id, row.seq);
|
|
7152
|
+
};
|
|
6899
7153
|
const bind = (next) => {
|
|
6900
7154
|
if (!pending.length)
|
|
6901
7155
|
return;
|
|
@@ -6905,7 +7159,7 @@ function bindKeptInstructionCoordinates(messages, incoming, reader, sessionID) {
|
|
|
6905
7159
|
selected.forEach((row, index) => {
|
|
6906
7160
|
const message = pending[index];
|
|
6907
7161
|
if (message)
|
|
6908
|
-
|
|
7162
|
+
bindRow(message, row);
|
|
6909
7163
|
});
|
|
6910
7164
|
} else {
|
|
6911
7165
|
const earliest = [];
|
|
@@ -6926,16 +7180,16 @@ function bindKeptInstructionCoordinates(messages, incoming, reader, sessionID) {
|
|
|
6926
7180
|
pending.forEach((message, index) => {
|
|
6927
7181
|
const row = candidates[earliest[index]];
|
|
6928
7182
|
if (row && earliest[index] === latest[index])
|
|
6929
|
-
|
|
7183
|
+
bindRow(message, row);
|
|
6930
7184
|
});
|
|
6931
7185
|
}
|
|
6932
7186
|
pending = [];
|
|
6933
7187
|
};
|
|
6934
7188
|
for (const message of messages) {
|
|
6935
|
-
if (
|
|
7189
|
+
if (!visible(message))
|
|
6936
7190
|
continue;
|
|
6937
7191
|
const id = nativeRowID(message);
|
|
6938
|
-
const seq = id ?
|
|
7192
|
+
const seq = id ? sequences.get(id) : undefined;
|
|
6939
7193
|
if (seq !== undefined) {
|
|
6940
7194
|
bind(seq);
|
|
6941
7195
|
previous = seq;
|
|
@@ -7284,9 +7538,31 @@ function coveredInstructionRows(reader, sessionID, throughSeq, model, retired =
|
|
|
7284
7538
|
return [];
|
|
7285
7539
|
return reader.instructionRowsThrough(sessionID, throughSeq).filter((row) => !retired(row)).flatMap((row) => restoreRow(row, model)).map((message) => markCoveredInstructionRow(message));
|
|
7286
7540
|
}
|
|
7541
|
+
var contextDisposalOrder = [
|
|
7542
|
+
"sweep",
|
|
7543
|
+
"backfill",
|
|
7544
|
+
"idle",
|
|
7545
|
+
"abortUsage",
|
|
7546
|
+
"hidden",
|
|
7547
|
+
"joinUsage",
|
|
7548
|
+
"rpc",
|
|
7549
|
+
"transform",
|
|
7550
|
+
"tools",
|
|
7551
|
+
"deletedSessions",
|
|
7552
|
+
"dream",
|
|
7553
|
+
"maintenance",
|
|
7554
|
+
"embeddings",
|
|
7555
|
+
"rawProviders",
|
|
7556
|
+
"restoredRows",
|
|
7557
|
+
"readerPool",
|
|
7558
|
+
"synthetic"
|
|
7559
|
+
];
|
|
7287
7560
|
async function registerContext(context) {
|
|
7288
7561
|
const directory = context.location.directory;
|
|
7289
7562
|
const config = loadPluginConfigDetailed(directory).config;
|
|
7563
|
+
for (const warning of new Set(config.configWarnings)) {
|
|
7564
|
+
log2(`[magic-context] config warning: ${warning}`);
|
|
7565
|
+
}
|
|
7290
7566
|
log2(`[magic-context] OpenCode 2 keep_subagents=${config.keep_subagents === true}`);
|
|
7291
7567
|
setHomeProjectPermission(config.allow_home_project);
|
|
7292
7568
|
if (!config.enabled)
|
|
@@ -7312,7 +7588,6 @@ async function registerContext(context) {
|
|
|
7312
7588
|
});
|
|
7313
7589
|
return;
|
|
7314
7590
|
}
|
|
7315
|
-
const liveConfigReader = pluginConfigReader(directory, config);
|
|
7316
7591
|
const compactionOff = !isCompactionEnabled(config);
|
|
7317
7592
|
const conflicts = detectConflicts(directory, {
|
|
7318
7593
|
compactionEnabled: !compactionOff,
|
|
@@ -7322,7 +7597,32 @@ async function registerContext(context) {
|
|
|
7322
7597
|
console.warn(`[magic-context] v2 setup disabled by conflicting context hooks: ${conflicts.reasons.join("; ")}`);
|
|
7323
7598
|
return;
|
|
7324
7599
|
}
|
|
7600
|
+
const cleanup = {};
|
|
7601
|
+
const dispose = async (failedSetup = false) => {
|
|
7602
|
+
for (const resource of contextDisposalOrder) {
|
|
7603
|
+
try {
|
|
7604
|
+
const drain = cleanup[resource]?.(failedSetup);
|
|
7605
|
+
if (drain)
|
|
7606
|
+
await drain;
|
|
7607
|
+
} catch (error) {
|
|
7608
|
+
const kind = error instanceof Error ? error.constructor.name : typeof error;
|
|
7609
|
+
log2(`[magic-context] v2 ${resource} cleanup failed (${kind})`);
|
|
7610
|
+
}
|
|
7611
|
+
}
|
|
7612
|
+
};
|
|
7613
|
+
try {
|
|
7614
|
+
await setupContext(context, config, nativeRemove, compactionOff, cleanup);
|
|
7615
|
+
} catch (error) {
|
|
7616
|
+
await dispose(true);
|
|
7617
|
+
throw error;
|
|
7618
|
+
}
|
|
7619
|
+
return { dispose: () => dispose() };
|
|
7620
|
+
}
|
|
7621
|
+
async function setupContext(context, config, nativeRemove, compactionOff, cleanup) {
|
|
7622
|
+
const directory = context.location.directory;
|
|
7623
|
+
const liveConfigReader = pluginConfigReader(directory, config);
|
|
7325
7624
|
const embeddings = createV2EmbeddingScope(resolveProjectIdentityForSession(directory, config.allow_home_project));
|
|
7625
|
+
cleanup.embeddings = () => embeddings.dispose();
|
|
7326
7626
|
const folds = new FoldOwner(context.storage);
|
|
7327
7627
|
const nativeFoldMemory = compactionOff ? undefined : nativeFoldCache(() => storage.require());
|
|
7328
7628
|
const loggedUncoveredCheckpoints = new BoundedSessionMap(1000);
|
|
@@ -7390,6 +7690,11 @@ async function registerContext(context) {
|
|
|
7390
7690
|
isLive: isLiveHiddenChild
|
|
7391
7691
|
});
|
|
7392
7692
|
let hiddenChildSweepTimer;
|
|
7693
|
+
cleanup.sweep = () => {
|
|
7694
|
+
if (hiddenChildSweepTimer)
|
|
7695
|
+
clearInterval(hiddenChildSweepTimer);
|
|
7696
|
+
return hiddenChildSweep.dispose();
|
|
7697
|
+
};
|
|
7393
7698
|
const sweepChildren = (database) => {
|
|
7394
7699
|
hiddenChildSweep.start(database);
|
|
7395
7700
|
};
|
|
@@ -7455,6 +7760,8 @@ async function registerContext(context) {
|
|
|
7455
7760
|
});
|
|
7456
7761
|
});
|
|
7457
7762
|
const tools = db && isDatabasePersisted(db) ? await registerTools(context, db, config, moduleToolBackends?.backends, (id) => hiddenChildHook.owns(id), embeddings.ensureRegistered) : undefined;
|
|
7763
|
+
if (tools)
|
|
7764
|
+
cleanup.tools = () => tools.dispose();
|
|
7458
7765
|
const generateReplay = new V2GenerateReplay;
|
|
7459
7766
|
await context.session.hook("http.response", async (draft) => {
|
|
7460
7767
|
if (!db || draft.kind !== "primary" || draft.response.ok)
|
|
@@ -7478,6 +7785,11 @@ async function registerContext(context) {
|
|
|
7478
7785
|
});
|
|
7479
7786
|
let hiddenAgentsReady;
|
|
7480
7787
|
const readerPool = new V2StoreReaderPool2;
|
|
7788
|
+
cleanup.readerPool = async () => {
|
|
7789
|
+
if (!await readerPool.closeWhenIdle(STORE_POOL_DISPOSE_WAIT_MS))
|
|
7790
|
+
log2(`[magic-context] v2 store reader pool still held by ${readerPool.holdCount()} hidden run(s) at dispose; it closes when they finish`);
|
|
7791
|
+
};
|
|
7792
|
+
cleanup.synthetic = () => clearSyntheticCandidates(context);
|
|
7481
7793
|
const messageTimesCache = new V2MessageTimesCache;
|
|
7482
7794
|
const openStoreReader = () => readerPool.open(gaDatabasePath2(getDataDir(), process.env.OPENCODE_CHANNEL ?? "latest"));
|
|
7483
7795
|
const revertMessageCleanup = new V2RevertMessageCleanup(openStoreReader);
|
|
@@ -7548,6 +7860,10 @@ async function registerContext(context) {
|
|
|
7548
7860
|
hostVersion: context.app?.version,
|
|
7549
7861
|
keepSubagents: config.keep_subagents === true,
|
|
7550
7862
|
ensureAgent: () => hiddenAgentsReady ??= context.agent.reload(),
|
|
7863
|
+
forgetAgent: (pending) => {
|
|
7864
|
+
if (hiddenAgentsReady === pending)
|
|
7865
|
+
hiddenAgentsReady = undefined;
|
|
7866
|
+
},
|
|
7551
7867
|
modelCatalog: () => Promise.resolve(context.model.list()),
|
|
7552
7868
|
historianTemperature: () => config.historian?.temperature,
|
|
7553
7869
|
openReader: openStoreReader,
|
|
@@ -7581,9 +7897,12 @@ async function registerContext(context) {
|
|
|
7581
7897
|
return trigger;
|
|
7582
7898
|
};
|
|
7583
7899
|
let hiddenCompletionExecutor = db && isDatabasePersisted(db) && resolveProjectIdentityForSession(directory, config.allow_home_project) ? await createHiddenExecutor(db) : undefined;
|
|
7900
|
+
cleanup.hidden = () => hiddenCompletionExecutor?.dispose();
|
|
7584
7901
|
let dreamTrigger = hiddenCompletionExecutor ? startDreamer(hiddenCompletionExecutor) : undefined;
|
|
7902
|
+
cleanup.dream = () => dreamTrigger?.dispose();
|
|
7585
7903
|
const lateHiddenExecutor = createLateHiddenExecutor(() => hiddenCompletionExecutor);
|
|
7586
7904
|
let maintenanceTimer;
|
|
7905
|
+
cleanup.maintenance = () => maintenanceTimer?.dispose();
|
|
7587
7906
|
const startMaintenance = () => {
|
|
7588
7907
|
const timerProjectIdentity = resolveProjectIdentityForSession(directory, config.allow_home_project);
|
|
7589
7908
|
if (!timerProjectIdentity || maintenanceTimer)
|
|
@@ -7644,6 +7963,12 @@ async function registerContext(context) {
|
|
|
7644
7963
|
const commitSeenLastPass = new Map;
|
|
7645
7964
|
const restoredRows = new RestoredRowCache;
|
|
7646
7965
|
const rawProviders = new Map;
|
|
7966
|
+
cleanup.restoredRows = () => restoredRows.clear();
|
|
7967
|
+
cleanup.rawProviders = () => {
|
|
7968
|
+
for (const release of rawProviders.values())
|
|
7969
|
+
release();
|
|
7970
|
+
rawProviders.clear();
|
|
7971
|
+
};
|
|
7647
7972
|
let passDuties;
|
|
7648
7973
|
let toolDuties;
|
|
7649
7974
|
const droppedInputGuard = createDroppedInputGuard();
|
|
@@ -7682,8 +8007,9 @@ async function registerContext(context) {
|
|
|
7682
8007
|
};
|
|
7683
8008
|
if (db && isDatabasePersisted(db)) {
|
|
7684
8009
|
const backfillDb = db;
|
|
7685
|
-
|
|
7686
|
-
|
|
8010
|
+
let running;
|
|
8011
|
+
const timer = scheduleAfterBootQuiet(() => {
|
|
8012
|
+
running = runV2SessionProjectBackfill(backfillDb, openStoreReader, config.allow_home_project).then(() => backfillSessionActivity(backfillDb, "opencode", (sessionId) => {
|
|
7687
8013
|
const reader = openStoreReader();
|
|
7688
8014
|
try {
|
|
7689
8015
|
return reader.latestMessageTime(sessionId);
|
|
@@ -7692,6 +8018,17 @@ async function registerContext(context) {
|
|
|
7692
8018
|
}
|
|
7693
8019
|
})).catch((error) => log2("[session-project-backfill] OpenCode 2 backfill failed:", error));
|
|
7694
8020
|
});
|
|
8021
|
+
cleanup.backfill = (failedSetup) => {
|
|
8022
|
+
if (!failedSetup)
|
|
8023
|
+
return;
|
|
8024
|
+
clearTimeout(timer);
|
|
8025
|
+
if (!running)
|
|
8026
|
+
return;
|
|
8027
|
+
return settleWithin(running, DEFAULT_DISPOSE_DRAIN_MS).then((joined) => {
|
|
8028
|
+
if (!joined)
|
|
8029
|
+
log2("[magic-context] v2 backfill still running after setup cleanup");
|
|
8030
|
+
});
|
|
8031
|
+
};
|
|
7695
8032
|
}
|
|
7696
8033
|
const readAllForConversion = (sessionID) => readAllV2RawMessagesForConversion2(openStoreReader, sessionID);
|
|
7697
8034
|
const readRowsFrom = (sessionID, messageID) => {
|
|
@@ -7712,10 +8049,12 @@ async function registerContext(context) {
|
|
|
7712
8049
|
}) : undefined;
|
|
7713
8050
|
const lkgSystems = new V2LkgSystemReplay;
|
|
7714
8051
|
let transform;
|
|
8052
|
+
cleanup.transform = () => transform?.disposeRust();
|
|
7715
8053
|
let systemPrompt;
|
|
7716
8054
|
const systemPromptRefreshSessions = new Set;
|
|
7717
8055
|
const tagger = createTagger();
|
|
7718
8056
|
const deletedSessions = new DeletedSessionTombstones;
|
|
8057
|
+
cleanup.deletedSessions = () => deletedSessions.clear();
|
|
7719
8058
|
const forkSeedSettled = new Set;
|
|
7720
8059
|
const inheritForkState = (database, sessionID) => {
|
|
7721
8060
|
if (forkSeedSettled.has(sessionID))
|
|
@@ -7886,6 +8225,12 @@ async function registerContext(context) {
|
|
|
7886
8225
|
log: sessionLog2
|
|
7887
8226
|
}) : undefined;
|
|
7888
8227
|
const hostFoldIdle = hostFolds ? new HostFoldIdle((sessionID, eligible) => hostFolds.idle(sessionID, eligible)) : undefined;
|
|
8228
|
+
cleanup.idle = () => {
|
|
8229
|
+
messageTimesCache.clear();
|
|
8230
|
+
recoveredInstructions.clear();
|
|
8231
|
+
loggedUncoveredCheckpoints.clear();
|
|
8232
|
+
hostFoldIdle?.dispose();
|
|
8233
|
+
};
|
|
7889
8234
|
const armBindingRecoveryFromEvent = (sessionID, error, source) => {
|
|
7890
8235
|
if (error === undefined || !db || compactionOff)
|
|
7891
8236
|
return;
|
|
@@ -7921,6 +8266,7 @@ async function registerContext(context) {
|
|
|
7921
8266
|
}
|
|
7922
8267
|
};
|
|
7923
8268
|
const usageController = new AbortController;
|
|
8269
|
+
cleanup.abortUsage = () => usageController.abort();
|
|
7924
8270
|
const usageDone = (async () => {
|
|
7925
8271
|
let retry = 0;
|
|
7926
8272
|
while (!usageController.signal.aborted) {
|
|
@@ -8100,6 +8446,7 @@ async function registerContext(context) {
|
|
|
8100
8446
|
}
|
|
8101
8447
|
}
|
|
8102
8448
|
})();
|
|
8449
|
+
cleanup.joinUsage = () => usageDone;
|
|
8103
8450
|
const m0RenderOptions = (draft, store, state) => {
|
|
8104
8451
|
const modelKey = `${draft.model.providerID}/${draft.model.id}`;
|
|
8105
8452
|
const contextLimit = resolveContextLimit(draft.model.providerID, draft.model.id, {
|
|
@@ -9137,6 +9484,10 @@ async function registerContext(context) {
|
|
|
9137
9484
|
const storageDir = getMagicContextStorageDir();
|
|
9138
9485
|
const rpcServer = new MagicContextRpcServer(storageDir, directory);
|
|
9139
9486
|
let rpcStopped = false;
|
|
9487
|
+
cleanup.rpc = () => {
|
|
9488
|
+
rpcStopped = true;
|
|
9489
|
+
rpcServer.stop();
|
|
9490
|
+
};
|
|
9140
9491
|
registerRpcHandlers(rpcServer, {
|
|
9141
9492
|
directory,
|
|
9142
9493
|
config,
|
|
@@ -9243,35 +9594,6 @@ async function registerContext(context) {
|
|
|
9243
9594
|
return;
|
|
9244
9595
|
rpcServer.start().catch((error) => log2("[magic-context] v2 RPC server failed to start", error));
|
|
9245
9596
|
}, 0));
|
|
9246
|
-
return {
|
|
9247
|
-
async dispose() {
|
|
9248
|
-
if (hiddenChildSweepTimer)
|
|
9249
|
-
clearInterval(hiddenChildSweepTimer);
|
|
9250
|
-
await hiddenChildSweep.dispose();
|
|
9251
|
-
messageTimesCache.clear();
|
|
9252
|
-
recoveredInstructions.clear();
|
|
9253
|
-
loggedUncoveredCheckpoints.clear();
|
|
9254
|
-
hostFoldIdle?.dispose();
|
|
9255
|
-
usageController.abort();
|
|
9256
|
-
await hiddenCompletionExecutor?.dispose();
|
|
9257
|
-
await usageDone;
|
|
9258
|
-
rpcStopped = true;
|
|
9259
|
-
rpcServer.stop();
|
|
9260
|
-
transform?.disposeRust();
|
|
9261
|
-
tools?.dispose();
|
|
9262
|
-
deletedSessions.clear();
|
|
9263
|
-
await dreamTrigger?.dispose();
|
|
9264
|
-
await maintenanceTimer?.dispose();
|
|
9265
|
-
await embeddings.dispose();
|
|
9266
|
-
for (const release of rawProviders.values())
|
|
9267
|
-
release();
|
|
9268
|
-
rawProviders.clear();
|
|
9269
|
-
restoredRows.clear();
|
|
9270
|
-
if (!await readerPool.closeWhenIdle(STORE_POOL_DISPOSE_WAIT_MS))
|
|
9271
|
-
log2(`[magic-context] v2 store reader pool still held by ${readerPool.holdCount()} hidden run(s) at dispose; it closes when they finish`);
|
|
9272
|
-
clearSyntheticCandidates(context);
|
|
9273
|
-
}
|
|
9274
|
-
};
|
|
9275
9597
|
}
|
|
9276
9598
|
export {
|
|
9277
9599
|
registerContext
|