nullius-eval 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- _nullius_app/.env.example +55 -0
- _nullius_app/app.py +96 -0
- _nullius_app/requirements.txt +1 -0
- _nullius_app/static/brand/README.md +20 -0
- _nullius_app/static/brand/docker.svg +1 -0
- _nullius_app/static/brand/favicon.svg +1 -0
- _nullius_app/static/brand/huggingface.svg +37 -0
- _nullius_app/static/brand/mark-dark.svg +1 -0
- _nullius_app/static/brand/marks.svg +34 -0
- _nullius_app/static/brand/tokens.json +51 -0
- _nullius_app/static/brand/typesafe.png +0 -0
- _nullius_app/static/brand/wordmark-dark.svg +1 -0
- _nullius_app/static/builder/command-builder-core.js +763 -0
- _nullius_app/static/builder/command-builder.css +248 -0
- _nullius_app/static/builder/command-builder.js +997 -0
- _nullius_app/static/builder/model-picker.js +113 -0
- _nullius_app/static/catalogue/add.js +46 -0
- _nullius_app/static/catalogue/agents/README.md +19 -0
- _nullius_app/static/catalogue/agents/goose.svg +1 -0
- _nullius_app/static/catalogue/agents/jiuwenswarm.svg +33 -0
- _nullius_app/static/catalogue/agents/marks.json +8 -0
- _nullius_app/static/catalogue/agents/mini-swe-agent.svg +7 -0
- _nullius_app/static/catalogue/agents/opencode.svg +7 -0
- _nullius_app/static/catalogue/catalogue.css +436 -0
- _nullius_app/static/catalogue/catalogue.js +87 -0
- _nullius_app/static/catalogue/marks.svg +29 -0
- _nullius_app/static/catalogue/new.js +78 -0
- _nullius_app/static/catalogue/versions.js +27 -0
- _nullius_app/static/compare/comparison.css +117 -0
- _nullius_app/static/controls.js +117 -0
- _nullius_app/static/disclosures.js +70 -0
- _nullius_app/static/fonts/0569c822cab6c510.ttf +0 -0
- _nullius_app/static/fonts/DMSans-Medium.woff2 +0 -0
- _nullius_app/static/fonts/DMSans-Regular.woff2 +0 -0
- _nullius_app/static/fonts/IBMPlexMono-Medium.woff2 +0 -0
- _nullius_app/static/fonts/IBMPlexMono-Regular.woff2 +0 -0
- _nullius_app/static/fonts/InstrumentSerif-Italic.woff2 +0 -0
- _nullius_app/static/fonts/InstrumentSerif-Regular.woff2 +0 -0
- _nullius_app/static/fonts/README.md +11 -0
- _nullius_app/static/fonts/c1e7453ea26a1d1e.ttf +0 -0
- _nullius_app/static/fonts/dmsans-OFL.txt +93 -0
- _nullius_app/static/fonts/ibmplexmono-OFL.txt +93 -0
- _nullius_app/static/fonts/instrumentserif-OFL.txt +93 -0
- _nullius_app/static/fonts.css +49 -0
- _nullius_app/static/notifications.js +49 -0
- _nullius_app/static/onboarding.css +78 -0
- _nullius_app/static/onboarding.js +158 -0
- _nullius_app/static/runs/board.css +358 -0
- _nullius_app/static/runs/board.js +127 -0
- _nullius_app/static/runs/launches.css +88 -0
- _nullius_app/static/runs/live-board.css +186 -0
- _nullius_app/static/runs/live-board.js +394 -0
- _nullius_app/static/runs/path-suggestions.js +119 -0
- _nullius_app/static/runs/start.css +38 -0
- _nullius_app/static/settings/autosave.js +162 -0
- _nullius_app/static/settings/custom-keys.js +100 -0
- _nullius_app/static/settings/data-providers.js +88 -0
- _nullius_app/static/settings/judges.js +61 -0
- _nullius_app/static/settings/modal.css +32 -0
- _nullius_app/static/settings/modal.js +91 -0
- _nullius_app/static/settings/navigation.js +55 -0
- _nullius_app/static/settings/prices.js +68 -0
- _nullius_app/static/settings/providers.js +134 -0
- _nullius_app/static/settings/sandbox.js +20 -0
- _nullius_app/static/settings/settings.css +288 -0
- _nullius_app/static/style.css +1752 -0
- _nullius_app/static/traces/conversation.css +49 -0
- _nullius_app/static/traces/conversation.js +215 -0
- _nullius_app/templates/agents/entry.html +145 -0
- _nullius_app/templates/agents/kits.html +55 -0
- _nullius_app/templates/agents/standard.html +58 -0
- _nullius_app/templates/base.html +479 -0
- _nullius_app/templates/builder/new_run.html +55 -0
- _nullius_app/templates/catalogue/add_dialog.html +32 -0
- _nullius_app/templates/catalogue/benchmark.html +270 -0
- _nullius_app/templates/catalogue/benchmarks.html +91 -0
- _nullius_app/templates/catalogue/new.html +141 -0
- _nullius_app/templates/catalogue/use_bundle.html +11 -0
- _nullius_app/templates/compare/_detail.html +37 -0
- _nullius_app/templates/compare/compare.html +298 -0
- _nullius_app/templates/compare/compare_picker.html +51 -0
- _nullius_app/templates/compare/compare_task.html +329 -0
- _nullius_app/templates/runs/_execution.html +131 -0
- _nullius_app/templates/runs/_runtime.html +5 -0
- _nullius_app/templates/runs/ascii-mark.html +485 -0
- _nullius_app/templates/runs/feasibility.html +127 -0
- _nullius_app/templates/runs/index.html +609 -0
- _nullius_app/templates/runs/job.html +460 -0
- _nullius_app/templates/runs/preparing.html +14 -0
- _nullius_app/templates/runs/reconcile.html +124 -0
- _nullius_app/templates/runs/setup.html +272 -0
- _nullius_app/templates/runs/start.html +51 -0
- _nullius_app/templates/settings/custom_keys.html +82 -0
- _nullius_app/templates/settings/data_providers.html +27 -0
- _nullius_app/templates/settings/settings.html +564 -0
- _nullius_app/templates/traces/_charts.html +165 -0
- _nullius_app/templates/traces/_conversation.html +37 -0
- _nullius_app/templates/traces/_judgement.html +72 -0
- _nullius_app/templates/traces/artifact.html +15 -0
- _nullius_app/templates/traces/trial.html +838 -0
- _nullius_app/templates/workspaces/workspaces.html +54 -0
- _nullius_app/web/__init__.py +1 -0
- _nullius_app/web/agents.py +230 -0
- _nullius_app/web/builder.py +406 -0
- _nullius_app/web/catalogue.py +370 -0
- _nullius_app/web/compare.py +489 -0
- _nullius_app/web/core.py +17 -0
- _nullius_app/web/execute.py +223 -0
- _nullius_app/web/feasibility.py +60 -0
- _nullius_app/web/filters.py +231 -0
- _nullius_app/web/judge.py +623 -0
- _nullius_app/web/judge_tasks.py +33 -0
- _nullius_app/web/onboarding.py +20 -0
- _nullius_app/web/reconcile.py +47 -0
- _nullius_app/web/runs.py +464 -0
- _nullius_app/web/settings.py +324 -0
- _nullius_app/web/shared.py +507 -0
- _nullius_app/web/traces.py +306 -0
- _nullius_app/web/workspaces.py +84 -0
- nullius/__init__.py +32 -0
- nullius/__main__.py +5 -0
- nullius/catalogue/__init__.py +0 -0
- nullius/catalogue/agents/codex/agent.json +222 -0
- nullius/catalogue/agents/codex/config.toml +25 -0
- nullius/catalogue/agents/goose/agent.json +131 -0
- nullius/catalogue/agents/goose/config.yaml +20 -0
- nullius/catalogue/agents/jiuwenswarm/agent.json +197 -0
- nullius/catalogue/agents/jiuwenswarm/agent.py +355 -0
- nullius/catalogue/agents/mini-swe-agent/agent.json +68 -0
- nullius/catalogue/agents/mini-swe-agent/run.py +37 -0
- nullius/catalogue/agents/opencode/agent.json +135 -0
- nullius/catalogue/agents/opencode/opencode.json +32 -0
- nullius/catalogue/agents/reference/agent.json +89 -0
- nullius/catalogue/agents/reference/agent.py +209 -0
- nullius/catalogue/agents/terminus-2/agent.json +64 -0
- nullius/catalogue/agents/terminus-2/run.py +139 -0
- nullius/catalogue/benchmarks/gaia/benchmark.json +72 -0
- nullius/catalogue/benchmarks/gaia/data-source.json +29 -0
- nullius/catalogue/benchmarks/gaia/driver.py +188 -0
- nullius/catalogue/benchmarks/gaia/env/data.Dockerfile +1 -0
- nullius/catalogue/benchmarks/gaia/standard.json +278 -0
- nullius/catalogue/benchmarks/gaia/upstream-settings.json +275 -0
- nullius/catalogue/benchmarks/gsm8k/benchmark.json +34 -0
- nullius/catalogue/benchmarks/gsm8k/driver.py +101 -0
- nullius/catalogue/benchmarks/gsm8k/standard.json +124 -0
- nullius/catalogue/benchmarks/gsm8k/upstream-settings.json +86 -0
- nullius/catalogue/benchmarks/humaneval/benchmark.json +36 -0
- nullius/catalogue/benchmarks/humaneval/driver.py +243 -0
- nullius/catalogue/benchmarks/humaneval/standard.json +115 -0
- nullius/catalogue/benchmarks/humaneval/upstream-settings.json +72 -0
- nullius/catalogue/benchmarks/locomo/benchmark.json +43 -0
- nullius/catalogue/benchmarks/locomo/driver.py +270 -0
- nullius/catalogue/benchmarks/locomo/standard.json +265 -0
- nullius/catalogue/benchmarks/locomo/upstream-settings.json +112 -0
- nullius/catalogue/benchmarks/longmemeval/benchmark.json +65 -0
- nullius/catalogue/benchmarks/longmemeval/driver.py +379 -0
- nullius/catalogue/benchmarks/longmemeval/standard.json +733 -0
- nullius/catalogue/benchmarks/longmemeval/upstream-settings.json +76 -0
- nullius/catalogue/benchmarks/swe-bench-verified/benchmark.json +87 -0
- nullius/catalogue/benchmarks/swe-bench-verified/driver.py +116 -0
- nullius/catalogue/benchmarks/swe-bench-verified/images.json +502 -0
- nullius/catalogue/benchmarks/swe-bench-verified/standard.json +108 -0
- nullius/catalogue/benchmarks/swe-bench-verified/upstream-settings.json +32 -0
- nullius/catalogue/benchmarks/tau3-bench/benchmark.json +49 -0
- nullius/catalogue/benchmarks/tau3-bench/driver.py +608 -0
- nullius/catalogue/benchmarks/tau3-bench/mcp_server.py +114 -0
- nullius/catalogue/benchmarks/tau3-bench/search_relay.py +19 -0
- nullius/catalogue/benchmarks/tau3-bench/standard.json +547 -0
- nullius/catalogue/benchmarks/tau3-bench/upstream-settings.json +639 -0
- nullius/catalogue/benchmarks/terminal-bench/benchmark.json +69 -0
- nullius/catalogue/benchmarks/terminal-bench/driver.py +267 -0
- nullius/catalogue/benchmarks/terminal-bench/standard.json +166 -0
- nullius/catalogue/benchmarks/terminal-bench/upstream-settings.json +36 -0
- nullius/catalogue/capabilities/keyword-memory/capability.json +33 -0
- nullius/catalogue/capabilities/keyword-memory/capability.py +55 -0
- nullius/catalogue/capabilities/mem0/capability.json +135 -0
- nullius/catalogue/capabilities/mem0/capability.py +173 -0
- nullius/catalogue/catalog.py +426 -0
- nullius/catalogue/citation.py +116 -0
- nullius/catalogue/harbor_entry.py +367 -0
- nullius/catalogue/sources.py +107 -0
- nullius/catalogue/storage.py +125 -0
- nullius/cli.py +85 -0
- nullius/compare/__init__.py +0 -0
- nullius/compare/cli.py +265 -0
- nullius/compare/comparison.py +184 -0
- nullius/execution.py +1653 -0
- nullius/gitrefs.py +128 -0
- nullius/harbor/__init__.py +0 -0
- nullius/harbor/feed.py +228 -0
- nullius/harbor/source.py +582 -0
- nullius/hosts.py +368 -0
- nullius/importers/__init__.py +140 -0
- nullius/importers/atif.py +387 -0
- nullius/importers/cli.py +135 -0
- nullius/importers/otel.py +274 -0
- nullius/judge/__init__.py +14 -0
- nullius/judge/batch.py +122 -0
- nullius/judge/client.py +433 -0
- nullius/judge/floors.json +86 -0
- nullius/judge/lanes.py +89 -0
- nullius/judge/questions.py +309 -0
- nullius/judge/reconcile.py +176 -0
- nullius/judge/state.py +224 -0
- nullius/judge/store.py +167 -0
- nullius/migrate.py +268 -0
- nullius/onboarding.py +116 -0
- nullius/paths.py +89 -0
- nullius/prerequisites.py +129 -0
- nullius/prices.py +283 -0
- nullius/progress.py +294 -0
- nullius/providers.py +335 -0
- nullius/recipes/__init__.py +0 -0
- nullius/recipes/launch.py +78 -0
- nullius/redact.py +72 -0
- nullius/runs/__init__.py +0 -0
- nullius/runs/artifacts.py +67 -0
- nullius/runs/classify.py +216 -0
- nullius/runs/cli.py +316 -0
- nullius/runs/feasibility.py +442 -0
- nullius/runs/launches.py +149 -0
- nullius/runs/live.py +715 -0
- nullius/runs/parse.py +1016 -0
- nullius/runs/verifier.py +241 -0
- nullius/sandbox.py +109 -0
- nullius/secrets.py +218 -0
- nullius/settings.py +544 -0
- nullius/standard/__init__.py +15 -0
- nullius/standard/acp.py +305 -0
- nullius/standard/cancellation.py +19 -0
- nullius/standard/cli.py +703 -0
- nullius/standard/conform.py +149 -0
- nullius/standard/container.py +1870 -0
- nullius/standard/definitions.py +1651 -0
- nullius/standard/egress.py +192 -0
- nullius/standard/fidelity.py +256 -0
- nullius/standard/forward.py +139 -0
- nullius/standard/hostreq.py +105 -0
- nullius/standard/howto.py +220 -0
- nullius/standard/install.py +428 -0
- nullius/standard/job.py +551 -0
- nullius/standard/jsonl.py +129 -0
- nullius/standard/kits/agent-conversation-v1/driver.py +27 -0
- nullius/standard/kits/agent-conversation-v1/standard.json +26 -0
- nullius/standard/kits/agent-search-v1/driver.py +171 -0
- nullius/standard/kits/agent-search-v1/standard.json +99 -0
- nullius/standard/kits/agent-task-v1/driver.py +25 -0
- nullius/standard/kits/agent-task-v1/standard.json +100 -0
- nullius/standard/kits/agent-tools-v1/driver.py +206 -0
- nullius/standard/kits/agent-tools-v1/mcp_kit.py +209 -0
- nullius/standard/kits/agent-tools-v1/standard.json +119 -0
- nullius/standard/kits/agent-workspace-v1/driver.py +65 -0
- nullius/standard/kits/agent-workspace-v1/standard.json +95 -0
- nullius/standard/kits/cap-memory-v1/driver.py +39 -0
- nullius/standard/kits/cap-memory-v1/standard.json +91 -0
- nullius/standard/ledger.py +326 -0
- nullius/standard/match.py +66 -0
- nullius/standard/mcp_bridge.py +62 -0
- nullius/standard/mcp_machine.py +193 -0
- nullius/standard/merge.py +239 -0
- nullius/standard/names.py +37 -0
- nullius/standard/network_requirements.py +44 -0
- nullius/standard/pace.py +53 -0
- nullius/standard/payload.py +278 -0
- nullius/standard/pins.py +130 -0
- nullius/standard/ports.py +70 -0
- nullius/standard/prove.py +373 -0
- nullius/standard/recipe.py +746 -0
- nullius/standard/relay.py +785 -0
- nullius/standard/run.py +1781 -0
- nullius/standard/setup_container.py +208 -0
- nullius/standard/shard.py +71 -0
- nullius/standard/web_services.py +30 -0
- nullius/traces/__init__.py +0 -0
- nullius/traces/behavior.py +131 -0
- nullius/traces/charts.py +898 -0
- nullius/traces/conversation.py +159 -0
- nullius/traces/otel.py +608 -0
- nullius/traces/trace.py +1477 -0
- nullius/traces/trajectory.py +586 -0
- nullius/workspace.py +417 -0
- nullius/workspace_cli.py +119 -0
- nullius_eval-0.1.0.dist-info/METADATA +93 -0
- nullius_eval-0.1.0.dist-info/RECORD +291 -0
- nullius_eval-0.1.0.dist-info/WHEEL +5 -0
- nullius_eval-0.1.0.dist-info/entry_points.txt +2 -0
- nullius_eval-0.1.0.dist-info/licenses/LICENSE +202 -0
- nullius_eval-0.1.0.dist-info/licenses/app/static/fonts/dmsans-OFL.txt +93 -0
- nullius_eval-0.1.0.dist-info/licenses/app/static/fonts/ibmplexmono-OFL.txt +93 -0
- nullius_eval-0.1.0.dist-info/licenses/app/static/fonts/instrumentserif-OFL.txt +93 -0
- nullius_eval-0.1.0.dist-info/top_level.txt +2 -0
nullius/standard/acp.py
ADDED
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
"""Nullius as an ACP client: start an agent, give it a session, prompt it.
|
|
2
|
+
|
|
3
|
+
ACP (the Agent Client Protocol) is JSON-RPC 2.0 over the agent's stdin and
|
|
4
|
+
stdout. Nullius asks ``initialize``, then ``session/new`` with the unit's
|
|
5
|
+
workspace as ``cwd`` and the benchmark's MCP servers, then ``session/prompt``
|
|
6
|
+
once per turn. While a turn runs the agent streams ``session/update``
|
|
7
|
+
notifications - its message in chunks, its tool calls and their results, its
|
|
8
|
+
plan - which are kept, timed, as the turn's events: the trajectory is built
|
|
9
|
+
from them.
|
|
10
|
+
|
|
11
|
+
The agent may ask the client things too. Permission is refused unless the
|
|
12
|
+
client was made with ``grant=True``; a run does that, answering each request
|
|
13
|
+
once (``allow_once``), because no one is there to answer - the agent works in
|
|
14
|
+
a directory of its own, and isolation is the backend's business, not a
|
|
15
|
+
dialog's. And ``fs/read_text_file`` and ``fs/write_text_file`` are served,
|
|
16
|
+
but only inside the session's ``cwd``, and only to an agent running on this
|
|
17
|
+
machine (a run offers them to none in a container). No terminal is offered: an agent runs
|
|
18
|
+
commands with its own tools.
|
|
19
|
+
|
|
20
|
+
The client says it takes **notices** (``session.notices``): an adapter that
|
|
21
|
+
knows them sends its warnings - a configuration it questions, a model it has
|
|
22
|
+
no metadata for - as a ``notice`` update rather than as text in the agent's
|
|
23
|
+
message, where a benchmark would read it as part of the answer. An adapter
|
|
24
|
+
that does not know them sends text as before, and :func:`last_message` keeps
|
|
25
|
+
what it can of the answer apart.
|
|
26
|
+
|
|
27
|
+
A turn that outlives its timeout is cancelled (``session/cancel``) and, if the
|
|
28
|
+
agent does not stop, its process group is killed; :class:`Timeout` is raised
|
|
29
|
+
either way, and the events so far are kept on it.
|
|
30
|
+
"""
|
|
31
|
+
from __future__ import annotations
|
|
32
|
+
|
|
33
|
+
import json
|
|
34
|
+
import os
|
|
35
|
+
import queue
|
|
36
|
+
from concurrent.futures import CancelledError
|
|
37
|
+
from nullius.standard.cancellation import reply
|
|
38
|
+
import signal
|
|
39
|
+
import subprocess
|
|
40
|
+
import threading
|
|
41
|
+
import time
|
|
42
|
+
from dataclasses import dataclass, field
|
|
43
|
+
from typing import Dict, List, Optional
|
|
44
|
+
|
|
45
|
+
PROTOCOL_VERSION = 1
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class AgentFailed(Exception):
|
|
49
|
+
"""The agent ended, or answered a request with an error."""
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class Timeout(Exception):
|
|
53
|
+
def __init__(self, msg, events):
|
|
54
|
+
super().__init__(msg)
|
|
55
|
+
self.events = events
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
@dataclass
|
|
59
|
+
class Turn:
|
|
60
|
+
text: str
|
|
61
|
+
stop: str
|
|
62
|
+
events: List[dict] = field(default_factory=list)
|
|
63
|
+
started: float = 0.0
|
|
64
|
+
ended: float = 0.0
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
# What does not end a message: its own chunks, its thoughts, and the usage
|
|
68
|
+
# notes an agent sends while it streams.
|
|
69
|
+
_IN_MESSAGE = {"agent_message_chunk", "agent_thought_chunk", "usage_update"}
|
|
70
|
+
# Session metadata: a title, the commands on offer, the mode. Between two
|
|
71
|
+
# messages it parts them (Codex sends a notice as text, then its commands,
|
|
72
|
+
# then the answer); after the last message it ends nothing (Goose names the
|
|
73
|
+
# session once it has answered).
|
|
74
|
+
_METADATA = {"session_info_update", "available_commands_update", "current_mode_update"}
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def last_message(events: List[dict]) -> str:
|
|
78
|
+
"""A turn's answer: the agent's last message, separated by ACP messageId
|
|
79
|
+
or another event (a tool call, a plan, a notice). Without IDs, session
|
|
80
|
+
metadata parts two messages only when another message follows it."""
|
|
81
|
+
parts: List[str] = []
|
|
82
|
+
parted = False
|
|
83
|
+
message_id = None
|
|
84
|
+
for e in events:
|
|
85
|
+
u = e.get("update") or {}
|
|
86
|
+
kind = u.get("sessionUpdate")
|
|
87
|
+
if kind == "agent_message_chunk":
|
|
88
|
+
chunk_id = u.get("messageId")
|
|
89
|
+
new_message = chunk_id != message_id if chunk_id and message_id else parted
|
|
90
|
+
if new_message:
|
|
91
|
+
parts, message_id = [], None
|
|
92
|
+
message_id = chunk_id or message_id
|
|
93
|
+
parted = False
|
|
94
|
+
parts.append(((u.get("content") or {}).get("text")) or "")
|
|
95
|
+
elif kind in _METADATA:
|
|
96
|
+
parted = True
|
|
97
|
+
elif kind not in _IN_MESSAGE:
|
|
98
|
+
parts, parted = [], False
|
|
99
|
+
message_id = None
|
|
100
|
+
return "".join(parts)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _inside(path: str, root: str) -> bool:
|
|
104
|
+
path, root = os.path.realpath(path), os.path.realpath(root)
|
|
105
|
+
return path == root or path.startswith(root + os.sep)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
class Client:
|
|
109
|
+
def __init__(self, cmd: List[str], env: dict, log: str, cwd: Optional[str] = None, grant: bool = False,
|
|
110
|
+
fs: bool = True, stopped=None):
|
|
111
|
+
"""``grant``: answer the agent's permission requests with yes (once
|
|
112
|
+
each). Off unless the caller turns it on: a benchmark run does, since
|
|
113
|
+
no one is there to answer, and records that it did. ``fs``: offer the
|
|
114
|
+
agent this machine's files inside the session's ``cwd``; off when that
|
|
115
|
+
``cwd`` is a container's own (an own image), which this machine does
|
|
116
|
+
not see, so the agent reads and writes there with its own tools."""
|
|
117
|
+
self.cmd, self.env, self.log, self.cwd, self.grant, self.fs = cmd, env, log, cwd, grant, fs
|
|
118
|
+
self.n = 0
|
|
119
|
+
self.waiting: Dict[int, "queue.Queue"] = {}
|
|
120
|
+
self.events: List[dict] = []
|
|
121
|
+
self.roots: Dict[str, str] = {} # session id -> its cwd, for fs requests
|
|
122
|
+
self.lock = threading.Lock()
|
|
123
|
+
self.stopped = stopped
|
|
124
|
+
self.p: Optional[subprocess.Popen] = None
|
|
125
|
+
self.ended = threading.Event()
|
|
126
|
+
self.info: dict = {}
|
|
127
|
+
|
|
128
|
+
def start(self) -> "Client":
|
|
129
|
+
if self.stopped is not None and self.stopped.is_set():
|
|
130
|
+
raise CancelledError("the run was stopped")
|
|
131
|
+
self.err = open(self.log, "a")
|
|
132
|
+
self.p = subprocess.Popen(self.cmd, cwd=self.cwd, env=self.env, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
|
|
133
|
+
stderr=self.err, bufsize=0, start_new_session=True)
|
|
134
|
+
threading.Thread(target=self._read, daemon=True).start()
|
|
135
|
+
self.info = self.request("initialize", {"protocolVersion": PROTOCOL_VERSION,
|
|
136
|
+
"clientCapabilities": {"fs": {"readTextFile": self.fs, "writeTextFile": self.fs},
|
|
137
|
+
"terminal": False, "session": {"notices": {}}},
|
|
138
|
+
"clientInfo": {"name": "nullius", "version": "1"}}, timeout=120)
|
|
139
|
+
return self
|
|
140
|
+
|
|
141
|
+
def _send(self, msg: dict):
|
|
142
|
+
data = (json.dumps(msg) + "\n").encode()
|
|
143
|
+
with self.lock:
|
|
144
|
+
self.p.stdin.write(data)
|
|
145
|
+
self.p.stdin.flush()
|
|
146
|
+
|
|
147
|
+
def _read(self):
|
|
148
|
+
for raw in self.p.stdout:
|
|
149
|
+
try:
|
|
150
|
+
m = json.loads(raw)
|
|
151
|
+
except ValueError:
|
|
152
|
+
continue # an agent's stray print is not protocol
|
|
153
|
+
if "method" in m and "id" in m:
|
|
154
|
+
threading.Thread(target=self._answer, args=(m,), daemon=True).start()
|
|
155
|
+
elif "method" in m:
|
|
156
|
+
if m["method"] == "session/update":
|
|
157
|
+
self.events.append({"t": time.time(), "update": (m.get("params") or {}).get("update") or {}})
|
|
158
|
+
elif m.get("id") in self.waiting:
|
|
159
|
+
self.waiting[m["id"]].put(m)
|
|
160
|
+
self.ended.set()
|
|
161
|
+
for q in list(self.waiting.values()):
|
|
162
|
+
q.put(None)
|
|
163
|
+
|
|
164
|
+
def tail(self) -> str:
|
|
165
|
+
try:
|
|
166
|
+
self.err.flush()
|
|
167
|
+
with open(self.log, errors="replace") as f:
|
|
168
|
+
lines = [l.strip() for l in f.read().splitlines() if l.strip()]
|
|
169
|
+
return " | ".join(lines[-3:])[-400:] if lines else "nothing on stderr"
|
|
170
|
+
except OSError:
|
|
171
|
+
return "no log"
|
|
172
|
+
|
|
173
|
+
def request(self, method: str, params: dict, timeout: float = 600):
|
|
174
|
+
if self.stopped is not None and self.stopped.is_set():
|
|
175
|
+
raise CancelledError("the run was stopped")
|
|
176
|
+
self.n += 1
|
|
177
|
+
i, q = self.n, queue.Queue()
|
|
178
|
+
self.waiting[i] = q
|
|
179
|
+
try:
|
|
180
|
+
self._send({"jsonrpc": "2.0", "id": i, "method": method, "params": params})
|
|
181
|
+
except (BrokenPipeError, OSError):
|
|
182
|
+
raise AgentFailed(f"{method}: the agent had already ended ({self.tail()})")
|
|
183
|
+
try:
|
|
184
|
+
m = reply(q, timeout, self.stopped)
|
|
185
|
+
except queue.Empty:
|
|
186
|
+
raise TimeoutError(method)
|
|
187
|
+
finally:
|
|
188
|
+
self.waiting.pop(i, None)
|
|
189
|
+
if m is None:
|
|
190
|
+
raise AgentFailed(f"{method}: the agent ended ({self.tail()})")
|
|
191
|
+
if "error" in m:
|
|
192
|
+
e = m["error"] or {}
|
|
193
|
+
raise AgentFailed(f"{method}: {e.get('message')} {json.dumps(e.get('data'))[:300] if e.get('data') else ''}".strip())
|
|
194
|
+
return m.get("result") or {}
|
|
195
|
+
|
|
196
|
+
def _answer(self, m: dict):
|
|
197
|
+
method, params = m["method"], m.get("params") or {}
|
|
198
|
+
try:
|
|
199
|
+
result = self._serve(method, params)
|
|
200
|
+
reply = {"jsonrpc": "2.0", "id": m["id"], "result": result}
|
|
201
|
+
except Exception as e:
|
|
202
|
+
reply = {"jsonrpc": "2.0", "id": m["id"], "error": {"code": -32603 if not isinstance(e, KeyError) else -32601,
|
|
203
|
+
"message": str(e)[:300]}}
|
|
204
|
+
try:
|
|
205
|
+
self._send(reply)
|
|
206
|
+
except OSError:
|
|
207
|
+
pass
|
|
208
|
+
|
|
209
|
+
def _serve(self, method: str, params: dict):
|
|
210
|
+
if method == "session/request_permission":
|
|
211
|
+
opts = params.get("options") or []
|
|
212
|
+
pick = None
|
|
213
|
+
if self.grant:
|
|
214
|
+
pick = next((o for o in opts if o.get("kind") == "allow_once"), None) or \
|
|
215
|
+
next((o for o in opts if str(o.get("kind", "")).startswith("allow")), None)
|
|
216
|
+
self.events.append({"t": time.time(), "update": {"sessionUpdate": "permission", "granted": bool(pick),
|
|
217
|
+
"toolCall": params.get("toolCall")}})
|
|
218
|
+
if not pick:
|
|
219
|
+
return {"outcome": {"outcome": "cancelled"}}
|
|
220
|
+
return {"outcome": {"outcome": "selected", "optionId": pick["optionId"]}}
|
|
221
|
+
root = self.roots.get(params.get("sessionId"))
|
|
222
|
+
if method in ("fs/read_text_file", "fs/write_text_file"):
|
|
223
|
+
path = params.get("path") or ""
|
|
224
|
+
if not self.fs:
|
|
225
|
+
raise PermissionError("no files are served: the workspace is the container's own")
|
|
226
|
+
if not root or not os.path.isabs(path) or not _inside(path, root):
|
|
227
|
+
raise PermissionError(f"{path} is outside the session's workspace")
|
|
228
|
+
if method == "fs/read_text_file":
|
|
229
|
+
with open(path, errors="replace") as f:
|
|
230
|
+
lines = f.read().splitlines(keepends=True)
|
|
231
|
+
start = max(0, (params.get("line") or 1) - 1)
|
|
232
|
+
end = start + params["limit"] if params.get("limit") else None
|
|
233
|
+
return {"content": "".join(lines[start:end])}
|
|
234
|
+
os.makedirs(os.path.dirname(path), exist_ok=True)
|
|
235
|
+
with open(path, "w") as f:
|
|
236
|
+
f.write(params.get("content") or "")
|
|
237
|
+
return None
|
|
238
|
+
raise KeyError(f"{method} is not served")
|
|
239
|
+
|
|
240
|
+
def new_session(self, cwd: str, mcp: Optional[List[dict]] = None) -> str:
|
|
241
|
+
servers = [{"name": s["name"], "command": s["command"], "args": s.get("args") or [],
|
|
242
|
+
"env": [{"name": k, "value": v} for k, v in (s.get("env") or {}).items()]} for s in mcp or []]
|
|
243
|
+
params = {"cwd": os.path.abspath(cwd), "mcpServers": servers}
|
|
244
|
+
try:
|
|
245
|
+
res = self.request("session/new", params, timeout=180)
|
|
246
|
+
except AgentFailed as e:
|
|
247
|
+
if "auth" not in str(e).lower() or not self.authenticate():
|
|
248
|
+
raise
|
|
249
|
+
res = self.request("session/new", params, timeout=180)
|
|
250
|
+
sid = res["sessionId"]
|
|
251
|
+
self.roots[sid] = os.path.abspath(cwd)
|
|
252
|
+
return sid
|
|
253
|
+
|
|
254
|
+
def authenticate(self) -> bool:
|
|
255
|
+
"""ACP's ``authenticate`` with the first method of type ``env_var``
|
|
256
|
+
whose variables the agent's environment holds - the relay's address
|
|
257
|
+
and placeholder key, never a real one. False when none is."""
|
|
258
|
+
for m in self.info.get("authMethods") or []:
|
|
259
|
+
names = [v.get("name") for v in m.get("vars") or []]
|
|
260
|
+
if m.get("type") == "env_var" and names and all(self.env.get(n) for n in names):
|
|
261
|
+
self.request("authenticate", {"methodId": m["id"]}, timeout=60)
|
|
262
|
+
return True
|
|
263
|
+
return False
|
|
264
|
+
|
|
265
|
+
def prompt(self, sid: str, text: str, timeout: float = 600) -> Turn:
|
|
266
|
+
start = len(self.events)
|
|
267
|
+
t0 = time.time()
|
|
268
|
+
self.events.append({"t": t0, "update": {"sessionUpdate": "user_message_chunk", "content": {"type": "text", "text": text}}})
|
|
269
|
+
try:
|
|
270
|
+
res = self.request("session/prompt", {"sessionId": sid, "prompt": [{"type": "text", "text": text}]}, timeout=timeout)
|
|
271
|
+
except TimeoutError:
|
|
272
|
+
try:
|
|
273
|
+
self._send({"jsonrpc": "2.0", "method": "session/cancel", "params": {"sessionId": sid}})
|
|
274
|
+
except OSError:
|
|
275
|
+
pass
|
|
276
|
+
time.sleep(3)
|
|
277
|
+
self.close()
|
|
278
|
+
raise Timeout(f"the turn did not end in {int(timeout)} s", self.events[start:])
|
|
279
|
+
evs = self.events[start:]
|
|
280
|
+
return Turn(text=last_message(evs), stop=res.get("stopReason") or "", events=evs, started=t0, ended=time.time())
|
|
281
|
+
|
|
282
|
+
def close(self):
|
|
283
|
+
if self.p and self.p.poll() is None:
|
|
284
|
+
try:
|
|
285
|
+
self.p.stdin.close()
|
|
286
|
+
except OSError:
|
|
287
|
+
pass
|
|
288
|
+
try:
|
|
289
|
+
self.p.wait(timeout=3)
|
|
290
|
+
except subprocess.TimeoutExpired:
|
|
291
|
+
pass
|
|
292
|
+
if self.p:
|
|
293
|
+
try: # the agent, and whatever it left running in its group
|
|
294
|
+
os.killpg(self.p.pid, signal.SIGKILL)
|
|
295
|
+
except OSError:
|
|
296
|
+
pass
|
|
297
|
+
try:
|
|
298
|
+
self.p.wait(timeout=3)
|
|
299
|
+
self.p.stdout.close()
|
|
300
|
+
except Exception:
|
|
301
|
+
pass
|
|
302
|
+
try:
|
|
303
|
+
self.err.close()
|
|
304
|
+
except Exception:
|
|
305
|
+
pass
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"""Interrupt protocol waits when their run stops."""
|
|
2
|
+
import queue
|
|
3
|
+
import time
|
|
4
|
+
from concurrent.futures import CancelledError
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def reply(messages, timeout, stopped=None):
|
|
8
|
+
deadline = time.monotonic() + timeout
|
|
9
|
+
while True:
|
|
10
|
+
if stopped is not None and stopped.is_set():
|
|
11
|
+
raise CancelledError("the run was stopped")
|
|
12
|
+
remaining = deadline - time.monotonic()
|
|
13
|
+
if remaining <= 0:
|
|
14
|
+
raise queue.Empty
|
|
15
|
+
try:
|
|
16
|
+
return messages.get(timeout=min(remaining, 0.1) if stopped is not None else remaining)
|
|
17
|
+
except queue.Empty:
|
|
18
|
+
if time.monotonic() >= deadline:
|
|
19
|
+
raise
|