Debian Codegraph @

examples/managed-agents-worker-dispatch.py

raw
1 #!/usr/bin/env python3
2 """Service one already-claimed work item — the self-hosted "sandbox process" shape.
3
4 Unlike `managed-agents-self-hosted-sandbox-worker.py` (which creates an agent +
5 session and runs `EnvironmentWorker.run()` as a long-running poll loop), this
6 process does *not* create anything and does *not* poll. Something upstream — an
7 `ant worker poll --on-work` script, or your own orchestrator that spawns a
8 sandbox per work item — already claimed a `session` work item and handed it to
9 this process. Our only job is to run that one item's tool calls to completion,
10 then exit.
11
12 `EnvironmentWorker.handle_item()` with no arguments reads the work-item
13 identity from the environment variables that `ant worker poll --on-work` sets
14 on the process it spawns:
15
16 ANTHROPIC_WORK_ID the claimed work item to service
17 ANTHROPIC_ENVIRONMENT_ID the self-hosted environment it belongs to
18 ANTHROPIC_SESSION_ID the session whose tool calls we run
19 ANTHROPIC_ENVIRONMENT_KEY the environment key (the worker's single credential)
20
21 It builds the per-session workdir, downloads the session agent's skills, runs the
22 tools while heartbeating the work-item lease, and force-stops the item on exit.
23
24 Security model: the worker executes bash and file operations directly on the
25 host. Run inside a container or other isolation boundary you control.
26 """
27
28 from __future__ import annotations
29
30 import asyncio
31 import logging
32 from datetime import datetime
33
34 from anthropic import AsyncAnthropic
35 from anthropic.lib.tools import beta_async_tool
36 from anthropic.lib.tools.agent_toolset import beta_agent_toolset_20260401
37
38
39 # A custom tool, same pattern as managed-agents-self-hosted-sandbox-worker.py:
40 # @beta_async_tool produces the same type the worker's tool runner accepts, so it
41 # runs alongside the built-in agent_toolset tools with no extra wiring.
42 @beta_async_tool
43 async def current_time() -> str:
44 """Return the host's current local time as an ISO-8601 string."""
45 return datetime.now().isoformat()
46
47
48 async def main() -> None:
49 logging.basicConfig(level=logging.INFO)
50 client = AsyncAnthropic()
51
52 # No agent/session creation and no polling here — an upstream poller already
53 # claimed the item. Build the worker with just a `tools` factory: it is
54 # invoked once per claimed session with that session's `AgentToolContext`,
55 # so the built-in toolset binds to the right per-session workdir.
56 # (`worker(...)` returns an `EnvironmentWorker`; you can also construct one
57 # directly with `EnvironmentWorker(client, ...)` from `anthropic.lib.environments`.)
58 worker = client.beta.environments.work.worker(
59 workdir="/workspace",
60 tools=lambda env: [*beta_agent_toolset_20260401(env), current_time],
61 )
62
63 # handle_item() with no arguments reads ANTHROPIC_WORK_ID /
64 # ANTHROPIC_ENVIRONMENT_ID / ANTHROPIC_SESSION_ID / ANTHROPIC_ENVIRONMENT_KEY
65 # from the environment, then runs the per-item flow: set up the workdir +
66 # skills, run the session's tool calls while heartbeating the lease, and
67 # force-stop the work item on exit. It returns once the item is done.
68 await worker.handle_item()
69 print("work item complete")
70
71
72 if __name__ == "__main__":
73 asyncio.run(main())