summaryrefslogtreecommitdiff
path: root/tests/lifecycle.lua
blob: 061dbb2a24c97b28f6b2f19e8dd09b4700396c44 (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
-- Exercise the real protocol lifecycle without launching agy or requiring a UI.
local uv, real_panto = require('luv'), require('panto')
local root=assert(uv.fs_realpath(debug.getinfo(1,'S').source:sub(2))):match('^(.*)/tests/[^/]+$')..'/'
local protocol, launched, events = nil, {}, {}
local ext={json=real_panto.ext.json,config={providers={test={protocol='antigravity-acp',usage_footer=false}}},
    register_command=function() end, on=function(name,fn) events[name]=fn end,
    register_protocol=function(p) protocol=p end,
    session_info=function() error('non-UI open must not query session_info') end}
local acp={message_bytes=196608,effort_levels={},checkpoint=function() end,
    launch=function(s,parts)
        local key=parts[1].text; assert(not launched[key]); launched[key]=s
        s.session='stub-'..key; s.shutdown=function() s.stopped=true end
    end}
local old_dofile,old_panto=dofile,package.loaded.panto
package.loaded.panto={ext=ext}
_G.dofile=function(path) if path==root..'acp.lua' then return acp end; return old_dofile(path) end
local ok,err=xpcall(function()
    old_dofile(root..'extensions/antigravity-acp.lua').activate()
    local function open(context,purpose,text)
        return protocol.open{context=context,purpose=purpose,provider='test',model='fixture',tools={},
            messages={{role='user',blocks={{type='text',text=text}}}},notify=function() end}
    end
    -- Serve IDs are opaque, while a child may have a durable thread ID.
    local child=('%x-%x'):format(uv.getpid(),uv.hrtime())
    local serve=open('serve/opaque:../context','turn','serve')
    local main=open(child,'turn','child')
    local compact=open(child,'compaction','compact')
    local a,b,c=assert(launched.serve),assert(launched.child),assert(launched.compact)
    assert(a~=b and b~=c and a~=c and not a.lock and b.lock and not c.lock)
    assert(uv.fs_stat(a.dir) and uv.fs_stat(b.dir) and uv.fs_stat(c.dir) and uv.fs_stat(b.lock))
    protocol.release_context('serve/opaque:../context')
    assert(a.stopped and not uv.fs_stat(a.dir) and serve.next().type=='error')
    assert(not b.stopped and not c.stopped and not main.next() and not compact.next())
    c.delta('text','compact-only'); c.complete('end_turn')
    assert(compact.next().text=='compact-only' and compact.next().type=='done')
    assert(c.stopped and not uv.fs_stat(c.dir) and uv.fs_stat(b.lock) and not main.next())
    b.delta('text','child-only'); b.complete('end_turn')
    assert(main.next().text=='child-only' and main.next().type=='done')
    assert(not b.stopped and uv.fs_stat(b.lock))
    protocol.release_context(child)
    assert(b.stopped and not uv.fs_stat(b.dir) and not uv.fs_stat(b.lock))

    -- A fresh HTTP request has no live MCP call. Preserve its tool result
    -- in the transcript instead of dispatching it or dropping the answer.
    local transcript='[Tool result (id)]: saved result'
    protocol.open{context='stateless-request',purpose='turn',provider='test',model='fixture',tools={},
        history_transcript=transcript,messages={{role='assistant',blocks={{type='tool_use',id='id'}}},
        {role='user',blocks={{type='tool_result',tool_use_id='id',content={{type='text',text='saved result'}}}}}},notify=function() end}
    local found=false; for prompt in pairs(launched) do if prompt:find(transcript,1,true) then found=true end end
    assert(found,'cold tool result did not reach the new session')
end,debug.traceback)
if protocol then protocol.close() end
_G.dofile,package.loaded.panto=old_dofile,old_panto
uv.run('nowait')
assert(ok,err)
print('PASS: overlapping non-UI protocol contexts and cleanup')