@join-pantheon/cli 0.37.1 → 0.37.3

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/worker.js CHANGED
@@ -29,7 +29,7 @@ import { removalWords, removedLine, removedNotice, takeTold } from "./removed.js
29
29
  import { agentHookStates, repairHook, repairNote } from "./notify.js";
30
30
  import { fence, assess } from "./untrusted.js";
31
31
  import { allowed, closeStartedRun, finishRun, recordRun, startRun } from "./spend.js";
32
- import { handledIn, loadContinued, markContinued, storedIn, storedKey, unmarkContinued, wakeAlertedKey, WORKER_MARK } from "./continued.js";
32
+ import { handledIn, liveReadAt, loadContinued, markContinued, storedIn, storedKey, unmarkContinued, wakeAlertedKey, WORKER_MARK } from "./continued.js";
33
33
  import { decisionAlertsOn, loadProfile, profileForTeam, reservedDecisionsNote, saveProfile, wakeMode } from "./profile.js";
34
34
  import { clip } from "./brief.js";
35
35
  import { composeWakeAsk, composeWorker } from "./notice.js";
@@ -44,6 +44,7 @@ import { CONFIG_DIR, CONFIG_PATH, configFor, findMembership, load as loadConfig,
44
44
  import { shellArg } from "./team-name.js";
45
45
  import { repoName, routeRequest, safeName } from "./repo-route.js";
46
46
  import { holdsRepo } from "./repo-find.js";
47
+ import { cleanRuns, listRuns, recordRunEnd, recordRunStart, runRecordAlive } from "./runs.js";
47
48
  import { FOLDER_SNAPSHOT_LIMITS, keepDrafts, removeSnapshot, shown, snapshotDrafts, snapshotRoot, sweepSnapshots, takeSnapshot } from "./folder-snapshot.js";
48
49
  import { gitActivity } from "./workdir.js";
49
50
  import { isContinuationFor, LIMITS, shouldDeferToHuman } from "./protocol.js";
@@ -358,7 +359,7 @@ export function settleWorktree(project, worktree, log) {
358
359
  let whole = false;
359
360
  try {
360
361
  git(worktree, "rev-parse", "--verify", "HEAD^{commit}");
361
- const status = git(worktree, "status", "--porcelain", "--untracked-files=no");
362
+ const status = git(worktree, "-c", "diff.ignoreSubmodules=all", "status", "--porcelain", "--untracked-files=no", "--ignore-submodules=all");
362
363
  whole = !status.split("\n").some((l) => /^(?:D.|.D)/.test(l));
363
364
  }
364
365
  catch {
@@ -507,6 +508,39 @@ export async function prepareWorktreeAsync(project, scratch, goal, log, branch =
507
508
  throw addFailed(project, p.result.worktree, why);
508
509
  return finishWorktree(project, p.result, log);
509
510
  }
511
+ export function settledReply(r, mine, seen, me) {
512
+ const answered = mine.find((a) => a.ref === r.id && a.id !== r.id && a.role !== "worker");
513
+ if (answered)
514
+ return `your side already replied to it (${answered.act} ${answered.id})`;
515
+ if (r.act === "ANSWER" && r.ref) {
516
+ const to = seen(r.ref);
517
+ if (to && to.from === me && ["ANSWER", "ACCEPT", "CLOSE", "DELIVERED"].includes(to.act) && isThanksOnly(r.body))
518
+ return "a short thank-you to your reply, with nothing asked in it";
519
+ }
520
+ return null;
521
+ }
522
+ const THANKS_WORDS = new Set(["thanks", "thank", "you", "thx", "ty", "tysm", "great", "perfect", "got", "it", "ok", "okay", "k", "cool", "nice",
523
+ "awesome", "sounds", "good", "noted", "all", "much", "so", "very", "a", "lot", "lots", "cheers", "appreciated", "appreciate", "that",
524
+ "really", "super", "brilliant", "excellent", "wonderful", "amazing", "makes", "sense", "understood", "received", "ack", "acknowledged"]);
525
+ const THANKS_EMOJI = new Set(["👍", "🙏", "🎉", "✅", "🙌", "👌", "❤", "💯"]);
526
+ export function isThanksOnly(body) {
527
+ const b = body.trim();
528
+ if (!b || b.length > 120 || b.includes("?"))
529
+ return false;
530
+ const emoji = [...b.replace(/[\u{1F3FB}-\u{1F3FF}\uFE0F\u200D]/gu, "").matchAll(/\p{Extended_Pictographic}/gu)].map((m) => m[0]);
531
+ if (emoji.some((e) => !THANKS_EMOJI.has(e)))
532
+ return false;
533
+ const words = b.toLowerCase().replace(/[\p{Extended_Pictographic}\u{1F3FB}-\u{1F3FF}\uFE0F\u200D]/gu, " ").split(/[^\p{L}\p{N}']+/u).filter(Boolean);
534
+ if (!words.length)
535
+ return emoji.length > 0;
536
+ return words.every((w) => THANKS_WORDS.has(w.replace(/'/g, "")));
537
+ }
538
+ export const CHECKOUT_SWEEP_MS = 30 * 60_000;
539
+ export const READ_ONLY_IDLE_MS = 2 * 60 * 60_000;
540
+ export function adhocGoal(o) {
541
+ const tail = (o.ref || o.id || "").replace(/[^A-Za-z0-9]/g, "").slice(-12);
542
+ return tail ? `req-${tail}` : "adhoc";
543
+ }
510
544
  export function branchName(space, goalId, title) {
511
545
  const slug = (title ?? "").toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "").slice(0, 40).replace(/-+$/, "");
512
546
  return `pantheon/${branchSpace(space)}/${slug ? `${slug}-${goalId.slice(-4)}` : goalId}`;
@@ -2020,6 +2054,7 @@ export class Worker {
2020
2054
  unmarkContinued(k, act.id, this.opts.home);
2021
2055
  if (!this.contDone.has(`held-cont:${act.id}`))
2022
2056
  this.markDone(`held-cont:${act.id}`);
2057
+ this.forgetQueued(act.id);
2023
2058
  }
2024
2059
  makeRoomForHeld(id) {
2025
2060
  if (this.parkedNoSandboxConts.has(id) || this.parkedNoSandboxConts.size < HELD_CONTINUATIONS_MAX)
@@ -2582,6 +2617,8 @@ export class Worker {
2582
2617
  markDone(id) {
2583
2618
  this.contDone.add(id);
2584
2619
  markContinued(this.pinTeam, id, this.opts.home);
2620
+ if (this.contDone.has(`queued-cont:${id}`))
2621
+ this.forgetQueued(id);
2585
2622
  if (WORKER_MARK.test(id))
2586
2623
  this.keepOwn();
2587
2624
  }
@@ -3172,8 +3209,10 @@ export class Worker {
3172
3209
  await this.recoverInterruptedRun(client);
3173
3210
  await this.settleLeftHeld(client);
3174
3211
  this.reportStanding(true);
3175
- if (this.opts.continuations !== false)
3212
+ if (this.opts.continuations !== false) {
3176
3213
  await this.restoreHeldContinuations(client);
3214
+ await this.restoreQueuedContinuations();
3215
+ }
3177
3216
  this.writeState("watching", "waiting for work");
3178
3217
  while (!this.stopped) {
3179
3218
  const arrived = await client.next(5000);
@@ -3208,6 +3247,8 @@ export class Worker {
3208
3247
  this.refusedSaid = false;
3209
3248
  if (this.opts.continuations !== false)
3210
3249
  this.enqueueContinuations(client, arrived);
3250
+ if (this.queuedRestoreAt && Date.now() >= this.queuedRestoreAt)
3251
+ await this.restoreQueuedContinuations();
3211
3252
  this.writeCursor(client.seq);
3212
3253
  this.putHookBack();
3213
3254
  await this.maintain(client, arrived);
@@ -3221,6 +3262,7 @@ export class Worker {
3221
3262
  this.tickHold = { reason: "busy_elsewhere", detail: "another team's run is in progress on this machine" };
3222
3263
  continue;
3223
3264
  }
3265
+ this.sweepCheckouts(client);
3224
3266
  if (this.settlePendingRestore()) {
3225
3267
  this.writeState("held", `${this.pendingRestore.rels.join(", ")} could not be put back after a run`);
3226
3268
  continue;
@@ -3472,6 +3514,19 @@ export class Worker {
3472
3514
  this.requeueContinuation(continuation, 10_000);
3473
3515
  continue;
3474
3516
  }
3517
+ if (continuation && me?.work && !me.work.busy && me.work.idle_since !== null && this.liveThread(client, continuation)) {
3518
+ const asked = continuation.ref ? client.seenAct(continuation.ref)?.session : undefined;
3519
+ const open = new Set(me.sessions.map((s) => s.id));
3520
+ const shown = asked && open.has(asked) ? (this.opts.home !== undefined ? [this.opts.home, undefined] : [undefined])
3521
+ .flatMap((h) => liveReadAt(this.contKeys(), continuation.id, h)).filter((m) => m.session === asked) : [];
3522
+ if (shown.some((m) => me.work.idle_since >= m.at)) {
3523
+ this.markDone(continuation.id);
3524
+ this.forgetQueued(continuation.id);
3525
+ this.opts.log(`- not continuing from ${continuation.act} ${continuation.id}: the window that asked was shown it, and its turn ended after that`);
3526
+ this.writeState("watching", "waiting for work");
3527
+ continue;
3528
+ }
3529
+ }
3475
3530
  if (me && !me.work && hooksInstalled && !this.skipLogged.has("nosignal")) {
3476
3531
  this.skipLogged.add("nosignal");
3477
3532
  this.opts.log(` no activity signal from the relay, but the wake-up hook is installed here — reading this machine as idle until a turn is reported`);
@@ -4042,17 +4097,74 @@ export class Worker {
4042
4097
  }
4043
4098
  async runJob(client, o, persona, policy, makeBrief, requireReply, needsCommands = false, trigger = "", wokeBy = null) {
4044
4099
  const snaps = [];
4100
+ const track = { tree: null };
4101
+ let job;
4045
4102
  try {
4046
- return await this.runJobIn(client, o, persona, policy, makeBrief, requireReply, needsCommands, trigger, wokeBy, snaps);
4103
+ job = await this.runJobIn(client, o, persona, policy, makeBrief, requireReply, needsCommands, trigger, wokeBy, snaps, track);
4104
+ return job;
4047
4105
  }
4048
4106
  finally {
4049
4107
  for (const s of snaps)
4050
4108
  if (!s.kept)
4051
4109
  await removeSnapshot(s.dir);
4110
+ if (track.tree)
4111
+ this.afterRunIn(track.tree, job);
4112
+ }
4113
+ }
4114
+ afterRunIn(tree, _job) {
4115
+ try {
4116
+ recordRunEnd(this.stateDir(), tree.worktree);
4117
+ }
4118
+ catch (e) {
4119
+ this.opts.log(`! could not record the end of the run in ${tree.worktree}: ${e.message.split("\n")[0]}`);
4120
+ }
4121
+ }
4122
+ checkoutsSweptAt = Date.now() - CHECKOUT_SWEEP_MS + 2 * 60_000;
4123
+ sweepCheckouts(client) {
4124
+ if (Date.now() - this.checkoutsSweptAt < CHECKOUT_SWEEP_MS)
4125
+ return;
4126
+ this.checkoutsSweptAt = Date.now();
4127
+ try {
4128
+ const repos = this.workspaceList().filter((w) => w.kind === "repo").map((w) => w.path);
4129
+ if (!repos.length)
4130
+ return;
4131
+ const g = (cwd, args) => gitScan(cwd, args).trim();
4132
+ const ours = listRuns(repos, [this.opts.scratch], this.stateDir(), g).filter((t) => !!t.worktree && !!t.entry && t.entry.space === this.cfg.space);
4133
+ const closed = (t) => !!t.entry.goal && client.goals.get(t.entry.goal)?.state === "CLOSED";
4134
+ const idleUnchanged = (t) => {
4135
+ if (!t.branch || !t.entry.base_sha || Date.now() - t.entry.last_run < READ_ONLY_IDLE_MS)
4136
+ return false;
4137
+ try {
4138
+ return git(t.repo, "rev-parse", "--verify", "--quiet", ...revSep(), `refs/heads/${t.branch}^{commit}`) === t.entry.base_sha;
4139
+ }
4140
+ catch {
4141
+ return false;
4142
+ }
4143
+ };
4144
+ const alive = (pid, id) => sameProcess(pid, id);
4145
+ if (runRecordAlive(this.stateDir()))
4146
+ return;
4147
+ const done = ours.filter(closed);
4148
+ const quiet = ours.filter((t) => !closed(t) && idleUnchanged(t));
4149
+ const r1 = done.length ? cleanRuns(done, { home: this.stateDir(), git: g, alive, dropPushed: true }) : { removed: [], kept: [] };
4150
+ const r2 = quiet.length ? cleanRuns(quiet, { home: this.stateDir(), git: g, alive }) : { removed: [], kept: [] };
4151
+ for (const x of r1.removed)
4152
+ this.opts.log(` cleanup ${x.line} (its goal is closed)`);
4153
+ for (const x of r2.removed)
4154
+ this.opts.log(` cleanup ${x.line} (its runs changed nothing, and it was unused for two hours)`);
4155
+ for (const x of [...r1.kept, ...r2.kept])
4156
+ if (!this.keptSaid.has(x.tree.worktree)) {
4157
+ this.keptSaid.add(x.tree.worktree);
4158
+ this.opts.log(` cleanup ${x.line} — see pantheon runs`);
4159
+ }
4160
+ }
4161
+ catch (e) {
4162
+ this.opts.log(`! could not look at this worker's checkouts: ${e.message.split("\n")[0]}`);
4052
4163
  }
4053
4164
  }
4054
- async runJobIn(client, o, persona, policy, makeBrief, requireReply, needsCommands, trigger, wokeBy, snaps) {
4055
- const goal = o.goal ? (safeSegment(o.goal) ?? `goal-${createHash("sha256").update(o.goal).digest("hex").slice(0, 12)}`) : "adhoc";
4165
+ keptSaid = new Set();
4166
+ async runJobIn(client, o, persona, policy, makeBrief, requireReply, needsCommands, trigger, wokeBy, snaps, track = { tree: null }) {
4167
+ const goal = o.goal ? (safeSegment(o.goal) ?? `goal-${createHash("sha256").update(o.goal).digest("hex").slice(0, 12)}`) : adhocGoal(o);
4056
4168
  const gRec = this.client?.goals.get(goal);
4057
4169
  let branch = branchName(this.cfg.space, goal, gRec && (!client.signaturesExpected() || trusted(client.goalTrust(gRec).trust)) ? gRec.title : undefined);
4058
4170
  let worktree = join(this.opts.scratch, goal);
@@ -4182,8 +4294,19 @@ export class Worker {
4182
4294
  this.opts.log(` copy ${taken.snap.files.size} file${taken.snap.files.size === 1 ? "" : "s"} (${mb(taken.snap.bytes)}) of ${place.name} into ${worktree}${skippedNote(taken.snap.skipped)}`);
4183
4295
  }
4184
4296
  else {
4297
+ const runEntry = { worktree, repo: project, ...(place.origin ? { origin: place.origin } : {}), branch, space: this.cfg.space, goal: o.goal, ...(gRec?.title ? { goal_title: gRec.title } : {}), request: o.ref };
4298
+ const self = { pid: process.pid, ...(workerIdentity() ? { id: workerIdentity() } : {}) };
4299
+ if (isRepo(project)) {
4300
+ recordRunStart(this.stateDir(), runEntry, self);
4301
+ track.tree = { repo: project, worktree, branch, baseSha: "" };
4302
+ }
4185
4303
  try {
4186
4304
  ({ branch, worktree, baseRef, baseSha, tipSha, fetched } = await prepareWorktreeAsync(project, scratch, goal, this.opts.log, branch, this.repoBase(place.fp)));
4305
+ if (branch && baseSha) {
4306
+ const asker = (await this.actOf(client, o.ref).catch(() => null))?.from;
4307
+ recordRunStart(this.stateDir(), { ...runEntry, worktree, branch, ...(asker ? { asker } : {}), base_ref: baseRef, base_sha: baseSha }, self);
4308
+ track.tree = { repo: project, worktree, branch, baseSha };
4309
+ }
4187
4310
  }
4188
4311
  catch (e) {
4189
4312
  if (e instanceof WorktreeTampered)
@@ -5568,13 +5691,66 @@ export class Worker {
5568
5691
  continue;
5569
5692
  }
5570
5693
  this.contQueue.push({ act: r, notBefore: Date.now() });
5571
- if (this.contQueue.length > 20)
5572
- this.contQueue.shift();
5694
+ this.markQueued(r.id);
5695
+ if (this.contQueue.length > 20) {
5696
+ const gone = this.contQueue.shift();
5697
+ if (gone)
5698
+ this.forgetQueued(gone.act.id);
5699
+ }
5573
5700
  }
5574
5701
  }
5575
5702
  requeueContinuation(act, delayMs) {
5576
5703
  if (!this.contQueue.some((c) => c.act.id === act.id)) {
5577
5704
  this.contQueue.push({ act, notBefore: Date.now() + delayMs });
5705
+ this.markQueued(act.id);
5706
+ }
5707
+ }
5708
+ markQueued(actId) { if (!this.contDone.has(`queued-cont:${actId}`))
5709
+ this.markDone(`queued-cont:${actId}`); }
5710
+ forgetQueued(actId) {
5711
+ if (!this.contDone.has(`queued-cont:${actId}`))
5712
+ return;
5713
+ this.contDone.delete(`queued-cont:${actId}`);
5714
+ for (const k of this.contKeys())
5715
+ unmarkContinued(k, `queued-cont:${actId}`, this.opts.home);
5716
+ }
5717
+ queuedRestoreAt = 0;
5718
+ queuedRestoreTries = new Map();
5719
+ async restoreQueuedContinuations() {
5720
+ this.queuedRestoreAt = 0;
5721
+ this.loadContinued();
5722
+ for (const key of [...this.contDone]) {
5723
+ if (!key.startsWith("queued-cont:"))
5724
+ continue;
5725
+ const actId = key.slice("queued-cont:".length);
5726
+ if (this.contDone.has(actId) || this.contQueue.some((c) => c.act.id === actId)) {
5727
+ if (this.contDone.has(actId))
5728
+ this.forgetQueued(actId);
5729
+ continue;
5730
+ }
5731
+ if (this.contDone.has(`held-cont:${actId}`)) {
5732
+ this.forgetQueued(actId);
5733
+ continue;
5734
+ }
5735
+ const got = await this.fetchHeldAct(actId);
5736
+ if (got === "gone") {
5737
+ this.forgetQueued(actId);
5738
+ continue;
5739
+ }
5740
+ if (!got) {
5741
+ const n = (this.queuedRestoreTries.get(actId) ?? 0) + 1;
5742
+ this.queuedRestoreTries.set(actId, n);
5743
+ if (n < 5)
5744
+ this.queuedRestoreAt = Date.now() + 60_000;
5745
+ continue;
5746
+ }
5747
+ if (Date.now() - got.ts > CONTINUATION_HORIZON_MS) {
5748
+ this.forgetQueued(actId);
5749
+ this.markDone(actId);
5750
+ continue;
5751
+ }
5752
+ this.contQueue.push({ act: got, notBefore: Date.now() });
5753
+ this.opts.log(` queue ${got.act} ${actId} was waiting when the worker last stopped — queued again`);
5578
5754
  }
5579
5755
  }
5580
5756
  async tellStops(client) {
@@ -5637,10 +5813,24 @@ export class Worker {
5637
5813
  const r = c.act;
5638
5814
  const g = r.goal ? client.goals.get(r.goal) : undefined;
5639
5815
  this.contQueue = this.contQueue.filter((x) => x.act.id !== r.id);
5640
- if (this.contDone.has(r.id) || g?.state === "CLOSED")
5816
+ if (this.contDone.has(r.id)) {
5817
+ this.forgetQueued(r.id);
5818
+ continue;
5819
+ }
5820
+ if (g?.state === "CLOSED") {
5821
+ this.forgetQueued(r.id);
5641
5822
  continue;
5823
+ }
5642
5824
  if (stopFor(`cont:${r.id}`, this.opts.home)) {
5643
5825
  this.markDone(r.id);
5826
+ this.forgetQueued(r.id);
5827
+ continue;
5828
+ }
5829
+ const why = settledReply(r, client.mineSince(r.ts, r.goal), (id) => client.seenAct(id), this.cfg.member);
5830
+ if (why) {
5831
+ this.markDone(r.id);
5832
+ this.forgetQueued(r.id);
5833
+ this.opts.log(`- not continuing from ${r.act} ${r.id}: ${why}`);
5644
5834
  continue;
5645
5835
  }
5646
5836
  const sel = this.selectPersona(client, {
@@ -6115,6 +6305,12 @@ export class Worker {
6115
6305
  return false;
6116
6306
  return ref.sig?.role === "run" || ref.role === "worker";
6117
6307
  }
6308
+ liveThread(client, act) {
6309
+ const ref = act.ref ? client.seenAct(act.ref) : undefined;
6310
+ if (!ref || ref.from !== this.cfg.member)
6311
+ return false;
6312
+ return !(ref.sig?.role === "run" || ref.role === "worker");
6313
+ }
6118
6314
  pickupOpen(ref) {
6119
6315
  try {
6120
6316
  if (stopFor(ref, this.opts.home))
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@join-pantheon/cli",
3
- "version": "0.37.1",
3
+ "version": "0.37.3",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "@join-pantheon/cli",
9
- "version": "0.37.1",
9
+ "version": "0.37.3",
10
10
  "license": "MIT",
11
11
  "dependencies": {
12
12
  "@modelcontextprotocol/sdk": "^1.32.1",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@join-pantheon/cli",
3
- "version": "0.37.1",
3
+ "version": "0.37.3",
4
4
  "description": "Let your coding agent collaborate directly with your teammates' agents — ask, agree an interface, and deliver, without a human relaying messages.",
5
5
  "type": "module",
6
6
  "bin": {