@stage-labs/metro 0.1.0-beta.185 → 0.1.0-beta.186
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/service.js +1 -0
- package/package.json +1 -1
- package/runtime/marketplace/plugin/.claude-plugin/plugin.json +1 -1
- package/runtime/node_modules/@metro-labs/daemon/src/boot/boot.ts +2 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/memory.ts +72 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/session.ts +5 -1
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/api.ts +11 -2
- package/runtime/runtime.json +1 -1
package/dist/service.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stage-labs/metro",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.186",
|
|
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.
|
|
3
|
+
"version": "0.1.0-beta.186",
|
|
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",
|
|
@@ -24,6 +24,7 @@ import { syncPluginServers } from '../connectors/plugin-sync.js';
|
|
|
24
24
|
import { loadConnectorPolicies, readLocalConnectors } from '../connectors/store.js';
|
|
25
25
|
import { ensureMetroPlugin } from '../claude/plugin-install.js';
|
|
26
26
|
import { provisionAgentUser } from '../agent-user/provision.js';
|
|
27
|
+
import { ensureServiceOomPolicy } from '../claude/memory.js';
|
|
27
28
|
import { unwatchSession, watchSession } from '../claude/session.js';
|
|
28
29
|
import { tryClaudeSetup } from '../claude/setup.js';
|
|
29
30
|
import { applyLocalOwner } from './local-owner.js';
|
|
@@ -154,6 +155,7 @@ installBearerSessions(agentsDir(), localOwner);
|
|
|
154
155
|
}
|
|
155
156
|
|
|
156
157
|
async function startClaude(): Promise<void> {
|
|
158
|
+
ensureServiceOomPolicy();
|
|
157
159
|
const agentUser = await provisionAgentUser();
|
|
158
160
|
if (agentUser !== 'off') log.info({ agentUser }, 'agent-user: Claude Code runs as its own user');
|
|
159
161
|
if (agentUser === 'failed' || agentUser === 'unsupported') return;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { totalmem } from 'node:os';
|
|
4
|
+
import { dirname } from 'node:path';
|
|
5
|
+
import { errMsg, log } from '@metro-labs/core/log';
|
|
6
|
+
|
|
7
|
+
const UNIT = '/etc/systemd/system/metro.service';
|
|
8
|
+
const DROP_IN = '/etc/systemd/system/metro.service.d/10-metro-memory.conf';
|
|
9
|
+
const DROP_IN_TEXT = '[Service]\nOOMPolicy=continue\n';
|
|
10
|
+
const GIB = 1024 ** 3;
|
|
11
|
+
const RESERVE = 1.5 * GIB;
|
|
12
|
+
const FLOOR = GIB;
|
|
13
|
+
|
|
14
|
+
export interface ScopeHost {
|
|
15
|
+
platform: string;
|
|
16
|
+
uid: number | undefined;
|
|
17
|
+
systemd: boolean;
|
|
18
|
+
total: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const realHost = (): ScopeHost => ({
|
|
22
|
+
platform: process.platform,
|
|
23
|
+
uid: process.getuid?.(),
|
|
24
|
+
systemd: existsSync('/run/systemd/system'),
|
|
25
|
+
total: totalmem(),
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
export function sessionMemoryLimit(total: number): number {
|
|
29
|
+
const kept = Math.min(Math.floor(total * 0.8), total - RESERVE);
|
|
30
|
+
return Math.max(FLOOR, Math.floor(kept / (1024 * 1024)) * 1024 * 1024);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const usesScopes = (host: ScopeHost): boolean => host.platform === 'linux' && host.uid === 0 && host.systemd;
|
|
34
|
+
|
|
35
|
+
export function inSessionScope(command: [string, string[]], host: ScopeHost = realHost(), now = Date.now()): [string, string[]] {
|
|
36
|
+
if (!usesScopes(host)) return command;
|
|
37
|
+
const limit = sessionMemoryLimit(host.total);
|
|
38
|
+
const [file, args] = command;
|
|
39
|
+
return [
|
|
40
|
+
'systemd-run',
|
|
41
|
+
[
|
|
42
|
+
'--scope',
|
|
43
|
+
'--quiet',
|
|
44
|
+
'--collect',
|
|
45
|
+
`--unit=metro-claude-${String(now)}`,
|
|
46
|
+
'-p',
|
|
47
|
+
`MemoryMax=${String(limit)}`,
|
|
48
|
+
'-p',
|
|
49
|
+
`MemoryHigh=${String(Math.floor(limit * 0.9))}`,
|
|
50
|
+
'-p',
|
|
51
|
+
'OOMPolicy=continue',
|
|
52
|
+
'--',
|
|
53
|
+
file,
|
|
54
|
+
...args,
|
|
55
|
+
],
|
|
56
|
+
];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function ensureServiceOomPolicy(host: ScopeHost = realHost(), env: NodeJS.ProcessEnv = process.env): 'written' | 'present' | 'skipped' {
|
|
60
|
+
if (!usesScopes(host) || (env.INVOCATION_ID ?? '') === '' || !existsSync(UNIT)) return 'skipped';
|
|
61
|
+
try {
|
|
62
|
+
if (existsSync(DROP_IN) && readFileSync(DROP_IN, 'utf8') === DROP_IN_TEXT) return 'present';
|
|
63
|
+
mkdirSync(dirname(DROP_IN), { recursive: true });
|
|
64
|
+
writeFileSync(DROP_IN, DROP_IN_TEXT, { mode: 0o644 });
|
|
65
|
+
const reload = spawnSync('systemctl', ['daemon-reload'], { stdio: 'ignore' });
|
|
66
|
+
log.info({ reloaded: reload.status === 0 }, 'claude-memory: the metro service no longer stops as a whole when the kernel kills one process for memory');
|
|
67
|
+
return 'written';
|
|
68
|
+
} catch (err) {
|
|
69
|
+
log.warn({ err: errMsg(err) }, 'claude-memory: could not set the service memory policy');
|
|
70
|
+
return 'skipped';
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -11,6 +11,7 @@ import { notReady, readModelConfig, routedConnection } from '../gateway/model-co
|
|
|
11
11
|
import { claudeDir, listClaudeProjects } from './files.js';
|
|
12
12
|
import { claudeAccount, claudeInstalled } from './login.js';
|
|
13
13
|
import { trustFolder } from './onboarding.js';
|
|
14
|
+
import { inSessionScope } from './memory.js';
|
|
14
15
|
import { agentUser, agentViewDir, asAgent, claudeHome, wantedAgentUser } from '../agent-user/user.js';
|
|
15
16
|
|
|
16
17
|
function sessionEnv(): Record<string, string> {
|
|
@@ -88,6 +89,8 @@ function tmuxOk(tmux: string, args: string[]): boolean {
|
|
|
88
89
|
|
|
89
90
|
export const sessionRunning = (tmux = 'tmux'): boolean => tmuxOk(tmux, ['has-session', '-t', SESSION_NAME]);
|
|
90
91
|
|
|
92
|
+
export const tmuxServerUp = (tmux = 'tmux'): boolean => tmuxOk(tmux, ['list-sessions']);
|
|
93
|
+
|
|
91
94
|
function realDir(dir: string): string {
|
|
92
95
|
try {
|
|
93
96
|
return realpathSync(dir);
|
|
@@ -179,7 +182,8 @@ export function startSession(deps: SessionDeps = {}): SessionStatus {
|
|
|
179
182
|
const trusted = trustFolder(home);
|
|
180
183
|
const [command = 'metro', ...args] = metroCommand(deps, home);
|
|
181
184
|
const tmuxArgs = ['new-session', '-d', '-s', SESSION_NAME, '-c', home, '-x', '200', '-y', '50', command, ...args];
|
|
182
|
-
const
|
|
185
|
+
const launch = deps.tmux === undefined && !tmuxServerUp(tmux) ? inSessionScope(asAgent(tmux, tmuxArgs)) : asAgent(tmux, tmuxArgs);
|
|
186
|
+
const run = spawnSync(...launch, { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'], cwd: home });
|
|
183
187
|
recordStart(deps, tmux, now, run);
|
|
184
188
|
if (memory.lastError === null) log.info({ home, trusted, command: [command, ...args].join(' ') }, 'claude-session: started Claude Code in tmux');
|
|
185
189
|
return sessionStatus(deps);
|
|
@@ -6,12 +6,21 @@ import { bodyField, readJsonBody, sessionRoute } from '@metro-labs/http/api-http
|
|
|
6
6
|
import { log } from '@metro-labs/core/log';
|
|
7
7
|
import { mintTerminalTicket } from './tickets.js';
|
|
8
8
|
import { agentCommand, asAgent, claudeHome } from '../agent-user/user.js';
|
|
9
|
+
import { inSessionScope } from '../claude/memory.js';
|
|
10
|
+
import { tmuxServerUp } from '../claude/session.js';
|
|
9
11
|
|
|
10
12
|
const PREFIX = '/api/terminal';
|
|
11
13
|
const TICKETS = `${PREFIX}/tickets`;
|
|
12
14
|
const SESSION_RE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,31}$/;
|
|
13
15
|
|
|
14
|
-
|
|
16
|
+
const scoped = (command: string[]): string[] => {
|
|
17
|
+
if (tmuxServerUp()) return command;
|
|
18
|
+
const [file = '', ...args] = command;
|
|
19
|
+
const [bin, argv] = inSessionScope([file, args]);
|
|
20
|
+
return [bin, ...argv];
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export const tmuxCommand = (session: string, home = claudeHome() ?? homedir()): string[] => scoped(agentCommand([
|
|
15
24
|
'tmux',
|
|
16
25
|
'new-session',
|
|
17
26
|
'-A',
|
|
@@ -35,7 +44,7 @@ export const tmuxCommand = (session: string, home = claudeHome() ?? homedir()):
|
|
|
35
44
|
'-as',
|
|
36
45
|
'terminal-features',
|
|
37
46
|
',xterm-256color:clipboard',
|
|
38
|
-
], { TERM: 'xterm-256color', COLORTERM: 'truecolor' });
|
|
47
|
+
], { TERM: 'xterm-256color', COLORTERM: 'truecolor' }));
|
|
39
48
|
|
|
40
49
|
export interface TerminalApiDeps {
|
|
41
50
|
command?: (session: string) => string[];
|
package/runtime/runtime.json
CHANGED