@stage-labs/metro 0.1.0-beta.106 → 0.1.0-beta.108

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/dist/claude.js CHANGED
@@ -6,10 +6,12 @@ import { localAgents, pickLocalAgent } from './local.js';
6
6
  import { PROVIDER_FLAGS } from './provider-flags.js';
7
7
  import { localPort, localUrl } from './runtime.js';
8
8
  const CHANNEL_FLAGS = ['--dangerously-load-development-channels', 'server:metro'];
9
+ const PERMISSION_FLAGS = ['--permission-mode', 'auto'];
9
10
  const KEY_HEADER = 'x-metro-key';
10
11
  const PROBE_MS = 3_000;
11
12
  export const claudeArgs = (extra, mcpConfig) => [
12
13
  ...CHANNEL_FLAGS,
14
+ ...PERMISSION_FLAGS,
13
15
  ...(mcpConfig === undefined ? [] : ['--mcp-config', mcpConfig]),
14
16
  ...extra,
15
17
  ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stage-labs/metro",
3
- "version": "0.1.0-beta.106",
3
+ "version": "0.1.0-beta.108",
4
4
  "description": "The metro command line. Sign in once per machine, then hand your MCP connector list to Claude Code without the credentials touching disk, argv or shell history.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "metro",
3
- "version": "0.1.0-beta.106",
3
+ "version": "0.1.0-beta.108",
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, the standing rules loaded at each session start, and a refresh skill for when the daemon could not keep the server list current. No vendor credential ever sits in a config file.",
5
5
  "author": {
6
6
  "name": "Bonustrack",
@@ -1,11 +1,14 @@
1
1
  import { spawnSync } from 'node:child_process';
2
+ import { realpathSync } from 'node:fs';
2
3
  import { homedir } from 'node:os';
3
4
  import { join } from 'node:path';
4
5
  import { errMsg, log } from '@metro-labs/core/log';
5
6
  import { isRecord } from '@metro-labs/core/is-record';
6
7
  import { readJson, writeJson } from '@metro-labs/core/secure-fs';
8
+ import { METRO_VERSION } from '@metro-labs/core/version';
7
9
  import { agentsDir, listAgentFiles } from '../agents/files.js';
8
10
  import { notReady, readModelConfig } from '../gateway/model-config.js';
11
+ import { claudeDir, listClaudeProjects } from './files.js';
9
12
  import { claudeAccount, claudeInstalled } from './login.js';
10
13
  import { trustFolder } from './onboarding.js';
11
14
 
@@ -26,6 +29,8 @@ export interface SessionDeps {
26
29
  agents?: string;
27
30
  signedIn?: () => boolean;
28
31
  now?: () => number;
32
+ version?: string;
33
+ continues?: (home: string) => boolean;
29
34
  }
30
35
 
31
36
  export interface SessionStatus {
@@ -48,13 +53,24 @@ const memory: Memory = { lastStartedAt: null, lastError: null, strikes: 0, pause
48
53
 
49
54
  const statePath = (agents: string): string => join(agents, STATE_FILE);
50
55
 
51
- export function autostartEnabled(agents = agentsDir()): boolean {
56
+ function readState(agents: string): Record<string, unknown> {
52
57
  const raw = readJson<unknown>(statePath(agents), null);
53
- return !isRecord(raw) || raw.autostart !== false;
58
+ return isRecord(raw) ? raw : {};
54
59
  }
55
60
 
61
+ const writeState = (agents: string, patch: Record<string, unknown>): void => {
62
+ writeJson(statePath(agents), { ...readState(agents), ...patch });
63
+ };
64
+
65
+ export const autostartEnabled = (agents = agentsDir()): boolean => readState(agents).autostart !== false;
66
+
56
67
  export function setAutostart(enabled: boolean, agents = agentsDir()): void {
57
- writeJson(statePath(agents), { autostart: enabled });
68
+ writeState(agents, { autostart: enabled });
69
+ }
70
+
71
+ export function startedVersion(agents = agentsDir()): string | null {
72
+ const version = readState(agents).version;
73
+ return typeof version === 'string' ? version : null;
58
74
  }
59
75
 
60
76
  function tmuxOk(tmux: string, args: string[]): boolean {
@@ -64,10 +80,24 @@ function tmuxOk(tmux: string, args: string[]): boolean {
64
80
 
65
81
  export const sessionRunning = (tmux = 'tmux'): boolean => tmuxOk(tmux, ['has-session', '-t', SESSION_NAME]);
66
82
 
67
- function metroCommand(deps: SessionDeps): string[] {
68
- if (deps.metro !== undefined) return deps.metro;
83
+ function realDir(dir: string): string {
84
+ try {
85
+ return realpathSync(dir);
86
+ } catch {
87
+ return dir;
88
+ }
89
+ }
90
+
91
+ export function hasConversation(home: string, dir = claudeDir()): boolean {
92
+ const cwd = realDir(home);
93
+ return listClaudeProjects(dir).some((project) => project.sessions > 0 && project.cwd !== null && realDir(project.cwd) === cwd);
94
+ }
95
+
96
+ function metroCommand(deps: SessionDeps, home: string): string[] {
69
97
  const bin = process.env.METRO_CLI_BIN?.trim() ?? '';
70
- return bin === '' ? ['metro', 'claude'] : [process.execPath, bin, 'claude'];
98
+ const base = deps.metro ?? (bin === '' ? ['metro', 'claude'] : [process.execPath, bin, 'claude']);
99
+ const continues = (deps.continues ?? hasConversation)(home);
100
+ return continues ? [...base, '-c'] : base;
71
101
  }
72
102
 
73
103
  function credentialReady(deps: SessionDeps): string | null {
@@ -108,22 +138,27 @@ function confirmChannels(tmux: string, until: number): void {
108
138
  setTimeout(tick, CONFIRM_POLL_MS).unref();
109
139
  }
110
140
 
141
+ function recordStart(deps: SessionDeps, tmux: string, now: number, run: { error?: Error; status: number | null; stderr: string }): void {
142
+ memory.lastStartedAt = now;
143
+ if (run.error !== undefined || run.status !== 0) {
144
+ memory.lastError = run.error === undefined ? `tmux new-session exited ${String(run.status)}: ${run.stderr.trim()}` : errMsg(run.error);
145
+ log.warn({ err: memory.lastError }, 'claude-session: tmux could not start');
146
+ return;
147
+ }
148
+ memory.lastError = null;
149
+ writeState(deps.agents ?? agentsDir(), { version: deps.version ?? METRO_VERSION });
150
+ confirmChannels(tmux, now + CONFIRM_WAIT_MS);
151
+ }
152
+
111
153
  export function startSession(deps: SessionDeps = {}): SessionStatus {
112
154
  const tmux = deps.tmux ?? 'tmux';
113
155
  const home = deps.home ?? homedir();
114
156
  const now = (deps.now ?? Date.now)();
115
157
  const trusted = trustFolder(home);
116
- const [command = 'metro', ...args] = metroCommand(deps);
158
+ const [command = 'metro', ...args] = metroCommand(deps, home);
117
159
  const run = spawnSync(tmux, ['new-session', '-d', '-s', SESSION_NAME, '-c', home, '-x', '200', '-y', '50', command, ...args], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'], cwd: home });
118
- memory.lastStartedAt = now;
119
- if (run.error !== undefined || run.status !== 0) {
120
- memory.lastError = run.error === undefined ? `tmux new-session exited ${String(run.status)}: ${run.stderr.trim()}` : errMsg(run.error);
121
- log.warn({ err: memory.lastError }, 'claude-session: tmux could not start');
122
- } else {
123
- memory.lastError = null;
124
- log.info({ home, trusted, command: [command, ...args].join(' ') }, 'claude-session: started Claude Code in tmux');
125
- confirmChannels(tmux, now + CONFIRM_WAIT_MS);
126
- }
160
+ recordStart(deps, tmux, now, run);
161
+ if (memory.lastError === null) log.info({ home, trusted, command: [command, ...args].join(' ') }, 'claude-session: started Claude Code in tmux');
127
162
  return sessionStatus(deps);
128
163
  }
129
164
 
@@ -153,15 +188,46 @@ function strike(now: number): boolean {
153
188
  return true;
154
189
  }
155
190
 
156
- export function ensureSession(deps: SessionDeps = {}): 'started' | 'running' | 'blocked' | 'off' | 'paused' {
157
- const now = (deps.now ?? Date.now)();
158
- if (!autostartEnabled(deps.agents ?? agentsDir())) return 'off';
159
- if (sessionRunning(deps.tmux ?? 'tmux')) return 'running';
191
+ export type Ensured = 'started' | 'restarted' | 'running' | 'blocked' | 'off' | 'paused';
192
+
193
+ function holdsBack(deps: SessionDeps, now: number): Ensured | null {
160
194
  if (now < memory.pausedUntil) return 'paused';
161
195
  if (sessionBlocked(deps) !== null) return 'blocked';
162
- if (strike(now)) return 'paused';
196
+ return strike(now) ? 'paused' : null;
197
+ }
198
+
199
+ interface Resolved {
200
+ now: number;
201
+ agents: string;
202
+ version: string;
203
+ tmux: string;
204
+ }
205
+
206
+ const resolved = (deps: SessionDeps): Resolved => ({
207
+ now: (deps.now ?? Date.now)(),
208
+ agents: deps.agents ?? agentsDir(),
209
+ version: deps.version ?? METRO_VERSION,
210
+ tmux: deps.tmux ?? 'tmux',
211
+ });
212
+
213
+ function situation(r: Resolved): 'running' | 'stale' | 'absent' {
214
+ if (!sessionRunning(r.tmux)) return 'absent';
215
+ return startedVersion(r.agents) === r.version ? 'running' : 'stale';
216
+ }
217
+
218
+ export function ensureSession(deps: SessionDeps = {}): Ensured {
219
+ const r = resolved(deps);
220
+ if (!autostartEnabled(r.agents)) return 'off';
221
+ const found = situation(r);
222
+ if (found === 'running') return 'running';
223
+ const held = holdsBack(deps, r.now);
224
+ if (held !== null) return held;
225
+ if (found === 'stale') {
226
+ log.info({ was: startedVersion(r.agents), now: r.version }, 'claude-session: restarting the session an older metro started, so the new plugin and flags load');
227
+ stopSession(deps);
228
+ }
163
229
  startSession(deps);
164
- return 'started';
230
+ return found === 'stale' ? 'restarted' : 'started';
165
231
  }
166
232
 
167
233
  let watcher: ReturnType<typeof setInterval> | null = null;
@@ -171,7 +237,7 @@ export function watchSession(deps: SessionDeps = {}, everyMs = WATCH_MS): void {
171
237
  const tick = (): void => {
172
238
  try {
173
239
  const outcome = ensureSession(deps);
174
- if (outcome === 'started') log.info('claude-session: started');
240
+ if (outcome === 'started' || outcome === 'restarted') log.info({ outcome }, 'claude-session: Claude Code is up');
175
241
  } catch (err) {
176
242
  log.warn({ err: errMsg(err) }, 'claude-session: check failed');
177
243
  }
@@ -1,3 +1,3 @@
1
1
  {
2
- "version": "0.1.0-beta.106"
2
+ "version": "0.1.0-beta.108"
3
3
  }