active-turn-timing.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * Process-local timing for the logical turn currently owned by a session.
  3. *
  4. * This deliberately lives above PiTurnRunner: one logical local-runtime turn
  5. * may invoke the runner more than once while output safety regenerates a
  6. * response. Persisting this state would also be incorrect because a runtime
  7. * crash cannot resume the in-flight work and would leave a stale running row.
  8. */
  9. export interface LocalActiveTurnTiming {
  10. readonly sessionId: string;
  11. readonly turnId: string;
  12. readonly source?: string;
  13. readonly startedAtMs: number;
  14. readonly endedAtMs?: number;
  15. readonly phase: 'running' | 'settling';
  16. }
  17. export interface LocalActiveTurnTimingReader {
  18. getBySession(sessionId: string): LocalActiveTurnTiming | undefined;
  19. getByTurn(sessionId: string, turnId: string): LocalActiveTurnTiming | undefined;
  20. elapsedSeconds(timing: LocalActiveTurnTiming): number;
  21. onFinished(listener: FinishedListener): () => void;
  22. }
  23. export type FinishedListener = (timing: LocalActiveTurnTiming) => void;
  24. /**
  25. * Keeps at most one timing record per session. Every mutation is fenced by
  26. * both sessionId and turnId so a force-cleared turn cannot erase a successor
  27. * that already acquired the same session.
  28. */
  29. export class LocalActiveTurnTimingRegistry implements LocalActiveTurnTimingReader {
  30. private readonly bySession = new Map<string, LocalActiveTurnTiming>();
  31. private readonly finishedListeners = new Set<FinishedListener>();
  32. constructor(private readonly nowMs: () => number) {}
  33. begin(sessionId: string, turnId: string, source?: string): LocalActiveTurnTiming {
  34. const previous = this.bySession.get(sessionId);
  35. if (previous?.turnId === turnId) return previous;
  36. const timing: LocalActiveTurnTiming = {
  37. sessionId,
  38. turnId,
  39. ...(source ? { source } : {}),
  40. startedAtMs: this.nowMs(),
  41. phase: 'running',
  42. };
  43. this.bySession.set(sessionId, timing);
  44. // Force-clear may let a successor begin before the old promise observes
  45. // its finally. Retire the superseded projection now; the late old finish
  46. // remains harmless because the map already points at the new turnId.
  47. if (previous) this.notifyFinished(previous);
  48. return timing;
  49. }
  50. markSettling(sessionId: string, turnId: string): void {
  51. const current = this.getByTurn(sessionId, turnId);
  52. if (!current || current.phase === 'settling') return;
  53. // Freeze the end time before outer accounting starts. GET projections can
  54. // then remain stable throughout async SQLite and session-finalization work.
  55. this.bySession.set(sessionId, {
  56. ...current,
  57. endedAtMs: this.nowMs(),
  58. phase: 'settling',
  59. });
  60. }
  61. finish(sessionId: string, turnId: string): void {
  62. const current = this.getByTurn(sessionId, turnId);
  63. if (!current) return;
  64. this.bySession.delete(sessionId);
  65. this.notifyFinished(current);
  66. }
  67. private notifyFinished(timing: LocalActiveTurnTiming): void {
  68. // Cleanup observers are best-effort. Timing ownership must be released
  69. // even if one domain observer has a bug in its in-memory teardown.
  70. for (const listener of this.finishedListeners) {
  71. try {
  72. listener(timing);
  73. } catch {
  74. continue;
  75. }
  76. }
  77. }
  78. getBySession(sessionId: string): LocalActiveTurnTiming | undefined {
  79. return this.bySession.get(sessionId);
  80. }
  81. getByTurn(sessionId: string, turnId: string): LocalActiveTurnTiming | undefined {
  82. const timing = this.bySession.get(sessionId);
  83. return timing?.turnId === turnId ? timing : undefined;
  84. }
  85. elapsedSeconds(timing: LocalActiveTurnTiming): number {
  86. const endMs = timing.endedAtMs ?? this.nowMs();
  87. const elapsedMs = Math.max(0, endMs - timing.startedAtMs);
  88. return elapsedMs === 0 ? 0 : Math.ceil(elapsedMs / 1_000);
  89. }
  90. onFinished(listener: FinishedListener): () => void {
  91. this.finishedListeners.add(listener);
  92. return () => this.finishedListeners.delete(listener);
  93. }
  94. }