One poll tick: assign each NEW (since-register) session id to the oldest * unresolved pending in spawn order — same heuristic as the fs-watch path.
(key: string)
| 213 | /** One poll tick: assign each NEW (since-register) session id to the oldest |
| 214 | * unresolved pending in spawn order — same heuristic as the fs-watch path. */ |
| 215 | private async pollOnce(key: string): Promise<void> { |
| 216 | const entry = this.pollEntries.get(key); |
| 217 | if (!entry) return; |
| 218 | const live = entry.pending.filter((p) => p.session.agentSessionId === null); |
| 219 | entry.pending.length = 0; |
| 220 | entry.pending.push(...live); |
| 221 | if (entry.pending.length === 0 || Date.now() - entry.startedAtMs > POLL_MAX_MS) { |
| 222 | this.stopPoll(key); |
| 223 | return; |
| 224 | } |
| 225 | let list: readonly OnDiskSession[]; |
| 226 | try { |
| 227 | list = await entry.adapter.listOnDisk!(entry.cwd); |
| 228 | } catch (err) { |
| 229 | this.logger.warn('transcript_watch.poll_list_failed', { agent: entry.adapter.id, cwd: entry.cwd, err }); |
| 230 | return; |
| 231 | } |
| 232 | const byOldest = [...list].sort((a, b) => a.mtime.localeCompare(b.mtime)); |
| 233 | const claimedThisRound = new Set<string>(); |
| 234 | for (const p of entry.pending) { |
| 235 | if (p.session.agentSessionId !== null) continue; |
| 236 | const pick = byOldest.find((s) => !p.existingBefore.has(s.sessionId) && !claimedThisRound.has(s.sessionId)); |
| 237 | if (!pick) continue; |
| 238 | p.session.setAgentSessionId(pick.sessionId); |
| 239 | claimedThisRound.add(pick.sessionId); |
| 240 | this.logger.info('transcript.session.captured', { |
| 241 | wsId: p.session.wsId, |
| 242 | recordId: p.session.recordId, |
| 243 | agent: p.adapter.id, |
| 244 | agentSessionId: pick.sessionId, |
| 245 | }); |
| 246 | if (this.sessionRegistry) { |
| 247 | void this.sessionRegistry |
| 248 | .update(p.session.wsId, p.session.recordId, { |
| 249 | resumeHint: { kind: 'agent-session-id', value: pick.sessionId }, |
| 250 | }) |
| 251 | .catch((err) => |
| 252 | this.logger.warn('transcript_watch.registry_update_failed', { |
| 253 | wsId: p.session.wsId, |
| 254 | id: p.session.recordId, |
| 255 | err, |
| 256 | }), |
| 257 | ); |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | private stopPoll(key: string): void { |
| 263 | const entry = this.pollEntries.get(key); |
no test coverage detected