summaryrefslogtreecommitdiff
path: root/acp.lua
diff options
context:
space:
mode:
Diffstat (limited to 'acp.lua')
-rw-r--r--acp.lua72
1 files changed, 61 insertions, 11 deletions
diff --git a/acp.lua b/acp.lua
index 9e648b9..f401a70 100644
--- a/acp.lua
+++ b/acp.lua
@@ -104,6 +104,35 @@ local function verified(run, on_error)
return cancel
end
M.probe = verified
+local function checkpoint_path(id)
+ assert(type(id)=='string' and #id>0 and #id<=128 and id:match('^[%w_-]+$'),'Invalid ACP session ID')
+ local dir=private_profile()..'/checkpoints'
+ local made,err,code=uv.fs_mkdir(dir,448); assert(made or code=='EEXIST',err)
+ local st=assert(uv.fs_lstat(dir)); assert(st.type=='directory' and st.uid==uv.getuid() and st.mode & 63==0,'Unsafe checkpoint directory')
+ return dir..'/'..id
+end
+function M.checkpoint(id, ...)
+ if select('#',...)==0 then
+ local ok,data=pcall(function()
+ local path=checkpoint_path(id)
+ local st=uv.fs_lstat(path); if not st or st.type~='file' or st.uid~=uv.getuid() then return nil end
+ local fd=uv.fs_open(path,'r',384); if not fd then return nil end
+ local v=uv.fs_read(fd,512,0); uv.fs_close(fd); return v
+ end)
+ return ok and data or nil
+ end
+ local value=(...)
+ local path=checkpoint_path(id)
+ if value==nil then
+ local ok,err,code=uv.fs_unlink(path); assert(ok or code=='ENOENT',err); return
+ end
+ local tmp=path..'.tmp.'..tostring(uv.hrtime())
+ local fd=assert(uv.fs_open(tmp,'wx',384))
+ local n,err=uv.fs_write(fd,value,0); assert(uv.fs_close(fd))
+ if n~=#value then uv.fs_unlink(tmp); error(err or 'Incomplete checkpoint write') end
+ local ok,rerr=uv.fs_rename(tmp,path)
+ if not ok then uv.fs_unlink(tmp); error(rerr) end
+end
-- agy discovers named agents under <gemini-dir>/config/agents. Declare the
-- relay on the agent itself: session/new MCP servers need not be inherited by
-- custom agents. Never edit the user's agents or global MCP configuration.
@@ -115,8 +144,8 @@ local function install_agent(s, profile)
local made,err,code=uv.fs_mkdir(dir,448); assert(made or code=='EEXIST',err)
local st=assert(uv.fs_lstat(dir)); assert(st.type=='directory' and st.uid==uv.getuid() and st.mode & 18==0,'Unsafe agy agent directory')
end
- -- Saved sessions already hold an exclusive lock on this thread ID.
- local name='panto-'..(s.save and s.sid or s.dir:match('([^/]+)$')):lower()
+ -- Active threads hold an exclusive lock on this Panto session ID.
+ local name='panto-'..s.sid:lower()
local servers=json.array({})
if #s.list>0 then servers[1]=relay(s) end
local data=([[---
@@ -161,12 +190,12 @@ function M.launch(s, first, resume_id)
end
s.shutdown=function()
if closed then return end; closed=true
- local cancel=child and s.pending and s.session
- if cancel then pcall(send,{method='session/cancel',params={sessionId=s.session}}) end
+ local active_turn=child and (s.pending or s.cancelling) and s.session
+ if active_turn and not s.cancelling then pcall(send,{method='session/cancel',params={sessionId=s.session}}) end
if cancel_check then cancel_check() end
if s.agent_path then uv.fs_unlink(s.agent_path); s.agent_path=nil end
close(stdout)
- if cancel then timer:stop(); timer:start(500,0,cleanup) else cleanup() end
+ if active_turn then timer:stop(); timer:start(500,0,cleanup) else cleanup() end
end
local function step(co,...)
local ok,err=coroutine.resume(co,...); if not ok then s.fail(tostring(err)) end
@@ -178,15 +207,29 @@ function M.launch(s, first, resume_id)
serial=serial+1; steering[serial]=true
send({id=serial,method='session/prompt',params={sessionId=s.session,prompt=parts}})
end
- local function activity() if waiting then deadline=uv.now()+idle end end
+ local function activity() if waiting and not s.cancelling then deadline=uv.now()+idle end end
local function call(method,params)
assert(not waiting,'Overlapping ACP requests'); serial=serial+1; request_id=serial; waiting=coroutine.running()
idle=(method=='session/prompt' or method=='authenticate') and 600000 or 60000
activity(); timer:start(1000,1000,function()
- if s.call or s.issued then activity() elseif uv.now()>=deadline then s.fail('ACP timeout: '..method) end
+ if not s.cancelling and (s.call or s.issued) then activity() elseif uv.now()>=deadline then s.fail('ACP timeout: '..method) end
end)
send({id=serial,method=method,params=params}); return coroutine.yield()
end
+ s.cancel=function()
+ if closed or not s.prompting or not s.session then return false end
+ if not s.cancelling then
+ s.cancelling=true
+ if not primary_reply or next(steering) then
+ send({method='session/cancel',params={sessionId=s.session}})
+ deadline=uv.now()+3000; timer:stop(); timer:start(100,100,function()
+ if uv.now()>=deadline then s.fail('ACP timeout: session/cancel') end
+ end)
+ end
+ end
+ s.settle()
+ return true
+ end
local selectors, opts, configuring = {}, {}, false
local function advertised()
local model=config(opts,s.selector)
@@ -228,6 +271,13 @@ function M.launch(s, first, resume_id)
s.prompting=true
if s.refresh then pcall(s.refresh) end
local r=call('session/prompt',{sessionId=s.session,prompt={{type='text',text=text}}})
+ if s.cancelling then
+ assert(r.stopReason=='cancelled' or r.stopReason=='end_turn' or r.stopReason=='max_tokens',
+ 'Turn stopped: '..tostring(r.stopReason))
+ s.prompting,s.cancelling=false,false; steer_usage,last_steer=nil,nil
+ if s.cancelled then s.cancelled() end
+ return
+ end
local reason=({end_turn='end_turn',max_tokens='max_tokens'})[r.stopReason]
assert(reason,'Turn stopped: '..tostring(r.stopReason))
local usage=M.usage(r.usage)
@@ -284,7 +334,7 @@ function M.launch(s, first, resume_id)
assert(waiting,'Unexpected ACP reply'); assert(not q.error,(q.error or {}).message or 'ACP error')
if steering[q.id] then
local r=assert(q.result); local reason=r.stopReason
- assert(not reason or reason=='end_turn' or reason=='max_tokens','ACP steering rejected: '..tostring(reason))
+ assert(s.cancelling or not reason or reason=='end_turn' or reason=='max_tokens','ACP steering rejected: '..tostring(reason))
if reason and (not last_steer or q.id>last_steer.id) then last_steer={id=q.id,reason=reason} end
local u=M.usage(r.usage); if u then
if steer_usage then for k,v in pairs(u) do steer_usage[k]=steer_usage[k]+v end else steer_usage=u end
@@ -301,11 +351,11 @@ function M.launch(s, first, resume_id)
local u=p.update
if u.sessionUpdate=='config_option_update' and s.wire and not configuring then
opts=u.configOptions or {}; assert(confirmed(),'Configured model or reasoning level changed'); advertised()
- elseif s.prompting and u.sessionUpdate=='usage_update' and count(u.used) and count(u.size) and u.size>0 and u.used<=u.size then
+ elseif s.prompting and not s.cancelling and u.sessionUpdate=='usage_update' and count(u.used) and count(u.size) and u.size>0 and u.used<=u.size then
s.context_window={used=u.used,size=u.size}
- elseif s.prompting and (u.sessionUpdate=='tool_call' or u.sessionUpdate=='tool_call_update') then
+ elseif s.prompting and not s.cancelling and (u.sessionUpdate=='tool_call' or u.sessionUpdate=='tool_call_update') then
pcall(s.observe,u) -- a cosmetic mount failure must not cancel an agy turn
- elseif (u.sessionUpdate=='agent_message_chunk' or u.sessionUpdate=='agent_thought_chunk') and u.content.type=='text' and s.prompting then
+ elseif (u.sessionUpdate=='agent_message_chunk' or u.sessionUpdate=='agent_thought_chunk') and u.content.type=='text' and s.prompting and not s.cancelling then
s.delta(u.sessionUpdate=='agent_message_chunk' and 'text' or 'thinking',u.content.text)
end
elseif q.id~=nil then send({id=q.id,error={code=-32601,message='Unsupported client method'}}) end