@cortexkit/opencode-magic-context 0.49.24 → 0.49.25
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 +1 -1
- package/dist/chunk-embedding-write-worker.js +2 -2
- package/dist/{context-tvrjhnmj.js → context-keb00be8.js} +22 -16
- package/dist/features/magic-context/tool-definition-tokens.d.ts +113 -57
- package/dist/features/magic-context/tool-definition-tokens.d.ts.map +1 -1
- package/dist/hooks/magic-context/final-wire-token-estimate.d.ts +12 -4
- package/dist/hooks/magic-context/final-wire-token-estimate.d.ts.map +1 -1
- package/dist/hooks/magic-context/hook.d.ts.map +1 -1
- package/dist/hooks/magic-context/lkg-measured-request.d.ts.map +1 -1
- package/dist/hooks/magic-context/lkg-replay-fit.d.ts.map +1 -1
- package/dist/hooks/magic-context/rust-mode-transform.d.ts.map +1 -1
- package/dist/hooks/magic-context/signed-thinking-fit-snapshot.d.ts +42 -0
- package/dist/hooks/magic-context/signed-thinking-fit-snapshot.d.ts.map +1 -0
- package/dist/hooks/magic-context/transform.d.ts.map +1 -1
- package/dist/{index-dds7cxhz.js → index-0s8454t2.js} +9 -9
- package/dist/{index-p6evxfcc.js → index-awcftgmx.js} +5 -47
- package/dist/{index-8e9qsnrw.js → index-axjgxeww.js} +6 -4
- package/dist/{index-57jf5ztp.js → index-bhyksfyy.js} +1 -1
- package/dist/index-cfaet9cg.js +405 -0
- package/dist/{index-7516anfj.js → index-ebsvmc5s.js} +3 -3
- package/dist/{index-21bkd3m6.js → index-n18sch77.js} +1 -1
- package/dist/{index-jrjr58ez.js → index-q7ywt6xt.js} +2 -2
- package/dist/{index-6g7tyb6e.js → index-y6xpzjzr.js} +69 -24
- package/dist/{index-t1zk694n.js → index-yrrsp47d.js} +13 -11
- package/dist/{index-jd0y50w3.js → index-zzrzjnx5.js} +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +25 -20
- package/dist/{message-index-1sgzev6c.js → message-index-3g966xn7.js} +3 -3
- package/dist/{message-index-bounded-k8pgrxad.js → message-index-bounded-vaeqhp9w.js} +3 -3
- package/dist/migration-worker.js +2 -2
- package/dist/plugin/hooks/create-session-hooks.d.ts.map +1 -1
- package/dist/plugin/rpc-handlers.d.ts.map +1 -1
- package/dist/raw-ordinal-warmup-worker.js +3 -3
- package/dist/{read-session-chunk-q9z6s48t.js → read-session-chunk-z5gnc0zb.js} +2 -2
- package/dist/{recomp-orchestrator-hr8ddk8z.js → recomp-orchestrator-qwqeskhq.js} +6 -6
- package/dist/{search-3zjjmc60.js → search-84zfqeky.js} +4 -4
- package/dist/{search-archive-c00etw7k.js → search-archive-mfabzyn2.js} +3 -3
- package/dist/{transform-postprocess-phase-rredw6bz.js → transform-postprocess-phase-hr1ntmjj.js} +6 -6
- package/dist/v2/auto-search-worker.js +1 -1
- package/dist/v2/chunk-embedding-write-worker.js +2 -2
- package/dist/v2/{context-zbydzq4g.js → context-y7wfvdve.js} +77 -26
- package/dist/v2/hooks/context.d.ts +4 -0
- package/dist/v2/hooks/context.d.ts.map +1 -1
- package/dist/v2/{message-index-bounded-a9xa8wsq.js → message-index-bounded-te303atj.js} +3 -3
- package/dist/v2/{message-index-5gnnn64b.js → message-index-fz6b4jeh.js} +3 -3
- package/dist/v2/migration-worker.js +2 -2
- package/dist/v2/raw-ordinal-warmup-worker.js +3 -3
- package/dist/v2/{read-session-chunk-qqvda7zv.js → read-session-chunk-knvkq1hf.js} +2 -2
- package/dist/v2/{recomp-orchestrator-nmt9y25j.js → recomp-orchestrator-e8a4jpvr.js} +5 -5
- package/dist/v2/{search-archive-dhqj1w31.js → search-archive-vrwgxv6z.js} +3 -3
- package/dist/v2/{search-6dawx2gt.js → search-hav4dggf.js} +4 -4
- package/dist/v2/{server-gwmshgx9.js → server-5brp2rfn.js} +13 -11
- package/dist/v2/{server-mefps930.js → server-6a8pxpxg.js} +5 -47
- package/dist/v2/server-7d6m1p2b.js +361 -0
- package/dist/v2/{server-r209n2fk.js → server-7weh65vd.js} +9 -9
- package/dist/v2/{server-y9ncq4ck.js → server-e75vag9d.js} +2 -2
- package/dist/v2/{server-16dpnz56.js → server-m6yyn961.js} +1 -1
- package/dist/v2/{server-9e55n4wx.js → server-tfawsj08.js} +1 -1
- package/dist/v2/server.js +2 -2
- package/dist/v2/{wrapup-orchestrator-9z81xj66.js → wrapup-orchestrator-ycs8y6n4.js} +5 -5
- package/dist/{wrapup-orchestrator-dbnb530w.js → wrapup-orchestrator-f642w8n3.js} +7 -7
- package/package.json +1 -1
- package/dist/index-j76ah6d7.js +0 -120
- package/dist/v2/server-d1dzgvwd.js +0 -120
|
@@ -7,10 +7,10 @@ import {
|
|
|
7
7
|
setRecompTerminal2,
|
|
8
8
|
buildRecompDeps2,
|
|
9
9
|
runManagedRecomp2
|
|
10
|
-
} from "./server-
|
|
10
|
+
} from "./server-7weh65vd.js";
|
|
11
11
|
import"./server-806rzhs6.js";
|
|
12
12
|
import"./server-9xbbaekh.js";
|
|
13
|
-
import"./server-
|
|
13
|
+
import"./server-6a8pxpxg.js";
|
|
14
14
|
import"./server-8j2e7t31.js";
|
|
15
15
|
import"./server-jjvbk92s.js";
|
|
16
16
|
import"./server-q5z0gag0.js";
|
|
@@ -18,7 +18,7 @@ import"./server-4dhv7mqz.js";
|
|
|
18
18
|
import"./server-53eh1r8r.js";
|
|
19
19
|
import"./server-6v3cdzsx.js";
|
|
20
20
|
import"./server-qt1qqasc.js";
|
|
21
|
-
import"./server-
|
|
21
|
+
import"./server-5brp2rfn.js";
|
|
22
22
|
import"./server-fa7vtp40.js";
|
|
23
23
|
import"./server-1zx87sdm.js";
|
|
24
24
|
import"./server-3vhxy2k5.js";
|
|
@@ -31,14 +31,14 @@ import"./server-24g8r2d0.js";
|
|
|
31
31
|
import"./server-bsgd4dk1.js";
|
|
32
32
|
import"./server-pw21ypwb.js";
|
|
33
33
|
import"./server-ab1g0wn9.js";
|
|
34
|
-
import"./server-
|
|
34
|
+
import"./server-e75vag9d.js";
|
|
35
35
|
import"./server-fb3zxn06.js";
|
|
36
36
|
import"./server-wwaw5npa.js";
|
|
37
37
|
import"./server-fr0wbhdn.js";
|
|
38
38
|
import"./server-qjvk4a9t.js";
|
|
39
39
|
import"./server-wxrdher8.js";
|
|
40
40
|
import"./server-k3t06fxr.js";
|
|
41
|
-
import"./server-
|
|
41
|
+
import"./server-7d6m1p2b.js";
|
|
42
42
|
import"./server-rqebawx1.js";
|
|
43
43
|
import"./server-2ra6pwf3.js";
|
|
44
44
|
import"./server-swvhxhxz.js";
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import {
|
|
2
2
|
renderSearchArchive2,
|
|
3
3
|
renderSearchArchiveOffThread2
|
|
4
|
-
} from "./server-
|
|
4
|
+
} from "./server-m6yyn961.js";
|
|
5
5
|
import"./server-kaqs6h31.js";
|
|
6
|
-
import"./server-
|
|
6
|
+
import"./server-5brp2rfn.js";
|
|
7
7
|
import"./server-fa7vtp40.js";
|
|
8
8
|
import"./server-1zx87sdm.js";
|
|
9
9
|
import"./server-3vhxy2k5.js";
|
|
10
10
|
import"./server-24g8r2d0.js";
|
|
11
|
-
import"./server-
|
|
11
|
+
import"./server-7d6m1p2b.js";
|
|
12
12
|
import"./server-rqebawx1.js";
|
|
13
13
|
import"./server-2ra6pwf3.js";
|
|
14
14
|
import"./server-swvhxhxz.js";
|
|
@@ -5,16 +5,16 @@ import {
|
|
|
5
5
|
resolveMemoriesByIdsForSearch2,
|
|
6
6
|
unifiedSearch2,
|
|
7
7
|
formatSearchResults2
|
|
8
|
-
} from "./server-
|
|
8
|
+
} from "./server-tfawsj08.js";
|
|
9
9
|
import"./server-kaqs6h31.js";
|
|
10
10
|
import"./server-9xbbaekh.js";
|
|
11
|
-
import"./server-
|
|
11
|
+
import"./server-6a8pxpxg.js";
|
|
12
12
|
import"./server-8j2e7t31.js";
|
|
13
13
|
import"./server-jjvbk92s.js";
|
|
14
14
|
import"./server-q5z0gag0.js";
|
|
15
15
|
import"./server-4dhv7mqz.js";
|
|
16
16
|
import"./server-qt1qqasc.js";
|
|
17
|
-
import"./server-
|
|
17
|
+
import"./server-5brp2rfn.js";
|
|
18
18
|
import"./server-fa7vtp40.js";
|
|
19
19
|
import"./server-1zx87sdm.js";
|
|
20
20
|
import"./server-3vhxy2k5.js";
|
|
@@ -28,7 +28,7 @@ import"./server-fr0wbhdn.js";
|
|
|
28
28
|
import"./server-qjvk4a9t.js";
|
|
29
29
|
import"./server-wxrdher8.js";
|
|
30
30
|
import"./server-k3t06fxr.js";
|
|
31
|
-
import"./server-
|
|
31
|
+
import"./server-7d6m1p2b.js";
|
|
32
32
|
import"./server-rqebawx1.js";
|
|
33
33
|
import"./server-2ra6pwf3.js";
|
|
34
34
|
import"./server-swvhxhxz.js";
|
|
@@ -79,9 +79,9 @@ import {
|
|
|
79
79
|
} from "./server-3vhxy2k5.js";
|
|
80
80
|
import {
|
|
81
81
|
getToolDefinitionMeasurementSignature,
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
} from "./server-
|
|
82
|
+
getMeasuredToolDefinitionTokens,
|
|
83
|
+
getSessionToolDefinitionTokens
|
|
84
|
+
} from "./server-7d6m1p2b.js";
|
|
85
85
|
import {
|
|
86
86
|
canonicalModelIdentity,
|
|
87
87
|
piModelRefToCanonical
|
|
@@ -5721,7 +5721,7 @@ function createFinalWireUsageTracker() {
|
|
|
5721
5721
|
const envelopeFor = (input) => {
|
|
5722
5722
|
if (!input.providerID || !input.modelID || !input.systemPromptHash)
|
|
5723
5723
|
return;
|
|
5724
|
-
const tools = getToolDefinitionMeasurementSignature(input.providerID, input.modelID, input.agentName);
|
|
5724
|
+
const tools = getToolDefinitionMeasurementSignature(input.providerID, input.modelID, input.agentName, input.sessionId);
|
|
5725
5725
|
if (!tools)
|
|
5726
5726
|
return;
|
|
5727
5727
|
return JSON.stringify([
|
|
@@ -5734,7 +5734,8 @@ function createFinalWireUsageTracker() {
|
|
|
5734
5734
|
]);
|
|
5735
5735
|
};
|
|
5736
5736
|
return {
|
|
5737
|
-
estimate(sessionId,
|
|
5737
|
+
estimate(sessionId, request, limit) {
|
|
5738
|
+
const input = { ...request, sessionId: request.sessionId ?? sessionId };
|
|
5738
5739
|
let measuredPrefix;
|
|
5739
5740
|
try {
|
|
5740
5741
|
const previous = served.get(sessionId);
|
|
@@ -5765,7 +5766,8 @@ function createFinalWireUsageTracker() {
|
|
|
5765
5766
|
}
|
|
5766
5767
|
return estimateOutgoingWireForRefusal({ ...input, measuredPrefix }, limit);
|
|
5767
5768
|
},
|
|
5768
|
-
capture(sessionId,
|
|
5769
|
+
capture(sessionId, request) {
|
|
5770
|
+
const input = { ...request, sessionId: request.sessionId ?? sessionId };
|
|
5769
5771
|
const previous = served.get(sessionId);
|
|
5770
5772
|
served.delete(sessionId);
|
|
5771
5773
|
try {
|
|
@@ -5836,7 +5838,7 @@ function estimateOutgoingWireForRefusal(input, limit) {
|
|
|
5836
5838
|
if (bytes) {
|
|
5837
5839
|
const lower = baseline + Math.ceil(bytes.bytes / 4);
|
|
5838
5840
|
const calibration = resolveDecisionCalibration(input.providerID, input.modelID);
|
|
5839
|
-
const tools = input.providerID && input.modelID ? getMeasuredToolDefinitionTokens(input.providerID, input.modelID, input.agentName) : undefined;
|
|
5841
|
+
const tools = input.providerID && input.modelID ? getMeasuredToolDefinitionTokens(input.providerID, input.modelID, input.agentName, input.sessionId) : undefined;
|
|
5840
5842
|
const envelope = measured ? baseline : tools !== undefined && Number.isFinite(input.systemPromptTokens) && input.systemPromptTokens > 0 ? providerMass({ system: input.systemPromptTokens, tools }, calibration, true) : undefined;
|
|
5841
5843
|
const upper = envelope === undefined ? undefined : envelope + Math.ceil(bytes.bytes * Math.max(UNKNOWN_FIT_RATIO, calibration.toolsRatio, calibration.proseRatio));
|
|
5842
5844
|
if (bytes.aborted || lower > limit || upper !== undefined && upper <= limit) {
|
|
@@ -5864,17 +5866,17 @@ function estimateFinalWireInputTokens(input) {
|
|
|
5864
5866
|
total.toolCall += next.toolCall;
|
|
5865
5867
|
return total;
|
|
5866
5868
|
}, { conversation: 0, toolCall: 0 });
|
|
5867
|
-
const measuredToolDefinitions = input.toolDefinitionTokens ?? (input.providerID && input.modelID ? getMeasuredToolDefinitionTokens(input.providerID, input.modelID, input.agentName) : undefined);
|
|
5869
|
+
const measuredToolDefinitions = input.toolDefinitionTokens ?? (input.providerID && input.modelID ? getMeasuredToolDefinitionTokens(input.providerID, input.modelID, input.agentName, input.sessionId) : undefined);
|
|
5868
5870
|
const calibration = resolveDecisionCalibration(input.providerID, input.modelID);
|
|
5869
|
-
const
|
|
5870
|
-
const toolDefinitions = measuredToolDefinitions ?? (
|
|
5871
|
+
const borrowedToolDefinitions = measuredToolDefinitions === undefined ? getSessionToolDefinitionTokens(input.sessionId) : undefined;
|
|
5872
|
+
const toolDefinitions = measuredToolDefinitions ?? (borrowedToolDefinitions === undefined ? undefined : Math.ceil(borrowedToolDefinitions * UNKNOWN_FIT_RATIO / (calibration.seeded ? calibration.toolsRatio : UNKNOWN_FIT_RATIO)));
|
|
5871
5873
|
const rawComponents = {
|
|
5872
5874
|
system: input.systemPromptTokens,
|
|
5873
5875
|
tools: (toolDefinitions ?? 0) + messageTokens.toolCall,
|
|
5874
5876
|
prose: messageTokens.conversation
|
|
5875
5877
|
};
|
|
5876
5878
|
const tokens = providerMass(rawComponents, calibration, true);
|
|
5877
|
-
const pressureToolDefinitions = measuredToolDefinitions ??
|
|
5879
|
+
const pressureToolDefinitions = measuredToolDefinitions ?? borrowedToolDefinitions ?? 0;
|
|
5878
5880
|
const pressureTokens = providerMass({
|
|
5879
5881
|
system: rawComponents.system,
|
|
5880
5882
|
tools: pressureToolDefinitions + messageTokens.toolCall,
|
|
@@ -35,7 +35,10 @@ import {
|
|
|
35
35
|
} from "./server-rqebawx1.js";
|
|
36
36
|
import {
|
|
37
37
|
cleanUserText2
|
|
38
|
-
} from "./server-
|
|
38
|
+
} from "./server-5brp2rfn.js";
|
|
39
|
+
import {
|
|
40
|
+
BoundedSessionMap
|
|
41
|
+
} from "./server-7d6m1p2b.js";
|
|
39
42
|
import {
|
|
40
43
|
recordMessageFtsRowid2,
|
|
41
44
|
recordIndexedMessageTime2,
|
|
@@ -63,51 +66,6 @@ import {
|
|
|
63
66
|
writeStorageFileDurableAtomicSync
|
|
64
67
|
} from "./server-fvnzcnww.js";
|
|
65
68
|
|
|
66
|
-
// src/shared/bounded-session-map.ts
|
|
67
|
-
class BoundedSessionMap {
|
|
68
|
-
maxEntries;
|
|
69
|
-
store = new Map;
|
|
70
|
-
constructor(maxEntries) {
|
|
71
|
-
if (!Number.isFinite(maxEntries) || maxEntries < 1) {
|
|
72
|
-
throw new Error(`BoundedSessionMap: maxEntries must be >= 1, got ${maxEntries}`);
|
|
73
|
-
}
|
|
74
|
-
this.maxEntries = maxEntries;
|
|
75
|
-
}
|
|
76
|
-
get(sessionId) {
|
|
77
|
-
const value = this.store.get(sessionId);
|
|
78
|
-
if (value === undefined)
|
|
79
|
-
return;
|
|
80
|
-
this.store.delete(sessionId);
|
|
81
|
-
this.store.set(sessionId, value);
|
|
82
|
-
return value;
|
|
83
|
-
}
|
|
84
|
-
peek(sessionId) {
|
|
85
|
-
return this.store.get(sessionId);
|
|
86
|
-
}
|
|
87
|
-
has(sessionId) {
|
|
88
|
-
return this.store.has(sessionId);
|
|
89
|
-
}
|
|
90
|
-
set(sessionId, value) {
|
|
91
|
-
if (this.store.has(sessionId)) {
|
|
92
|
-
this.store.delete(sessionId);
|
|
93
|
-
} else if (this.store.size >= this.maxEntries) {
|
|
94
|
-
const oldest = this.store.keys().next().value;
|
|
95
|
-
if (oldest !== undefined)
|
|
96
|
-
this.store.delete(oldest);
|
|
97
|
-
}
|
|
98
|
-
this.store.set(sessionId, value);
|
|
99
|
-
}
|
|
100
|
-
delete(sessionId) {
|
|
101
|
-
return this.store.delete(sessionId);
|
|
102
|
-
}
|
|
103
|
-
clear() {
|
|
104
|
-
this.store.clear();
|
|
105
|
-
}
|
|
106
|
-
get size() {
|
|
107
|
-
return this.store.size;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
69
|
// src/hooks/magic-context/lkg-recovery-fence.ts
|
|
112
70
|
import { createHash as createHash2, randomUUID as randomUUID2 } from "node:crypto";
|
|
113
71
|
import {
|
|
@@ -2259,4 +2217,4 @@ async function sweepOrphanedOpenCodeMessageIndexesCooperatively2(db, openSource,
|
|
|
2259
2217
|
}));
|
|
2260
2218
|
}
|
|
2261
2219
|
|
|
2262
|
-
export {
|
|
2220
|
+
export { LkgRecoveryFenceError, clearLkgFenceProcessState, lkgServedArrayDigest, takeLkgRecoveryLockSweepPage, hasCleanLkgRecoveryFence, lkgFenceRequiresRebuild, lkgRecoveryFenceUnclean, dirtyLkgRecoveryRevision, ownsLkgDirtyFence, invalidateCleanLkgRecoveryFence, prepareLkgCaptureFence, certifyCommittedLkgCapture, markLkgRecoveryDirty, commitLkgWithRecoveryFence, clearLkgRecoveryFence, gaDatabasePath, RAW_MESSAGE_TYPES, V2StoreReaderPool, V2StoreReader, cleanSearchText, keywordRelevance, matchedSearchSnippet, clearCompressionDepth, clearCompressionDepthRange, advanceSessionActivity, backfillSessionActivity, deleteSessionScopedRows, MESSAGE_INDEX_MAX_FTS_BYTES2, messageIndexSkipKey2, toMessageIndexSource2, getMessageIndexSourceIdentity2, isMessageIndexSourceCurrent2, getLastIndexedOrdinal2, getIndexedMessageCorpusSize2, markMessageIndexDirty2, getMessageIndexReconciliationStartOrdinal2, isMessageIndexReconciledThrough2, deleteIndexedMessage2, clearIndexedMessagesInTransaction2, clearIndexedMessages2, getIndexableContent2, indexSingleMessage2, indexMessagesAfterOrdinal2, indexSourcesAfterOrdinal2, sweepOrphanedOpenCodeMessageIndexesCooperatively2 };
|
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
import {
|
|
2
|
+
estimateTokens
|
|
3
|
+
} from "./server-3mz2qh5a.js";
|
|
4
|
+
import {
|
|
5
|
+
stableStringify
|
|
6
|
+
} from "./server-rqebawx1.js";
|
|
7
|
+
|
|
8
|
+
// src/features/magic-context/tool-definition-tokens.ts
|
|
9
|
+
import { createHash } from "node:crypto";
|
|
10
|
+
|
|
11
|
+
// src/shared/bounded-session-map.ts
|
|
12
|
+
class BoundedSessionMap {
|
|
13
|
+
maxEntries;
|
|
14
|
+
store = new Map;
|
|
15
|
+
constructor(maxEntries) {
|
|
16
|
+
if (!Number.isFinite(maxEntries) || maxEntries < 1) {
|
|
17
|
+
throw new Error(`BoundedSessionMap: maxEntries must be >= 1, got ${maxEntries}`);
|
|
18
|
+
}
|
|
19
|
+
this.maxEntries = maxEntries;
|
|
20
|
+
}
|
|
21
|
+
get(sessionId) {
|
|
22
|
+
const value = this.store.get(sessionId);
|
|
23
|
+
if (value === undefined)
|
|
24
|
+
return;
|
|
25
|
+
this.store.delete(sessionId);
|
|
26
|
+
this.store.set(sessionId, value);
|
|
27
|
+
return value;
|
|
28
|
+
}
|
|
29
|
+
peek(sessionId) {
|
|
30
|
+
return this.store.get(sessionId);
|
|
31
|
+
}
|
|
32
|
+
has(sessionId) {
|
|
33
|
+
return this.store.has(sessionId);
|
|
34
|
+
}
|
|
35
|
+
set(sessionId, value) {
|
|
36
|
+
if (this.store.has(sessionId)) {
|
|
37
|
+
this.store.delete(sessionId);
|
|
38
|
+
} else if (this.store.size >= this.maxEntries) {
|
|
39
|
+
const oldest = this.store.keys().next().value;
|
|
40
|
+
if (oldest !== undefined)
|
|
41
|
+
this.store.delete(oldest);
|
|
42
|
+
}
|
|
43
|
+
this.store.set(sessionId, value);
|
|
44
|
+
}
|
|
45
|
+
delete(sessionId) {
|
|
46
|
+
return this.store.delete(sessionId);
|
|
47
|
+
}
|
|
48
|
+
clear() {
|
|
49
|
+
this.store.clear();
|
|
50
|
+
}
|
|
51
|
+
get size() {
|
|
52
|
+
return this.store.size;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// src/features/magic-context/tool-definition-tokens.ts
|
|
57
|
+
var ACTIVITY_WINDOW_MS = 10 * 60000;
|
|
58
|
+
var MAX_ACTIVITY_GAP_MS = 60000;
|
|
59
|
+
var RETIRED_CACHE_LIMIT = 64;
|
|
60
|
+
var WRITE_RETRY_MS = 5000;
|
|
61
|
+
var WINDOWED_SINCE_KEY = "tool_definition_measurements:windowed_since";
|
|
62
|
+
var routes = new Map;
|
|
63
|
+
var sessionFlights = new BoundedSessionMap(1000);
|
|
64
|
+
var persistenceDb = null;
|
|
65
|
+
var windowedSince;
|
|
66
|
+
var boundAt = 0;
|
|
67
|
+
var cachedInsertStmt = null;
|
|
68
|
+
var cachedDeleteStmt = null;
|
|
69
|
+
function agentKey(agentName) {
|
|
70
|
+
return agentName && agentName.length > 0 ? agentName : "default";
|
|
71
|
+
}
|
|
72
|
+
function keyFor(providerID, modelID, agentName) {
|
|
73
|
+
return `${providerID}/${modelID}/${agentKey(agentName)}`;
|
|
74
|
+
}
|
|
75
|
+
function fingerprintFor(description, parameters) {
|
|
76
|
+
return createHash("sha256").update(description).update("\x00").update(stableStringify(parameters)).digest("hex");
|
|
77
|
+
}
|
|
78
|
+
function measure(description, parameters) {
|
|
79
|
+
let paramsText = "";
|
|
80
|
+
try {
|
|
81
|
+
paramsText = parameters === undefined ? "" : JSON.stringify(parameters);
|
|
82
|
+
} catch {
|
|
83
|
+
paramsText = "";
|
|
84
|
+
}
|
|
85
|
+
return estimateTokens(description) + estimateTokens(paramsText);
|
|
86
|
+
}
|
|
87
|
+
function routeFor(providerID, modelID, agentName) {
|
|
88
|
+
const key = keyFor(providerID, modelID, agentName);
|
|
89
|
+
let route = routes.get(key);
|
|
90
|
+
if (!route) {
|
|
91
|
+
route = {
|
|
92
|
+
providerID,
|
|
93
|
+
modelID,
|
|
94
|
+
agentName: agentKey(agentName),
|
|
95
|
+
tools: new Map,
|
|
96
|
+
live: false,
|
|
97
|
+
clock: 0,
|
|
98
|
+
lastWallAt: Date.now(),
|
|
99
|
+
oldestFireAt: Number.POSITIVE_INFINITY,
|
|
100
|
+
retired: new Map,
|
|
101
|
+
legacyRows: new Set
|
|
102
|
+
};
|
|
103
|
+
routes.set(key, route);
|
|
104
|
+
}
|
|
105
|
+
return { key, route };
|
|
106
|
+
}
|
|
107
|
+
function advanceClock(route) {
|
|
108
|
+
const now = Date.now();
|
|
109
|
+
const gap = Math.min(Math.max(now - route.lastWallAt, 0), MAX_ACTIVITY_GAP_MS);
|
|
110
|
+
route.clock += gap;
|
|
111
|
+
route.lastWallAt = Math.max(route.lastWallAt, now);
|
|
112
|
+
return route.clock;
|
|
113
|
+
}
|
|
114
|
+
function counts(route, slot) {
|
|
115
|
+
if (!route.live)
|
|
116
|
+
return true;
|
|
117
|
+
if (slot.legacy)
|
|
118
|
+
return false;
|
|
119
|
+
return slot.lastFireAt >= route.clock - ACTIVITY_WINDOW_MS;
|
|
120
|
+
}
|
|
121
|
+
function currentTools(route) {
|
|
122
|
+
return [...route.tools.entries()].filter(([, slot]) => counts(route, slot));
|
|
123
|
+
}
|
|
124
|
+
function summarize(entries, live) {
|
|
125
|
+
let total = 0;
|
|
126
|
+
for (const [, slot] of entries)
|
|
127
|
+
total += slot.tokens;
|
|
128
|
+
const ids = entries.map(([id]) => id).sort();
|
|
129
|
+
const fingerprints = new Map(entries.map(([id, slot]) => [
|
|
130
|
+
id,
|
|
131
|
+
slot.fingerprint ?? (live ? `stored:${slot.tokens}` : undefined)
|
|
132
|
+
]));
|
|
133
|
+
const complete = ids.every((id) => fingerprints.get(id) !== undefined);
|
|
134
|
+
return {
|
|
135
|
+
total,
|
|
136
|
+
signature: complete ? createHash("sha256").update(JSON.stringify(ids.map((id) => [id, fingerprints.get(id)]))).digest("hex") : undefined,
|
|
137
|
+
hash: ids.length === 0 ? "" : createHash("sha256").update(ids.join("\x00")).digest("hex")
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
function resolve(providerID, modelID, agentName, sessionId) {
|
|
141
|
+
if (!providerID || !modelID)
|
|
142
|
+
return;
|
|
143
|
+
const key = keyFor(providerID, modelID, agentName);
|
|
144
|
+
if (sessionId) {
|
|
145
|
+
const flight = sessionFlights.peek(sessionId);
|
|
146
|
+
if (flight?.key === key && flight.exact)
|
|
147
|
+
return flight.exact;
|
|
148
|
+
}
|
|
149
|
+
const route = routes.get(key);
|
|
150
|
+
if (!route)
|
|
151
|
+
return;
|
|
152
|
+
const entries = currentTools(route);
|
|
153
|
+
if (!route.live && entries.length === 0)
|
|
154
|
+
return;
|
|
155
|
+
return summarize(entries, route.live);
|
|
156
|
+
}
|
|
157
|
+
function readWindowedSince(db) {
|
|
158
|
+
try {
|
|
159
|
+
const row = db.prepare("SELECT value FROM schema_migrations_meta WHERE key = ?").get(WINDOWED_SINCE_KEY);
|
|
160
|
+
if (!row)
|
|
161
|
+
return;
|
|
162
|
+
const value = Number(row.value);
|
|
163
|
+
return Number.isFinite(value) ? value : undefined;
|
|
164
|
+
} catch {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
function markWindowed() {
|
|
169
|
+
if (windowedSince !== undefined || !persistenceDb)
|
|
170
|
+
return;
|
|
171
|
+
try {
|
|
172
|
+
persistenceDb.prepare("INSERT OR IGNORE INTO schema_migrations_meta (key, value) VALUES (?, ?)").run(WINDOWED_SINCE_KEY, String(boundAt));
|
|
173
|
+
windowedSince = readWindowedSince(persistenceDb);
|
|
174
|
+
} catch {}
|
|
175
|
+
}
|
|
176
|
+
function persistInsert(route, toolID, tokens) {
|
|
177
|
+
if (!persistenceDb)
|
|
178
|
+
return false;
|
|
179
|
+
try {
|
|
180
|
+
cachedInsertStmt ??= persistenceDb.prepare(`INSERT OR REPLACE INTO tool_definition_measurements
|
|
181
|
+
(provider_id, model_id, agent_name, tool_id, token_count, recorded_at)
|
|
182
|
+
VALUES (?, ?, ?, ?, ?, ?)`);
|
|
183
|
+
cachedInsertStmt.run(route.providerID, route.modelID, route.agentName, toolID, tokens, Date.now());
|
|
184
|
+
} catch {
|
|
185
|
+
cachedInsertStmt = null;
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
if (route.legacyRows.delete(toolID) && route.legacyRows.size === 0)
|
|
189
|
+
markWindowed();
|
|
190
|
+
return true;
|
|
191
|
+
}
|
|
192
|
+
function persistDelete(route, toolIDs) {
|
|
193
|
+
if (!persistenceDb)
|
|
194
|
+
return false;
|
|
195
|
+
try {
|
|
196
|
+
cachedDeleteStmt ??= persistenceDb.prepare(`DELETE FROM tool_definition_measurements
|
|
197
|
+
WHERE provider_id = ? AND model_id = ? AND agent_name = ?
|
|
198
|
+
AND tool_id IN (SELECT value FROM json_each(?))`);
|
|
199
|
+
cachedDeleteStmt.run(route.providerID, route.modelID, route.agentName, JSON.stringify(toolIDs));
|
|
200
|
+
} catch {
|
|
201
|
+
cachedDeleteStmt = null;
|
|
202
|
+
return false;
|
|
203
|
+
}
|
|
204
|
+
for (const id of toolIDs)
|
|
205
|
+
route.legacyRows.delete(id);
|
|
206
|
+
markWindowed();
|
|
207
|
+
return true;
|
|
208
|
+
}
|
|
209
|
+
function writeSlot(route, toolID, slot) {
|
|
210
|
+
if (slot.persisted || !persistenceDb)
|
|
211
|
+
return;
|
|
212
|
+
const now = Date.now();
|
|
213
|
+
if (slot.writeFailedAt !== undefined && now - slot.writeFailedAt < WRITE_RETRY_MS)
|
|
214
|
+
return;
|
|
215
|
+
if (persistInsert(route, toolID, slot.tokens)) {
|
|
216
|
+
slot.persisted = true;
|
|
217
|
+
slot.writeFailedAt = undefined;
|
|
218
|
+
} else {
|
|
219
|
+
slot.writeFailedAt = now;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
function retire(route, toolID, slot, rowDeleted) {
|
|
223
|
+
if (slot.fingerprint === undefined)
|
|
224
|
+
return;
|
|
225
|
+
route.retired.delete(toolID);
|
|
226
|
+
route.retired.set(toolID, {
|
|
227
|
+
fingerprint: slot.fingerprint,
|
|
228
|
+
tokens: slot.tokens,
|
|
229
|
+
rowKept: slot.persisted && !rowDeleted
|
|
230
|
+
});
|
|
231
|
+
if (route.retired.size > RETIRED_CACHE_LIMIT) {
|
|
232
|
+
const oldest = route.retired.keys().next().value;
|
|
233
|
+
if (oldest !== undefined)
|
|
234
|
+
route.retired.delete(oldest);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
function freshSlot(route, toolID, description, parameters, fingerprint, now) {
|
|
238
|
+
const retired = route.retired.get(toolID);
|
|
239
|
+
if (retired)
|
|
240
|
+
route.retired.delete(toolID);
|
|
241
|
+
const reuse = retired?.fingerprint === fingerprint ? retired : undefined;
|
|
242
|
+
return {
|
|
243
|
+
tokens: reuse ? reuse.tokens : measure(description, parameters),
|
|
244
|
+
fingerprint,
|
|
245
|
+
lastFireAt: now,
|
|
246
|
+
legacy: false,
|
|
247
|
+
persisted: reuse?.rowKept === true
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
function setDatabase(db) {
|
|
251
|
+
const rebound = persistenceDb !== null && persistenceDb !== db;
|
|
252
|
+
persistenceDb = db;
|
|
253
|
+
boundAt = Date.now();
|
|
254
|
+
cachedInsertStmt = null;
|
|
255
|
+
cachedDeleteStmt = null;
|
|
256
|
+
if (rebound) {
|
|
257
|
+
for (const route of routes.values()) {
|
|
258
|
+
for (const slot of route.tools.values()) {
|
|
259
|
+
slot.persisted = false;
|
|
260
|
+
slot.writeFailedAt = undefined;
|
|
261
|
+
}
|
|
262
|
+
for (const retired of route.retired.values())
|
|
263
|
+
retired.rowKept = false;
|
|
264
|
+
route.legacyRows.clear();
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
windowedSince = readWindowedSince(db);
|
|
268
|
+
if (windowedSince !== undefined)
|
|
269
|
+
return;
|
|
270
|
+
try {
|
|
271
|
+
if (!db.prepare("SELECT 1 FROM tool_definition_measurements LIMIT 1").get())
|
|
272
|
+
markWindowed();
|
|
273
|
+
} catch {}
|
|
274
|
+
}
|
|
275
|
+
function loadToolDefinitionMeasurements(db) {
|
|
276
|
+
let rows = [];
|
|
277
|
+
try {
|
|
278
|
+
rows = db.prepare("SELECT provider_id, model_id, agent_name, tool_id, token_count, recorded_at FROM tool_definition_measurements").all();
|
|
279
|
+
} catch {
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
const since = readWindowedSince(db);
|
|
283
|
+
const loadedAt = new Map;
|
|
284
|
+
for (const row of rows) {
|
|
285
|
+
const { route } = routeFor(row.provider_id, row.model_id, row.agent_name);
|
|
286
|
+
if (route.live)
|
|
287
|
+
continue;
|
|
288
|
+
let now = loadedAt.get(route);
|
|
289
|
+
if (now === undefined) {
|
|
290
|
+
now = advanceClock(route);
|
|
291
|
+
loadedAt.set(route, now);
|
|
292
|
+
}
|
|
293
|
+
const legacy = since === undefined || !(Number(row.recorded_at) >= since);
|
|
294
|
+
route.tools.set(row.tool_id, {
|
|
295
|
+
tokens: row.token_count,
|
|
296
|
+
fingerprint: undefined,
|
|
297
|
+
lastFireAt: now,
|
|
298
|
+
legacy,
|
|
299
|
+
persisted: !legacy
|
|
300
|
+
});
|
|
301
|
+
route.oldestFireAt = Math.min(route.oldestFireAt, now);
|
|
302
|
+
if (legacy)
|
|
303
|
+
route.legacyRows.add(row.tool_id);
|
|
304
|
+
else
|
|
305
|
+
route.legacyRows.delete(row.tool_id);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
function recordToolDefinitionSet(providerID, modelID, agentName, tools, sessionId) {
|
|
309
|
+
if (!providerID || !modelID)
|
|
310
|
+
return;
|
|
311
|
+
const { key, route } = routeFor(providerID, modelID, agentName);
|
|
312
|
+
const now = advanceClock(route);
|
|
313
|
+
route.live = true;
|
|
314
|
+
const next = new Map;
|
|
315
|
+
for (const tool of tools) {
|
|
316
|
+
if (!tool.id)
|
|
317
|
+
continue;
|
|
318
|
+
const fingerprint = fingerprintFor(tool.description ?? "", tool.parameters);
|
|
319
|
+
const previous = route.tools.get(tool.id);
|
|
320
|
+
if (previous && previous.fingerprint === fingerprint) {
|
|
321
|
+
previous.lastFireAt = now;
|
|
322
|
+
next.set(tool.id, previous);
|
|
323
|
+
continue;
|
|
324
|
+
}
|
|
325
|
+
next.set(tool.id, freshSlot(route, tool.id, tool.description ?? "", tool.parameters, fingerprint, now));
|
|
326
|
+
}
|
|
327
|
+
const removed = [...route.tools].filter(([id]) => !next.has(id));
|
|
328
|
+
route.tools = next;
|
|
329
|
+
route.oldestFireAt = now;
|
|
330
|
+
if (removed.length > 0) {
|
|
331
|
+
const deleted = persistDelete(route, removed.map(([id]) => id));
|
|
332
|
+
for (const [id, slot] of removed)
|
|
333
|
+
retire(route, id, slot, deleted);
|
|
334
|
+
}
|
|
335
|
+
for (const [toolID, slot] of next)
|
|
336
|
+
writeSlot(route, toolID, slot);
|
|
337
|
+
if (sessionId)
|
|
338
|
+
sessionFlights.set(sessionId, { key, exact: summarize([...next.entries()], true) });
|
|
339
|
+
}
|
|
340
|
+
function getCurrentToolSetHash(providerID, modelID, agentName, sessionId) {
|
|
341
|
+
return resolve(providerID, modelID, agentName, sessionId)?.hash ?? "";
|
|
342
|
+
}
|
|
343
|
+
function getToolDefinitionMeasurementSignature(providerID, modelID, agentName, sessionId) {
|
|
344
|
+
return resolve(providerID, modelID, agentName, sessionId)?.signature;
|
|
345
|
+
}
|
|
346
|
+
function getMeasuredToolDefinitionTokens(providerID, modelID, agentName, sessionId) {
|
|
347
|
+
return resolve(providerID, modelID, agentName, sessionId)?.total;
|
|
348
|
+
}
|
|
349
|
+
function getSessionToolDefinitionTokens(sessionId) {
|
|
350
|
+
if (!sessionId)
|
|
351
|
+
return;
|
|
352
|
+
const flight = sessionFlights.peek(sessionId);
|
|
353
|
+
if (!flight)
|
|
354
|
+
return;
|
|
355
|
+
if (flight.exact)
|
|
356
|
+
return flight.exact.total;
|
|
357
|
+
const route = routes.get(flight.key);
|
|
358
|
+
return route ? summarize(currentTools(route), route.live).total : undefined;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
export { BoundedSessionMap, setDatabase, loadToolDefinitionMeasurements, recordToolDefinitionSet, getCurrentToolSetHash, getToolDefinitionMeasurementSignature, getMeasuredToolDefinitionTokens, getSessionToolDefinitionTokens };
|
|
@@ -75,7 +75,6 @@ import {
|
|
|
75
75
|
isPidAlive
|
|
76
76
|
} from "./server-k3t06fxr.js";
|
|
77
77
|
import {
|
|
78
|
-
BoundedSessionMap,
|
|
79
78
|
clearLkgFenceProcessState,
|
|
80
79
|
lkgFenceRequiresRebuild,
|
|
81
80
|
lkgRecoveryFenceUnclean,
|
|
@@ -90,7 +89,7 @@ import {
|
|
|
90
89
|
toMessageIndexSource2,
|
|
91
90
|
clearIndexedMessagesInTransaction2,
|
|
92
91
|
indexSourcesAfterOrdinal2
|
|
93
|
-
} from "./server-
|
|
92
|
+
} from "./server-6a8pxpxg.js";
|
|
94
93
|
import {
|
|
95
94
|
retreatPastHostUnservedRows,
|
|
96
95
|
snapTerminalCompartmentToServedRow,
|
|
@@ -188,7 +187,7 @@ import {
|
|
|
188
187
|
getRawSessionTagKeysThrough2,
|
|
189
188
|
getLegacyProtectedTailStartOrdinal2,
|
|
190
189
|
readSessionChunk2
|
|
191
|
-
} from "./server-
|
|
190
|
+
} from "./server-5brp2rfn.js";
|
|
192
191
|
import {
|
|
193
192
|
getTableColumnNames
|
|
194
193
|
} from "./server-rqebawx1.js";
|
|
@@ -198,7 +197,7 @@ import {
|
|
|
198
197
|
LATEST_SUPPORTED_VERSION,
|
|
199
198
|
getPersistedSchemaVersion,
|
|
200
199
|
openDatabase
|
|
201
|
-
} from "./server-
|
|
200
|
+
} from "./server-e75vag9d.js";
|
|
202
201
|
import {
|
|
203
202
|
providerMass,
|
|
204
203
|
calibrationForModelKey,
|
|
@@ -229,8 +228,9 @@ import {
|
|
|
229
228
|
getMessageTimesFromOpenCodeDb
|
|
230
229
|
} from "./server-3vhxy2k5.js";
|
|
231
230
|
import {
|
|
231
|
+
BoundedSessionMap,
|
|
232
232
|
getMeasuredToolDefinitionTokens
|
|
233
|
-
} from "./server-
|
|
233
|
+
} from "./server-7d6m1p2b.js";
|
|
234
234
|
import {
|
|
235
235
|
V2_MEMORY_CATEGORIES,
|
|
236
236
|
CATEGORY_PRIORITY,
|
|
@@ -1489,13 +1489,13 @@ function identity(slot) {
|
|
|
1489
1489
|
slot.inputContentDigests
|
|
1490
1490
|
])).update(slot.jsonPrefix).digest("hex");
|
|
1491
1491
|
}
|
|
1492
|
-
function envelope(modelKey, systemPromptTokens, agentName) {
|
|
1492
|
+
function envelope(modelKey, systemPromptTokens, agentName, sessionId) {
|
|
1493
1493
|
const slash = modelKey.indexOf("/");
|
|
1494
1494
|
return JSON.stringify([
|
|
1495
1495
|
modelKey,
|
|
1496
1496
|
systemPromptTokens,
|
|
1497
1497
|
agentName,
|
|
1498
|
-
getMeasuredToolDefinitionTokens(modelKey.slice(0, slash), modelKey.slice(slash + 1), agentName)
|
|
1498
|
+
getMeasuredToolDefinitionTokens(modelKey.slice(0, slash), modelKey.slice(slash + 1), agentName, sessionId)
|
|
1499
1499
|
]);
|
|
1500
1500
|
}
|
|
1501
1501
|
function beginV2LkgRequest(sessionId, modelKey, previousResponseId, responseAfterSequence) {
|
|
@@ -1527,7 +1527,7 @@ function noteCapturedLkgRequest(args) {
|
|
|
1527
1527
|
request,
|
|
1528
1528
|
slot: identity(args.slot),
|
|
1529
1529
|
prefixCount: JSON.parse(args.slot.jsonPrefix).length,
|
|
1530
|
-
envelope: envelope(request.modelKey, args.systemPromptTokens, args.agentName)
|
|
1530
|
+
envelope: envelope(request.modelKey, args.systemPromptTokens, args.agentName, args.sessionId)
|
|
1531
1531
|
});
|
|
1532
1532
|
}
|
|
1533
1533
|
function noteLkgProviderResponse(args) {
|
|
@@ -1558,7 +1558,7 @@ function noteLkgProviderResponse(args) {
|
|
|
1558
1558
|
}
|
|
1559
1559
|
function measuredLkgPrefix(args) {
|
|
1560
1560
|
const capture = captures.get(args.sessionId);
|
|
1561
|
-
if (!capture?.usage || !args.slot || capture.slot !== identity(args.slot) || capture.request.modelKey !== args.modelKey || capture.envelope !== envelope(args.modelKey, args.systemPromptTokens, args.agentName))
|
|
1561
|
+
if (!capture?.usage || !args.slot || capture.slot !== identity(args.slot) || capture.request.modelKey !== args.modelKey || capture.envelope !== envelope(args.modelKey, args.systemPromptTokens, args.agentName, args.sessionId))
|
|
1562
1562
|
return;
|
|
1563
1563
|
const appendedMessages = args.messages.slice(capture.prefixCount);
|
|
1564
1564
|
if (appendedMessages[0]?.info.id !== capture.usage.responseId || appendedMessages[0]?.info.role !== "assistant" || JSON.stringify(args.messages.slice(0, capture.prefixCount)) !== args.slot.jsonPrefix)
|
|
@@ -65,7 +65,7 @@ import {
|
|
|
65
65
|
import {
|
|
66
66
|
setDatabase,
|
|
67
67
|
loadToolDefinitionMeasurements
|
|
68
|
-
} from "./server-
|
|
68
|
+
} from "./server-7d6m1p2b.js";
|
|
69
69
|
import {
|
|
70
70
|
getErrorMessage
|
|
71
71
|
} from "./server-y8v6yerm.js";
|
|
@@ -1221,7 +1221,7 @@ function finishDatabaseOpen(db, dbPath, explicitDbPath, latestSupportedVersion)
|
|
|
1221
1221
|
state.running = startMessageFtsRowidMapBackfill2(db, signal).then(async () => {
|
|
1222
1222
|
if (signal.aborted || !readsOpenCodeStore)
|
|
1223
1223
|
return;
|
|
1224
|
-
const { readRawSessionMessagePage, readRawSessionMessages } = await importPluginModule(() => import("./read-session-chunk-
|
|
1224
|
+
const { readRawSessionMessagePage, readRawSessionMessages } = await importPluginModule(() => import("./read-session-chunk-knvkq1hf.js"));
|
|
1225
1225
|
const { startMessageTimeBackfill } = await importPluginModule(() => import("./message-time-backfill-zvthw6xb.js"));
|
|
1226
1226
|
if (signal.aborted)
|
|
1227
1227
|
return;
|