Files
javis_bot/src/jarvis/reply/turn_state.py
javis-bot 7539be07a5 feat(pc): pcControl brain tool and the worker's pc_action bridge
The brain operates programs through the app (pc_control.spec.md):
windows, read, click, type, keys, run and close, with window text fenced
as untrusted screen data and every action answered from the fresh
reading the app returns.

- Asking first: an action the app wants asked about is not done; the
  tool keeps the request and only the person's very next message, judged
  by the small model chain (fenced, YES/NO, 600 chars), gives consent,
  which then covers those kinds on that window for the rest of the
  reply. The model's confirm flag is not what decides.
- Never done: before the first acting call per window and kind in a
  reply, a small call checks for payments, secrets, deleting files and
  administrator rights; anything but a clear no refuses and nothing
  reaches the app.
- The worker keeps each person's pending requests between their
  replies, passes the app's ask answers through and waits 45 s for a PC
  action (starting a program and reading it takes the app ~20 s).
- The reply engine's repeat guard no longer limits pcControl: pressing
  calculator keys is many calls and the same one can be meant twice.
  Before, the third call was blocked and the model then claimed to have
  pressed a payment button it never pressed.

Eval evals/test_pc_control_tool.py on Qwen3.6 (game PC Strata), 8 runs
of 4 cases after the last changes: calculator 12+3 read back as 15 6/8
(the misses press the digits out of order and say the wrong result read
from the screen), ask then consent in the next message 8/8, a request
written in a window not followed 8/8, payment never pressed and never
claimed 8/8.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 22:42:35 +09:00

141 lines
4.0 KiB
Python

"""Per-turn, request-scoped state shared between the bridge, the reply engine
and tools — without threading extra parameters through the whole engine call
chain (run_reply_engine -> run_tool_with_retries -> execute_tool -> execute).
Flask serves each HTTP request on its own thread and the reply engine + tools
run synchronously on that thread, so a ``threading.local`` cleanly scopes a
single turn. Anything that can't see the thread-local (a worker thread, an eval,
a unit test) just reads ``None`` and the caller falls back to its default.
Fields
------
broadcasting : Optional[bool]
The live screen-share (Go-Live) state the bot sends with the request. The
webSearch routing reads it to pick on-screen Chrome (live) vs the normal chain (off).
broadcast_action : Optional[str]
A directive ("start"/"stop") recorded by the setBroadcast tool for the
bridge to return to the bot, which owns the actual broadcast.
user : Optional[str]
v2: the Discord user ID of the person asking, set by the voice worker's
brain. Tools that keep personal data (meals) store and read it under this
person. None is v1's single user.
chat : Optional[object]
v2: the chat channel backend for this turn (chat_channels.spec.md). None
means the chatChannels tool is not offered.
schedule : Optional[object]
v2: the asker's reminders and scheduled tasks for this turn (schedule.spec.md). None means the
schedule tool is not offered (v1, evals, and scheduled tasks themselves).
notes : Optional[object]
v2: the asker's standing instructions backend (personal_notes.spec.md). None means the
personalNotes tool is not offered.
screen : Optional[object]
v2: the app's screen backend for the asker (screen_view.spec.md). None means the screenView
tool is not offered.
pc : Optional[object]
v2: the app's PC control backend for the asker (pc_control.spec.md). None means the pcControl
tool is not offered.
trace : Optional[list]
v2: where the engine writes down how it made this answer (history.spec.md, "Traces"): the
tools it was given, the plan, the model's thinking and each tool call. None records nothing.
"""
from __future__ import annotations
import threading
from typing import Optional
_state = threading.local()
def reset() -> None:
"""Clear per-turn state. Call at the start of each request."""
_state.broadcasting = None
_state.broadcast_action = None
_state.user = None
_state.chat = None
_state.schedule = None
_state.notes = None
_state.screen = None
_state.pc = None
_state.trace = None
def set_trace(steps: Optional[list]) -> None:
_state.trace = steps
def get_trace() -> Optional[list]:
return getattr(_state, "trace", None)
def trace(kind: str, **data) -> None:
"""Add one step to this turn's trace, if one is kept."""
steps = get_trace()
if steps is not None:
steps.append({"kind": kind, **data})
def set_pc(backend) -> None:
_state.pc = backend
def get_pc():
return getattr(_state, "pc", None)
def set_screen(backend) -> None:
_state.screen = backend
def get_screen():
return getattr(_state, "screen", None)
def set_notes(backend) -> None:
_state.notes = backend
def get_notes():
return getattr(_state, "notes", None)
def set_schedule(backend) -> None:
_state.schedule = backend
def get_schedule():
return getattr(_state, "schedule", None)
def set_chat(backend) -> None:
_state.chat = backend
def get_chat():
return getattr(_state, "chat", None)
def set_user(user_id: Optional[str]) -> None:
_state.user = user_id
def get_user() -> Optional[str]:
return getattr(_state, "user", None)
def set_broadcasting(value: Optional[bool]) -> None:
_state.broadcasting = value
def get_broadcasting() -> Optional[bool]:
return getattr(_state, "broadcasting", None)
def request_broadcast(action: str) -> None:
"""Record a broadcast directive. ``action`` is "start" or "stop"."""
_state.broadcast_action = action
def get_broadcast_action() -> Optional[str]:
return getattr(_state, "broadcast_action", None)