@comput/pi-intercom 0.12.0 → 0.13.0

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/src/snapshot.ts CHANGED
@@ -86,7 +86,8 @@ export async function readObservationSnapshot(root: string): Promise<Observation
86
86
  result.truncated ||= config.agents.length > MAX_AGENTS;
87
87
  result.config = { multiplexer: config.multiplexer, agents: config.agents.slice(0, MAX_AGENTS).map(a => ({
88
88
  sessionId: a.sessionId, name: a.name, coordinator: a.coordinator, description: a.description,
89
- projectDirectory: a.projectDirectory, port: a.port, ...publicCloseMetadata(a),
89
+ projectDirectory: a.projectDirectory, port: a.port,
90
+ ...(!a.coordinator ? { worktree: a.worktree, branch: a.branch } : {}), ...publicCloseMetadata(a),
90
91
  })) };
91
92
  } catch { result.errors.push('config_unavailable'); }
92
93
  try {
package/src/transport.ts CHANGED
@@ -37,7 +37,7 @@ export function envelope(value: unknown): Envelope {
37
37
  return m;
38
38
  }
39
39
  export interface Endpoint { port: number; close(): Promise<void> }
40
- export async function listen(preferred: number | undefined, accept: (message: Envelope) => Promise<void>, health?: () => { version: 1; sessionId: string }): Promise<Endpoint> {
40
+ export async function listen(preferred: number | undefined, accept: (message: Envelope) => Promise<void>, health?: () => { version: 1; sessionId: string; busy?: boolean }): Promise<Endpoint> {
41
41
  const server = http.createServer(async (req, res) => {
42
42
  const reply = (status: number, data: unknown) => { if (!res.destroyed) { res.writeHead(status, { 'content-type': 'application/json', connection: 'close' }); res.end(JSON.stringify(data)); } };
43
43
  if (req.method === 'GET' && req.url === '/intercom/health') {
@@ -46,7 +46,7 @@ export async function listen(preferred: number | undefined, accept: (message: En
46
46
  else {
47
47
  const identity = health();
48
48
  if (identity.version !== 1 || typeof identity.sessionId !== 'string' || !identity.sessionId.trim() || identity.sessionId.length > 256) throw new Error('invalid health identity');
49
- reply(200, { version: 1, sessionId: identity.sessionId });
49
+ reply(200, { version: 1, sessionId: identity.sessionId, ...(typeof identity.busy === 'boolean' ? { busy: identity.busy } : {}) });
50
50
  }
51
51
  } catch { reply(503, { error: 'health unavailable' }); }
52
52
  req.resume(); return;
@@ -60,7 +60,10 @@ export function workerStatusPage(snapshot: ObservationSnapshot, options: { name?
60
60
  const age = now - Date.parse(timestamp);
61
61
  return Number.isFinite(age) && age >= 0 ? Math.floor(age / 1000) : null;
62
62
  };
63
- const entry = { sessionId: agent.sessionId, name: agent.name, ...workerObservation(snapshot, agent.sessionId, now),
63
+ const entry = { sessionId: agent.sessionId, name: agent.name, description: agent.description,
64
+ worktree: agent.worktree ?? null, branch: agent.branch ?? null,
65
+ configuration: agent.worktree && agent.branch ? 'configured (not proof of loaded configuration)' : 'reconfiguration_required',
66
+ ...workerObservation(snapshot, agent.sessionId, now),
64
67
  connection: currentConnection(agent.sessionId, snapshot.connections, now),
65
68
  handoff: saved.handoff ? { ...saved.handoff, ageSeconds: ageSeconds(saved.handoff.updatedAt), selfReported: true } : null,
66
69
  closeJob: saved.closeJob ? { ...saved.closeJob, ageSeconds: ageSeconds(saved.closeJob.updatedAt) } : null,
@@ -0,0 +1,51 @@
1
+ import { execFile } from 'node:child_process';
2
+ import { realpath, stat } from 'node:fs/promises';
3
+ import path from 'node:path';
4
+ import { promisify } from 'node:util';
5
+ import { fail, requireWorkspace, type Agent } from './config.js';
6
+
7
+ const exec = promisify(execFile);
8
+ const CHECK_TIMEOUT_MS = 3000;
9
+ export interface WorkerWorkspace { worktree: string; branch: string }
10
+
11
+ /** Compare the loaded contract, not transport ports or optional workflow metadata. */
12
+ export function sameResponsibility(a: Agent, b: Agent): boolean {
13
+ return a.sessionId === b.sessionId && a.coordinator === b.coordinator && a.name === b.name &&
14
+ a.description === b.description && a.projectDirectory === b.projectDirectory &&
15
+ a.worktree === b.worktree && a.branch === b.branch;
16
+ }
17
+
18
+ /** Read-only Git inspection. No shell, hooks, writes, retries, or inherited Git routing. */
19
+ export async function validateWorkerWorkspace(root: string, agent: Pick<Agent, 'worktree' | 'branch'>): Promise<WorkerWorkspace> {
20
+ requireWorkspace(agent);
21
+ const deadline = Date.now() + CHECK_TIMEOUT_MS;
22
+ const env = Object.fromEntries(Object.entries(process.env).filter(([key]) => !key.toUpperCase().startsWith('GIT_')));
23
+ env.GIT_OPTIONAL_LOCKS = '0';
24
+ env.GIT_TERMINAL_PROMPT = '0';
25
+ const git = async (cwd: string, args: string[]): Promise<string> => {
26
+ const timeout = deadline - Date.now();
27
+ if (timeout <= 0) fail('worker workspace validation timed out');
28
+ try {
29
+ const result = await exec('git', ['-C', cwd, ...args], { env, timeout, maxBuffer: 16384, windowsHide: true, encoding: 'utf8' });
30
+ return result.stdout.trimEnd();
31
+ } catch { return fail('worker workspace Git check failed (Git unavailable, invalid repository, detached HEAD, or timeout)'); }
32
+ };
33
+ let base: string, worktree: string;
34
+ try {
35
+ [base, worktree] = await Promise.all([realpath(root), realpath(path.resolve(root, agent.worktree))]);
36
+ if (!(await stat(worktree)).isDirectory()) fail('worker worktree is not a directory');
37
+ } catch { return fail('worker worktree directory is missing or inaccessible'); }
38
+ if (path.relative(base, worktree) === '') fail('worker worktree must be separate from the project root');
39
+ const [rootCommon, metadata, ref] = await Promise.all([
40
+ git(base, ['rev-parse', '--path-format=absolute', '--git-common-dir']),
41
+ git(worktree, ['rev-parse', '--path-format=absolute', '--show-toplevel', '--git-common-dir']),
42
+ git(worktree, ['symbolic-ref', '--quiet', 'HEAD']),
43
+ ]);
44
+ const parts = metadata.split(/\r?\n/);
45
+ if (parts.length !== 2) fail('invalid worker worktree metadata');
46
+ const [top, common, projectCommon] = await Promise.all([realpath(parts[0]), realpath(parts[1]), realpath(rootCommon)]);
47
+ if (path.relative(top, worktree) !== '') fail('worker worktree must be the worktree root, not a subdirectory');
48
+ if (path.relative(common, projectCommon) !== '') fail('worker worktree belongs to a different repository');
49
+ if (ref !== `refs/heads/${agent.branch}`) fail('worker worktree is not on its configured branch');
50
+ return { worktree, branch: agent.branch };
51
+ }