MCPcopy Create free account
hub / github.com/cortexkit/magic-context / insertCompactionMarker

Function insertCompactionMarker

packages/cli/src/commands/migrate.ts:665–699  ·  view source on GitHub ↗
(
    entries: PiJson[],
    boundaryEntryId: string | undefined,
)

Source from the content-addressed store, hash-verified

663}
664
665function insertCompactionMarker(
666 entries: PiJson[],
667 boundaryEntryId: string | undefined,
668): CompactionMarkerResult {
669 if (boundaryEntryId === undefined) return { written: false };
670
671 const boundaryIndex = entries.findIndex((entry) => entry.id === boundaryEntryId);
672 if (boundaryIndex < 0) return { written: false };
673
674 const firstKept = entries[boundaryIndex + 1];
675 if (!firstKept?.id) return { written: false };
676
677 const compactedPrefixChars = entries
678 .slice(0, boundaryIndex + 1)
679 .reduce((total, entry) => total + JSON.stringify(entry.message ?? "").length, 0);
680 const compactionId = shortId();
681 const marker: PiJson = {
682 type: "compaction",
683 id: compactionId,
684 parentId: boundaryEntryId,
685 timestamp: String(entries[boundaryIndex].timestamp),
686 summary: MIGRATION_COMPACTION_SUMMARY,
687 firstKeptEntryId: firstKept.id,
688 tokensBefore: Math.ceil(compactedPrefixChars / 4),
689 fromHook: true,
690 };
691
692 firstKept.parentId = compactionId;
693 entries.splice(boundaryIndex + 1, 0, marker);
694 return {
695 written: true,
696 boundaryEntryId,
697 firstKeptEntryId: firstKept.id as string,
698 };
699}
700
701/**
702 * Copy compartments + session_facts from the source OpenCode session

Callers 1

Calls 1

shortIdFunction · 0.85

Tested by

no test coverage detected