Skip to content

feat(mcp): watch and control agent sessions from the personal MCP - #77

Merged
artempartos merged 1 commit into
developfrom
feat/session-observability-mcp-tools
Aug 10, 2026
Merged

artempartos merged 1 commit into
developfrom
feat/session-observability-mcp-tools

Conversation

@artempartos

Copy link
Copy Markdown
Contributor

Why

A session's terminal is only readable after it ends: logs are collected in the container workflow's cleanup phase, and Api::V1::TerminalSessionsController#terminal_log deliberately 404s anything not finished/failed. The personal MCP had no session tool at all — so an agent could not tell a working session from a wedged one, and could not do anything about one that was stuck.

Research: docs/research/technical-agent-session-log-access-and-control-research-2026-08-10.md.
Spec: docs/implementation-artifacts/spec-session-observability-mcp-tools.md.

What

Four personal-MCP tools, over seams that already existed:

Tool Does
list_sessions Sessions the caller may see (own + reachable projects, filtered by the owner's sharing preferences), newest first, active by default
get_session_log Tail of one session's terminal — live via tmux capture-pane while the container is up, the stored terminal_output.log once it is over — plus idle_seconds, last_output_at and a QuotaErrorDetector verdict
stop_session Graceful SessionService.finish, or force → fail_session (what unblocks a workflow step waiting on a wedged agent)
trigger_task_workflow The board card's Run workflow button. force: true cancels the in-flight run first

Supporting changes:

  • Sessions::LiveLogReader — one exec that returns the log's mtime and the pane, so a read costs a single pod-exec upgrade. Same read ScanQuotaErrorsActivity has always done, factored out so the two cannot drift.
  • TerminalSession.readable_by(user) — the reachability half of the read rule, now shared with TerminalSessionsController#find_readable_session. visible_to? is still applied per row.
  • FakeRuntime gains set_terminal_pane / unreachable_on_exec, so the live path is tested through the blessed container seam rather than per-test stubs.

Decisions worth reviewing

  • Who may stop: anyone who may see the session (product decision), with one carve-out — a read-only (viewer) member cannot, since the viewer role exists to prevent writes.
  • No redaction: log content is returned as-is. Bounded, not filtered: 200 lines by default (cap 2000), and a stored log is read from a 256 KB tail window with a truncated flag.
  • ANSI: a live capture-pane read is already plain text; a stored log is stripped by default, raw: true returns the bytes verbatim.
  • Writes route through the services only — SessionService / WorkflowService / TaskService. A hand-rolled session.fail! leaves the pod, Service, IngressRoute and Middlewares behind (see session_service.rb:66-85).

Verified

  • docker compose exec -T web make check_all — all green (rails-test, rubocop, brakeman, system-test, eslint, typescript, fe-test, vite-build); backend line coverage 91.46%.
  • Cancel-then-retrigger was traced end to end before relying on it: WorkflowService#cancel_active_step_runs → SessionService.cancel → Temporal cancellation → ContainerWorkflow#run_cleanup_detached (a fresh Cancellation, so the cleanup activity still runs) → pod reclaimed and the terminal log collected. The force test exercises the real WorkflowService, not a mock of it.

Noted, not fixed (pre-existing)

WorkflowService.cancel records a :workflow_cancelled board activity, but BoardActivity's event_type enum has no such value — the write fails validation and is swallowed by the rescue, so cancellations never appear on the board timeline. Out of scope here.

🤖 Generated with Claude Code

A session's terminal was only readable after it ended: logs are collected
during the container workflow's cleanup phase, and the serving endpoint
404s anything that is not finished or failed. So an agent could not tell a
working session from a wedged one, and could not do anything about it.

Adds four personal-MCP tools over the seams that already existed:
list_sessions, get_session_log (live `tmux capture-pane` read while the
container is up, stored log once it is over, plus idle time and a quota
verdict), stop_session and trigger_task_workflow — the last being the board
card's Run workflow button, with a force that cancels the in-flight run
first.

Reads are gated by TerminalSession.readable_by + visible_to?, extracted so
the tool and Api::V1::TerminalSessionsController share one rule. Writes go
through SessionService/WorkflowService/TaskService only: a hand-rolled
transition leaves the pod, Service and IngressRoute behind.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Signed-off-by: Artem Petrov <[email protected]>
@artempartos
artempartos merged commit 0309092 into develop Aug 10, 2026
7 checks passed
alex-astafyev pushed a commit to alex-astafyev/flow that referenced this pull request Aug 19, 2026
* origin/develop:
  feat(mcp): watch and control agent sessions from the personal MCP (AixleHQ#77)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant