(data, source)
| 146 | } |
| 147 | |
| 148 | function maybeAttachHookSession(data, source) { |
| 149 | const target = resolveHookTranscript(data); |
| 150 | if (!target) return; |
| 151 | let hookSource = null; |
| 152 | |
| 153 | if (state.currentSessionId === target.sessionId && state.transcriptPath && |
| 154 | normalizeFsPath(state.transcriptPath) === normalizeFsPath(target.full)) { |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | const targetHasContent = fileLooksLikeTranscript(target.full); |
| 159 | |
| 160 | if (source === 'session-start') { |
| 161 | hookSource = data.source; |
| 162 | if (hookSource === 'clear' || hookSource === 'resume') { |
| 163 | log(`Deterministic session-start (hookSource=${hookSource}): ${target.sessionId}`); |
| 164 | } else { |
| 165 | if (state.currentSessionId && !state.expectingSwitch) { |
| 166 | const currentHasContent = state.transcriptPath && fileLooksLikeTranscript(state.transcriptPath); |
| 167 | if (!targetHasContent || currentHasContent) { |
| 168 | if (state.currentSessionId !== target.sessionId) { |
| 169 | state.pendingSwitchTarget = { ...target, seenAt: Date.now(), source }; |
| 170 | log(`Queued pending session-start: ${target.sessionId} (current=${state.currentSessionId} currentHasContent=${currentHasContent} targetHasContent=${targetHasContent})`); |
| 171 | } |
| 172 | log(`Ignored session-start: ${target.sessionId} (current=${state.currentSessionId} currentHasContent=${currentHasContent} targetHasContent=${targetHasContent})`); |
| 173 | return; |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | } else if (source === 'pre-tool-use') { |
| 178 | if (state.currentSessionId && state.currentSessionId !== target.sessionId && !targetHasContent) { |
| 179 | log(`Ignored pre-tool-use: ${target.sessionId} (no conversation content)`); |
| 180 | return; |
| 181 | } |
| 182 | } else { |
| 183 | if (state.currentSessionId && state.currentSessionId !== target.sessionId && !state.expectingSwitch) { |
| 184 | log(`Ignored hook session from ${source}: ${target.sessionId} (current=${state.currentSessionId})`); |
| 185 | return; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | log(`Hook session attached from ${source}: ${target.sessionId}`); |
| 190 | attachTranscript({ |
| 191 | full: target.full, |
| 192 | ignoreInitialClearCommand: source === 'session-start' && hookSource === 'clear', |
| 193 | }, 0); |
| 194 | } |
| 195 | |
| 196 | function maybeAttachPendingSwitchTarget(reason, requireReady = true) { |
| 197 | if (!state.pendingSwitchTarget) return false; |
no test coverage detected