2code icon indicating copy to clipboard operation
2code copied to clipboard

Task 13: Drive agent indicators from Herdr

Open AkaraChen opened this issue 2 months ago • 0 comments

Summary

Replace local screen/OSC detection with server-provided agent state for Herdr-owned terminals, as required by parent plan #394. Map Herdr identity and lifecycle into the existing badges, waiting notifications, completion dots, and sidebar consumers. Stop invoking the local detector on Herdr terminals. Define unknown-state, viewed-completion, and reconnect behavior so duplicate notifications cannot fire. Do not switch the default runtime, delete the detector, or let Local detection and Herdr state both drive the same session.

Pin remains Herdr v0.9.0, JSON protocol 22, endpoint generation 1. Verified on empty shells: pane.agent_status is "unknown" and snapshot.agents is empty (docs/herdr-integration.md Later-task capabilities; herdr_contract.rs idle pane.get). Live working / blocked / done / idle transitions are unverified (#398). Map those documented strings from snapshot and events using fixtures; do not claim live CLI parity and do not close #398 unless a real agent CLI is exercised against this pin. Production must consume Herdr state, not write it: no pane.report_agent, pane.report_agent_session, agent.start, or agent.prompt. Namespace stays Task 4 2code. Session ownership stays per-id RuntimeRouter::backend_for, not RuntimeDiscovery.selected_backend.

Task 6 currently stores no agent fields on ProjectedPane, subscribes to pane.agent_detected but does not apply it, and excludes pane.agent_status_changed (subscribe_params_cover_lifecycle_not_output_waits). Task 11 still runs createAgentStatusDetector on Herdr frames. Task 12 reattaches the same 2code session id, so reconnect hydration must reuse that id and must not treat a still-blocked pane as a fresh waiting edge.

Dependencies: Parent plan #394. Prerequisites: #395 (Task 1, accepted on issue-395-herdr-integration-contract), #402 (Task 2, accepted on task-2-runtime-boundary), #404 (Task 3, accepted on task-3-herdr-sidecar), #406 (Task 4, accepted on task-4-server-discovery), #409 (Task 5, accepted on task-5-socket-client), #411 (Task 6, accepted on task-6-runtime-sync), #414 (Task 7, accepted on task-7-runtime-mapping), #417 (Task 8, accepted on task-8-profile-adoption), #419 (Task 9, accepted on task-9-terminal-lifecycle), #421 (Task 10, accepted on task-10-frame-bridge), #423 (Task 11, accepted on task-11-xterm-transport), #425 (Task 12, accepted on task-12-reattach-restore). Checkout base: #425 (task-12-reattach-restore). Open contract-gap issues #396–#401, docs gap #407, Task 6 event-apply gap #413, implemented Task 8/9 gaps #403/#416, and line-count trackers #400/#405/#408/#410/#412/#415/#418/#420/#422/#424/#426 do not block Linux Task 13. Live agent CLI parity remains #398 unless this task actually exercises a real agent against v0.9.0.

Work to complete

  1. Project Herdr agent state onto mapped sessions

    • Store agent_status (raw Herdr string) and optional agent identity on ProjectedPane. Seed both from session.snapshot panes (and snapshot.agents when it names the pane). Objects stay keyed by pane_id, never by display name.
    • Subscribe to pane.agent_status_changed in SUBSCRIBE_TYPES. Apply it. Also apply already-subscribed pane.agent_detected for identity. Stale generations stay IgnoredStale. Unmapped panes (no find_session_by_pane row in namespace 2code) are ignored, not bound.
    • Keep pane.output_matched unsubscribed. Do not start applying pane.exited / layout.updated / tab.moved (#413).
    • Drive this from the Herdr adapter that already has a JSON client. Do not call ensure_herdr_listener / HerdrRuntimeSync from Local default startup in src-tauri/src/lib.rs. RuntimeRouter::new stays Local-default.
    • Bridge application DTOs to the GUI (thin get_session_agent_status hydrate plus events or a Channel). Map pane_id → 2code session id before emit. Do not put wire JSON (type, underscored event envelopes, Herdr agent records) in handler signatures or model IPC types. Run bun run typegen; do not edit src/generated/ by hand.
    • Ownership is backend_for. A Local-owned id never receives Herdr agent updates. A Herdr-owned id with a missing/replaced pane fails closed (no badge, no Local detector fallback).
  2. Map into existing badges and notifications

    • Keep AgentStatus as running | waiting, with idle clearing the record and completed remaining a viewed notification. Do not add Herdr strings to the store.
    • Mapping (verified unknown; others documented by the pin/schema and #398, same vocabulary as local AgentSemanticState):
      • unknown → idle (no badge)
      • working → running
      • blocked → waiting
      • idle → idle
      • done → idle; if the previous published status for that session was running, set the existing completion notification
      • any other string → idle, no detector fallback, log once
    • Reuse setAgentStatus, shouldNotifyAgentWaiting / sendAgentWaitingNotification, waiting sound, AgentStatusDot, and sidebar useProfileAgentStatus. Agent display name comes from Herdr identity when present; otherwise the existing generic title.
    • Unknown / reconnect: hydrate from the current projection on Herdr attach (Task 12 same session id). Seed the published previous status from the store so a still-blocked pane does not re-notify or re-play the waiting sound. Snapshot replay after subscribe rebuild must not recreate a dismissed completion.
    • Viewed completions: keep dismiss-on-click. done / idle after running creates completion once. Waiting → idle still does not create completion.
    • Duplicate notifications: waiting only on a true edge (previousStatus !== "waiting"), including GUI reconnect, stream reattach, and hidden-tab frame traffic. Tests must prove reconnect does not notify twice.
  3. Stop the local detector on Herdr terminals only

    • In Terminal.tsx, the Herdr transport path must not construct createAgentStatusDetector, schedule detection, or pass scheduleAgentDetection into applyHerdrFrameAction. Do not parse xterm screen, OSC title, or OSC progress for Herdr-owned ids.
    • Local-owned ids keep today’s detector, sound, and notification path unchanged.
    • Running both detectors on one session is dual ownership of agent lifecycle — forbidden.
    • Do not delete src/features/terminal/detector/ (Task 22). Do not call --takeover. Do not attach observe and control together.
  4. Prove with tests

    • Projection: snapshot seeds unknown as idle; working / blocked / done / idle events update the mapped session; stale generation ignored; unmapped pane_id ignored; pane.agent_status_changed is subscribed.
    • Frontend: Herdr-owned Terminal does not call the detector; Local-owned still publishes from title/screen fixtures (existing Terminal.test.tsx waiting-title case stays).
    • Notifications: blocked → waiting notifies once when unfocused; reconnect/hydrate of the same session id does not notify again; running → idle/done creates completion once and does not recreate it after snapshot replay if dismissed.
    • Selector: Local-owned id never consumes Herdr agent IPC; Herdr-owned id never runs the detector and never falls back to Local PTY parsing.
    • RuntimeRouter::new still selects Local. Existing Local PTY, Task 11 transport, and Task 12 reattach tests keep passing.
    • Optional live agent CLI probe: skip unless an explicit required flag and a real agent binary are present. Never target the user default Herdr session. Do not close #398 from fixtures or pane.report_agent injection alone.
    • bun run lint:check, bun run typecheck, bun run test. If Rust changes: cargo test --workspace from src-tauri (or --exclude code where GTK is unavailable). IPC changes use bun run typegen.

Likely files and directories

  • Update: src-tauri/crates/service/src/runtime_sync.rs — pane agent fields, subscribe pane.agent_status_changed, apply status/identity events
  • Update: src-tauri/crates/service/src/runtime/herdr.rs, src-tauri/src/bridge.rs, src-tauri/src/handler/pty.rs — map pane → session, hydrate IPC, emit application DTOs; Local default startup unchanged
  • Update if needed: src-tauri/crates/model/src/runtime.rs — application agent DTO only (no wire types); src-tauri/crates/repo/src/runtime_mapping.rs already has find_session_by_pane
  • Update: src/features/terminal/Terminal.tsx, src/features/terminal/Terminal.test.tsx, src/features/terminal/lib/agentNotification.ts / tests, src/features/terminal/store.ts only if reconnect/completion seeding needs a helper
  • Reuse, do not duplicate: AgentStatusDot.tsx, TerminalTabs.tsx, sidebar ProfileItem.tsx / ProjectMenuItem.tsx, Task 7 session↔pane mapping, Task 12 same-id reattach
  • Do not rewrite: Local PTY read loop, Task 10 CLI attach, Task 11 frame semantics, detector/rules/ (Task 22), docs/herdr-integration.md

Estimated changes: 200–400 lines total, additions plus deletions, including tests; excluding generated files and lockfile churn. Material scope growth should become a separate issue.

Acceptance criteria

  • [ ] Herdr-owned sessions take agent badges, waiting notifications, waiting sound, and completion dots from projected Herdr agent_status / identity, keyed by persisted pane_id → 2code session id. unknown is idle.
  • [ ] Documented working / blocked / idle / done map to existing running / waiting / idle / completion behavior. Unrecognized statuses are idle with no detector fallback.
  • [ ] Herdr terminals do not run createAgentStatusDetector or parse xterm/OSC for lifecycle. Local-owned terminals still do.
  • [ ] Reattach and snapshot rebuild do not duplicate waiting notifications or recreate a dismissed completion for the same session id.
  • [ ] Default runtime remains Local. Local PTY/agent path still works. No 2code session or worktree is owned by both backends. Agent updates are not started from Local default startup. Adapter choice uses per-session backend_for.
  • [ ] Production UI does not call pane.report_agent, pane.report_agent_session, agent.start, agent.prompt, pane.send_*, server.stop, --takeover, or a reimplemented herdr-client.sock protocol.
  • [ ] Live CLI detection parity is not claimed (#398) unless a real agent was exercised against v0.9.0. Windows live attach is not claimed (#396). Detector deletion is not this task.
  • [ ] bun run lint:check, bun run typecheck, and bun run test pass. If Rust changes: cargo test --workspace from src-tauri (or --exclude code where GTK is unavailable). bun run typegen for IPC; do not edit src/generated/ by hand.

Out of scope

  • Creating profiles through Herdr worktree.create (Task 14) or routing worktree/project cleanup through Herdr (Task 15).
  • Git/editor path reconciliation (Task 16), startup-command mapping (Task 17 / #397), runtime health UI (Task 18), legacy-session migration (Task 19), or making Herdr the default (Task 21).
  • Deleting detector/ (Task 22) or the local PTY runtime (Task 23).
  • Closing or implementing contract-gap issues #396–#401 except that this task may add fixture-only mapping for #398 without claiming live parity. Do not close #398 from fixtures or pane.report_agent injection.
  • Docs gap #407, Task 6 event-apply gap #413, implemented gaps #403/#416, or line-count trackers #400/#405/#408/#410/#412/#415/#418/#420/#422/#424/#426.
  • Applying pane.exited / layout.updated / tab.moved in the projection (#413).
  • Updating docs/herdr-integration.md.
  • Changing Task 10 CLI attach, Task 11 frame/full/seq semantics, or Task 12 reattach/history policy.
  • Remote Herdr operation.

Standing constraints

  • No default-runtime switch.
  • Existing Local PTY/agent path stays working until the Herdr path passes acceptance.
  • Never let both runtimes own the same terminal or worktree.

AkaraChen avatar Sep 15 '26 16:09 AkaraChen