@chipschat/acp 1.0.24 → 1.0.25

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,9 +1,9 @@
1
1
  # Third-party notices
2
2
 
3
- `dist/main.js` in @chipschat/acp 1.0.24 carries these packages, bundled in. Each one's
3
+ `dist/main.js` in @chipschat/acp 1.0.25 carries these packages, bundled in. Each one's
4
4
  licence follows.
5
5
 
6
- ## @chipschat/harness 1.0.5 (Apache-2.0)
6
+ ## @chipschat/harness 1.0.6 (Apache-2.0)
7
7
 
8
8
  Apache License
9
9
  Version 2.0, January 2004
@@ -207,7 +207,7 @@ Apache License
207
207
  See the License for the specific language governing permissions and
208
208
  limitations under the License.
209
209
 
210
- ## @chipschat/protocol 1.0.5 (Apache-2.0)
210
+ ## @chipschat/protocol 1.0.6 (Apache-2.0)
211
211
 
212
212
  Apache License
213
213
  Version 2.0, January 2004
package/dist/main.js CHANGED
@@ -22771,6 +22771,7 @@ var clientHelloFrame = external_exports.object({
22771
22771
  autonomy: autonomyLevel.optional().describe("The level the harness believes it has.")
22772
22772
  })).max(64).optional().describe("Not read by Chips: a connection is its token's, and the server's `hello` names the token's harness and its agents (ADR 0087). Absent for a person's client."),
22773
22773
  since: external_exports.int().nonnegative().optional().describe("Not read by Chips: a stream resumes from `subscribe`'s `since`."),
22774
+ level: external_exports.int().positive().max(1e4).optional().describe("What a person's app understands, as `CATALOGUE_LEVEL` names it (everything in step). An event added at a higher level is sent to it as the line in words its addition came with, or not at all. Absent, from an app before levels, everything is sent as it always was."),
22774
22775
  accepts: external_exports.array(external_exports.string().min(1).max(40)).max(32).optional().describe("Server frames beyond the base set this client understands, by the names in `SERVER_EXTENSIONS`. A name the server does not know is ignored rather than refused, so a client may name a frame a newer server sends and still be answered by an older one (ADR 0211, Phase 3). A client that says `events` is sent its replay in pages rather than one frame per event; one that says `presence` is sent an agent's state as it changes (ADR 0083); a harness's socket that says `restart` is sent `restart` when a person asks it to restart (ADR 0094); an owner's or admin's that says `join` is sent each join request as it changes (ADR 0122); a harness's that says `context` is sent `context.edit` when a person changes one of its context files (ADR 0134); a harness's that says `models` is sent `model.change` when a person changes an agent's model or effort (ADR 0140); a harness's that says `heartbeats` is sent `heartbeat.change` when a person switches an agent's heartbeat or changes how often it beats (ADR 0143); a harness's that says `context_windows` is sent `context_window.compact` and `context_window.change` when a person asks an agent to compact now or changes the limit it compacts itself at (ADR 0146); a harness's that says `jobs` is sent `job.change` when a person switches one of the jobs it runs on its own (ADR 0252); a person's that says `folders` is sent `folder.open` when they ask, from another of their devices, for a room's folder to be opened in Finder (ADR 0225), and `folder.run` when they ask for its project to be run or stopped there (ADR 0226); a person's that says `folder_runs` is sent `folder.run_state` when their Mac says how a run of theirs stands (ADR 0226); a person's that says `reads` is sent `read` when they read further on another of their devices (ADR 0262); one that says nothing is sent what it was always sent, since an unknown frame is dropped in silence."),
22775
22776
  context: contextBudget.optional().describe("How much of a room a harness is handed with each message for it (ADR 0088): 40 lines and 16,000 characters when absent, 200 and 32,000 at most. For this connection. A person's client is handed none.")
22776
22777
  }).describe("The first frame a client sends.");
@@ -23774,6 +23775,8 @@ var Lane = class {
23774
23775
  stop;
23775
23776
  laneState = "opening";
23776
23777
  laneRunId;
23778
+ /** The lane's run, once `work` has taken it up. */
23779
+ worked;
23777
23780
  laneSpaceId;
23778
23781
  lastSummary;
23779
23782
  lastCost;
@@ -23820,7 +23823,8 @@ var Lane = class {
23820
23823
  if (this.laneRunId === void 0 || this.laneSpaceId === void 0) {
23821
23824
  throw new Error(this.laneState === "flattened" ? "this task was flattened into a step; there is no lane to work" : "the lane has not been acknowledged yet");
23822
23825
  }
23823
- return this.wire.accept(this.laneRunId, this.laneSpaceId, this.id);
23826
+ this.worked ??= this.wire.accept(this.laneRunId, this.laneSpaceId, this.id);
23827
+ return this.worked;
23824
23828
  }
23825
23829
  /** The task's acknowledgement: where the lane is, or that there is none. */
23826
23830
  opened(lane) {
@@ -24325,6 +24329,13 @@ function refusalOf(kind2, error62) {
24325
24329
 
24326
24330
  // node_modules/@chipschat/harness/dist/connect.js
24327
24331
  var HELD_MOST = 2e4;
24332
+ var UNANSWERED_KEPT_MS = 60 * 6e4;
24333
+ function takenUp(takers, by) {
24334
+ if (by === void 0 || takers.size === 0)
24335
+ return true;
24336
+ return takers.delete(by) && takers.size === 0;
24337
+ }
24338
+ var UNREACHABLE = /* @__PURE__ */ new Set(["busy", "no_such_server"]);
24328
24339
  var OF_THE_CONNECTION = /* @__PURE__ */ new Set(["hello", "state", "receipt"]);
24329
24340
  function agentOf(body) {
24330
24341
  return {
@@ -24389,6 +24400,21 @@ var Chips = class _Chips {
24389
24400
  helloEvent;
24390
24401
  /** What this harness last said is wrong on its side, said again whenever it reconnects. */
24391
24402
  trouble;
24403
+ /**
24404
+ * Messages for this harness, by `seq`, with no run started in their room since: a bridge
24405
+ * gathers a message for a moment, or queues it behind a turn already going (ADR 0294), before
24406
+ * it starts one. `since` is past them, so they are kept to be heard again after a restart,
24407
+ * which would otherwise lose them for good.
24408
+ */
24409
+ unanswered = /* @__PURE__ */ new Map();
24410
+ /** When each message a process before this one left waiting was heard, by `seq`. */
24411
+ leftAt = /* @__PURE__ */ new Map();
24412
+ /**
24413
+ * After a restart that kept messages unanswered: everything up to `upTo` was heard before, so
24414
+ * it is passed over, except the messages in `again`, which are heard as if new. Over once an
24415
+ * event past `upTo` arrives.
24416
+ */
24417
+ replay;
24392
24418
  /** The runs this process started or took up and has not ended, for the store. */
24393
24419
  openRuns = /* @__PURE__ */ new Map();
24394
24420
  /** Runs a process before this one left open, ended once this one has said hello. */
@@ -24398,6 +24424,16 @@ var Chips = class _Chips {
24398
24424
  this.options = options;
24399
24425
  const kept2 = options.store?.load();
24400
24426
  this.since = options.since ?? kept2?.since;
24427
+ const fresh = Date.now() - UNANSWERED_KEPT_MS;
24428
+ const again2 = (kept2?.unanswered ?? []).filter((seq) => {
24429
+ const at = kept2?.unansweredAt?.[String(seq)];
24430
+ return this.since !== void 0 && seq <= this.since && at !== void 0 && at > fresh;
24431
+ });
24432
+ for (const seq of again2)
24433
+ this.leftAt.set(seq, kept2?.unansweredAt?.[String(seq)] ?? 0);
24434
+ if (options.since === void 0 && this.since !== void 0 && again2.length > 0) {
24435
+ this.replay = { upTo: this.since, again: new Set(again2) };
24436
+ }
24401
24437
  for (const event of kept2?.outbox ?? []) {
24402
24438
  this.outbox.add(String(event["idempotency_key"]), event).catch(() => void 0);
24403
24439
  }
@@ -24704,7 +24740,6 @@ var Chips = class _Chips {
24704
24740
  ...input2.sessionRef === void 0 ? {} : { session_ref: input2.sessionRef }
24705
24741
  });
24706
24742
  } catch (error62) {
24707
- this.activeRuns.delete(runId);
24708
24743
  this.ended(runId);
24709
24744
  throw error62;
24710
24745
  }
@@ -24719,6 +24754,11 @@ var Chips = class _Chips {
24719
24754
  const answer = this.outbox.add(key, event);
24720
24755
  if (type === "run.completed" || type === "run.failed")
24721
24756
  this.ended(String(envelope2["run_id"]));
24757
+ if (type === "run.started" || type === "message.post" && envelope2["run_id"] === void 0) {
24758
+ this.answered(envelope2);
24759
+ }
24760
+ if (type === "message.heard")
24761
+ this.heardInTurn(payload["message_id"]);
24722
24762
  this.save();
24723
24763
  if (this.ready && this.socket !== void 0) {
24724
24764
  this.socket.send(JSON.stringify({ frame: "event", event }));
@@ -24730,10 +24770,91 @@ var Chips = class _Chips {
24730
24770
  this.openRuns.set(run2.runId, run2);
24731
24771
  this.save();
24732
24772
  }
24773
+ /**
24774
+ * A run is over: it completed, failed, or a person stopped it. It is let go here, so nothing
24775
+ * more reaches it and a harness that runs for weeks doesn't keep every run it ever had.
24776
+ */
24733
24777
  ended(runId) {
24778
+ this.activeRuns.delete(runId);
24734
24779
  if (this.openRuns.delete(runId))
24735
24780
  this.save();
24736
24781
  }
24782
+ /**
24783
+ * Heard before this process started, so passed over: everything up to the replay's `upTo`,
24784
+ * except the messages kept unanswered, which are heard again.
24785
+ */
24786
+ passedOver(seq) {
24787
+ const replay = this.replay;
24788
+ if (replay === void 0)
24789
+ return false;
24790
+ if (seq > replay.upTo) {
24791
+ this.replay = void 0;
24792
+ return false;
24793
+ }
24794
+ return !replay.again.delete(seq);
24795
+ }
24796
+ /** A message for this harness waits until a run starts for it in its room. */
24797
+ waitsForAnswer(event, seq) {
24798
+ const spaceId = event["space_id"];
24799
+ if (event["type"] !== "message.new" || typeof spaceId !== "string")
24800
+ return;
24801
+ const takers = Array.isArray(event["for"]) ? event["for"].filter((each) => typeof each === "string") : [];
24802
+ const messageId = typeof event["message_id"] === "string" ? event["message_id"] : void 0;
24803
+ const at = this.leftAt.get(seq) ?? Date.now();
24804
+ this.unanswered.set(seq, { spaceId, messageId, takers: new Set(takers), at });
24805
+ }
24806
+ /** A working turn said it heard this message: it waits for no run of its own any more. */
24807
+ heardInTurn(messageId) {
24808
+ if (typeof messageId !== "string")
24809
+ return;
24810
+ let changed = false;
24811
+ for (const [seq, waiting] of this.unanswered) {
24812
+ if (waiting.messageId === messageId) {
24813
+ this.unanswered.delete(seq);
24814
+ changed = true;
24815
+ }
24816
+ }
24817
+ if (changed)
24818
+ this.save();
24819
+ }
24820
+ /**
24821
+ * A run started in a room, or something said there outside a run: the messages waiting there
24822
+ * for whoever did it are taken up. Sent as one of its agents, only those for that agent; sent
24823
+ * as the harness itself, all of them.
24824
+ */
24825
+ answered(envelope2) {
24826
+ const spaceId = envelope2["space_id"];
24827
+ if (typeof spaceId !== "string")
24828
+ return;
24829
+ const actor2 = envelope2["actor"];
24830
+ const by = typeof actor2?.id === "string" ? actor2.id : void 0;
24831
+ let changed = false;
24832
+ for (const [seq, waiting] of this.unanswered) {
24833
+ if (waiting.spaceId === spaceId && takenUp(waiting.takers, by)) {
24834
+ this.unanswered.delete(seq);
24835
+ changed = true;
24836
+ }
24837
+ }
24838
+ if (changed)
24839
+ this.save();
24840
+ }
24841
+ /**
24842
+ * The messages to hear again after a restart, each with when it was first heard: those still
24843
+ * unanswered, and those a process before this one left that are not yet replayed.
24844
+ */
24845
+ toHearAgain() {
24846
+ const now = Date.now();
24847
+ for (const [seq, waiting] of this.unanswered) {
24848
+ if (now - waiting.at > UNANSWERED_KEPT_MS)
24849
+ this.unanswered.delete(seq);
24850
+ }
24851
+ const heardAt = /* @__PURE__ */ new Map();
24852
+ for (const seq of this.replay?.again ?? [])
24853
+ heardAt.set(seq, this.leftAt.get(seq) ?? now);
24854
+ for (const [seq, waiting] of this.unanswered)
24855
+ heardAt.set(seq, waiting.at);
24856
+ return new Map([...heardAt].sort((a, b) => a[0] - b[0]));
24857
+ }
24737
24858
  /**
24738
24859
  * End the runs a process before this one left open: its work died with it, and a run nobody
24739
24860
  * ends keeps its thinking bubble until the harness's next one. Refused for one Chips had ended
@@ -24762,8 +24883,12 @@ var Chips = class _Chips {
24762
24883
  this.saving.unref?.();
24763
24884
  }
24764
24885
  saveNow() {
24765
- this.options.store?.save({
24886
+ if (this.options.store === void 0)
24887
+ return;
24888
+ const again2 = this.toHearAgain();
24889
+ this.options.store.save({
24766
24890
  ...this.since === void 0 ? {} : { since: this.since },
24891
+ ...again2.size === 0 ? {} : { unanswered: [...again2.keys()], unansweredAt: Object.fromEntries(again2) },
24767
24892
  outbox: this.outbox.unanswered(),
24768
24893
  runs: [...this.openRuns.values(), ...this.orphans]
24769
24894
  });
@@ -24802,10 +24927,13 @@ var Chips = class _Chips {
24802
24927
  return new Promise((resolve5, reject) => {
24803
24928
  this.setState("connecting");
24804
24929
  const failed = (what, error62) => {
24805
- if (first)
24806
- reject(new ConnectError(`${what}: ${describe3(error62)}`));
24807
- else
24930
+ const why = `${what}: ${describe3(error62)}`;
24931
+ if (!first)
24808
24932
  this.scheduleReconnect();
24933
+ else if (this.waitsFor(error62))
24934
+ this.tryAgain(why, resolve5, reject);
24935
+ else
24936
+ reject(new ConnectError(why));
24809
24937
  };
24810
24938
  void this.socketUrl().then((url2) => {
24811
24939
  let socket;
@@ -24825,10 +24953,13 @@ var Chips = class _Chips {
24825
24953
  /** The handshake's outcome: the hello, a refusal, or the socket gone before either. */
24826
24954
  settled(socket, hello2, first, resolve5, reject) {
24827
24955
  if (hello2 === void 0) {
24828
- if (first)
24829
- reject(new ConnectError("the server closed the socket before saying hello"));
24830
- else
24956
+ const why = "the server closed the socket before saying hello";
24957
+ if (!first)
24831
24958
  this.scheduleReconnect();
24959
+ else if (this.options.waitForServer === true)
24960
+ this.tryAgain(why, resolve5, reject);
24961
+ else
24962
+ reject(new ConnectError(why));
24832
24963
  return;
24833
24964
  }
24834
24965
  if (hello2 instanceof ConnectError) {
@@ -24907,10 +25038,12 @@ var Chips = class _Chips {
24907
25038
  onHello(socket, hello2) {
24908
25039
  this.helloEvent = hello2;
24909
25040
  this.emitter.emit("hello", hello2);
25041
+ const again2 = this.replay?.again;
25042
+ const from = again2 !== void 0 && again2.size > 0 ? Math.min(...again2) - 1 : this.since;
24910
25043
  socket.send(JSON.stringify({
24911
25044
  frame: "subscribe",
24912
25045
  workspace_id: hello2.workspace_id,
24913
- ...this.since === void 0 ? {} : { since: this.since }
25046
+ ...from === void 0 ? {} : { since: from }
24914
25047
  }));
24915
25048
  for (const event of this.outbox.unanswered()) {
24916
25049
  socket.send(JSON.stringify({ frame: "event", event }));
@@ -25028,15 +25161,18 @@ var Chips = class _Chips {
25028
25161
  }
25029
25162
  const seq = event["seq"];
25030
25163
  if (typeof seq === "number") {
25164
+ if (this.passedOver(seq))
25165
+ return;
25031
25166
  this.since = Math.max(this.since ?? 0, seq);
25167
+ this.waitsForAnswer(event, seq);
25032
25168
  this.save();
25033
25169
  }
25034
- if (event["type"] === "run.cancel" && typeof event["run_id"] === "string") {
25035
- this.ended(event["run_id"]);
25036
- }
25037
25170
  this.settleWaiters(event);
25038
25171
  this.emitter.emit("event", event);
25039
25172
  this.routeToRuns(event);
25173
+ if (event["type"] === "run.cancel" && typeof event["run_id"] === "string") {
25174
+ this.ended(event["run_id"]);
25175
+ }
25040
25176
  const asked = this.asks.heard(event);
25041
25177
  const type = event["type"];
25042
25178
  if (typeof type === "string" && type in this.named && !asked) {
@@ -25154,6 +25290,32 @@ var Chips = class _Chips {
25154
25290
  this.connectionState = state2;
25155
25291
  this.emitter.emit("state", state2);
25156
25292
  }
25293
+ /**
25294
+ * Whether a first connect waits on this failure: told to wait for its server, and the server
25295
+ * couldn't be reached or couldn't answer (no answer at all, a 5xx, busy, or not on the relay
25296
+ * yet), rather than refusing this harness.
25297
+ */
25298
+ waitsFor(error62) {
25299
+ if (this.options.waitForServer !== true)
25300
+ return false;
25301
+ if (!(error62 instanceof RefusedError))
25302
+ return true;
25303
+ return /^(5\d\d|429)$/.test(error62.code) || UNREACHABLE.has(error62.code);
25304
+ }
25305
+ /** The first connect again after a wait, as a reconnect waits; `connect` resolves once it is in. */
25306
+ tryAgain(why, resolve5, reject) {
25307
+ if (this.closed) {
25308
+ reject(new ConnectError("the connection was closed while waiting for the server"));
25309
+ return;
25310
+ }
25311
+ this.options.onWaiting?.(why);
25312
+ this.retry += 1;
25313
+ clearTimeout(this.timer);
25314
+ this.timer = setTimeout(() => {
25315
+ this.timer = void 0;
25316
+ this.open(true).then(resolve5, reject);
25317
+ }, Math.min(500 * this.retry, 5e3));
25318
+ }
25157
25319
  scheduleReconnect() {
25158
25320
  if (this.closed || this.options.reconnect === false) {
25159
25321
  if (!this.closed)
@@ -26034,6 +26196,16 @@ function nodeFirst(folder2, path) {
26034
26196
  const rest = (path ?? "").split(delimiter).filter((each) => each !== "" && each !== folder2);
26035
26197
  return [folder2, ...rest].join(delimiter);
26036
26198
  }
26199
+ function runsKept(label2, given = {}) {
26200
+ const env2 = given.env ?? process.env;
26201
+ if (env2["XPC_SERVICE_NAME"] === label2)
26202
+ return true;
26203
+ const started = env2["SYSTEMD_EXEC_PID"] ?? "";
26204
+ if (started !== "")
26205
+ return started === String(given.pid ?? process.pid);
26206
+ const terminal = given.terminal ?? process.stdout.isTTY === true;
26207
+ return (env2["INVOCATION_ID"] ?? "") !== "" && !terminal;
26208
+ }
26037
26209
  function keptNode(env2 = process.env) {
26038
26210
  const given = env2["CHIPS_NODE"];
26039
26211
  return given !== void 0 && given !== "" ? given : nodeOnPath(env2);
@@ -26171,18 +26343,23 @@ function stopKeeping(label2, options = {}) {
26171
26343
  // node_modules/@chipschat/harness/dist/store.js
26172
26344
  import { existsSync as existsSync4, mkdirSync as mkdirSync3, readFileSync as readFileSync4, renameSync, writeFileSync as writeFileSync3 } from "node:fs";
26173
26345
  import { dirname as dirname3 } from "node:path";
26346
+ function stateOf(parsed) {
26347
+ const unanswered = Array.isArray(parsed.unanswered) ? parsed.unanswered.filter((seq) => Number.isInteger(seq)) : [];
26348
+ const heardAt = Object.fromEntries(Object.entries(typeof parsed.unansweredAt === "object" && parsed.unansweredAt !== null ? parsed.unansweredAt : {}).filter((entry) => typeof entry[1] === "number"));
26349
+ return {
26350
+ ...typeof parsed.since === "number" ? { since: parsed.since } : {},
26351
+ ...unanswered.length === 0 ? {} : { unanswered, unansweredAt: heardAt },
26352
+ outbox: Array.isArray(parsed.outbox) ? parsed.outbox : [],
26353
+ runs: Array.isArray(parsed.runs) ? parsed.runs : []
26354
+ };
26355
+ }
26174
26356
  function fileStore(path) {
26175
26357
  return {
26176
26358
  load() {
26177
26359
  if (!existsSync4(path))
26178
26360
  return void 0;
26179
26361
  try {
26180
- const parsed = JSON.parse(readFileSync4(path, "utf8"));
26181
- return {
26182
- ...typeof parsed.since === "number" ? { since: parsed.since } : {},
26183
- outbox: Array.isArray(parsed.outbox) ? parsed.outbox : [],
26184
- runs: Array.isArray(parsed.runs) ? parsed.runs : []
26185
- };
26362
+ return stateOf(JSON.parse(readFileSync4(path, "utf8")));
26186
26363
  } catch {
26187
26364
  return void 0;
26188
26365
  }
@@ -29440,12 +29617,21 @@ var state = {
29440
29617
  costs: kept.costs ?? {}
29441
29618
  };
29442
29619
  var env = adapterEnv(kind, { ...process.env, ...kind.env });
29620
+ var waitingFor;
29621
+ var waitingSaid = (why) => {
29622
+ if (why !== waitingFor) log(`waiting for Chips: ${why}`);
29623
+ waitingFor = why;
29624
+ };
29443
29625
  var bridge;
29444
29626
  var chips = await connect({
29445
29627
  url: joined.url,
29446
29628
  token: joined.token,
29447
29629
  client: { name: "chips-acp", version: VERSION },
29448
29630
  store: fileStore(join12(folder, "chips-state.json")),
29631
+ // The copy this machine keeps running starts at login, perhaps before its server is up: it waits
29632
+ // and tries again, where a run by hand hears at once that Chips can't be reached.
29633
+ waitForServer: runsKept(LABEL),
29634
+ onWaiting: waitingSaid,
29449
29635
  onRestart: async () => {
29450
29636
  log("a person asked for a restart");
29451
29637
  await bridge?.restart();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chipschat/acp",
3
- "version": "1.0.24",
3
+ "version": "1.0.25",
4
4
  "description": "An agent that speaks the Agent Client Protocol, as a Chips harness of its kind: agents made from Chips, each in a folder of its own, each room a session.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -33,9 +33,9 @@
33
33
  "release": "npm run build && npm test && npm pack"
34
34
  },
35
35
  "devDependencies": {
36
- "@chipschat/conformance": "1.0.5",
37
- "@chipschat/harness": "1.0.5",
38
- "@chipschat/protocol": "1.0.5",
36
+ "@chipschat/conformance": "1.0.6",
37
+ "@chipschat/harness": "1.0.6",
38
+ "@chipschat/protocol": "1.0.6",
39
39
  "@types/node": "22.19.0",
40
40
  "esbuild": "0.27.7",
41
41
  "tsx": "4.21.0",