@bonesofspring/ai-rules 0.2.68 → 0.2.70

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.
Files changed (115) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/README.md +1 -1
  3. package/package.json +2 -2
  4. package/presets/_shared/hooks/chain-team-phases.sh.tmpl +137 -2
  5. package/presets/claude/android-kotlin/hooks/chain-team-phases.sh +137 -2
  6. package/presets/claude/go/hooks/chain-team-phases.sh +137 -2
  7. package/presets/claude/ios-swift/hooks/chain-team-phases.sh +137 -2
  8. package/presets/claude/java/hooks/chain-team-phases.sh +137 -2
  9. package/presets/claude/mcp-ts/hooks/chain-team-phases.sh +137 -2
  10. package/presets/claude/mol/hooks/chain-team-phases.sh +137 -2
  11. package/presets/claude/next/commands/README.md +5 -0
  12. package/presets/claude/next/commands/task-continue.md +17 -6
  13. package/presets/claude/next/commands/ui-start.md +54 -0
  14. package/presets/claude/next/commands/ui-stop.md +43 -0
  15. package/presets/claude/next/hooks/chain-team-phases.sh +137 -2
  16. package/presets/claude/nuxt/hooks/chain-team-phases.sh +137 -2
  17. package/presets/claude/php-hexagonal/hooks/chain-team-phases.sh +137 -2
  18. package/presets/claude/php-laravel/hooks/chain-team-phases.sh +137 -2
  19. package/presets/claude/svelte/hooks/chain-team-phases.sh +137 -2
  20. package/presets/cursor/android-kotlin/hooks/chain-team-phases.sh +137 -2
  21. package/presets/cursor/go/hooks/chain-team-phases.sh +137 -2
  22. package/presets/cursor/ios-swift/hooks/chain-team-phases.sh +137 -2
  23. package/presets/cursor/java/hooks/chain-team-phases.sh +137 -2
  24. package/presets/cursor/mcp-ts/hooks/chain-team-phases.sh +137 -2
  25. package/presets/cursor/mol/hooks/chain-team-phases.sh +137 -2
  26. package/presets/cursor/next/commands/README.md +5 -0
  27. package/presets/cursor/next/commands/task-continue.md +17 -6
  28. package/presets/cursor/next/commands/ui-start.md +54 -0
  29. package/presets/cursor/next/commands/ui-stop.md +43 -0
  30. package/presets/cursor/next/hooks/chain-team-phases.sh +137 -2
  31. package/presets/cursor/nuxt/hooks/chain-team-phases.sh +137 -2
  32. package/presets/cursor/php-hexagonal/hooks/chain-team-phases.sh +137 -2
  33. package/presets/cursor/php-laravel/hooks/chain-team-phases.sh +137 -2
  34. package/presets/cursor/svelte/hooks/chain-team-phases.sh +137 -2
  35. package/presets/mavis/android-kotlin/adapter-map.json +1 -1
  36. package/presets/mavis/android-kotlin/hooks/chain-team-phases.sh +137 -2
  37. package/presets/mavis/go/adapter-map.json +1 -1
  38. package/presets/mavis/go/hooks/chain-team-phases.sh +137 -2
  39. package/presets/mavis/ios-swift/adapter-map.json +1 -1
  40. package/presets/mavis/ios-swift/hooks/chain-team-phases.sh +137 -2
  41. package/presets/mavis/java/adapter-map.json +1 -1
  42. package/presets/mavis/java/hooks/chain-team-phases.sh +137 -2
  43. package/presets/mavis/mcp-ts/adapter-map.json +1 -1
  44. package/presets/mavis/mcp-ts/hooks/chain-team-phases.sh +137 -2
  45. package/presets/mavis/mol/adapter-map.json +1 -1
  46. package/presets/mavis/mol/hooks/chain-team-phases.sh +137 -2
  47. package/presets/mavis/next/adapter-map.json +17 -1
  48. package/presets/mavis/next/commands/README.md +5 -0
  49. package/presets/mavis/next/commands/task-continue.md +16 -5
  50. package/presets/mavis/next/commands/ui-start.md +49 -0
  51. package/presets/mavis/next/commands/ui-stop.md +38 -0
  52. package/presets/mavis/next/hooks/chain-team-phases.sh +137 -2
  53. package/presets/mavis/nuxt/adapter-map.json +1 -1
  54. package/presets/mavis/nuxt/hooks/chain-team-phases.sh +137 -2
  55. package/presets/mavis/php-hexagonal/adapter-map.json +1 -1
  56. package/presets/mavis/php-hexagonal/hooks/chain-team-phases.sh +137 -2
  57. package/presets/mavis/php-laravel/adapter-map.json +1 -1
  58. package/presets/mavis/php-laravel/hooks/chain-team-phases.sh +137 -2
  59. package/presets/mavis/svelte/adapter-map.json +1 -1
  60. package/presets/mavis/svelte/hooks/chain-team-phases.sh +137 -2
  61. package/presets/opencode/android-kotlin/adapter-map.json +1 -1
  62. package/presets/opencode/android-kotlin/hooks/chain-team-phases.sh +137 -2
  63. package/presets/opencode/go/adapter-map.json +1 -1
  64. package/presets/opencode/go/hooks/chain-team-phases.sh +137 -2
  65. package/presets/opencode/ios-swift/adapter-map.json +1 -1
  66. package/presets/opencode/ios-swift/hooks/chain-team-phases.sh +137 -2
  67. package/presets/opencode/java/adapter-map.json +1 -1
  68. package/presets/opencode/java/hooks/chain-team-phases.sh +137 -2
  69. package/presets/opencode/mcp-ts/adapter-map.json +1 -1
  70. package/presets/opencode/mcp-ts/hooks/chain-team-phases.sh +137 -2
  71. package/presets/opencode/mol/adapter-map.json +1 -1
  72. package/presets/opencode/mol/hooks/chain-team-phases.sh +137 -2
  73. package/presets/opencode/next/adapter-map.json +17 -1
  74. package/presets/opencode/next/commands/README.md +5 -0
  75. package/presets/opencode/next/commands/task-continue.md +17 -6
  76. package/presets/opencode/next/commands/ui-start.md +57 -0
  77. package/presets/opencode/next/commands/ui-stop.md +46 -0
  78. package/presets/opencode/next/hooks/chain-team-phases.sh +137 -2
  79. package/presets/opencode/nuxt/adapter-map.json +1 -1
  80. package/presets/opencode/nuxt/hooks/chain-team-phases.sh +137 -2
  81. package/presets/opencode/php-hexagonal/adapter-map.json +1 -1
  82. package/presets/opencode/php-hexagonal/hooks/chain-team-phases.sh +137 -2
  83. package/presets/opencode/php-laravel/adapter-map.json +1 -1
  84. package/presets/opencode/php-laravel/hooks/chain-team-phases.sh +137 -2
  85. package/presets/opencode/plugins/output-guard/README.md +33 -0
  86. package/presets/opencode/plugins/output-guard/index.ts +225 -3
  87. package/presets/opencode/svelte/adapter-map.json +1 -1
  88. package/presets/opencode/svelte/hooks/chain-team-phases.sh +137 -2
  89. package/presets/zcode/android-kotlin/adapter-map.json +1 -1
  90. package/presets/zcode/android-kotlin/hooks/chain-team-phases.sh +137 -2
  91. package/presets/zcode/go/adapter-map.json +1 -1
  92. package/presets/zcode/go/hooks/chain-team-phases.sh +137 -2
  93. package/presets/zcode/ios-swift/adapter-map.json +1 -1
  94. package/presets/zcode/ios-swift/hooks/chain-team-phases.sh +137 -2
  95. package/presets/zcode/java/adapter-map.json +1 -1
  96. package/presets/zcode/java/hooks/chain-team-phases.sh +137 -2
  97. package/presets/zcode/mcp-ts/adapter-map.json +1 -1
  98. package/presets/zcode/mcp-ts/hooks/chain-team-phases.sh +137 -2
  99. package/presets/zcode/mol/adapter-map.json +1 -1
  100. package/presets/zcode/mol/hooks/chain-team-phases.sh +137 -2
  101. package/presets/zcode/next/adapter-map.json +17 -1
  102. package/presets/zcode/next/commands/README.md +5 -0
  103. package/presets/zcode/next/commands/task-continue.md +17 -6
  104. package/presets/zcode/next/commands/ui-start.md +57 -0
  105. package/presets/zcode/next/commands/ui-stop.md +46 -0
  106. package/presets/zcode/next/hooks/chain-team-phases.sh +137 -2
  107. package/presets/zcode/nuxt/adapter-map.json +1 -1
  108. package/presets/zcode/nuxt/hooks/chain-team-phases.sh +137 -2
  109. package/presets/zcode/php-hexagonal/adapter-map.json +1 -1
  110. package/presets/zcode/php-hexagonal/hooks/chain-team-phases.sh +137 -2
  111. package/presets/zcode/php-laravel/adapter-map.json +1 -1
  112. package/presets/zcode/php-laravel/hooks/chain-team-phases.sh +137 -2
  113. package/presets/zcode/svelte/adapter-map.json +1 -1
  114. package/presets/zcode/svelte/hooks/chain-team-phases.sh +137 -2
  115. package/scripts/report-context-cost.mjs +216 -5
@@ -41,8 +41,12 @@ if (hookInput.hook_event_name === 'SubagentStop') {
41
41
  }
42
42
 
43
43
  if (hookInput.status !== 'completed') {
44
- process.stdout.write('{}');
45
- process.exit(0);
44
+ // `resume_blocked` is a lifecycle repair, not a producer receipt: it runs
45
+ // with no `status`, and a `status` it carries is ignored.
46
+ if (hookInput.action !== 'resume_blocked') {
47
+ process.stdout.write('{}');
48
+ process.exit(0);
49
+ }
46
50
  }
47
51
 
48
52
  // Slug resolution, strongest first:
@@ -2113,6 +2117,137 @@ function syncMetricsProfile(pipeline) {
2113
2117
  });
2114
2118
  }
2115
2119
 
2120
+ function resumeBlockedRefuse(reason) {
2121
+ process.stdout.write(JSON.stringify({ ok: false, action: 'resume_blocked', reason }));
2122
+ }
2123
+
2124
+ /**
2125
+ * Arm attempt N+1 for one linear step. The ordinal read and the append share
2126
+ * one metrics critical section, like `completeTaskMetrics`: a second process
2127
+ * serializes on the lock and sees the first attempt instead of minting the
2128
+ * same ordinal.
2129
+ */
2130
+ function armResumedAttempt(step, stepIndex, agent) {
2131
+ let attemptId = null;
2132
+ mutateMetrics((metrics) => {
2133
+ const ledgerRunId = metrics.task && typeof metrics.task.runId === 'string' ? metrics.task.runId : '';
2134
+ const runId = ledgerRunId || status.metricsRunId || `${slug}-${Date.now()}-${Math.random().toString(16).slice(2)}`;
2135
+ metrics.task = { ...(metrics.task || {}), runId };
2136
+ status.metricsRunId = runId;
2137
+ const events = Array.isArray(metrics.events) ? metrics.events : (metrics.events = []);
2138
+ const attempt =
2139
+ events.reduce(
2140
+ (highest, event) =>
2141
+ event && event.type === 'attempt_started' && event.stepIndex === stepIndex && event.agent === agent
2142
+ ? Math.max(highest, Number(event.attempt) || 0)
2143
+ : highest,
2144
+ 0,
2145
+ ) + 1;
2146
+ attemptId = `${runId}:step-${stepIndex}:${agent}:attempt-${attempt}`;
2147
+ status.activeAttempts = {
2148
+ ...(status.activeAttempts || {}),
2149
+ [attemptId]: { agent, stepIndex, attempt, startedAt: now, parallelGroupId: null, waveId: null },
2150
+ };
2151
+ events.push({
2152
+ eventId: `${runId}:${Date.now()}:${Math.random().toString(16).slice(2)}`,
2153
+ type: 'attempt_started',
2154
+ at: now,
2155
+ attemptId,
2156
+ stepIndex,
2157
+ stepLabel: step.label || null,
2158
+ agent,
2159
+ role: roleFor(agent),
2160
+ attempt,
2161
+ parallelGroupId: null,
2162
+ waveId: null,
2163
+ source: 'hook',
2164
+ attributionConfidence: 'high',
2165
+ });
2166
+ });
2167
+ return attemptId;
2168
+ }
2169
+
2170
+ // Re-arm the step a producer left `blocked`. It arms the attempt only: a stop
2171
+ // that finds `awaitingHumanGate: true` returns before the stop tail, so an open
2172
+ // gate would swallow the retried receipt. Gate/advance stays receipt-driven.
2173
+ if (hookInput.action === 'resume_blocked') {
2174
+ if (pauseBlocksAdvance()) process.exit(0);
2175
+ const pipeline = readPipeline();
2176
+ if (!pipeline || !Array.isArray(pipeline.steps) || pipeline.steps.length === 0) {
2177
+ resumeBlockedRefuse(
2178
+ `pipeline.json is missing, unparseable, or has no steps for slug "${slug}"; no attempt was armed and status.json is unchanged.`,
2179
+ );
2180
+ process.exit(0);
2181
+ }
2182
+ const stepIndex = typeof status.pipelineIndex === 'number' ? status.pipelineIndex : 0;
2183
+ const step = pipeline.steps[stepIndex];
2184
+ const blockedSteps = Array.isArray(status.blockedSteps) ? status.blockedSteps : [];
2185
+ if (blockedSteps.length > 0 || pipeline.steps.some((entry) => entry && Array.isArray(entry.dependsOn))) {
2186
+ resumeBlockedRefuse(
2187
+ `graph mode is not supported by resume_blocked for slug "${slug}" (status.blockedSteps or a step.dependsOn): nothing was armed; resume the wave by hand.`,
2188
+ );
2189
+ process.exit(0);
2190
+ }
2191
+ const agents = step ? getStepAgents(step).filter((agent) => typeof agent === 'string' && agent.length > 0) : [];
2192
+ if (agents.length === 0) {
2193
+ resumeBlockedRefuse(
2194
+ `pipeline.json has no executable step at pipelineIndex ${stepIndex} for slug "${slug}"; nothing was armed.`,
2195
+ );
2196
+ process.exit(0);
2197
+ }
2198
+ // A parallel wave is not a single linear step: re-arming it would re-run the
2199
+ // agents that already succeeded. Refuse the shape instead of arming the wave.
2200
+ if (agents.length > 1) {
2201
+ resumeBlockedRefuse(
2202
+ `step ${stepIndex} is a parallel wave for slug "${slug}" (${agents.join(', ')}); resume_blocked arms one linear step, so nothing was armed.`,
2203
+ );
2204
+ process.exit(0);
2205
+ }
2206
+ if (status.state !== 'blocked') {
2207
+ resumeBlockedRefuse(
2208
+ `state "${status.state || 'unknown'}" is not blocked for slug "${slug}"; resume_blocked only re-arms the step a producer left blocked.`,
2209
+ );
2210
+ process.exit(0);
2211
+ }
2212
+ if (status.currentAgent === 'mockup-reviewer') {
2213
+ resumeBlockedRefuse(
2214
+ `the mockup-reviewer BLOCKED_MCP skip-forward owns this state for slug "${slug}": run {"action":"end_human_gate","activateNext":true} instead; resume_blocked armed nothing.`,
2215
+ );
2216
+ process.exit(0);
2217
+ }
2218
+ const activeForStep = Object.values(status.activeAttempts || {}).filter(
2219
+ (entry) => entry && entry.stepIndex === stepIndex,
2220
+ );
2221
+ if (activeForStep.length > 0) {
2222
+ resumeBlockedRefuse(
2223
+ `step ${stepIndex} already has an active attempt for slug "${slug}" (${activeForStep.map((entry) => entry.agent).join(', ')}); resume_blocked changed nothing.`,
2224
+ );
2225
+ process.exit(0);
2226
+ }
2227
+ const attemptId = armResumedAttempt(step, stepIndex, agents[0]);
2228
+ // `currentAgent` names the blocked producer; a pipeline blocked without one
2229
+ // (missing consumer artifacts) falls back to the step's agent.
2230
+ const resumedAgent = status.currentAgent || agents[0];
2231
+ writeStatus({
2232
+ pipelineIndex: stepIndex,
2233
+ currentAgent: resumedAgent,
2234
+ phase: 'executing',
2235
+ state: 'in_progress',
2236
+ awaitingHumanGate: false,
2237
+ });
2238
+ process.stdout.write(
2239
+ JSON.stringify({
2240
+ ok: true,
2241
+ action: 'resume_blocked',
2242
+ pipelineIndex: stepIndex,
2243
+ agents,
2244
+ attemptIds: [attemptId],
2245
+ followup_message: handoffForAgent(resumedAgent, step.scope, step),
2246
+ }),
2247
+ );
2248
+ process.exit(0);
2249
+ }
2250
+
2116
2251
  if (hookInput.action === 'bootstrap') {
2117
2252
  if (pauseBlocksAdvance()) process.exit(0);
2118
2253
  const pipeline = readPipeline();
@@ -41,8 +41,12 @@ if (hookInput.hook_event_name === 'SubagentStop') {
41
41
  }
42
42
 
43
43
  if (hookInput.status !== 'completed') {
44
- process.stdout.write('{}');
45
- process.exit(0);
44
+ // `resume_blocked` is a lifecycle repair, not a producer receipt: it runs
45
+ // with no `status`, and a `status` it carries is ignored.
46
+ if (hookInput.action !== 'resume_blocked') {
47
+ process.stdout.write('{}');
48
+ process.exit(0);
49
+ }
46
50
  }
47
51
 
48
52
  // Slug resolution, strongest first:
@@ -2113,6 +2117,137 @@ function syncMetricsProfile(pipeline) {
2113
2117
  });
2114
2118
  }
2115
2119
 
2120
+ function resumeBlockedRefuse(reason) {
2121
+ process.stdout.write(JSON.stringify({ ok: false, action: 'resume_blocked', reason }));
2122
+ }
2123
+
2124
+ /**
2125
+ * Arm attempt N+1 for one linear step. The ordinal read and the append share
2126
+ * one metrics critical section, like `completeTaskMetrics`: a second process
2127
+ * serializes on the lock and sees the first attempt instead of minting the
2128
+ * same ordinal.
2129
+ */
2130
+ function armResumedAttempt(step, stepIndex, agent) {
2131
+ let attemptId = null;
2132
+ mutateMetrics((metrics) => {
2133
+ const ledgerRunId = metrics.task && typeof metrics.task.runId === 'string' ? metrics.task.runId : '';
2134
+ const runId = ledgerRunId || status.metricsRunId || `${slug}-${Date.now()}-${Math.random().toString(16).slice(2)}`;
2135
+ metrics.task = { ...(metrics.task || {}), runId };
2136
+ status.metricsRunId = runId;
2137
+ const events = Array.isArray(metrics.events) ? metrics.events : (metrics.events = []);
2138
+ const attempt =
2139
+ events.reduce(
2140
+ (highest, event) =>
2141
+ event && event.type === 'attempt_started' && event.stepIndex === stepIndex && event.agent === agent
2142
+ ? Math.max(highest, Number(event.attempt) || 0)
2143
+ : highest,
2144
+ 0,
2145
+ ) + 1;
2146
+ attemptId = `${runId}:step-${stepIndex}:${agent}:attempt-${attempt}`;
2147
+ status.activeAttempts = {
2148
+ ...(status.activeAttempts || {}),
2149
+ [attemptId]: { agent, stepIndex, attempt, startedAt: now, parallelGroupId: null, waveId: null },
2150
+ };
2151
+ events.push({
2152
+ eventId: `${runId}:${Date.now()}:${Math.random().toString(16).slice(2)}`,
2153
+ type: 'attempt_started',
2154
+ at: now,
2155
+ attemptId,
2156
+ stepIndex,
2157
+ stepLabel: step.label || null,
2158
+ agent,
2159
+ role: roleFor(agent),
2160
+ attempt,
2161
+ parallelGroupId: null,
2162
+ waveId: null,
2163
+ source: 'hook',
2164
+ attributionConfidence: 'high',
2165
+ });
2166
+ });
2167
+ return attemptId;
2168
+ }
2169
+
2170
+ // Re-arm the step a producer left `blocked`. It arms the attempt only: a stop
2171
+ // that finds `awaitingHumanGate: true` returns before the stop tail, so an open
2172
+ // gate would swallow the retried receipt. Gate/advance stays receipt-driven.
2173
+ if (hookInput.action === 'resume_blocked') {
2174
+ if (pauseBlocksAdvance()) process.exit(0);
2175
+ const pipeline = readPipeline();
2176
+ if (!pipeline || !Array.isArray(pipeline.steps) || pipeline.steps.length === 0) {
2177
+ resumeBlockedRefuse(
2178
+ `pipeline.json is missing, unparseable, or has no steps for slug "${slug}"; no attempt was armed and status.json is unchanged.`,
2179
+ );
2180
+ process.exit(0);
2181
+ }
2182
+ const stepIndex = typeof status.pipelineIndex === 'number' ? status.pipelineIndex : 0;
2183
+ const step = pipeline.steps[stepIndex];
2184
+ const blockedSteps = Array.isArray(status.blockedSteps) ? status.blockedSteps : [];
2185
+ if (blockedSteps.length > 0 || pipeline.steps.some((entry) => entry && Array.isArray(entry.dependsOn))) {
2186
+ resumeBlockedRefuse(
2187
+ `graph mode is not supported by resume_blocked for slug "${slug}" (status.blockedSteps or a step.dependsOn): nothing was armed; resume the wave by hand.`,
2188
+ );
2189
+ process.exit(0);
2190
+ }
2191
+ const agents = step ? getStepAgents(step).filter((agent) => typeof agent === 'string' && agent.length > 0) : [];
2192
+ if (agents.length === 0) {
2193
+ resumeBlockedRefuse(
2194
+ `pipeline.json has no executable step at pipelineIndex ${stepIndex} for slug "${slug}"; nothing was armed.`,
2195
+ );
2196
+ process.exit(0);
2197
+ }
2198
+ // A parallel wave is not a single linear step: re-arming it would re-run the
2199
+ // agents that already succeeded. Refuse the shape instead of arming the wave.
2200
+ if (agents.length > 1) {
2201
+ resumeBlockedRefuse(
2202
+ `step ${stepIndex} is a parallel wave for slug "${slug}" (${agents.join(', ')}); resume_blocked arms one linear step, so nothing was armed.`,
2203
+ );
2204
+ process.exit(0);
2205
+ }
2206
+ if (status.state !== 'blocked') {
2207
+ resumeBlockedRefuse(
2208
+ `state "${status.state || 'unknown'}" is not blocked for slug "${slug}"; resume_blocked only re-arms the step a producer left blocked.`,
2209
+ );
2210
+ process.exit(0);
2211
+ }
2212
+ if (status.currentAgent === 'mockup-reviewer') {
2213
+ resumeBlockedRefuse(
2214
+ `the mockup-reviewer BLOCKED_MCP skip-forward owns this state for slug "${slug}": run {"action":"end_human_gate","activateNext":true} instead; resume_blocked armed nothing.`,
2215
+ );
2216
+ process.exit(0);
2217
+ }
2218
+ const activeForStep = Object.values(status.activeAttempts || {}).filter(
2219
+ (entry) => entry && entry.stepIndex === stepIndex,
2220
+ );
2221
+ if (activeForStep.length > 0) {
2222
+ resumeBlockedRefuse(
2223
+ `step ${stepIndex} already has an active attempt for slug "${slug}" (${activeForStep.map((entry) => entry.agent).join(', ')}); resume_blocked changed nothing.`,
2224
+ );
2225
+ process.exit(0);
2226
+ }
2227
+ const attemptId = armResumedAttempt(step, stepIndex, agents[0]);
2228
+ // `currentAgent` names the blocked producer; a pipeline blocked without one
2229
+ // (missing consumer artifacts) falls back to the step's agent.
2230
+ const resumedAgent = status.currentAgent || agents[0];
2231
+ writeStatus({
2232
+ pipelineIndex: stepIndex,
2233
+ currentAgent: resumedAgent,
2234
+ phase: 'executing',
2235
+ state: 'in_progress',
2236
+ awaitingHumanGate: false,
2237
+ });
2238
+ process.stdout.write(
2239
+ JSON.stringify({
2240
+ ok: true,
2241
+ action: 'resume_blocked',
2242
+ pipelineIndex: stepIndex,
2243
+ agents,
2244
+ attemptIds: [attemptId],
2245
+ followup_message: handoffForAgent(resumedAgent, step.scope, step),
2246
+ }),
2247
+ );
2248
+ process.exit(0);
2249
+ }
2250
+
2116
2251
  if (hookInput.action === 'bootstrap') {
2117
2252
  if (pauseBlocksAdvance()) process.exit(0);
2118
2253
  const pipeline = readPipeline();
@@ -43,8 +43,12 @@ if (hookInput.hook_event_name === 'SubagentStop') {
43
43
  }
44
44
 
45
45
  if (hookInput.status !== 'completed') {
46
- process.stdout.write('{}');
47
- process.exit(0);
46
+ // `resume_blocked` is a lifecycle repair, not a producer receipt: it runs
47
+ // with no `status`, and a `status` it carries is ignored.
48
+ if (hookInput.action !== 'resume_blocked') {
49
+ process.stdout.write('{}');
50
+ process.exit(0);
51
+ }
48
52
  }
49
53
 
50
54
  // Slug resolution, strongest first:
@@ -2111,6 +2115,137 @@ function syncMetricsProfile(pipeline) {
2111
2115
  });
2112
2116
  }
2113
2117
 
2118
+ function resumeBlockedRefuse(reason) {
2119
+ process.stdout.write(JSON.stringify({ ok: false, action: 'resume_blocked', reason }));
2120
+ }
2121
+
2122
+ /**
2123
+ * Arm attempt N+1 for one linear step. The ordinal read and the append share
2124
+ * one metrics critical section, like `completeTaskMetrics`: a second process
2125
+ * serializes on the lock and sees the first attempt instead of minting the
2126
+ * same ordinal.
2127
+ */
2128
+ function armResumedAttempt(step, stepIndex, agent) {
2129
+ let attemptId = null;
2130
+ mutateMetrics((metrics) => {
2131
+ const ledgerRunId = metrics.task && typeof metrics.task.runId === 'string' ? metrics.task.runId : '';
2132
+ const runId = ledgerRunId || status.metricsRunId || `${slug}-${Date.now()}-${Math.random().toString(16).slice(2)}`;
2133
+ metrics.task = { ...(metrics.task || {}), runId };
2134
+ status.metricsRunId = runId;
2135
+ const events = Array.isArray(metrics.events) ? metrics.events : (metrics.events = []);
2136
+ const attempt =
2137
+ events.reduce(
2138
+ (highest, event) =>
2139
+ event && event.type === 'attempt_started' && event.stepIndex === stepIndex && event.agent === agent
2140
+ ? Math.max(highest, Number(event.attempt) || 0)
2141
+ : highest,
2142
+ 0,
2143
+ ) + 1;
2144
+ attemptId = `${runId}:step-${stepIndex}:${agent}:attempt-${attempt}`;
2145
+ status.activeAttempts = {
2146
+ ...(status.activeAttempts || {}),
2147
+ [attemptId]: { agent, stepIndex, attempt, startedAt: now, parallelGroupId: null, waveId: null },
2148
+ };
2149
+ events.push({
2150
+ eventId: `${runId}:${Date.now()}:${Math.random().toString(16).slice(2)}`,
2151
+ type: 'attempt_started',
2152
+ at: now,
2153
+ attemptId,
2154
+ stepIndex,
2155
+ stepLabel: step.label || null,
2156
+ agent,
2157
+ role: roleFor(agent),
2158
+ attempt,
2159
+ parallelGroupId: null,
2160
+ waveId: null,
2161
+ source: 'hook',
2162
+ attributionConfidence: 'high',
2163
+ });
2164
+ });
2165
+ return attemptId;
2166
+ }
2167
+
2168
+ // Re-arm the step a producer left `blocked`. It arms the attempt only: a stop
2169
+ // that finds `awaitingHumanGate: true` returns before the stop tail, so an open
2170
+ // gate would swallow the retried receipt. Gate/advance stays receipt-driven.
2171
+ if (hookInput.action === 'resume_blocked') {
2172
+ if (pauseBlocksAdvance()) process.exit(0);
2173
+ const pipeline = readPipeline();
2174
+ if (!pipeline || !Array.isArray(pipeline.steps) || pipeline.steps.length === 0) {
2175
+ resumeBlockedRefuse(
2176
+ `pipeline.json is missing, unparseable, or has no steps for slug "${slug}"; no attempt was armed and status.json is unchanged.`,
2177
+ );
2178
+ process.exit(0);
2179
+ }
2180
+ const stepIndex = typeof status.pipelineIndex === 'number' ? status.pipelineIndex : 0;
2181
+ const step = pipeline.steps[stepIndex];
2182
+ const blockedSteps = Array.isArray(status.blockedSteps) ? status.blockedSteps : [];
2183
+ if (blockedSteps.length > 0 || pipeline.steps.some((entry) => entry && Array.isArray(entry.dependsOn))) {
2184
+ resumeBlockedRefuse(
2185
+ `graph mode is not supported by resume_blocked for slug "${slug}" (status.blockedSteps or a step.dependsOn): nothing was armed; resume the wave by hand.`,
2186
+ );
2187
+ process.exit(0);
2188
+ }
2189
+ const agents = step ? getStepAgents(step).filter((agent) => typeof agent === 'string' && agent.length > 0) : [];
2190
+ if (agents.length === 0) {
2191
+ resumeBlockedRefuse(
2192
+ `pipeline.json has no executable step at pipelineIndex ${stepIndex} for slug "${slug}"; nothing was armed.`,
2193
+ );
2194
+ process.exit(0);
2195
+ }
2196
+ // A parallel wave is not a single linear step: re-arming it would re-run the
2197
+ // agents that already succeeded. Refuse the shape instead of arming the wave.
2198
+ if (agents.length > 1) {
2199
+ resumeBlockedRefuse(
2200
+ `step ${stepIndex} is a parallel wave for slug "${slug}" (${agents.join(', ')}); resume_blocked arms one linear step, so nothing was armed.`,
2201
+ );
2202
+ process.exit(0);
2203
+ }
2204
+ if (status.state !== 'blocked') {
2205
+ resumeBlockedRefuse(
2206
+ `state "${status.state || 'unknown'}" is not blocked for slug "${slug}"; resume_blocked only re-arms the step a producer left blocked.`,
2207
+ );
2208
+ process.exit(0);
2209
+ }
2210
+ if (status.currentAgent === 'mockup-reviewer') {
2211
+ resumeBlockedRefuse(
2212
+ `the mockup-reviewer BLOCKED_MCP skip-forward owns this state for slug "${slug}": run {"action":"end_human_gate","activateNext":true} instead; resume_blocked armed nothing.`,
2213
+ );
2214
+ process.exit(0);
2215
+ }
2216
+ const activeForStep = Object.values(status.activeAttempts || {}).filter(
2217
+ (entry) => entry && entry.stepIndex === stepIndex,
2218
+ );
2219
+ if (activeForStep.length > 0) {
2220
+ resumeBlockedRefuse(
2221
+ `step ${stepIndex} already has an active attempt for slug "${slug}" (${activeForStep.map((entry) => entry.agent).join(', ')}); resume_blocked changed nothing.`,
2222
+ );
2223
+ process.exit(0);
2224
+ }
2225
+ const attemptId = armResumedAttempt(step, stepIndex, agents[0]);
2226
+ // `currentAgent` names the blocked producer; a pipeline blocked without one
2227
+ // (missing consumer artifacts) falls back to the step's agent.
2228
+ const resumedAgent = status.currentAgent || agents[0];
2229
+ writeStatus({
2230
+ pipelineIndex: stepIndex,
2231
+ currentAgent: resumedAgent,
2232
+ phase: 'executing',
2233
+ state: 'in_progress',
2234
+ awaitingHumanGate: false,
2235
+ });
2236
+ process.stdout.write(
2237
+ JSON.stringify({
2238
+ ok: true,
2239
+ action: 'resume_blocked',
2240
+ pipelineIndex: stepIndex,
2241
+ agents,
2242
+ attemptIds: [attemptId],
2243
+ followup_message: handoffForAgent(resumedAgent, step.scope, step),
2244
+ }),
2245
+ );
2246
+ process.exit(0);
2247
+ }
2248
+
2114
2249
  if (hookInput.action === 'bootstrap') {
2115
2250
  if (pauseBlocksAdvance()) process.exit(0);
2116
2251
  const pipeline = readPipeline();
@@ -43,8 +43,12 @@ if (hookInput.hook_event_name === 'SubagentStop') {
43
43
  }
44
44
 
45
45
  if (hookInput.status !== 'completed') {
46
- process.stdout.write('{}');
47
- process.exit(0);
46
+ // `resume_blocked` is a lifecycle repair, not a producer receipt: it runs
47
+ // with no `status`, and a `status` it carries is ignored.
48
+ if (hookInput.action !== 'resume_blocked') {
49
+ process.stdout.write('{}');
50
+ process.exit(0);
51
+ }
48
52
  }
49
53
 
50
54
  // Slug resolution, strongest first:
@@ -2111,6 +2115,137 @@ function syncMetricsProfile(pipeline) {
2111
2115
  });
2112
2116
  }
2113
2117
 
2118
+ function resumeBlockedRefuse(reason) {
2119
+ process.stdout.write(JSON.stringify({ ok: false, action: 'resume_blocked', reason }));
2120
+ }
2121
+
2122
+ /**
2123
+ * Arm attempt N+1 for one linear step. The ordinal read and the append share
2124
+ * one metrics critical section, like `completeTaskMetrics`: a second process
2125
+ * serializes on the lock and sees the first attempt instead of minting the
2126
+ * same ordinal.
2127
+ */
2128
+ function armResumedAttempt(step, stepIndex, agent) {
2129
+ let attemptId = null;
2130
+ mutateMetrics((metrics) => {
2131
+ const ledgerRunId = metrics.task && typeof metrics.task.runId === 'string' ? metrics.task.runId : '';
2132
+ const runId = ledgerRunId || status.metricsRunId || `${slug}-${Date.now()}-${Math.random().toString(16).slice(2)}`;
2133
+ metrics.task = { ...(metrics.task || {}), runId };
2134
+ status.metricsRunId = runId;
2135
+ const events = Array.isArray(metrics.events) ? metrics.events : (metrics.events = []);
2136
+ const attempt =
2137
+ events.reduce(
2138
+ (highest, event) =>
2139
+ event && event.type === 'attempt_started' && event.stepIndex === stepIndex && event.agent === agent
2140
+ ? Math.max(highest, Number(event.attempt) || 0)
2141
+ : highest,
2142
+ 0,
2143
+ ) + 1;
2144
+ attemptId = `${runId}:step-${stepIndex}:${agent}:attempt-${attempt}`;
2145
+ status.activeAttempts = {
2146
+ ...(status.activeAttempts || {}),
2147
+ [attemptId]: { agent, stepIndex, attempt, startedAt: now, parallelGroupId: null, waveId: null },
2148
+ };
2149
+ events.push({
2150
+ eventId: `${runId}:${Date.now()}:${Math.random().toString(16).slice(2)}`,
2151
+ type: 'attempt_started',
2152
+ at: now,
2153
+ attemptId,
2154
+ stepIndex,
2155
+ stepLabel: step.label || null,
2156
+ agent,
2157
+ role: roleFor(agent),
2158
+ attempt,
2159
+ parallelGroupId: null,
2160
+ waveId: null,
2161
+ source: 'hook',
2162
+ attributionConfidence: 'high',
2163
+ });
2164
+ });
2165
+ return attemptId;
2166
+ }
2167
+
2168
+ // Re-arm the step a producer left `blocked`. It arms the attempt only: a stop
2169
+ // that finds `awaitingHumanGate: true` returns before the stop tail, so an open
2170
+ // gate would swallow the retried receipt. Gate/advance stays receipt-driven.
2171
+ if (hookInput.action === 'resume_blocked') {
2172
+ if (pauseBlocksAdvance()) process.exit(0);
2173
+ const pipeline = readPipeline();
2174
+ if (!pipeline || !Array.isArray(pipeline.steps) || pipeline.steps.length === 0) {
2175
+ resumeBlockedRefuse(
2176
+ `pipeline.json is missing, unparseable, or has no steps for slug "${slug}"; no attempt was armed and status.json is unchanged.`,
2177
+ );
2178
+ process.exit(0);
2179
+ }
2180
+ const stepIndex = typeof status.pipelineIndex === 'number' ? status.pipelineIndex : 0;
2181
+ const step = pipeline.steps[stepIndex];
2182
+ const blockedSteps = Array.isArray(status.blockedSteps) ? status.blockedSteps : [];
2183
+ if (blockedSteps.length > 0 || pipeline.steps.some((entry) => entry && Array.isArray(entry.dependsOn))) {
2184
+ resumeBlockedRefuse(
2185
+ `graph mode is not supported by resume_blocked for slug "${slug}" (status.blockedSteps or a step.dependsOn): nothing was armed; resume the wave by hand.`,
2186
+ );
2187
+ process.exit(0);
2188
+ }
2189
+ const agents = step ? getStepAgents(step).filter((agent) => typeof agent === 'string' && agent.length > 0) : [];
2190
+ if (agents.length === 0) {
2191
+ resumeBlockedRefuse(
2192
+ `pipeline.json has no executable step at pipelineIndex ${stepIndex} for slug "${slug}"; nothing was armed.`,
2193
+ );
2194
+ process.exit(0);
2195
+ }
2196
+ // A parallel wave is not a single linear step: re-arming it would re-run the
2197
+ // agents that already succeeded. Refuse the shape instead of arming the wave.
2198
+ if (agents.length > 1) {
2199
+ resumeBlockedRefuse(
2200
+ `step ${stepIndex} is a parallel wave for slug "${slug}" (${agents.join(', ')}); resume_blocked arms one linear step, so nothing was armed.`,
2201
+ );
2202
+ process.exit(0);
2203
+ }
2204
+ if (status.state !== 'blocked') {
2205
+ resumeBlockedRefuse(
2206
+ `state "${status.state || 'unknown'}" is not blocked for slug "${slug}"; resume_blocked only re-arms the step a producer left blocked.`,
2207
+ );
2208
+ process.exit(0);
2209
+ }
2210
+ if (status.currentAgent === 'mockup-reviewer') {
2211
+ resumeBlockedRefuse(
2212
+ `the mockup-reviewer BLOCKED_MCP skip-forward owns this state for slug "${slug}": run {"action":"end_human_gate","activateNext":true} instead; resume_blocked armed nothing.`,
2213
+ );
2214
+ process.exit(0);
2215
+ }
2216
+ const activeForStep = Object.values(status.activeAttempts || {}).filter(
2217
+ (entry) => entry && entry.stepIndex === stepIndex,
2218
+ );
2219
+ if (activeForStep.length > 0) {
2220
+ resumeBlockedRefuse(
2221
+ `step ${stepIndex} already has an active attempt for slug "${slug}" (${activeForStep.map((entry) => entry.agent).join(', ')}); resume_blocked changed nothing.`,
2222
+ );
2223
+ process.exit(0);
2224
+ }
2225
+ const attemptId = armResumedAttempt(step, stepIndex, agents[0]);
2226
+ // `currentAgent` names the blocked producer; a pipeline blocked without one
2227
+ // (missing consumer artifacts) falls back to the step's agent.
2228
+ const resumedAgent = status.currentAgent || agents[0];
2229
+ writeStatus({
2230
+ pipelineIndex: stepIndex,
2231
+ currentAgent: resumedAgent,
2232
+ phase: 'executing',
2233
+ state: 'in_progress',
2234
+ awaitingHumanGate: false,
2235
+ });
2236
+ process.stdout.write(
2237
+ JSON.stringify({
2238
+ ok: true,
2239
+ action: 'resume_blocked',
2240
+ pipelineIndex: stepIndex,
2241
+ agents,
2242
+ attemptIds: [attemptId],
2243
+ followup_message: handoffForAgent(resumedAgent, step.scope, step),
2244
+ }),
2245
+ );
2246
+ process.exit(0);
2247
+ }
2248
+
2114
2249
  if (hookInput.action === 'bootstrap') {
2115
2250
  if (pauseBlocksAdvance()) process.exit(0);
2116
2251
  const pipeline = readPipeline();