summaryrefslogtreecommitdiff
path: root/subagents/progress.lua
blob: 03fe62a90b2fbced4cc3c3b75e271a78c6cb637a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
-- Expandable progress boards for in-flight children, with a bounded replay
-- path that rebuilds durable child turns when a primary session is reopened.

local paths = require("subagents.paths")

local COLLAPSED_LINES = 4
local MAX_HISTORY_LINES = 512
local MAX_HISTORY_BYTES = 128 * 1024
local MAX_LINE_BYTES = 4096
local MAX_PRESENTATION_BYTES = 32 * 1024
local MAX_PRESENTATION_LINES = 128
local MAX_RENDER_LINES = 4096
local MAX_REPLAY_SESSIONS = 1024
local MAX_REPLAY_MESSAGES = 8192
local MAX_REPLAY_CARDS = 512
local MAX_REPLAY_BLOCKS = 4096
local MAX_REPLAY_BLOCK_BYTES = 64 * 1024
local MAX_REPLAY_MESSAGE_BYTES = 256 * 1024
local BODY_INDENT = "    "
local PROGRESS_TOOLS = {
    ["subagents.run"] = true,
    ["subagents.lua"] = true,
    ["subagents.workflow"] = true,
}

local GLYPHS = {
    running = "◷",
    completed = "✔",
    failed = "✖",
    cancelled = "⊘",
}

local M = {}
local boards = {}
local bound = setmetatable({}, { __mode = "k" })
local tools_collapsed = true
local sid_width = 8
local replay_board
local replaying = true

local function sanitize(text)
    return (tostring(text or ""):gsub("[%z\1-\31\127]", " "))
end

local function sanitize_multiline(text)
    return (tostring(text or ""):gsub("[%z\1-\9\11-\31\127]", " "))
end

local function display_tool_name(name)
    return type(name) == "string" and name:gsub("__", ".") or "tool"
end

local function prefix(text, width)
    if width < 1 then return "" end
    local count, stop = 0, 0
    local ok = pcall(function()
        for pos in utf8.codes(text) do
            if count == width then break end
            stop = pos
            count = count + 1
        end
        if count > 0 then stop = (utf8.offset(text, count + 1) or (#text + 1)) - 1 end
    end)
    if not ok then return text:sub(1, width) end
    return text:sub(1, stop)
end

local function wrap(text, width, emit)
    width = math.max(1, width)
    text = sanitize(text)
    if text == "" then emit(""); return end
    local rest = text
    while rest ~= "" do
        local piece = prefix(rest, width)
        if piece == "" then piece = rest:sub(1, 1) end
        emit(piece)
        rest = rest:sub(#piece + 1)
    end
end

local card_mt = {}
card_mt.__index = card_mt

local function repaint(card)
    local board = card.board
    if board and board.handle then pcall(board.handle.invalidate, board.handle) end
end

local function trim(card)
    while #card.history > MAX_HISTORY_LINES or card.history_bytes > MAX_HISTORY_BYTES do
        local removed = table.remove(card.history, 1)
        card.history_bytes = card.history_bytes - #removed
    end
end

local function adopt(card, line)
    line = sanitize(line):sub(1, MAX_LINE_BYTES)
    card.history[#card.history + 1] = line
    card.history_bytes = card.history_bytes + #line
    trim(card)
end

local function marker(card, text)
    card.partial = false
    if text ~= nil and text ~= "" then adopt(card, text) end
end

local function append_text(card, text)
    text = tostring(text or "")
    local from = 1
    while from <= #text do
        local newline = text:find("\n", from, true)
        local last = newline and newline - 1 or #text
        if card.partial and #card.history > 0 then
            local index = #card.history
            local old = card.history[index]
            local room = MAX_LINE_BYTES - #old
            local chunk = room > 0 and text:sub(from, math.min(last, from + room - 1)) or ""
            local grown = old .. sanitize(chunk)
            card.history[index] = grown
            card.history_bytes = card.history_bytes + #grown - #old
            trim(card)
        else
            adopt(card, text:sub(from, math.min(last, from + MAX_LINE_BYTES - 1)))
        end
        card.partial = newline == nil
        if not newline then break end
        from = newline + 1
    end
end

local function append_labeled(card, label, text)
    text = tostring(text or "")
    local continuation = string.rep(" ", #label)
    local from, first = 1, true
    while true do
        local newline = text:find("\n", from, true)
        local last = newline and newline - 1 or #text
        local prefix = first and label or continuation
        local room = math.max(0, MAX_LINE_BYTES - #prefix)
        local line = room > 0 and text:sub(from, math.min(last, from + room - 1)) or ""
        adopt(card, prefix .. line)
        first = false
        if not newline then break end
        from = newline + 1
    end
end

function card_mt:event(event)
    if type(event) ~= "table" then return end
    local kind, index = event.type, event.index
    if kind == "block_start" then
        self.blocks[index] = event.block_type
        self.block_had_delta[index] = false
    elseif kind == "content_delta" then
        if self.blocks[index] == "text" and type(event.delta) == "string" then
            append_text(self, event.delta)
            self.block_had_delta[index] = true
        end
    elseif kind == "tool_details" then
        self.tools[index] = { id = event.id, name = event.name }
        local detail = display_tool_name(event.name)
        if event.id and event.id ~= "" then detail = detail .. " [" .. event.id .. "]" end
        marker(self, "⚒ " .. detail)
    elseif kind == "block_complete" then
        if event.block_type == "text" and not self.block_had_delta[index] and type(event.text) == "string" then
            append_text(self, event.text)
        elseif event.block_type == "tool_use" then
            if not self.tools[index] then
                local detail = display_tool_name(event.name)
                if event.id and event.id ~= "" then detail = detail .. " [" .. event.id .. "]" end
                marker(self, "⚒ " .. detail)
            end
            if type(event.text) == "string" then append_labeled(self, "input: ", event.text) end
        end
        self.blocks[index], self.block_had_delta[index] = nil, nil
        self.partial = false
    elseif kind == "tool_dispatch_result" then
        for _, result in ipairs(type(event.tool_results) == "table" and event.tool_results or {}) do
            if type(result) == "table" then
                local label = result.is_error and "error" or "result"
                if type(result.tool_use_id) == "string" and result.tool_use_id ~= "" then
                    label = label .. " [" .. result.tool_use_id .. "]"
                end
                append_labeled(self, label .. ": ", result.output)
            end
        end
        self.partial = false
    elseif kind == "message_complete" or kind == "tool_dispatch_complete" then
        self.partial = false
    else
        return
    end
    repaint(self)
end

function card_mt:done(status, message)
    self.status = GLYPHS[status] and status or "failed"
    if self.status ~= "completed" then marker(self, message) else self.partial = false end
    repaint(self)
end

local NOOP = setmetatable({ history = {} }, { __index = { event = function() end, done = function() end } })

local function register_sid(sid)
    if sid == "" then return end
    for _, board in pairs(boards) do
        for _, card in ipairs(board.cards) do
            local other = card.sid
            if other ~= "" and other ~= sid then
                local limit, common = math.min(#sid, #other), 0
                while common < limit and sid:byte(common + 1) == other:byte(common + 1) do
                    common = common + 1
                end
                sid_width = math.max(sid_width, math.min(common + 1, math.max(#sid, #other)))
            end
        end
    end
end

local function body_lines(card, width, collapsed)
    local out, total = {}, 0
    local function add(line)
        total = total + 1
        if collapsed and #out == COLLAPSED_LINES then table.remove(out, 1) end
        if #out < MAX_RENDER_LINES then out[#out + 1] = BODY_INDENT .. line end
    end
    if not collapsed then
        local metadata_truncated = false
        local function add_metadata(label, text)
            local continuation = string.rep(" ", #label)
            local from, first, lines = 1, true, 0
            local function add_bounded(line)
                if lines < MAX_PRESENTATION_LINES then
                    lines = lines + 1
                    add(line)
                else
                    metadata_truncated = true
                end
            end
            while true do
                local newline = text:find("\n", from, true)
                local last = newline and newline - 1 or #text
                wrap((first and label or continuation) .. text:sub(from, last), width, add_bounded)
                first = false
                if not newline then break end
                from = newline + 1
            end
        end
        if card.system_prompt then add_metadata("system prompt: ", card.system_prompt) end
        if card.prompt then add_metadata("prompt: ", card.prompt) end
        if metadata_truncated then add("… prompt display truncated") end
    end
    for _, line in ipairs(card.history) do wrap(line, width, add) end
    if not collapsed and total > #out then
        out[#out] = BODY_INDENT .. "… retained history exceeds render limit"
    end
    return out
end

local function render(board, width)
    local out = {}
    width = math.floor(tonumber(width) or 0)
    if #board.cards == 0 or width <= #BODY_INDENT then return out end
    out[#out + 1] = ""
    for _, card in ipairs(board.cards) do
        local sid = card.sid ~= "" and (" " .. card.sid:sub(1, sid_width) ..
            (sid_width < #card.sid and "…" or "")) or ""
        out[#out + 1] = prefix(sanitize(string.format("%s %s%s — %s",
            GLYPHS[card.status] or GLYPHS.running, card.label, sid, card.status)), width)
        local lines = body_lines(card, width - #BODY_INDENT, board.collapsed)
        for _, line in ipairs(lines) do out[#out + 1] = line end
    end
    if board.pinned then out[#out + 1] = "" end
    return out
end

local function set_board_pinned(board, pinned)
    if board.pinned == pinned or not board.handle then return end
    local ok = pcall(function() board.handle:set_pinned(pinned) end)
    if ok then
        board.pinned = pinned
        pcall(board.handle.invalidate, board.handle)
    end
end

local function prune_boards()
    for key, board in pairs(boards) do
        if board.handle and board.handle.alive then
            local ok, alive = pcall(board.handle.alive, board.handle)
            if ok and not alive then boards[key] = nil end
        end
    end
end

local function new_board()
    local board = { cards = {}, handle = nil, collapsed = tools_collapsed, next_sequence = 0, pinned = false }
    board.component = { render = function(_, width) return render(board, width) end }
    return board
end

function M.claim(event)
    prune_boards()
    local name = event.tool_name
    if not PROGRESS_TOOLS[name] then return end
    if type(event.set_component) ~= "function" then return end
    if type(event.collapsed) == "boolean" then tools_collapsed = event.collapsed end
    local key, board = event.id or event.tool_call_id or name, new_board()
    local attached, handle = pcall(event.set_component, event, board.component)
    if not attached then return end
    board.handle = handle
    local prior = boards[key]
    if prior then set_board_pinned(prior, false) end
    boards[key] = board
    -- Replayed calls are already historical. Only a call dispatched during the
    -- live turn belongs at the bottom of the screen.
    if replaying then
        replay_board(board, type(key) == "string" and key or nil)
    else
        set_board_pinned(board, true)
    end
end

-- A result settles the outer tool invocation. Keep the board attached to its
-- transcript entry, but return it to that entry's original ordering.
function M.settle(event)
    local key = type(event) == "table" and (event.id or event.tool_call_id) or nil
    local board = key and boards[key]
    if not board then return end
    set_board_pinned(board, false)
end

function M.collapse(event)
    if type(event.collapsed) ~= "boolean" then return end
    prune_boards()
    tools_collapsed = event.collapsed
    for _, board in pairs(boards) do
        board.collapsed = tools_collapsed
        if board.handle then pcall(board.handle.invalidate, board.handle) end
    end
end

function M.bind(context)
    local key = type(context) == "table" and context.tool_call_id or nil
    if type(key) ~= "string" or key == "" then key = nil end
    local co = coroutine.running()
    if co then bound[co] = key end
end

-- The outer tool call is durable child-turn metadata, not model-visible output.
-- Expose it to the spawn seam without making the board state global: workflow
-- callbacks run on the same coroutine as their owning tool handler.
function M.tool_call_id()
    local co = coroutine.running()
    return co and bound[co] or nil
end

local function new_card(board, label, id, detail, presentation, sequence)
    if board == nil then return NOOP end
    local sid = type(id) == "string" and sanitize(id):sub(1, MAX_LINE_BYTES) or ""
    register_sid(sid)
    if type(sequence) ~= "number" or sequence < 1 or sequence % 1 ~= 0 then
        board.next_sequence = board.next_sequence + 1
        sequence = board.next_sequence
    else
        board.next_sequence = math.max(board.next_sequence, sequence)
    end
    local card = setmetatable({
        board = board,
        label = sanitize(label or "subagent"):sub(1, MAX_LINE_BYTES),
        sid = sid,
        sequence = sequence,
        status = "running",
        history = {},
        history_bytes = 0,
        partial = false,
        blocks = {},
        block_had_delta = {},
        tools = {},
        prompt = type(presentation) == "table" and type(presentation.prompt) == "string" and
            sanitize_multiline(presentation.prompt):sub(1, MAX_PRESENTATION_BYTES) or nil,
        system_prompt = type(presentation) == "table" and type(presentation.system_prompt) == "string" and
            sanitize_multiline(presentation.system_prompt):sub(1, MAX_PRESENTATION_BYTES) or nil,
    }, card_mt)
    board.cards[#board.cards + 1] = card
    if detail then marker(card, "↳ " .. sanitize(detail):sub(1, MAX_LINE_BYTES)) end
    repaint(card)
    return card
end

function M.card(label, id, detail, presentation)
    local key = bound[coroutine.running()]
    local board = key and boards[key]
    if board == nil or (board.handle and board.handle.alive and not board.handle:alive()) then return NOOP end
    return new_card(board, label, id, detail, presentation)
end

local function nonempty(value)
    return type(value) == "string" and value ~= "" and value or nil
end

-- Keep replay work finite even when a damaged child file contains pathological
-- payloads. The card itself has stricter history limits; this cap bounds the
-- temporary strings made while translating stored blocks into progress events.
local function bounded_text(value, limit)
    if type(value) ~= "string" then return "" end
    return #value > limit and value:sub(1, limit) or value
end

local function text_blocks(blocks, limit)
    if type(blocks) ~= "table" then return "" end
    local pieces, bytes, seen = {}, 0, 0
    for _, block in ipairs(blocks) do
        seen = seen + 1
        if seen > MAX_REPLAY_BLOCKS then break end
        if type(block) == "table" and (block.type == "text" or block.type == "system") and type(block.text) == "string" then
            if #pieces > 0 and bytes < limit then
                pieces[#pieces + 1] = "\n"
                bytes = bytes + 1
            end
            local room = limit - bytes
            if room <= 0 then break end
            local text = block.text:sub(1, room)
            pieces[#pieces + 1] = text
            bytes = bytes + #text
            if #text < #block.text then break end
        end
    end
    return table.concat(pieces)
end

local function blocks_of(message)
    return type(message) == "table" and type(message.blocks) == "table" and message.blocks or {}
end

local function message_text(message)
    return type(message) == "table" and text_blocks(blocks_of(message), MAX_REPLAY_BLOCK_BYTES) or ""
end

local function metadata_for(conv, index)
    if conv == nil or type(conv.message_metadata) ~= "function" then return nil end
    local ok, metadata = pcall(conv.message_metadata, conv, index)
    if not ok or type(metadata) ~= "table" or type(metadata.subagents) ~= "table" then
        return nil
    end
    return metadata.subagents
end

local function owner_of(metadata)
    if type(metadata) ~= "table" then return nil end
    return nonempty(metadata.tool_call_id)
end

local function child_manifest(conv, messages)
    for index = 1, #messages do
        local message = messages[index]
        if type(message) == "table" and message.role == "system" then
            local metadata = metadata_for(conv, index)
            local agent = metadata and nonempty(metadata.agent)
            if agent then
                local system_prompt
                if metadata.inline == true then
                    system_prompt = nonempty(message_text(message))
                end
                return agent, system_prompt
            end
        end
    end
    return nil, nil
end

local function result_text(block)
    if type(block) ~= "table" then return "" end
    local parts = block.parts or block.content
    if type(parts) ~= "table" then return "" end
    local pieces, bytes, seen = {}, 0, 0
    for _, part in ipairs(parts) do
        seen = seen + 1
        if seen > MAX_REPLAY_BLOCKS then break end
        if type(part) == "table" and type(part.text) == "string" then
            local room = MAX_REPLAY_BLOCK_BYTES - bytes
            if room <= 0 then break end
            local text = part.text:sub(1, room)
            pieces[#pieces + 1] = text
            bytes = bytes + #text
            if #text < #part.text then break end
        end
    end
    return table.concat(pieces, "\n")
end

local function is_prompt_message(message)
    return type(message) == "table" and message.role == "user" and message_text(message) ~= ""
end

-- Translate one stored child turn through the same card event path used by a
-- live stream. No outer tool result is involved: all displayed content comes
-- from this child's durable conversation.
local function replay_turn(board, conv, messages, start, finish, info, agent, system_prompt, sequence)
    local metadata = metadata_for(conv, start)
    local prompt = message_text(messages[start])
    if prompt == "" then return end

    local id = nonempty(info and info.id)
    local model = nonempty(metadata and metadata.model) or nonempty(info and info.model)
    local card = new_card(board, agent or "subagent", id, model, {
        prompt = prompt,
        system_prompt = system_prompt,
    }, sequence)
    local pending, terminal_assistant = {}, false
    local block_index = 0

    for index = start + 1, finish - 1 do
        local message = messages[index]
        if type(message) == "table" and message.role == "assistant" then
            local has_tool = false
            local has_response = false
            local seen_blocks = 0
            for _, block in ipairs(blocks_of(message)) do
                seen_blocks = seen_blocks + 1
                if seen_blocks > MAX_REPLAY_BLOCKS then break end
                if type(block) == "table" and block.type == "tool_use" then
                    has_tool = true
                    local tool_id = bounded_text(block.id, MAX_REPLAY_BLOCK_BYTES)
                    local tool_name = bounded_text(block.name, MAX_REPLAY_BLOCK_BYTES)
                    local input = bounded_text(block.input, MAX_REPLAY_BLOCK_BYTES)
                    if tool_id ~= "" then pending[tool_id] = true end
                    card:event({ type = "block_complete", block_type = "tool_use",
                        index = block_index, id = tool_id, name = tool_name, text = input })
                    block_index = block_index + 1
                else
                    -- Thinking-only and other non-text assistant messages are
                    -- still real completed responses; presentation text is
                    -- optional and must not decide terminal status.
                    has_response = true
                    if type(block) == "table" and block.type == "text" then
                        local text = bounded_text(block.text, MAX_REPLAY_BLOCK_BYTES)
                        if text ~= "" then
                            card:event({ type = "block_complete", block_type = "text",
                                index = block_index, text = text })
                        end
                        block_index = block_index + 1
                    end
                end
            end
            -- A response containing a tool call is not the settled assistant
            -- answer; a later assistant message after its results is.
            terminal_assistant = has_response and not has_tool
        elseif type(message) == "table" and message.role == "user" then
            local results = {}
            local seen_blocks = 0
            for _, block in ipairs(blocks_of(message)) do
                seen_blocks = seen_blocks + 1
                if seen_blocks > MAX_REPLAY_BLOCKS then break end
                if type(block) == "table" and block.type == "tool_result" then
                    local tool_id = bounded_text(block.tool_use_id, MAX_REPLAY_BLOCK_BYTES)
                    if tool_id ~= "" then pending[tool_id] = nil end
                    results[#results + 1] = {
                        tool_use_id = tool_id,
                        output = result_text(block),
                        is_error = block.is_error == true,
                    }
                end
            end
            if #results > 0 then
                card:event({ type = "tool_dispatch_result", tool_results = results })
            end
        end
    end

    local status = metadata and nonempty(metadata.status)
    if status ~= "completed" and status ~= "failed" and status ~= "cancelled" then
        status = nil
    end
    if status == nil then
        local settled = terminal_assistant
        if settled then
            for _ in pairs(pending) do
                settled = false
                break
            end
        end
        status = settled and "completed" or "failed"
    end
    card:done(status, status == "failed" and "child turn has no settled assistant response" or nil)
    card.replay_sequence = sequence
    return card
end

local function replay_sequence(metadata)
    local sequence = type(metadata) == "table" and tonumber(metadata.sequence) or nil
    if sequence == nil or sequence < 1 or sequence % 1 ~= 0 then return nil end
    return sequence
end

replay_board = function(board, tool_call_id)
    if board == nil or type(tool_call_id) ~= "string" or tool_call_id == "" then return end

    local path_ok, store_dir = pcall(paths.child_store_dir)
    if not path_ok or type(store_dir) ~= "string" or store_dir == "" then return end
    local panto_ok, panto = pcall(require, "panto")
    if not panto_ok or type(panto) ~= "table" or type(panto.file_system_jsonl_store) ~= "function" then return end
    local store_ok, store = pcall(panto.file_system_jsonl_store, { dir = store_dir })
    if not store_ok or store == nil or type(store.list_bounded) ~= "function" or
        type(store.load_messages) ~= "function" then return end

    -- The store owns both bounds. In particular, this never asks the binding
    -- for an unbounded catalog or conversation and trims only after a backend
    -- has already applied the requested limit.
    local listed, infos = pcall(store.list_bounded, store, { limit = MAX_REPLAY_SESSIONS })
    if not listed or type(infos) ~= "table" then return end
    local sessions = {}
    for _, info in ipairs(infos) do
        if type(info) == "table" and nonempty(info.id) then sessions[#sessions + 1] = info end
    end
    table.sort(sessions, function(a, b)
        local ac, bc = nonempty(a.created), nonempty(b.created)
        if ac and bc and ac ~= bc then return ac < bc end
        return a.id < b.id
    end)

    -- Translate each bounded session into bounded card history immediately;
    -- retaining whole conversations for all sessions would merely move the
    -- unbounded replay footprint from the store to Lua while sorting.
    local scratch = new_board()
    local cards = {}
    local ordinal = 0
    for _, info in ipairs(sessions) do
        if #cards >= MAX_REPLAY_CARDS then break end
        local loaded, conv = pcall(store.load_messages, store, info.id, {
            limit = MAX_REPLAY_MESSAGES,
            from_end = true,
            max_bytes = MAX_REPLAY_MESSAGE_BYTES,
        })
        if loaded and conv ~= nil and type(conv.messages) == "function" then
            local messages_ok, messages = pcall(conv.messages, conv)
            if messages_ok and type(messages) == "table" and #messages > 0 then
                local turns = {}
                local index = 1
                while index <= #messages do
                    if is_prompt_message(messages[index]) then
                        local finish = index + 1
                        while finish <= #messages and not is_prompt_message(messages[finish]) do
                            finish = finish + 1
                        end
                        local metadata = metadata_for(conv, index)
                        if owner_of(metadata) == tool_call_id then
                            turns[#turns + 1] = {
                                start = index,
                                finish = finish,
                                metadata = metadata,
                                sequence = replay_sequence(metadata),
                            }
                        end
                        index = finish
                    else
                        index = index + 1
                    end
                end

                if #turns > 0 then
                    local manifest_ok, manifest_conv = pcall(store.load_messages, store, info.id, {
                        limit = 1,
                        role = "system",
                        metadata_key = "subagents",
                        max_bytes = MAX_REPLAY_MESSAGE_BYTES,
                    })
                    local agent, system_prompt
                    if manifest_ok and manifest_conv ~= nil and type(manifest_conv.messages) == "function" then
                        local got_messages, manifest_messages = pcall(manifest_conv.messages, manifest_conv)
                        if got_messages and type(manifest_messages) == "table" then
                            local got_manifest
                            got_manifest, agent, system_prompt = pcall(child_manifest, manifest_conv, manifest_messages)
                            if not got_manifest then agent, system_prompt = nil, nil end
                        end
                    end
                    for _, turn in ipairs(turns) do
                        if #cards >= MAX_REPLAY_CARDS then break end
                        ordinal = ordinal + 1
                        local card_ok, card = pcall(replay_turn, scratch, conv, messages,
                            turn.start, turn.finish, info, agent, system_prompt, turn.sequence)
                        if card_ok and card ~= nil then
                            card.replay_ordinal = ordinal
                            cards[#cards + 1] = card
                        end
                    end
                end
            end
        end
    end

    table.sort(cards, function(a, b)
        if a.replay_sequence and b.replay_sequence and a.replay_sequence ~= b.replay_sequence then
            return a.replay_sequence < b.replay_sequence
        end
        if (a.replay_sequence ~= nil) ~= (b.replay_sequence ~= nil) then
            return a.replay_sequence ~= nil
        end
        return a.replay_ordinal < b.replay_ordinal
    end)
    for _, card in ipairs(cards) do
        if #board.cards >= MAX_REPLAY_CARDS then break end
        card.board = board
        board.cards[#board.cards + 1] = card
        register_sid(card.sid)
        repaint(card)
    end
end

-- The host fires this before the first live model turn, after startup
-- conversation replay has finished. Keeping the mode explicit avoids scanning
-- child files for every ordinary foreground call.
function M.begin_replay()
    replaying = true
end

function M.begin_live_turn()
    replaying = false
end

function M.reset()
    for _, board in pairs(boards) do
        set_board_pinned(board, false)
    end
    prune_boards()
    bound = setmetatable({}, { __mode = "k" })
end

return M