@volter/supercode-teams 0.2.166 → 0.2.167

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.
@@ -1,6 +1,6 @@
1
1
  const BOOLEANS = new Set([
2
2
  'json', 'help', 'live', 'saved', 'watch', 'paginate', 'token-stdin',
3
- 'invite-stdin', 'include-children', 'rotate', 'install', 'force',
3
+ 'invite-stdin', 'enrollment-stdin', 'include-children', 'rotate', 'install', 'force',
4
4
  'take-control', 'dry-run', 'volter', 'foreground', 'no-connect', 'record-only', 'workplace-only',
5
5
  ]);
6
6
 
@@ -11,7 +11,7 @@ const VALUES = new Set([
11
11
  'out', 'ttl', 'type', 'name', 'visibility', 'backfill', 'format', 'to',
12
12
  'grant', 'binding', 'caps', 'fidelity', 'cwd', 'root', 'workspace-from', 'since', 'until', 'session',
13
13
  'on', 'timeout', 'plan', 'tmux-socket', 'volter-issuer', 'workplace', 'organization', 'key', 'room',
14
- 'conversation', 'rh2', 'scope', 'agent', 'body-file',
14
+ 'conversation', 'rh2', 'scope', 'agent', 'body-file', 'enrollment',
15
15
  ]);
16
16
 
17
17
  export function parseArgs(argv) {
package/bin/workspace.mjs CHANGED
@@ -2,7 +2,7 @@
2
2
  import { hostname } from 'node:os';
3
3
  import { fileURLToPath } from 'node:url';
4
4
  import { existsSync, readFileSync, realpathSync, rmSync } from 'node:fs';
5
- import { basename, join, resolve } from 'node:path';
5
+ import { basename, isAbsolute, join, resolve } from 'node:path';
6
6
  import { execFileSync, spawn } from 'node:child_process';
7
7
  import { createHash } from 'node:crypto';
8
8
  import { SupercodeHarnessClient } from '@volter/supercode-harness-sdk';
@@ -47,6 +47,7 @@ export async function workspaceMain(argv = process.argv.slice(2), env = process.
47
47
  if (verb === 'server') return serverCommand({ flags, sub, rest, home, output });
48
48
  if (verb === 'login') return loginCommand({ flags, origin: sub, rest, home, output });
49
49
  if (verb === 'auth') return authCommand({ flags, sub, rest, home, contexts, output, env, dependencies });
50
+ if (verb === 'connect' && flags.enrollment_stdin) adoptEnrollment(contexts, home, flags);
50
51
  const explicitContext = flags.context ?? env.SUPERCODE_CONTEXT;
51
52
  let selected = explicitContext ?? contexts.read().current;
52
53
  const urlReference = verb === 'sessions' ? rest.find(value => /^https?:\/\//.test(value)) : null;
@@ -55,6 +56,8 @@ export async function workspaceMain(argv = process.argv.slice(2), env = process.
55
56
  if (selected === 'local') return localCommand({ verb, sub, rest, flags, output });
56
57
  const { client, context } = fromContext({ supercodeHome: home, name: selected, fetch: globalThis.fetch });
57
58
  const team = client.team(context.team_id);
59
+ // A machine-only context (a box enrolled by its custodian) holds its machine credential and nothing a person has.
60
+ if (context.machine_only && !['connect', 'disconnect', 'status'].includes(verb)) usage(`context ${selected} is machine ${context.machine_name}'s: run teams ${verb} where a person is signed in`);
58
61
  const command = { flags, rest, output, client, context, team, explicitContext, home, contextName: selected, env, dependencies };
59
62
  if (teamsPrefix && ['list', 'create'].includes(verb)) return teamsCommand(verb, sub, command);
60
63
  if (verb === 'apps' || verb === 'app-installations') return appsCommand(verb,sub,{...command,output:value=>output(flags.json?value:redactAppSecrets(value))});
@@ -588,6 +591,26 @@ async function sourcesCommand(sub, command) {
588
591
  }
589
592
 
590
593
  async function machinesCommand(sub, { flags, rest, output, client, context, team }) {
594
+ if (sub === 'remove') {
595
+ // The custodian revokes a machine: its credentials, links and grants end, and every later door is refused.
596
+ requireArgs(rest, 1, 1, 'teams machines remove MACHINE_ID'); const current = await team.machines.get(rest[0]);
597
+ await client.request('DELETE', teamPath(context, `/machines/${rest[0]}`), { ifMatch: current.data.revision });
598
+ return output({ version: 1, context: scope(context), data: { removed: rest[0] } });
599
+ }
600
+ if (sub === 'enroll') {
601
+ // The signed-in person enrolls another machine as its custodian. The file carries only that machine's own
602
+ // credential; the machine connects from it (teams connect --enrollment-stdin) and never holds a person's.
603
+ requireArgs(rest, 0, 0, 'teams machines enroll --name NAME --out PATH'); if (!flags.name) usage('--name is required');
604
+ if (!flags.out) usage('--out is required to deliver the machine credential'); const destination = reserveProtected(flags.out, { force: flags.force });
605
+ try {
606
+ const enrollment_key = crypto.randomUUID();
607
+ const response = await team.machines.create({ name: flags.name, enrollment_key });
608
+ let credential = response.data.credential;
609
+ if (!credential) credential = (await client.request('POST', teamPath(context, `/sources/${response.data.source.id}/credentials`), { body: { rotate: true } })).data;
610
+ destination.write({ version: 1, kind: 'machine-enrollment', origin: context.origin, server_id: context.server_id, team_id: context.team_id, team_name: context.team_name ?? null, enrollment_key, machine: response.data.machine, source: response.data.source, credential });
611
+ return output({ version: 1, context: scope(context), data: { machine: response.data.machine, source: response.data.source, written_to: resolve(flags.out) } });
612
+ } catch (error) { destination.abort(); throw error; }
613
+ }
591
614
  if (['subscribe','unsubscribe','events'].includes(sub)) {
592
615
  requireArgs(rest,sub==='events'?0:1,sub==='events'?0:1,`teams machines ${sub} ${sub==='events'?'':'ADDRESS'}`);
593
616
  const channel=await openLocalMachine(paths(teamsHome()).socket);
@@ -647,11 +670,24 @@ async function machinesCommand(sub, { flags, rest, output, client, context, team
647
670
  return output(await client.request('POST', machineBase, { body: { subject_id: subject, caps, targets, expires_at: Date.now() + ttl } }));
648
671
  }
649
672
  }
650
- usage('teams machines list|roots|access|authorize-continuation|subscribe|unsubscribe|events');
673
+ usage('teams machines list|roots|access|enroll|remove|authorize-continuation|subscribe|unsubscribe|events');
651
674
  }
652
675
 
653
676
  async function syncCommand(sub, { flags, rest, output, client, context, team, home }) {
654
677
  if (sub === 'list') return output(await team.syncRules.list(optional(flags, ['machine'])));
678
+ if (sub === 'add' && flags.enrollment) {
679
+ // A custodian's rule for a machine it enrolled: the path is the machine's own, and its root id is the one the
680
+ // machine's connector registers for it (sha256 of machine id and path). The harness is named, as this computer
681
+ // cannot ask that machine's native core.
682
+ requireArgs(rest, 0, 0, 'teams sync add --enrollment FILE --repo PATH --harness ID'); if (!flags.repo) usage('--repo is required'); if (!flags.harness) usage('--harness is required with --enrollment');
683
+ const enrolled = JSON.parse(readFileSync(resolve(flags.enrollment), 'utf8'));
684
+ if (enrolled.kind !== 'machine-enrollment' || enrolled.team_id !== context.team_id) usage('--enrollment names no machine of this team');
685
+ // connect registers the folder's real path; name it the same way (absolute, normalized, no symlink).
686
+ if (!isAbsolute(flags.repo)) usage('--repo with --enrollment is the machine\'s absolute path');
687
+ const repo = resolve(flags.repo);
688
+ const root_id = `root_${createHash('sha256').update(`${enrolled.machine.id}\0${repo}`).digest('hex').slice(0, 32)}`;
689
+ return output(await team.syncRules.create({ machine_id: enrolled.machine.id, source_id: enrolled.source.id, selector: { root_id, harnesses: [flags.harness] }, visibility: flags.visibility ?? 'restricted', backfill: backfill(flags.backfill ?? 'none') }));
690
+ }
655
691
  if (sub === 'add') {
656
692
  requireArgs(rest, 0, 0, 'teams sync add --repo PATH'); if (!flags.repo) usage('--repo is required');
657
693
  const enrollment = new WorkspaceContexts(home).connector(context.server_id, context.team_id).read();
@@ -683,16 +719,38 @@ async function stopRunningConnector(saved) {
683
719
  for (let waited = 0; running.pid > 0 && alive() && waited < 15_000; waited += 250) await new Promise(resolve => setTimeout(resolve, 250));
684
720
  }
685
721
 
722
+ /**
723
+ * The machine side of `teams machines enroll`: the enrollment on stdin becomes this home's machine-only context (its
724
+ * machine credential, never a person's) and its connector state, and the connect that follows starts from them.
725
+ */
726
+ function adoptEnrollment(contexts, home, flags) {
727
+ let enrolled;
728
+ try { enrolled = JSON.parse(readFileSync(0, 'utf8')); } catch { usage('stdin is not JSON: pipe the file teams machines enroll --out wrote'); }
729
+ if (enrolled?.version !== 1 || enrolled.kind !== 'machine-enrollment' || !enrolled.credential?.token || !enrolled.machine?.id || !enrolled.source?.id || !enrolled.enrollment_key) usage('stdin is not a machine enrollment (teams machines enroll --out)');
730
+ const connector = contexts.connector(enrolled.server_id, enrolled.team_id), saved = connector.read();
731
+ if (saved?.machine && saved.machine.id !== enrolled.machine.id) usage(`this home is already connected as ${saved.machine.name ?? saved.machine.id}; disconnect it first`);
732
+ const name = flags.context ?? 'machine', existing = contexts.read().contexts[name];
733
+ if (existing && !(existing.machine_only && existing.server_id === enrolled.server_id && existing.team_id === enrolled.team_id)) usage(`context ${name} is not this machine's; name another with --context`);
734
+ const credentialId = `cred_${crypto.randomUUID().replaceAll('-', '')}`;
735
+ contexts.writeCredential(credentialId, { token: enrolled.credential.token, kind: 'machine', expires_at: enrolled.credential.expires_at ?? null, server_id: enrolled.server_id, origin: enrolled.origin });
736
+ contexts.upsert(name, { origin: enrolled.origin, server_id: enrolled.server_id, team_id: enrolled.team_id, team_name: enrolled.team_name ?? null, principal_id: null, credential_ref: credentialId, label: null, machine_only: true, machine_name: enrolled.machine.name });
737
+ if (!saved?.machine) connector.write({ version: 1, enrollment_key: enrolled.enrollment_key, phase: 'enrolled', machine: enrolled.machine, source: enrolled.source, credential: enrolled.credential });
738
+ if (!contexts.read().current) contexts.use(name);
739
+ flags.context = name;
740
+ }
741
+
686
742
  async function connectorCommand(verb, { flags, rest, output, client, context, team, contextName }, home) {
687
743
  requireArgs(rest, 0, 0, `teams ${verb}`); const state = new WorkspaceContexts(home).connector(context.server_id, context.team_id);
688
744
  if (verb === 'status') {
689
- let reachable = true; let account = null; try { account = await client.me(); } catch { reachable = false; }
745
+ // A machine has no account to name: its own door answering its credential is the answer.
746
+ let reachable = true; let account = null;
747
+ try { if (context.machine_only) { const enrolled = state.read(); await client.request('GET', teamPath(context, `/machines/${enrolled?.machine?.id}/configuration`)); } else account = await client.me(); } catch { reachable = false; }
690
748
  const saved = state.read(); const running = saved?.process?.socket ? await activeSocket(saved.process.socket) : false;
691
749
  // A connector deep in a large capture can miss the probe's second while its process lives on: it is busy, not stopped.
692
750
  const busy = !running && Boolean(saved?.process) && lifecycleHeld(state.directory);
693
751
  // What the connector could not sync, as it last wrote it to its state (it is written while it runs).
694
752
  const sync = saved ? readSyncStatus(state.directory) ?? { not_syncing: [], last_error: null, queued_captures: 0 } : null;
695
- const value = { version: 1, context: scope(context), data: { reachable, account, connector: saved ? { enrolled: true, running: running || busy, ...(busy ? { busy: true } : {}), machine: saved.machine, source: saved.source, sync } : { enrolled: false, running: false }, sources: reachable ? (await team.sources.list()).items : [] } };
753
+ const value = { version: 1, context: scope(context), data: { reachable, account, connector: saved ? { enrolled: true, running: running || busy, ...(busy ? { busy: true } : {}), machine: saved.machine, source: saved.source, sync } : { enrolled: false, running: false }, sources: reachable && !context.machine_only ? (await team.sources.list()).items : [] } };
696
754
  if (flags.json) return output(value);
697
755
  const lines = [`Team ${context.team_name ?? context.team_id} · ${new URL(context.origin).host}${reachable ? '' : ' · unreachable'}`];
698
756
  lines.push(saved?.machine ? `Connector ${saved.machine.name ?? saved.machine.id} · ${running ? 'running' : busy ? 'running (busy: its control socket did not answer within 1 s)' : 'not running'}${saved.service?.kind ? ` (${saved.service.kind})` : ''}` : 'Connector not connected (supercode teams connect)');
@@ -846,6 +904,9 @@ async function connectorCommand(verb, { flags, rest, output, client, context, te
846
904
  }
847
905
  // Uninstalling a Windows task already stopped its connector, so only one still answering is asked to stop.
848
906
  if (enrollment.process?.socket && enrollment.process?.token && await activeSocket(enrollment.process.socket)) await requestStop(enrollment.process.socket, enrollment.process.token);
907
+ // A machine its custodian enrolled stops here; revoking it is the custodian's (teams machines remove).
908
+ // It keeps its enrollment, so connect resumes it; the custodian's removal is what ends it.
909
+ if (context.machine_only) { state.update((current) => ({ ...current, process: null, service: null })); return output({ version: 1, context: scope(context), data: { disconnected: true, machine_id: enrollment.machine.id, revoked: false } }); }
849
910
  const machine = await team.machines.get(enrollment.machine.id);
850
911
  await client.request('DELETE', teamPath(context, `/machines/${enrollment.machine.id}`), { ifMatch: machine.data.revision });
851
912
  state.remove();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@volter/supercode-teams",
3
- "version": "0.2.166",
3
+ "version": "0.2.167",
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",