| 1 | package turnevent |
| 2 | |
| 3 | // transcriptSnapshot is the transcript identity stamped on every envelope: the |
| 4 | // schema-1 revision/digest pair, and for schema-2 sessions the head and leaf |
| 5 | // message the turn ended on, which is known before the save lands. |
| 6 | type transcriptSnapshot struct { |
| 7 | revision int64 |
| 8 | digest string |
| 9 | headID string |
| 10 | leafID string |
| 11 | rewriteEpoch uint64 |
| 12 | } |
| 13 | |
| 14 | func (l *Ledger) SetTranscriptRewriteEpoch(epoch uint64) { |
| 15 | l.mu.Lock() |
| 16 | l.transcript.rewriteEpoch = epoch |
| 17 | l.mu.Unlock() |
| 18 | } |
| 19 | |
| 20 | func (l *Ledger) SetTranscriptSnapshot(revision int64, digest string) { |
| 21 | if l == nil { |
| 22 | return |
| 23 | } |
| 24 | l.mu.Lock() |
| 25 | l.transcript.revision, l.transcript.digest = revision, digest |
| 26 | l.mu.Unlock() |
| 27 | } |
| 28 | |
| 29 | // SetTranscriptHead records the schema-2 head position the next envelopes |
| 30 | // describe. An empty head id clears it (schema-1 session). |
| 31 | func (l *Ledger) SetTranscriptHead(headID, leafID string) { |
| 32 | if l == nil { |
| 33 | return |
| 34 | } |
| 35 | l.mu.Lock() |
| 36 | l.transcript.headID, l.transcript.leafID = headID, leafID |
| 37 | l.mu.Unlock() |
| 38 | } |
| 39 | |
| 40 | // terminalSummaryFor folds a terminal envelope into the summary a checkpoint |
| 41 | // keeps once its events are compacted away. |
| 42 | func terminalSummaryFor(rec Envelope, outcome string, started int64) TerminalSummary { |
| 43 | summary := TerminalSummary{ |
| 44 | TurnID: rec.TurnID, TerminalSequence: rec.Sequence, Status: rec.Status, Outcome: outcome, |
| 45 | RuntimeEpoch: rec.RuntimeEpoch, SubmissionID: rec.SubmissionID, |
| 46 | StartedAt: started, FinishedAt: rec.CreatedAt, |
| 47 | TranscriptRevision: rec.TranscriptRevision, TranscriptDigest: rec.TranscriptDigest, |
| 48 | HeadID: rec.HeadID, LeafMessageID: rec.LeafMessageID, |
| 49 | } |
| 50 | if started > 0 && rec.CreatedAt >= started { |
| 51 | summary.DurationMs = rec.CreatedAt - started |
| 52 | } |
| 53 | return summary |
| 54 | } |
| 55 |