Merge branch 'terminal-activity-from-output'

This commit is contained in:
James Babcock committed 2026-08-30 21:27:15 -07:00
commit 9fbb327e31
2 files changed
+122

No files matched your search

+90
View File
@@ -274,6 +274,18 @@ let controlsInitialized = false;
// created, so they get "now".
const sessionRecency = new Map<string, number>();
// When each session's app last produced output, as LOCAL receive time of the
// server's `activity` push (phone clock — the wire timestamp is host-clock
// and can't be compared against Date.now() across machines). Keys are
// recencyKey(connectionId, socket). A session is "active" (hub rows show an
// animated indicator) while an activity push arrived within the last
// ACTIVITY_ACTIVE_MS; the wrapper reports at most one per 2s while output
// continues, so 5s of slack keeps the indicator lit through the gaps.
const sessionActivity = new Map<string, number>();
const ACTIVITY_ACTIVE_MS = 5_000;
// Alternation period of the hub's activity indicator.
const HUB_ANIMATION_STEP_MS = 800;
function recencyKey(connectionId: string, socket: string): string {
return `${connectionId}\n${socket}`;
}
@@ -343,6 +355,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
}
if (window.foreground && window.kind === "hub") window.sessionOrder = [];
if (window.foreground) renderAndSubmit(window, 0);
updateHubAnimation();
break;
}
case "screen":
@@ -356,6 +369,7 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
renderAndSubmit(window, 0);
}
}
updateHubAnimation();
break;
case "tool-call": {
const callId = message.callId;
@@ -568,6 +582,9 @@ function startControl(control: ControlConnection): void {
client.onTitle((socket) => {
noteSessionUpdated(connectionId, socket);
}),
client.onActivity((socket) => {
noteSessionActivity(connectionId, socket);
}),
);
client.start();
}
@@ -676,6 +693,63 @@ function renderHubWindows(): void {
}
}
// Hub activity animation: while a foregrounded hub lists at least one active
// session, a timer re-renders it so the per-row indicator alternates. The
// timer stops itself once every session's activity ages out (its final tick
// renders the rows indicator-free) or the hub leaves the foreground.
let hubAnimationPhase = 0;
let hubAnimationTimer: ReturnType<typeof setInterval> | null = null;
function isSessionActive(connectionId: string, socket: string): boolean {
const at = sessionActivity.get(recencyKey(connectionId, socket));
return at !== undefined && Date.now() - at < ACTIVITY_ACTIVE_MS;
}
function hubAnimationShouldRun(): boolean {
if (!screenOn) return false;
let hubVisible = false;
for (const window of windows.values()) {
if (window.kind === "hub" && window.foreground) hubVisible = true;
}
if (!hubVisible) return false;
const now = Date.now();
for (const at of sessionActivity.values()) {
if (now - at < ACTIVITY_ACTIVE_MS) return true;
}
return false;
}
/** Start or stop the animation timer to match the current state. */
function updateHubAnimation(): void {
if (hubAnimationShouldRun()) {
if (hubAnimationTimer) return;
hubAnimationTimer = setInterval(() => {
hubAnimationPhase = (hubAnimationPhase + 1) % 2;
if (!hubAnimationShouldRun() && hubAnimationTimer) {
clearInterval(hubAnimationTimer);
hubAnimationTimer = null;
}
// Render even on the stopping tick, to clear expired indicators.
renderHubWindows();
}, HUB_ANIMATION_STEP_MS);
} else if (hubAnimationTimer) {
clearInterval(hubAnimationTimer);
hubAnimationTimer = null;
}
}
/** An activity push arrived for a session: mark it and (re)start the animation. */
function noteSessionActivity(connectionId: string, socket: string): void {
const key = recencyKey(connectionId, socket);
const wasActive = isSessionActive(connectionId, socket);
sessionActivity.set(key, Date.now());
sessionRecency.set(key, Date.now());
updateHubAnimation();
// A session just turned active: show its indicator now rather than up to
// one animation step later.
if (!wasActive) renderHubWindows();
}
function createViewWindow(
windowId: string,
surfaceId: string,
@@ -1000,6 +1074,8 @@ type HubItem = {
label: string;
/** Group heading (host name): drawn differently and skipped by selection. */
heading?: boolean;
/** Session with recent output: an animated indicator marks the row. */
active?: boolean;
onSelect?: () => void;
};
@@ -1068,6 +1144,7 @@ function hubSessionItems(window: HubWindow): HubItem[] {
const openWindowId = viewWindowIdForSocket(control.config.id, session.socket);
items.push({
label: openWindowId ? `${sessionLabel(session)} [open]` : sessionLabel(session),
active: isSessionActive(control.config.id, session.socket),
onSelect: () => {
const windowId = viewWindowIdForSocket(control.config.id, session.socket);
if (windowId) {
@@ -1425,6 +1502,19 @@ function paintHub(window: HubWindow): GrayImage {
drawSelectionHighlight(image, 20, y - 2, window.viewportWidth - 40, hubRowH - 1, window.focused, 8);
}
image.drawText(chromeFont(), 32, y + 2, item.label, selected ? 255 : 200);
if (item.active) {
// Activity indicator in the gutter left of the label, alternating
// filled/outline each animation step (drawn shapes, not a font glyph,
// so the two states render distinctly in every UI font).
const size = 6;
const iy = y + 2 + Math.max(0, ((font.lineHeight - size) / 2) | 0);
const value = selected ? 255 : 200;
if (hubAnimationPhase === 0) {
image.fillRect(22, iy, size, size, value);
} else {
image.drawRect(22, iy, size, size, value);
}
}
}
if (items.length > visibleRowCount) {
drawListScrollbar(
+32
View File
@@ -25,6 +25,13 @@ export type G2MirrorSession = {
cwdHint: string;
/** Unix epoch ms of the terminal's last bell, or null if none observed. */
lastBellAt: number | null;
/**
* Unix epoch ms (host clock) of the app's last output, or null if none
* observed. Fresh to within the wrapper's 2s activity reporting interval
* while we're connected; compare against other host timestamps, not the
* phone clock (see the worker's local-receive-time activity tracking).
*/
lastOutputAt: number | null;
/** Window title the app last set (xterm OSC 0/2), or null if none observed. */
title: string | null;
};
@@ -95,6 +102,7 @@ export class G2MirrorClient {
private readonly sessionAttachedListeners = new Set<(command: string) => void>();
private readonly sessionDetachedListeners = new Set<(reason: string) => void>();
private readonly bellListeners = new Set<(socket: string, lastBellAtMs: number) => void>();
private readonly activityListeners = new Set<(socket: string, lastOutputAtMs: number) => void>();
private readonly titleListeners = new Set<(socket: string, title: string) => void>();
// Launch requests awaiting their reply, oldest first. The server answers
// each `launch` with either `launched` or an `error` whose message starts
@@ -144,6 +152,16 @@ export class G2MirrorClient {
return () => this.bellListeners.delete(listener);
}
/**
* Unsolicited output-activity notification for any monitored terminal
* (rate-limited by the wrapper to one per 2s per terminal, leading edge
* only — notifications stop as soon as the output does).
*/
onActivity(listener: (socket: string, lastOutputAtMs: number) => void): () => void {
this.activityListeners.add(listener);
return () => this.activityListeners.delete(listener);
}
/** Unsolicited title change for any monitored terminal. */
onTitle(listener: (socket: string, title: string) => void): () => void {
this.titleListeners.add(listener);
@@ -350,6 +368,7 @@ export class G2MirrorClient {
pid: Number(item?.pid) || 0,
cwdHint: String(item?.cwd_hint ?? ""),
lastBellAt: typeof item?.last_bell_at === "number" ? item.last_bell_at : null,
lastOutputAt: typeof item?.last_output_at === "number" ? item.last_output_at : null,
title: typeof item?.title === "string" ? item.title : null,
}))
.filter((session: G2MirrorSession) => session.socket.length > 0);
@@ -367,6 +386,19 @@ export class G2MirrorClient {
}
return;
}
case "activity": {
const socket = String(message.socket ?? "");
const lastOutputAt = Number(message.last_output_at) || 0;
if (!socket || !lastOutputAt) return;
const session = this.sessions.find((s) => s.socket === socket);
if (session) session.lastOutputAt = lastOutputAt;
// No emitState: activity is frequent while an app is busy, and the
// terminal worker drives its own hub animation off the listener.
for (const listener of Array.from(this.activityListeners)) {
listener(socket, lastOutputAt);
}
return;
}
case "title": {
const socket = String(message.socket ?? "");
const title = String(message.title ?? "");