shadok-ai 0.19.46 → 0.19.48
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/busy-latch.d.ts +88 -0
- package/dist/busy-latch.js +67 -0
- package/dist/busy-latch.js.map +1 -0
- package/dist/server.js +86 -11
- package/dist/server.js.map +1 -1
- package/package.json +1 -1
- package/public/index.html +5 -1
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* When the server's "a turn is running" flag no longer has a turn behind it.
|
|
3
|
+
*
|
|
4
|
+
* `Live.busy` is a BELIEF, not an observation: it is raised when a prompt is
|
|
5
|
+
* accepted and lowered in `finishTurn`'s `finally`. Every door into an agent
|
|
6
|
+
* reads it first — a second prompt is refused `busy`, a cron slot is skipped,
|
|
7
|
+
* the auto-reload stands down, a parent's notification is queued. So the flag
|
|
8
|
+
* latching ON is not one lost message: it is an agent nobody can reach again,
|
|
9
|
+
* for as long as the process lives.
|
|
10
|
+
*
|
|
11
|
+
* And it is invisible. The pane sits at an empty prompt, the transcript is
|
|
12
|
+
* silent, `/live` reports `busy: true`, and nothing is logged — the shape of
|
|
13
|
+
* every other silent-loss bug in this codebase (invariants 7 and 23).
|
|
14
|
+
*
|
|
15
|
+
* Measured on a live instance: a message arrived while that agent's TUI had
|
|
16
|
+
* stopped taking input, the submit recovery respawned it and delivered the
|
|
17
|
+
* message (`src/submit-recovery.ts`), the answer came back — and the flag
|
|
18
|
+
* stayed up afterwards. Forty minutes later the turn stopwatch still read from
|
|
19
|
+
* that message, every later one was refused, and the one thing that cleared it
|
|
20
|
+
* was a human clicking "Reload agent".
|
|
21
|
+
*
|
|
22
|
+
* The TELEGRAM side made it look like a dead end rather than a bug. A `busy`
|
|
23
|
+
* refusal there is answered with an Escape and a wait for `turn-done`
|
|
24
|
+
* (`shouldPreempt`), so the sender is told "couldn't take the hand back — try
|
|
25
|
+
* /stop, then send it again". But `/stop` is that same Escape, and an Escape
|
|
26
|
+
* interrupts a turn; it cannot lower a flag no turn is holding. Every retry
|
|
27
|
+
* therefore reproduced the message word for word, which is what someone
|
|
28
|
+
* reports — not "an agent is wedged".
|
|
29
|
+
*
|
|
30
|
+
* So: rather than hunt every path that could leave the flag up (the one that
|
|
31
|
+
* did has not been pinned, and the next will be somewhere else), make the flag
|
|
32
|
+
* UNABLE to latch. `busy` is legitimately held by exactly four things, each of
|
|
33
|
+
* which the server knows about — a `finishTurn` waiting for the turn to end, a
|
|
34
|
+
* prompt being typed, a failed submit being recovered, a process being swapped.
|
|
35
|
+
* When none of them holds it and the pane shows no work either, the flag is
|
|
36
|
+
* stale by construction, whatever raised it.
|
|
37
|
+
*
|
|
38
|
+
* The grace window is what keeps that honest: the four facts are read off a
|
|
39
|
+
* 300 ms timer, so a verdict taken on one poll would race the microsecond gaps
|
|
40
|
+
* between them. Requiring the same answer for a continuous stretch means a
|
|
41
|
+
* release only ever follows a state that has been true long enough to be real.
|
|
42
|
+
*
|
|
43
|
+
* Pure, with the facts handed in — tested without a server or a TUI, like
|
|
44
|
+
* `submitRecoveryVerdict`.
|
|
45
|
+
*/
|
|
46
|
+
/** How long `busy` must be held by nobody before it is taken back. Long
|
|
47
|
+
* enough that no ordinary gap between two of the four holders can reach it,
|
|
48
|
+
* short enough that the person who just wrote is still waiting. */
|
|
49
|
+
export declare const BUSY_LATCH_GRACE_MS = 15000;
|
|
50
|
+
export interface BusyLatchInput {
|
|
51
|
+
/** The server believes a turn is running on this agent. */
|
|
52
|
+
busy: boolean;
|
|
53
|
+
/** A `finishTurn` is awaiting the end of that turn — the normal holder. */
|
|
54
|
+
turnInFlight: boolean;
|
|
55
|
+
/** A prompt is being typed into the TUI right now (`submitGuarded`). */
|
|
56
|
+
submitting: boolean;
|
|
57
|
+
/** A failed submit is being recovered: respawn + re-delivery. */
|
|
58
|
+
recovering: boolean;
|
|
59
|
+
/** The agent's process is being swapped (a restart/reload). */
|
|
60
|
+
restarting: boolean;
|
|
61
|
+
/** The pane shows a turn running. Last resort, and deliberately not the
|
|
62
|
+
* only term: `screenShowsWork` is the fragile heuristic of invariant 2, so
|
|
63
|
+
* it may only ever KEEP the flag up, never bring it down on its own. */
|
|
64
|
+
working: boolean;
|
|
65
|
+
/** When this state was first seen (ms epoch), or null if the previous poll
|
|
66
|
+
* did not see it. */
|
|
67
|
+
since: number | null;
|
|
68
|
+
now: number;
|
|
69
|
+
graceMs: number;
|
|
70
|
+
}
|
|
71
|
+
export type BusyLatchVerdict =
|
|
72
|
+
/** Someone is legitimately holding it: forget any window in progress. */
|
|
73
|
+
{
|
|
74
|
+
action: "none";
|
|
75
|
+
}
|
|
76
|
+
/** Nobody is holding it, but not for long enough yet. */
|
|
77
|
+
| {
|
|
78
|
+
action: "watch";
|
|
79
|
+
since: number;
|
|
80
|
+
}
|
|
81
|
+
/** Stale: hand the agent back. */
|
|
82
|
+
| {
|
|
83
|
+
action: "clear";
|
|
84
|
+
};
|
|
85
|
+
export declare function busyLatchVerdict(i: BusyLatchInput): BusyLatchVerdict;
|
|
86
|
+
/** The log line a release writes. Loud on purpose: a flag that had to be taken
|
|
87
|
+
* back is a bug upstream of here, and the only trace it leaves is this. */
|
|
88
|
+
export declare function busyLatchLog(id: string, heldMs: number): string;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* When the server's "a turn is running" flag no longer has a turn behind it.
|
|
3
|
+
*
|
|
4
|
+
* `Live.busy` is a BELIEF, not an observation: it is raised when a prompt is
|
|
5
|
+
* accepted and lowered in `finishTurn`'s `finally`. Every door into an agent
|
|
6
|
+
* reads it first — a second prompt is refused `busy`, a cron slot is skipped,
|
|
7
|
+
* the auto-reload stands down, a parent's notification is queued. So the flag
|
|
8
|
+
* latching ON is not one lost message: it is an agent nobody can reach again,
|
|
9
|
+
* for as long as the process lives.
|
|
10
|
+
*
|
|
11
|
+
* And it is invisible. The pane sits at an empty prompt, the transcript is
|
|
12
|
+
* silent, `/live` reports `busy: true`, and nothing is logged — the shape of
|
|
13
|
+
* every other silent-loss bug in this codebase (invariants 7 and 23).
|
|
14
|
+
*
|
|
15
|
+
* Measured on a live instance: a message arrived while that agent's TUI had
|
|
16
|
+
* stopped taking input, the submit recovery respawned it and delivered the
|
|
17
|
+
* message (`src/submit-recovery.ts`), the answer came back — and the flag
|
|
18
|
+
* stayed up afterwards. Forty minutes later the turn stopwatch still read from
|
|
19
|
+
* that message, every later one was refused, and the one thing that cleared it
|
|
20
|
+
* was a human clicking "Reload agent".
|
|
21
|
+
*
|
|
22
|
+
* The TELEGRAM side made it look like a dead end rather than a bug. A `busy`
|
|
23
|
+
* refusal there is answered with an Escape and a wait for `turn-done`
|
|
24
|
+
* (`shouldPreempt`), so the sender is told "couldn't take the hand back — try
|
|
25
|
+
* /stop, then send it again". But `/stop` is that same Escape, and an Escape
|
|
26
|
+
* interrupts a turn; it cannot lower a flag no turn is holding. Every retry
|
|
27
|
+
* therefore reproduced the message word for word, which is what someone
|
|
28
|
+
* reports — not "an agent is wedged".
|
|
29
|
+
*
|
|
30
|
+
* So: rather than hunt every path that could leave the flag up (the one that
|
|
31
|
+
* did has not been pinned, and the next will be somewhere else), make the flag
|
|
32
|
+
* UNABLE to latch. `busy` is legitimately held by exactly four things, each of
|
|
33
|
+
* which the server knows about — a `finishTurn` waiting for the turn to end, a
|
|
34
|
+
* prompt being typed, a failed submit being recovered, a process being swapped.
|
|
35
|
+
* When none of them holds it and the pane shows no work either, the flag is
|
|
36
|
+
* stale by construction, whatever raised it.
|
|
37
|
+
*
|
|
38
|
+
* The grace window is what keeps that honest: the four facts are read off a
|
|
39
|
+
* 300 ms timer, so a verdict taken on one poll would race the microsecond gaps
|
|
40
|
+
* between them. Requiring the same answer for a continuous stretch means a
|
|
41
|
+
* release only ever follows a state that has been true long enough to be real.
|
|
42
|
+
*
|
|
43
|
+
* Pure, with the facts handed in — tested without a server or a TUI, like
|
|
44
|
+
* `submitRecoveryVerdict`.
|
|
45
|
+
*/
|
|
46
|
+
/** How long `busy` must be held by nobody before it is taken back. Long
|
|
47
|
+
* enough that no ordinary gap between two of the four holders can reach it,
|
|
48
|
+
* short enough that the person who just wrote is still waiting. */
|
|
49
|
+
export const BUSY_LATCH_GRACE_MS = 15_000;
|
|
50
|
+
export function busyLatchVerdict(i) {
|
|
51
|
+
if (!i.busy)
|
|
52
|
+
return { action: "none" };
|
|
53
|
+
if (i.turnInFlight || i.submitting || i.recovering || i.restarting)
|
|
54
|
+
return { action: "none" };
|
|
55
|
+
if (i.working)
|
|
56
|
+
return { action: "none" };
|
|
57
|
+
// First poll of a new window: the clock starts now, never at an earlier
|
|
58
|
+
// instant we did not observe.
|
|
59
|
+
const since = i.since ?? i.now;
|
|
60
|
+
return i.now - since >= i.graceMs ? { action: "clear" } : { action: "watch", since };
|
|
61
|
+
}
|
|
62
|
+
/** The log line a release writes. Loud on purpose: a flag that had to be taken
|
|
63
|
+
* back is a bug upstream of here, and the only trace it leaves is this. */
|
|
64
|
+
export function busyLatchLog(id, heldMs) {
|
|
65
|
+
return `[sk-${id}] busy was held with no turn behind it for ${Math.round(heldMs / 1000)}s — taking it back (the agent was unreachable)`;
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=busy-latch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"busy-latch.js","sourceRoot":"","sources":["../src/busy-latch.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AAEH;;oEAEoE;AACpE,MAAM,CAAC,MAAM,mBAAmB,GAAG,MAAM,CAAC;AAgC1C,MAAM,UAAU,gBAAgB,CAAC,CAAiB;IAChD,IAAI,CAAC,CAAC,CAAC,IAAI;QAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;IACvC,IAAI,CAAC,CAAC,YAAY,IAAI,CAAC,CAAC,UAAU,IAAI,CAAC,CAAC,UAAU,IAAI,CAAC,CAAC,UAAU;QAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;IAC9F,IAAI,CAAC,CAAC,OAAO;QAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;IACzC,wEAAwE;IACxE,8BAA8B;IAC9B,MAAM,KAAK,GAAG,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,GAAG,CAAC;IAC/B,OAAO,CAAC,CAAC,GAAG,GAAG,KAAK,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;AACvF,CAAC;AAED;4EAC4E;AAC5E,MAAM,UAAU,YAAY,CAAC,EAAU,EAAE,MAAc;IACrD,OAAO,OAAO,EAAE,8CAA8C,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,IAAI,CAAC,gDAAgD,CAAC;AAC1I,CAAC"}
|
package/dist/server.js
CHANGED
|
@@ -23,6 +23,7 @@ import { extractLiveText } from "../public/live-text.js";
|
|
|
23
23
|
import { findTransientErrors, newTransientErrors, RETRY_DELAYS_MS } from "./retry.js";
|
|
24
24
|
import { backgroundTasks, screenShowsWork, moveToOption, boxText, SubmitError } from "./detect.js";
|
|
25
25
|
import { submitRecoveryVerdict, submitWithRecovery, submitRecoveryEnabled, RECOVERY_COOLDOWN_MS, UnresponsiveError, UNRESPONSIVE_REPORT_TITLE, unresponsiveReportBody, } from "./submit-recovery.js";
|
|
26
|
+
import { busyLatchVerdict, busyLatchLog, BUSY_LATCH_GRACE_MS } from "./busy-latch.js";
|
|
26
27
|
import { autoReloadVerdict, autoReloadEnabled } from "./auto-reload.js";
|
|
27
28
|
import { PtyPilot } from "./session.js";
|
|
28
29
|
import { ensureClaudeHome, ensureProjectTrusted, getDefaultModel, setDefaultModel, parseClaudeVersion, seedPlanForCwds, settingsPlan, } from "./claude-home.js";
|
|
@@ -9248,6 +9249,7 @@ group = null) {
|
|
|
9248
9249
|
effort: effort ?? undefined,
|
|
9249
9250
|
clients: new Set(),
|
|
9250
9251
|
busy: false,
|
|
9252
|
+
turnWaiters: 0,
|
|
9251
9253
|
lastPrompt: "",
|
|
9252
9254
|
screenTimer: null,
|
|
9253
9255
|
lastScreen: "",
|
|
@@ -9542,8 +9544,38 @@ async function attachPilot(s) {
|
|
|
9542
9544
|
// Spontaneous resume: work restarting without a client prompt (e.g. a
|
|
9543
9545
|
// background agent completing and waking the model). No handler called
|
|
9544
9546
|
// finishTurn, so watch for it here and signal the turn like any other.
|
|
9547
|
+
// SAY it when that fails: this is the one `finishTurn` call with nobody to
|
|
9548
|
+
// report to, and swallowing it is how a flag left up here stayed invisible.
|
|
9545
9549
|
if (settled && !s.busy && pilot.isWorking())
|
|
9546
|
-
finishTurn(s).catch(() => { });
|
|
9550
|
+
finishTurn(s).catch((e) => console.error(`[sk-${s.id}] finishTurn (spontaneous resume) failed — ${e instanceof Error ? e.message : e}`));
|
|
9551
|
+
// Is `busy` still held by anyone? A flag that latches ON makes the agent
|
|
9552
|
+
// refuse every later prompt, for as long as the process lives, with
|
|
9553
|
+
// nothing on screen or in the log to say so — see `src/busy-latch.ts`.
|
|
9554
|
+
if (settled) {
|
|
9555
|
+
const heldByUs = s.turnWaiters > 0 || !!s.submitting || !!s.recovering || !!s.restarting;
|
|
9556
|
+
const now = Date.now();
|
|
9557
|
+
const verdict = busyLatchVerdict({
|
|
9558
|
+
busy: s.busy,
|
|
9559
|
+
turnInFlight: s.turnWaiters > 0,
|
|
9560
|
+
submitting: !!s.submitting,
|
|
9561
|
+
recovering: !!s.recovering,
|
|
9562
|
+
restarting: !!s.restarting,
|
|
9563
|
+
// Read only when it can change the answer: this runs every 300 ms on
|
|
9564
|
+
// every live agent, and `isWorking` scans the whole screen.
|
|
9565
|
+
working: s.busy && !heldByUs ? pilot.isWorking() : false,
|
|
9566
|
+
since: s.busyLatchSince ?? null,
|
|
9567
|
+
now,
|
|
9568
|
+
graceMs: BUSY_LATCH_GRACE_MS,
|
|
9569
|
+
});
|
|
9570
|
+
if (verdict.action === "watch")
|
|
9571
|
+
s.busyLatchSince = verdict.since;
|
|
9572
|
+
else {
|
|
9573
|
+
const heldSince = s.busyLatchSince;
|
|
9574
|
+
s.busyLatchSince = null;
|
|
9575
|
+
if (verdict.action === "clear")
|
|
9576
|
+
releaseBusyLatch(s, now - (heldSince ?? now));
|
|
9577
|
+
}
|
|
9578
|
+
}
|
|
9547
9579
|
// Every ~15 s (50 × 300 ms): if the pane's process moved to a different
|
|
9548
9580
|
// transcript (Claude Code forked the session on a context overflow), follow
|
|
9549
9581
|
// it so the chat doesn't freeze on the old, dead transcript.
|
|
@@ -9750,17 +9782,51 @@ function escalateUnresponsive(s, why, restarted) {
|
|
|
9750
9782
|
console.error(`submit-recovery: could not file the report for ${s.id.slice(0, 8)} — ${e instanceof Error ? e.message : e}`);
|
|
9751
9783
|
}
|
|
9752
9784
|
}
|
|
9785
|
+
/**
|
|
9786
|
+
* Take `busy` back when nothing is behind it any more.
|
|
9787
|
+
*
|
|
9788
|
+
* Deliberately NOT an interrupt: there is no turn to stop — that is the whole
|
|
9789
|
+
* finding. It undoes the belief and nothing else, then says the turn is over
|
|
9790
|
+
* so every client leaves its "working" state. `turn-done` is also what the
|
|
9791
|
+
* Telegram bridge waits on to replay the message it preempted, so the message
|
|
9792
|
+
* that hit the latch is delivered instead of being lost (`shouldPreempt`).
|
|
9793
|
+
*
|
|
9794
|
+
* `flushParentInbox` for the same reason as in `finishTurn`: a notification
|
|
9795
|
+
* queued while the flag was up ("parent busy") would otherwise sit there for
|
|
9796
|
+
* the life of the process.
|
|
9797
|
+
*/
|
|
9798
|
+
function releaseBusyLatch(s, heldMs) {
|
|
9799
|
+
console.error(busyLatchLog(s.id, heldMs));
|
|
9800
|
+
s.busy = false;
|
|
9801
|
+
s.interruptedAt = null;
|
|
9802
|
+
s.turnStartedAt = null;
|
|
9803
|
+
s.lastActivityAt = Date.now();
|
|
9804
|
+
// The flag was up, so the watcher's dialog detection was off the whole time:
|
|
9805
|
+
// whatever is on screen now has never been announced. Forget the dedup key
|
|
9806
|
+
// and let the next poll publish it.
|
|
9807
|
+
s.lastDialogKey = null;
|
|
9808
|
+
broadcast(s, { type: "turn-done", sessionId: s.id });
|
|
9809
|
+
flushParentInbox(s.id);
|
|
9810
|
+
}
|
|
9753
9811
|
async function finishTurn(s) {
|
|
9754
9812
|
s.busy = true;
|
|
9755
|
-
//
|
|
9756
|
-
//
|
|
9757
|
-
//
|
|
9758
|
-
//
|
|
9759
|
-
|
|
9760
|
-
|
|
9761
|
-
|
|
9762
|
-
|
|
9813
|
+
// Everything from here is INSIDE the try, including the three statements
|
|
9814
|
+
// that used to sit between raising `busy` and entering it. A throw there —
|
|
9815
|
+
// a transcript read, a screen read, a broadcast — left the flag up with no
|
|
9816
|
+
// `finally` to lower it, and the spontaneous-resume caller below swallows
|
|
9817
|
+
// what it throws: an agent refusing every later prompt, in silence. The
|
|
9818
|
+
// watcher now takes the flag back on its own (`src/busy-latch.ts`); this
|
|
9819
|
+
// just stops handing it that work.
|
|
9820
|
+
s.turnWaiters++;
|
|
9763
9821
|
try {
|
|
9822
|
+
// Turn found already running (not started by us): typically after a server
|
|
9823
|
+
// restart, the tmux agent having carried on without it. `turnStartedAt` lives
|
|
9824
|
+
// in memory, so restarting from `now` reset the stopwatch while the agent had
|
|
9825
|
+
// been thinking for ten minutes; the transcript, though, survived.
|
|
9826
|
+
if (!s.turnStartedAt)
|
|
9827
|
+
s.turnStartedAt = resumedTurnStart(Date.now(), lastPromptAt(s.cwd, s.id, s.transcriptHome));
|
|
9828
|
+
s.errorsAtTurnStart = findTransientErrors(s.pilot.screen());
|
|
9829
|
+
broadcast(s, workingMessage(s));
|
|
9764
9830
|
// Read on every poll, so an interrupt arriving mid-wait takes effect at
|
|
9765
9831
|
// once instead of at the next turn.
|
|
9766
9832
|
await s.pilot.waitForIdle({
|
|
@@ -9803,7 +9869,9 @@ async function finishTurn(s) {
|
|
|
9803
9869
|
}
|
|
9804
9870
|
}
|
|
9805
9871
|
finally {
|
|
9872
|
+
s.turnWaiters = Math.max(0, s.turnWaiters - 1);
|
|
9806
9873
|
s.busy = false;
|
|
9874
|
+
s.busyLatchSince = null;
|
|
9807
9875
|
s.interruptedAt = null;
|
|
9808
9876
|
s.lastActivityAt = Date.now();
|
|
9809
9877
|
// Remember how long it took: a dialog suspends the turn and a completion
|
|
@@ -10010,7 +10078,12 @@ function maybeScheduleRetry(s) {
|
|
|
10010
10078
|
s.turnStartedAt = Date.now();
|
|
10011
10079
|
broadcast(s, workingMessage(s));
|
|
10012
10080
|
try {
|
|
10013
|
-
|
|
10081
|
+
// Through `submitGuarded`, like every other submit. It holds an interrupt
|
|
10082
|
+
// that lands mid-typing — which would delete the text instead of stopping
|
|
10083
|
+
// anything — and it is what says someone IS behind `busy` while a slow
|
|
10084
|
+
// paste is in flight, so the latch watcher does not mistake a retry being
|
|
10085
|
+
// typed for a flag nobody holds (`src/busy-latch.ts`).
|
|
10086
|
+
await submitGuarded(s, "continue");
|
|
10014
10087
|
}
|
|
10015
10088
|
catch {
|
|
10016
10089
|
return; // TUI unreachable: give the user back the controls
|
|
@@ -10018,7 +10091,9 @@ function maybeScheduleRetry(s) {
|
|
|
10018
10091
|
finally {
|
|
10019
10092
|
s.busy = false;
|
|
10020
10093
|
}
|
|
10021
|
-
|
|
10094
|
+
// Same reason as the spontaneous resume: nobody is waiting on this call,
|
|
10095
|
+
// so a throw it swallows leaves no trace anywhere.
|
|
10096
|
+
await finishTurn(s).catch((e) => console.error(`[sk-${s.id}] finishTurn (auto-retry) failed — ${e instanceof Error ? e.message : e}`));
|
|
10022
10097
|
};
|
|
10023
10098
|
s.retryTimer = setTimeout(fire, delayMs);
|
|
10024
10099
|
}
|