mandrel 2.2.0 → 2.4.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/.agents/scripts/lib/observability/runtime-friction.js +62 -0
- package/.agents/scripts/lib/orchestration/resolve-stories.js +9 -1
- package/.agents/scripts/lib/orchestration/retro-proposals.js +49 -1
- package/.agents/scripts/lib/orchestration/single-story-close/phases/post-land.js +73 -15
- package/.agents/scripts/lib/orchestration/ticket-lease.js +78 -10
- package/.agents/scripts/lib/orchestration/ticketing/transition.js +68 -16
- package/.agents/scripts/lib/single-story-sweep/sweep-lock.js +73 -0
- package/.agents/scripts/lib/wave-runner/live-probe.js +93 -12
- package/.agents/scripts/single-story-init.js +189 -51
- package/.agents/scripts/stories-wave-tick.js +39 -2
- package/.agents/workflows/deliver.md +32 -8
- package/bin/mandrel.js +0 -0
- package/docs/CHANGELOG.md +21 -0
- package/lib/cli/update.js +83 -34
- package/package.json +1 -1
|
@@ -159,6 +159,68 @@ export async function emitRuntimeFriction({
|
|
|
159
159
|
}
|
|
160
160
|
}
|
|
161
161
|
|
|
162
|
+
/**
|
|
163
|
+
* Emit the recovery counterpart of a `story-blocked` record when a Story
|
|
164
|
+
* leaves `agent::blocked` for an active state (Story #4622).
|
|
165
|
+
*
|
|
166
|
+
* A transient block that self-resolves — lease contention or a stale label
|
|
167
|
+
* read under concurrent shared-checkout pressure (swarm-os friction #581) —
|
|
168
|
+
* still fired a `story-blocked` record at the block flip, which the retro
|
|
169
|
+
* composer counts toward the `story-blocked` recurrence total exactly like a
|
|
170
|
+
* terminal block. This emits a companion `story-blocked` record carrying the
|
|
171
|
+
* `details.recovered: true` discriminator, so the composer can net the whole
|
|
172
|
+
* incident out (see `retro-proposals.js`). The category is deliberately kept
|
|
173
|
+
* as `story-blocked` rather than a new bucket: a distinct category would
|
|
174
|
+
* itself aggregate into a routable proposal, re-introducing the noise.
|
|
175
|
+
*
|
|
176
|
+
* Best-effort; never throws.
|
|
177
|
+
*
|
|
178
|
+
* @param {object} args
|
|
179
|
+
* @param {number} args.storyId
|
|
180
|
+
* @param {string} [args.fromState] The state parked at (`agent::blocked`).
|
|
181
|
+
* @param {string} [args.toState] The active state recovered into.
|
|
182
|
+
* @param {object} [args.config]
|
|
183
|
+
* @returns {Promise<boolean>} true when a record was appended.
|
|
184
|
+
*/
|
|
185
|
+
export async function emitBlockRecoveredFriction({
|
|
186
|
+
storyId,
|
|
187
|
+
fromState,
|
|
188
|
+
toState,
|
|
189
|
+
config,
|
|
190
|
+
} = {}) {
|
|
191
|
+
return emitRuntimeFriction({
|
|
192
|
+
storyId,
|
|
193
|
+
category: RUNTIME_FRICTION_CATEGORIES.STORY_BLOCKED,
|
|
194
|
+
tool: 'transitionTicketState',
|
|
195
|
+
details: {
|
|
196
|
+
recovered: true,
|
|
197
|
+
fromState: fromState ?? null,
|
|
198
|
+
toState: toState ?? null,
|
|
199
|
+
},
|
|
200
|
+
config,
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Pure predicate: is this signal a recovery-marked `story-blocked` record?
|
|
206
|
+
* Shared with the retro composer so the "recovered" discriminator is read
|
|
207
|
+
* from one place. A record is a recovery marker when its category is
|
|
208
|
+
* `story-blocked` and `details.recovered === true`.
|
|
209
|
+
*
|
|
210
|
+
* @param {object} signal
|
|
211
|
+
* @returns {boolean}
|
|
212
|
+
*/
|
|
213
|
+
export function isRecoveredBlockSignal(signal) {
|
|
214
|
+
return (
|
|
215
|
+
signal !== null &&
|
|
216
|
+
typeof signal === 'object' &&
|
|
217
|
+
signal.category === RUNTIME_FRICTION_CATEGORIES.STORY_BLOCKED &&
|
|
218
|
+
signal.details !== null &&
|
|
219
|
+
typeof signal.details === 'object' &&
|
|
220
|
+
signal.details.recovered === true
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
|
|
162
224
|
/**
|
|
163
225
|
* Decide whether a `story-deliver-terminal` envelope is worth a friction
|
|
164
226
|
* record, and describe it. **Pure** — no I/O — so the (interesting) policy
|
|
@@ -62,7 +62,7 @@ function normalizeIssueLabels(issue) {
|
|
|
62
62
|
*
|
|
63
63
|
* @param {object} issue
|
|
64
64
|
* @param {number} [requestedId] The id the operator asked for, for error text.
|
|
65
|
-
* @returns {{ id, title, body, url, labels, state }}
|
|
65
|
+
* @returns {{ id, title, body, url, labels, state, assignees }}
|
|
66
66
|
*/
|
|
67
67
|
export function toStoryRecord(issue, requestedId) {
|
|
68
68
|
const id = Number(issue?.number ?? issue?.id ?? requestedId);
|
|
@@ -93,6 +93,14 @@ export function toStoryRecord(issue, requestedId) {
|
|
|
93
93
|
url: issue?.html_url ?? issue?.url ?? null,
|
|
94
94
|
labels,
|
|
95
95
|
state: String(issue?.state ?? 'open').toLowerCase(),
|
|
96
|
+
// The assignee list carries the Story lease (`ticket-lease.js`): its sole
|
|
97
|
+
// assignee is the operator that owns the in-flight run. The probe reads it
|
|
98
|
+
// to withhold a Story another operator holds (`live-probe.js`), so it is
|
|
99
|
+
// threaded onto the record here rather than dropped. `issueToTicket`
|
|
100
|
+
// already reduces assignees to bare login strings; keep only those.
|
|
101
|
+
assignees: Array.isArray(issue?.assignees)
|
|
102
|
+
? issue.assignees.filter((a) => typeof a === 'string' && a.length > 0)
|
|
103
|
+
: [],
|
|
96
104
|
};
|
|
97
105
|
}
|
|
98
106
|
|
|
@@ -34,6 +34,11 @@
|
|
|
34
34
|
* @typedef {Object} FrictionSignal
|
|
35
35
|
* @property {string} category Free-form bucket (e.g. `"lint-loop"`).
|
|
36
36
|
* @property {"framework"|"consumer"} source
|
|
37
|
+
* @property {number} [storyId] Emitting Story id (used to net out recovered
|
|
38
|
+
* `story-blocked` incidents — Story #4622).
|
|
39
|
+
* @property {object} [details] Kind-specific payload; a `story-blocked`
|
|
40
|
+
* record with `details.recovered === true` is a
|
|
41
|
+
* recovery marker.
|
|
37
42
|
*
|
|
38
43
|
* @typedef {Object} BlockedEvent
|
|
39
44
|
* @property {number} ticketId
|
|
@@ -68,6 +73,11 @@
|
|
|
68
73
|
* @property {DiscardedItem[]} discarded
|
|
69
74
|
*/
|
|
70
75
|
|
|
76
|
+
import {
|
|
77
|
+
isRecoveredBlockSignal,
|
|
78
|
+
RUNTIME_FRICTION_CATEGORIES,
|
|
79
|
+
} from '../observability/runtime-friction.js';
|
|
80
|
+
|
|
71
81
|
/**
|
|
72
82
|
* Empty result helper — returned for zero-input callers so the consumer
|
|
73
83
|
* never needs to defensively spread undefineds.
|
|
@@ -89,6 +99,44 @@ function asString(value) {
|
|
|
89
99
|
return value.trim();
|
|
90
100
|
}
|
|
91
101
|
|
|
102
|
+
/**
|
|
103
|
+
* Net transient (self-resolved) blocks out of the signal stream before it is
|
|
104
|
+
* aggregated (Story #4622).
|
|
105
|
+
*
|
|
106
|
+
* A `blocked → active` recovery emits a `story-blocked` record carrying
|
|
107
|
+
* `details.recovered === true`. When a Story has such a marker, its block was
|
|
108
|
+
* transient — lease contention or a stale label read under concurrent
|
|
109
|
+
* shared-checkout pressure (swarm-os friction #581) that cleared on a later
|
|
110
|
+
* beat — not a terminal HITL pause. This drops **every** `story-blocked`
|
|
111
|
+
* record for such a Story (both the original block and its recovery marker),
|
|
112
|
+
* so the retro counts only Stories still parked at `agent::blocked`.
|
|
113
|
+
*
|
|
114
|
+
* The netting is by `storyId`, not 1:1 pairing: a Story that ever recovered
|
|
115
|
+
* from a block in the run is treated as non-terminal for the whole run. That
|
|
116
|
+
* is a deliberate coarsening — the aggregate is a routing heuristic, not an
|
|
117
|
+
* incident ledger, and the signal stream carries no reliable ordering to
|
|
118
|
+
* reconstruct interleaved block/recover cycles. Non-`story-blocked` records
|
|
119
|
+
* and Stories with no recovery marker pass through untouched.
|
|
120
|
+
*
|
|
121
|
+
* @param {FrictionSignal[]} signals
|
|
122
|
+
* @returns {FrictionSignal[]}
|
|
123
|
+
*/
|
|
124
|
+
function netOutRecoveredBlocks(signals) {
|
|
125
|
+
const recoveredStoryIds = new Set();
|
|
126
|
+
for (const sig of signals) {
|
|
127
|
+
if (isRecoveredBlockSignal(sig) && Number.isInteger(sig.storyId)) {
|
|
128
|
+
recoveredStoryIds.add(sig.storyId);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
if (recoveredStoryIds.size === 0) return signals;
|
|
132
|
+
return signals.filter((sig) => {
|
|
133
|
+
if (sig === null || typeof sig !== 'object') return true;
|
|
134
|
+
const isBlocked =
|
|
135
|
+
sig.category === RUNTIME_FRICTION_CATEGORIES.STORY_BLOCKED;
|
|
136
|
+
return !(isBlocked && recoveredStoryIds.has(sig.storyId));
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
92
140
|
/**
|
|
93
141
|
* Aggregate friction signals by `category`, tracking per-source counts and
|
|
94
142
|
* arrival order so we can pick a dominant source deterministically.
|
|
@@ -403,7 +451,7 @@ export function composeRoutedProposals(input) {
|
|
|
403
451
|
} = normalised;
|
|
404
452
|
|
|
405
453
|
return routeCategoryBuckets({
|
|
406
|
-
byCategory: aggregateByCategory(signals),
|
|
454
|
+
byCategory: aggregateByCategory(netOutRecoveredBlocks(signals)),
|
|
407
455
|
blockedForceActionable: blockedForceMap(unresolvedBlockedEvents),
|
|
408
456
|
anchorId,
|
|
409
457
|
anchorKind,
|
|
@@ -29,8 +29,11 @@
|
|
|
29
29
|
* is best-effort and records its own reason.
|
|
30
30
|
*/
|
|
31
31
|
|
|
32
|
+
import path from 'node:path';
|
|
33
|
+
|
|
32
34
|
import { gitSpawn as defaultGitSpawn } from '../../../git-utils.js';
|
|
33
35
|
import { Logger } from '../../../Logger.js';
|
|
36
|
+
import { acquireLockWithWait as defaultAcquireLockWithWait } from '../../../single-story-sweep/sweep-lock.js';
|
|
34
37
|
import {
|
|
35
38
|
executeFastForward as defaultExecuteFastForward,
|
|
36
39
|
planFastForward as defaultPlanFastForward,
|
|
@@ -38,6 +41,21 @@ import {
|
|
|
38
41
|
import { reassertStatusColumn as defaultReassertStatusColumn } from '../../reassert-status-column.js';
|
|
39
42
|
import { captureStoryFollowUps as defaultCaptureStoryFollowUps } from '../../story-follow-ups.js';
|
|
40
43
|
|
|
44
|
+
/**
|
|
45
|
+
* Lockfile that serializes the local-checkout git mutations of the land tail
|
|
46
|
+
* across concurrent closes. Keyed on the **main checkout** (never a
|
|
47
|
+
* worktree): every concurrent `single-story-close` runs its tail against the
|
|
48
|
+
* same `cwd`, so anchoring the lock under that checkout's `.git` directory
|
|
49
|
+
* makes them all contend on one file. `.git` is always present, is one per
|
|
50
|
+
* checkout, and is never itself tracked, so it is a safe rendezvous home.
|
|
51
|
+
*
|
|
52
|
+
* @param {string} cwd Main checkout root.
|
|
53
|
+
* @returns {string}
|
|
54
|
+
*/
|
|
55
|
+
function postLandLockPath(cwd) {
|
|
56
|
+
return path.join(cwd, '.git', 'mandrel-post-land-tail.lock');
|
|
57
|
+
}
|
|
58
|
+
|
|
41
59
|
/**
|
|
42
60
|
* Run one tail step, converting any throw into a `false` + reason. Keeps
|
|
43
61
|
* each step's own body free of defensive boilerplate while guaranteeing the
|
|
@@ -210,6 +228,19 @@ async function stepBaseFastForward({
|
|
|
210
228
|
* after the ref reap so `git branch -D` is not fighting a checkout that just
|
|
211
229
|
* moved HEAD.
|
|
212
230
|
*
|
|
231
|
+
* **Cross-process serialization (Story #4622).** The two local-checkout
|
|
232
|
+
* mutations — `stepRefCleanup` (`git branch -D`) and `stepBaseFastForward`
|
|
233
|
+
* (fast-forward `baseBranch`) — run inside a best-effort cross-process lock
|
|
234
|
+
* keyed on the main checkout. Under concurrent delivery (multiple
|
|
235
|
+
* story-workers closing against one shared checkout + per-Story worktrees),
|
|
236
|
+
* an unserialized tail races on the `main` ref and the worktree registry —
|
|
237
|
+
* the `refCleanup:false` ("used by worktree") / `baseFastForward:false`
|
|
238
|
+
* ("not-fast-forward") signature reported in swarm-os friction #579. The
|
|
239
|
+
* GitHub-touching steps stay OUTSIDE the lock so a contended checkout never
|
|
240
|
+
* delays them. The lock is never load-bearing: on sustained contention the
|
|
241
|
+
* bounded wait expires and the mutations run anyway (proceeding is the same
|
|
242
|
+
* best-effort contract every tail step already has).
|
|
243
|
+
*
|
|
213
244
|
* @param {object} args
|
|
214
245
|
* @param {number} args.storyId
|
|
215
246
|
* @param {string} args.storyBranch
|
|
@@ -223,6 +254,7 @@ async function stepBaseFastForward({
|
|
|
223
254
|
* @param {Function} [args.gitSpawnFn] Test seam.
|
|
224
255
|
* @param {Function} [args.planFastForwardFn] Test seam.
|
|
225
256
|
* @param {Function} [args.executeFastForwardFn] Test seam.
|
|
257
|
+
* @param {Function} [args.acquireLockWithWaitFn] Test seam.
|
|
226
258
|
* @returns {Promise<{ followUps: boolean, statusResync: boolean, refCleanup: boolean, baseFastForward: boolean, details: Record<string, string|null> }>}
|
|
227
259
|
*/
|
|
228
260
|
export async function runPostLandTail({
|
|
@@ -238,6 +270,7 @@ export async function runPostLandTail({
|
|
|
238
270
|
gitSpawnFn = defaultGitSpawn,
|
|
239
271
|
planFastForwardFn = defaultPlanFastForward,
|
|
240
272
|
executeFastForwardFn = defaultExecuteFastForward,
|
|
273
|
+
acquireLockWithWaitFn = defaultAcquireLockWithWait,
|
|
241
274
|
}) {
|
|
242
275
|
progress?.('POST-LAND', `🧾 Running land tail for Story #${storyId}...`);
|
|
243
276
|
|
|
@@ -264,21 +297,46 @@ export async function runPostLandTail({
|
|
|
264
297
|
}),
|
|
265
298
|
{ name: 'status-column resync', progress },
|
|
266
299
|
);
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
300
|
+
// Local-checkout mutations: serialized behind a best-effort cross-process
|
|
301
|
+
// lock (Story #4622). Acquire once, run both steps, release in `finally`.
|
|
302
|
+
const lockCfg = config?.delivery?.postLandLock ?? {};
|
|
303
|
+
const lock = await acquireLockWithWaitFn({
|
|
304
|
+
lockPath: postLandLockPath(cwd),
|
|
305
|
+
waitMs: lockCfg.waitMs,
|
|
306
|
+
pollMs: lockCfg.pollMs,
|
|
307
|
+
timeoutMs: lockCfg.timeoutMs,
|
|
308
|
+
ownerId: `post-land-${storyId}`,
|
|
309
|
+
});
|
|
310
|
+
if (!lock.acquired) {
|
|
311
|
+
// Never load-bearing: proceed anyway. The bounded wait already gave the
|
|
312
|
+
// concurrent holder its window; blocking the land on a lock we could not
|
|
313
|
+
// take would turn a best-effort damper into a false negative.
|
|
314
|
+
progress?.(
|
|
315
|
+
'POST-LAND',
|
|
316
|
+
`⚠️ post-land lock not acquired (${lock.reason}); proceeding unserialized.`,
|
|
317
|
+
);
|
|
318
|
+
}
|
|
319
|
+
let refCleanup;
|
|
320
|
+
let baseFastForward;
|
|
321
|
+
try {
|
|
322
|
+
refCleanup = await step(
|
|
323
|
+
() => stepRefCleanup({ cwd, storyBranch, progress, gitSpawnFn }),
|
|
324
|
+
{ name: 'local ref cleanup', progress },
|
|
325
|
+
);
|
|
326
|
+
baseFastForward = await step(
|
|
327
|
+
() =>
|
|
328
|
+
stepBaseFastForward({
|
|
329
|
+
cwd,
|
|
330
|
+
baseBranch,
|
|
331
|
+
progress,
|
|
332
|
+
planFastForwardFn,
|
|
333
|
+
executeFastForwardFn,
|
|
334
|
+
}),
|
|
335
|
+
{ name: 'base fast-forward', progress },
|
|
336
|
+
);
|
|
337
|
+
} finally {
|
|
338
|
+
if (lock.acquired) lock.release();
|
|
339
|
+
}
|
|
282
340
|
|
|
283
341
|
const tail = {
|
|
284
342
|
followUps: followUps.ok,
|
|
@@ -211,6 +211,16 @@ export async function describeLease(opts) {
|
|
|
211
211
|
* `reason: 'reclaimed'`.
|
|
212
212
|
* - Foreign claim + `steal:true` → reassign operator, `acquired: true`,
|
|
213
213
|
* `reason: 'stolen'`.
|
|
214
|
+
* - Lost a write race → a foreign login co-assigned between our
|
|
215
|
+
* PATCH and the verify re-read; back the
|
|
216
|
+
* operator out, `acquired: false`,
|
|
217
|
+
* `owner: <foreign>`, `reason: 'lost-race'`.
|
|
218
|
+
*
|
|
219
|
+
* Every claiming write is verified: GitHub's assignee PATCH is not a
|
|
220
|
+
* compare-and-set, so two runs that both read the ticket unassigned will both
|
|
221
|
+
* write themselves. {@link claimAndVerify} re-reads after the write and refuses
|
|
222
|
+
* (fail-closed) when a foreign login is present, so the loser of a simultaneous
|
|
223
|
+
* claim never proceeds as though it holds the lease.
|
|
214
224
|
*
|
|
215
225
|
* @param {object} opts
|
|
216
226
|
* @param {object} opts.provider Ticketing provider.
|
|
@@ -225,7 +235,7 @@ export async function describeLease(opts) {
|
|
|
225
235
|
* acquired: boolean,
|
|
226
236
|
* owner: string,
|
|
227
237
|
* previousOwner: string|null,
|
|
228
|
-
* reason: 'unclaimed'|'already-held'|'reclaimed'|'stolen'|'held',
|
|
238
|
+
* reason: 'unclaimed'|'already-held'|'reclaimed'|'stolen'|'held'|'lost-race',
|
|
229
239
|
* }>}
|
|
230
240
|
*/
|
|
231
241
|
export async function acquireLease(opts) {
|
|
@@ -240,13 +250,13 @@ export async function acquireLease(opts) {
|
|
|
240
250
|
|
|
241
251
|
// Unclaimed → take it.
|
|
242
252
|
if (owner === null) {
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
253
|
+
return claimAndVerify({
|
|
254
|
+
provider,
|
|
255
|
+
ticketId,
|
|
256
|
+
operator,
|
|
247
257
|
previousOwner: null,
|
|
248
258
|
reason: 'unclaimed',
|
|
249
|
-
};
|
|
259
|
+
});
|
|
250
260
|
}
|
|
251
261
|
|
|
252
262
|
// Already ours → no write needed.
|
|
@@ -270,12 +280,70 @@ export async function acquireLease(opts) {
|
|
|
270
280
|
};
|
|
271
281
|
}
|
|
272
282
|
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
283
|
+
return claimAndVerify({
|
|
284
|
+
provider,
|
|
285
|
+
ticketId,
|
|
286
|
+
operator,
|
|
277
287
|
previousOwner: owner,
|
|
278
288
|
reason: steal && live ? 'stolen' : 'reclaimed',
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Write the operator to a ticket's assignees, then re-read to confirm the
|
|
294
|
+
* claim actually stuck before reporting success.
|
|
295
|
+
*
|
|
296
|
+
* The assignee write is not atomic — GitHub offers no compare-and-set on the
|
|
297
|
+
* assignees surface — so two runs that both observed the ticket unassigned (or
|
|
298
|
+
* a stale foreign claim) will both PATCH themselves in. Without a check the
|
|
299
|
+
* loser of that race returns `acquired: true` and marches into the worktree
|
|
300
|
+
* the winner is already building. The verify closes that window: it re-reads
|
|
301
|
+
* with `fresh: true` (bypassing any provider cache so it sees the other run's
|
|
302
|
+
* write, not our own), and if a foreign login is present it concedes — removes
|
|
303
|
+
* the operator from the assignee set so no phantom co-owner lingers, and
|
|
304
|
+
* returns `acquired: false` / `reason: 'lost-race'` so the fail-closed caller
|
|
305
|
+
* refuses. A clean read (assignees exactly `[operator]`) confirms the claim.
|
|
306
|
+
*
|
|
307
|
+
* It does not eliminate the race — two writes still happen — but it makes the
|
|
308
|
+
* outcome deterministic: exactly one operator survives as the sole assignee,
|
|
309
|
+
* and the other is told it lost.
|
|
310
|
+
*
|
|
311
|
+
* @param {object} args
|
|
312
|
+
* @param {object} args.provider Ticketing provider.
|
|
313
|
+
* @param {number} args.ticketId Ticket being claimed.
|
|
314
|
+
* @param {string} args.operator Operator acquiring the lease.
|
|
315
|
+
* @param {string|null} args.previousOwner Owner before this write (for the result).
|
|
316
|
+
* @param {string} args.reason Success reason when the claim holds.
|
|
317
|
+
* @returns {Promise<{ acquired: boolean, owner: string, previousOwner: string|null, reason: string }>}
|
|
318
|
+
*/
|
|
319
|
+
async function claimAndVerify({
|
|
320
|
+
provider,
|
|
321
|
+
ticketId,
|
|
322
|
+
operator,
|
|
323
|
+
previousOwner,
|
|
324
|
+
reason,
|
|
325
|
+
}) {
|
|
326
|
+
await provider.updateTicket(ticketId, { assignees: [operator] });
|
|
327
|
+
|
|
328
|
+
const after = await provider.getTicket(ticketId, { fresh: true });
|
|
329
|
+
const assignees = Array.isArray(after?.assignees) ? after.assignees : [];
|
|
330
|
+
const foreign = assignees.filter((login) => login !== operator);
|
|
331
|
+
|
|
332
|
+
if (foreign.length === 0) {
|
|
333
|
+
return { acquired: true, owner: operator, previousOwner, reason };
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
// A foreign login co-assigned after our write — we lost a simultaneous
|
|
337
|
+
// claim. Back ourselves out so the winner is the sole assignee, and report
|
|
338
|
+
// the loss so the fail-closed caller refuses rather than double-delivering.
|
|
339
|
+
await provider
|
|
340
|
+
.updateTicket(ticketId, { assignees: foreign })
|
|
341
|
+
.catch(() => undefined);
|
|
342
|
+
return {
|
|
343
|
+
acquired: false,
|
|
344
|
+
owner: foreign[0],
|
|
345
|
+
previousOwner,
|
|
346
|
+
reason: 'lost-race',
|
|
279
347
|
};
|
|
280
348
|
}
|
|
281
349
|
|
|
@@ -38,6 +38,7 @@ import {
|
|
|
38
38
|
renderTransitionMessage,
|
|
39
39
|
} from '../../notifications/notifier.js';
|
|
40
40
|
import {
|
|
41
|
+
emitBlockRecoveredFriction,
|
|
41
42
|
emitRuntimeFriction,
|
|
42
43
|
RUNTIME_FRICTION_CATEGORIES,
|
|
43
44
|
} from '../../observability/runtime-friction.js';
|
|
@@ -122,20 +123,41 @@ function validateTransitionInputs(newState) {
|
|
|
122
123
|
return newState;
|
|
123
124
|
}
|
|
124
125
|
|
|
126
|
+
/**
|
|
127
|
+
* Active states a `agent::blocked` Story can recover into (Story #4622). A
|
|
128
|
+
* `blocked → {executing|ready}` transition is a self-resolved block; every
|
|
129
|
+
* other target (`done`, `closing`) is a real terminal outcome, not a
|
|
130
|
+
* recovery.
|
|
131
|
+
*/
|
|
132
|
+
const BLOCK_RECOVERY_TARGETS = [STATE_LABELS.EXECUTING, STATE_LABELS.READY];
|
|
133
|
+
|
|
125
134
|
/**
|
|
126
135
|
* Resolve the pre-transition ticket snapshot that drives the notify
|
|
127
136
|
* payload and the provider's label-merge path. Honors the caller-supplied
|
|
128
137
|
* `opts.ticketSnapshot` (Story #1795) when present; otherwise issues a
|
|
129
138
|
* best-effort `getTicket` and returns `null` on transient failure.
|
|
130
139
|
*
|
|
140
|
+
* The snapshot is loaded when a caller threads `notify` (its `fromState`
|
|
141
|
+
* feeds the notification payload) OR when `needFromState` is set — Story
|
|
142
|
+
* #4622's recovery detection needs the *prior* state, and `getTicket` after
|
|
143
|
+
* `updateTicket` would already read the new label. Bounding the extra read
|
|
144
|
+
* to recovery-target transitions keeps every other flip on the snapshot-free
|
|
145
|
+
* fast path.
|
|
146
|
+
*
|
|
131
147
|
* @param {object} provider
|
|
132
148
|
* @param {{ notify?: Function, ticketSnapshot?: object|null }} opts
|
|
133
149
|
* @param {number} ticketId
|
|
150
|
+
* @param {boolean} [needFromState]
|
|
134
151
|
* @returns {Promise<object|null>}
|
|
135
152
|
*/
|
|
136
|
-
async function loadTicketSnapshot(provider, opts, ticketId) {
|
|
153
|
+
async function loadTicketSnapshot(provider, opts, ticketId, needFromState) {
|
|
137
154
|
if (opts.ticketSnapshot) return opts.ticketSnapshot;
|
|
138
|
-
if (
|
|
155
|
+
if (
|
|
156
|
+
(!opts.notify && !needFromState) ||
|
|
157
|
+
typeof provider.getTicket !== 'function'
|
|
158
|
+
) {
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
139
161
|
try {
|
|
140
162
|
return await provider.getTicket(ticketId);
|
|
141
163
|
} catch (err) {
|
|
@@ -301,26 +323,50 @@ function dispatchTransitionNotification(args) {
|
|
|
301
323
|
* (see `frictionForTerminal`): the two would otherwise count one incident
|
|
302
324
|
* twice.
|
|
303
325
|
*
|
|
304
|
-
*
|
|
305
|
-
*
|
|
326
|
+
* Story #4622 extends the hook to the inverse edge: a `blocked → active`
|
|
327
|
+
* transition emits a recovery marker so a transient block that self-resolved
|
|
328
|
+
* can be netted out of the retro's `story-blocked` recurrence total.
|
|
329
|
+
*
|
|
330
|
+
* Best-effort and awaited: the friction emitters swallow their own failures
|
|
331
|
+
* and resolve `false`, so this can neither throw nor block the transition.
|
|
306
332
|
* It is awaited rather than fire-and-forget because CLI entry points exit
|
|
307
333
|
* via `process.exit` as soon as `main` resolves (`cli-utils.runAsCli` with
|
|
308
334
|
* `propagateExitCode`), which would discard a still-pending append.
|
|
309
335
|
*
|
|
310
336
|
* @param {number} ticketId
|
|
337
|
+
* @param {string|null} fromState Prior state label, or null.
|
|
311
338
|
* @param {string} newState
|
|
312
339
|
* @param {{ config?: object }} opts
|
|
313
340
|
* @returns {Promise<void>}
|
|
314
341
|
*/
|
|
315
|
-
async function emitBlockedFriction(ticketId, newState, opts) {
|
|
316
|
-
if (newState
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
342
|
+
async function emitBlockedFriction(ticketId, fromState, newState, opts) {
|
|
343
|
+
if (newState === STATE_LABELS.BLOCKED) {
|
|
344
|
+
await emitRuntimeFriction({
|
|
345
|
+
storyId: ticketId,
|
|
346
|
+
category: RUNTIME_FRICTION_CATEGORIES.STORY_BLOCKED,
|
|
347
|
+
tool: 'transitionTicketState',
|
|
348
|
+
details: { toState: newState },
|
|
349
|
+
config: opts?.config,
|
|
350
|
+
});
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
// Story #4622 — a transition *out* of `agent::blocked` into an active state
|
|
354
|
+
// is a recovery: the earlier block self-resolved. Emit its recovery marker
|
|
355
|
+
// so the retro composer can net the transient block out of the
|
|
356
|
+
// `story-blocked` recurrence total (swarm-os friction #581). Only a genuine
|
|
357
|
+
// block→active recovery qualifies; blocked→done/closing is a real
|
|
358
|
+
// terminal outcome, not a recovery, so it is left counted.
|
|
359
|
+
if (
|
|
360
|
+
fromState === STATE_LABELS.BLOCKED &&
|
|
361
|
+
BLOCK_RECOVERY_TARGETS.includes(newState)
|
|
362
|
+
) {
|
|
363
|
+
await emitBlockRecoveredFriction({
|
|
364
|
+
storyId: ticketId,
|
|
365
|
+
fromState,
|
|
366
|
+
toState: newState,
|
|
367
|
+
config: opts?.config,
|
|
368
|
+
});
|
|
369
|
+
}
|
|
324
370
|
}
|
|
325
371
|
|
|
326
372
|
/**
|
|
@@ -382,7 +428,12 @@ export async function transitionTicketState(
|
|
|
382
428
|
// snapshot is also forwarded to `provider.updateTicket` so the label
|
|
383
429
|
// merge path skips its own `getTicket` call (the second of the two
|
|
384
430
|
// round-trips this seam eliminates).
|
|
385
|
-
const ticketSnapshot = await loadTicketSnapshot(
|
|
431
|
+
const ticketSnapshot = await loadTicketSnapshot(
|
|
432
|
+
provider,
|
|
433
|
+
opts,
|
|
434
|
+
ticketId,
|
|
435
|
+
BLOCK_RECOVERY_TARGETS.includes(newState),
|
|
436
|
+
);
|
|
386
437
|
const fromState =
|
|
387
438
|
ticketSnapshot?.labels?.find((l) => ALL_STATES.includes(l)) ?? null;
|
|
388
439
|
|
|
@@ -406,8 +457,9 @@ export async function transitionTicketState(
|
|
|
406
457
|
});
|
|
407
458
|
|
|
408
459
|
// Story #4578 — derive a friction signal from the block, at the point the
|
|
409
|
-
// runtime already knows.
|
|
410
|
-
|
|
460
|
+
// runtime already knows. Story #4622 also emits the recovery marker on the
|
|
461
|
+
// inverse block→active transition. Best-effort; never blocks the transition.
|
|
462
|
+
await emitBlockedFriction(ticketId, fromState, newState, opts);
|
|
411
463
|
|
|
412
464
|
// Story #2548 — mirror the new state onto the Projects v2 Status
|
|
413
465
|
// column. Best-effort; never blocks the transition.
|
|
@@ -167,3 +167,76 @@ function buildAcquired(lockPath, ownerId, fsImpl) {
|
|
|
167
167
|
}
|
|
168
168
|
return { acquired: true, release, ownerId };
|
|
169
169
|
}
|
|
170
|
+
|
|
171
|
+
const DEFAULT_WAIT_MS = 8_000;
|
|
172
|
+
const DEFAULT_POLL_MS = 150;
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Promise-based delay. Injectable so tests can drive the wait loop on a fake
|
|
176
|
+
* clock without a real timer.
|
|
177
|
+
*
|
|
178
|
+
* @param {number} ms
|
|
179
|
+
* @returns {Promise<void>}
|
|
180
|
+
*/
|
|
181
|
+
function defaultSleep(ms) {
|
|
182
|
+
return new Promise((resolve) => {
|
|
183
|
+
setTimeout(resolve, ms);
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Bounded-wait wrapper over {@link acquireSweepLock}.
|
|
189
|
+
*
|
|
190
|
+
* `acquireSweepLock` is single-attempt on purpose: a *skipped* sweep is
|
|
191
|
+
* harmless, so the sweep caller proceeds immediately on contention. The
|
|
192
|
+
* post-land tail is the opposite case — proceeding immediately IS the race
|
|
193
|
+
* two concurrent closes hit on a shared main checkout — so this wrapper
|
|
194
|
+
* polls the primitive with short backoff up to `waitMs` before giving up.
|
|
195
|
+
*
|
|
196
|
+
* It is still **never load-bearing**: on `waitMs` exhaustion it returns
|
|
197
|
+
* `{ acquired: false, reason: 'contended-after-wait' }` and the caller is
|
|
198
|
+
* expected to proceed anyway. The bounded wait is a best-effort collision
|
|
199
|
+
* damper, not a mutual-exclusion guarantee. A hard I/O error short-circuits
|
|
200
|
+
* the loop (spinning would just re-hit it).
|
|
201
|
+
*
|
|
202
|
+
* @param {object} opts
|
|
203
|
+
* @param {string} opts.lockPath
|
|
204
|
+
* @param {number} [opts.waitMs] Max total time to wait for the lock.
|
|
205
|
+
* @param {number} [opts.pollMs] Delay between acquire attempts.
|
|
206
|
+
* @param {number} [opts.timeoutMs] Stale-lock expiry, forwarded to the
|
|
207
|
+
* underlying acquire.
|
|
208
|
+
* @param {string} [opts.ownerId]
|
|
209
|
+
* @param {() => number} [opts.nowFn]
|
|
210
|
+
* @param {(ms: number) => Promise<void>} [opts.sleepFn]
|
|
211
|
+
* @param {object} [opts.fsImpl]
|
|
212
|
+
* @returns {Promise<{ acquired: true, release: () => void, ownerId: string }
|
|
213
|
+
* | { acquired: false, reason: 'contended-after-wait' | 'error', detail?: string }>}
|
|
214
|
+
*/
|
|
215
|
+
export async function acquireLockWithWait({
|
|
216
|
+
lockPath,
|
|
217
|
+
waitMs = DEFAULT_WAIT_MS,
|
|
218
|
+
pollMs = DEFAULT_POLL_MS,
|
|
219
|
+
timeoutMs = DEFAULT_TIMEOUT_MS,
|
|
220
|
+
ownerId,
|
|
221
|
+
nowFn = Date.now,
|
|
222
|
+
sleepFn = defaultSleep,
|
|
223
|
+
fsImpl = fs,
|
|
224
|
+
} = {}) {
|
|
225
|
+
const deadline = nowFn() + Math.max(0, waitMs);
|
|
226
|
+
for (;;) {
|
|
227
|
+
const res = acquireSweepLock({
|
|
228
|
+
lockPath,
|
|
229
|
+
timeoutMs,
|
|
230
|
+
ownerId,
|
|
231
|
+
nowFn,
|
|
232
|
+
fsImpl,
|
|
233
|
+
});
|
|
234
|
+
if (res.acquired) return res;
|
|
235
|
+
// A hard error will not resolve by retrying — surface it immediately.
|
|
236
|
+
if (res.reason === 'error') return res;
|
|
237
|
+
if (nowFn() >= deadline) {
|
|
238
|
+
return { acquired: false, reason: 'contended-after-wait' };
|
|
239
|
+
}
|
|
240
|
+
await sleepFn(Math.max(0, pollMs));
|
|
241
|
+
}
|
|
242
|
+
}
|