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

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.267",
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.267",
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
 
@@ -1,3 +1,3 @@
1
1
  {
2
- "version": "0.1.0-beta.266"
2
+ "version": "0.1.0-beta.267"
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.267",
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