| 811 | // (session_id, sequence) is UNIQUE on compartments — a new Pi session uuid |
| 812 | // means no conflict. |
| 813 | const commit = () => { |
| 814 | const insertCompartment = stmt( |
| 815 | args.cortexkitDb, |
| 816 | `INSERT INTO compartments ( |
| 817 | session_id, sequence, start_message, end_message, |
| 818 | start_message_id, end_message_id, title, content, |
| 819 | p1, p2, p3, p4, importance, episode_type, legacy, |
| 820 | created_at, harness |
| 821 | ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'pi')`, |
| 822 | ); |
| 823 | const insertFact = stmt( |
| 824 | args.cortexkitDb, |
| 825 | `INSERT INTO session_facts ( |
| 826 | session_id, category, content, created_at, updated_at, harness |
| 827 | ) VALUES (?, ?, ?, ?, ?, 'pi')`, |
| 828 | ); |
| 829 | args.cortexkitDb.exec("BEGIN IMMEDIATE"); |
| 830 | try { |
| 831 | for (const c of remappedCompartments) { |
| 832 | insertCompartment.run( |
| 833 | args.piSessionId, |
| 834 | c.sequence, |
| 835 | c.start_message, |
| 836 | c.end_message, |
| 837 | c.start_message_id, |
| 838 | c.end_message_id, |
| 839 | c.title, |
| 840 | c.content, |
| 841 | c.p1, |
| 842 | c.p2, |
| 843 | c.p3, |
| 844 | c.p4, |
| 845 | // Preserve v2 metadata so the decay renderer tiers/decays |
| 846 | // migrated history. Without these, rows land legacy=0 + NULL |
| 847 | // tiers and the renderer falls back to full `content` for every |
| 848 | // tier (no decay, prompt bloat). importance mirrors the schema |
| 849 | // default when absent. |
| 850 | typeof c.importance === "number" ? c.importance : 50, |
| 851 | c.episode_type, |
| 852 | c.legacy, |
| 853 | args.now, |
| 854 | ); |
| 855 | } |
| 856 | for (const f of sourceFacts) { |
| 857 | insertFact.run(args.piSessionId, f.category, f.content, f.created_at, f.updated_at); |
| 858 | } |
| 859 | args.cortexkitDb.exec("COMMIT"); |
| 860 | } catch (error) { |
| 861 | args.cortexkitDb.exec("ROLLBACK"); |
| 862 | throw error; |
| 863 | } |
| 864 | }; |
| 865 | |
| 866 | return { ...result, commit }; |
| 867 | } |