Skip to content
322 changes: 284 additions & 38 deletions agentdescent/mcp.py

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions agentdescent/runstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,11 @@ def cancel(run_id: str, *, store: Optional[str] = None, grace: float = 5.0) -> R
st = rd.status()
if st.pid is None or not st.alive():
if st.state == "running":
st = rd.update_status(state="cancelled", finished=time.time())
# Clear `pending_calls` with the state: a cancelled run that kept a
# call in flight would hold a phantom charge against its Ticket
# forever (#1082).
st = rd.update_status(state="cancelled", finished=time.time(),
pending_calls=0)
return st
pgid = None
try:
Expand All @@ -830,7 +834,7 @@ def cancel(run_id: str, *, store: Optional[str] = None, grace: float = 5.0) -> R
if st.alive():
_signal(st.pid, pgid, getattr(signal, "SIGKILL", signal.SIGTERM))
return rd.update_status(state="cancelled", finished=time.time(),
stop_reason="cancelled")
stop_reason="cancelled", pending_calls=0)


def _signal(pid: int, pgid: Optional[int], sig: int) -> None:
Expand Down
19 changes: 19 additions & 0 deletions changelog.d/1082-ticket-call-cap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Fixed
`AGENTDESCENT_MAX_CALLS` is now a budget for one worker session, not a number
each run may ask for again: an MCP `start` after earlier runs is clamped to
what the Ticket has left, reports `call_limit.ticket` with ceiling/spent/
remaining, and refuses outright once the ceiling is spent (#1082). The clamp is
what the run is actually started with, not just what the reply says, and a
started run holds its cap until it reports, so two overlapping starts cannot
both take the remainder (#1082).

A run whose status cannot be read is no longer counted as free: the Ticket
refuses to start or resume anything until the run store is readable again,
instead of granting a remainder that may already be spent. A `max_usd` without
`usd_per_call` is still refused with the argument named in the message, now
from inside the same gate (#1196).

`resume` shares the same Ticket remainder as `start`: a run this server started
is relaunched with its ledger plus what is left, and a run it did not start is
registered and charged on its first resume here, its whole earlier ledger
counted, with no grant once that ledger fills the remainder.
25 changes: 25 additions & 0 deletions docs/dsh-super-agent.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,31 @@ your tree, under the operator's write allowlist).
the Ticket.
- `plan` is the budget line: it estimates cost without executing. Read it before
`start`, and put the number in the Ticket.
- A Ticket's ceiling is the session's, not the run's. With
`AGENTDESCENT_MAX_CALLS` set, the server spends it across the runs it starts:
a second `start` is clamped to the remainder, its reply says
`ticket: {ceiling, spent, remaining}`, and once the ceiling is spent `start`
refuses. `resume` is held to the same remainder: a run this server started is
relaunched with its own ledger plus what is left, while a run it did not start
is relaunched with the remainder itself, its earlier calls counted inside that
number, and a run whose ledger already fills the remainder is refused instead
of relaunched. Start the next Ticket in a new server session (or raise the
ceiling) rather than reusing the spent one (#1082).
Only the runs this server started are charged: a run it did not start (a CLI
`evolve`, a run from another process) is outside the accounting unless it is
resumed here, so mixing CLI and MCP runs inside one session is not covered.
Once an external run is resumed here its whole ledger is charged -- 2 calls
before plus 1 after counts as 3 spent, and the next `start` is refused -- and
its child is still clamped by the absolute ceiling, never by the remainder
alone.
A run whose status cannot be read is fail-closed: while its `status.json` is
missing or corrupt, `start`, `plan` and `resume` refuse rather than guess what
the Ticket spent, and the way back is to restore that file or start a new
session -- deleting the run directory does not help, because the budget it
freed cannot be verified.
`AGENTDESCENT_MAX_CALLS` is the only operator ceiling and it is per-Ticket; a
caller's `max_calls` and `max_usd` stay per-run flags, and only `max_calls` is
clamped by what the Ticket has left (#1082).
- A detached run no longer inherits the worker session: `launch` drops the
host-session markers it lists, except the four host config roots it keeps
(`DETACHED_KEEP_VARS`: `CLAUDE_CONFIG_DIR`, `CODEX_HOME`, `DSH_HOME`,
Expand Down
7 changes: 6 additions & 1 deletion docs/plugin-design.md
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,12 @@ Two guards keep this from folding in on itself:
the environment, not by review. The MCP surface takes the same numbers —
`plan` / `start` accept `max_calls` / `max_usd` and report `call_limit` with
what was asked for and what will be enforced, so a host sees a request of 9
come back as 2 with `source: AGENTDESCENT_MAX_CALLS` (#1184).
come back as 2 with `source: AGENTDESCENT_MAX_CALLS` (#1184). One worker
session is one Ticket, so the server does not hand the ceiling out again per
run: a second `start` is clamped to what the Ticket has left, `call_limit`
then carries `ticket: {ceiling, spent, remaining}`, and a spent Ticket
refuses another `start` instead of launching a run that cannot charge a
single call (#1082).
* **The controller never hot-swaps itself.** A self-evolved plugin reaches the
user's real installation only through `apply`, which is a human decision on
a diff, after an L1 oracle gate, with a backup. The loop can improve its own
Expand Down
Loading
Loading