summaryrefslogtreecommitdiff
path: root/spec/fake_ext.lua
blob: fce012b990efd54bd102c45537235722fbee838e (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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
-- A scriptable stand-in for the `panto` module the extension talks to.
--
-- Every module reaches the host through `require("panto")` at call time, never
-- as a load-time alias, so a spec can install this fake after the modules are
-- already loaded. `install()` writes `package.loaded.panto`; `handle.restore()`
-- removes it again.
--
-- The surface mirrors the real one, covering what the extension actually calls:
--
--   panto.ext.session_info()        -> { session_id, session_dir, model, reasoning }
--   panto.ext.resolve_model(q)      -> cfg | nil, "resolve_model: ..."
--   panto.ext.models(query)         -> catalog answer
--   panto.ext.agent                 -- the primary, borrowed: :tools(), :conversation()
--   panto.ext.on(name, handler)     -- lifecycle subscriptions
--   panto.ext.register_tool / register_command
--   panto.ext.json                  -- panto's JSON codec (dkjson stands in here)
--   panto.agent{ config =, store =, session_id =, conversation = } -> agent
--   panto.file_system_jsonl_store{ dir = } / panto.null_store()
--   agent:conversation() / :session_id() / :tools() / :set_tools(decls) / :run_async(opts)
--   store:list() / :list_bounded(opts) / :resolve(id) / :load(id) /
--   :load_messages(id, opts)
--   conv:messages() / :message_metadata(i) / :add_system_message(text, { metadata = })
--   job:next_event() / :result() / :request_cancel() / :close()
--
-- Scripting. Every child takes its outcome from a queue: `handle.queue(outcome)`
-- is matched first-in-first-out, `handle.queue_for(label, outcome)` is matched
-- against the profile name the child was seeded with (its manifest metadata; a
-- resumed child carries no fresh manifest, so it always draws from the FIFO
-- queue). An outcome describes the settled turn — `{ status =, output =,
-- error =, structured_json =, resumable =, id =, settle = }`. A child with
-- nothing queued completes with a generated id and a generic output, which
-- keeps tests that only care about what was asked for short.
--
-- Settling. There is no event loop here, so a fake job settles on the Nth call
-- to `job:result()`: `settle = N`, lowest first, which is the same ordering key
-- the previous fake used for "first" awaits. Real wake pipes are opened and
-- polled (the job machinery refuses to start a child without one), but nothing
-- ever runs the loop and nothing writes a wake byte; the specs call await from
-- a plain script, which cannot park, so awaiting drains its jobs in place and
-- those polls are what turn into progress. `request_cancel` settles the next
-- poll as cancelled, exactly like the binding's pump.
--
-- Everything the host was asked to do is recorded on the returned handle:
-- `spawns` (one record per child agent, in creation order, carrying the
-- resolve_model arguments, the store directory, the seeded system messages, the
-- tool declarations it was given and the run_async options), `runs` (run_async
-- options only, so a gated child is visibly not started yet), `jobs`,
-- `resolves`, `tools`, `commands`, `models_queries`, `subscriptions`, `mkdirs`,
-- `max_live` and `polls`. Assertions read those tables directly.

local M = {}

-- Enough of panto.text for progress-renderer specs; dedicated host tests cover
-- the real terminal-width implementation.
M.text = {
    truncate = function(value, width)
        return tostring(value or ""):sub(1, math.max(0, width))
    end,
    wrap = function(value, width)
        width = math.max(1, width)
        value = tostring(value or "")
        if value == "" then return { "" } end
        local rows = {}
        for line in (value .. "\n"):gmatch("(.-)\n") do
            repeat
                rows[#rows + 1] = line:sub(1, width)
                line = line:sub(width + 1)
            until line == ""
        end
        return rows
    end,
}

-- ---------------------------------------------------------------------------
-- luv stand-in, installed once when this module loads
-- ---------------------------------------------------------------------------

-- `fs_mkdir` is recorded rather than performed, so a spec can assert which
-- directories a child store would need without touching the disk. Everything
-- else — pipes included — delegates to the real luv, so a spec exercises the
-- same wake-pipe arming production does. This has to happen at load time, not
-- inside install(), because a module that resolves luv once at load would
-- otherwise capture the real one.
local made_dirs = {}
do
    local ok, real = pcall(require, "luv")
    if ok and type(real) == "table" then
        package.loaded.luv = setmetatable({
            fs_mkdir = function(path)
                made_dirs[#made_dirs + 1] = path
                return true
            end,
        }, { __index = real })
    end
end

-- A job polled this many times without settling means the drain loop is
-- spinning; failing beats hanging the whole suite.
local POLL_LIMIT = 5000

local DEFAULT_SESSION = {
    session_id = "0198-primary",
    session_dir = "/tmp/panto-spec-sessions/--Users-travis-Code-panto-subagents--",
    model = "anthropic:sonnet",
    reasoning = "medium",
}

local DEFAULT_OVERVIEW = {
    model = "anthropic:sonnet",
    reasoning = "medium",
    providers = {
        { name = "anthropic", style = "messages", models = 7 },
        { name = "openai", style = "responses", models = 12 },
    },
}

-- Stands in for the lightuserdata re-registration tag a real decl carries. The
-- identity is what matters: a child must be given the primary's own tags.
M.SOURCE = setmetatable({}, { __name = "fake.tool_source" })

-- The primary's tools: two a child inherits, and the four it must not.
-- agent:tools() sorts by name, so this is the order the rock sees them in.
local DEFAULT_TOOL_NAMES = {
    "bash",
    "read_file",
    "subagents.lua",
    "subagents.models",
    "subagents.run",
    "subagents.workflow",
}

local function default_tools()
    local decls = {}
    for index, name in ipairs(DEFAULT_TOOL_NAMES) do
        decls[index] = {
            name = name,
            description = name .. " does a thing",
            schema = { type = "object", properties = {} },
            _source = M.SOURCE,
        }
    end
    return decls
end

-- panto installs its own JSON codec as `panto.ext.json`; dkjson is the closest
-- stand-in available to a bare `lua` process. Absent, the field is simply
-- missing, exactly as it would be on a host too old to provide it.
local function json_codec()
    local ok, dkjson = pcall(require, "dkjson")
    if not ok or type(dkjson) ~= "table" then
        return nil
    end
    return {
        encode = function(value)
            return dkjson.encode(value)
        end,
        decode = function(text)
            local value, _, err = dkjson.decode(text)
            if err then
                error(err, 0)
            end
            return value
        end,
    }
end

local function copy(source, fallback)
    local out = {}
    for key, value in pairs(source or fallback or {}) do
        out[key] = value
    end
    return out
end

-- ---------------------------------------------------------------------------
-- Conversations
-- ---------------------------------------------------------------------------

local conv_mt = {}
conv_mt.__index = conv_mt
conv_mt.__name = "fake.conversation"

-- scripted = array of { role =, text = | blocks =, metadata = }. `record`, when
-- given, is the child record seeded system messages are mirrored onto. A
-- scripted message may instead carry `metadata_error =`, which is how the
-- binding reports a stored record it cannot decode: by raising.
local function new_conversation(scripted, record)
    local messages = {}
    for index, message in ipairs(scripted or {}) do
        messages[index] = {
            role = message.role or "user",
            blocks = message.blocks or { { type = "text", text = message.text or "" } },
            metadata = message.metadata,
            metadata_error = message.metadata_error,
        }
    end
    return setmetatable({ _messages = messages, _record = record }, conv_mt)
end

-- Faithful to the binding: metadata is not part of a message table, so the only
-- way to reach it is message_metadata(i).
function conv_mt:messages()
    local out = {}
    for index, message in ipairs(self._messages) do
        local blocks = {}
        for block_index, block in ipairs(message.blocks) do
            blocks[block_index] = copy(block)
        end
        out[index] = { role = message.role, blocks = blocks }
    end
    return out
end

function conv_mt:len()
    return #self._messages
end

function conv_mt:message_metadata(index)
    local message = self._messages[tonumber(index) or 0]
    if message == nil then
        return nil
    end
    if message.metadata_error then
        error("panto: " .. message.metadata_error, 2)
    end
    return message.metadata
end

function conv_mt:add_system_message(text, opts)
    if type(text) ~= "string" then
        error("panto: add_system_message expects a string", 2)
    end
    local metadata = opts and opts.metadata
    if metadata ~= nil and type(metadata) ~= "table" then
        error("panto: metadata must be a table", 2)
    end
    self._messages[#self._messages + 1] = {
        role = "system",
        blocks = { { type = "text", text = text } },
        metadata = metadata,
    }
    local record = self._record
    if record then
        record.system_messages[#record.system_messages + 1] = { text = text, metadata = metadata }
        local manifest = metadata and metadata.subagents
        if type(manifest) == "table" and type(manifest.agent) == "string" then
            record.label = manifest.agent
        end
    end
end

-- ---------------------------------------------------------------------------
-- Session stores
-- ---------------------------------------------------------------------------

local store_mt = {}
store_mt.__index = store_mt
store_mt.__name = "fake.store"

function store_mt:resolve(id)
    local session = self._harness.sessions[id]
    if session == nil then
        return nil
    end
    return { id = id, message_count = #session, api_style = "messages" }
end

function store_mt:list()
    if self._harness.reject_unbounded_replay then error("unbounded list was used", 2) end
    local ids = {}
    for id in pairs(self._harness.sessions) do
        ids[#ids + 1] = id
    end
    table.sort(ids)
    local out = {}
    for _, id in ipairs(ids) do
        out[#out + 1] = {
            id = id,
            created = "",
            modified = "",
            message_count = #self._harness.sessions[id],
            model = "",
        }
    end
    return out
end

function store_mt:list_bounded(opts)
    local limit = type(opts) == "table" and math.max(0, math.floor(tonumber(opts.limit) or 0)) or 0
    self._harness.bounded_list_calls = self._harness.bounded_list_calls + 1
    local ids = {}
    for id in pairs(self._harness.sessions) do ids[#ids + 1] = id end
    table.sort(ids)
    local out = {}
    for index = 1, math.min(limit, #ids) do
        local id = ids[index]
        out[#out + 1] = {
            id = id,
            created = "",
            modified = "",
            message_count = #self._harness.sessions[id],
            model = "",
        }
    end
    return out
end

local function metadata_has_key(metadata, key)
    return type(metadata) == "table" and metadata[key] ~= nil
end

function store_mt:load_messages(id, opts)
    self._harness.bounded_load_calls = self._harness.bounded_load_calls + 1
    local source = self._harness.sessions[id]
    if type(source) ~= "table" then return nil end
    opts = type(opts) == "table" and opts or {}
    local selected = {}
    for index, message in ipairs(source) do
        local role_ok = opts.role == nil or message.role == opts.role
        local metadata_ok = opts.metadata_key == nil or metadata_has_key(message.metadata, opts.metadata_key)
        if role_ok and metadata_ok then selected[#selected + 1] = index end
    end
    local indices = {}
    if opts.from_end then
        local first = math.max(1, #selected - (tonumber(opts.limit) or 0) + 1)
        for index = first, #selected do indices[#indices + 1] = selected[index] end
    else
        for index = 1, math.min(#selected, tonumber(opts.limit) or 0) do indices[#indices + 1] = selected[index] end
    end
    local messages = {}
    for _, index in ipairs(indices) do messages[#messages + 1] = source[index] end
    return new_conversation(messages)
end

function store_mt:load(id)
    if self._harness.reject_unbounded_replay then error("unbounded load was used", 2) end
    local session = self._harness.sessions[id]
    if session == nil then
        return nil
    end
    return new_conversation(session)
end

-- ---------------------------------------------------------------------------
-- Jobs
-- ---------------------------------------------------------------------------

local job_mt = {}
job_mt.__index = job_mt
job_mt.__name = "fake.job"

local function new_job(cfg)
    return setmetatable({
        _settle_after = math.max(1, math.floor(tonumber(cfg.settle) or 1)),
        _events = cfg.events or {},
        _finish = cfg.finish,
        _harness = cfg.harness,
        _wake_fd = cfg.wake_fd,
        _polls = 0,
        _settled = false,
        _result = nil,
        _cancel_requested = false,
        _closed = false,
    }, job_mt)
end

function job_mt:next_event()
    if self._closed or #self._events == 0 then
        return nil
    end
    return table.remove(self._events, 1)
end

function job_mt:result()
    if self._closed then
        -- close() frees the settled result in the binding, so a caller that
        -- reads it back afterwards sees exactly this.
        return nil
    end
    self._polls = self._polls + 1
    if self._polls > POLL_LIMIT then
        error(string.format(
            "fake job: polled %d times without settling; the drain loop is making no progress on a job with no wake pipe",
            POLL_LIMIT), 0)
    end
    if self._harness then
        self._harness.polls = self._harness.polls + 1
    end
    if self._settled then
        return self._result
    end
    if self._cancel_requested then
        self._result = { status = "cancelled", error = "cancelled" }
    elseif self._polls >= self._settle_after then
        self._result = self._finish(self)
    else
        if self._wake_fd then pcall(require("luv").fs_write, self._wake_fd, "x", -1) end
        return nil
    end
    self._settled = true
    if self._harness then
        self._harness.live = self._harness.live - 1
    end
    return self._result
end

function job_mt:request_cancel()
    self._cancel_requested = true
end

function job_mt:close()
    if self._closed then
        return
    end
    self._closed = true
    self._result = nil
    self._events = {}
end

-- job(spec) -> a standalone fake job, for specs that drive the job machinery
-- directly rather than through a child. spec = { result =, events =, settle = }.
function M.job(spec)
    spec = spec or {}
    return new_job({
        settle = spec.settle,
        events = spec.events,
        finish = function()
            return spec.result or { status = "completed", text = "done" }
        end,
    })
end

-- ---------------------------------------------------------------------------
-- Outcomes: which scripted turn a child gets, and what it settles to
-- ---------------------------------------------------------------------------

local function next_outcome(h, label)
    local bucket = label and h.labelled[label]
    if bucket and #bucket > 0 then
        return table.remove(bucket, 1)
    end
    if #h.queued > 0 then
        return table.remove(h.queued, 1)
    end
    return {}
end

-- Bound as late as possible: the profile label only exists once the child has
-- been seeded with its manifest system message.
local function outcome_for(h, record)
    if record.outcome == nil then
        record.outcome = next_outcome(h, record.label)
    end
    return record.outcome
end

local function child_id(h, record)
    if record.session_id then
        return record.session_id
    end
    if record.id == nil then
        record.id = outcome_for(h, record).id or ("child-" .. record.index)
    end
    return record.id
end

-- The settled turn, in the shape agent:run_async reports it.
local function settled_result(h, record)
    local outcome = outcome_for(h, record)
    local status = outcome.status or "completed"
    local result = { status = status }
    local one_shot = record.run and record.run.dispatch_tools == false
    if status == "completed" then
        if one_shot then
            result.text = outcome.output
            if outcome.structured_json then
                local decl = record.tool_decls and record.tool_decls[1]
                result.tool_calls = { {
                    id = "call-" .. record.index,
                    name = outcome.tool_name or (decl and decl.name) or "emit_result",
                    input = outcome.structured_json,
                } }
            end
        else
            result.text = outcome.output or ("output of " .. tostring(record.label or child_id(h, record)))
        end
    else
        result.error = outcome.error or ("the child " .. status)
    end

    -- The durable file: a child that never reached its first assistant message
    -- has none, which is what makes it unresumable.
    local id = child_id(h, record)
    if outcome.resumable == false then
        h.sessions[id] = nil
    else
        h.sessions[id] = h.sessions[id] or {}
    end
    return result
end

-- ---------------------------------------------------------------------------
-- Agents
-- ---------------------------------------------------------------------------

local agent_mt = {}
agent_mt.__index = agent_mt
agent_mt.__name = "fake.agent"

function agent_mt:conversation()
    return self._conv
end

function agent_mt:add_system_message(text)
    return self._conv:add_system_message(text)
end

function agent_mt:submit(value)
    if not self._borrowed then
        error("panto: submit is only available on the host session agent", 2)
    end
    self._harness.submissions[#self._harness.submissions + 1] = value
end

function agent_mt:set_message_metadata(index, metadata)
    self._record.final_metadata = metadata
    return true
end

function agent_mt:session_id()
    return child_id(self._harness, self._record)
end

function agent_mt:tools()
    local out = {}
    for index, decl in ipairs(self._record.tool_decls or {}) do
        out[index] = decl
    end
    return out
end

function agent_mt:set_tools(decls)
    if self._borrowed then
        error("panto: set_tools is not supported on a borrowed agent", 2)
    end
    if type(decls) ~= "table" then
        error("panto: set_tools expects an array of declarations", 2)
    end
    local names = {}
    for index, decl in ipairs(decls) do
        if type(decl) ~= "table" or type(decl.name) ~= "string" then
            error("panto: tool declaration " .. index .. " has no name", 2)
        end
        names[index] = decl.name
    end
    self._record.tool_decls = decls
    self._record.tools = names
end

function agent_mt:run_async(options)
    if self._borrowed then
        return nil, "run_async: not supported on a borrowed agent"
    end
    if type(options) ~= "table" then
        error("panto: run_async expects a table", 2)
    end
    if options.prompt == nil and options.blocks == nil then
        return nil, "run_async: pass a prompt or blocks"
    end
    if options.metadata ~= nil and type(options.metadata) ~= "table" then
        return nil, "run_async: metadata must be a table"
    end

    local h, record = self._harness, self._record
    record.run = {
        prompt = options.prompt,
        blocks = options.blocks,
        metadata = options.metadata,
        dispatch_tools = options.dispatch_tools,
        wake_fd = options.wake_fd,
    }
    record.prompt = options.prompt
    h.runs[#h.runs + 1] = record.run

    local job = new_job({
        settle = outcome_for(h, record).settle,
        events = outcome_for(h, record).events,
        harness = h,
        wake_fd = options.wake_fd,
        finish = function()
            return settled_result(h, record)
        end,
    })
    h.jobs[#h.jobs + 1] = job
    record.job = job
    h.live = h.live + 1
    if options.wake_fd then pcall(require("luv").fs_write, options.wake_fd, "x", -1) end
    if h.live > h.max_live then
        h.max_live = h.live
    end
    return job
end

-- ---------------------------------------------------------------------------
-- install
-- ---------------------------------------------------------------------------

-- install(opts) -> handle
--
-- opts = {
--   session          = { session_id =, session_dir =, model =, reasoning = },
--   models_response  = table | function(query),
--   primary_tools    = array of decl tables (defaults to DEFAULT_TOOL_NAMES),
--   primary_messages = array of { role =, text = } for the primary conversation,
--   sessions         = { [id] = array of scripted messages } already on disk,
--   unknown_models   = { ["provider:model"] = true } resolve_model rejects,
-- }
function M.install(opts)
    opts = opts or {}

    for index = #made_dirs, 1, -1 do
        made_dirs[index] = nil
    end

    -- A partial `session` overrides only the fields it names.
    local session = copy(DEFAULT_SESSION)
    for key, value in pairs(opts.session or {}) do
        session[key] = value
    end

    local handle = {
        session = session,
        models_response = opts.models_response or DEFAULT_OVERVIEW,
        primary_tools = opts.primary_tools or default_tools(),
        sessions = opts.sessions or {},
        unknown_models = opts.unknown_models or {},
        spawns = {},
        runs = {},
        jobs = {},
        resolves = {},
        stores = {},
        tools = {},
        tools_by_name = {},
        commands = {},
        commands_by_name = {},
        models_queries = {},
        subscriptions = {},
        submissions = {},
        emitted = {},
        on_by_name = {},
        mkdirs = made_dirs,
        queued = {},
        labelled = {},
        polls = 0,
        live = 0,
        max_live = 0,
        bounded_list_calls = 0,
        bounded_load_calls = 0,
    }

    function handle.queue(outcome)
        handle.queued[#handle.queued + 1] = outcome or {}
    end

    function handle.queue_for(label, outcome)
        local bucket = handle.labelled[label]
        if bucket == nil then
            bucket = {}
            handle.labelled[label] = bucket
        end
        bucket[#bucket + 1] = outcome or {}
    end

    -- A child session that already exists on disk, for the resume cases.
    function handle.add_session(id, messages)
        handle.sessions[id] = messages or {}
    end

    function handle.made_dir(path)
        for _, made in ipairs(handle.mkdirs) do
            if made == path then
                return true
            end
        end
        return false
    end

    -- resolve_model hands back an opaque config; this is how a spec gets from
    -- the config a child was built with back to the query that produced it.
    local resolved_from = setmetatable({}, { __mode = "k" })

    local ext = {}

    -- The host's layer list, in the same shape and order panto installs it.
    ext.dirs = opts.dirs or {
        home = "/home/u",
        project_root = "/proj",
        layers = {
            { name = "base", dir = "/data/panto/agent" },
            { name = "user", dir = "/home/u/.config/panto" },
            { name = "project", dir = "/proj/.panto" },
            { name = "local", dir = "/proj/.panto/local" },
        },
    }

    function ext.session_info()
        return copy(handle.session)
    end

    function ext.resolve_model(query)
        if type(query) ~= "table" then
            return nil, "resolve_model: expected a table of arguments"
        end
        local request = {
            model = query.model,
            reasoning = query.reasoning,
            tool_choice = query.tool_choice,
        }
        handle.resolves[#handle.resolves + 1] = request
        if type(query.model) ~= "string" or query.model == "" then
            return nil, "resolve_model: model must be a 'provider:model' string"
        end
        if handle.unknown_models[query.model] then
            return nil, string.format("resolve_model: unknown model '%s'", query.model)
        end
        local cfg = {
            model = query.model,
            reasoning = query.reasoning,
            style = "messages",
            wire_model = query.model:match(":(.+)$") or query.model,
        }
        resolved_from[cfg] = request
        return cfg
    end

    function ext.models(query)
        handle.models_queries[#handle.models_queries + 1] = query or {}
        local response = handle.models_response
        if type(response) == "function" then
            return response(query or {})
        end
        return response
    end

    function ext.on(name, fn)
        handle.subscriptions[#handle.subscriptions + 1] = { name = name, fn = fn }
        handle.on_by_name[name] = fn
    end

    function ext.emit(name)
        handle.emitted[#handle.emitted + 1] = name
    end

    -- Fire a subscribed lifecycle handler, the way the host would.
    function handle.emit(name, event)
        for _, subscription in ipairs(handle.subscriptions) do
            if subscription.name == name then
                subscription.fn(event)
            end
        end
    end

    function ext.register_tool(tool)
        handle.tools[#handle.tools + 1] = tool
        handle.tools_by_name[tool.name] = tool
    end

    function ext.register_command(command)
        handle.commands[#handle.commands + 1] = command
        handle.commands_by_name[command.name] = command
    end

    ext.json = json_codec()

    -- The primary agent, borrowed: readable tools and conversation, nothing else.
    ext.agent = setmetatable({
        _borrowed = true,
        _harness = handle,
        _record = { index = 0, tool_decls = handle.primary_tools, system_messages = {}, tools = {} },
        _conv = new_conversation(opts.primary_messages),
    }, agent_mt)

    local function new_store(kind, arg)
        local dir = nil
        if type(arg) == "table" then
            dir = arg.dir
        elseif type(arg) == "string" then
            dir = arg
        end
        if kind == "fs" and type(dir) ~= "string" then
            error("panto.file_system_jsonl_store: missing dir", 2)
        end
        handle.stores[#handle.stores + 1] = dir
        return setmetatable({ dir = dir, kind = kind, _harness = handle }, store_mt)
    end

    local function new_agent(options)
        if type(options) ~= "table" then
            error("panto.agent expects a table", 2)
        end
        local record = {
            index = #handle.spawns + 1,
            config = options.config,
            store_dir = type(options.store) == "table" and options.store.dir or nil,
            session_id = options.session_id,
            resumed = options.session_id ~= nil,
            system_messages = {},
            tools = {},
            resolve = resolved_from[options.config],
        }
        if record.resolve then
            record.model = record.resolve.model
            record.reasoning = record.resolve.reasoning
            record.tool_choice = record.resolve.tool_choice
        end
        handle.spawns[record.index] = record

        local conv = options.conversation
        if conv == nil then
            conv = new_conversation(nil, record)
        else
            conv._record = record
        end
        return setmetatable({ _harness = handle, _record = record, _conv = conv }, agent_mt)
    end

    handle.ext = ext
    package.loaded.panto = {
        ext = ext,
        text = M.text,
        agent = new_agent,
        file_system_jsonl_store = function(arg)
            return new_store("fs", arg)
        end,
        null_store = function()
            return new_store("null", nil)
        end,
    }

    function handle.restore()
        package.loaded.panto = nil
    end

    return handle
end

return M