@ctrl-spc/cs 0.2.0 → 0.3.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/dist/presence.js CHANGED
@@ -2,6 +2,7 @@ import { platform } from 'node:os';
2
2
  import { getClient } from './supabase.js';
3
3
  import { getMachineIdentity, supersededMachineIds, clearSupersededMachineIds } from './config.js';
4
4
  import { detectAgents } from './agents.js';
5
+ import { startToolsServer, stopToolsServer, toolsServerStatus, registerWithClaude, registerWithCodex, unregisterFromClaude, unregisterFromCodex, agentRegStatus, heartbeatOpenSessions, setToolsClient, } from './mcp.js';
5
6
  import { HEARTBEAT_INTERVAL_MS, COMMAND_POLL_INTERVAL_MS } from './env.js';
6
7
  let presence = null;
7
8
  /** In-flight guard: startPresence yields to the event loop (network setSession)
@@ -33,9 +34,41 @@ async function heartbeat(p) {
33
34
  console.warn(`heartbeat failed, will retry: ${err.message}`);
34
35
  try {
35
36
  p.client = await getClient();
37
+ // FIX 2: flow the rebuilt client (fresh/refreshed token) into the tools
38
+ // session-lifecycle heartbeat too, so a wedged token can't leave the session
39
+ // heartbeat 401ing forever (the CLI-v1 stale-token root cause). No-op unless
40
+ // the tools server is running.
41
+ setToolsClient(p.client);
36
42
  }
37
43
  catch { /* stay down until the next tick */ }
38
44
  }
45
+ // Re-attempt agent registration each heartbeat for any detected agent still
46
+ // idle or failed, while the tools server is up. Registration otherwise fires
47
+ // only ONCE at startPresence against that instant's detectAgents(): an agent
48
+ // installed AFTER sign-in would stay 'idle' forever (badge stuck "connecting…")
49
+ // and a 'failed' registration would never retry (making the "will retry" copy a
50
+ // lie). registerWithX's double-kick guard skips agents already 'registering',
51
+ // and 'registered' agents aren't retried, so this is safe to call every tick.
52
+ // Runs only in the presence process and stops when presence stops (this is the
53
+ // heartbeat, whose interval stopPresence clears).
54
+ // ponytail: a genuinely-unregisterable agent retries every ~10s indefinitely;
55
+ // add backoff if that churn ever matters.
56
+ const server = toolsServerStatus();
57
+ if (server.running) {
58
+ const status = agentRegStatus();
59
+ for (const agent of detectAgents()) {
60
+ if (status[agent] !== 'idle' && status[agent] !== 'failed')
61
+ continue;
62
+ if (agent === 'claude')
63
+ registerWithClaude(server.port);
64
+ else
65
+ registerWithCodex(server.port);
66
+ }
67
+ // D2b: while the tools server is up, keep every open work session's
68
+ // last_seen_at fresh so the web presence "working" chip stays lit while
69
+ // agents work. Best-effort/no-throw, exactly like the block above.
70
+ await heartbeatOpenSessions();
71
+ }
39
72
  }
40
73
  /** Delete cloud rows for machine ids this install has migrated off of, so an old
41
74
  * per-install id can't linger as a duplicate "offline" row for the same box.
@@ -114,6 +147,20 @@ export async function startPresence() {
114
147
  await cleanupSupersededRows(p);
115
148
  p.hb = setInterval(() => void heartbeat(p), HEARTBEAT_INTERVAL_MS);
116
149
  p.cp = setInterval(() => void pollCommands(p), COMMAND_POLL_INTERVAL_MS);
150
+ // Bring up the local ctrl-spc MCP tools server and register it into Claude
151
+ // so any agent run on this machine has the tools with zero setup. Strictly
152
+ // best-effort: a tools-server or registration failure must never break the
153
+ // presence heartbeat above, so it's caught and warned like everything here.
154
+ try {
155
+ await startToolsServer({ client, userId, machineId: identity.id });
156
+ if (agents.includes('claude'))
157
+ registerWithClaude(toolsServerStatus().port);
158
+ if (agents.includes('codex'))
159
+ registerWithCodex(toolsServerStatus().port);
160
+ }
161
+ catch (err) {
162
+ console.warn(`Agent tools server did not start: ${err.message}`);
163
+ }
117
164
  return { machineName: identity.name, agents };
118
165
  })();
119
166
  try {
@@ -124,14 +171,29 @@ export async function startPresence() {
124
171
  }
125
172
  }
126
173
  /** Go offline. Best-effort stamps last_seen_at into the past so the web sheet
127
- * reads offline immediately instead of waiting out the freshness window. */
128
- export async function stopPresence({ markOffline = true } = {}) {
174
+ * reads offline immediately instead of waiting out the freshness window. Pass
175
+ * `unregister` (logout only, never plain shutdown) to also remove the ctrl-spc
176
+ * entry from each detected agent's config. */
177
+ export async function stopPresence({ markOffline = true, unregister = false } = {}) {
129
178
  const p = presence;
130
179
  if (!p)
131
180
  return;
132
181
  presence = null;
133
182
  clearInterval(p.hb);
134
183
  clearInterval(p.cp);
184
+ // On logout (unregister), scrub the ctrl-spc entry from each detected agent's
185
+ // config so a logged-out machine leaves no dead server that would read "failed
186
+ // to connect" on the next agent run. Fire-and-forget best-effort — never blocks
187
+ // logout. On a plain SIGINT shutdown the entries are intentionally left in place
188
+ // (the caller passes no `unregister`); the badge just reads not-connected
189
+ // because the server below is torn down.
190
+ if (unregister) {
191
+ if (p.agents.includes('claude'))
192
+ void unregisterFromClaude();
193
+ if (p.agents.includes('codex'))
194
+ void unregisterFromCodex();
195
+ }
196
+ await stopToolsServer().catch((err) => console.warn(`Agent tools server did not stop cleanly: ${err.message}`));
135
197
  if (markOffline) {
136
198
  try {
137
199
  await p.client
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ctrl-spc/cs",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "CTRL+SPC — minimal, reliable per-machine agent presence. Sign-in, auto-start, agent detection, heartbeat presence, and ping acknowledgement.",
5
5
  "engines": {
6
6
  "node": ">=22"
@@ -21,7 +21,9 @@
21
21
  },
22
22
  "license": "UNLICENSED",
23
23
  "dependencies": {
24
- "@supabase/supabase-js": "^2.110.1"
24
+ "@modelcontextprotocol/sdk": "^1.29.0",
25
+ "@supabase/supabase-js": "^2.110.1",
26
+ "zod": "^4.4.3"
25
27
  },
26
28
  "devDependencies": {
27
29
  "@types/node": "^26.1.1",