On a guest's own computer, `claude` in a terminal now asks (arrow-key box, remembered per folder tree): ai-agent Claude — the hub's proxy pair, synced — or the machine's own login, synced later on demand. - worker/claude_launcher.py + the installer's shell hook (bash/zsh/fish): the picker, --ai-agent/--local/--choose, no question for -p / pipes / known --resume, --session-id up front so the start line links the app; ai-agent-worker sync|unsync|claude. - worker/claude-mod: a Claude Code mod — status line (synced · budget left), a toast with the link, /ai-agent [sync|unsync], and Continue on ai-agent Claude when the guest's own plan runs out (the launcher resumes it there). - hub: workers/terminal.py claims a session on its first proxied call (X-Claude-Code-Session-Id) or when /feed lists it as synced; refused for an id known elsewhere; Stop syncing keeps the copy, takes no more. The probe pushes the picker's figures (budget left, paused) to the computer. - portal: My computers → Terminal sessions (Sync / Stop syncing, Ask at start); the Terminal badge; continuing a local one confirms the move to the host's Claude and sends on a fresh context. Form choose-claude-at-terminal-start-sync-to-the-app_n789ku. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
151 lines
6.2 KiB
Python
151 lines
6.2 KiB
Python
"""
|
|
The transcript feed — how a hub mirrors a *remote* runner's transcripts.
|
|
|
|
On the homelab the backend reads the host's transcripts through a read-only
|
|
mount. A worker on another machine (the Orus MacBook) has no such mount: the
|
|
hub **pulls** instead, over the tailnet, with two bearer-guarded GETs:
|
|
|
|
GET /feed?since=<epoch> every transcript (``*.jsonl``, subagents
|
|
included) and subagent ``*.meta.json`` under the
|
|
default account's ``projects/`` whose mtime is
|
|
≥ ``since`` — ``{rel, mtime, size}`` each
|
|
GET /feed/file?rel=&offset= the file's bytes from ``offset`` (at most
|
|
``limit``), with its current size and mtime in
|
|
``X-Feed-Size`` / ``X-Feed-Mtime``
|
|
|
|
That is the whole protocol (plus ``synced``, the terminal sessions the
|
|
machine's user chose to share — ``terminal.py``): the hub appends what grew and re-downloads a file
|
|
that shrank (fork surgery rewrites one). The worker never calls the hub — it
|
|
doesn't know where the hub is, and doesn't need to.
|
|
|
|
**Extra roots.** The opencode harness's mirrored transcripts live outside
|
|
``projects/`` (one dir per opencode account): they are listed under a ``rel``
|
|
prefix — ``@opencode/<account>/<slug>/<uuid>.jsonl`` — which ``/feed/file``
|
|
maps back to that root. The hub strips the prefix into the same layout and
|
|
stamps the sessions with that account.
|
|
|
|
``rel`` is always relative to the projects dir and must name a ``.jsonl`` or
|
|
``.meta.json`` inside it; anything else (``..``, an absolute path, a symlink
|
|
out, another suffix) is a 404, so the feed can't be turned into a file reader
|
|
for the rest of the laptop.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import pathlib
|
|
import time
|
|
from typing import Callable
|
|
|
|
from fastapi import APIRouter, Header, HTTPException, Query, Response
|
|
|
|
SUFFIXES = (".jsonl", ".meta.json")
|
|
# One response's worth of bytes. The hub loops until it has caught up, so this
|
|
# only bounds memory on both ends (a long session's transcript is tens of MB).
|
|
MAX_CHUNK = 8 * 1024 * 1024
|
|
|
|
|
|
def list_files(root: pathlib.Path, since: float = 0.0) -> list[dict]:
|
|
"""Every feed file under ``root`` with mtime ≥ ``since`` (scandir walk —
|
|
the hub polls this every couple of seconds)."""
|
|
out: list[dict] = []
|
|
if not root.is_dir():
|
|
return out
|
|
base = str(root)
|
|
stack = [base]
|
|
while stack:
|
|
d = stack.pop()
|
|
try:
|
|
it = os.scandir(d)
|
|
except OSError:
|
|
continue
|
|
with it:
|
|
for e in it:
|
|
try:
|
|
if e.is_dir(follow_symlinks=False):
|
|
stack.append(e.path)
|
|
continue
|
|
if not e.name.endswith(SUFFIXES) or \
|
|
not e.is_file(follow_symlinks=False):
|
|
continue
|
|
st = e.stat(follow_symlinks=False)
|
|
except OSError:
|
|
continue
|
|
if st.st_mtime >= since:
|
|
out.append({"rel": os.path.relpath(e.path, base)
|
|
.replace(os.sep, "/"),
|
|
"mtime": st.st_mtime, "size": st.st_size})
|
|
return out
|
|
|
|
|
|
def resolve(root: pathlib.Path, rel: str) -> pathlib.Path:
|
|
"""``rel`` → a file inside ``root``, or 404 (traversal, wrong suffix,
|
|
missing)."""
|
|
rel = (rel or "").strip()
|
|
if not rel or rel.startswith("/") or "\\" in rel or "\0" in rel \
|
|
or not rel.endswith(SUFFIXES) \
|
|
or any(p in ("", ".", "..") for p in rel.split("/")):
|
|
raise HTTPException(404, "not a feed file")
|
|
try:
|
|
real_root = root.resolve()
|
|
p = (root / rel).resolve()
|
|
p.relative_to(real_root)
|
|
except (OSError, ValueError):
|
|
raise HTTPException(404, "not a feed file")
|
|
if not p.is_file():
|
|
raise HTTPException(404, "not a feed file")
|
|
return p
|
|
|
|
|
|
def read_chunk(p: pathlib.Path, offset: int, limit: int) -> tuple[bytes, os.stat_result]:
|
|
with open(p, "rb") as f:
|
|
st = os.fstat(f.fileno())
|
|
f.seek(max(0, offset))
|
|
return f.read(max(0, min(limit, MAX_CHUNK))), st
|
|
|
|
|
|
def _split(rel: str, extra: dict[str, pathlib.Path]) -> tuple[pathlib.Path | None, str]:
|
|
"""``(root, rel inside it)`` for a prefixed ``rel`` (None ⇒ projects/)."""
|
|
for prefix, root in extra.items():
|
|
if rel.startswith(prefix + "/"):
|
|
return root, rel[len(prefix) + 1:]
|
|
return None, rel
|
|
|
|
|
|
def make_router(auth: Callable[[str | None], None],
|
|
projects_dir: Callable[[], pathlib.Path],
|
|
extra_roots: Callable[[], dict[str, pathlib.Path]] | None = None,
|
|
synced: Callable[[], list[str]] | None = None
|
|
) -> APIRouter:
|
|
r = APIRouter()
|
|
|
|
@r.get("/feed")
|
|
def feed(since: float = Query(0.0),
|
|
authorization: str | None = Header(default=None)) -> dict:
|
|
auth(authorization)
|
|
now = time.time() # taken first: a file touched mid-walk is re-listed
|
|
root = projects_dir()
|
|
files = list_files(root, since)
|
|
for prefix, extra in (extra_roots() if extra_roots else {}).items():
|
|
files += [{**f, "rel": f"{prefix}/{f['rel']}"} for f in list_files(extra, since)]
|
|
# The terminal sessions the guest chose to share (terminal.py) — a
|
|
# guest computer's hub mirrors those besides its own runs.
|
|
return {"now": now, "root": str(root), "files": files,
|
|
**({"synced": synced()} if synced else {})}
|
|
|
|
@r.get("/feed/file")
|
|
def feed_file(rel: str = Query(...), offset: int = Query(0, ge=0),
|
|
limit: int = Query(MAX_CHUNK, ge=1),
|
|
authorization: str | None = Header(default=None)) -> Response:
|
|
auth(authorization)
|
|
root, inner = _split(rel or "", extra_roots() if extra_roots else {})
|
|
p = resolve(root or projects_dir(), inner)
|
|
try:
|
|
data, st = read_chunk(p, offset, limit)
|
|
except OSError:
|
|
raise HTTPException(404, "not a feed file")
|
|
return Response(data, media_type="application/octet-stream", headers={
|
|
"X-Feed-Size": str(st.st_size), "X-Feed-Mtime": repr(st.st_mtime)})
|
|
|
|
return r
|