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

Function copyMagicContextState

packages/cli/src/commands/migrate.ts:716–867  ·  view source on GitHub ↗

* Copy compartments + session_facts from the source OpenCode session * into a new Pi session keyed by the migrated session UUID. Boundary * IDs are remapped from OpenCode message ids to Pi entry ids (the * runtime path also stores entry.id; see read-session-pi.ts and * inject-compartments-pi.ts

(args: {
    cortexkitDb: DatabaseLike;
    sourceSessionId: string;
    piSessionId: string;
    messageIdToLastPiEntryId: Map<string, string>;
    orderedSourceMessageIds: readonly string[];
    now: number;
    dryRun: boolean;
})

Source from the content-addressed store, hash-verified

714 * result counts are accurate, but we don't write anything to the DB.
715 */
716function copyMagicContextState(args: {
717 cortexkitDb: DatabaseLike;
718 sourceSessionId: string;
719 piSessionId: string;
720 messageIdToLastPiEntryId: Map<string, string>;
721 orderedSourceMessageIds: readonly string[];
722 now: number;
723 dryRun: boolean;
724}): CopyMagicContextStatePlan {
725 const sourceCompartments = stmt<CortexkitCompartmentRow>(
726 args.cortexkitDb,
727 `SELECT sequence, start_message, end_message, start_message_id, end_message_id,
728 title, content, created_at,
729 p1, p2, p3, p4, importance, episode_type, legacy
730 FROM compartments
731 WHERE session_id = ? AND harness = 'opencode'
732 ORDER BY sequence ASC`,
733 ).all(args.sourceSessionId);
734
735 const sourceFacts = stmt<CortexkitSessionFactRow>(
736 args.cortexkitDb,
737 `SELECT category, content, created_at, updated_at
738 FROM session_facts
739 WHERE session_id = ? AND harness = 'opencode'
740 ORDER BY category ASC, id ASC`,
741 ).all(args.sourceSessionId);
742
743 let boundariesApproximated = 0;
744 const remappedCompartments: Array<{
745 sequence: number;
746 start_message: number;
747 end_message: number;
748 start_message_id: string;
749 end_message_id: string;
750 title: string;
751 content: string;
752 p1: string | null;
753 p2: string | null;
754 p3: string | null;
755 p4: string | null;
756 importance: number | null;
757 episode_type: string | null;
758 legacy: number;
759 }> = [];
760
761 for (const c of sourceCompartments) {
762 const startRemap = remapBoundaryId(
763 c.start_message_id,
764 args.messageIdToLastPiEntryId,
765 args.orderedSourceMessageIds,
766 );
767 const endRemap = remapBoundaryId(
768 c.end_message_id,
769 args.messageIdToLastPiEntryId,
770 args.orderedSourceMessageIds,
771 );
772 // If either boundary doesn't translate (precedes our migrated
773 // range entirely), skip that compartment. The remaining compartments

Callers 1

Calls 2

stmtFunction · 0.85
remapBoundaryIdFunction · 0.85

Tested by

no test coverage detected