MCPcopy Create free account
hub / github.com/WJX20/claude-code / applyPreservedSegmentRelinks

Function applyPreservedSegmentRelinks

src/utils/sessionStorage.ts:1840–1957  ·  view source on GitHub ↗

* Splice the preserved segment back into the chain after compaction. * * Preserved messages exist in the JSONL with their ORIGINAL pre-compact * parentUuids (recordTranscript dedup-skipped them — can't rewrite). * The internal chain (keep[i+1]→keep[i]) is intact; only endpoints need * patching:

(
  messages: Map<UUID, TranscriptMessage>,
)

Source from the content-addressed store, hash-verified

1838 * Mutates the Map in place.
1839 */
1840function applyPreservedSegmentRelinks(
1841 messages: Map<UUID, TranscriptMessage>,
1842): void {
1843 type Seg = NonNullable<
1844 SystemCompactBoundaryMessage['compactMetadata']['preservedSegment']
1845 >
1846
1847 // Find the absolute-last boundary and the last seg-boundary (can differ:
1848 // manual /compact after reactive compact → seg is stale).
1849 let lastSeg: Seg | undefined
1850 let lastSegBoundaryIdx = -1
1851 let absoluteLastBoundaryIdx = -1
1852 const entryIndex = new Map<UUID, number>()
1853 let i = 0
1854 for (const entry of messages.values()) {
1855 entryIndex.set(entry.uuid, i)
1856 if (isCompactBoundaryMessage(entry)) {
1857 absoluteLastBoundaryIdx = i
1858 const seg = entry.compactMetadata?.preservedSegment
1859 if (seg) {
1860 lastSeg = seg
1861 lastSegBoundaryIdx = i
1862 }
1863 }
1864 i++
1865 }
1866 // No seg anywhere → no-op. findUnresolvedToolUse etc. read the full map.
1867 if (!lastSeg) return
1868
1869 // Seg stale (no-seg boundary came after): skip relink, still prune at
1870 // absolute — otherwise the stale preserved chain becomes a phantom leaf.
1871 const segIsLive = lastSegBoundaryIdx === absoluteLastBoundaryIdx
1872
1873 // Validate tail→head BEFORE mutating so malformed metadata is a true
1874 // no-op (walk stops at headUuid, doesn't need the relink to run first).
1875 const preservedUuids = new Set<UUID>()
1876 if (segIsLive) {
1877 const walkSeen = new Set<UUID>()
1878 let cur = messages.get(lastSeg.tailUuid)
1879 let reachedHead = false
1880 while (cur && !walkSeen.has(cur.uuid)) {
1881 walkSeen.add(cur.uuid)
1882 preservedUuids.add(cur.uuid)
1883 if (cur.uuid === lastSeg.headUuid) {
1884 reachedHead = true
1885 break
1886 }
1887 cur = cur.parentUuid ? messages.get(cur.parentUuid) : undefined
1888 }
1889 if (!reachedHead) {
1890 // tail→head walk broke — a UUID in the preserved segment isn't in the
1891 // transcript. Returning here skips the prune below, so resume loads
1892 // the full pre-compact history. Known cause: mid-turn-yielded
1893 // attachment pushed to mutableMessages but never recordTranscript'd
1894 // (SDK subprocess restarted before next turn's qe:420 flush).
1895 logEvent('tengu_relink_walk_broken', {
1896 tailInTranscript: messages.has(lastSeg.tailUuid),
1897 headInTranscript: messages.has(lastSeg.headUuid),

Callers 1

loadTranscriptFileFunction · 0.85

Calls 7

isCompactBoundaryMessageFunction · 0.85
logEventFunction · 0.85
setMethod · 0.80
deleteMethod · 0.80
getMethod · 0.45
hasMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected