summaryrefslogtreecommitdiff
path: root/check.lua
blob: eac0f55e6921495abd0f15a5a9bfc417baaee86d (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
-- macOS: run with `panto lua check.lua`. Use the same Panto binary for the TUI and fake agy.
local uv, json = require('luv'), require('panto').ext.json
local panto = assert(uv.fs_realpath(uv.exepath()))
local root = assert(uv.fs_realpath(arg[0])):match('^(.*)/')
local function write(path, data)
    local f = assert(io.open(path, 'wb')); assert(f:write(data)); assert(f:close())
end
local function read(path)
    local f = assert(io.open(path, 'rb')); local data = assert(f:read('a')); f:close(); return data
end
local function run(bin, args, opts, timeout)
    opts = opts or {}; opts.args = args; opts.stdio = opts.stdio or {0, 1, 2}
    local proc, timer, code, signal, expired
    proc = assert(uv.spawn(bin, opts, function(c, s)
        code, signal = c, s; proc:close(); if timer then timer:close() end
    end))
    if timeout then
        timer = assert(uv.new_timer()); timer:start(timeout, 0, function() expired = true; proc:kill('sigkill') end)
    end
    uv.run(); assert(not expired and signal == 0, bin .. ' timed out or was killed'); return code
end
local home = assert(uv.fs_mkdtemp('/private/tmp/panto-agy-XXXXXX'))
local function check()
    local acp_mod=dofile(root..'/acp.lua'); local usage=acp_mod.usage
    local function cut(text,room)
        local head,tail=acp_mod.history_split(text,room)
        assert(head..tail==text and #tail<=math.max(room,0) and utf8.len(head) and utf8.len(tail),'bad history split'); return head,tail
    end
    local entries={}; for i=1,40 do entries[i]=('[User]: line %02d '):format(i)..('\u{E9}'):rep(i % 7) end
    local head,tail=cut(table.concat(entries,'\n\n'),200)
    assert(head:sub(-2)=='\n\n' and tail:sub(1,7)=='[User]:' and #tail>100,'expected an entry-aligned cut')
    assert(#select(2,cut(('\u{754C}'):rep(100),10))==9) -- three-byte characters stay whole
    assert(cut('abc',10)=='' and cut('',10)=='' and cut('abc',0)=='abc' and select(2,cut('[a]\n\n[b]',3))=='[b]')
    local u={inputTokens=155,outputTokens=41,thoughtTokens=8,cachedReadTokens=55,cachedWriteTokens=10,totalTokens=196}
    local v=usage(u); assert(v.input==90 and v.output==41 and v.cache_read==55 and v.cache_write==10 and v.reasoning==8)
    u.totalTokens=261; assert(usage(u).input==155); u.totalTokens=99; assert(not usage(u) and not usage({}) and not usage(nil))
    local cat=dofile(root..'/catalog.lua'); local catalog=cat.catalog
    local wide='a'..('\u{754C}'):rep(8000); local pages=cat.pages(wide) -- no newline to cut at: characters stay whole
    assert(#pages==3 and table.concat(pages)==wide and #cat.pages('abc')==1)
    for i,p in ipairs(pages) do assert(utf8.len(p) and #cat.part(pages,7,i)<=cat.result_bytes) end
    local req={tools={{name='std.read',description='Read',input_schema_json='{"type":"object"}'},
        {name='fixture.echo',description='Echo',input_schema_json='{"type":"object"}'}}}
    local _,none=catalog(req,{}); assert(#none==0)
    local by,one=catalog(req,{'std.read'}); assert(#one==1 and by.std__read and not by.fixture__echo)
    local configure=dofile(root..'/extensions/antigravity-acp.lua').configure
    assert(not pcall(configure,{providers={antigravity={protocol='antigravity-acp',save_session=true}}}))
    local cfg, data = home .. '/.panto', home .. '/.local/share/panto'
    assert(run('/bin/mkdir', {'-p', cfg .. '/extensions', data}) == 0)
    assert(run('/bin/cp', {'-cR', assert(os.getenv('HOME')) .. '/.local/share/panto/rocks', data .. '/rocks'}) == 0)
    local source = 'paths=[' .. json.encode(root .. '/extensions') .. ']'
    if os.getenv('PANTO_CHECK_ROCK') == '1' then
        assert(run('luarocks', {'--lua-version','5.4','--tree',data .. '/rocks/lua-5.4.7','make','--deps-mode=none'}, {cwd=root}) == 0)
        source = 'rocks=["panto-antigravity"]'
    end
    write(cfg .. '/config.toml', '[extensions]\n' .. source .. [[

[providers.antigravity]
protocol="antigravity-acp"
]])
    write(cfg .. '/models.toml', '[antigravity.fast]\nmodel="gemini-test"\ninput=10.0\noutput=10.0\ncache_read=1.0\ncache_write=10.0\n')
    write(cfg .. '/SYSTEM.md', 'Short answers.')
    local filler={}; for i=1,1900 do filler[i]=('filler line %04d abcdefghij'):format(i) end
    write(cfg .. '/history-filler.txt', table.concat(filler,'\n'))
    write(home .. '/drop.png', '\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52\x00\x00\x00\x01\x00\x00\x00\x01\x08\x04\x00\x00\x00\xb5\x1c\x0c\x02\x00\x00\x00\x0b\x49\x44\x41\x54\x78\xda\x63\xfc\xff\x1f\x00\x03\x03\x02\x00\xef\x9a\x5f\x59\x00\x00\x00\x00\x49\x45\x4e\x44\xae\x42\x60\x82')
    write(home .. '/drop.pdf', '%PDF-1.7')
    local exe = home .. '/agy'
    assert(uv.fs_symlink(panto, home .. '/panto')) -- short, space-free shebang path
    write(exe, '#!' .. home .. '/panto lua\n' .. read(root .. '/tests/fake-agy.lua'))
    assert(uv.fs_chmod(exe, 448))
    write(cfg .. '/extensions/signature.lua', [[
local uv = require('luv'); local spawn = uv.spawn
uv.spawn = function(bin, opts, cb)
    if bin == '/usr/bin/codesign' and opts.args[4] == os.getenv('HOME') .. '/agy' then
        bin, opts = '/usr/bin/true', {args={}}
    end
    return spawn(bin, opts, cb)
end
return {name='test.signature', activate=function()
    local ext=require('panto').ext
    ext.on('render_thinking_complete',function(e)
        if e.text=='fixture-visible-thinking' then local f=assert(io.open(os.getenv('HOME')..'/thinking-rendered','w')); f:close() end
    end)
    ext.register_command{name='fixture-notice',description='Queue a workflow completion',handler=function()
        ext.agent:submit('[subagents] Workflow fixture completed. Output below: marker')
    end}
    ext.register_tool{name='fixture.echo',description='Return a nested value.',
        schema={type='object',properties={rows={type='array',minItems=1,items={type='object',properties={value={type='integer'}},required={'value'}}}},required={'rows'}},
        handler=function(input)
            local profile=os.getenv('HOME')..'/.local/share/panto-antigravity/google'
            if input.rows[1].value==43 then
                local f=assert(io.open(profile..'/tool-running','w')); f:close()
                local co=coroutine.running(); local timer=uv.new_timer()
                timer:start(10,10,function()
                    if uv.fs_stat(profile..'/return-result') then timer:close(); assert(coroutine.resume(co)) end
                end)
                coroutine.yield()
            elseif not uv.fs_stat(profile..'/usage-failure') then ext.agent:submit('boundary workflow notice') end
            return tostring(input.rows[1].value)
        end}
end}
]])
    local env = {'HOME=' .. home, 'QUOTA_LOG=' .. home .. '/screen', 'TMPDIR=' .. home,
        'TERM=xterm-256color', 'PATH=' .. home .. ':/usr/bin:/bin', 'PANTO=' .. panto}
    local profile = [[(version 1)(allow default)
(deny network-outbound (remote ip "*:*"))(deny network-bind (local ip "*:*"))
(deny process-exec)(allow process-exec (literal (param "PANTO")) (literal (param "FAKE"))
(literal "/usr/bin/true") (literal "/usr/bin/false") (literal "/usr/bin/nc")
(literal "/usr/bin/expect") (literal "/bin/stty"))]]
    local function sandbox(args, timeout, stdio)
        local cmd = {'-D', 'PANTO=' .. panto, '-D', 'FAKE=' .. exe, '-p', profile}
        for _, a in ipairs(args) do cmd[#cmd+1] = a end
        return run('/usr/bin/sandbox-exec', cmd, {cwd=home, env=env, stdio=stdio}, timeout)
    end
    local log = home .. '/headless.stderr'
    local fd = assert(uv.fs_open(log, 'w', 384))
    local code = sandbox({panto, '--incognito', '-p', '-m', 'antigravity:fast', '--', 'hello'}, 10000, {nil, 1, fd})
    uv.fs_close(fd)
    local google = home .. '/.local/share/panto-antigravity/google'
    local stderr = read(log)
    assert(code ~= 0 and stderr:find('ProviderProtocolFailed', 1, true) and not uv.fs_stat(google), stderr)
    local tui=sandbox({'/usr/bin/expect', root .. '/tests/tui.exp'}, 65000)
    if tui~=0 then error('TUI check failed: '..read(home..'/screen'):gsub('\27%[[0-9;?]*[A-Za-z]', ''):sub(-3500)) end
    assert(uv.fs_stat(home..'/thinking-rendered'),'ACP thought did not reach the native thinking component')
    local agents = assert(uv.fs_scandir(google .. '/config/agents')); assert(not uv.fs_scandir_next(agents), 'Generated agent left in private profile')
    assert(not uv.fs_stat(data .. '/state.toml') or not read(data .. '/state.toml'):find('antigravity', 1, true), 'state.toml polluted with session checkpoint')
    local screen = read(home .. '/screen'):gsub('\27%[[0-9;?]*[A-Za-z]', '')
    assert(screen:find('1.3k/1M ctx',1,true),'expected ACP context occupancy, not summed turn inputs')
    assert(screen:find('3.4k tok',1,true),'expected primary and steering usage counted exactly once')
    assert(screen:find('agy native: manage_task [completed]',1,true) and not screen:find('agy native: mcp_pantograph_',1,true),'native activity missing or MCP call duplicated')
    assert(screen:find('27%%%s+━'), 'expected 27% used and a filled bar in the rendered footer')
end
local ok, err = xpcall(check, debug.traceback)
assert(run('/bin/rm', {'-rf', home}) == 0)
assert(ok, err)
print('PASS: headless denial, native TUI, quota meter, model alias, primary MCP tool, permissions, native resume, /tree transcript seed, warm follow-up and cold resume')