Show activity indicators for terminals and their workspaces (#1507)
* feat: show activity indicators for terminals and their workspaces A terminal surfaces whether the agent running inside it is working, idle, or waiting for input as a tab dot, and that state rolls up into the owning workspace status — so an agent run inside a terminal reads the same as a native agent. Finishing a turn raises a "Terminal finished" notification that routes back to the workspace terminal tab. Activity is reported by the agents, not parsed from terminal output: Claude and Codex command hooks and an OpenCode plugin post coarse state to a local daemon endpoint, and a source-agnostic per-terminal tracker maps it to working/idle/attention. Hook installation is opt-in and off by default because it edits your real agent config files; enable it under a host's Terminal settings and Paseo installs the hooks globally, runs them via `paseo hooks <agent> <event>`, and removes its own hooks when you opt back out. Installation and reporting are idempotent and fail-open, so a missing or failing hook never breaks the user's shell. * Fix terminal activity routing and CI test Test drift: anchor the provider-leak test to the repo root instead of process.cwd() so it passes under the server workspace CI runner. Also restore auth-first JSON parsing outside the terminal-activity route and route subdirectory terminal activity to the deepest active workspace. * Fix workspace resolver regression Code drift: terminal activity routing needs parent-workspace prefix matching, but agent directory and historical fetches keep exact registered workspace semantics, including archived exact matches. * Narrow terminal/draft workspace subscriptions to the fields they use terminal-panel and the draft agent tab subscribed to the whole workspace descriptor via useWorkspace but read only workspaceDirectory (plus projectKind in TerminalPanel, plus id in the draft tab), so they re-rendered on any field change of that workspace (status, diffStat, scripts). Switch them to the existing useWorkspaceDirectory / useWorkspaceFields hooks so they re-render only when the fields they actually read change. No behavior change. * Make TerminalActivity.state forward-compatible A strict z.enum rejects the whole payload when a newer daemon reports a terminal activity state this client predates, breaking the protocol's old-client-parses-new-daemon contract (the activity field has no .catch at its embedding in messages.ts, so the failure bubbles up). Degrade unknown states to "idle" (no indicator, no notification) via .catch, matching the enum-with-catch pattern used elsewhere in the protocol. Add a forward-compat regression test.
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { TerminalActivityTracker } from "./terminal-activity-tracker.js";
|
||||
import type { TerminalActivitySnapshot } from "./terminal-activity-tracker.js";
|
||||
|
||||
describe("TerminalActivityTracker — initial state", () => {
|
||||
it("starts as unknown", () => {
|
||||
const tracker = new TerminalActivityTracker();
|
||||
|
||||
expect(tracker.getSnapshot().state).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("TerminalActivityTracker — set", () => {
|
||||
it("updates the snapshot state", () => {
|
||||
const tracker = new TerminalActivityTracker();
|
||||
|
||||
tracker.set("working");
|
||||
|
||||
expect(tracker.getSnapshot().state).toBe("working");
|
||||
});
|
||||
});
|
||||
|
||||
describe("TerminalActivityTracker — clearAttention", () => {
|
||||
it("moves attention back to idle", () => {
|
||||
const tracker = new TerminalActivityTracker();
|
||||
|
||||
tracker.set("attention");
|
||||
|
||||
expect(tracker.clearAttention()).toBe(true);
|
||||
expect(tracker.getSnapshot().state).toBe("idle");
|
||||
});
|
||||
|
||||
it("leaves non-attention states unchanged", () => {
|
||||
const tracker = new TerminalActivityTracker();
|
||||
|
||||
tracker.set("working");
|
||||
|
||||
expect(tracker.clearAttention()).toBe(false);
|
||||
expect(tracker.getSnapshot().state).toBe("working");
|
||||
});
|
||||
});
|
||||
|
||||
describe("TerminalActivityTracker — onChange listener", () => {
|
||||
it("fires when state changes", () => {
|
||||
const tracker = new TerminalActivityTracker();
|
||||
const changes: TerminalActivitySnapshot[] = [];
|
||||
tracker.onChange((snap) => changes.push(snap));
|
||||
|
||||
tracker.set("working");
|
||||
|
||||
expect(changes).toHaveLength(1);
|
||||
expect(changes[0].state).toBe("working");
|
||||
});
|
||||
|
||||
it("does not fire when state stays the same", () => {
|
||||
const tracker = new TerminalActivityTracker();
|
||||
const changes: TerminalActivitySnapshot[] = [];
|
||||
tracker.onChange((snap) => changes.push(snap));
|
||||
|
||||
tracker.clear();
|
||||
|
||||
expect(changes).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("fires when state clears to unknown", () => {
|
||||
const tracker = new TerminalActivityTracker();
|
||||
const changes: TerminalActivitySnapshot[] = [];
|
||||
tracker.onChange((snap) => changes.push(snap));
|
||||
|
||||
tracker.set("working");
|
||||
tracker.clear();
|
||||
|
||||
expect(changes.map((change) => change.state)).toEqual(["working", null]);
|
||||
});
|
||||
|
||||
it("listener can be unsubscribed", () => {
|
||||
const tracker = new TerminalActivityTracker();
|
||||
const changes: TerminalActivitySnapshot[] = [];
|
||||
const off = tracker.onChange((snap) => changes.push(snap));
|
||||
|
||||
tracker.set("working");
|
||||
expect(changes).toHaveLength(1);
|
||||
|
||||
off();
|
||||
tracker.set("idle");
|
||||
|
||||
expect(changes).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("delivers the previous snapshot alongside each transition", () => {
|
||||
const tracker = new TerminalActivityTracker();
|
||||
const transitions: Array<{
|
||||
snapshot: TerminalActivitySnapshot;
|
||||
previous: TerminalActivitySnapshot;
|
||||
}> = [];
|
||||
tracker.onChange((snapshot, previous) => transitions.push({ snapshot, previous }));
|
||||
|
||||
tracker.set("working");
|
||||
tracker.set("idle");
|
||||
|
||||
expect(transitions).toHaveLength(2);
|
||||
expect(transitions[0].previous.state).toBeNull();
|
||||
expect(transitions[0].snapshot.state).toBe("working");
|
||||
expect(transitions[1].previous).toEqual(transitions[0].snapshot);
|
||||
expect(transitions[1].snapshot.state).toBe("idle");
|
||||
});
|
||||
});
|
||||
|
||||
describe("TerminalActivityTracker — dispose", () => {
|
||||
it("removes listeners", () => {
|
||||
const tracker = new TerminalActivityTracker();
|
||||
const listener = vi.fn();
|
||||
tracker.onChange(listener);
|
||||
|
||||
tracker.dispose();
|
||||
tracker.set("working");
|
||||
|
||||
expect(listener).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,67 @@
|
||||
import type { TerminalActivityState } from "@getpaseo/protocol/terminal-activity";
|
||||
|
||||
export interface TerminalActivitySnapshot {
|
||||
state: TerminalActivityState | null;
|
||||
changedAt: number;
|
||||
}
|
||||
|
||||
export class TerminalActivityTracker {
|
||||
// unknown != idle: a plain shell, or a terminal whose agent was killed, has no dot or rollup.
|
||||
private resolvedState: TerminalActivityState | null = null;
|
||||
private changedAt = Date.now();
|
||||
|
||||
private readonly changeListeners = new Set<
|
||||
(snapshot: TerminalActivitySnapshot, previous: TerminalActivitySnapshot) => void
|
||||
>();
|
||||
|
||||
set(state: TerminalActivityState): void {
|
||||
this.setState(state);
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
this.setState(null);
|
||||
}
|
||||
|
||||
clearAttention(): boolean {
|
||||
if (this.resolvedState !== "attention") {
|
||||
return false;
|
||||
}
|
||||
this.setState("idle");
|
||||
return true;
|
||||
}
|
||||
|
||||
private setState(state: TerminalActivityState | null): void {
|
||||
if (state === this.resolvedState) {
|
||||
return;
|
||||
}
|
||||
|
||||
const previous = this.getSnapshot();
|
||||
this.resolvedState = state;
|
||||
this.changedAt = Date.now();
|
||||
|
||||
const snapshot = this.getSnapshot();
|
||||
for (const listener of Array.from(this.changeListeners)) {
|
||||
listener(snapshot, previous);
|
||||
}
|
||||
}
|
||||
|
||||
onChange(
|
||||
listener: (snapshot: TerminalActivitySnapshot, previous: TerminalActivitySnapshot) => void,
|
||||
): () => void {
|
||||
this.changeListeners.add(listener);
|
||||
return () => {
|
||||
this.changeListeners.delete(listener);
|
||||
};
|
||||
}
|
||||
|
||||
getSnapshot(): TerminalActivitySnapshot {
|
||||
return {
|
||||
state: this.resolvedState,
|
||||
changedAt: this.changedAt,
|
||||
};
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
this.changeListeners.clear();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user