summaryrefslogtreecommitdiff
path: root/extensions
diff options
context:
space:
mode:
Diffstat (limited to 'extensions')
-rw-r--r--extensions/antigravity-acp.lua132
1 files changed, 76 insertions, 56 deletions
diff --git a/extensions/antigravity-acp.lua b/extensions/antigravity-acp.lua
index 1809a19..a8d2229 100644
--- a/extensions/antigravity-acp.lua
+++ b/extensions/antigravity-acp.lua
@@ -9,9 +9,9 @@ local function companion(name)
return require('panto-antigravity.'..name)
end
local catalog,acp=companion('catalog'),companion('acp')
-local active, failed_handoff
-local function stop()
- local s=active; if not s then return end; active=nil
+local sessions, compactions, failed_handoffs = {}, {}, {}
+local function stop(s)
+ if not s or s.pool[s.sid]~=s then return end; s.pool[s.sid]=nil
if s.card then s.card:unmount() end
if s.shutdown then s.shutdown() end
if s.peer and not s.peer:is_closing() then s.peer:close() end
@@ -21,12 +21,16 @@ local function stop()
end
local function send(c,msg) msg.jsonrpc='2.0'; c:write(json.encode(msg)..'\n') end
local function release(id)
- if failed_handoff and failed_handoff.sid==id then failed_handoff=nil end
- if active and active.sid==id then stop() end
+ failed_handoffs[id]=nil
+ stop(sessions[id]); stop(compactions[id])
+end
+local function close_all()
+ for _,pool in ipairs({sessions,compactions}) do for _,s in pairs(pool) do stop(s) end end
+ failed_handoffs={}
end
local function cancel_turn(id)
- if failed_handoff and failed_handoff.sid==id then failed_handoff=nil end
- local s=active; if not s or s.sid~=id or not (s.pending or s.cancelling) then return end
+ failed_handoffs[id]=nil
+ local s=sessions[id]; if not s or not (s.pending or s.cancelling) then return end
if s.card then s.card:unmount(); s.card=nil end
if s.call and s.peer and not s.peer:is_closing() then
pcall(send,s.peer,{id=s.call.id,error={code=-32800,message='Turn cancelled'}})
@@ -36,7 +40,7 @@ local function cancel_turn(id)
s.turn=(s.turn or 0)+1; s.last,s.cancelled_prefix=nil,s.turn_prefix
local ok,kept=false,false
if s.cancel then ok,kept=pcall(s.cancel) end
- if not (ok and kept) then stop() end
+ if not (ok and kept) then stop(s) end
end
local function rpc(s,c,q)
if type(q)~='table' or q.jsonrpc~='2.0' or type(q.method)~='string' then error('Invalid MCP request') end
@@ -67,7 +71,7 @@ local function rpc(s,c,q)
else
if not tool then return fail(-32602,'Tool unavailable') end
local ok,why=catalog.arguments(tool.schema,v); if not ok then return fail(-32602,why or 'Tool arguments rejected') end
- if not s.prompting or s.cancelling then return fail(-32000,'No human turn pending') end
+ if not s.prompting or s.cancelling then return fail(-32000,'No active turn pending') end
if s.call then return fail(-32000,'Another Panto tool call is pending') end
s.seq=s.seq+1; s.call={id=q.id,tag=s.dir:match('[^/]+$')..'-'..s.seq,name=tool.name,input=json.encode(v)}; s.notify(); return
end
@@ -116,34 +120,40 @@ local function native_activity(s)
s.card:invalidate()
end
end
-local function start(req,by,list,signature)
+local function start(pool,req,by,list,signature)
local dir=assert(uv.fs_mkdtemp('/tmp/panto-antigravity-XXXXXX'))
- local s={sid=req.context,dir=dir,server=uv.new_pipe(false),by=by,list=list,seq=0,results={},notify=req.notify}; active=s
+ local s={pool=pool,sid=req.context,dir=dir,server=uv.new_pipe(false),by=by,list=list,seq=0,results={},notify=req.notify}; pool[s.sid]=s
s.fail=function(msg)
- if active~=s then return end
+ if pool[s.sid]~=s then return end
s.error=tostring(msg):gsub('[%c]',' '):sub(1,512)
- if s.issued then failed_handoff={sid=s.sid,error=s.error} end
- stop()
+ if s.issued then failed_handoffs[s.sid]={tag=s.issued.tag,error=s.error} end
+ stop(s)
end
s.model,s.instructions,s.cwd,s.signature=req.model,req.system_prompt or '',assert(uv.cwd()),signature
- assert(s.sid:match('^[%x-]+$'),'Invalid Panto session ID')
- local lock='/tmp/panto-antigravity-lock-'..uv.getuid()..'-'..s.sid
- assert(uv.fs_mkdir(lock,448),'Saved thread locked: '..lock); s.lock=lock
+ -- Durable threads keep their cross-process lock and agent name. Other
+ -- host contexts (including serve's process-local IDs) get private names;
+ -- never interpolate an opaque context into a path or agent definition.
+ local durable=pool==sessions and #s.sid<=64 and s.sid:match('^[%x-]+$')
+ s.agent_key=durable and s.sid:lower() or dir:sub(-6):gsub('.',function(c) return ('%02x'):format(c:byte()) end)
+ if durable then
+ local lock='/tmp/panto-antigravity-lock-'..uv.getuid()..'-'..s.agent_key
+ assert(uv.fs_mkdir(lock,448),'Saved thread locked: '..lock); s.lock=lock
+ end
assert(s.server:bind(dir..'/mcp')); assert(s.server:listen(1,function(err)
- if err or active~=s then return end
+ if err or pool[s.sid]~=s then return end
local c=uv.new_pipe(false); if not s.server:accept(c) or s.peer then c:close(); return end
- s.peer=c; local buf=''; c:read_start(function(e,chunk)
- if active~=s or s.peer~=c or c:is_closing() then return end
+ s.peer=c; c:unref(); local buf=''; c:read_start(function(e,chunk)
+ if pool[s.sid]~=s or s.peer~=c or c:is_closing() then return end
if e or not chunk then s.call=nil; s.peer=nil; s.listed=false; c:close(); return end
buf=buf..chunk
- while active==s do
+ while pool[s.sid]==s do
local at=buf:find('\n',1,true); if (at or #buf)>1048576 then s.fail('Oversized MCP request'); return end
if not at then return end
local line=buf:sub(1,at-1); buf=buf:sub(at+1)
if not pcall(function() rpc(s,c,json.decode(line)) end) then s.fail('Malformed MCP request'); return end
end
end)
- end))
+ end)); s.server:unref() -- idle relays must not keep print mode running
return s
end
local function digest(text,h)
@@ -194,39 +204,41 @@ end
return {name='protocol.antigravity-acp',configure=function(config)
for _,cfg in pairs(config.providers or {}) do if cfg.protocol=='antigravity-acp' then validate(cfg) end end
end,activate=function()
- local ready,refresh,offers=false,nil,nil
+ local tui_id,refresh
+ local function current() return tui_id and sessions[tui_id] end
for _,cfg in pairs(ext.config.providers or {}) do
if cfg.protocol=='antigravity-acp' and cfg.usage_footer~=false then
refresh=companion('quota').activate(ext,companion('_usage_footer'),acp); break
end
end
ext.register_command{name='antigravity-status',description='Show local Antigravity connection status (no account request).',handler=function()
+ local active=current()
if not active or not active.session then return 'Antigravity: no connected ACP session. Submit text to start; sign-in is managed by the Google CLI.' end
local model=(active.wire or active.model)..(active.thought and ' ('..active.thought..')' or '')
return ('Antigravity: %s · %s · %d Panto tools · MCP %s · %d calls'):format(model,active.pending and 'turn pending' or active.cancelling and 'cancelling' or 'ready',#active.list,active.listed and 'listed' or 'not listed',active.seq)
end}
ext.register_command{name='antigravity-models',description='List Gemini families and reasoning levels from the last ACP setup (no account request).',handler=function()
+ local offers=(current() or {}).offers
if not offers then return 'No model list yet. Submit a turn to let the Google CLI advertise its available models.' end
return #offers>0 and ('Gemini models: '..table.concat(offers,'; ')) or 'No displayable Gemini models were advertised.'
end}
- ext.on('session_ready',function() ready=true end)
- ext.on('session_end',function() ready=false; offers=nil; failed_handoff=nil; stop() end)
- ext.register_protocol{name='antigravity-acp',effort_levels=acp.effort_levels,close=stop,cancel_turn=cancel_turn,reset_context=release,release_context=release,open=function(req)
- local info=ext.session_info()
- if not ready or not info or req.purpose~='turn' or info.session_id~=req.context then return terminal('Interactive primary thread required') end
- local msgs=req.messages; local last=msgs[#msgs]; local b=last and last.blocks[1]; local s=active
- if s and s.sid~=req.context then
- if s.pending or s.cancelling or not req.tui_submission then return terminal('Another thread owns this session') end
- stop(); s=nil
- end
- local direct=req.tui_submission==true
- -- Panto extensions submit workflow completions as ordinary user text.
- -- They may continue a matching live turn, never open or reseed one.
- local warm=s and not s.pending and s.last~=nil
- if failed_handoff then
- local failure=failed_handoff
- if direct or failure.sid==req.context then failed_handoff=nil end
- if not direct and failure.sid==req.context then return terminal(failure.error) end
+ ext.on('session_ready',function(e) tui_id=e.has_tui and ext.session_info().session_id or nil end)
+ ext.on('session_end',function() tui_id=nil; close_all() end)
+ ext.register_protocol{name='antigravity-acp',effort_levels=acp.effort_levels,close=close_all,cancel_turn=cancel_turn,reset_context=release,release_context=release,open=function(req)
+ local compact=req.purpose=='compaction'
+ local pool=compact and compactions or sessions
+ local s=pool[req.context]; local msgs=req.messages; local last=msgs[#msgs]; local b
+ -- If ACP died after emitting a tool call, surface its failure when
+ -- that result comes back (possibly followed by steering), not on the
+ -- next unrelated submission in this or another context.
+ local failure=not compact and failed_handoffs[req.context]
+ if failure then
+ failed_handoffs[req.context]=nil
+ for i=#msgs,1,-1 do
+ if msgs[i].role~='user' then break end
+ local block=msgs[i].blocks[1]
+ if block and block.type=='tool_result' and block.tool_use_id==failure.tag then return terminal(failure.error) end
+ end
end
-- An emitted tool call belongs to Panto until its result returns,
-- independently of the (cancellable) MCP request that asked for it.
@@ -260,12 +272,18 @@ end,activate=function()
end
s.issued=nil; s.settle()
elseif s and s.pending then
- s.fail('Unexpected Panto continuation'); return terminal(s.error)
+ return terminal('Antigravity already has a pending turn in this context')
else
local parts,bytes=prompt_parts(last)
+ -- A stateless client can return tool results in a *new* context.
+ -- Panto includes that carrier in history_transcript; use it as
+ -- a record, never as a reply to a live MCP request.
+ if not s and not parts and last and last.role=='user' and msgs[#msgs-1] and last.blocks[1] and last.blocks[1].type=='tool_result' then
+ local lead='Continue from the tool results in the selected conversation history.'
+ parts,bytes=json.array({{type='text',text=lead}}),#lead
+ end
if not parts then return terminal(bytes or 'No matching Antigravity tool handoff or text prompt') end
- if not (direct or warm) then return terminal('Antigravity requires direct TUI input on a provenance-aware Panto build. No subscription request sent.') end
- local cfg=ext.config.providers[req.provider]; validate(cfg); local by,list=catalog.catalog(req,cfg.tools)
+ local cfg=ext.config.providers[req.provider]; validate(cfg); local by,list=catalog.catalog(req,compact and {} or cfg.tools)
local sig=('%016x:skip'):format(digest(json.encode({'panto-agent-acp-v2',uv.cwd(),req.provider,req.model,req.system_prompt or '',list})))
-- agy silently drops everything past ~191 KB of one user message's text.
if bytes>acp.message_bytes-1024 then return terminal(('This message is %d bytes; Antigravity keeps only the first ~191 KB of one message. No subscription request sent.'):format(bytes)) end
@@ -280,8 +298,7 @@ end,activate=function()
end
if not (tag and tag.thread==req.context and tag.signature==sig and tag.history==history(msgs,#msgs-2) and reply and tag.response==reply_key(reply) and
((s and s.last==tag.turn) or (not s and tag.session and acp.checkpoint(tag.session)==tag.turn))) then
- if not direct then return terminal('Extension follow-up requires a matching live Antigravity checkpoint') end
- stop(); s=nil; tag=nil
+ stop(s); s=nil; tag=nil
if prior then
local transcript=req.history_transcript
if type(transcript)~='string' or transcript=='' then return terminal('Cannot continue: Panto did not supply the selected history transcript.') end
@@ -301,18 +318,19 @@ end,activate=function()
end
end
if not s then
- local ok,value=pcall(start,req,by,list,sig); if not ok then stop(); return terminal(tostring(value)) end; s=value
- if tag and acp.checkpoint(tag.session)~=tag.turn then stop(); return terminal('Saved Antigravity checkpoint changed') end
+ local ok,value=pcall(start,pool,req,by,list,sig); if not ok then stop(pool[req.context]); return terminal(tostring(value)) end; s=value
+ if tag and acp.checkpoint(tag.session)~=tag.turn then stop(s); return terminal('Saved Antigravity checkpoint changed') end
end
if s.resolve then
local wire,why=s.resolve(req.effort); if not wire then return terminal(why) end
end
if s.card then s.card:unmount(); s.card=nil end
- s.observe=native_activity(s); s.context_window=nil; s.usage=nil
+ local visible=not compact and tui_id==s.sid
+ s.observe=visible and native_activity(s) or nil; s.context_window=nil; s.usage=nil
s.effort=req.effort
local active_session=s.session or (tag and tag.session)
- if active_session then
- local ok,err=pcall(acp.checkpoint,active_session,nil); if not ok then stop(); return terminal(tostring(err)) end
+ if active_session and s.lock then
+ local ok,err=pcall(acp.checkpoint,active_session,nil); if not ok then stop(s); return terminal(tostring(err)) end
end
s.cancelled_prefix=nil; s.turn_prefix=history(msgs,#msgs)
s.pending=true; s.notify=req.notify; s.chunks={}; s.bytes=0; s.response={}; s.prefix=s.turn_prefix
@@ -322,8 +340,8 @@ end,activate=function()
else s.chunks[#s.chunks+1]={kind=kind,text=text} end
s.bytes=s.bytes+#text; s.notify()
end
- s.offered=function(models) offers=models end
- s.refresh=refresh and cfg.usage_footer~=false and refresh or nil
+ s.offered=function(models) s.offers=models end
+ s.refresh=visible and cfg.usage_footer~=false and refresh or nil
s.complete=function(reason,usage)
s.usage=usage
assert(not s.call,'Prompt ended with an outstanding MCP call'); s.last=s.dir..':'..tostring(s.turn); s.finished=reason; s.notify()
@@ -339,9 +357,10 @@ end,activate=function()
end); if not ok then s.fail(err) end
end
s.notify=req.notify; local turn_id=s.turn
- return {next=function()
+ -- Compaction doesn't receive the registration's turn-cancel callback.
+ return {cancel_turn=compact and function() stop(s) end or nil,next=function()
if s.error then return {type='error',kind='terminal',message=s.error} end
- if active~=s then return {type='error',kind='terminal',message='Antigravity session closed'} end
+ if pool[s.sid]~=s then return {type='error',kind='terminal',message='Antigravity session closed'} end
if turn_id~=s.turn then return nil end
if s.issued then return {type='done',termination={reason='tool_use'},metadata=s.context_window and {context_window=s.context_window}} end
if s.bytes>0 then
@@ -351,7 +370,8 @@ end,activate=function()
if s.finished then
local reason=s.finished; s.finished=nil; s.pending=false
if s.refresh then pcall(s.refresh) end
- acp.checkpoint(s.session,s.last)
+ if compact then stop(s); return {type='done',usage=s.usage,termination={reason=reason}} end
+ if s.lock then acp.checkpoint(s.session,s.last) end
return {type='done',usage=s.usage,termination={reason=reason},metadata={context_window=s.context_window,antigravity_acp={thread=s.sid,session=s.session,turn=s.last,signature=s.signature,history=s.prefix,response=reply_key(s.response)}}}
end
local call=s.call; if not call then return nil end