@join-pantheon/cli 0.35.1 → 0.35.2-rc.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/dist/worker.js CHANGED
@@ -33,7 +33,7 @@ import { handledIn, loadContinued, markContinued, storedIn, storedKey, unmarkCon
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";
36
- import { diagnoseRun, gitProbe, refusedWords, workNeedsCommands, signInFixFor } from "./run-failure.js";
36
+ import { blockedCommands, diagnoseRun, gitProbe, refusedWords, workNeedsCommands, signInFixFor } from "./run-failure.js";
37
37
  import { claudeSettingsAuth, clearSignedOut, markSignedOut, retryAfter, signedOutChecking, signedOutNow, SIGNED_OUT_AGENTS } from "./signed-out.js";
38
38
  import { claudeTokenForRun } from "./agent-token.js";
39
39
  import { installedTools, preferredTool } from "./notifier.js";
@@ -521,7 +521,7 @@ export function commandNote(worktree, rev, log = () => { }) {
521
521
  const repo = repoCommands(worktree, rev);
522
522
  for (const r of repo.refused)
523
523
  log(` commands: ignored "${r.line}" in ${REPO_COMMANDS_FILE} — ${r.why}`);
524
- return `\n\nCOMMANDS FOR THIS RUN: git by subcommand (status, diff, log, add, commit, push, fetch, checkout, …), the repo's test/build/run scripts through npm/pnpm/yarn/bun, make, cargo, go, pytest, mvn/gradle and dotnet` +
524
+ return `\n\nCOMMANDS FOR THIS RUN: git by subcommand (status, diff, log, add, commit, push, fetch, checkout, …), the repo's test/build/run scripts through npm/pnpm/yarn/bun, make, cargo, go, pytest, mvn/gradle, dotnet, flutter (build, test, analyze, pub get) and dart (test, analyze, pub get)` +
525
525
  (repo.ok.length ? `, and this repo's own list (${REPO_COMMANDS_FILE}): ${repo.ok.join(", ")}` : "") +
526
526
  `. Anything else is refused — do not try to get around it. If a command you needed was refused, say so in your reply, and end your final message with one line per command: BLOCKED-COMMAND: <the command>`;
527
527
  }
@@ -798,6 +798,10 @@ in your own plain words, and act on it if the request asks for work. Do not esca
798
798
  `;
799
799
  }
800
800
  const ESCALATED_FOR = "esc-for:";
801
+ const HELD_FOR = "held-for:";
802
+ const RUN_FOR = "run-for:";
803
+ const DECISION_MISS_MS = 10 * 60_000;
804
+ const HELD_GONE_MS = 30 * 60_000;
801
805
  const DECIDED_FOR = "decided-for:";
802
806
  export const MOOT_GRACE_MS = 2 * 60_000;
803
807
  export function buildBrief(o, trigger, branch, persona, canPush = false, decided) {
@@ -1813,7 +1817,7 @@ export class Worker {
1813
1817
  return false;
1814
1818
  }
1815
1819
  }
1816
- outageDiagnosis(label, out, others, meanwhile) {
1820
+ outageDiagnosis(label, out, others, meanwhile, gaveUp = false) {
1817
1821
  const id = AGENTS.find((a) => a.label === label)?.id;
1818
1822
  if (isSignOut(out) || (id && this.signedOutHere.has(id))) {
1819
1823
  let why = (id && this.signedOutHere.has(id) && this.outages.get(id)?.why) || undefined;
@@ -1836,6 +1840,13 @@ export class Worker {
1836
1840
  },
1837
1841
  };
1838
1842
  }
1843
+ if (out.brief && gaveUp) {
1844
+ return {
1845
+ cause: "agent_unavailable",
1846
+ says: `${label} ${outagePredicate(out.why)} — on every try for this request, a few minutes apart. It usually clears within a minute; here it kept happening.`,
1847
+ fix: `Close the other ${label} windows and sessions on this machine, or open ${label} once in a terminal so it can finish refreshing its sign-in (sign in again if it asks) — then answer this request in a live session.`,
1848
+ };
1849
+ }
1839
1850
  const at = whenAgain(out.until);
1840
1851
  const rest = meanwhile ? `; the agent standing in for it cannot take this one (${meanwhile.replace(/\.$/, "")})`
1841
1852
  : others.length ? `; ${others.join("; ")}` : "; no other agent is installed for runs while you are away";
@@ -1847,6 +1858,7 @@ export class Worker {
1847
1858
  summary: `${this.cfg.member}'s agent is unavailable for ${forAbout(out.until)} — this waits`,
1848
1859
  body: `${this.cfg.member}'s worker could not start a run: ${label} ${outagePredicate(out.why)}, and no other agent on that machine could take it. It is still owed; the worker tries again in ${forAbout(out.until)}.`,
1849
1860
  },
1861
+ ...(out.brief ? { quiet: true } : {}),
1850
1862
  };
1851
1863
  }
1852
1864
  signedOutHere = new Set();
@@ -2203,14 +2215,15 @@ export class Worker {
2203
2215
  return !a.sig && !signs && t === "unsigned" && (want === "verified-run") === (a.role === "worker");
2204
2216
  };
2205
2217
  const byId = new Map(acts.map((a) => [a.id, a]));
2218
+ const withdrawn = new Set([...acts, ...client.mineSince(0)].filter((a) => a.act === "RETRACT" && a.ref && a.from === this.cfg.member).map((a) => a.ref));
2206
2219
  for (let i = acts.length - 1; i >= 0; i--) {
2207
2220
  const r = acts[i];
2208
- if (r.act !== "ANSWER" || r.from !== this.cfg.member || !r.ref)
2221
+ if (r.act !== "ANSWER" || r.from !== this.cfg.member || !r.ref || withdrawn.has(r.id))
2209
2222
  continue;
2210
2223
  const e = byId.get(r.ref) ?? client.seenAct(r.ref);
2211
2224
  if (!e || e.act !== "ESCALATE" || e.from !== this.cfg.member)
2212
2225
  continue;
2213
- if (e.ref ? e.ref !== o.ref : !this.hasMark(`${ESCALATED_FOR}${e.id}:${o.ref}`))
2226
+ if (!this.escalationAbout(e, o))
2214
2227
  continue;
2215
2228
  if (!as(e, "verified-run") || !as(r, "verified"))
2216
2229
  continue;
@@ -2218,9 +2231,214 @@ export class Worker {
2218
2231
  }
2219
2232
  return null;
2220
2233
  }
2234
+ escalationAbout(e, o) {
2235
+ if (e.ref)
2236
+ return e.ref === o.ref;
2237
+ const run = e.sig?.cert?.run;
2238
+ this.loadContinued();
2239
+ const has = (k) => this.contDone.has(k);
2240
+ return has(`${ESCALATED_FOR}${e.id}:${o.ref}`) || has(`${HELD_FOR}${o.ref}:${e.id}`) || (!!run && has(`${RUN_FOR}${run}:${o.ref}`));
2241
+ }
2242
+ byOwnRun(client, e) {
2243
+ const t = client.trustOf(e).trust;
2244
+ return t === "verified-run" || (!e.sig && !client.memberSigns(this.cfg.member) && t === "unsigned" && e.role === "worker");
2245
+ }
2246
+ decisionActs = new Map();
2247
+ async decisionAct(client, id) {
2248
+ const seen = client.seenAct(id);
2249
+ if (seen)
2250
+ return seen;
2251
+ const kept = this.decisionActs.get(id);
2252
+ if (kept && (kept.act || Date.now() - kept.at < DECISION_MISS_MS))
2253
+ return kept.act;
2254
+ const got = (await this.fetchActById(id));
2255
+ this.decisionActs.delete(id);
2256
+ this.decisionActs.set(id, { act: got, at: Date.now() });
2257
+ if (this.decisionActs.size > 200)
2258
+ this.decisionActs.delete(this.decisionActs.keys().next().value);
2259
+ return got;
2260
+ }
2261
+ async awaitingOwnDecision(client, o) {
2262
+ for (const h of client.obligations) {
2263
+ if (h.kind !== "HUMAN" || h.owner !== `human:${this.cfg.member}`)
2264
+ continue;
2265
+ if (h.about !== undefined && h.about !== o.ref)
2266
+ continue;
2267
+ const e = await this.decisionAct(client, h.ref);
2268
+ if (!e || e.act !== "ESCALATE" || e.from !== this.cfg.member || !this.escalationAbout(e, o))
2269
+ continue;
2270
+ if (this.byOwnRun(client, e))
2271
+ return e.id;
2272
+ }
2273
+ return null;
2274
+ }
2275
+ unmark(id) {
2276
+ this.contDone.delete(id);
2277
+ const homes = this.opts.home !== undefined ? [this.opts.home, undefined] : [undefined];
2278
+ for (const h of homes)
2279
+ for (const k of this.contKeys()) {
2280
+ try {
2281
+ unmarkContinued(k, id, h);
2282
+ }
2283
+ catch { }
2284
+ }
2285
+ }
2286
+ fateLookedAt = new Map();
2287
+ goneSince = new Map();
2288
+ fateActs = new Map();
2289
+ async ownActsAfter(seq, keep) {
2290
+ const kept = [];
2291
+ let at = seq;
2292
+ for (let page = 0; page < 20; page++) {
2293
+ let acts = [];
2294
+ try {
2295
+ const res = await signedRead(this.cfg, "/history", { since: String(at) }, { timeoutMs: 5000 });
2296
+ if (!res.ok)
2297
+ break;
2298
+ acts = (await res.json()).acts ?? [];
2299
+ }
2300
+ catch {
2301
+ break;
2302
+ }
2303
+ if (!acts.length)
2304
+ break;
2305
+ for (const a of acts)
2306
+ if (a.from === this.cfg.member && keep(a))
2307
+ kept.push(a);
2308
+ at = acts[acts.length - 1].seq;
2309
+ if (acts.length < 500)
2310
+ break;
2311
+ }
2312
+ return { to: at, kept };
2313
+ }
2314
+ runQuestions = new Map();
2315
+ async questionsAbout(client, o) {
2316
+ const known = this.runQuestions.get(o.ref);
2317
+ if (known && Date.now() - known.at < 60_000)
2318
+ return known.acts;
2319
+ const req = await this.decisionAct(client, o.ref);
2320
+ const from = known?.to ?? (typeof req?.seq === "number" ? req.seq : null);
2321
+ if (from === null)
2322
+ return known?.acts ?? [];
2323
+ const asked = new Set((known?.acts ?? []).filter((a) => a.act === "ESCALATE").map((a) => a.id));
2324
+ const answers = new Set((known?.acts ?? []).filter((a) => a.act === "ANSWER").map((a) => a.id));
2325
+ const { to, kept } = await this.ownActsAfter(from, (a) => {
2326
+ if (a.act === "ESCALATE" && this.escalationAbout(a, o)) {
2327
+ asked.add(a.id);
2328
+ return true;
2329
+ }
2330
+ if (a.act === "ANSWER" && a.ref && asked.has(a.ref)) {
2331
+ answers.add(a.id);
2332
+ return true;
2333
+ }
2334
+ return a.act === "RETRACT" && !!a.ref && (asked.has(a.ref) || answers.has(a.ref));
2335
+ });
2336
+ const acts = [...(known?.acts ?? []), ...kept].slice(-40);
2337
+ this.runQuestions.delete(o.ref);
2338
+ this.runQuestions.set(o.ref, { to, acts, at: Date.now() });
2339
+ if (this.runQuestions.size > 200)
2340
+ this.runQuestions.delete(this.runQuestions.keys().next().value);
2341
+ return acts;
2342
+ }
2343
+ async decisionFate(client, id) {
2344
+ const judge = (acts) => {
2345
+ const mine = acts.filter((a) => a.from === this.cfg.member);
2346
+ const back = new Set(mine.filter((a) => a.act === "RETRACT" && a.ref).map((a) => a.ref));
2347
+ if (mine.some((a) => a.act === "ANSWER" && a.ref === id && !back.has(a.id)))
2348
+ return "answered";
2349
+ return back.has(id) ? "withdrawn" : null;
2350
+ };
2351
+ const seen = client.mineSince(0);
2352
+ const known = this.fateActs.get(id);
2353
+ const now = judge([...(known?.acts ?? []), ...seen]);
2354
+ if (now)
2355
+ return { fate: now, acts: [...(known?.acts ?? []), ...seen] };
2356
+ if (Date.now() - (this.fateLookedAt.get(id) ?? 0) >= 60_000) {
2357
+ this.fateLookedAt.set(id, Date.now());
2358
+ if (this.fateLookedAt.size > 200)
2359
+ this.fateLookedAt.delete(this.fateLookedAt.keys().next().value);
2360
+ const e = await this.decisionAct(client, id);
2361
+ const from = known?.to ?? (typeof e?.seq === "number" ? e.seq : null);
2362
+ if (from !== null) {
2363
+ const answers = new Set((known?.acts ?? []).filter((a) => a.act === "ANSWER").map((a) => a.id));
2364
+ const { to, kept } = await this.ownActsAfter(from, (a) => {
2365
+ if (!a.ref)
2366
+ return false;
2367
+ if (a.act === "ANSWER" && a.ref === id) {
2368
+ answers.add(a.id);
2369
+ return true;
2370
+ }
2371
+ return a.act === "RETRACT" && (a.ref === id || answers.has(a.ref));
2372
+ });
2373
+ const keep = [...(known?.acts ?? []), ...kept];
2374
+ this.fateActs.delete(id);
2375
+ this.fateActs.set(id, { to, acts: keep.slice(-20) });
2376
+ if (this.fateActs.size > 200)
2377
+ this.fateActs.delete(this.fateActs.keys().next().value);
2378
+ const read = judge([...keep, ...seen]);
2379
+ if (read)
2380
+ return { fate: read, acts: [...(e ? [e] : []), ...keep, ...seen] };
2381
+ }
2382
+ }
2383
+ const since = this.goneSince.get(id) ?? Date.now();
2384
+ this.goneSince.set(id, since);
2385
+ if (this.goneSince.size > 200)
2386
+ this.goneSince.delete(this.goneSince.keys().next().value);
2387
+ return { fate: Date.now() - since >= HELD_GONE_MS ? "withdrawn" : "unknown", acts: [] };
2388
+ }
2389
+ heldDiag = new Map();
2390
+ async holdFor(client, o) {
2391
+ const open = await this.awaitingOwnDecision(client, o);
2392
+ if (open) {
2393
+ this.goneSince.delete(open);
2394
+ if (!this.contDone.has(`${HELD_FOR}${o.ref}:${open}`))
2395
+ this.markDone(`${HELD_FOR}${o.ref}:${open}`);
2396
+ return "wait";
2397
+ }
2398
+ this.loadContinued();
2399
+ const prefix = `${HELD_FOR}${o.ref}:`;
2400
+ const held = [...this.contDone].filter((k) => k.startsWith(prefix)).map((k) => k.slice(prefix.length));
2401
+ const answered = [];
2402
+ const told = [];
2403
+ for (const id of held) {
2404
+ if (client.obligations.some((h) => h.kind === "HUMAN" && h.ref === id)) {
2405
+ this.goneSince.delete(id);
2406
+ return "wait";
2407
+ }
2408
+ const { fate, acts } = await this.decisionFate(client, id);
2409
+ if (fate === "unknown")
2410
+ return "wait";
2411
+ if (fate === "answered") {
2412
+ answered.push(id);
2413
+ const e = await this.decisionAct(client, id);
2414
+ told.push(...(e ? [e] : []), ...acts);
2415
+ continue;
2416
+ }
2417
+ this.unmark(`${prefix}${id}`);
2418
+ this.goneSince.delete(id);
2419
+ this.opts.log(` ${o.id}: the question a run here asked about it (${id}) was withdrawn without an answer — counted as the failed run it was`);
2420
+ const d = this.heldDiag.get(o.ref) ?? { cause: "crashed", says: "A run asked you about this request instead of answering it, and the question was withdrawn before anyone answered it.", fix: "Answer the teammate from a live session, or ask your agent there to take the request up." };
2421
+ this.heldDiag.delete(o.ref);
2422
+ await this.countFailure(client, o, d);
2423
+ return "wait";
2424
+ }
2425
+ const ranHere = held.length > 0 || [...this.contDone].some((k) => k.startsWith(RUN_FOR) && k.endsWith(`:${o.ref}`));
2426
+ if (ranHere && !held.length)
2427
+ told.push(...await this.questionsAbout(client, o));
2428
+ if (ranHere)
2429
+ await this.reopenDecided(client, o, told);
2430
+ for (const id of answered) {
2431
+ this.unmark(`${prefix}${id}`);
2432
+ this.goneSince.delete(id);
2433
+ }
2434
+ if (answered.length)
2435
+ this.heldDiag.delete(o.ref);
2436
+ return "go";
2437
+ }
2221
2438
  decidedLookedUp = new Set();
2222
- async reopenDecided(client, o) {
2223
- let d = this.escalationAnswer(client, o);
2439
+ async reopenDecided(client, o, told = []) {
2440
+ let d = told.length ? this.escalationAnswer(client, o, [...told].sort((x, y) => (x.seq ?? 0) - (y.seq ?? 0))) : null;
2441
+ d ??= this.escalationAnswer(client, o);
2224
2442
  if (!d && !this.decidedLookedUp.has(o.ref)) {
2225
2443
  this.decidedLookedUp.add(o.ref);
2226
2444
  if (this.decidedLookedUp.size > 500)
@@ -2228,6 +2446,18 @@ export class Worker {
2228
2446
  const acts = await this.recentActs();
2229
2447
  d = this.escalationAnswer(client, o, acts.filter((a) => a.from === this.cfg.member).sort((x, y) => x.seq - y.seq));
2230
2448
  }
2449
+ if (!d) {
2450
+ const mine = client.mineSince(0);
2451
+ for (let i = mine.length - 1, n = 0; i >= 0 && !d && n < 50; i--) {
2452
+ const r = mine[i];
2453
+ if (r.act !== "ANSWER" || r.from !== this.cfg.member || !r.ref || client.seenAct(r.ref))
2454
+ continue;
2455
+ n++;
2456
+ const e = await this.decisionAct(client, r.ref);
2457
+ if (e)
2458
+ d = this.escalationAnswer(client, o, [e, r]);
2459
+ }
2460
+ }
2231
2461
  if (!d)
2232
2462
  return;
2233
2463
  const mark = `decided:${d.answer.id}`;
@@ -3020,6 +3250,14 @@ export class Worker {
3020
3250
  continue;
3021
3251
  }
3022
3252
  }
3253
+ if (await this.holdFor(client, o) === "wait") {
3254
+ if (!this.skipLogged.has(`awaits:${o.id}`)) {
3255
+ this.skipLogged.add(`awaits:${o.id}`);
3256
+ this.opts.log(`- not taking ${owedLabel(o.kind)} ${o.id}: a run here asked you about it — it waits for your answer`);
3257
+ }
3258
+ continue;
3259
+ }
3260
+ this.skipLogged.delete(`awaits:${o.id}`);
3023
3261
  const reqAct = (client.seenAct(o.ref) ?? await this.fetchActById(o.ref));
3024
3262
  const distrust = await this.requestTrust(client, o, reqAct);
3025
3263
  if (distrust) {
@@ -3272,13 +3510,19 @@ export class Worker {
3272
3510
  this.runProject = null;
3273
3511
  this.done.push(job);
3274
3512
  this.report("idle");
3275
- if (candidate && job.ok)
3513
+ if (candidate && this.runKey && (job.ok || job.handedTo))
3514
+ this.unmark(`${RUN_FOR}${this.runKey.runId}:${candidate.ref}`);
3515
+ if (candidate && job.ok && job.handedTo) {
3516
+ this.markDone(`${HELD_FOR}${candidate.ref}:${job.handedTo}`);
3517
+ this.opts.log(` ${candidate.id} waits for your decision (${job.handedTo})`);
3518
+ }
3519
+ else if (candidate && job.ok)
3276
3520
  this.markDone(`handled:${candidate.ref}`);
3277
3521
  if (candidate)
3278
3522
  for (const id of job.escalated ?? [])
3279
3523
  this.markDone(`${ESCALATED_FOR}${id}:${candidate.ref}`);
3280
3524
  const ran = candidate ? candidateAct : continuation;
3281
- if (job.ok && ran?.sig) {
3525
+ if (job.ok && !job.handedTo && ran?.sig) {
3282
3526
  try {
3283
3527
  markAnswered(this.pinTeam, ran.from, ran.sig.nonce, ran.id, this.opts.home, ran.sig.s);
3284
3528
  if (this.opts.home !== undefined)
@@ -3318,7 +3562,7 @@ export class Worker {
3318
3562
  this.holdContinuation(continuation, policy.agent);
3319
3563
  this.outageHeldUntil.set(`cont:${continuation.id}`, job.waitUntil);
3320
3564
  }
3321
- if (!this.skipLogged.has(`out-told:${job.obligation.id}`)) {
3565
+ if (!job.diagnosis.quiet && !this.skipLogged.has(`out-told:${job.obligation.id}`)) {
3322
3566
  this.skipLogged.add(`out-told:${job.obligation.id}`);
3323
3567
  await this.reportRefused(client, job.obligation, job.diagnosis, !!candidate);
3324
3568
  }
@@ -3338,29 +3582,19 @@ export class Worker {
3338
3582
  if (job.diagnosis.cause === "outside_links" || job.diagnosis.cause === "worktree_tampered")
3339
3583
  await this.reportStuck(client, job.obligation, job.diagnosis, 0);
3340
3584
  }
3341
- else if (candidate && !job.ok) {
3342
- const attempts = this.failuresOf(candidate.id) + 1;
3343
- const failures = job.diagnosis?.cause === "no_commands" || job.diagnosis?.cause === "no_sandbox_commands" ? 2 : attempts;
3344
- if (job.diagnosis?.cause === "no_sandbox_commands")
3345
- this.markFor("parked-ns", candidate.id);
3346
- if (this.marked("unparked-ns", candidate.id))
3347
- this.markFor("parked2-ns", candidate.id);
3348
- this.failedRuns.set(this.oKey(candidate.id), failures);
3349
- this.markFor(failures >= 2 ? "parked" : "failed1", candidate.id);
3350
- const under = this.unparkedUnder.get(this.oKey(candidate.id));
3351
- if (under)
3352
- this.markDone(`parked2@${under}:${this.oKey(candidate.id)}`);
3353
- else if (this.marked("unparked", candidate.id))
3354
- this.markFor("parked2", candidate.id);
3355
- if (job.diagnosis?.cause === "no_commands")
3356
- this.markFor("parked-nc", candidate.id);
3357
- if (this.failedRuns.size > 200) {
3358
- this.failedRuns.delete(this.failedRuns.keys().next().value);
3585
+ else if (candidate && !job.ok && job.handedTo) {
3586
+ this.markDone(`${HELD_FOR}${candidate.ref}:${job.handedTo}`);
3587
+ if (job.diagnosis) {
3588
+ this.heldDiag.set(candidate.ref, job.diagnosis);
3589
+ if (this.heldDiag.size > 200)
3590
+ this.heldDiag.delete(this.heldDiag.keys().next().value);
3359
3591
  }
3360
- if (failures >= 2)
3361
- this.decline(candidate.ref, "its run failed twice on this computer");
3362
- if (failures >= 2 && job.diagnosis)
3363
- await this.reportStuck(client, candidate, job.diagnosis, attempts);
3592
+ this.opts.log(` ${candidate.id} waits for your decision (${job.handedTo}) — not run again before you answer`);
3593
+ if (job.diagnosis)
3594
+ await this.noteOnDecision(client, candidate, job.diagnosis, job.handedTo, job.blocked ?? []);
3595
+ }
3596
+ else if (candidate && !job.ok) {
3597
+ await this.countFailure(client, candidate, job.diagnosis);
3364
3598
  }
3365
3599
  }
3366
3600
  finally {
@@ -3974,6 +4208,8 @@ export class Worker {
3974
4208
  }
3975
4209
  const runLimit = this.runLimit(persona);
3976
4210
  const runHome = this.openRunHome(runCfg, client.features, runLimit.ms);
4211
+ if (this.runKey && o.ref && o.kind !== "CONTINUE")
4212
+ this.markDone(`${RUN_FOR}${this.runKey.runId}:${o.ref}`);
3977
4213
  try {
3978
4214
  if (holdRepoConfig) {
3979
4215
  fenced = quarantineRepoAgentConfig(worktree, runHome);
@@ -4271,7 +4507,8 @@ export class Worker {
4271
4507
  const elsewhere = o.goal == null ? [] : client.mineSince(startedAt).filter((a) => a.goal !== o.goal && !settledIds.has(a.id)
4272
4508
  && ((!!o.ref && a.ref === o.ref) || (!!runId && a.goal == null && (a.ref == null || a.ref === o.ref || goalOf.get(a.ref) === o.goal) && a.sig?.role === "run" && a.sig.cert?.run === runId)));
4273
4509
  const sent = [...client.mineSince(startedAt, o.goal).filter((a) => !settledIds.has(a.id)), ...elsewhere, ...settled.filter((a) => o.goal == null || a.goal === o.goal)];
4274
- const repliedWith = sent.find((a) => ["DELIVERED", "ANSWER", "ESCALATE", "ACCEPT", "OBJECT", "PROPOSE", "CONTRACT", "CLOSE", "WITHDRAW"].includes(a.act) || (a.act === "NOTE" && !!a.ref));
4510
+ const withdrawnHere = new Set(client.mineSince(startedAt).filter((r) => r.act === "RETRACT" && r.ref).map((r) => r.ref));
4511
+ const repliedWith = sent.find((a) => !withdrawnHere.has(a.id) && (["DELIVERED", "ANSWER", "ESCALATE", "ACCEPT", "OBJECT", "PROPOSE", "CONTRACT", "CLOSE", "WITHDRAW"].includes(a.act) || (a.act === "NOTE" && !!a.ref)));
4275
4512
  const replied = requireReply ? client.repliedTo(o) || !!repliedWith : true;
4276
4513
  if (requireReply && replied && !client.repliedTo(o) && repliedWith) {
4277
4514
  this.opts.log(` reply ${repliedWith.act} ${repliedWith.id} answers ${o.id}, though the relay still lists it as owed${repliedWith.ref ? "" : " (the act named no ref)"}`);
@@ -4337,13 +4574,25 @@ export class Worker {
4337
4574
  return { obligation: o, branch, worktree, ok: false, detail, agentOut: true };
4338
4575
  }
4339
4576
  const quiet = ok && !requireReply && sent.length === 0;
4340
- const diagnosis = ok ? undefined : outage ? this.outageDiagnosis(choice.backend.label, outage, [], "it was handed to another agent three times already") : diagnoseRun({ exited, replied, allowBash, branch, project, worktree, detail, transcript, runLog, needsCommands,
4577
+ const facts = { exited, replied, allowBash, branch, project, worktree, detail, transcript, runLog, needsCommands,
4341
4578
  ...(agentId === "claude-code" && !this.opts.agentCmd && claudeTokenForRun(this.stateDir())
4342
4579
  ? { env: { ...process.env, CLAUDE_CODE_OAUTH_TOKEN: undefined }, savedToken: true } : { env: process.env }),
4343
4580
  ...(agentId === "claude-code" ? { claudeSettings: claudeSettingsAuth({ env: process.env }) } : {}),
4344
4581
  ...(/\(stopped after \d+s\)/.test(detail.split("\n")[0] ?? "") ? { timedOut: { minutes: runLimit.minutes, from: runLimit.from } } : {}),
4345
4582
  ...(bashDroppedForSandbox ? { commandsNeedSandbox: byCli ? "agent-cmd" : "claude" } : {}), ...(agentId ? { agent: agentId } : {}), ...(place.kind === "folder" ? { folder: true } : {}),
4346
- ...(agentId === "claude-code" && allowBash ? { allowedRules: bashRules } : {}), ...(baseRef ? { baseRef } : {}) }, this.opts.probe ?? gitProbe);
4583
+ ...(agentId === "claude-code" && allowBash ? { allowedRules: bashRules } : {}), ...(baseRef ? { baseRef } : {}) };
4584
+ let diagnosis = ok ? undefined : outage ? this.outageDiagnosis(choice.backend.label, outage, [], "it was handed to another agent three times already", true) : diagnoseRun(facts, this.opts.probe ?? gitProbe);
4585
+ const asked = outage ? undefined : sent.find((a) => a.act === "ESCALATE" && a.from === this.cfg.member
4586
+ && (a.ref === o.ref || !a.ref)
4587
+ && (a.sig ? a.sig.role === "run" && (!runId || a.sig.cert?.run === runId) : a.role === "worker")
4588
+ && !withdrawnHere.has(a.id))?.id;
4589
+ const realReply = sent.some((a) => !withdrawnHere.has(a.id) && a.act !== "ESCALATE" && a.act !== "HUMAN_REQUIRED"
4590
+ && (["DELIVERED", "ANSWER", "ACCEPT", "OBJECT", "PROPOSE", "CONTRACT", "CLOSE", "WITHDRAW"].includes(a.act) || (a.act === "NOTE" && a.ref === o.ref)));
4591
+ const handedTo = !asked ? undefined : !ok ? asked : requireReply && !realReply && !client.repliedTo(o) ? asked : undefined;
4592
+ const blocked = handedTo ? blockedCommands(transcript ?? detail) : [];
4593
+ if (handedTo && diagnosis?.cause === "crashed" && blocked.length) {
4594
+ diagnosis = diagnoseRun({ ...facts, exited: true }, this.opts.probe ?? gitProbe);
4595
+ }
4347
4596
  if (ok && agentId && !this.opts.agentCmd)
4348
4597
  clearSignedOut(agentId, { pantheonHome: this.stateDir() });
4349
4598
  if (!ok && diagnosis?.cause === "not_signed_in" && agentId && !this.opts.agentCmd
@@ -4363,7 +4612,7 @@ export class Worker {
4363
4612
  : ok ? `< handled ${o.id} on ${branch}`
4364
4613
  : `! ${o.id} stopped [${diagnosis.cause}]: ${diagnosis.says} (transcript: ${runLog})`);
4365
4614
  const escalated = sent.filter((a) => a.act === "ESCALATE" && !a.ref && a.from === this.cfg.member && (a.sig?.role === "run" || a.role === "worker")).map((a) => a.id);
4366
- return { obligation: o, branch, worktree, ok, detail, ...(diagnosis ? { diagnosis } : {}), ...(escalated.length ? { escalated } : {}) };
4615
+ return { obligation: o, branch, worktree, ok, detail, ...(diagnosis ? { diagnosis } : {}), ...(escalated.length ? { escalated } : {}), ...(handedTo ? { handedTo, ...(blocked.length ? { blocked } : {}) } : {}) };
4367
4616
  }
4368
4617
  catch (e) {
4369
4618
  if (!recorded) {
@@ -4771,6 +5020,8 @@ export class Worker {
4771
5020
  }
4772
5021
  refusalsNotified = new Set();
4773
5022
  async reportRefused(client, o, d, owed) {
5023
+ if (d.quiet)
5024
+ return;
4774
5025
  if (this.opts.notify && !this.refusalsNotified.has(d.says)) {
4775
5026
  this.refusalsNotified.add(d.says);
4776
5027
  try {
@@ -4896,9 +5147,61 @@ export class Worker {
4896
5147
  return r ? { id: r.id, seq: r.seq } : null;
4897
5148
  }
4898
5149
  safetyRaised = new Map();
5150
+ async countFailure(client, candidate, diagnosis) {
5151
+ const attempts = this.failuresOf(candidate.id) + 1;
5152
+ const failures = diagnosis?.cause === "no_commands" || diagnosis?.cause === "no_sandbox_commands" ? 2 : attempts;
5153
+ if (diagnosis?.cause === "no_sandbox_commands")
5154
+ this.markFor("parked-ns", candidate.id);
5155
+ if (this.marked("unparked-ns", candidate.id))
5156
+ this.markFor("parked2-ns", candidate.id);
5157
+ this.failedRuns.set(this.oKey(candidate.id), failures);
5158
+ this.markFor(failures >= 2 ? "parked" : "failed1", candidate.id);
5159
+ const under = this.unparkedUnder.get(this.oKey(candidate.id));
5160
+ if (under)
5161
+ this.markDone(`parked2@${under}:${this.oKey(candidate.id)}`);
5162
+ else if (this.marked("unparked", candidate.id))
5163
+ this.markFor("parked2", candidate.id);
5164
+ if (diagnosis?.cause === "no_commands")
5165
+ this.markFor("parked-nc", candidate.id);
5166
+ if (this.failedRuns.size > 200) {
5167
+ this.failedRuns.delete(this.failedRuns.keys().next().value);
5168
+ }
5169
+ if (failures >= 2)
5170
+ this.decline(candidate.ref, "its run failed twice on this computer");
5171
+ if (failures >= 2 && diagnosis)
5172
+ await this.reportStuck(client, candidate, diagnosis, attempts);
5173
+ }
5174
+ async noteOnDecision(client, o, d, decision, blocked = []) {
5175
+ const key = `stuck-note:${decision}`;
5176
+ this.loadContinued();
5177
+ if (this.hasMark(key))
5178
+ return;
5179
+ const named = blocked.filter((c) => !d.says.includes(c)).slice(0, 5);
5180
+ const body = [
5181
+ ...(named.length ? [`It also needed ${named.map((c) => `\`${clip(c, 120)}\``).join(", ")}, and could not run ${named.length === 1 ? "it" : "them"} here.`] : []),
5182
+ `Also, while working on this: ${d.says}`,
5183
+ ``,
5184
+ `What to do: ${d.fix}`,
5185
+ ...(d.dir && d.lines?.length ? [``, `On this computer, in ${d.dir}:`, ...d.lines.map((l) => ` ${l}`)] : []),
5186
+ ].join("\n");
5187
+ try {
5188
+ await client.send({
5189
+ act: "NOTE", goal: null, ref: decision, after_seq: Math.max(client.readSeq, client.seq),
5190
+ summary: clip(this.ledgerPaths(blocked.length ? `${this.cfg.member}'s agent also needed \`${blocked.join("`, `")}\`, which it could not run` : `${this.cfg.member}'s agent also hit: ${d.says}`), 190),
5191
+ body: clip(this.ledgerPaths(body), LIMITS.bodyChars - 300),
5192
+ });
5193
+ this.markDone(key);
5194
+ this.opts.log(`* said on decision ${decision} why the run for ${o.id} stopped: ${d.cause}`);
5195
+ }
5196
+ catch (e) {
5197
+ this.opts.log(`! could not add why the run stopped to decision ${decision}: ${e.message.split("\n")[0]}`);
5198
+ }
5199
+ }
4899
5200
  async reportStuck(client, o, d, attempts, again = 0, known) {
4900
5201
  this.learnRequests([o]);
4901
5202
  const safety = SAFETY_CAUSES.has(d.cause);
5203
+ if (!safety && o.ref && stopFor(o.ref, this.opts.home))
5204
+ return;
4902
5205
  const key = safety ? `stuck-safety:${this.oKey(o.id)}:${d.cause}` : `stuck:${this.oKey(o.id)}`;
4903
5206
  const space = this.spaceKey();
4904
5207
  const said = loadContinued(space, this.opts.home);
@@ -1560,6 +1560,9 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
1560
1560
  // A send in flight belongs to its decision (#105): Send stays off for that one, also when its panel
1561
1561
  // is closed and opened again, so the same words never go twice. Other decisions can still be answered.
1562
1562
  private var panelInFlight: Set<String> = []
1563
+ // The "Reply as" title of a typed reply on its way (rc.3 check): a reopened panel shows that kind, and a
1564
+ // refused one comes back as it — never the menu's default, Answer, which would settle the decision.
1565
+ private var panelInFlightKind: [String: String] = [:]
1563
1566
  // Decisions settled by a button while another panel was open, with words typed on them: each opens,
1564
1567
  // with its words and what happened, once the panel the person is on is closed (round three, F8).
1565
1568
  private var panelUnsentQueue: [Item] = []
@@ -1608,6 +1611,8 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
1608
1611
  guard let cur = panelItem, !cur.ref.isEmpty else { return }
1609
1612
  let k = panelKey(cur)
1610
1613
  guard panelRetractedHere.contains(k) else { return }
1614
+ // Something still on its way on the old id is told there first; the next refresh follows (0.35.2 review).
1615
+ if panelInFlight.contains(k) || panelRetracting.contains(k) { return }
1611
1616
  if state.items.contains(where: { panelKey($0) == k }) { return }
1612
1617
  guard let next = state.items.first(where: { panelKey($0) != k && $0.ref == cur.ref && $0.space == cur.space && $0.kind == cur.kind && $0.who == cur.who }) else { return }
1613
1618
  let k2 = panelKey(next)
@@ -1616,9 +1621,12 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
1616
1621
  if let d = panelDrafts.removeValue(forKey: k), panelDrafts[k2] == nil { panelDraftName[k2] = next.headline; panelDrafts[k2] = d }
1617
1622
  if let n = panelNotes.removeValue(forKey: k), panelNotes[k2] == nil { panelNotes[k2] = n }
1618
1623
  panelAnsweredHere.remove(k); panelAnsweredHere.remove(k2)
1619
- // Drawn again for the new row, so its buttons act on it — without taking the keyboard.
1624
+ // Drawn again for the new row, so its buttons act on it — without taking the keyboard. "Reply as" is
1625
+ // kept: drawn again, it would go back to its default, Answer (0.35.2 review).
1626
+ let kindWas = panelReplyKind?.titleOfSelectedItem
1620
1627
  panelItem = next
1621
1628
  showDetail(next, byPerson: false)
1629
+ if let kindWas = kindWas { showKind(kindWas) }
1622
1630
  }
1623
1631
  /// Decisions this panel retracted an answer on: the only ones followMoved follows.
1624
1632
  private var panelRetractedHere: Set<String> = []
@@ -2190,7 +2198,13 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
2190
2198
  // Its reply still on the way: Send stays off here, and says why (#105).
2191
2199
  if panelInFlight.contains(k) {
2192
2200
  panelSendButton?.isEnabled = false
2193
- setStatus("Still sending your last answer to this — Send works again once it lands.")
2201
+ // Nothing typed on it since: the menu shows the kind of the reply on its way (rc.3 check).
2202
+ if panelDrafts[k] == nil, let fk = panelInFlightKind[k] { showKind(fk) }
2203
+ setStatus(stillSending(k, "Send works again once it lands."))
2204
+ } else if panelRetracting.contains(k) {
2205
+ // Reopened while its Retract is on its way: Send is off here too, and says why (0.35.2 review).
2206
+ panelSendButton?.isEnabled = false
2207
+ setStatus("Still retracting your answer to this — Send works again once it lands.")
2194
2208
  } else if let note = panelNotes.removeValue(forKey: k) {
2195
2209
  setStatus(note)
2196
2210
  } else if restored, panelDrafts[k] != nil {
@@ -2417,7 +2431,9 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
2417
2431
  let k = panelKey(item)
2418
2432
  // Return is the send key, and the panel can be closed and opened again: while this
2419
2433
  // decision's reply is in flight, nothing sends the same words twice (#105) — and it says why.
2420
- if panelInFlight.contains(k) { setStatus("Still sending your last answer to this — Send works again once it lands."); return }
2434
+ if panelInFlight.contains(k) { setStatus(stillSending(k, "Send works again once it lands.")); return }
2435
+ // A Retract on its way: nothing is sent until it lands and the decision is back under its new id (0.35.2 review).
2436
+ if panelRetracting.contains(k) { setStatus("Still retracting your answer to this — Send works again once it lands."); return }
2421
2437
  let text = tv.string.trimmingCharacters(in: .whitespacesAndNewlines)
2422
2438
  if text.isEmpty { setStatus("Type your reply first."); return }
2423
2439
  if kindCode(panelReplyKind?.titleOfSelectedItem) == "ANSWER", panelAnsweredHere.contains(k) {
@@ -2442,11 +2458,13 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
2442
2458
  panelSendButton?.isEnabled = false
2443
2459
  // In flight, the words are not a draft: reopening this decision meanwhile does not show them as unsent.
2444
2460
  let kindTitle = panelReplyKind?.titleOfSelectedItem ?? ""
2461
+ panelInFlightKind[k] = kindTitle
2445
2462
  panelDrafts.removeValue(forKey: k)
2446
2463
  DispatchQueue.global().async {
2447
2464
  let r = run(pantheonBin, ["explain", "--queued", "--only-action", "--action-id", aid], timeout: QUEUED_WAIT_S)
2448
2465
  DispatchQueue.main.async {
2449
2466
  self.panelInFlight.remove(k)
2467
+ self.panelInFlightKind.removeValue(forKey: k)
2450
2468
  let appendOnly = self.panelAppendOnly.removeValue(forKey: k) ?? false
2451
2469
  // Still the decision this was sent from? Only then is the open panel touched.
2452
2470
  let here = self.isOpen(k)
@@ -2513,7 +2531,8 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
2513
2531
  }
2514
2532
  if here, let box = self.panelReply {
2515
2533
  let v = box.string.trimmingCharacters(in: .whitespacesAndNewlines)
2516
- if v.isEmpty { box.string = text } else if !startsWithSent(box.string) { box.string = text + "\n\n" + box.string }
2534
+ // The box holds only the words put back: "Reply as" goes back to the kind they were sent as (rc.3 check).
2535
+ if v.isEmpty { box.string = text; self.showKind(kindTitle) } else if !startsWithSent(box.string) { box.string = text + "\n\n" + box.string }
2517
2536
  }
2518
2537
  // "Not confirmed": the relay's answer was lost and the reply is not found yet, or the CLI stopped after
2519
2538
  // taking it — it may have landed (#107; item 6).
@@ -2538,6 +2557,8 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
2538
2557
  return
2539
2558
  }
2540
2559
  if panelRetracting.contains(k) { return }
2560
+ // A reply or an action still on its way on this decision: the Retract waits for it (0.35.2 review).
2561
+ if panelInFlight.contains(k) { setStatus(stillSending(k, "Retract works once it lands.")); return }
2541
2562
  let id = s.act
2542
2563
  var payload: [String: Any] = ["action": "retract", "act": id]
2543
2564
  if !s.space.isEmpty { payload["space"] = s.space }
@@ -2545,11 +2566,12 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
2545
2566
  setStatus("Retracting…")
2546
2567
  panelRetracting.insert(k)
2547
2568
  panelRetract?.isEnabled = false
2569
+ panelSendButton?.isEnabled = false
2548
2570
  DispatchQueue.global().async {
2549
2571
  let r = run(pantheonBin, ["explain", "--queued", "--only-action", "--action-id", aid], timeout: QUEUED_WAIT_S)
2550
2572
  DispatchQueue.main.async {
2551
2573
  self.panelRetracting.remove(k)
2552
- if self.isOpen(k) { self.panelRetract?.isEnabled = true }
2574
+ if self.isOpen(k) { self.panelRetract?.isEnabled = true; self.panelSendButton?.isEnabled = !self.panelInFlight.contains(k) }
2553
2575
  if r?.code == 0 {
2554
2576
  if self.panelSent[k]?.act == id { self.panelSent.removeValue(forKey: k) }
2555
2577
  if self.isOpen(k) { self.panelRetract?.isHidden = true }
@@ -2677,10 +2699,21 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
2677
2699
  }
2678
2700
  /// One action per decision at a time, for every button on its panel (0.35.1 review, item 5): said, never a second answer.
2679
2701
  private func busyHere(_ k: String) -> Bool {
2702
+ // A Retract on its way is an action on the decision too (0.35.2 review): no choice, option or Done meanwhile.
2703
+ if panelRetracting.contains(k) { setStatus("Still retracting your answer to this — wait for it to land."); return true }
2680
2704
  guard panelInFlight.contains(k) else { return false }
2681
- setStatus("Still sending your last answer to this — wait for it to land.")
2705
+ setStatus(stillSending(k, "wait for it to land."))
2682
2706
  return true
2683
2707
  }
2708
+ /// What is on its way on a decision, in words: a typed Note or Ask is not "your last answer" (rc.3 check).
2709
+ private func stillSending(_ k: String, _ tail: String) -> String {
2710
+ let what = panelInFlightKind[k] == "Note" ? "note" : panelInFlightKind[k] == "Ask back" ? "question" : "answer"
2711
+ return "Still sending your last \(what) to this — \(tail)"
2712
+ }
2713
+ /// "Reply as" set to a kind this panel offers; any other is left as it is.
2714
+ private func showKind(_ title: String) {
2715
+ if panelReplyKind?.itemTitles.contains(title) == true { panelReplyKind?.selectItem(withTitle: title) }
2716
+ }
2684
2717
  func copyPrompt(_ item: Item) {
2685
2718
  let pb = NSPasteboard.general
2686
2719
  pb.clearContents()