While the sidecar's Stop guard holds a run's ended turn for a background
shell (a deploy, a build), the hub let the 10-min recency window flip the
card to "finished", the result still to come (conv 01a1b0cf: "finished ·
ended 20m ago" while its process sat in the hold).
- sidecar /sessions: each live run reports `held` ({since, tasks}) from
the guard's <sid>.hold file.
- core.liveruns keeps it per runner next to liveness (held(sid),
held_snapshot() for the list's cache key); the exit watcher feeds it.
- _resolve_state: a live run that is held reads running; the meta carries
`held` (schema HeldTurn, regenerated types).
- Cards: a "Waiting on a background shell · N min" chip next to
Deploying…, the label truncating before the minutes on a narrow card.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
125 lines
5.4 KiB
Python
125 lines
5.4 KiB
Python
"""A run whose ended turn the runner's Stop guard holds for a background shell
|
|
(a deploy, a build) reads ``running`` — not ``finished`` once the recency
|
|
window passes — and its meta says what it waits on (``held``).
|
|
|
|
2026-11-06, conv 01a1b0cf: the session waited on its deploy from a background
|
|
shell, ended its turn, and the card read "finished · ended 20m ago" while the
|
|
process sat alive in the guard's hold.
|
|
|
|
Imports ``main`` against throwaway paths (no startup event → no threads)."""
|
|
|
|
import datetime
|
|
import json
|
|
import os
|
|
import pathlib
|
|
import tempfile
|
|
import unittest
|
|
|
|
TMP = pathlib.Path(tempfile.mkdtemp())
|
|
for k, v in {"DB_PATH": TMP / "x.db", "TRANSCRIPTS_DIR": TMP / "t",
|
|
"WORKSPACE": TMP, "REPO_DIR": TMP, "ARCHIVE_DIR": TMP / "arch",
|
|
"META_PATH": TMP / "meta.json", "WORKERS_PATH": TMP / "w.json",
|
|
"PROFILES_PATH": TMP / "profiles.json",
|
|
"WORKERS_TRANSCRIPTS_DIR": TMP / "mirror",
|
|
"CRON_PATH": TMP / "cron.json", "NOTIFY_PATH": TMP / "n.json",
|
|
"FORMS_PATH": TMP / "f.json", "UI_STATE_PATH": TMP / "ui.json",
|
|
"NOTIF_READ_PATH": TMP / "nr.json",
|
|
"MAIL_TRIGGER_SENDERS_PATH": TMP / "mt.json",
|
|
"USERS_PATH": TMP / "users.json", "AUTH_PATH": TMP / "auth.json",
|
|
"NEW_PROJECTS_PATH": TMP / "np.json", "PROJECTS_DIR": TMP / "projects",
|
|
"INTERNAL_API_TOKEN": "tok", "SIDECAR_TOKEN": "",
|
|
"PI_TRANSCRIPTS_DIR": "", "RUNNING_WINDOW_SECS": "600"}.items():
|
|
os.environ[k] = str(v)
|
|
|
|
from fastapi.testclient import TestClient # noqa: E402
|
|
|
|
import main # noqa: E402
|
|
from core import liveruns # noqa: E402
|
|
|
|
DEPS = main.app.state.ai
|
|
PROJ = "-home-x"
|
|
SID = "bbbbbbbb-0000-4000-8000-000000000001"
|
|
TU = "toolu_wait1"
|
|
|
|
|
|
def ago(secs: float) -> str:
|
|
t = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(seconds=secs)
|
|
return t.isoformat(timespec="milliseconds").replace("+00:00", "Z")
|
|
|
|
|
|
def held_conversation() -> str:
|
|
"""A run that started a background shell (its deploy wait) 30 min ago and
|
|
ended its turn on "I'll check the result when it finishes"."""
|
|
base = {"sessionId": SID}
|
|
recs = [
|
|
{**base, "type": "user", "uuid": "u1", "timestamp": ago(1810),
|
|
"message": {"role": "user", "content": "deploy it"}},
|
|
{**base, "type": "assistant", "uuid": "a1", "timestamp": ago(1805),
|
|
"message": {"role": "assistant", "id": "m1", "model": "claude-opus-5-5",
|
|
"content": [{"type": "tool_use", "id": TU, "name": "Bash",
|
|
"input": {"command": "./deploy.sh --watch",
|
|
"run_in_background": True}}]}},
|
|
{**base, "type": "user", "uuid": "u2", "timestamp": ago(1805),
|
|
"message": {"role": "user", "content": [
|
|
{"type": "tool_result", "tool_use_id": TU,
|
|
"content": "Command running in background with ID: bw1. Output is "
|
|
"being written to: /tmp/x/tasks/bw1.output."}]}},
|
|
{**base, "type": "assistant", "uuid": "a2", "timestamp": ago(1800),
|
|
"message": {"role": "assistant", "id": "m2", "model": "claude-opus-5-5",
|
|
"content": [{"type": "text",
|
|
"text": "I'll check the result when it finishes."}]}},
|
|
]
|
|
p = TMP / "arch" / PROJ / f"{SID}.jsonl"
|
|
p.parent.mkdir(parents=True, exist_ok=True)
|
|
p.write_text("\n".join(json.dumps(o) for o in recs) + "\n")
|
|
DEPS.indexer.scan_transcripts()
|
|
return f"{PROJ}/{SID}.jsonl"
|
|
|
|
|
|
class HeldTurnReadsRunning(unittest.TestCase):
|
|
def setUp(self):
|
|
self.c = TestClient(main.app, headers={"x-internal-auth": "tok"})
|
|
self.cid = held_conversation()
|
|
|
|
def tearDown(self):
|
|
liveruns.drop("w-held")
|
|
|
|
def meta(self) -> dict:
|
|
[card] = self.c.get("/api/conversations", params={"sessions": SID}).json()["conversations"]
|
|
detail = self.c.get("/api/conversation", params={"id": self.cid}).json()
|
|
self.assertEqual(card["meta"]["state"], detail["meta"]["state"])
|
|
self.assertEqual(card["meta"].get("held"), detail["meta"].get("held"))
|
|
return card["meta"]
|
|
|
|
def test_quiet_past_the_window_is_finished(self):
|
|
m = self.meta()
|
|
self.assertEqual(m["state"], "finished")
|
|
self.assertIsNone(m.get("held"))
|
|
|
|
def test_alive_but_not_held_is_not_kept_running(self):
|
|
# The process outliving its turn by itself says nothing (it is about
|
|
# to exit); only the guard's hold does.
|
|
liveruns.update("w-held", {SID})
|
|
self.assertEqual(self.meta()["state"], "finished")
|
|
|
|
def test_held_reads_running_with_what_it_waits_on(self):
|
|
hold = {"since": 1793968070.2, "tasks": ["bw1"]}
|
|
liveruns.update("w-held", {SID}, {SID: hold})
|
|
m = self.meta()
|
|
self.assertEqual(m["state"], "running")
|
|
self.assertEqual(m["held"], hold)
|
|
# the hold ends (the shell ended): back to what the transcript says
|
|
liveruns.update("w-held", {SID})
|
|
m = self.meta()
|
|
self.assertEqual(m["state"], "finished")
|
|
self.assertIsNone(m.get("held"))
|
|
|
|
def test_a_hold_reported_for_a_dead_process_is_ignored(self):
|
|
liveruns.update("w-held", set(), {SID: {"since": 1.0, "tasks": []}})
|
|
self.assertIsNone(liveruns.held(SID))
|
|
self.assertEqual(self.meta()["state"], "finished")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|