@treestructuredplanning/gateway 0.1.1 → 0.1.2

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/README.md CHANGED
@@ -18,8 +18,9 @@ only connects outward to TSP.
18
18
  run without an OS credential store.
19
19
  - Build must be available for your TSP account.
20
20
 
21
- `connect` and `tsp-gateway status` print the steps left to you for each folder, such as
22
- installing or signing in to a harness or accepting its trust prompt.
21
+ `connect` and `tsp-gateway status` say which harnesses can build in each folder and what is left to
22
+ you: installing or signing in to a harness, or accepting its trust prompt. Once one harness is ready
23
+ in a folder, steps for your other harnesses are optional.
23
24
 
24
25
  ## Connect this computer
25
26
 
@@ -39,8 +40,9 @@ it again is safe:
39
40
  kept.
40
41
  3. It registers the folder.
41
42
  4. It installs a login item that starts the gateway when you log in.
42
- 5. It prints the gateway's status and any steps only you can take in each folder: install a
43
- harness, sign in, or run the harness there once to accept its trust prompt.
43
+ 5. It prints a short summary: which harnesses are ready in each folder, and the steps only you can
44
+ take (install a harness, sign in, or run it there once to accept its trust prompt). They are
45
+ required until one harness is ready, and optional after that.
44
46
 
45
47
  Run outside any clone, `connect` keeps the folders already registered and repairs the rest,
46
48
  so it also fixes a stopped, outdated or revoked gateway. If TSP cannot be reached, nothing
@@ -71,19 +73,20 @@ With a global install (`npm install --global @treestructuredplanning/gateway@lat
71
73
  command is `tsp-gateway`; with `npx`, use `npx -y @treestructuredplanning/gateway@latest`
72
74
  in its place.
73
75
 
74
- | Command | What it does |
75
- | --------------------------- | ---------------------------------------------------------------------------- |
76
- | `connect [folder]` | Pair if needed, register the folder, install the login item, print status. |
77
- | `disconnect` | Remove the login item, then unpair. |
78
- | `pair` | Pair this computer with your TSP account (replaces any existing pairing). |
79
- | `add <folder>` | Register a repository folder. |
80
- | `remove <owner/name>` | Unregister a repository. |
81
- | `configure-launch '<json>'` | Set how sessions get your shell environment (see below). |
82
- | `status` | Show pairing, connection, harness sign-in and trust, and registered folders. |
83
- | `start` | Run the gateway in the foreground. Ctrl-C stops it, not your sessions. |
84
- | `install` | Install or refresh the login item. Run it again after upgrading the gateway. |
85
- | `uninstall` | Remove the login item; keep the pairing and folders. |
86
- | `unpair` | Revoke this computer's pairing in TSP and delete the stored credential. |
76
+ | Command | What it does |
77
+ | ----------------------------- | ------------------------------------------------------------------------------- |
78
+ | `connect [folder]` | Pair if needed, register the folder, install the login item, print a summary. |
79
+ | `disconnect` | Remove the login item, then unpair. |
80
+ | `pair` | Pair this computer with your TSP account (replaces any existing pairing). |
81
+ | `add <folder>` | Register a repository folder. |
82
+ | `remove <owner/name>` | Unregister a repository. |
83
+ | `configure-launch '<json>'` | Set how sessions get your shell environment (see below). |
84
+ | `configure-reporting <level>` | `activity` (default) or `off`: whether session activity is sent to TSP. |
85
+ | `status [--json]` | Show pairing, connection and each folder's next steps; `--json` is for scripts. |
86
+ | `start` | Run the gateway in the foreground. Ctrl-C stops it, not your sessions. |
87
+ | `install` | Install or refresh the login item. Run it again after upgrading the gateway. |
88
+ | `uninstall` | Remove the login item; keep the pairing and folders. |
89
+ | `unpair` | Revoke this computer's pairing in TSP and delete the stored credential. |
87
90
 
88
91
  Registering or removing a folder takes effect in the running gateway without a restart.
89
92
 
@@ -123,8 +126,11 @@ needed and never stops.
123
126
 
124
127
  The gateway sends TSP only repository names (`owner/name`), which harnesses are available,
125
128
  signed in and trusted per folder, and session status, including any waiting message with
126
- folder paths removed. Folder paths, environment variables, command lines, transcripts and raw
127
- error output stay on your computer.
129
+ folder paths removed. For each launched session it also sends what the session is doing
130
+ (reading, editing, running a command), when it was last active, and counts of files edited
131
+ and commands run; never file names, commands or transcript text. Folder paths, environment
132
+ variables, command lines, transcripts and raw error output stay on your computer. Run
133
+ `tsp-gateway configure-reporting off` to stop sending activity (`activity` turns it back on).
128
134
 
129
135
  The gateway only connects outward, to TSP, and accepts no inbound connections. The only
130
136
  exception is pairing: while you approve in the browser, it listens briefly on `127.0.0.1`
@@ -143,7 +149,9 @@ Supported shells are bash, zsh, ksh, fish, csh, tcsh and sh. With any other shel
143
149
  the gateway falls back to `/bin/sh`, then to starting the harness directly. On Windows the
144
150
  harness always starts directly and PowerShell or cmd profiles are not loaded. Your shell
145
151
  startup must finish quickly, must not wait for input, and must not redirect its own output to
146
- a file or terminal.
152
+ a file or terminal. If it takes longer than 10 seconds, for example on a busy computer, the
153
+ session starts with the environment your shell gave the gateway last time; the first time, the
154
+ gateway waits up to 30 seconds before it gives up.
147
155
 
148
156
  Change this with `configure-launch`, which sets the `launch` section of the gateway's config
149
157
  file. Omitted settings use defaults:
package/dist/cli.js CHANGED
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import { createRequire } from "node:module"; const require = createRequire(import.meta.url);
3
3
  import {
4
+ ActivityReporter,
4
5
  Commands,
5
6
  GATEWAY_CLOSE_DISPOSITION,
6
7
  GatewayStatus,
@@ -11,12 +12,15 @@ import {
11
12
  awaitDaemonConnection,
12
13
  builtinHarnesses,
13
14
  connect,
15
+ connectLines,
14
16
  daemonConnection,
15
17
  daemonLock,
16
18
  decodeLaunchConfig,
19
+ decodeReporting,
17
20
  disconnect,
18
21
  execute,
19
22
  field,
23
+ folderSetups,
20
24
  installLoginItem,
21
25
  isObject,
22
26
  keychain,
@@ -26,15 +30,15 @@ import {
26
30
  privateDirectory,
27
31
  readConfig,
28
32
  readLaunchObservation,
29
- remainingSteps,
30
33
  removeLoginItem,
31
34
  runDaemon,
32
35
  saveConfig,
36
+ statusLines,
33
37
  unpairMachine,
34
38
  validSlug,
35
39
  validateOrigin,
36
40
  watchConfig
37
- } from "./shared-3OFEPCJI.js";
41
+ } from "./shared-M7CEELPK.js";
38
42
 
39
43
  // src/cli.ts
40
44
  import { readFile } from "node:fs/promises";
@@ -46,7 +50,7 @@ import { parseArgs } from "node:util";
46
50
  // package.json
47
51
  var package_default = {
48
52
  name: "@treestructuredplanning/gateway",
49
- version: "0.1.1",
53
+ version: "0.1.2",
50
54
  description: "Outbound local harness gateway for Tree-Structured Planning",
51
55
  type: "module",
52
56
  engines: {
@@ -121,7 +125,9 @@ async function runLocal(loginPath, paths, config, version) {
121
125
  });
122
126
  const status = new GatewayStatus(join(paths.state, "status.json"), executor);
123
127
  const drivers = builtinHarnesses(environment, executor);
124
- const commands = new Commands(journal, drivers, repositories);
128
+ const commands = new Commands(journal, drivers, repositories, {
129
+ activity: new ActivityReporter(async () => (await readConfig(paths.config)).reporting)
130
+ });
125
131
  await runDaemon({
126
132
  pairing,
127
133
  version,
@@ -143,7 +149,7 @@ async function runLocal(loginPath, paths, config, version) {
143
149
  await release();
144
150
  }
145
151
  }
146
- async function printStatus(paths, config, drivers, executor, idle = "Not running") {
152
+ async function readStatus(paths, config, drivers, executor, idle = "Not running") {
147
153
  const connection = await daemonConnection(join(paths.state, "status.json")) ?? idle;
148
154
  let daemonLaunch = null;
149
155
  let lastMessage = null;
@@ -161,25 +167,42 @@ async function printStatus(paths, config, drivers, executor, idle = "Not running
161
167
  lastMessage = field(status, "message");
162
168
  } catch {
163
169
  }
164
- console.log(
165
- JSON.stringify(
166
- {
167
- paired: config.pairing !== null,
168
- machine_id: config.pairing?.machine_id ?? null,
169
- connection,
170
- last_message: lastMessage,
171
- close_code: closeCode,
172
- close_disposition: closeDisposition,
173
- harnesses: await drivers.detect(),
174
- launch: await executor.status(),
175
- launch_source: "status-probe",
176
- last_daemon_launch: daemonLaunch,
177
- repositories: Object.keys(config.repositories)
178
- },
179
- null,
180
- 2
181
- )
182
- );
170
+ return {
171
+ paired: config.pairing !== null,
172
+ machine_id: config.pairing?.machine_id ?? null,
173
+ connection,
174
+ last_message: lastMessage,
175
+ close_code: closeCode,
176
+ close_disposition: closeDisposition,
177
+ harnesses: await drivers.detect(),
178
+ launch: await executor.status(),
179
+ launch_source: "status-probe",
180
+ last_daemon_launch: daemonLaunch,
181
+ repositories: Object.keys(config.repositories),
182
+ reporting: config.reporting
183
+ };
184
+ }
185
+ function harnessName(drivers) {
186
+ return (harness) => drivers.get(harness).displayName;
187
+ }
188
+ async function printStatus(paths, config, drivers, executor, json) {
189
+ const status = await readStatus(paths, config, drivers, executor);
190
+ if (json) {
191
+ console.log(JSON.stringify(status, null, 2));
192
+ return;
193
+ }
194
+ const view = machineView(status);
195
+ const setups = await folderSetups(drivers, config, null);
196
+ for (const line of statusLines(view, setups, harnessName(drivers))) console.log(line);
197
+ }
198
+ function machineView(status) {
199
+ return {
200
+ machineId: status.machine_id,
201
+ connection: status.connection,
202
+ lastMessage: status.last_message,
203
+ launch: status.launch,
204
+ reporting: status.reporting
205
+ };
183
206
  }
184
207
  function startupOptions(args, version) {
185
208
  const options = args[0] === "start" ? parseArgs({
@@ -220,37 +243,30 @@ async function configureLaunch(args, config, path) {
220
243
  await saveConfig(path, config);
221
244
  console.log("Launch settings saved. The running gateway picks up the change automatically.");
222
245
  }
223
- function connectSummary(outcome, config, version) {
224
- const machine = config.pairing?.machine_id ?? "this computer";
225
- const repository = outcome.repository;
226
- const registeredCount = Object.keys(config.repositories).length;
227
- return [
228
- outcome.pairing === "kept" ? `Already paired as ${machine}.` : `Paired as ${machine}.`,
229
- repository === null ? `No GitHub repository here; kept ${registeredCount} registered ${registeredCount === 1 ? "folder" : "folders"}.` : repository.change === "unchanged" ? `${repository.slug} is already registered.` : repository.change === "moved" ? `Registered ${repository.slug} at this folder in place of its previous folder.` : `Registered ${repository.slug}.`,
230
- outcome.loginItem === "current" ? `The login item already runs tsp-gateway ${version}.` : `Installed tsp-gateway ${version} as a login item.`
231
- ];
246
+ async function configureReporting(args, config, path) {
247
+ if (args.length !== 2) throw new Error("Usage: tsp-gateway configure-reporting <activity|off>");
248
+ config.reporting = decodeReporting(args[1]);
249
+ await saveConfig(path, config);
250
+ console.log(
251
+ config.reporting === "off" ? "Reporting off: TSP shows your sessions' state only. The running gateway picks up the change automatically." : "Reporting activity: TSP shows what your sessions are doing, without file names, commands or text. The running gateway picks up the change automatically."
252
+ );
232
253
  }
233
254
  var VERBS = {
234
- connect: async ({ args, argument, config, version, host, paths, drivers, executor }) => {
255
+ connect: async ({ args, argument, config, host, paths, drivers, executor }) => {
235
256
  if (args.length > 2) throw new Error("Usage: tsp-gateway connect [folder]");
236
257
  const since = Date.now();
237
258
  const outcome = await connect(host, config, argument);
238
- for (const line of connectSummary(outcome, config, version)) console.log(line);
239
259
  const reported = outcome.loginItem === "installed" ? await awaitDaemonConnection(join(paths.state, "status.json"), since) : null;
240
- await printStatus(
260
+ const status = await readStatus(
241
261
  paths,
242
262
  config,
243
263
  drivers,
244
264
  executor,
245
265
  outcome.loginItem === "installed" && reported === null ? "Starting" : void 0
246
266
  );
247
- const remaining = await remainingSteps(drivers, config, outcome.repository);
248
- console.log(
249
- remaining.length > 0 ? [
250
- remaining.length === 1 ? "Remaining step:" : "Remaining steps:",
251
- ...remaining
252
- ].join("\n") : "Connected. Nothing left to do on this computer."
253
- );
267
+ const setups = await folderSetups(drivers, config, outcome.repository);
268
+ for (const line of connectLines(machineView(status), setups, harnessName(drivers)))
269
+ console.log(line);
254
270
  },
255
271
  disconnect: async ({ args, host, config }) => {
256
272
  if (args.length !== 1) throw new Error("Usage: tsp-gateway disconnect");
@@ -278,7 +294,13 @@ var VERBS = {
278
294
  console.log("Repository removed. The running gateway picks up the change automatically.");
279
295
  },
280
296
  "configure-launch": ({ args, config, paths }) => configureLaunch(args, config, paths.config),
281
- status: ({ paths, config, drivers, executor }) => printStatus(paths, config, drivers, executor),
297
+ "configure-reporting": ({ args, config, paths }) => configureReporting(args, config, paths.config),
298
+ status: ({ args, paths, config, drivers, executor }) => {
299
+ const json = args[1] === "--json";
300
+ if (args.length > 2 || args.length === 2 && !json)
301
+ throw new Error("Usage: tsp-gateway status [--json]");
302
+ return printStatus(paths, config, drivers, executor, json);
303
+ },
282
304
  install: async ({ host, config, version }) => {
283
305
  await installLoginItem(host, config);
284
306
  console.log(`Installed tsp-gateway ${version} as a login item.`);
@@ -304,7 +326,7 @@ async function main(args) {
304
326
  const verb = verbNamed(command);
305
327
  if (!verb) {
306
328
  console.log(
307
- "Usage: tsp-gateway connect [folder] | disconnect | pair | add <folder> | remove <slug> | configure-launch <json> | status | start | install | uninstall | unpair"
329
+ "Usage: tsp-gateway connect [folder] | disconnect | pair | add <folder> | remove <slug> | configure-launch <json> | configure-reporting <activity|off> | status [--json] | start | install | uninstall | unpair"
308
330
  );
309
331
  return;
310
332
  }
@@ -312,7 +334,11 @@ async function main(args) {
312
334
  const { paths, loginPath } = startupOptions(args, version);
313
335
  const config = await readConfig(paths.config);
314
336
  const environment = { ...process.env, PATH: config.loginPath ?? process.env["PATH"] };
315
- const executor = new HarnessExecutor(environment, { config: async () => config.launch });
337
+ const executor = new HarnessExecutor(environment, {
338
+ config: async () => config.launch,
339
+ log: () => {
340
+ }
341
+ });
316
342
  await verb({
317
343
  args,
318
344
  argument,
@@ -388,6 +388,11 @@
388
388
  "requester_mismatch",
389
389
  "protocol_outdated",
390
390
  "start_failed",
391
+ "shell_startup_timeout",
392
+ "shell_startup_failed",
393
+ "repository_folder_unavailable",
394
+ "harness_exited",
395
+ "start_uncertain",
391
396
  "stop_failed",
392
397
  "session_not_found",
393
398
  "machine_offline",
@@ -401,11 +406,12 @@
401
406
  "control_expired",
402
407
  "control_withdrawn"
403
408
  ],
404
- "title": "LaunchRefusalReason"
409
+ "title": "LaunchRefusalReason",
410
+ "description": "Why a gateway or the server refused a launch, stop or resume.\n\nThe start failures a gateway can prove are named for what went wrong:\n``shell_startup_timeout`` (the login shell was too slow to start and the\ngateway had no earlier environment to use instead), ``shell_startup_failed``\n(the login shell exited before running the harness),\n``repository_folder_unavailable`` (the registered folder could not be\nentered), ``harness_exited`` (the harness exited with an error while\nstarting) and ``start_uncertain`` (a retried attempt could not tell whether\nan earlier one started a session, so it did not start a second).\n``start_failed`` is what gateways before 0.1.2 send for all of these."
405
411
  },
406
412
  "GatewayRetire": {
407
413
  "additionalProperties": false,
408
- "description": "Forget local tracking for an action no longer resolved for this owner.",
414
+ "description": "Forget local tracking for an action TSP has released or no longer resolves.\n\nIt ends tracking only: the gateway leaves any running session alone.",
409
415
  "properties": {
410
416
  "protocol_version": {
411
417
  "const": 1,
@@ -791,6 +797,18 @@
791
797
  ],
792
798
  "default": null,
793
799
  "title": "Resumed As"
800
+ },
801
+ "activity": {
802
+ "anyOf": [
803
+ {
804
+ "$ref": "#/components/schemas/GatewaySessionActivity"
805
+ },
806
+ {
807
+ "type": "null"
808
+ }
809
+ ],
810
+ "default": null,
811
+ "description": "Coarse, content-free activity the gateway derived locally; absent from gateways before 0.1.2 and when its reporting level is off."
794
812
  }
795
813
  },
796
814
  "required": [
@@ -837,6 +855,65 @@
837
855
  "title": "AttachHint",
838
856
  "description": "Path-free session identity; command rendering belongs to the consumer."
839
857
  },
858
+ "GatewaySessionActivity": {
859
+ "additionalProperties": false,
860
+ "description": "The snapshot as the gateway sends it, relative to its own send time.",
861
+ "properties": {
862
+ "kind": {
863
+ "anyOf": [
864
+ {
865
+ "$ref": "#/components/schemas/SessionActivityKind"
866
+ },
867
+ {
868
+ "type": "null"
869
+ }
870
+ ],
871
+ "description": "What the session is doing now; null unless it is working."
872
+ },
873
+ "files_edited": {
874
+ "description": "Distinct files the session edited successfully.",
875
+ "maximum": 1000000,
876
+ "minimum": 0,
877
+ "title": "Files Edited",
878
+ "type": "integer"
879
+ },
880
+ "commands_run": {
881
+ "description": "Commands the session ran, reads excluded.",
882
+ "maximum": 1000000,
883
+ "minimum": 0,
884
+ "title": "Commands Run",
885
+ "type": "integer"
886
+ },
887
+ "idle_ms": {
888
+ "description": "Milliseconds since the session was last active, measured by the gateway when it sent the frame, so no clock skew reaches TSP.",
889
+ "minimum": 0,
890
+ "title": "Idle Ms",
891
+ "type": "integer"
892
+ }
893
+ },
894
+ "required": [
895
+ "kind",
896
+ "files_edited",
897
+ "commands_run",
898
+ "idle_ms"
899
+ ],
900
+ "title": "GatewaySessionActivity",
901
+ "type": "object"
902
+ },
903
+ "SessionActivityKind": {
904
+ "type": "string",
905
+ "enum": [
906
+ "thinking",
907
+ "reading",
908
+ "editing",
909
+ "running_command",
910
+ "using_plan",
911
+ "delegating",
912
+ "using_tool"
913
+ ],
914
+ "title": "SessionActivityKind",
915
+ "description": "What a working session is doing right now, as its harness shows it.\n\nReported only while the session is working. Waiting on the person is the\nsession's ``needs_input`` state, not a kind."
916
+ },
840
917
  "GatewayPong": {
841
918
  "additionalProperties": false,
842
919
  "properties": {