diff options
| author | t <t@tjp.lol> | 2026-12-01 16:31:43 -0700 |
|---|---|---|
| committer | t <t@tjp.lol> | 2026-12-04 13:53:47 -0700 |
| commit | 8c1c5786ebfe8dda40a70571f332e876b184ad4d (patch) | |
| tree | 1116068ae630c45c1218b17fabb3d253bb8003d9 /extensions/antigravity-acp.lua | |
| parent | f7a1e30c1dfbe5fddcbfe38e89a0c5efef9fbeab (diff) | |
Isolate ACP via custom agent and add steering, usage, reseed, and footer updates
Replace the osascript permission dialog and settings.json permission seed with
a per-thread generated custom main agent (excludeDefaultComponents, tools: [],
no inherited customizations or MCP) launched with --agent,
--include-default-customizations=false, and --dangerously-skip-permissions.
Keep HOME unchanged (isolating via --gemini-dir only) so macOS finds the login
Keychain, mark exported tools with antigravity/alwaysLoad, and require the
relay to serve tools/list before the first prompt.
Add ACP turn steering for queued user and workflow text at tool boundaries,
preserve agent_thought_chunk thinking blocks with block-aware checkpoint
hashing, report live usage_update context snapshots and deduplicated final plus
steering token usage, and mount a live above-input native tool activity
component for up to six non-MCP ACP calls.
Support reseeding a new ACP session from history_transcript after /tree or cold
resume, allow direct submissions to take over an idle session from another
thread, and let extension follow-ups continue a matching warm session. Update
the usage footer with startup and three-minute timer refreshes, 1w/5h labels,
per-letter antigravity colors, and expired-bucket handling.
Diffstat (limited to 'extensions/antigravity-acp.lua')
| -rw-r--r-- | extensions/antigravity-acp.lua | 123 |
1 files changed, 105 insertions, 18 deletions
diff --git a/extensions/antigravity-acp.lua b/extensions/antigravity-acp.lua index 98ab958..8484bce 100644 --- a/extensions/antigravity-acp.lua +++ b/extensions/antigravity-acp.lua @@ -12,6 +12,7 @@ local catalog,acp=companion('catalog'),companion('acp') local active local function stop() local s=active; if not s then return end; active=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 s.server:close(); uv.fs_unlink(s.dir..'/mcp'); uv.fs_rmdir(s.dir) @@ -41,6 +42,37 @@ local function rpc(s,c,q) else return fail(-32601,'Unsupported method') end send(c,{id=q.id,result=result}) end +-- ACP tool notifications are observations, NOT Panto tool invocations. Keep +-- a small live component for the current/last prompt; never enter tool dispatch. +local function native_activity(s) + local rows={} + local statuses={pending='pending',in_progress='running',completed='completed',failed='failed'} + return function(u) + local id=u.toolCallId + if type(id)~='string' or #id>256 then return end + local row; for _,v in ipairs(rows) do if v.id==id then row=v; break end end + if not row then + local name=u.name or (type(u.title)=='string' and u.title:match('^Run ([%w_.-]+)$')) + if type(name)~='string' or #name>96 or not name:match('^[%w_.-]+$') then return end + if s.by[name:match('^mcp_pantograph_(.+)$') or false] then return end + row={id=id,name=name,status='reported'}; rows[#rows+1]=row + if #rows>6 then table.remove(rows,1) end + end + local input=type(u.rawInput)=='table' and u.rawInput or {} + if row.name=='manage_task' and type(input.Action)=='string' and ({list=true,kill=true,status=true,send_input=true})[input.Action] then + row.action=input.Action + end + row.status=statuses[u.status] or row.status + if not s.card then + s.card=ext.mount({render=function(_,width) + local lines={}; for i,v in ipairs(rows) do + lines[i]=('agy native: '..v.name..(v.action and ' ('..v.action..')' or '')..' ['..v.status..']'):sub(1,width) + end; return lines + end},'above_input') + end + s.card:invalidate() + end +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 @@ -57,7 +89,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; c:close() end; 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 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 @@ -72,6 +104,20 @@ end local function digest(text,h) h=h or 0xcbf29ce484222325; for i=1,#text do h=(h ~ text:byte(i))*0x100000001b3 end; return h end +-- Preserve legacy all-text checkpoints. For mixed replies, hash the ordered +-- typed runs, independent of how ACP and the TUI split their chunks. +local function append_reply(r,kind,text) + if text=='' then return end + if r.kind~=kind then + if r.kind then r.prefix=digest(('%s:%016x'):format(r.kind,r.tail),r.prefix) end + r.kind,r.tail=kind,nil + end + r.tail=digest(text,r.tail) +end +local function reply_key(r) + if not r.prefix and r.kind~='thinking' then return ('%016x'):format(r.tail or digest('')) end + return ('blocks:%016x'):format(digest(('%s:%016x'):format(r.kind,r.tail),r.prefix)) +end local function history(msgs,n) local h; for i=1,n do h=digest(json.encode({msgs[i].role,msgs[i].blocks}),h) end; return ('%016x'):format(h or 0) end @@ -109,18 +155,36 @@ end,activate=function() 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 return terminal('Another thread owns this session') end - if req.tui_submission==true and last and last.role=='user' and #last.blocks==1 and b.type=='text' then + if s and s.sid~=req.context then + if s.pending 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 + 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 local cfg=ext.config.providers[req.provider]; validate(cfg); local by,list=catalog.catalog(req,cfg.tools) - local sig=('%016x'):format(digest(json.encode({'mcp-only-v1',uv.cwd(),req.provider,req.model,req.system_prompt or '',list}))) + 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 local prior=msgs[#msgs-1]; local tag=prior and prior.metadata and prior.metadata.antigravity_acp local key='antigravity_acp.checkpoint.'..req.context if prior or s then - local reply=''; for _,v in ipairs(prior and prior.blocks or {}) do if v.type=='text' then reply=reply..v.text else reply=nil; break end 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==('%016x'):format(digest(reply)) and + local reply={}; for _,v in ipairs(prior and prior.blocks or {}) do + if v.type~='text' and v.type~='thinking' then reply=nil; break end + append_reply(reply,v.type,v.text) + 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 cfg.save_session==true and tag.session and ext.state.get(key)==tag.turn))) then - stop(); return terminal('No matching Antigravity history checkpoint. Start a new Panto thread; nothing was replayed.') + if not direct then return terminal('Extension follow-up requires a matching live Antigravity checkpoint') end + stop(); 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 + prompt='Selected Panto conversation history (background only; do not repeat past actions):\n\n'..transcript..'\n\nCurrent user request:\n'..b.text + end end end if not s then @@ -130,28 +194,46 @@ end,activate=function() 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 s.effort=req.effort if s.save then local ok,err=ext.state.set(key,nil); if not ok then stop(); return terminal(tostring(err)) end end - s.pending=true; s.notify=req.notify; s.buffer=''; s.response=digest(''); s.prefix=history(msgs,#msgs) - s.delta=function(text) - assert(type(text)=='string' and #s.buffer+#text<=4<<20,'Invalid ACP text'); s.buffer=s.buffer..text; s.notify() + s.pending=true; s.notify=req.notify; s.chunks={}; s.bytes=0; s.response={}; s.prefix=history(msgs,#msgs) + s.delta=function(kind,text) + assert(type(text)=='string' and s.bytes+#text<=4<<20,'Invalid ACP text'); if text=='' then return end + local tail=s.chunks[#s.chunks]; if tail and tail.kind==kind then tail.text=tail.text..text + else s.chunks[#s.chunks+1]={kind=kind,text=text} end + s.bytes=s.bytes+#text; s.notify() end - -- Only an authenticated direct turn is allowed to request a quota read. s.offered=function(models) offers=models end s.refresh=refresh and cfg.usage_footer~=false and refresh or nil - s.complete=function(reason) + 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() end s.turn=(s.turn or 0)+1 - local ok,err=pcall(function() if s.submit then s.submit(b.text) else acp.launch(s,b.text,tag and tag.session) end end); if not ok then s.fail(err) end - elseif s and s.pending and s.call and s.call.sent and last and last.role=='user' and #last.blocks==1 and b.type=='tool_result' and b.tool_use_id==s.call.tag then - s.prefix=history(msgs,#msgs); s.response=digest('') + 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 @@ -161,15 +243,20 @@ end,activate=function() 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.buffer~='' then local text=s.buffer; s.buffer=''; s.response=digest(text,s.response); return {type='text_delta',text=text} 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} + end if s.finished then local reason=s.finished; s.finished=nil; s.pending=false if s.refresh then pcall(s.refresh) end if s.save then assert(ext.state.set(s.key,s.last)) end - return {type='done',termination={reason=reason},metadata={antigravity_acp={thread=s.sid,session=s.save and s.session or nil,turn=s.last,signature=s.signature,history=s.prefix,response=('%016x'):format(s.response)}}} + return {type='done',usage=s.usage,termination={reason=reason},metadata={context_window=s.context_window,antigravity_acp={thread=s.sid,session=s.save and s.session or nil,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 - if call.sent then return {type='done',termination={reason='tool_use'}} 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} end} end} |
