The process_manage model tool (its schema, the list helper, the session action table, the handler and its registration) moves word for word from tools/process_registry.py to the new tools/process_manage_tool.py. The registry file keeps the process registry, its singleton and the output redaction helpers the tool and the gateway share. The safe-worker import list names the new module, which loads the registry as before. The tool's name, schema, toolset and behaviour are unchanged, and nothing a user sees changes. Tests change only their imports and patch targets.
119 lines
5.6 KiB
Python
119 lines
5.6 KiB
Python
"""The ``process_manage`` model tool: list, poll, read, wait on, write to and kill
|
|
terminal(background=true) processes held by the process registry."""
|
|
|
|
import json
|
|
|
|
from tools.process_registry import _not_found, _redact_process_result, process_registry
|
|
from tools.process_registry_wait import process_deadline_floor
|
|
from tools.registry import registry, tool_error
|
|
|
|
PROCESS_SCHEMA = {
|
|
"name": "process_manage",
|
|
# The enum names the verbs; the description keeps only non-obvious semantics
|
|
# (write-vs-submit is the one real trap: a lone \n on a Windows PTY is not Enter).
|
|
# See #95681.
|
|
"description": (
|
|
"Poll, wait on, send input to, or kill terminal(background=true) processes. "
|
|
"Completed results remain retrievable by session_id when resuming their owning conversation "
|
|
"(up to 7 days, newest 64 results per profile; rolling output tail). "
|
|
"poll: status + new output. log: full output, paged. wait: block "
|
|
"until exit or timeout (partial output on timeout). write vs "
|
|
"submit: submit appends Enter — use it to answer prompts; write "
|
|
"sends raw bytes, no newline. close: EOF stdin. kill: terminate. "
|
|
"handoff (subagents only): transfer a running process you started to your parent agent, which then "
|
|
"receives its completion; `data` = one sentence on its purpose. Subagent-owned processes are otherwise "
|
|
"killed when the subagent finishes and their notifications never reach the parent."
|
|
),
|
|
"parameters": {
|
|
"type": "object",
|
|
"properties": {
|
|
"action": {
|
|
"type": "string",
|
|
"enum": ["list", "poll", "log", "wait", "kill", "write", "submit", "close", "handoff"]
|
|
},
|
|
"session_id": {
|
|
"type": "string",
|
|
"description": "From terminal background output; any unique prefix works ('4dae' for proc_4dae56ca81f6). Required except for 'list'."
|
|
},
|
|
"data": {
|
|
"type": "string",
|
|
"description": "Stdin text for write/submit; purpose sentence for handoff."
|
|
},
|
|
"timeout": {
|
|
"type": "integer",
|
|
"description": "Max seconds for 'wait'.",
|
|
"minimum": 1
|
|
},
|
|
"offset": {
|
|
"type": "integer",
|
|
"description": "Log line offset (default: last 200)."
|
|
},
|
|
"acknowledge": {"type": "boolean", "description": "'poll' on an exited process: you handled its result, so skip its notify_on_complete turn."},
|
|
"limit": {
|
|
"type": "integer",
|
|
"description": "Max log lines.",
|
|
"minimum": 1
|
|
}
|
|
},
|
|
"required": ["action"]
|
|
}
|
|
}
|
|
|
|
|
|
def _list_processes(task_id) -> dict:
|
|
# Also surface session-scoped background processes (e.g. a forgotten preview
|
|
# server): they share the gateway session_key and can block session reset.
|
|
from tools.process_registry_access import caller_session_key
|
|
return {"processes": [
|
|
_redact_process_result(p)
|
|
for p in process_registry.list_sessions(
|
|
task_id=task_id, session_key=caller_session_key() or None, include_retained=True)]}
|
|
|
|
|
|
# action -> (handler(session_id, args) -> dict, redact output?). Output-bearing
|
|
# actions are redacted; stdin actions return only status.
|
|
_SESSION_ACTIONS = {
|
|
"poll": (lambda sid, a: process_registry.poll(sid, acknowledge=a.get("acknowledge") is True), True),
|
|
"log": (lambda sid, a: process_registry.read_log(sid, offset=a.get("offset"), limit=a.get("limit", 200)), True),
|
|
"wait": (lambda sid, a: process_registry.wait(sid, timeout=a.get("timeout")), True),
|
|
"kill": (lambda sid, a: process_registry.kill_process(sid), True),
|
|
"write": (lambda sid, a: process_registry.write_stdin(sid, str(a.get("data", ""))), False),
|
|
"submit": (lambda sid, a: process_registry.submit_stdin(sid, str(a.get("data", ""))), False),
|
|
"close": (lambda sid, a: process_registry.close_stdin(sid), False),
|
|
}
|
|
|
|
|
|
def _handle_process(args, **kw):
|
|
action = args.get("action", "")
|
|
# Coerce to string — some models send session_id as an integer
|
|
session_id = str(args.get("session_id", "")) if args.get("session_id") is not None else ""
|
|
if action == "list":
|
|
return json.dumps(_list_processes(kw.get("task_id")), ensure_ascii=False)
|
|
if action == "handoff":
|
|
if not session_id:
|
|
return tool_error("session_id is required for handoff")
|
|
from tools.process_registry_handoff import handoff_process
|
|
return json.dumps(handoff_process(session_id, args, kw.get("task_id")), ensure_ascii=False)
|
|
if action in _SESSION_ACTIONS:
|
|
if not session_id:
|
|
return tool_error(f"session_id is required for {action}")
|
|
from tools.process_registry_access import resolve_actionable
|
|
allowed, session_id = resolve_actionable(session_id, kw.get("task_id"))
|
|
if not allowed:
|
|
return json.dumps(_not_found(session_id), ensure_ascii=False)
|
|
handler, redact = _SESSION_ACTIONS[action]
|
|
from tools.process_registry_wait_policy import apply_wait_poll_policy
|
|
result = apply_wait_poll_policy(action, session_id, handler(session_id, args))
|
|
return json.dumps(_redact_process_result(result) if redact else result, ensure_ascii=False)
|
|
return tool_error(f"Unknown process action: {action}. Use: list, poll, log, wait, kill, write, submit, close, handoff")
|
|
|
|
|
|
registry.register(
|
|
name="process_manage",
|
|
toolset="terminal",
|
|
schema=PROCESS_SCHEMA,
|
|
handler=_handle_process,
|
|
emoji="⚙️",
|
|
deadline_floor=process_deadline_floor,
|
|
)
|