@cortexkit/common-auth 0.23.0 → 0.25.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.
@@ -39,6 +39,13 @@ export declare function listSetupProcesses(runner?: CommandRunner, options?: {
39
39
  * a container this process can hold any small PID a fixture uses.
40
40
  */
41
41
  selfPid?: number;
42
+ /**
43
+ * Windows only: answer with one CIM query for the images a fence cares
44
+ * about (host programs, Node and Bun), returning image name, PID and
45
+ * command line together, instead of `tasklist` followed by a second CIM
46
+ * query. The result lists only those processes.
47
+ */
48
+ hostImagesOnly?: boolean;
42
49
  }): Promise<Array<{
43
50
  pid: number;
44
51
  command: string;
@@ -5,6 +5,18 @@ import { defaultCommandRunner } from './command-runner.js';
5
5
  const WINDOWS_CIM_TIMEOUT_MS = 45_000;
6
6
  const HOSTS = new Set(['opencode', 'litecode', 'opencode2', 'pi']);
7
7
  const RUNTIMES = new Set(['node', 'bun']);
8
+ /**
9
+ * The fence's one Windows query. It selects every image whose name starts
10
+ * with a host or runtime name, a superset of what `hostName` can recognize
11
+ * from an image (it strips only an extension), and returns image name, PID
12
+ * and command line from one snapshot. The answer always carries
13
+ * `complete: true`, so an empty or cut-off answer is never mistaken for an
14
+ * empty process list.
15
+ */
16
+ const WINDOWS_FENCE_FILTER = [...HOSTS, ...RUNTIMES]
17
+ .map((name) => `Name LIKE '${name}%'`)
18
+ .join(' OR ');
19
+ const WINDOWS_FENCE_QUERY = `$ErrorActionPreference = 'Stop'; $rows = @(Get-CimInstance Win32_Process -Filter "${WINDOWS_FENCE_FILTER}" | Select-Object Name,ProcessId,CommandLine); ConvertTo-Json -Compress -Depth 3 -InputObject @{ complete = $true; processes = $rows }`;
8
20
  const PI_SCRIPT_PATTERN = /(?:^|[/\\])pi-coding-agent[/\\]dist[/\\](?:(?:bundle|bun)[/\\])?cli\.m?js$/i;
9
21
  /** Splits a command line into arguments, keeping double-quoted ones whole. */
10
22
  function commandArguments(command) {
@@ -169,6 +181,45 @@ export async function listSetupProcesses(runner = defaultCommandRunner, options
169
181
  }
170
182
  };
171
183
  try {
184
+ if (windows && options.hostImagesOnly) {
185
+ const details = await inspect('CIM', 'powershell.exe', ['-NoProfile', '-NonInteractive', '-Command', WINDOWS_FENCE_QUERY], WINDOWS_CIM_TIMEOUT_MS);
186
+ if (details.exitCode !== 0)
187
+ throw new ProcessInspectionError(inspection);
188
+ // An empty or truncated answer fails to parse or lacks `complete`, so a
189
+ // query that silently printed nothing is never read as "no hosts".
190
+ const value = JSON.parse(details.stdout);
191
+ if (!value ||
192
+ typeof value !== 'object' ||
193
+ !('complete' in value) ||
194
+ value.complete !== true ||
195
+ !('processes' in value))
196
+ throw new ProcessInspectionError(inspection);
197
+ const listed = value.processes;
198
+ const rows = listed === null ? [] : Array.isArray(listed) ? listed : [listed];
199
+ const processes = [];
200
+ for (const row of rows) {
201
+ if (!row ||
202
+ typeof row !== 'object' ||
203
+ !('Name' in row) ||
204
+ typeof row.Name !== 'string' ||
205
+ !('ProcessId' in row) ||
206
+ !Number.isSafeInteger(row.ProcessId) ||
207
+ row.ProcessId < 0 ||
208
+ !('CommandLine' in row))
209
+ throw new ProcessInspectionError(inspection);
210
+ const pid = row.ProcessId;
211
+ // As with tasklist, a host program is judged by its image name, and
212
+ // a Node or Bun process by the script on its command line. A runtime
213
+ // whose command line cannot be read cannot be cleared.
214
+ if (!/^(?:node|bun)\.exe$/i.test(row.Name))
215
+ processes.push({ pid, command: row.Name });
216
+ else if (typeof row.CommandLine === 'string')
217
+ processes.push({ pid, command: row.CommandLine });
218
+ else if (pid !== selfPid)
219
+ throw new ProcessInspectionError(inspection);
220
+ }
221
+ return processes;
222
+ }
172
223
  const result = await inspect(windows ? 'tasklist' : 'ps', windows ? 'tasklist.exe' : 'ps', windows ? ['/fo', 'csv', '/nh'] : ['-eo', 'pid,command']);
173
224
  if (result.exitCode !== 0)
174
225
  throw new ProcessInspectionError(inspection);
@@ -246,7 +297,11 @@ export function createProcessFence(runner = defaultCommandRunner, options = {})
246
297
  const isHost = (entry) => entry.pid !== selfPid && hostName(entry.command) !== null;
247
298
  return {
248
299
  async listRunningHosts(env) {
249
- return (await listSetupProcesses(runner, { ...options, env })).filter(isHost);
300
+ return (await listSetupProcesses(runner, {
301
+ ...options,
302
+ env,
303
+ hostImagesOnly: true,
304
+ })).filter(isHost);
250
305
  },
251
306
  async previewRunningHosts(env) {
252
307
  const processes = await listSetupProcesses(runner, {
@@ -125,6 +125,11 @@ export interface SetupIO {
125
125
  handshakeTimeoutMs: number;
126
126
  }) => Promise<ClaustrumEnrollmentConnection>;
127
127
  wait?: (ms: number) => Promise<void>;
128
+ /**
129
+ * Monotonic milliseconds that bound the enrollment wait. Defaults to
130
+ * `performance.now`; tests pair it with `wait` to run a fake clock.
131
+ */
132
+ now?: () => number;
128
133
  }
129
134
  export interface HostDetection {
130
135
  installed: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cortexkit/common-auth",
3
- "version": "0.23.0",
3
+ "version": "0.25.0",
4
4
  "description": "Shared code for the CortexKit auth plugins: account pool, quota and routing, commands and auth menu, OpenCode 2 hooks, Claustrum custody, and plumbing (loopback RPC, file locks, logger, sidebar state, TUI preferences and build).",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -140,7 +140,7 @@
140
140
  },
141
141
  "devDependencies": {
142
142
  "@biomejs/biome": "2.5.14",
143
- "@cortexkit/claustrum-client": "^0.12.0",
143
+ "@cortexkit/claustrum-client": "^0.13.0",
144
144
  "@cortexkit/subc-client": "0.22.2",
145
145
  "@earendil-works/pi-coding-agent": "1.0.4",
146
146
  "@opencode/plugin": "2.0.21",