@stage-labs/metro 0.1.0-beta.266 → 0.1.0-beta.268

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stage-labs/metro",
3
- "version": "0.1.0-beta.266",
3
+ "version": "0.1.0-beta.268",
4
4
  "description": "The metro command line: run your agent on this machine, relaying its chat channels and connectors to Claude Code.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "metro",
3
- "version": "0.1.0-beta.266",
3
+ "version": "0.1.0-beta.268",
4
4
  "description": "Metro for Claude Code, installed by the metro daemon on this machine: every connector the agent holds as its own MCP server relayed through the daemon, the orchestrator-only guard on the main thread, and the standing rules loaded at each session start. No vendor credential ever sits in a config file.",
5
5
  "author": {
6
6
  "name": "Bonustrack",
@@ -41,6 +41,8 @@ export interface RunnerTask {
41
41
  id: string;
42
42
  kind: string | null;
43
43
  agent: string | null;
44
+ description?: string | null;
45
+ lastObservedModel?: string | null;
44
46
  status: RunnerTaskState;
45
47
  background: boolean;
46
48
  startedAt: number;
@@ -116,12 +118,23 @@ const eventKind = (value: unknown): value is RunnerEventKind => EVENTS.some((e)
116
118
  const name = (value: unknown): string | null => typeof value === 'string' && /^[A-Za-z0-9_.:-]{1,128}$/.test(value) ? value : null;
117
119
  const sessionId = (value: unknown): string | null => typeof value === 'string' && /^[0-9a-f-]{36}$/.test(value) ? value : null;
118
120
 
121
+ export function runnerTaskDescription(value: unknown): string | null {
122
+ if (typeof value !== 'string') return null;
123
+ const line = value.split(/\r|\n|\p{Zl}|\p{Zp}/u, 1)[0] ?? '';
124
+ const description = line.replace(/[\p{Cc}\p{Cf}]/gu, ' ').replace(/\s+/g, ' ').trim().slice(0, 160).trim();
125
+ return description.length > 0 ? description : null;
126
+ }
127
+
128
+ export const runnerTaskModel = (value: unknown): string | null => typeof value === 'string' && /^[A-Za-z0-9][A-Za-z0-9_.:/@+[\]-]{0,127}$/.test(value) ? value : null;
129
+
119
130
  function task(raw: unknown): RunnerTask | null {
120
131
  if (!isRecord(raw) || !name(raw.id) || !taskState(raw.status)) return null;
121
132
  return {
122
133
  id: String(raw.id), kind: name(raw.kind), agent: name(raw.agent), status: raw.status, background: raw.background === true,
123
134
  startedAt: count(raw.startedAt), updatedAt: count(raw.updatedAt), endedAt: count(raw.endedAt) || null,
124
135
  lastTool: name(raw.lastTool), toolUses: count(raw.toolUses), durationMs: count(raw.durationMs),
136
+ ...(raw.description === undefined ? {} : { description: runnerTaskDescription(raw.description) }),
137
+ ...(raw.lastObservedModel === undefined ? {} : { lastObservedModel: runnerTaskModel(raw.lastObservedModel) }),
125
138
  };
126
139
  }
127
140
 
@@ -188,6 +188,7 @@ export class SharedCalls {
188
188
  private status(route: CallRoute, action: Action, status: SpeechStatus): void {
189
189
  if (this.terminal(action.status) || (status === 'started' && action.status === 'started')) return;
190
190
  action.status = status;
191
+ log.info({ actionId: action.actionId, status, at: Date.now() }, 'sdk-call: speech action');
191
192
  this.bridge?.notify({ type: 'speech', route, sourceId: action.sourceId, actionId: action.actionId, status });
192
193
  }
193
194
  }
@@ -12,17 +12,24 @@ const SPEECH_MS = 60_000;
12
12
  type Speech = Pick<Utterance, 'attach' | 'say' | 'end' | 'abort'>;
13
13
  type MakeSpeech = (failed: (reason: string) => void) => Speech;
14
14
 
15
- interface Playing {
15
+ interface Queued {
16
16
  action: SharedSpeechAction;
17
+ queuedAt: number;
18
+ }
19
+
20
+ interface Playing extends Queued {
17
21
  utterance: Speech | null;
18
22
  timer: ReturnType<typeof setTimeout>;
19
23
  synthesized: boolean;
20
24
  started: boolean;
25
+ synthesisStartedAt: number;
26
+ firstAudioAt: number | null;
27
+ markedFrameResolvedAt: number | null;
21
28
  }
22
29
 
23
30
  export class SpeechQueue {
24
31
  private readonly player: Player;
25
- private readonly waiting: SharedSpeechAction[] = [];
32
+ private readonly waiting: Queued[] = [];
26
33
  private current: Playing | null = null;
27
34
  private live = false;
28
35
  private closed = false;
@@ -45,6 +52,7 @@ export class SpeechQueue {
45
52
  const current = this.current;
46
53
  if (current === null || current.started) return;
47
54
  current.started = true;
55
+ current.markedFrameResolvedAt = Date.now();
48
56
  current.action.status('started');
49
57
  });
50
58
  this.player.whenIdle(() => {
@@ -66,7 +74,7 @@ export class SpeechQueue {
66
74
  enqueue(action: SharedSpeechAction): boolean {
67
75
  if (this.closed || action.text.trim() === '' || action.text.length > MAX_TEXT) return false;
68
76
  if (this.waiting.length + (this.current === null ? 0 : 1) >= MAX_QUEUED || !action.isValid()) return false;
69
- this.waiting.push(action);
77
+ this.waiting.push({ action, queuedAt: Date.now() });
70
78
  queueMicrotask(() => {
71
79
  this.next();
72
80
  });
@@ -83,7 +91,10 @@ export class SpeechQueue {
83
91
  cancel(): void {
84
92
  const waiting = this.waiting.splice(0);
85
93
  this.settle('interrupted');
86
- for (const action of waiting) action.status('interrupted');
94
+ for (const queued of waiting) {
95
+ this.report(queued, 'interrupted');
96
+ queued.action.status('interrupted');
97
+ }
87
98
  }
88
99
 
89
100
  close(): void {
@@ -96,15 +107,18 @@ export class SpeechQueue {
96
107
 
97
108
  private next(): void {
98
109
  if (!this.live || this.closed || this.current !== null) return;
99
- const action = this.waiting.shift();
100
- if (action === undefined) return;
110
+ const queued = this.waiting.shift();
111
+ if (queued === undefined) return;
112
+ const { action } = queued;
101
113
  if (!action.isValid()) {
114
+ this.report(queued, 'failed');
102
115
  action.status('failed');
103
116
  this.next();
104
117
  return;
105
118
  }
106
119
  const playing: Playing = {
107
- action, utterance: null, synthesized: false, started: false,
120
+ ...queued, utterance: null, synthesized: false, started: false,
121
+ synthesisStartedAt: Date.now(), firstAudioAt: null, markedFrameResolvedAt: null,
108
122
  timer: setTimeout(() => {
109
123
  if (this.current === playing) this.fail('speech timed out');
110
124
  }, this.timeoutMs),
@@ -121,7 +135,9 @@ export class SpeechQueue {
121
135
  playing.utterance = utterance;
122
136
  utterance.attach({
123
137
  audio: (pcm, chars) => {
124
- if (this.valid(playing)) this.player.push(pcm, chars);
138
+ if (this.current === playing && pcm.length > 0) playing.firstAudioAt ??= Date.now();
139
+ if (!this.valid(playing)) return;
140
+ this.player.push(pcm, chars);
125
141
  },
126
142
  done: () => {
127
143
  if (!this.valid(playing)) return;
@@ -157,6 +173,16 @@ export class SpeechQueue {
157
173
  this.onFailed();
158
174
  }
159
175
 
176
+ private report(queued: Queued, status: SpeechStatus, playing?: Playing): void {
177
+ log.info({
178
+ actionId: queued.action.actionId, status, queuedAt: queued.queuedAt,
179
+ synthesisStartedAt: playing?.synthesisStartedAt ?? null,
180
+ firstAudioAt: playing?.firstAudioAt ?? null,
181
+ markedFrameResolvedAt: playing?.markedFrameResolvedAt ?? null,
182
+ settledAt: Date.now(),
183
+ }, 'voice: speech timing');
184
+ }
185
+
160
186
  private settle(status: SpeechStatus): void {
161
187
  const current = this.current;
162
188
  this.current = null;
@@ -164,6 +190,7 @@ export class SpeechQueue {
164
190
  if (current !== null) {
165
191
  clearTimeout(current.timer);
166
192
  current.utterance?.abort();
193
+ this.report(current, status, current);
167
194
  current.action.status(status);
168
195
  }
169
196
  queueMicrotask(() => {
@@ -1,3 +1,3 @@
1
1
  {
2
- "version": "0.1.0-beta.266"
2
+ "version": "0.1.0-beta.268"
3
3
  }
@@ -41,6 +41,8 @@ export interface RunnerTask {
41
41
  id: string;
42
42
  kind: string | null;
43
43
  agent: string | null;
44
+ description?: string | null;
45
+ lastObservedModel?: string | null;
44
46
  status: RunnerTaskState;
45
47
  background: boolean;
46
48
  startedAt: number;
@@ -116,12 +118,23 @@ const eventKind = (value: unknown): value is RunnerEventKind => EVENTS.some((e)
116
118
  const name = (value: unknown): string | null => typeof value === 'string' && /^[A-Za-z0-9_.:-]{1,128}$/.test(value) ? value : null;
117
119
  const sessionId = (value: unknown): string | null => typeof value === 'string' && /^[0-9a-f-]{36}$/.test(value) ? value : null;
118
120
 
121
+ export function runnerTaskDescription(value: unknown): string | null {
122
+ if (typeof value !== 'string') return null;
123
+ const line = value.split(/\r|\n|\p{Zl}|\p{Zp}/u, 1)[0] ?? '';
124
+ const description = line.replace(/[\p{Cc}\p{Cf}]/gu, ' ').replace(/\s+/g, ' ').trim().slice(0, 160).trim();
125
+ return description.length > 0 ? description : null;
126
+ }
127
+
128
+ export const runnerTaskModel = (value: unknown): string | null => typeof value === 'string' && /^[A-Za-z0-9][A-Za-z0-9_.:/@+[\]-]{0,127}$/.test(value) ? value : null;
129
+
119
130
  function task(raw: unknown): RunnerTask | null {
120
131
  if (!isRecord(raw) || !name(raw.id) || !taskState(raw.status)) return null;
121
132
  return {
122
133
  id: String(raw.id), kind: name(raw.kind), agent: name(raw.agent), status: raw.status, background: raw.background === true,
123
134
  startedAt: count(raw.startedAt), updatedAt: count(raw.updatedAt), endedAt: count(raw.endedAt) || null,
124
135
  lastTool: name(raw.lastTool), toolUses: count(raw.toolUses), durationMs: count(raw.durationMs),
136
+ ...(raw.description === undefined ? {} : { description: runnerTaskDescription(raw.description) }),
137
+ ...(raw.lastObservedModel === undefined ? {} : { lastObservedModel: runnerTaskModel(raw.lastObservedModel) }),
125
138
  };
126
139
  }
127
140
 
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.1.0-beta.266",
2
+ "version": "0.1.0-beta.268",
3
3
  "dependencies": {
4
4
  "@anthropic-ai/claude-agent-sdk": "0.3.287",
5
5
  "@modelcontextprotocol/sdk": "^1.29.0",
@@ -1,16 +1,19 @@
1
1
  import type { SDKMessage } from '@anthropic-ai/claude-agent-sdk';
2
- import type { RunnerEventKind, RunnerTask, RunnerTaskState } from '@metro-labs/core/runner-activity';
2
+ import { isRecord } from '@metro-labs/core/is-record';
3
+ import { runnerTaskDescription, runnerTaskModel, type RunnerEventKind, type RunnerTask, type RunnerTaskState } from '@metro-labs/core/runner-activity';
3
4
 
4
5
  export const activityName = (name: unknown): string | null => typeof name === 'string' && /^[A-Za-z0-9_.:-]{1,128}$/.test(name) ? name : null;
5
6
  const live = (task: RunnerTask): boolean => ['running', 'pending', 'paused'].includes(task.status);
6
7
  const terminal = (status: RunnerTaskState): boolean => ['completed', 'failed', 'stopped'].includes(status);
7
8
  type System = Extract<SDKMessage, { type: 'system' }>;
8
9
  type Emit = (kind: RunnerEventKind, tool?: string | null, taskId?: string | null) => void;
10
+ type Metadata = Pick<RunnerTask, 'description' | 'lastObservedModel'>;
9
11
 
10
12
  export class ActivityTasks {
11
13
  private readonly tasks = new Map<string, RunnerTask>();
12
14
  private readonly parents = new Map<string, string>();
13
15
  private readonly hidden = new Map<string, string>();
16
+ private readonly pending = new Map<string, Metadata>();
14
17
  constructor(private readonly emit: Emit) {}
15
18
 
16
19
  snapshot(): RunnerTask[] {
@@ -34,6 +37,41 @@ export class ActivityTasks {
34
37
  }
35
38
  }
36
39
 
40
+ launch(id: string, input: unknown): void {
41
+ if (!isRecord(input)) return;
42
+ const description = runnerTaskDescription(input.description);
43
+ if (description !== null) this.metadata(id, { description });
44
+ }
45
+
46
+ model(parent: string, value: unknown): void {
47
+ const lastObservedModel = runnerTaskModel(value);
48
+ if (lastObservedModel !== null) this.metadata(parent, { lastObservedModel });
49
+ }
50
+
51
+ private metadata(parent: string, fields: Metadata): void {
52
+ if (!activityName(parent) || this.isHidden(parent)) return;
53
+ const id = this.taskFor(parent);
54
+ const task = id === null ? undefined : this.tasks.get(id);
55
+ if (task !== undefined) { Object.assign(task, fields); return; }
56
+ const known = this.pending.get(parent);
57
+ this.pending.delete(parent);
58
+ const oldest = this.pending.keys().next();
59
+ if (this.pending.size >= 200 && !oldest.done) this.pending.delete(oldest.value);
60
+ this.pending.set(parent, { ...known, ...fields });
61
+ }
62
+
63
+ private link(parent: string, id: string): void {
64
+ this.parents.set(parent, id);
65
+ const task = this.tasks.get(id);
66
+ if (task !== undefined) Object.assign(task, this.pending.get(parent));
67
+ this.pending.delete(parent);
68
+ }
69
+
70
+ private describe(task: RunnerTask, value: unknown): void {
71
+ const description = runnerTaskDescription(value);
72
+ if (description !== null) task.description = description;
73
+ }
74
+
37
75
  stop(): void {
38
76
  for (const task of this.tasks.values()) if (live(task)) this.status(task, 'stopped');
39
77
  }
@@ -61,6 +99,7 @@ export class ActivityTasks {
61
99
  const oldest = this.hidden.keys().next();
62
100
  if (this.hidden.size >= 400 && !oldest.done) this.hidden.delete(oldest.value);
63
101
  this.hidden.set(key, id);
102
+ this.pending.delete(key);
64
103
  }
65
104
  }
66
105
 
@@ -71,7 +110,7 @@ export class ActivityTasks {
71
110
  for (const [key, taskId] of this.hidden) {
72
111
  if (taskId !== id) continue;
73
112
  this.hidden.delete(key);
74
- this.parents.set(key, id);
113
+ this.link(key, id);
75
114
  }
76
115
  return task;
77
116
  }
@@ -80,18 +119,20 @@ export class ActivityTasks {
80
119
  if (message.ambient || message.skip_transcript) { this.hide(message.task_id, message.tool_use_id); return; }
81
120
  const task = this.get(message.task_id);
82
121
  if (!task) return;
83
- task.kind = activityName(message.task_type);
84
- task.agent = activityName(message.subagent_type);
122
+ task.kind = activityName(message.task_type) ?? task.kind;
123
+ task.agent = activityName(message.subagent_type) ?? task.agent;
85
124
  task.background = message.is_backgrounded === true;
86
125
  task.startedAt = Date.now();
87
126
  task.endedAt = null;
88
- if (message.tool_use_id) this.parents.set(message.tool_use_id, task.id);
127
+ if (message.tool_use_id) this.link(message.tool_use_id, task.id);
128
+ this.describe(task, message.description);
89
129
  this.status(task, 'running');
90
130
  }
91
131
 
92
132
  private progress(message: Extract<System, { subtype: 'task_progress' }>): void {
93
133
  const task = this.tasks.get(message.task_id);
94
134
  if (!task || terminal(task.status)) return;
135
+ if (message.tool_use_id) this.link(message.tool_use_id, task.id);
95
136
  task.lastTool = activityName(message.last_tool_name);
96
137
  task.toolUses = message.usage.tool_uses;
97
138
  task.durationMs = message.usage.duration_ms;
@@ -101,15 +142,17 @@ export class ActivityTasks {
101
142
  private patch(message: Extract<System, { subtype: 'task_updated' }>): void {
102
143
  const task = this.tasks.get(message.task_id);
103
144
  if (!task) return;
145
+ this.describe(task, message.patch.description);
104
146
  if (message.patch.is_backgrounded !== undefined) task.background = message.patch.is_backgrounded;
105
147
  if (message.patch.status) this.status(task, message.patch.status === 'killed' ? 'stopped' : message.patch.status);
106
148
  task.updatedAt = Date.now();
107
149
  }
108
150
 
109
151
  private end(message: Extract<System, { subtype: 'task_notification' }>): void {
110
- if (message.ambient || message.skip_transcript) { this.hide(message.task_id); return; }
152
+ if (message.ambient || message.skip_transcript) { this.hide(message.task_id, message.tool_use_id); return; }
111
153
  const task = this.get(message.task_id);
112
154
  if (!task) return;
155
+ if (message.tool_use_id) this.link(message.tool_use_id, task.id);
113
156
  if (message.usage) {
114
157
  task.durationMs = message.usage.duration_ms;
115
158
  task.toolUses = message.usage.tool_uses;
@@ -123,7 +166,9 @@ export class ActivityTasks {
123
166
  for (const row of message.tasks) {
124
167
  if (row.ambient) { this.hide(row.task_id); continue; }
125
168
  const task = this.show(row.task_id);
126
- if (!task || terminal(task.status)) continue;
169
+ if (!task) continue;
170
+ this.describe(task, row.description);
171
+ if (terminal(task.status)) continue;
127
172
  task.kind = activityName(row.task_type);
128
173
  task.background = true;
129
174
  this.status(task, 'running');
@@ -251,15 +251,21 @@ export class Activity {
251
251
  private content(message: Extract<SDKMessage, { type: 'assistant' | 'user' }>): void {
252
252
  const content = message.message.content;
253
253
  const parent = message.parent_tool_use_id ?? null;
254
- if (parent === null && message.type === 'assistant') {
255
- this.turn();
256
- if (message.error !== undefined) this.providerCode = message.error === 'unknown' ? null : PROVIDER_ERRORS[message.error];
257
- }
254
+ if (message.type === 'assistant') this.assistant(message, parent);
258
255
  if (Array.isArray(content)) for (const block of content) if (isRecord(block)) this.block(block, parent);
259
256
  }
260
257
 
258
+ private assistant(message: Extract<SDKMessage, { type: 'assistant' }>, parent: string | null): void {
259
+ if (parent !== null) { this.tasks.model(parent, message.message.model); return; }
260
+ this.turn();
261
+ if (message.error !== undefined) this.providerCode = message.error === 'unknown' ? null : PROVIDER_ERRORS[message.error];
262
+ }
263
+
261
264
  private block(block: Record<string, unknown>, parent: string | null): void {
262
- if (block.type === 'tool_use' && typeof block.id === 'string') this.tool(block.id, block.name, parent);
265
+ if (block.type === 'tool_use' && typeof block.id === 'string') {
266
+ if (block.name === 'Agent' || block.name === 'Task') this.tasks.launch(block.id, block.input);
267
+ this.tool(block.id, block.name, parent);
268
+ }
263
269
  else if (block.type === 'tool_result' && typeof block.tool_use_id === 'string') this.finish(block.tool_use_id, block.is_error === true, parent, block.content);
264
270
  }
265
271
 
@@ -63,6 +63,6 @@ export class InputTiming {
63
63
 
64
64
  private note(input: RunnerInput): void {
65
65
  this.changed({ ...input });
66
- log.info({ input: input.id, kind: input.kind, state: input.state, queueAgeMs: (input.consumedAt ?? Date.now()) - input.acceptedAt }, 'sdk-runner: input lifecycle');
66
+ log.info({ input: input.id, kind: input.kind, state: input.state, acceptedAt: input.acceptedAt, dispatchedAt: input.dispatchedAt, consumedAt: input.consumedAt, firstOutputAt: input.firstOutputAt, completedAt: input.completedAt, queueAgeMs: (input.consumedAt ?? Date.now()) - input.acceptedAt }, 'sdk-runner: input lifecycle');
67
67
  }
68
68
  }
@@ -3,6 +3,7 @@ import type { CallRoute, CallSource } from '@metro-labs/core/call';
3
3
  export const FRONT_RULES = `You are the orchestrator of this agent's persistent Agent SDK session. Typed chat, live call words (<call> blocks), and background worker reports share this conversation.
4
4
  - Be fast. When you can answer a chat message at once, make the reaction and the answer one response of parallel tool calls (react, then send), with no typing signal. Use typing only when the answer will take a while.
5
5
  - Stay light: talk, decide and delegate. Real work goes to background workers (Agent tool, subagent_type "worker", run_in_background: true); keep answering while they run, and relay what they report.
6
+ - At the next safe tool boundary, answer pending live-call inputs before starting unrelated delegation or processing worker reports. Give a short, direct answer when possible; do not generate a long task prompt before replying to the caller. Never interrupt active writes or approvals, invent filler, or promise work is done before it is.
6
7
  - Plain assistant text is never spoken. Reply to a live <call> with mcp__metro__send using its exact line, text, and speech:{callId,generation,sourceId}. This sends speech only, not a chat post. Keep it short. Never paste a spoken reply or transcript into chat unless the owner explicitly requests a chat post.
7
8
  - Ordinary typed chat and worker results are never automatically spoken. For an explicit typed owner request to speak, use only that channel message's call_id, call_generation and call_source_id with its exact line as send speech:{callId:call_id,generation:call_generation,sourceId:call_source_id}. Without all three authorized live-call values, do not speak or guess a target. Do not reuse an earlier sourceId for a new reply.
8
9
  - A call ending invalidates its speech targets. Keep the chat session and background work running. Nothing is posted about a hangup unless requested. Speech status notifications are status only, never a request to speak.