@cortexkit/opencode-magic-context 0.49.33 → 0.49.34
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 +1 -1
- package/dist/{context-jyzkstnr.js → context-p3xrarzt.js} +10 -10
- package/dist/features/magic-context/compaction-marker.d.ts +22 -6
- package/dist/features/magic-context/compaction-marker.d.ts.map +1 -1
- package/dist/features/magic-context/storage-meta-persisted.d.ts +7 -0
- package/dist/features/magic-context/storage-meta-persisted.d.ts.map +1 -1
- package/dist/hooks/magic-context/compaction-marker-manager.d.ts +5 -5
- package/dist/hooks/magic-context/compaction-marker-manager.d.ts.map +1 -1
- package/dist/hooks/magic-context/compaction-marker-publication.d.ts +9 -0
- package/dist/hooks/magic-context/compaction-marker-publication.d.ts.map +1 -0
- package/dist/hooks/magic-context/inject-compartments.d.ts.map +1 -1
- package/dist/{index-94ef0qe2.js → index-04z754rd.js} +1 -1
- package/dist/{index-qr2mb02j.js → index-0677w9sd.js} +2 -2
- package/dist/{index-43vqx0za.js → index-07vskjn8.js} +3 -3
- package/dist/{index-0fe4q77z.js → index-7g9dr8gy.js} +34 -2
- package/dist/{index-3fkk2gwj.js → index-97spytxq.js} +11 -11
- package/dist/{index-4s4zq97n.js → index-9gpw8hrm.js} +1 -1
- package/dist/{index-2xh37qn5.js → index-atekmqef.js} +241 -53
- package/dist/{index-hbt0sbv2.js → index-fm36bjv4.js} +1 -1
- package/dist/{index-dq5xzeb9.js → index-ftr84kkv.js} +2 -2
- package/dist/{index-b7w46fp2.js → index-hbx7m8he.js} +4 -4
- package/dist/{index-3jmx3fae.js → index-m1x9ghk5.js} +1 -1
- package/dist/index.js +14 -14
- package/dist/{message-index-bounded-zq7a5nm6.js → message-index-bounded-qh91fdr3.js} +2 -2
- package/dist/migration-worker.js +1 -1
- package/dist/raw-ordinal-warmup-worker.js +2 -2
- package/dist/{read-session-chunk-1ndphmb8.js → read-session-chunk-kaghtr75.js} +1 -1
- package/dist/{recomp-orchestrator-skqzzvby.js → recomp-orchestrator-0y983w7h.js} +5 -5
- package/dist/{search-archive-sr07c6aj.js → search-archive-1kw2rp19.js} +2 -2
- package/dist/{search-040pac3a.js → search-p684j8ww.js} +4 -4
- package/dist/{transform-postprocess-phase-zx1peqma.js → transform-postprocess-phase-ph38mhrs.js} +6 -6
- package/dist/v2/auto-search-worker.js +1 -1
- package/dist/v2/chunk-embedding-write-worker.js +1 -1
- package/dist/v2/{context-3c4z2wj5.js → context-s60hcatx.js} +8 -8
- package/dist/v2/{message-index-bounded-jvgz7vk9.js → message-index-bounded-4htdr1j6.js} +2 -2
- package/dist/v2/migration-worker.js +1 -1
- package/dist/v2/raw-ordinal-warmup-worker.js +2 -2
- package/dist/v2/{read-session-chunk-djzgm4jg.js → read-session-chunk-1fa9qzwb.js} +1 -1
- package/dist/v2/{recomp-orchestrator-qfqdvbta.js → recomp-orchestrator-7q9nvxte.js} +4 -4
- package/dist/v2/{search-archive-n3dhns4r.js → search-archive-xfp51s0g.js} +2 -2
- package/dist/v2/{search-q3wkpe8t.js → search-wf8rqxhm.js} +3 -3
- package/dist/v2/{server-n14edf07.js → server-ajxhrgjv.js} +1 -1
- package/dist/v2/{server-rr4cbkr0.js → server-grakwy1v.js} +1 -1
- package/dist/v2/{server-d01pe8q5.js → server-jw5qe9y8.js} +1 -1
- package/dist/v2/{server-hwag0khr.js → server-m1pfg2d4.js} +1 -1
- package/dist/v2/{server-rb8m4vry.js → server-qvy4qavp.js} +34 -2
- package/dist/v2/{server-aqbm9c8d.js → server-z2wft9zv.js} +242 -55
- package/dist/v2/server.js +2 -2
- package/dist/v2/{wrapup-orchestrator-ga5vfmwr.js → wrapup-orchestrator-wxf7n5k6.js} +4 -4
- package/dist/{wrapup-orchestrator-dy8r3gp8.js → wrapup-orchestrator-ys2nrkkb.js} +6 -6
- package/package.json +1 -1
- package/src/shared/sqlite-transaction-mode-guard.test.ts +2 -0
|
@@ -88,7 +88,7 @@ import {
|
|
|
88
88
|
toMessageIndexSource,
|
|
89
89
|
clearIndexedMessagesInTransaction,
|
|
90
90
|
indexSourcesAfterOrdinal
|
|
91
|
-
} from "./server-
|
|
91
|
+
} from "./server-m1pfg2d4.js";
|
|
92
92
|
import {
|
|
93
93
|
retreatPastHostUnservedRows,
|
|
94
94
|
snapTerminalCompartmentToServedRow,
|
|
@@ -154,7 +154,9 @@ import {
|
|
|
154
154
|
getOverflowState,
|
|
155
155
|
clearEmergencyRecovery,
|
|
156
156
|
getPersistedCompactionMarkerState,
|
|
157
|
-
|
|
157
|
+
getCompactionMarkerPublicationState,
|
|
158
|
+
compareAndSetCompactionMarkerPublicationState,
|
|
159
|
+
getDeferredClearedCompactionMarkerState,
|
|
158
160
|
parseStrippedPlaceholderState,
|
|
159
161
|
serializeStrippedPlaceholderState,
|
|
160
162
|
THINKING_BINDING_RECOVERY_FROZEN_PREFIX,
|
|
@@ -187,7 +189,7 @@ import {
|
|
|
187
189
|
getRawSessionTagKeysThrough2,
|
|
188
190
|
getLegacyProtectedTailStartOrdinal2,
|
|
189
191
|
readSessionChunk2
|
|
190
|
-
} from "./server-
|
|
192
|
+
} from "./server-qvy4qavp.js";
|
|
191
193
|
import {
|
|
192
194
|
getTableColumnNames,
|
|
193
195
|
tagTombstoneOwnerRange,
|
|
@@ -202,7 +204,7 @@ import {
|
|
|
202
204
|
LATEST_SUPPORTED_VERSION,
|
|
203
205
|
getPersistedSchemaVersion,
|
|
204
206
|
openDatabase
|
|
205
|
-
} from "./server-
|
|
207
|
+
} from "./server-jw5qe9y8.js";
|
|
206
208
|
import {
|
|
207
209
|
providerMass,
|
|
208
210
|
calibrationForModelKey,
|
|
@@ -6649,6 +6651,80 @@ function getOpenCodeMessageById(sessionId, messageId) {
|
|
|
6649
6651
|
const row = db.prepare(`SELECT id FROM message WHERE session_id = ? AND id = ? LIMIT 1`).get(sessionId, messageId);
|
|
6650
6652
|
return row ?? null;
|
|
6651
6653
|
}
|
|
6654
|
+
function planCompactionMarker(args, boundary) {
|
|
6655
|
+
const identity = `${args.sessionId}\x00${args.endMessageId}`;
|
|
6656
|
+
return {
|
|
6657
|
+
boundaryMessageId: boundary.id,
|
|
6658
|
+
summaryMessageId: generateMessageId(boundary.timeCreated + 1, 1n, `${identity}\x00summary-message`),
|
|
6659
|
+
compactionPartId: generatePartId(boundary.timeCreated, 1n, `${identity}\x00compaction-part`),
|
|
6660
|
+
summaryPartId: generatePartId(boundary.timeCreated + 1, 2n, `${identity}\x00summary-part`)
|
|
6661
|
+
};
|
|
6662
|
+
}
|
|
6663
|
+
function isCompactionMarkerIntentAbsent(marker, targetEndMessageId) {
|
|
6664
|
+
if (!targetEndMessageId)
|
|
6665
|
+
return false;
|
|
6666
|
+
const db = getWritableOpenCodeDb();
|
|
6667
|
+
return !db.prepare(`SELECT 1 FROM message WHERE id IN (?, ?)
|
|
6668
|
+
UNION ALL SELECT 1 FROM part WHERE id IN (?, ?) LIMIT 1`).get(targetEndMessageId, marker.summaryMessageId, marker.compactionPartId, marker.summaryPartId);
|
|
6669
|
+
}
|
|
6670
|
+
function withCompactionMarkerPublicationLock(operation) {
|
|
6671
|
+
return runMarkerWriteTransaction(getWritableOpenCodeDb(), "compaction-marker-replace", operation);
|
|
6672
|
+
}
|
|
6673
|
+
function inspectCompactionMarkerPublication(sessionId, recognized, summaryText) {
|
|
6674
|
+
const db = getWritableOpenCodeDb();
|
|
6675
|
+
const basis = readMarkerCandidateBasis2(db, sessionId);
|
|
6676
|
+
if (!basis)
|
|
6677
|
+
return { kind: "deferred" };
|
|
6678
|
+
const pairs = [];
|
|
6679
|
+
for (const id of basis.ids) {
|
|
6680
|
+
const rows = db.prepare(`SELECT m.id AS summaryId, p.message_id AS boundaryId, p.id AS partId
|
|
6681
|
+
FROM message m JOIN part p ON p.message_id = json_extract(m.data, '$.parentID')
|
|
6682
|
+
AND +p.session_id = m.session_id
|
|
6683
|
+
WHERE m.id = ? AND m.session_id = ? AND json_extract(m.data, '$.summary') = 1
|
|
6684
|
+
AND COALESCE(json_extract(m.data, '$.finish'), '') <> ''
|
|
6685
|
+
AND json_extract(p.data, '$.type') = 'compaction'`).all(id, sessionId);
|
|
6686
|
+
pairs.push(...rows);
|
|
6687
|
+
}
|
|
6688
|
+
if (pairs.length > 1)
|
|
6689
|
+
return { kind: "ambiguous" };
|
|
6690
|
+
if (pairs.length === 0) {
|
|
6691
|
+
for (const marker of recognized) {
|
|
6692
|
+
if (db.prepare(`SELECT 1 FROM message WHERE id = ?
|
|
6693
|
+
UNION ALL SELECT 1 FROM part WHERE id IN (?, ?) LIMIT 1`).get(marker.summaryMessageId, marker.compactionPartId, marker.summaryPartId))
|
|
6694
|
+
return { kind: "ambiguous" };
|
|
6695
|
+
}
|
|
6696
|
+
return { kind: "verified", marker: null };
|
|
6697
|
+
}
|
|
6698
|
+
const pair = pairs[0];
|
|
6699
|
+
const marker = recognized.find((item) => item.summaryMessageId === pair.summaryId && item.boundaryMessageId === pair.boundaryId && item.compactionPartId === pair.partId);
|
|
6700
|
+
if (!marker)
|
|
6701
|
+
return { kind: "ambiguous" };
|
|
6702
|
+
for (const other of recognized) {
|
|
6703
|
+
if (other.summaryMessageId === marker.summaryMessageId && other.compactionPartId === marker.compactionPartId && other.summaryPartId === marker.summaryPartId)
|
|
6704
|
+
continue;
|
|
6705
|
+
if (db.prepare(`SELECT 1 FROM message WHERE id = ?
|
|
6706
|
+
UNION ALL SELECT 1 FROM part WHERE id IN (?, ?) LIMIT 1`).get(other.summaryMessageId, other.compactionPartId, other.summaryPartId))
|
|
6707
|
+
return { kind: "ambiguous" };
|
|
6708
|
+
}
|
|
6709
|
+
const row = db.prepare(`SELECT c.data AS compactionData FROM message m
|
|
6710
|
+
JOIN message b ON b.id = ? AND b.session_id = m.session_id
|
|
6711
|
+
JOIN part c ON c.id = ? AND c.message_id = b.id AND c.session_id = m.session_id
|
|
6712
|
+
JOIN part t ON t.id = ? AND t.message_id = m.id AND t.session_id = m.session_id
|
|
6713
|
+
WHERE m.id = ? AND m.session_id = ?
|
|
6714
|
+
AND json_extract(b.data, '$.role') = 'user'
|
|
6715
|
+
AND json_extract(m.data, '$.role') = 'assistant'
|
|
6716
|
+
AND json_extract(m.data, '$.parentID') = b.id
|
|
6717
|
+
AND json_extract(m.data, '$.summary') = 1
|
|
6718
|
+
AND json_extract(m.data, '$.finish') = 'stop'
|
|
6719
|
+
AND json_extract(m.data, '$.error') IS NULL
|
|
6720
|
+
AND json_extract(m.data, '$.providerID') = 'magic-context'
|
|
6721
|
+
AND json_extract(m.data, '$.modelID') = 'magic-context'
|
|
6722
|
+
AND json_extract(t.data, '$.type') = 'text' AND json_extract(t.data, '$.text') = ?
|
|
6723
|
+
AND (SELECT COUNT(*) FROM part p WHERE p.message_id = m.id AND +p.session_id = m.session_id) = 1`).get(marker.boundaryMessageId, marker.compactionPartId, marker.summaryPartId, marker.summaryMessageId, sessionId, summaryText);
|
|
6724
|
+
if (!row || !isMcCanonicalCompactionPartData(JSON.parse(row.compactionData)) || findBoundaryUserMessage(sessionId, marker.boundaryMessageId)?.id !== marker.boundaryMessageId)
|
|
6725
|
+
return { kind: "ambiguous" };
|
|
6726
|
+
return { kind: "verified", marker };
|
|
6727
|
+
}
|
|
6652
6728
|
var LEGACY_MARKER_LINEAGE_SQL = `SELECT m.id
|
|
6653
6729
|
FROM message m
|
|
6654
6730
|
WHERE +m.session_id = ? AND m.id IN (SELECT value FROM json_each(?)) AND m.id <> ?
|
|
@@ -6698,10 +6774,11 @@ function injectCompactionMarkerFromBasis(args, validatedBasis) {
|
|
|
6698
6774
|
return null;
|
|
6699
6775
|
}
|
|
6700
6776
|
const boundaryTime = boundary.timeCreated;
|
|
6701
|
-
const
|
|
6702
|
-
|
|
6703
|
-
|
|
6704
|
-
|
|
6777
|
+
const {
|
|
6778
|
+
summaryMessageId: summaryMsgId,
|
|
6779
|
+
compactionPartId,
|
|
6780
|
+
summaryPartId
|
|
6781
|
+
} = planCompactionMarker(args, boundary);
|
|
6705
6782
|
const summaryMsgData = JSON.stringify({
|
|
6706
6783
|
role: "assistant",
|
|
6707
6784
|
parentID: boundary.id,
|
|
@@ -6861,10 +6938,12 @@ function listSessionCompactionMarkers(sessionId) {
|
|
|
6861
6938
|
markers.sort((left, right) => left.timeCreated - right.timeCreated || left.compactionPartId.localeCompare(right.compactionPartId));
|
|
6862
6939
|
return markers.map(({ timeCreated: _, ...marker }) => marker);
|
|
6863
6940
|
}
|
|
6864
|
-
function removeForeignCompactionMarker(sessionId, marker, protectedSummaryMessageId) {
|
|
6941
|
+
function removeForeignCompactionMarker(sessionId, marker, protectedSummaryMessageId, shouldRemove) {
|
|
6865
6942
|
try {
|
|
6866
6943
|
const db = getWritableOpenCodeDb("removal");
|
|
6867
6944
|
runMarkerRemovalTransaction(db, () => {
|
|
6945
|
+
if (shouldRemove && !shouldRemove())
|
|
6946
|
+
return;
|
|
6868
6947
|
const deletePartsOfMessage = db.prepare("DELETE FROM part WHERE +session_id = ? AND message_id = ?");
|
|
6869
6948
|
const deleteMessage = db.prepare("DELETE FROM message WHERE session_id = ? AND id = ?");
|
|
6870
6949
|
for (const summaryMessageId of marker.summaryMessageIds) {
|
|
@@ -9350,24 +9429,118 @@ function createPromptSurfaceGuidanceEpochCache(runtime) {
|
|
|
9350
9429
|
guidanceEpochCacheByRuntime.set(runtime, cache);
|
|
9351
9430
|
return cache;
|
|
9352
9431
|
}
|
|
9353
|
-
// src/hooks/magic-context/compaction-marker-
|
|
9354
|
-
|
|
9355
|
-
|
|
9356
|
-
|
|
9357
|
-
|
|
9358
|
-
|
|
9359
|
-
|
|
9360
|
-
|
|
9361
|
-
let
|
|
9362
|
-
|
|
9363
|
-
|
|
9364
|
-
|
|
9365
|
-
|
|
9366
|
-
|
|
9432
|
+
// src/hooks/magic-context/compaction-marker-publication.ts
|
|
9433
|
+
function samePair(left, right) {
|
|
9434
|
+
return left.boundaryMessageId === right.boundaryMessageId && left.summaryMessageId === right.summaryMessageId && left.compactionPartId === right.compactionPartId && left.summaryPartId === right.summaryPartId;
|
|
9435
|
+
}
|
|
9436
|
+
function publishRecoverableCompactionMarker(db, args, targetIsCurrent) {
|
|
9437
|
+
const { sessionId } = args;
|
|
9438
|
+
let raw = "";
|
|
9439
|
+
let previous = null;
|
|
9440
|
+
let oldSummary = null;
|
|
9441
|
+
let intended;
|
|
9442
|
+
let reason = "publication-failed";
|
|
9443
|
+
let recovered;
|
|
9444
|
+
let cut = "uncertain";
|
|
9445
|
+
let writingState = false;
|
|
9446
|
+
const persist = (state) => {
|
|
9447
|
+
writingState = true;
|
|
9448
|
+
db.transaction(() => {
|
|
9449
|
+
raw = compareAndSetCompactionMarkerPublicationState(db, sessionId, raw, state, null);
|
|
9450
|
+
if (previous && previous.summaryMessageId !== state.summaryMessageId) {
|
|
9451
|
+
const tag = getTagNumberByMessageId2(db, sessionId, `${previous.summaryMessageId}:p0`);
|
|
9452
|
+
if (tag !== null)
|
|
9453
|
+
updateTagStatus2(db, sessionId, tag, "dropped");
|
|
9454
|
+
}
|
|
9455
|
+
}).immediate();
|
|
9456
|
+
writingState = false;
|
|
9457
|
+
previous = state;
|
|
9458
|
+
};
|
|
9459
|
+
try {
|
|
9460
|
+
cut = getCompactionMarkerPublicationState(db, sessionId).pending ? "uncertain" : "definitely-no-cut";
|
|
9461
|
+
const result = withCompactionMarkerPublicationLock(() => {
|
|
9462
|
+
const snapshot = getCompactionMarkerPublicationState(db, sessionId);
|
|
9463
|
+
if (snapshot.pending)
|
|
9464
|
+
cut = "uncertain";
|
|
9465
|
+
raw = snapshot.raw;
|
|
9466
|
+
previous = getPersistedCompactionMarkerState(db, sessionId) ?? getDeferredClearedCompactionMarkerState(db, sessionId);
|
|
9467
|
+
oldSummary = previous?.summaryMessageId ?? null;
|
|
9468
|
+
if (snapshot.pending && (snapshot.pending.boundaryOrdinal > args.endOrdinal || snapshot.pending.boundaryOrdinal === args.endOrdinal && snapshot.pending.targetEndMessageId !== args.endMessageId) && !isCompactionMarkerIntentAbsent(snapshot.pending, snapshot.pending.targetEndMessageId))
|
|
9469
|
+
throw new Error("a newer marker publication intent is pending");
|
|
9470
|
+
const boundary = findBoundaryUserMessage(sessionId, args.endMessageId);
|
|
9471
|
+
if (!targetIsCurrent() || !boundary || boundary.id !== args.resolvedBoundary?.id || boundary.timeCreated !== args.resolvedBoundary.timeCreated)
|
|
9472
|
+
throw new Error("marker publication frontier changed before publication");
|
|
9473
|
+
intended = {
|
|
9474
|
+
...planCompactionMarker(args, boundary),
|
|
9475
|
+
boundaryOrdinal: args.endOrdinal,
|
|
9476
|
+
targetEndMessageId: args.endMessageId
|
|
9477
|
+
};
|
|
9478
|
+
let pending = snapshot.pending;
|
|
9479
|
+
if (pending) {
|
|
9480
|
+
const pendingTarget = pending.targetEndMessageId;
|
|
9481
|
+
const pendingBoundary = pendingTarget ? findBoundaryUserMessage(sessionId, pendingTarget) : null;
|
|
9482
|
+
const compartments = pendingTarget ? getCompartmentsByEndMessageId(db, sessionId, pendingTarget) : [];
|
|
9483
|
+
const validTarget = pending.targetEndMessageId === args.endMessageId && pending.boundaryOrdinal === args.endOrdinal || compartments.length === 1 && compartments[0].endMessage === pending.boundaryOrdinal;
|
|
9484
|
+
if (!validTarget || !pendingBoundary || !pendingTarget || !samePair(pending, planCompactionMarker({ ...args, endMessageId: pendingTarget }, pendingBoundary)))
|
|
9485
|
+
pending = null;
|
|
9486
|
+
}
|
|
9487
|
+
const inspection = inspectCompactionMarkerPublication(sessionId, [intended, ...previous ? [previous] : [], ...pending ? [pending] : []], args.summaryText);
|
|
9488
|
+
if (inspection.kind === "deferred") {
|
|
9489
|
+
reason = "candidate-preparation";
|
|
9490
|
+
return {
|
|
9491
|
+
kind: cut === "uncertain" ? "uncertain" : "deferred",
|
|
9492
|
+
error: new Error("marker candidate preparation pending")
|
|
9493
|
+
};
|
|
9494
|
+
}
|
|
9495
|
+
if (inspection.kind === "ambiguous") {
|
|
9496
|
+
reason = "ambiguous-left-alone";
|
|
9497
|
+
return {
|
|
9498
|
+
kind: cut,
|
|
9499
|
+
error: new Error("unverified or ambiguous host marker pairs; left alone")
|
|
9500
|
+
};
|
|
9501
|
+
}
|
|
9502
|
+
cut = "definitely-no-cut";
|
|
9503
|
+
const host = inspection.marker;
|
|
9504
|
+
if (host && (pending && samePair(host, pending) || samePair(host, intended))) {
|
|
9505
|
+
const adopted = pending && samePair(host, pending) ? pending : intended;
|
|
9506
|
+
recovered = adopted === pending ? "pending-intent-recovered" : "adopted-existing-pair";
|
|
9507
|
+
cut = "uncertain";
|
|
9508
|
+
if (samePair(host, intended))
|
|
9509
|
+
return { kind: "committed", marker: host };
|
|
9510
|
+
persist(adopted);
|
|
9511
|
+
}
|
|
9512
|
+
writingState = true;
|
|
9513
|
+
raw = compareAndSetCompactionMarkerPublicationState(db, sessionId, raw, previous, intended);
|
|
9514
|
+
writingState = false;
|
|
9515
|
+
cut = "uncertain";
|
|
9516
|
+
const replacement = replaceCompactionMarker(host, {
|
|
9517
|
+
...args,
|
|
9518
|
+
resolvedBoundary: boundary
|
|
9519
|
+
});
|
|
9520
|
+
if (recovered && replacement.kind !== "committed")
|
|
9521
|
+
return { kind: "uncertain", error: replacement.error };
|
|
9522
|
+
if (replacement.kind !== "committed")
|
|
9523
|
+
cut = replacement.kind === "uncertain" ? "uncertain" : "definitely-no-cut";
|
|
9524
|
+
return replacement;
|
|
9525
|
+
});
|
|
9526
|
+
if (result.kind === "committed") {
|
|
9527
|
+
if (!intended)
|
|
9528
|
+
throw new Error("committed marker has no publication intent");
|
|
9529
|
+
persist(intended);
|
|
9530
|
+
reason = recovered ?? "published";
|
|
9367
9531
|
}
|
|
9368
|
-
|
|
9369
|
-
|
|
9532
|
+
return result;
|
|
9533
|
+
} catch (error) {
|
|
9534
|
+
if (writingState)
|
|
9535
|
+
reason = "state-write-failed";
|
|
9536
|
+
return { kind: cut, error: error instanceof Error ? error : new Error(String(error)) };
|
|
9537
|
+
} finally {
|
|
9538
|
+
sessionLog2(sessionId, `compaction-marker publication reason=${reason} oldSummary=${oldSummary ?? "none"} newSummary=${intended?.summaryMessageId ?? "none"} target=${args.endMessageId} at=${Date.now()}`);
|
|
9539
|
+
}
|
|
9370
9540
|
}
|
|
9541
|
+
|
|
9542
|
+
// src/hooks/magic-context/compaction-marker-manager.ts
|
|
9543
|
+
var MARKER_SUMMARY_TEXT = "[Compacted by magic-context — session history is managed by the plugin]";
|
|
9371
9544
|
function markerUpdateDefinitelyDidNotCut(outcome) {
|
|
9372
9545
|
return outcome.kind === "already-current" || outcome.kind === "deferred" || outcome.kind === "stale-skip" || outcome.kind === "retryable-failure" && outcome.cut === "definitely-no-cut";
|
|
9373
9546
|
}
|
|
@@ -9436,7 +9609,7 @@ function boundaryWouldDiscardUncoveredMessage(db, sessionId, ordinal, boundaryMe
|
|
|
9436
9609
|
return false;
|
|
9437
9610
|
}
|
|
9438
9611
|
function applyDeferredCompactionMarker(db, sessionId, pending, directory, trustedBoundary) {
|
|
9439
|
-
let cut = "
|
|
9612
|
+
let cut = "uncertain";
|
|
9440
9613
|
let stepStartedAt = performance.now();
|
|
9441
9614
|
const stepMs = [];
|
|
9442
9615
|
const endStep = (name) => {
|
|
@@ -9445,41 +9618,61 @@ function applyDeferredCompactionMarker(db, sessionId, pending, directory, truste
|
|
|
9445
9618
|
stepStartedAt = now;
|
|
9446
9619
|
};
|
|
9447
9620
|
try {
|
|
9621
|
+
cut = getCompactionMarkerPublicationState(db, sessionId).pending ? "uncertain" : "definitely-no-cut";
|
|
9622
|
+
const unresolvedIntentFailure = () => {
|
|
9623
|
+
const alreadyUncertain = cut === "uncertain";
|
|
9624
|
+
cut = "uncertain";
|
|
9625
|
+
if (getCompactionMarkerPublicationState(db, sessionId).pending || alreadyUncertain) {
|
|
9626
|
+
return {
|
|
9627
|
+
kind: "retryable-failure",
|
|
9628
|
+
cut,
|
|
9629
|
+
error: new Error("unresolved marker publication intent; retaining retry state")
|
|
9630
|
+
};
|
|
9631
|
+
}
|
|
9632
|
+
cut = "definitely-no-cut";
|
|
9633
|
+
return null;
|
|
9634
|
+
};
|
|
9448
9635
|
const responseFencesTarget = trustedBoundary !== undefined && Number.isSafeInteger(trustedBoundary.rowVersion) && trustedBoundary.rowVersion > 0 && trustedBoundary.ordinal === pending.ordinal && trustedBoundary.endMessageId === pending.endMessageId;
|
|
9449
9636
|
const validation = responseFencesTarget ? "ok" : validatePendingTarget(db, sessionId, pending);
|
|
9450
9637
|
endStep("validate");
|
|
9451
9638
|
if (validation !== "ok") {
|
|
9639
|
+
const unresolved = unresolvedIntentFailure();
|
|
9640
|
+
if (unresolved)
|
|
9641
|
+
return unresolved;
|
|
9452
9642
|
sessionLog2(sessionId, `compaction-marker drain: stale-skip (${validation}) for ordinal ${pending.ordinal} endMessageId=${pending.endMessageId}`);
|
|
9453
9643
|
return { kind: "stale-skip", reason: validation };
|
|
9454
9644
|
}
|
|
9455
9645
|
const existing = getPersistedCompactionMarkerState(db, sessionId);
|
|
9456
9646
|
if (existing && existingMarkerAlreadyCoversTarget(sessionId, existing, pending.ordinal, pending.endMessageId)) {
|
|
9457
|
-
|
|
9647
|
+
if (!unresolvedIntentFailure())
|
|
9648
|
+
return { kind: "already-current" };
|
|
9458
9649
|
}
|
|
9459
9650
|
const boundary = findBoundaryUserMessage(sessionId, pending.endMessageId);
|
|
9460
9651
|
endStep("boundary");
|
|
9461
9652
|
if (!boundary) {
|
|
9462
|
-
return {
|
|
9653
|
+
return unresolvedIntentFailure() ?? {
|
|
9463
9654
|
kind: "retryable-failure",
|
|
9464
9655
|
cut: "definitely-no-cut",
|
|
9465
9656
|
error: new Error(`no user boundary found at or before endMessageId ${pending.endMessageId} (ordinal ${pending.ordinal}); preserving existing marker`)
|
|
9466
9657
|
};
|
|
9467
9658
|
}
|
|
9468
9659
|
if (boundaryWouldDiscardUncoveredMessage(db, sessionId, pending.ordinal, boundary.id)) {
|
|
9660
|
+
const unresolved = unresolvedIntentFailure();
|
|
9661
|
+
if (unresolved)
|
|
9662
|
+
return unresolved;
|
|
9469
9663
|
sessionLog2(sessionId, `compaction-marker drain: stale-skip (partial-message-boundary) for ordinal ${pending.ordinal} endMessageId=${pending.endMessageId}`);
|
|
9470
9664
|
return { kind: "stale-skip", reason: "partial-message-boundary" };
|
|
9471
9665
|
}
|
|
9472
9666
|
endStep("discardCheck");
|
|
9473
|
-
const removedSummaryMessageId = existing?.summaryMessageId ?? null;
|
|
9474
9667
|
cut = "uncertain";
|
|
9475
|
-
const replacement =
|
|
9668
|
+
const replacement = publishRecoverableCompactionMarker(db, {
|
|
9476
9669
|
sessionId,
|
|
9477
9670
|
endOrdinal: pending.ordinal,
|
|
9478
9671
|
endMessageId: pending.endMessageId,
|
|
9479
9672
|
summaryText: MARKER_SUMMARY_TEXT,
|
|
9480
9673
|
directory: directory ?? process.cwd(),
|
|
9481
9674
|
resolvedBoundary: boundary
|
|
9482
|
-
});
|
|
9675
|
+
}, () => JSON.stringify(getPersistedCompactionMarkerState(db, sessionId)) === JSON.stringify(existing) && (responseFencesTarget || validatePendingTarget(db, sessionId, pending) === "ok"));
|
|
9483
9676
|
endStep("replace");
|
|
9484
9677
|
if (replacement.kind === "deferred")
|
|
9485
9678
|
return { kind: "deferred", reason: "candidate-preparation" };
|
|
@@ -9490,14 +9683,8 @@ function applyDeferredCompactionMarker(db, sessionId, pending, directory, truste
|
|
|
9490
9683
|
error: replacement.error
|
|
9491
9684
|
};
|
|
9492
9685
|
}
|
|
9493
|
-
const result = replacement.marker;
|
|
9494
|
-
persistMarkerStateAndDropReplacedTag(db, sessionId, {
|
|
9495
|
-
...result,
|
|
9496
|
-
boundaryOrdinal: pending.ordinal,
|
|
9497
|
-
targetEndMessageId: pending.endMessageId
|
|
9498
|
-
}, removedSummaryMessageId);
|
|
9499
9686
|
endStep("persist");
|
|
9500
|
-
sessionLog2(sessionId, `compaction-marker drain: applied at ordinal ${pending.ordinal}, boundary user msg ${
|
|
9687
|
+
sessionLog2(sessionId, `compaction-marker drain: applied at ordinal ${pending.ordinal}, boundary user msg ${replacement.marker.boundaryMessageId} ${stepMs.join(" ")}`);
|
|
9501
9688
|
return {
|
|
9502
9689
|
kind: "applied",
|
|
9503
9690
|
markerOrdinal: pending.ordinal
|
|
@@ -9518,8 +9705,7 @@ function updateCompactionMarkerAfterPublication(db, sessionId, lastCompartmentEn
|
|
|
9518
9705
|
return false;
|
|
9519
9706
|
}
|
|
9520
9707
|
const existing = getPersistedCompactionMarkerState(db, sessionId);
|
|
9521
|
-
|
|
9522
|
-
if (existing) {
|
|
9708
|
+
if (existing && !getCompactionMarkerPublicationState(db, sessionId).pending) {
|
|
9523
9709
|
if (existingMarkerAlreadyCoversTarget(sessionId, existing, lastCompartmentEnd, targetEndMessageId)) {
|
|
9524
9710
|
return true;
|
|
9525
9711
|
}
|
|
@@ -9532,33 +9718,26 @@ function updateCompactionMarkerAfterPublication(db, sessionId, lastCompartmentEn
|
|
|
9532
9718
|
if (boundaryWouldDiscardUncoveredMessage(db, sessionId, lastCompartmentEnd, boundary.id)) {
|
|
9533
9719
|
return false;
|
|
9534
9720
|
}
|
|
9535
|
-
const replacement =
|
|
9721
|
+
const replacement = publishRecoverableCompactionMarker(db, {
|
|
9536
9722
|
sessionId,
|
|
9537
9723
|
endOrdinal: lastCompartmentEnd,
|
|
9538
9724
|
endMessageId: targetEndMessageId,
|
|
9539
9725
|
summaryText: MARKER_SUMMARY_TEXT,
|
|
9540
9726
|
directory: directory ?? process.cwd(),
|
|
9541
9727
|
resolvedBoundary: boundary
|
|
9542
|
-
});
|
|
9728
|
+
}, () => JSON.stringify(getPersistedCompactionMarkerState(db, sessionId)) === JSON.stringify(existing) && getCompartmentEndMessageIdForOrdinal(db, sessionId, lastCompartmentEnd) === targetEndMessageId);
|
|
9543
9729
|
if (replacement.kind === "committed") {
|
|
9544
|
-
const result = replacement.marker;
|
|
9545
|
-
persistMarkerStateAndDropReplacedTag(db, sessionId, {
|
|
9546
|
-
...result,
|
|
9547
|
-
boundaryOrdinal: lastCompartmentEnd,
|
|
9548
|
-
targetEndMessageId
|
|
9549
|
-
}, removedSummaryMessageId);
|
|
9550
|
-
sessionLog2(sessionId, `compaction-marker: injected at ordinal ${lastCompartmentEnd}, boundary user msg ${result.boundaryMessageId}`);
|
|
9551
9730
|
return true;
|
|
9552
9731
|
}
|
|
9553
9732
|
return false;
|
|
9554
9733
|
}
|
|
9555
|
-
function reconcileForkOrphanedCompactionMarkers(db, sessionId) {
|
|
9556
|
-
if (getHarness2() !== "opencode") {
|
|
9734
|
+
function reconcileForkOrphanedCompactionMarkers(db, sessionId, isCacheBusting) {
|
|
9735
|
+
if (!isCacheBusting || getHarness2() !== "opencode") {
|
|
9557
9736
|
return { removed: 0, failed: false };
|
|
9558
9737
|
}
|
|
9559
9738
|
try {
|
|
9560
9739
|
const owned = getPersistedCompactionMarkerState(db, sessionId);
|
|
9561
|
-
if (!owned) {
|
|
9740
|
+
if (!owned || getCompactionMarkerPublicationState(db, sessionId).pending) {
|
|
9562
9741
|
return { removed: 0, failed: false };
|
|
9563
9742
|
}
|
|
9564
9743
|
const markers = listSessionCompactionMarkers(sessionId);
|
|
@@ -9579,7 +9758,15 @@ function reconcileForkOrphanedCompactionMarkers(db, sessionId) {
|
|
|
9579
9758
|
if (!currentOwned || marker.compactionPartId === currentOwned.compactionPartId) {
|
|
9580
9759
|
continue;
|
|
9581
9760
|
}
|
|
9582
|
-
|
|
9761
|
+
let protectedByState = false;
|
|
9762
|
+
const ok = removeForeignCompactionMarker(sessionId, marker, currentOwned.summaryMessageId, () => db.transaction(() => {
|
|
9763
|
+
const publication = getCompactionMarkerPublicationState(db, sessionId);
|
|
9764
|
+
const latestOwned = getPersistedCompactionMarkerState(db, sessionId);
|
|
9765
|
+
protectedByState = Boolean(publication.pending || !latestOwned || marker.compactionPartId === latestOwned.compactionPartId || marker.summaryMessageIds.includes(latestOwned.summaryMessageId) || compareOpenCodeMessagesByCanonicalOrder(sessionId, marker.boundaryMessageId, latestOwned.boundaryMessageId) !== 1);
|
|
9766
|
+
return !protectedByState;
|
|
9767
|
+
}).deferred());
|
|
9768
|
+
if (protectedByState)
|
|
9769
|
+
continue;
|
|
9583
9770
|
if (ok) {
|
|
9584
9771
|
removed += 1;
|
|
9585
9772
|
sessionLog2(sessionId, `compaction-marker hygiene: removed fork-orphaned marker part=${marker.compactionPartId} boundary=${marker.boundaryMessageId} (outranked owned boundary ${currentOwned.boundaryMessageId})`);
|
|
@@ -12679,8 +12866,8 @@ function prepareCompartmentInjection(db, sessionId, messages, isCacheBusting, co
|
|
|
12679
12866
|
resultEndMessageId = trimEndMessageId;
|
|
12680
12867
|
} else {
|
|
12681
12868
|
const degradedCount = noteDegradedRebuild(sessionId);
|
|
12682
|
-
if (
|
|
12683
|
-
reconcileForkOrphanedCompactionMarkers(db, sessionId);
|
|
12869
|
+
if (isCacheBusting) {
|
|
12870
|
+
reconcileForkOrphanedCompactionMarkers(db, sessionId, isCacheBusting);
|
|
12684
12871
|
}
|
|
12685
12872
|
let reAnchored = false;
|
|
12686
12873
|
if (degradedCount >= REANCHOR_MIN_DEGRADED_PASSES && isCacheBusting) {
|
package/dist/v2/server.js
CHANGED
|
@@ -7874,7 +7874,7 @@ var require_src2 = __commonJS(function(exports, module) {
|
|
|
7874
7874
|
// package.json
|
|
7875
7875
|
var package_default = {
|
|
7876
7876
|
name: "@cortexkit/opencode-magic-context",
|
|
7877
|
-
version: "0.49.
|
|
7877
|
+
version: "0.49.34",
|
|
7878
7878
|
type: "module",
|
|
7879
7879
|
description: "OpenCode plugin for Magic Context — cross-session memory and context management",
|
|
7880
7880
|
main: "dist/index.js",
|
|
@@ -8195,7 +8195,7 @@ async function setup(context) {
|
|
|
8195
8195
|
if (claimStorageBackupWarningCheck() && isMagicContextStorageOutsideEngram(storageDir)) {
|
|
8196
8196
|
log2(`[magic-context] Magic Context storage is at ${storageDir}, outside the folder Engram backs up (~/.local/share/cortexkit/magic-context). It is not included in CortexKit backups.`);
|
|
8197
8197
|
}
|
|
8198
|
-
const { registerContext } = await import("./context-
|
|
8198
|
+
const { registerContext } = await import("./context-s60hcatx.js");
|
|
8199
8199
|
setHarness2("opencode2");
|
|
8200
8200
|
bindStaleBuildNotice({}, import.meta.url, "opencode2");
|
|
8201
8201
|
const duties = await registerContext(context);
|
|
@@ -19,7 +19,7 @@ import {
|
|
|
19
19
|
registerActiveCompartmentRun,
|
|
20
20
|
setRecompStarting2,
|
|
21
21
|
setRecompTerminal2
|
|
22
|
-
} from "./server-
|
|
22
|
+
} from "./server-z2wft9zv.js";
|
|
23
23
|
import"./server-ab8017gw.js";
|
|
24
24
|
import"./server-3mz2qh5a.js";
|
|
25
25
|
import"./server-ab1g0wn9.js";
|
|
@@ -31,7 +31,7 @@ import"./server-cpjk5jd2.js";
|
|
|
31
31
|
import"./server-6vd2n183.js";
|
|
32
32
|
import"./server-awr8qa4c.js";
|
|
33
33
|
import"./server-h1byrhwx.js";
|
|
34
|
-
import"./server-
|
|
34
|
+
import"./server-m1pfg2d4.js";
|
|
35
35
|
import {
|
|
36
36
|
getWrapupInProgressState,
|
|
37
37
|
acquireWrapupInProgress,
|
|
@@ -40,9 +40,9 @@ import {
|
|
|
40
40
|
getHistorianFailureState,
|
|
41
41
|
clearEmergencyRecovery,
|
|
42
42
|
prepareRawSessionOrdinals2
|
|
43
|
-
} from "./server-
|
|
43
|
+
} from "./server-qvy4qavp.js";
|
|
44
44
|
import"./server-v77xa5wj.js";
|
|
45
|
-
import"./server-
|
|
45
|
+
import"./server-jw5qe9y8.js";
|
|
46
46
|
import"./server-1zx87sdm.js";
|
|
47
47
|
import"./server-nxas8v1d.js";
|
|
48
48
|
import"./server-24g8r2d0.js";
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import {
|
|
2
2
|
runManagedWrapup2
|
|
3
|
-
} from "./index-
|
|
4
|
-
import"./index-
|
|
5
|
-
import"./index-
|
|
3
|
+
} from "./index-07vskjn8.js";
|
|
4
|
+
import"./index-ftr84kkv.js";
|
|
5
|
+
import"./index-atekmqef.js";
|
|
6
6
|
import"./index-qgh1qwjj.js";
|
|
7
7
|
import"./index-as2amvn3.js";
|
|
8
|
-
import"./index-
|
|
8
|
+
import"./index-9gpw8hrm.js";
|
|
9
9
|
import"./index-sd3egckz.js";
|
|
10
10
|
import"./index-rfxzazzy.js";
|
|
11
11
|
import"./index-2743x98y.js";
|
|
@@ -14,7 +14,7 @@ import"./index-4dhv7mqz.js";
|
|
|
14
14
|
import"./index-spwjbkc7.js";
|
|
15
15
|
import"./index-waktfbjy.js";
|
|
16
16
|
import"./index-qt1qqasc.js";
|
|
17
|
-
import"./index-
|
|
17
|
+
import"./index-7g9dr8gy.js";
|
|
18
18
|
import"./index-np3rgfw4.js";
|
|
19
19
|
import"./index-1zx87sdm.js";
|
|
20
20
|
import"./index-y1qwzvxn.js";
|
|
@@ -25,7 +25,7 @@ import"./index-nxrsns5x.js";
|
|
|
25
25
|
import"./index-kfyb3k6n.js";
|
|
26
26
|
import"./index-v7287tzq.js";
|
|
27
27
|
import"./index-wfybj1dw.js";
|
|
28
|
-
import"./index-
|
|
28
|
+
import"./index-fm36bjv4.js";
|
|
29
29
|
import"./index-fb3zxn06.js";
|
|
30
30
|
import"./index-7kkphb6c.js";
|
|
31
31
|
import"./index-kq5w68dp.js";
|
package/package.json
CHANGED
|
@@ -27,6 +27,8 @@ const READ_ONLY_DEFERRED_TRANSACTIONS: Record<string, string> = {
|
|
|
27
27
|
"pins one read snapshot so the session's rowid coverage proof and the filtered message search see the same rows; never writes (read-only connections use it too)",
|
|
28
28
|
"packages/plugin/src/hooks/magic-context/lkg-persist.ts#loadPersistedLkgSlot":
|
|
29
29
|
"reads an LKG slot row and its prefix slices as one consistent snapshot; never writes (a failed verification clears the slot afterwards, outside this transaction)",
|
|
30
|
+
"packages/plugin/src/hooks/magic-context/compaction-marker-manager.ts#reconcileForkOrphanedCompactionMarkers":
|
|
31
|
+
"reads publication intent and finalized ownership as one context.db snapshot while host-order probes use the already-open host connection; never writes context.db",
|
|
30
32
|
};
|
|
31
33
|
|
|
32
34
|
const REPOSITORY_ROOT = resolve(import.meta.dir, "../../../..");
|