@volter/supercode-teams 0.2.34 → 0.2.35

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/bin/fleet.mjs CHANGED
@@ -83,6 +83,8 @@ async function main() {
83
83
  cwd: flags.cwd,
84
84
  initialInput: flags.input,
85
85
  name: flags.name,
86
+ // `open` attaching this terminal to the pane itself asks the machine for no window of its own.
87
+ ...(flags.popup === 'false' ? { popup: false } : {}),
86
88
  }));
87
89
  } finally {
88
90
  channel.close();
package/bin/workspace.mjs CHANGED
@@ -721,7 +721,8 @@ async function connectorCommand(verb, { flags, rest, output, client, context, te
721
721
  const daemonHome = teamsHome();
722
722
  const hostKeyId = publicKeyId(loadOrCreateKeyPair(paths(daemonHome).machineKey).publicKeyPem);
723
723
  const connector = new TeamsConnector({ server: context.origin, teamId: context.team_id, machineId: saved.machine.id, sourceId: saved.source.id, credential: saved.credential.token, stateDir: state.directory, core, hostKeyId });
724
- const machine = new TeamsMachine({ home: daemonHome, supercode: binary, ...(flags.tmux_socket ? { tmuxSocket: String(flags.tmux_socket) } : {}), link: { server: context.origin, teamId: context.team_id, machineId: saved.machine.id, credential: saved.credential.token, incarnationId: connector.state.incarnation_id, configuration: () => connector.configuration() } });
724
+ // A connected machine is a person's: each pane it launches opens a window on its screen (SUPERCODE_POPUPS=0: none).
725
+ const machine = new TeamsMachine({ home: daemonHome, supercode: binary, popups: process.env.SUPERCODE_POPUPS !== '0', ...(flags.tmux_socket ? { tmuxSocket: String(flags.tmux_socket) } : {}), link: { server: context.origin, teamId: context.team_id, machineId: saved.machine.id, credential: saved.credential.token, incarnationId: connector.state.incarnation_id, configuration: () => connector.configuration() } });
725
726
  machine.on('link', (event) => { if (event.event === 'replaced') process.stderr.write(`supercode teams: ${event.error}. Two connectors were running for this machine; stop one of them (\`supercode teams disconnect\` where it should not run).\n`); });
726
727
  const stopAll = async () => { await machine.stop().catch(() => {}); await connector.stop(); await core.close(); await lifecycle.close(); state.update(current => current ? ({ ...current, process: undefined }) : null); };
727
728
  try { await connector.start({ roots: () => saved.roots }); await machine.start(); } catch (error) { await stopAll(); throw error; }
package/machine.mjs CHANGED
@@ -29,7 +29,7 @@ import { EventEmitter } from 'node:events';
29
29
  import { readFileSync, writeFileSync, writeSync, statSync, readdirSync, mkdirSync, renameSync, rmSync, existsSync, openSync, readSync, closeSync } from 'node:fs';
30
30
  import { join, resolve, dirname } from 'node:path';
31
31
  import { fileURLToPath } from 'node:url';
32
- import { hostname } from 'node:os';
32
+ import { homedir, hostname } from 'node:os';
33
33
  import { randomBytes, createHash } from 'node:crypto';
34
34
  import { WebSocket } from 'ws';
35
35
  import { TmuxTerminalHost, classifyAttentionSignal, composerHoldsDraft, composerReadyForInput } from '@volter/supercode-terminal';
@@ -67,16 +67,64 @@ const COMPOSER_RECHECK_MS = 400;
67
67
  * must have stopped changing for this long before the pane counts as ready. */
68
68
  const INITIAL_INPUT_SETTLE_MS = 1_000;
69
69
 
70
+ /** The WezTerm this machine has, on PATH or where its installer puts it, or null. */
71
+ function weztermProgram() {
72
+ const candidates = process.platform === 'win32'
73
+ ? [join(process.env.ProgramFiles ?? 'C:\\Program Files', 'WezTerm', 'wezterm.exe')]
74
+ : ['/opt/homebrew/bin/wezterm', '/usr/local/bin/wezterm', '/Applications/WezTerm.app/Contents/MacOS/wezterm', '/usr/bin/wezterm'];
75
+ const onPath = spawnSync(process.platform === 'win32' ? 'where' : 'which', ['wezterm'], { encoding: 'utf8', timeout: 3_000 });
76
+ const found = onPath.status === 0 ? onPath.stdout.split(/\r?\n/).map((line) => line.trim()).find(Boolean) : null;
77
+ return found ?? candidates.find((path) => existsSync(path)) ?? null;
78
+ }
79
+
80
+ /** How long a launch watches its new pane before answering, so a program that ends at once is reported. */
81
+ const LAUNCH_SETTLE_MS = 500;
82
+
83
+ /**
84
+ * What a pane runs for a launch: the program with its arguments, or, when the caller named a whole command line
85
+ * as the program (`open --new "npm run build"`), that line through this machine's own shell, so a caller on
86
+ * another machine need not know which shell or platform this one has.
87
+ */
88
+ function launchCommand(program, args) {
89
+ const line = typeof program === 'string' ? program.trim() : '';
90
+ if (!args.length && /\s/.test(line) && !existsSync(line)) {
91
+ return process.platform === 'win32'
92
+ ? { program: 'powershell.exe', arguments: ['-NoProfile', '-Command', line] }
93
+ : { program: process.env.SHELL || '/bin/sh', arguments: ['-lc', line] };
94
+ }
95
+ return { program, arguments: args };
96
+ }
97
+
98
+ /**
99
+ * How a pane that is gone ended, from the pane-exit log its tmux server's hooks write (under SUPERCODE_HOME, or the
100
+ * user's ~/.config/supercode when the server's environment has none): { failed, how } or null when nothing is
101
+ * recorded. A pane that failed has a `died` line with its status or signal; one that ended cleanly only `closed`.
102
+ */
103
+ function paneExit(pane) {
104
+ const logs = [process.env.SUPERCODE_HOME, join(homedir(), '.config', 'supercode')].filter(Boolean).map((dir) => join(dir, 'pane-exits.log'));
105
+ for (const log of logs) {
106
+ let lines;
107
+ try { lines = readFileSync(log, 'utf8').split('\n'); } catch { continue; }
108
+ const died = lines.findLast((entry) => entry.includes(` ${pane} died `));
109
+ const match = died && /status=(\d*) signal=(\S*)/.exec(died);
110
+ if (match) return { failed: true, how: match[1] ? `status ${match[1]}` : match[2] ? `signal ${match[2]}` : 'failed' };
111
+ if (lines.some((entry) => entry.endsWith(` ${pane} closed`))) return { failed: false, how: 'status 0' };
112
+ }
113
+ return null;
114
+ }
115
+
70
116
  export class TeamsMachine extends MachineCore {
71
117
  /**
72
118
  * `link`: { server, teamId, machineId, credential, incarnationId, configuration } to hold the
73
119
  * server link; `configuration()` resolves the machine's current configuration from the server
74
120
  * (custodian and machine grants). Without it the daemon serves its local door only.
75
121
  */
76
- constructor({ home = teamsHome(), supercode = 'supercode', tmuxSocket, floor = null, limits = null, env = process.env, link = null } = {}) {
122
+ constructor({ home = teamsHome(), supercode = 'supercode', tmuxSocket, floor = null, limits = null, env = process.env, link = null, popups = false } = {}) {
77
123
  super({ home, floor, limits, env, link });
78
124
  this.supercodeBin = supercode;
79
125
  this.tmuxSocket = tmuxSocket ?? null;
126
+ // Each pane launched here opens a terminal window on this machine's screen (popupWindow).
127
+ this.popups = Boolean(popups);
80
128
  this.launches = new Launches(this.p.launches);
81
129
  // Windows has no tmux: its panes are ConPTY children of this daemon, and end with it.
82
130
  if (process.platform === 'win32' && !terminal.PtyTerminalHost) throw new Error('panes on Windows need a newer @volter/supercode-terminal (one with PtyTerminalHost)');
@@ -627,6 +675,26 @@ export class TeamsMachine extends MachineCore {
627
675
 
628
676
  listPorts() { return listAnnouncedPorts(this.portsDir); }
629
677
 
678
+ /**
679
+ * A terminal window on this machine's screen showing the pane, so the people at the machine see what runs on it.
680
+ * WezTerm runs one process per window (`start --always-new-process`) with an attach client in it: the pane ending
681
+ * ends the client and the window closes, and a person closing the window ends only the client, never the pane (the
682
+ * pane lives in tmux, or in this daemon's ConPTY host on Windows). No OS automation permission is involved. Without
683
+ * WezTerm there is no window, as before.
684
+ */
685
+ #popupWindow(contextKey, session) {
686
+ const wezterm = weztermProgram();
687
+ if (!wezterm) return;
688
+ const attach = process.platform === 'win32'
689
+ ? [process.execPath, fileURLToPath(new URL('./bin/fleet.mjs', import.meta.url)), 'attach', contextKey, '--mode', 'control']
690
+ : ['tmux', ...(this.tmuxSocket ? ['-L', this.tmuxSocket] : []), 'attach-session', '-t', session.label ?? contextKey];
691
+ try {
692
+ const child = spawn(wezterm, ['start', '--always-new-process', '--', ...attach], { detached: true, stdio: 'ignore', env: { ...this.env, TMUX: '' } });
693
+ child.on('error', () => {});
694
+ child.unref();
695
+ } catch { /* no window; the pane runs regardless */ }
696
+ }
697
+
630
698
  /**
631
699
  * Rename the tmux window behind an owned pane. The terminal host exposes
632
700
  * no rename, so this is `tmux` on the host's own socket; automatic-rename is
@@ -939,7 +1007,7 @@ export class TeamsMachine extends MachineCore {
939
1007
  let session;
940
1008
  try {
941
1009
  session = await this.host.createSession({
942
- command: { program: params.program, arguments: params.arguments ?? [] },
1010
+ command: launchCommand(params.program, params.arguments ?? []),
943
1011
  contextKey,
944
1012
  cwd: params.cwd,
945
1013
  env: this.#paneEnv(contextKey, params.env),
@@ -961,6 +1029,17 @@ export class TeamsMachine extends MachineCore {
961
1029
  // answers "did my message go in, and was it late?" without anyone
962
1030
  // reading the screen.
963
1031
  if (params.initialInput) this.#deliverInitialInput({ key, contextKey, session, kind: params.kind ?? null, input: params.initialInput, act });
1032
+ // A program that ends at once (not found, not runnable, failing on its first line) takes its pane with it:
1033
+ // the caller is told, with its exit status, instead of being handed a pane that is already gone.
1034
+ await new Promise((resolve) => setTimeout(resolve, LAUNCH_SETTLE_MS));
1035
+ if (!await this.#paneByContext(contextKey)) {
1036
+ // Finished (it ran to a clean end) is an answer; exited (it failed at once) is the caller's error.
1037
+ const exit = paneExit(contextKey);
1038
+ const status = exit && !exit.failed ? 'finished' : 'exited';
1039
+ this.launches.record({ key, pane: contextKey, kind: params.kind ?? null, status, error: exit?.failed ? exit.how : undefined });
1040
+ return { key, pane: contextKey, reused: false, status, exit: exit?.how ?? null };
1041
+ }
1042
+ if (this.popups && params.popup !== false) this.#popupWindow(contextKey, session);
964
1043
  return { key, pane: contextKey, reused: false, status: 'ready' };
965
1044
  }
966
1045
  case 'launches.get': {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@volter/supercode-teams",
3
- "version": "0.2.34",
3
+ "version": "0.2.35",
4
4
  "type": "module",
5
5
  "description": "A machine daemon serving this machine's doors, and a local or remote Teams server for shared session discovery, OpenTelemetry ingestion and access to enrolled machines.",
6
6
  "license": "MIT",