From b4c1ad53077556e5401569a41a739f58a5ab2c86 Mon Sep 17 00:00:00 2001 From: t Date: Tue, 1 Dec 2026 17:42:15 -0700 Subject: Drain slow MCP tool results after ACP cancellation or disconnect agy can time out and cancel or disconnect an MCP request while Panto is still running the local tool. Failing immediately broke the turn or risked replying to a cancelled request. Request a 30-minute MCP timeout (timeoutSeconds=1800) for the pantograph relay and track Panto's active tool handoff (s.issued) independently of the cancellable MCP call. Keep the ACP idle timer alive while s.issued is pending, let notifications/cancelled and MCP disconnects clear s.call without aborting the turn, hold completion in s.settle until the local tool result and any queued steering drain, and record failed_handoff so a failed handoff surfaces on continuation. Document slow-tool handling in README.md and add a late-tool acceptance fixture in check.lua, tests/fake-agy.lua, and tests/tui.exp. --- extensions/antigravity-acp.lua | 93 ++++++++++++++++++++++++++---------------- 1 file changed, 57 insertions(+), 36 deletions(-) (limited to 'extensions/antigravity-acp.lua') diff --git a/extensions/antigravity-acp.lua b/extensions/antigravity-acp.lua index 8484bce..0f3faa9 100644 --- a/extensions/antigravity-acp.lua +++ b/extensions/antigravity-acp.lua @@ -9,7 +9,7 @@ local function companion(name) return require('panto-antigravity.'..name) end local catalog,acp=companion('catalog'),companion('acp') -local active +local active, failed_handoff local function stop() local s=active; if not s then return end; active=nil if s.card then s.card:unmount() end @@ -19,11 +19,17 @@ local function stop() if s.lock then uv.fs_rmdir(s.lock) end if s.notify then s.notify() end end -local function release(id) if active and active.sid==id then stop() end 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 +end local function send(c,msg) msg.jsonrpc='2.0'; c:write(json.encode(msg)..'\n') 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 - if q.method=='notifications/cancelled' and s.call and q.params and q.params.requestId==s.call.id then s.fail('MCP call cancelled'); return end + if q.method=='notifications/cancelled' then + if s.call and q.params and q.params.requestId==s.call.id then s.call=nil end + return + end if q.id==nil then return end assert(type(q.id)=='string' or math.type(q.id)=='integer','Invalid MCP request ID') local function fail(code,msg) send(c,{id=q.id,error={code=code,message=msg}}) end @@ -76,7 +82,12 @@ end local function start(req,by,list,signature,save) 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,notify=req.notify}; active=s - s.fail=function(msg) if active==s then s.error=tostring(msg):gsub('[%c]',' '):sub(1,512); stop() end end + s.fail=function(msg) + if active~=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() + end s.model,s.instructions,s.cwd,s.signature,s.save=req.model,req.system_prompt or '',assert(uv.cwd()),signature,save if save then assert(s.sid:match('^[%x-]+$'),'Invalid Panto session ID') @@ -89,7 +100,7 @@ local function start(req,by,list,signature,save) 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 - if e or not chunk then if s.call then s.fail('MCP disconnected during tool call') else s.peer=nil; s.listed=false; c:close() end; 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 local at=buf:find('\n',1,true); if (at or #buf)>1048576 then s.fail('Oversized MCP request'); return end @@ -150,7 +161,7 @@ end,activate=function() 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; stop() 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=release,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 @@ -163,9 +174,44 @@ end,activate=function() -- 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 - local awaiting=s and s.pending and s.call and s.call.sent - if not awaiting and (direct or warm) and last and last.role=='user' and #last.blocks==1 and b.type=='text' then - if s and s.pending then return terminal('An Antigravity turn is already pending') end + 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 + end + -- An emitted tool call belongs to Panto until its result returns, + -- independently of the (cancellable) MCP request that asked for it. + local awaiting=s and s.issued + if awaiting then + -- Panto may append one or more user messages after the single tool + -- result. Never search past other roles/results for a reusable tag. + local i=#msgs; local steer=json.array({}) + while i>0 and msgs[i].role=='user' and msgs[i].blocks[1] and msgs[i].blocks[1].type=='text' do i=i-1 end + local carrier=msgs[i]; b=carrier and carrier.blocks[1] + if not (carrier and carrier.role=='user' and #carrier.blocks==1 and b.type=='tool_result' and b.tool_use_id==awaiting.tag) then + s.fail('Missing matching tool result at ACP continuation'); return terminal(s.error) + end + for j=i+1,#msgs do for _,v in ipairs(msgs[j].blocks) do + if v.type~='text' then s.fail('Only text can steer an active ACP turn'); return terminal(s.error) end + steer[#steer+1]={type='text',text=v.text} + end end + s.prefix=history(msgs,#msgs); s.response={} + if #steer>0 then s.steer(steer) end + if s.call==awaiting then + local parts=json.array({}); local failed=b.is_error + for _,v in ipairs(b.content) do + if v.type=='text' then parts[#parts+1]={type='text',text=v.text} + elseif v.type=='media' and v.media_type:match('^image/') then parts[#parts+1]={type='image',mimeType=v.media_type,data=v.data} + else failed=true; parts[#parts+1]={type='text',text='Unsupported tool result content'} end + end + send(s.peer,{id=awaiting.id,result={isError=failed,content=parts}}); s.call=nil + end + s.issued=nil; s.settle() + elseif s and s.pending then + s.fail('Unexpected Panto continuation'); return terminal(s.error) + else + if not (last and last.role=='user' and #last.blocks==1 and b.type=='text') then return terminal('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 sig=('%016x:skip'):format(digest(json.encode({'panto-agent-acp-v2',uv.cwd(),req.provider,req.model,req.system_prompt or '',list}))) local prompt=b.text @@ -213,36 +259,12 @@ end,activate=function() end s.turn=(s.turn or 0)+1 local ok,err=pcall(function() if s.submit then s.submit(prompt) else acp.launch(s,prompt,tag and tag.session) end end); if not ok then s.fail(err) end - elseif awaiting then - -- Panto may append one or more user messages after the single tool - -- result. Never search past other roles/results for a reusable tag. - local i=#msgs; local steer=json.array({}) - while i>0 and msgs[i].role=='user' and msgs[i].blocks[1] and msgs[i].blocks[1].type=='text' do i=i-1 end - local carrier=msgs[i]; b=carrier and carrier.blocks[1] - if not (carrier and carrier.role=='user' and #carrier.blocks==1 and b.type=='tool_result' and b.tool_use_id==s.call.tag) then - s.fail('Missing matching tool result at ACP continuation'); return terminal(s.error) - end - for j=i+1,#msgs do for _,v in ipairs(msgs[j].blocks) do - if v.type~='text' then s.fail('Only text can steer an active ACP turn'); return terminal(s.error) end - steer[#steer+1]={type='text',text=v.text} - end end - s.prefix=history(msgs,#msgs); s.response={} - local parts=json.array({}); local failed=b.is_error - for _,v in ipairs(b.content) do - if v.type=='text' then parts[#parts+1]={type='text',text=v.text} - elseif v.type=='media' and v.media_type:match('^image/') then parts[#parts+1]={type='image',mimeType=v.media_type,data=v.data} - else failed=true; parts[#parts+1]={type='text',text='Unsupported tool result content'} end - end - if #steer>0 then s.steer(steer) end - send(s.peer,{id=s.call.id,result={isError=failed,content=parts}}); s.call=nil - else - if s and s.pending then s.fail('Unexpected Panto continuation') end - return terminal('Antigravity requires direct TUI input on a provenance-aware Panto build. No subscription request sent.') end s.notify=req.notify return {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 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 local chunk=table.remove(s.chunks,1); s.bytes=s.bytes-#chunk.text; append_reply(s.response,chunk.kind,chunk.text) return {type=chunk.kind..'_delta',text=chunk.text} @@ -256,8 +278,7 @@ end,activate=function() local call=s.call; if not call then return nil end -- A single ACP prompt can span many Panto messages. Publish its -- latest occupancy at tool boundaries too, without billing a turn. - if call.sent then return {type='done',termination={reason='tool_use'},metadata=s.context_window and {context_window=s.context_window}} end - call.sent=true; return {type='tool_call',id=call.tag,name=call.name,input_json=call.input} + s.issued=call; return {type='tool_call',id=call.tag,name=call.name,input_json=call.input} end} end} end} -- cgit v1.3