pi-odd-worktree 2.1.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +54 -319
- package/extensions/odd-worktree-tools.ts +4145 -2529
- package/lib/bash-activity.js +1164 -0
- package/lib/bash-design.js +684 -0
- package/lib/bash-destructive.js +559 -0
- package/lib/bash-frame.js +205 -0
- package/lib/card-content.js +23 -0
- package/lib/card-style.js +175 -0
- package/lib/checkout-facts.js +30 -70
- package/lib/codegraph-card-preview.js +628 -0
- package/lib/codegraph-card.js +515 -0
- package/lib/codegraph-compaction.js +76 -0
- package/lib/codegraph-daemon.js +246 -0
- package/lib/codegraph-edit-sync.js +182 -0
- package/lib/codegraph-guard-target.js +337 -0
- package/lib/codegraph-guard.js +211 -0
- package/lib/codegraph-index.js +1020 -0
- package/lib/codegraph-lock.js +607 -0
- package/lib/codegraph-mcp-config.js +134 -0
- package/lib/codegraph-mcp.js +123 -0
- package/lib/codegraph-paths.js +69 -0
- package/lib/codegraph-prompt/explore.md +11 -0
- package/lib/codegraph-prompt/fix.md +6 -0
- package/lib/codegraph-prompt/generic.md +8 -0
- package/lib/codegraph-prompt/judge.md +9 -0
- package/lib/codegraph-prompt/orchestrator.md +17 -0
- package/lib/codegraph-prompt/verify.md +10 -0
- package/lib/codegraph-prompt/worker.md +7 -0
- package/lib/codegraph-role.js +55 -0
- package/lib/codegraph-rules.js +146 -0
- package/lib/codegraph-session.js +303 -383
- package/lib/codegraph-warmup.js +139 -0
- package/lib/file-card-markdown.js +141 -0
- package/lib/file-card-resolver.js +245 -0
- package/lib/file-card.js +659 -0
- package/lib/files-index.js +387 -0
- package/lib/files-insert.js +81 -0
- package/lib/files-orders.js +326 -0
- package/lib/files-picker-view.js +1416 -0
- package/lib/files-preview.js +338 -0
- package/lib/files-scope-detail.js +171 -0
- package/lib/files-scope-view.js +507 -0
- package/lib/gh-issues.js +308 -14
- package/lib/issue-preview.js +26 -7
- package/lib/issues-bash-mutation.js +206 -0
- package/lib/issues-cache-store.js +261 -0
- package/lib/issues-section.js +710 -0
- package/lib/issues-view.js +649 -52
- package/lib/odd-prompt/odd-worktrees.md +21 -0
- package/lib/odd-worktrees-prompt.js +10 -28
- package/lib/related-issues-cache.js +210 -0
- package/lib/shell-card.js +825 -0
- package/lib/skill-odd-card.js +183 -0
- package/lib/tui-paint.js +4 -5
- package/lib/warp-create-panel.js +538 -250
- package/lib/warp-create-picker.js +56 -9
- package/lib/warp-delete-confirm.js +19 -28
- package/lib/warp-selector-view.js +160 -82
- package/lib/which-command.js +90 -0
- package/lib/worktree-card.js +267 -897
- package/lib/worktree-cards.js +383 -0
- package/lib/worktree-create.js +287 -0
- package/lib/worktree-git.js +3 -3
- package/lib/worktree-guard.js +548 -0
- package/lib/worktree-hooks.js +421 -712
- package/lib/worktree-list.js +120 -0
- package/lib/worktree-registry.js +162 -879
- package/lib/{worktree-close-herdr.js → worktree-remove.js} +9 -11
- package/lib/worktree-setup-runner.js +376 -0
- package/lib/worktree-setup.js +140 -0
- package/lib/worktree-warp-busy.js +462 -85
- package/lib/worktree-warp-sessions.js +1 -2
- package/package.json +2 -2
- package/tools/bash-activity.test.js +1419 -0
- package/tools/bash-design.test.js +1091 -0
- package/tools/bash-destructive.test.js +139 -0
- package/tools/card-content.test.js +44 -0
- package/tools/card-style.test.js +298 -0
- package/tools/codegraph-card-preview.test.js +254 -0
- package/tools/codegraph-card.test.js +1080 -0
- package/tools/codegraph-compaction.test.js +378 -0
- package/tools/codegraph-daemon.test.js +248 -0
- package/tools/codegraph-edit-sync.test.js +295 -0
- package/tools/codegraph-guard-target.test.js +174 -0
- package/tools/codegraph-guard.test.js +238 -0
- package/tools/codegraph-index.test.js +1948 -0
- package/tools/codegraph-lock.test.js +932 -0
- package/tools/codegraph-mcp-config.test.js +181 -0
- package/tools/codegraph-mcp.test.js +194 -0
- package/tools/codegraph-paths.test.js +44 -0
- package/tools/codegraph-role.test.js +30 -0
- package/tools/codegraph-rules.test.js +250 -0
- package/tools/codegraph-session.test.js +660 -391
- package/tools/codegraph-warmup.test.js +316 -0
- package/tools/commands.test.js +510 -31
- package/tools/file-card-markdown.test.js +188 -0
- package/tools/file-card.test.js +1289 -0
- package/tools/files-bridge.test.js +575 -0
- package/tools/files-command.test.js +610 -0
- package/tools/files-index.test.js +473 -0
- package/tools/files-insert.test.js +139 -0
- package/tools/files-orders.test.js +597 -0
- package/tools/files-picker-view.test.js +1871 -0
- package/tools/files-preview.test.js +452 -0
- package/tools/files-scope-detail.test.js +264 -0
- package/tools/files-scope-view.test.js +614 -0
- package/tools/fixtures/card-codegraph-outputs.js +110 -0
- package/tools/fixtures/codegraph-cli-stub.js +16 -0
- package/tools/fixtures/codegraph-init-process.js +28 -0
- package/tools/fixtures/codegraph-lock-worker.js +117 -0
- package/tools/fixtures/daemon-fake.js +98 -0
- package/tools/fixtures/daemon-mcp-server.js +39 -0
- package/tools/fixtures/daemon-warmup-worker.js +12 -0
- package/tools/fixtures/load-extension.js +136 -3
- package/tools/gh-issues-feedback.test.js +31 -7
- package/tools/gh-issues.test.js +391 -1
- package/tools/issue-preview.test.js +44 -7
- package/tools/issues-bash-mutation.test.js +101 -0
- package/tools/issues-cache-store.test.js +222 -0
- package/tools/issues-command.test.js +1512 -66
- package/tools/issues-open-guard.test.js +1096 -0
- package/tools/issues-section.test.js +4195 -0
- package/tools/issues-seed-query-regression.test.js +67 -0
- package/tools/issues-view.test.js +1489 -60
- package/tools/odd-worktrees-prompt.test.js +98 -65
- package/tools/package-surface.test.js +431 -36
- package/tools/related-issues-cache.test.js +324 -0
- package/tools/shell-card.test.js +834 -0
- package/tools/skill-odd-card.test.js +200 -0
- package/tools/t5-session-identity.test.js +44 -22
- package/tools/warp-busy-guard.test.js +2565 -309
- package/tools/warp-create-panel.test.js +499 -74
- package/tools/warp-create-picker.test.js +101 -5
- package/tools/warp-delete-confirm.test.js +49 -15
- package/tools/warp-empty-session.test.js +0 -1
- package/tools/warp-fallback-regression.test.js +533 -0
- package/tools/warp-selector-view.test.js +460 -196
- package/tools/which-command.test.js +126 -0
- package/tools/worktree-card.test.js +551 -0
- package/tools/worktree-cards.test.js +845 -0
- package/tools/worktree-changes.test.js +4 -5
- package/tools/worktree-create.test.js +489 -0
- package/tools/worktree-guard.test.js +823 -0
- package/tools/worktree-hooks.test.js +905 -0
- package/tools/worktree-list.test.js +170 -0
- package/tools/worktree-registry.test.js +89 -230
- package/tools/worktree-remove-async.test.js +318 -0
- package/tools/worktree-setup-runner.test.js +276 -0
- package/tools/worktree-setup.test.js +140 -0
- package/tools/worktree-tools.test.js +419 -199
- package/lib/codegraph-commands.js +0 -165
- package/lib/odd-prompt/odd-worktrees.with-codegraph.md +0 -19
- package/lib/odd-prompt/odd-worktrees.without-codegraph.md +0 -15
- package/lib/worktree-model-result.js +0 -291
- package/tools/codegraph-commands.test.js +0 -371
- package/tools/herdr-free-create.test.js +0 -161
- package/tools/worktree-close-herdr-async.test.js +0 -204
- package/tools/worktree-odd-migration.test.js +0 -224
- package/tools/worktree-registry-cache.test.js +0 -163
- package/tools/worktree-registry-odd.test.js +0 -231
|
@@ -1,17 +1,30 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
// S4/S5/S9: a warp waits for THIS session's own background work —
|
|
3
|
-
// AND background jobs — and the request is
|
|
2
|
+
// S4/S5/S9: a warp waits for THIS session's own background work — the tools it
|
|
3
|
+
// is executing, its agent tasks, AND its background jobs — and the request is
|
|
4
|
+
// QUEUED while that work runs.
|
|
4
5
|
//
|
|
5
|
-
// WHAT IT READS.
|
|
6
|
+
// WHAT IT READS. Three sources:
|
|
6
7
|
//
|
|
7
|
-
// 1.
|
|
8
|
+
// 1. Tools executing in this session. The host emits `tool_execution_start` and
|
|
9
|
+
// `tool_execution_end` for EVERY tool execution of the session (its own
|
|
10
|
+
// `bash`, `read`, and every other tool), and every finalized call gets its
|
|
11
|
+
// end; the extension's one entry point feeds this counter from that pair.
|
|
12
|
+
// Only TOP-LEVEL executions are counted: a nested call (a tool another tool
|
|
13
|
+
// ran through `ctx.executeTool`) always runs inside a parent execution that
|
|
14
|
+
// is already counted, and this module's own `job_list` probe is one, so a
|
|
15
|
+
// warp never waits on its own probe. An execution with no end yet means
|
|
16
|
+
// something is still doing work, which is what the warp must wait for. The
|
|
17
|
+
// counter is process memory: a host restart starts empty, `session_shutdown`
|
|
18
|
+
// clears it, and a tool that really never ends is bounded by the pending
|
|
19
|
+
// record's own lifetime, which expires with a notice and clears.
|
|
20
|
+
// 2. Agent tasks. Gentle Shell publishes one presence header per live Pi
|
|
8
21
|
// session, carrying that session's agent-task counters
|
|
9
22
|
// (`counts.{running,queued,waiting,finished}`) and a heartbeat. This module
|
|
10
23
|
// reads that store through the SAME bounded reader the checkout-deletion
|
|
11
24
|
// guard uses, hashes this session's id with the same sha256, and keeps only
|
|
12
25
|
// ITS freshest header. Nothing else is read from it: no transcript, no
|
|
13
26
|
// prompt, no activity payload.
|
|
14
|
-
//
|
|
27
|
+
// 3. Background jobs. Gentle Shell registers a `job_list` tool on the session,
|
|
15
28
|
// reachable through the host seam this package already uses for
|
|
16
29
|
// `session_worktree_register` (`ctx.tools` lists it, `ctx.executeTool` calls
|
|
17
30
|
// it). Its text is one line per job:
|
|
@@ -20,26 +33,95 @@
|
|
|
20
33
|
// monitor's `monitor running`); `exited 0`, `failed 1` and `stopped` are all
|
|
21
34
|
// finished, and "not exited" is never read as activity.
|
|
22
35
|
//
|
|
23
|
-
// THE DECISION. One rule, shared by the
|
|
24
|
-
//
|
|
25
|
-
// agent task OR an active job.
|
|
36
|
+
// THE DECISION. One rule, shared by the command front and the queue's poller:
|
|
37
|
+
// nothing may move while this session has a tool still executing, a running or
|
|
38
|
+
// queued agent task, OR an active job. The command front adds one more source
|
|
39
|
+
// the counters cannot see: the HOST's own idle state (`ctx.isIdle()`), which is
|
|
40
|
+
// false while Pi processes a run, a retry, an auto-compaction retry or a queued
|
|
41
|
+
// continuation. "The assistant is still thinking" is work even though no tool,
|
|
42
|
+
// task or job reports it. A front reads the counters ONCE, as they are, when the
|
|
43
|
+
// warp is requested: a busy session is QUEUED AT ONCE, and nothing waits for the
|
|
44
|
+
// work to drain (a request that waited found the session idle afterwards,
|
|
45
|
+
// skipped the queue — and so its steering row — and asked the pending-changes
|
|
46
|
+
// question again on every attempt).
|
|
26
47
|
//
|
|
27
48
|
// THE QUEUE. One pending warp per session, held in this process (the tool and
|
|
28
|
-
// the command share it). A bounded poller re-reads
|
|
29
|
-
// re-dispatches the SAME validated handoff once the session reports
|
|
30
|
-
//
|
|
31
|
-
//
|
|
32
|
-
//
|
|
33
|
-
//
|
|
34
|
-
//
|
|
49
|
+
// the command share it). A bounded poller re-reads all three sources and
|
|
50
|
+
// re-dispatches the SAME validated handoff once the session reports none of that
|
|
51
|
+
// work; the caller clears the record on every terminal outcome (moved, refused,
|
|
52
|
+
// cancelled, expired, shutdown). A second warp is refused while one is pending:
|
|
53
|
+
// the pending request is never replaced. The pending lifetime is bounded
|
|
54
|
+
// (`ODD_WORKTREE_WARP_PENDING_MS`) and every timer is unref'd where the platform
|
|
55
|
+
// allows it, so a queue never keeps the process alive.
|
|
56
|
+
//
|
|
57
|
+
// THE PERSISTENT LINE. While the record exists, the queue also OWNS one line for
|
|
58
|
+
// the human — `Queued: warp <worktree> — waiting for <the work>` — and asks the
|
|
59
|
+
// injected `paint` seam to show it (the extension renders it in the zone above
|
|
60
|
+
// the editor, in the shape of Pi's own steering row). It is painted when the
|
|
61
|
+
// record arms and again only when the reason changes, and it is cleared on the
|
|
62
|
+
// dispatch and on every terminal outcome, because a row may never claim a move
|
|
63
|
+
// that has not happened. The line is presentation, never state: a host that
|
|
64
|
+
// offers no such seam simply shows nothing, and the queue behaves identically.
|
|
65
|
+
// It is also the SAME sentence the queued answer carries (`queuedText`), so the
|
|
66
|
+
// row and the answer can never describe different work or different state.
|
|
67
|
+
//
|
|
68
|
+
// ONE SURFACE PER REQUEST (S8, warp-queue-boundary). The record keeps the answer
|
|
69
|
+
// the painter gave it (`painted`): true only when a surface actually took the
|
|
70
|
+
// line — the row above the editor, or the durable fallback entry. The queued
|
|
71
|
+
// answer reports that fact back to its own front, so a line the human can already
|
|
72
|
+
// see is never printed a second time in the chat; only a host with no surface at
|
|
73
|
+
// all falls back to the one notice.
|
|
74
|
+
//
|
|
75
|
+
// THE DISPATCH BOUNDARY (S2, warp-queue-boundary). A dispatch is only ever
|
|
76
|
+
// attempted into an IDLE host: the record holds an `isIdle` seam (the bound
|
|
77
|
+
// `ctx.isIdle`), and a host that reports a live run leaves the record PENDING,
|
|
78
|
+
// with its dispatch slot released and its poller re-armed, instead of claiming
|
|
79
|
+
// a dispatch. The host does NOT reject a slash command sent while it streams:
|
|
80
|
+
// `AgentSession.prompt` tries the extension command first, so the handler runs
|
|
81
|
+
// at once and parks in `ctx.waitForIdle()` until the WHOLE run settles. A record
|
|
82
|
+
// that claimed such a dispatch would therefore sit `dispatching` behind a parked
|
|
83
|
+
// handler, which is exactly the "it only moved after I stopped it" symptom.
|
|
84
|
+
// Absent or unknown blocks nothing, exactly like the other best-effort sources.
|
|
85
|
+
//
|
|
86
|
+
// THE TURN BOUNDARY (S1/S13/S17, warp-queue-boundary). Pi delivers a queued
|
|
87
|
+
// input at the end of the current assistant turn — after its tool calls, before
|
|
88
|
+
// the next model call — and a pending warp takes effect at that SAME boundary.
|
|
89
|
+
// On the host's `turn_end` event, when the ONLY thing still blocking the record
|
|
90
|
+
// is the run itself (no tool executing, no running or queued agent task, no
|
|
91
|
+
// active job) AND the turn that just ended executed tools, the extension ends the
|
|
92
|
+
// run through the host's own abort, ONCE, so the move lands where a queued input
|
|
93
|
+
// would. NOTHING is ever sent to the agent (S17): an instruction to a model is
|
|
94
|
+
// probabilistic — the human's own session watched the model answer it with a long
|
|
95
|
+
// text turn instead of ending its run, and a text turn produces no later boundary
|
|
96
|
+
// for a safety net to fire on — while the abort is deterministic. One abort per
|
|
97
|
+
// record, never twice, and a record with real workload pending is never aborted.
|
|
98
|
+
// A turn that executed no tool is the agent's final answer — the run settles by
|
|
99
|
+
// itself and the settle tick dispatches the record — so such a boundary does
|
|
100
|
+
// nothing at all.
|
|
101
|
+
//
|
|
102
|
+
// NOTHING LANDS AFTER A READ (S13). An abort that fires after an awaited read
|
|
103
|
+
// arrives too late: the host has already started the next provider request, and
|
|
104
|
+
// ending the run there cuts an assistant message that carried tool calls, which
|
|
105
|
+
// the host repaints as failed `Operation aborted` results. So the
|
|
106
|
+
// boundary DECIDES SYNCHRONOUSLY, before this module awaits anything: the
|
|
107
|
+
// guards, the host's idle state, the turn's own tool evidence, and the record's
|
|
108
|
+
// LAST SAMPLED activity (`lastActivity`, kept for the expiry text) are all read
|
|
109
|
+
// without a promise, and real work that sample reports still wins over ending
|
|
110
|
+
// the run. The awaited re-read is only the FALLBACK for a record whose last
|
|
111
|
+
// sample is absent or older than one poll interval — the rate at which the queue
|
|
112
|
+
// re-samples — and even then the decision follows the read only when the read
|
|
113
|
+
// proves nothing is running.
|
|
35
114
|
//
|
|
36
115
|
// FAILURE MODE. Best effort, like the close guard: an absent or unreadable
|
|
37
116
|
// presence store, a `job_list` that is missing, fails or answers in a shape this
|
|
38
117
|
// parser does not know, all answer `unknown` and block nothing. Such an answer
|
|
39
118
|
// is never reported as "no jobs" — the texts say the job state is unknown only
|
|
40
119
|
// when that actually happened, and never blame a missing tool that is present.
|
|
120
|
+
// The tool counter has no unknown state of its own: it is fed by the same entry
|
|
121
|
+
// point that owns this queue, and it is 0 only when this process observed no
|
|
122
|
+
// unfinished top-level execution.
|
|
41
123
|
|
|
42
|
-
const { readGentlePresence, hashSessionId
|
|
124
|
+
const { readGentlePresence, hashSessionId } = require('./worktree-session-guard.js');
|
|
43
125
|
|
|
44
126
|
// The host tool that lists this session's background jobs, and the word that
|
|
45
127
|
// makes a job active. The status field is `running`/`queued`, or the job kind
|
|
@@ -49,13 +131,6 @@ const JOB_LIST_TOOL = 'job_list';
|
|
|
49
131
|
const ACTIVE_JOB_STATUS = /(^|\s)(running|queued)(\s|$)/i;
|
|
50
132
|
const NO_JOBS_LINE = /^no background jobs\b/i;
|
|
51
133
|
|
|
52
|
-
// How long the command front waits for both workloads to drain before it queues
|
|
53
|
-
// the request, and how often it re-reads. Overridable so a session can shorten or
|
|
54
|
-
// lengthen its own wait; the default keeps a warp from stalling a turn.
|
|
55
|
-
const DEFAULT_WAIT_MS = 60000;
|
|
56
|
-
const POLL_MS = 2000;
|
|
57
|
-
const MAX_WAIT_MS = 600000;
|
|
58
|
-
|
|
59
134
|
// How long a queued warp may wait for both workloads to drain before it expires
|
|
60
135
|
// with a notice, and how often its poller re-reads them.
|
|
61
136
|
const DEFAULT_PENDING_MS = 30 * 60 * 1000;
|
|
@@ -65,6 +140,42 @@ const MAX_PENDING_POLL_MS = 60000;
|
|
|
65
140
|
|
|
66
141
|
const isCount = (value) => Number.isSafeInteger(value) && value >= 0;
|
|
67
142
|
|
|
143
|
+
// The tools executing in this session, by tool call id. Fed by the host's
|
|
144
|
+
// `tool_execution_start` / `tool_execution_end` pair and read by the one `busy`
|
|
145
|
+
// decision; the module keeps no other tool state.
|
|
146
|
+
const inFlightTools = new Set();
|
|
147
|
+
|
|
148
|
+
// A top-level execution started. A nested call (one a tool ran through
|
|
149
|
+
// `ctx.executeTool`) is NOT counted: it always runs inside a parent execution
|
|
150
|
+
// that is already counted, and this module's own `job_list` probe is exactly
|
|
151
|
+
// that kind of call, so counting it would let a warp wait on its own probe.
|
|
152
|
+
function noteToolStart(event) {
|
|
153
|
+
const id = event && typeof event.toolCallId === 'string' ? event.toolCallId : null;
|
|
154
|
+
if (id === null || id.length === 0) return;
|
|
155
|
+
if (typeof event.parentToolCallId === 'string' && event.parentToolCallId.length > 0) return;
|
|
156
|
+
inFlightTools.add(id);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// A call ended. Only its own id is released, so a `tool_execution_end` for a call
|
|
160
|
+
// that never started (a blocked or aborted one) is a no-op and can never release
|
|
161
|
+
// another execution.
|
|
162
|
+
function noteToolEnd(event) {
|
|
163
|
+
const id = event && typeof event.toolCallId === 'string' ? event.toolCallId : null;
|
|
164
|
+
if (id === null) return;
|
|
165
|
+
inFlightTools.delete(id);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function inFlightToolCount() {
|
|
169
|
+
return inFlightTools.size;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// A session replacement starts with no tool executing: a `tool_execution_end`
|
|
173
|
+
// the host could not deliver (a crash, a reload mid-call) never outlives the
|
|
174
|
+
// session that saw its start.
|
|
175
|
+
function forgetInFlightTools() {
|
|
176
|
+
inFlightTools.clear();
|
|
177
|
+
}
|
|
178
|
+
|
|
68
179
|
function envBudget(env, name, fallback, max) {
|
|
69
180
|
const source = env && typeof env === 'object' ? env : {};
|
|
70
181
|
const raw = source[name];
|
|
@@ -74,10 +185,6 @@ function envBudget(env, name, fallback, max) {
|
|
|
74
185
|
return Math.min(parsed, max);
|
|
75
186
|
}
|
|
76
187
|
|
|
77
|
-
function waitBudgetMs(env) {
|
|
78
|
-
return envBudget(env, 'ODD_WORKTREE_WARP_BUSY_WAIT_MS', DEFAULT_WAIT_MS, MAX_WAIT_MS);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
188
|
function pendingBudgetMs(env) {
|
|
82
189
|
return envBudget(env, 'ODD_WORKTREE_WARP_PENDING_MS', DEFAULT_PENDING_MS, MAX_PENDING_MS);
|
|
83
190
|
}
|
|
@@ -119,9 +226,18 @@ async function agentActivity(opts) {
|
|
|
119
226
|
};
|
|
120
227
|
}
|
|
121
228
|
|
|
122
|
-
// The one drain rule, applied to
|
|
123
|
-
|
|
229
|
+
// The one drain rule, applied to every workload. `tools` is the count of
|
|
230
|
+
// executions this session has not finished, sampled before the queue's own host
|
|
231
|
+
// tool probe runs; `hostIdle` is the host's own idle state when the caller could
|
|
232
|
+
// read it (true/false), or null when it could not (an unknown host state blocks
|
|
233
|
+
// nothing, exactly like the other best-effort sources).
|
|
234
|
+
//
|
|
235
|
+
// A dispatch is allowed only on an idle host (`busy === false`), which is why the
|
|
236
|
+
// host's own state belongs in this answer and not in a second decision.
|
|
237
|
+
function combineActivity(agents, jobs, tools, hostIdle) {
|
|
124
238
|
const active = isCount(jobs.active) ? jobs.active : 0;
|
|
239
|
+
const executing = isCount(tools) ? tools : 0;
|
|
240
|
+
const host = { idle: hostIdle === true || hostIdle === false ? hostIdle : null };
|
|
125
241
|
return {
|
|
126
242
|
status: agents.status,
|
|
127
243
|
running: agents.running,
|
|
@@ -131,7 +247,9 @@ function combineActivity(agents, jobs) {
|
|
|
131
247
|
heartbeat: agents.heartbeat,
|
|
132
248
|
jobs: { status: jobs.status, active, count: isCount(jobs.count) ? jobs.count : 0 },
|
|
133
249
|
jobsUnknown: jobs.status !== 'ok',
|
|
134
|
-
|
|
250
|
+
tools: { inFlight: executing },
|
|
251
|
+
host,
|
|
252
|
+
busy: host.idle === false || agents.running > 0 || agents.queued > 0 || active > 0 || executing > 0,
|
|
135
253
|
};
|
|
136
254
|
}
|
|
137
255
|
|
|
@@ -194,11 +312,16 @@ async function sessionJobs(host) {
|
|
|
194
312
|
return parseJobList(text);
|
|
195
313
|
}
|
|
196
314
|
|
|
197
|
-
//
|
|
315
|
+
// Every workload, as one answer: `busy` is the single drain decision. The tool
|
|
316
|
+
// count is sampled FIRST, before `sessionJobs` reaches the host, so the queue's
|
|
317
|
+
// own `job_list` probe can never appear in its own answer. `hostIdle` is the
|
|
318
|
+
// host's own idle state when the caller could read it; absent means unknown,
|
|
319
|
+
// which blocks nothing.
|
|
198
320
|
async function sessionActivity(options = {}) {
|
|
199
321
|
const opts = options && typeof options === 'object' ? options : {};
|
|
322
|
+
const executing = inFlightToolCount();
|
|
200
323
|
const agents = await agentActivity(opts);
|
|
201
|
-
return combineActivity(agents, await sessionJobs(opts.host));
|
|
324
|
+
return combineActivity(agents, await sessionJobs(opts.host), executing, opts.hostIdle);
|
|
202
325
|
}
|
|
203
326
|
|
|
204
327
|
// The workloads this session actually reports, as a phrase. `busy` is what makes
|
|
@@ -207,10 +330,14 @@ function workloadText(activity) {
|
|
|
207
330
|
const running = activity && isCount(activity.running) ? activity.running : 0;
|
|
208
331
|
const queued = activity && isCount(activity.queued) ? activity.queued : 0;
|
|
209
332
|
const jobs = activity && activity.jobs && isCount(activity.jobs.active) ? activity.jobs.active : 0;
|
|
333
|
+
const executing = activity && activity.tools && isCount(activity.tools.inFlight) ? activity.tools.inFlight : 0;
|
|
334
|
+
const hostRunning = !!(activity && activity.host && activity.host.idle === false);
|
|
210
335
|
const parts = [];
|
|
211
336
|
if (running > 0) parts.push(`${running} running agent task(s)`);
|
|
212
337
|
if (queued > 0) parts.push(`${queued} queued agent task(s)`);
|
|
213
338
|
if (jobs > 0) parts.push(`${jobs} running background job${jobs === 1 ? '' : 's'}`);
|
|
339
|
+
if (executing > 0) parts.push(`${executing} tool${executing === 1 ? '' : 's'} still executing`);
|
|
340
|
+
if (hostRunning) parts.push('the current agent run');
|
|
214
341
|
return parts.length > 0 ? parts.join(' and ') : 'no';
|
|
215
342
|
}
|
|
216
343
|
|
|
@@ -230,42 +357,70 @@ function jobsUnknownText(activity) {
|
|
|
230
357
|
return `the background-job state is unknown (${reason}), so confirm any bash_background or monitor job finished`;
|
|
231
358
|
}
|
|
232
359
|
|
|
233
|
-
// The
|
|
234
|
-
//
|
|
235
|
-
//
|
|
236
|
-
|
|
237
|
-
|
|
360
|
+
// The ONE line every queued answer prints (S3/S4, warp-queue-boundary): one brief
|
|
361
|
+
// sentence, exactly the row above the editor, and never a claim that the session
|
|
362
|
+
// moved. No second sentence, no `queue:` caveat row, no join with `;` — the queue
|
|
363
|
+
// is not a log. The unknown-job disclosure lives in the expiry notice alone,
|
|
364
|
+
// where nothing moved and the caveat is what the human still needs.
|
|
365
|
+
function queuedText(name, activity) {
|
|
366
|
+
return pendingSteeringLine(name, activity);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
// S8/S9 (warp-idle-gate): the ONE line a queued warp shows in the zone above the
|
|
370
|
+
// editor, in the shape of Pi's own steering row. `Queued:` is the state and
|
|
371
|
+
// `waiting for …` is the reason, so it reads as a request standing in line and
|
|
372
|
+
// never as a finished move — nothing here says moved, switched or done. The
|
|
373
|
+
// workloads are the same phrase the queued notice prints, so the row and the
|
|
374
|
+
// notice never describe different work; an activity this process could not read
|
|
375
|
+
// is named in words instead of being promised, and a name can never break the
|
|
376
|
+
// one-row contract.
|
|
377
|
+
const ALL_WORK_TEXT = "this session's tools, agent tasks and background jobs";
|
|
378
|
+
|
|
379
|
+
function steeringClean(value) {
|
|
380
|
+
if (typeof value !== 'string') return '';
|
|
381
|
+
return value.replace(/[\x00-\x1f\x7f]+/g, ' ').replace(/\s+/g, ' ').trim();
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
function pendingSteeringLine(name, activity) {
|
|
385
|
+
const checkout = steeringClean(name);
|
|
386
|
+
const workloads = workloadText(activity);
|
|
387
|
+
return `Queued: warp ${checkout.length > 0 ? checkout : 'that checkout'} — waiting for ${workloads === 'no' ? ALL_WORK_TEXT : workloads}`;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// The host's own idle state through the record's seam. True while Pi processes a
|
|
391
|
+
// run, a retry, an auto-compaction retry or a queued continuation; null when this
|
|
392
|
+
// host offers no such seam, when it throws, or when it answers something that is
|
|
393
|
+
// neither true nor false — an unknown host state blocks nothing.
|
|
394
|
+
function readHostIdle(record) {
|
|
395
|
+
const seam = record && record.deps ? record.deps.isIdle : null;
|
|
396
|
+
if (typeof seam !== 'function') return null;
|
|
397
|
+
try {
|
|
398
|
+
const value = seam();
|
|
399
|
+
return value === true || value === false ? value : null;
|
|
400
|
+
} catch {
|
|
401
|
+
return null;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// The record's own answer with the host's idle state folded in, so ONE `busy`
|
|
406
|
+
// stays the whole drain decision for the poller, the turn boundary and the row.
|
|
407
|
+
// A read this process could not perform is combined as an unknown activity, and
|
|
408
|
+
// the host state alone still decides.
|
|
409
|
+
function withHostIdle(activity, hostIdle) {
|
|
410
|
+
if (hostIdle !== true && hostIdle !== false) return activity;
|
|
411
|
+
const base = activity && typeof activity === 'object'
|
|
412
|
+
? activity
|
|
413
|
+
: combineActivity(idleActivity('unknown'), { status: 'unknown', active: 0, count: 0 }, 0, hostIdle);
|
|
414
|
+
return { ...base, host: { idle: hostIdle }, busy: base.busy === true || hostIdle === false };
|
|
238
415
|
}
|
|
239
416
|
|
|
240
417
|
function expiredText(name, minutes, activity) {
|
|
241
418
|
const checkout = typeof name === 'string' && name.length > 0 ? name : 'that checkout';
|
|
242
419
|
const unknown = jobsUnknownText(activity);
|
|
243
|
-
const line = `worktree warp: the pending warp to ${checkout} expired after ${minutes} min with nothing moved; warp again when this session's agent tasks and background jobs finish`;
|
|
420
|
+
const line = `worktree warp: the pending warp to ${checkout} expired after ${minutes} min with nothing moved; warp again when this session's tools, agent tasks and background jobs finish`;
|
|
244
421
|
return unknown.length > 0 ? `${line} (${unknown})` : line;
|
|
245
422
|
}
|
|
246
423
|
|
|
247
|
-
// Poll until this session has no running or queued agent task, or the budget
|
|
248
|
-
// runs out. `read` and `sleep` are injected so a test never waits for real.
|
|
249
|
-
async function waitForSessionActivity(options = {}) {
|
|
250
|
-
const opts = options && typeof options === 'object' ? options : {};
|
|
251
|
-
const read = typeof opts.read === 'function'
|
|
252
|
-
? opts.read
|
|
253
|
-
: () => sessionActivity({ sessionId: opts.sessionId, env: opts.env, host: opts.host });
|
|
254
|
-
const sleep = typeof opts.sleep === 'function' ? opts.sleep : (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
255
|
-
const budget = isCount(opts.waitMs) ? opts.waitMs : waitBudgetMs(opts.env);
|
|
256
|
-
const poll = isCount(opts.pollMs) && opts.pollMs > 0 ? opts.pollMs : POLL_MS;
|
|
257
|
-
let activity = await read();
|
|
258
|
-
let waited = 0;
|
|
259
|
-
while (activity && activity.busy === true && waited < budget) {
|
|
260
|
-
const step = Math.min(poll, budget - waited);
|
|
261
|
-
if (step <= 0) break;
|
|
262
|
-
await sleep(step);
|
|
263
|
-
waited += step;
|
|
264
|
-
activity = await read();
|
|
265
|
-
}
|
|
266
|
-
return { activity, waited, waitedOut: !!(activity && activity.busy === true) };
|
|
267
|
-
}
|
|
268
|
-
|
|
269
424
|
// ---------------------------------------------------------------------------
|
|
270
425
|
// The queued warp: one pending request per session.
|
|
271
426
|
// ---------------------------------------------------------------------------
|
|
@@ -281,6 +436,11 @@ function pendingKeyFor(source) {
|
|
|
281
436
|
return root !== null ? `root:${root}` : null;
|
|
282
437
|
}
|
|
283
438
|
|
|
439
|
+
// The normalized shape of one handoff request: the identity of the move, the
|
|
440
|
+
// change selection, and the acceptance stamp the request was queued with. An
|
|
441
|
+
// unusable stamp (absent, non-numeric, negative, infinite) is dropped instead of
|
|
442
|
+
// defaulted, so a request that has none reports no elapsed time rather than one
|
|
443
|
+
// invented at dispatch.
|
|
284
444
|
function handoffShape(value) {
|
|
285
445
|
if (!value || typeof value !== 'object' || Array.isArray(value)) return null;
|
|
286
446
|
const changes = value.changes && typeof value.changes === 'object' && !Array.isArray(value.changes)
|
|
@@ -289,13 +449,17 @@ function handoffShape(value) {
|
|
|
289
449
|
paths: Array.isArray(value.changes.paths) ? value.changes.paths.map(String) : [],
|
|
290
450
|
}
|
|
291
451
|
: { mode: 'none', paths: [] };
|
|
292
|
-
|
|
452
|
+
const shape = {
|
|
293
453
|
name: String(value.name || ''),
|
|
294
454
|
target: String(value.target || ''),
|
|
295
455
|
from: String(value.from || ''),
|
|
296
456
|
branch: String(value.branch || ''),
|
|
297
457
|
changes,
|
|
298
458
|
};
|
|
459
|
+
if (typeof value.requestedAt === 'number' && Number.isFinite(value.requestedAt) && value.requestedAt >= 0) {
|
|
460
|
+
shape.requestedAt = value.requestedAt;
|
|
461
|
+
}
|
|
462
|
+
return shape;
|
|
299
463
|
}
|
|
300
464
|
|
|
301
465
|
// Whether two handoff requests are the SAME queued move. The command uses this
|
|
@@ -349,7 +513,7 @@ function pendingWarpRefusalText(pending, requestedName) {
|
|
|
349
513
|
const queuedName = pending && typeof pending.name === 'string' && pending.name.length > 0 ? pending.name : null;
|
|
350
514
|
const name = queuedName
|
|
351
515
|
|| (typeof requestedName === 'string' && requestedName.trim().length > 0 ? requestedName.trim() : 'that checkout');
|
|
352
|
-
return `a warp to ${name} is already pending for this session — it runs when
|
|
516
|
+
return `a warp to ${name} is already pending for this session — it runs when this session's tools, agent tasks and background jobs finish; wait for it or let it expire`;
|
|
353
517
|
}
|
|
354
518
|
|
|
355
519
|
function clearRecordTimer(record) {
|
|
@@ -393,9 +557,38 @@ function arm(record, delayMs) {
|
|
|
393
557
|
function settle(record) {
|
|
394
558
|
clearRecordTimer(record);
|
|
395
559
|
if (pendingWarps.get(record.key) === record) pendingWarps.delete(record.key);
|
|
560
|
+
// Every terminal outcome clears the line with the record: a move, a refusal, a
|
|
561
|
+
// cancellation, an expiry, or a session that went away. An already cleared
|
|
562
|
+
// line is not repainted.
|
|
563
|
+
setSteering(record, null);
|
|
396
564
|
return pendingInfo(record);
|
|
397
565
|
}
|
|
398
566
|
|
|
567
|
+
// The steering line is one host widget repainted, never state of its own: the
|
|
568
|
+
// queue keeps only the last text it asked for, so a two-second poll does not
|
|
569
|
+
// repaint an identical row for half an hour. A host whose paint throws cannot
|
|
570
|
+
// fail the queue, exactly like `notify`. Returns whether a surface actually took
|
|
571
|
+
// the line (S8): the painter's own answer, and `false` when the host has no
|
|
572
|
+
// surface to paint it on.
|
|
573
|
+
function setSteering(record, text) {
|
|
574
|
+
const next = typeof text === 'string' && text.length > 0 ? text : null;
|
|
575
|
+
if (record.steering === next) return record.steeringPainted === true;
|
|
576
|
+
record.steering = next;
|
|
577
|
+
record.steeringPainted = false;
|
|
578
|
+
const paint = record.deps ? record.deps.paint : null;
|
|
579
|
+
if (typeof paint !== 'function') return false;
|
|
580
|
+
try {
|
|
581
|
+
// The painter is called for EVERY change, a cleared line included: `painted`
|
|
582
|
+
// describes the text the human can see for THIS request, so a clear is never
|
|
583
|
+
// a line on screen.
|
|
584
|
+
const accepted = paint(next) === true;
|
|
585
|
+
record.steeringPainted = next !== null && accepted;
|
|
586
|
+
} catch {
|
|
587
|
+
// A dead or hostile UI never fails the queue.
|
|
588
|
+
}
|
|
589
|
+
return record.steeringPainted;
|
|
590
|
+
}
|
|
591
|
+
|
|
399
592
|
function expire(record) {
|
|
400
593
|
const minutes = Math.max(1, Math.round((record.expiresAt - record.queuedAt) / 60000));
|
|
401
594
|
const name = record.name;
|
|
@@ -408,9 +601,10 @@ function expire(record) {
|
|
|
408
601
|
return { status: 'expired' };
|
|
409
602
|
}
|
|
410
603
|
|
|
411
|
-
// One poll step: expire, keep waiting while the counters are busy
|
|
412
|
-
// the SAME validated handoff once
|
|
413
|
-
// deterministically; the timer above calls it
|
|
604
|
+
// One poll step: expire, keep waiting while the counters are busy OR the host is
|
|
605
|
+
// still running, or dispatch the SAME validated handoff once both are clear.
|
|
606
|
+
// Awaitable so tests drive it deterministically; the timer above calls it
|
|
607
|
+
// fire-and-forget. Never rejects.
|
|
414
608
|
async function tick(record) {
|
|
415
609
|
if (pendingWarps.get(record.key) !== record) return { status: 'none' };
|
|
416
610
|
const now = record.deps.now();
|
|
@@ -421,6 +615,11 @@ async function tick(record) {
|
|
|
421
615
|
arm(record, record.expiresAt - now);
|
|
422
616
|
return { status: 'waiting' };
|
|
423
617
|
}
|
|
618
|
+
// The dispatch slot is claimed BEFORE the counters are read, not after: the
|
|
619
|
+
// settle boundary and the poller can both tick the same record, and a read is
|
|
620
|
+
// awaited. A tick that arrives while this one is inside that read finds the
|
|
621
|
+
// slot taken and waits instead of reading and dispatching the record too.
|
|
622
|
+
record.dispatching = true;
|
|
424
623
|
let activity = null;
|
|
425
624
|
try {
|
|
426
625
|
activity = await record.deps.read();
|
|
@@ -428,14 +627,27 @@ async function tick(record) {
|
|
|
428
627
|
activity = null;
|
|
429
628
|
}
|
|
430
629
|
if (pendingWarps.get(record.key) !== record) return { status: 'none' };
|
|
630
|
+
// S2 (warp-queue-boundary): the dispatch is only ever attempted into an idle
|
|
631
|
+
// host. A host that reports a live run releases the slot and re-arms instead
|
|
632
|
+
// of marking a dispatch it could not accept.
|
|
633
|
+
activity = withHostIdle(activity, readHostIdle(record));
|
|
431
634
|
// The last answer is what the expiry notice can honestly say: an unknown job
|
|
432
|
-
// state is reported only when it was actually seen.
|
|
635
|
+
// state is reported only when it was actually seen. `lastActivityAt` is when it
|
|
636
|
+
// was sampled, so the turn boundary knows how far to trust it (S13).
|
|
433
637
|
record.lastActivity = activity;
|
|
638
|
+
record.lastActivityAt = record.deps.now();
|
|
434
639
|
if (activity && activity.busy === true) {
|
|
640
|
+
// Still waiting, and possibly for different work than last time: the line
|
|
641
|
+
// names the work it is actually waiting for. Nothing was dispatched, so the
|
|
642
|
+
// slot is released for the next tick.
|
|
643
|
+
record.dispatching = false;
|
|
644
|
+
setSteering(record, pendingSteeringLine(record.name, activity));
|
|
435
645
|
arm(record, record.deps.poll);
|
|
436
646
|
return { status: 'waiting' };
|
|
437
647
|
}
|
|
438
|
-
|
|
648
|
+
// The wait is over, so the line goes BEFORE the dispatch: it belongs to the
|
|
649
|
+
// queue, and it must never stand there claiming a move that has not happened.
|
|
650
|
+
setSteering(record, null);
|
|
439
651
|
let refused = false;
|
|
440
652
|
try {
|
|
441
653
|
const dispatched = await record.deps.dispatch(record.handoff, record.token);
|
|
@@ -462,6 +674,161 @@ async function pendingWarpTick(key) {
|
|
|
462
674
|
return tick(record);
|
|
463
675
|
}
|
|
464
676
|
|
|
677
|
+
// Whether the session reports REAL work of its own: a tool executing, a running
|
|
678
|
+
// or queued agent task, or an active job. `waiting`/`finished` counters never
|
|
679
|
+
// count, and an unreadable answer is not evidence of work.
|
|
680
|
+
function activityBlocks(activity) {
|
|
681
|
+
if (!activity || typeof activity !== 'object') return false;
|
|
682
|
+
const running = isCount(activity.running) ? activity.running : 0;
|
|
683
|
+
const queued = isCount(activity.queued) ? activity.queued : 0;
|
|
684
|
+
const jobs = activity.jobs && isCount(activity.jobs.active) ? activity.jobs.active : 0;
|
|
685
|
+
const tools = activity.tools && isCount(activity.tools.inFlight) ? activity.tools.inFlight : 0;
|
|
686
|
+
return running > 0 || queued > 0 || jobs > 0 || tools > 0;
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
// How far the record's last sample is trusted (S13): one poll interval, because
|
|
690
|
+
// that is how often the queue re-samples the workloads. A sample older than that
|
|
691
|
+
// may describe work that already drained, so the boundary re-reads instead of
|
|
692
|
+
// deciding on it — never the other way round, because the abort must not wait.
|
|
693
|
+
function sampleIsTrustworthy(record) {
|
|
694
|
+
if (!record.lastActivity || typeof record.lastActivity !== 'object') return false;
|
|
695
|
+
const at = record.lastActivityAt;
|
|
696
|
+
if (!Number.isFinite(at)) return false;
|
|
697
|
+
const age = record.deps.now() - at;
|
|
698
|
+
return age >= 0 && age <= record.deps.poll;
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
// End the run for this record NOW. The record is marked BEFORE the call, so a
|
|
702
|
+
// throwing or refused abort can never make the next turn abort it a second time.
|
|
703
|
+
function abortRecordRun(record, abort) {
|
|
704
|
+
record.aborted = true;
|
|
705
|
+
try {
|
|
706
|
+
abort();
|
|
707
|
+
} catch {
|
|
708
|
+
// A throwing abort never breaks the turn it was asked to end.
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
// The seams ONE boundary run may use. `sawTools` is the host's own evidence about
|
|
713
|
+
// the turn that just ended (`event.toolResults.length > 0`), kept as a strict
|
|
714
|
+
// boolean or null: an unknown answer is never read as "the agent finished",
|
|
715
|
+
// because that would silently drop the boundary. `abort` is the host's own abort
|
|
716
|
+
// and the ONLY thing this boundary ever does to the run — nothing is ever sent to
|
|
717
|
+
// the agent (S17).
|
|
718
|
+
function boundarySeams(options) {
|
|
719
|
+
const opts = options && typeof options === 'object' ? options : {};
|
|
720
|
+
return {
|
|
721
|
+
abort: typeof opts.abort === 'function' ? opts.abort : null,
|
|
722
|
+
sawTools: opts.sawTools === true || opts.sawTools === false ? opts.sawTools : null,
|
|
723
|
+
};
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
// The guards shared by the sync decision and its fallback: a record that is gone,
|
|
727
|
+
// already ended, or waiting on its own dispatch is left exactly as it is.
|
|
728
|
+
function boundaryGuards(record) {
|
|
729
|
+
if (pendingWarps.get(record.key) !== record) return { status: 'none' };
|
|
730
|
+
if (record.aborted === true || record.dispatching === true) return { status: 'skipped' };
|
|
731
|
+
if (record.turning === true) return { status: 'skipped' };
|
|
732
|
+
return null;
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
// The step taken once the run is the only thing left (S1/S13/S17). A turn that
|
|
736
|
+
// executed no tool is the agent's final answer: the run settles by itself, so
|
|
737
|
+
// nothing is ended. Otherwise the run is ended for it, ONCE, through the host's
|
|
738
|
+
// own abort — the deterministic step, taken before any await so it lands between
|
|
739
|
+
// provider requests. Nothing is ever sent to the agent, and a host with no abort
|
|
740
|
+
// seam leaves the run alone (`unavailable`).
|
|
741
|
+
function endRunStep(record, seams) {
|
|
742
|
+
if (seams.sawTools === false) return { status: 'settling' };
|
|
743
|
+
if (typeof seams.abort !== 'function') return { status: 'unavailable' };
|
|
744
|
+
abortRecordRun(record, seams.abort);
|
|
745
|
+
return { status: 'aborted' };
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
// The SYNC decision for ONE record, taken in the `turn_end` path before this
|
|
749
|
+
// module awaits anything (S1/S13/S17, warp-queue-boundary): while a warp is
|
|
750
|
+
// pending and the run itself is the only thing still blocking it, the run is
|
|
751
|
+
// ended for it, so the move lands where Pi would deliver a queued input. Nothing
|
|
752
|
+
// here can suspend, so the abort can only ever land between provider requests.
|
|
753
|
+
// The record's last sample decides whether real work is still running,
|
|
754
|
+
// which wins over ending the run; the host's own idle state decides the rest. A
|
|
755
|
+
// record that already aborted never aborts again, and a record whose dispatch is
|
|
756
|
+
// in flight is left alone — so a refused dispatch can never spin the session.
|
|
757
|
+
// Returns `{ status: 'stale' }` when the sample is absent or too old to trust,
|
|
758
|
+
// which is the only case the caller resolves with the awaited fallback.
|
|
759
|
+
function turnBoundaryDecision(record, seams) {
|
|
760
|
+
const guard = boundaryGuards(record);
|
|
761
|
+
if (guard !== null) return guard;
|
|
762
|
+
if (!sampleIsTrustworthy(record)) return { status: 'stale' };
|
|
763
|
+
// An idle host needs nothing: the settle tick dispatches the record itself.
|
|
764
|
+
if (readHostIdle(record) === true) return { status: 'idle' };
|
|
765
|
+
// Real work the last sample reported wins over ending the run.
|
|
766
|
+
if (activityBlocks(record.lastActivity)) return { status: 'busy' };
|
|
767
|
+
return endRunStep(record, seams);
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
// The FALLBACK for a record whose last sample is absent or too old to trust: one
|
|
771
|
+
// awaited re-read, and the step follows it only when it proves nothing is
|
|
772
|
+
// running. This is the slow path, never the step in front of a normal abort.
|
|
773
|
+
// Never rejects.
|
|
774
|
+
async function turnBoundaryFallback(record, seams) {
|
|
775
|
+
const guard = boundaryGuards(record);
|
|
776
|
+
if (guard !== null) return guard;
|
|
777
|
+
record.turning = true;
|
|
778
|
+
let activity = null;
|
|
779
|
+
try {
|
|
780
|
+
activity = await record.deps.read();
|
|
781
|
+
} catch {
|
|
782
|
+
activity = null;
|
|
783
|
+
}
|
|
784
|
+
record.turning = false;
|
|
785
|
+
if (pendingWarps.get(record.key) !== record) return { status: 'none' };
|
|
786
|
+
if (record.aborted === true || record.dispatching === true) return { status: 'skipped' };
|
|
787
|
+
// The read is the record's new sample, so the next boundary can decide without
|
|
788
|
+
// waiting again.
|
|
789
|
+
record.lastActivity = activity;
|
|
790
|
+
record.lastActivityAt = record.deps.now();
|
|
791
|
+
if (readHostIdle(record) === true) return { status: 'idle' };
|
|
792
|
+
if (activityBlocks(activity)) return { status: 'busy' };
|
|
793
|
+
return endRunStep(record, seams);
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
// The turn boundary for EVERY pending record, from the host's `turn_end`
|
|
797
|
+
// handler: the one place that knows a turn just ended, whether that turn
|
|
798
|
+
// executed tools, and whether the run is still the only blocker. EVERY record is
|
|
799
|
+
// decided in ONE synchronous pass before this function's first await (S13), so
|
|
800
|
+
// the abort can never arrive after the host has started the next request; only
|
|
801
|
+
// records whose sample is too old fall back to an awaited re-read, and those are
|
|
802
|
+
// resolved afterwards. `options` carries the two seams of the boundary: `abort`
|
|
803
|
+
// (the host's own abort) and `sawTools`. The seams come from the event's own
|
|
804
|
+
// context, so a record never pins a command context it no longer runs in. Never
|
|
805
|
+
// rejects.
|
|
806
|
+
async function pendingWarpTurnBoundary(options = {}) {
|
|
807
|
+
const seams = boundarySeams(options);
|
|
808
|
+
const results = [];
|
|
809
|
+
const fallbacks = [];
|
|
810
|
+
for (const key of [...pendingWarps.keys()]) {
|
|
811
|
+
const record = pendingWarps.get(key);
|
|
812
|
+
if (!record) continue;
|
|
813
|
+
let decision;
|
|
814
|
+
try {
|
|
815
|
+
decision = turnBoundaryDecision(record, seams);
|
|
816
|
+
} catch {
|
|
817
|
+
decision = { status: 'skipped' };
|
|
818
|
+
}
|
|
819
|
+
if (decision.status === 'stale') fallbacks.push([record, results.length]);
|
|
820
|
+
results.push(decision);
|
|
821
|
+
}
|
|
822
|
+
for (const [record, index] of fallbacks) {
|
|
823
|
+
try {
|
|
824
|
+
results[index] = await turnBoundaryFallback(record, seams);
|
|
825
|
+
} catch {
|
|
826
|
+
results[index] = { status: 'skipped' };
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
return results;
|
|
830
|
+
}
|
|
831
|
+
|
|
465
832
|
// Hold one validated handoff as this session's pending warp and start polling.
|
|
466
833
|
// A second request for the same key is refused (`reason: 'pending'`) and leaves
|
|
467
834
|
// the existing one untouched. Never throws.
|
|
@@ -484,14 +851,29 @@ function queuePendingWarp(options = {}) {
|
|
|
484
851
|
queuedAt,
|
|
485
852
|
expiresAt: queuedAt + pendingBudgetMs(env),
|
|
486
853
|
dispatching: false,
|
|
854
|
+
aborted: false,
|
|
855
|
+
turning: false,
|
|
487
856
|
timer: null,
|
|
488
|
-
|
|
857
|
+
steering: null,
|
|
858
|
+
// S8 (warp-queue-boundary): whether a surface actually took the line, so the
|
|
859
|
+
// queued answer knows it must not print the same sentence again.
|
|
860
|
+
steeringPainted: false,
|
|
861
|
+
lastActivity: opts.activity && typeof opts.activity === 'object' ? opts.activity : null,
|
|
862
|
+
// S13: when that sample was taken. A sample is trusted at the turn boundary
|
|
863
|
+
// for one poll interval, the rate at which the queue re-samples; an older one
|
|
864
|
+
// (or none) sends the boundary through its awaited fallback instead.
|
|
865
|
+
lastActivityAt: opts.activity && typeof opts.activity === 'object' ? queuedAt : null,
|
|
489
866
|
deps: {
|
|
490
867
|
read: typeof opts.read === 'function'
|
|
491
868
|
? opts.read
|
|
492
869
|
: () => sessionActivity({ env, host: opts.host }),
|
|
870
|
+
// S2 (warp-queue-boundary): the host's own idle state is a seam the RECORD
|
|
871
|
+
// holds, read on every tick, so a dispatch is never attempted into a
|
|
872
|
+
// streaming host. Null (no seam) blocks nothing.
|
|
873
|
+
isIdle: typeof opts.isIdle === 'function' ? opts.isIdle : null,
|
|
493
874
|
dispatch: typeof opts.dispatch === 'function' ? opts.dispatch : null,
|
|
494
875
|
notify: typeof opts.notify === 'function' ? opts.notify : null,
|
|
876
|
+
paint: typeof opts.paint === 'function' ? opts.paint : null,
|
|
495
877
|
env,
|
|
496
878
|
now,
|
|
497
879
|
poll: Math.max(1, pendingPollMs(env)),
|
|
@@ -499,19 +881,13 @@ function queuePendingWarp(options = {}) {
|
|
|
499
881
|
},
|
|
500
882
|
};
|
|
501
883
|
pendingWarps.set(key, record);
|
|
884
|
+
// The line is shown BEFORE the first poll, naming the work already known, so a
|
|
885
|
+
// request is visible the moment it is queued instead of after one interval.
|
|
886
|
+
setSteering(record, pendingSteeringLine(record.name, record.lastActivity));
|
|
502
887
|
arm(record, record.deps.poll);
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
// The request was already dispatched (the tool front hands its handoff to the
|
|
507
|
-
// command and only keeps the record so a second warp is refused): wait for that
|
|
508
|
-
// dispatch to settle or for the queue to expire instead of sending a copy.
|
|
509
|
-
function markPendingWarpDispatched(key) {
|
|
510
|
-
const record = pendingWarps.get(key);
|
|
511
|
-
if (!record) return false;
|
|
512
|
-
record.dispatching = true;
|
|
513
|
-
arm(record, record.expiresAt - record.deps.now());
|
|
514
|
-
return true;
|
|
888
|
+
// S8: `painted` is the painter's own answer, never a guess — a host with no row
|
|
889
|
+
// and no durable entry reports `false`, and only then does the front speak.
|
|
890
|
+
return { ok: true, pending: pendingInfo(record), painted: record.steeringPainted === true, line: record.steering };
|
|
515
891
|
}
|
|
516
892
|
|
|
517
893
|
// The dispatched retry found the session busy again: resume polling for the SAME
|
|
@@ -535,22 +911,25 @@ module.exports = {
|
|
|
535
911
|
ACTIVE_JOB_STATUS,
|
|
536
912
|
DEFAULT_PENDING_MS,
|
|
537
913
|
DEFAULT_PENDING_POLL_MS,
|
|
538
|
-
DEFAULT_WAIT_MS,
|
|
539
914
|
JOB_LIST_TOOL,
|
|
540
915
|
MAX_PENDING_MS,
|
|
541
916
|
MAX_PENDING_POLL_MS,
|
|
542
|
-
MAX_WAIT_MS,
|
|
543
917
|
expiredText,
|
|
918
|
+
forgetInFlightTools,
|
|
919
|
+
inFlightToolCount,
|
|
544
920
|
jobsUnknownText,
|
|
545
|
-
|
|
921
|
+
noteToolEnd,
|
|
922
|
+
noteToolStart,
|
|
546
923
|
parseJobList,
|
|
547
924
|
pendingBudgetMs,
|
|
548
925
|
pendingKeyFor,
|
|
549
926
|
pendingPollMs,
|
|
927
|
+
pendingSteeringLine,
|
|
550
928
|
pendingWarpFor,
|
|
551
929
|
pendingWarpKeys,
|
|
552
930
|
pendingWarpRefusalText,
|
|
553
931
|
pendingWarpTick,
|
|
932
|
+
pendingWarpTurnBoundary,
|
|
554
933
|
queuePendingWarp,
|
|
555
934
|
queuedText,
|
|
556
935
|
resumePendingWarp,
|
|
@@ -558,7 +937,5 @@ module.exports = {
|
|
|
558
937
|
sessionActivity,
|
|
559
938
|
sessionJobs,
|
|
560
939
|
settlePendingWarp,
|
|
561
|
-
waitBudgetMs,
|
|
562
|
-
waitForSessionActivity,
|
|
563
940
|
workloadText,
|
|
564
941
|
};
|