ai-foreman 1.7.5 → 1.7.7

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 (78) hide show
  1. package/README.md +17 -2
  2. package/dist/activity.d.ts +3 -0
  3. package/dist/activity.js +29 -4
  4. package/dist/adapters/claude.d.ts +33 -2
  5. package/dist/adapters/claude.js +208 -17
  6. package/dist/adapters/codex.d.ts +11 -1
  7. package/dist/adapters/codex.js +195 -20
  8. package/dist/adapters/recovering.d.ts +8 -2
  9. package/dist/adapters/recovering.js +23 -3
  10. package/dist/adapters/sessionFailure.d.ts +18 -0
  11. package/dist/adapters/sessionFailure.js +51 -0
  12. package/dist/adapters/types.d.ts +52 -9
  13. package/dist/agentRun.d.ts +4 -1
  14. package/dist/agentRun.js +96 -9
  15. package/dist/branch/currentGuard.d.ts +34 -0
  16. package/dist/branch/currentGuard.js +65 -0
  17. package/dist/branch/github.js +3 -2
  18. package/dist/branch/gitlab.js +3 -2
  19. package/dist/branch/planner.d.ts +5 -0
  20. package/dist/branch/planner.js +37 -3
  21. package/dist/branch/prefix.d.ts +8 -0
  22. package/dist/branch/prefix.js +26 -0
  23. package/dist/branch/resume.d.ts +2 -0
  24. package/dist/branch/resume.js +8 -0
  25. package/dist/branch/runner.d.ts +14 -2
  26. package/dist/branch/runner.js +77 -17
  27. package/dist/buildRuns.d.ts +51 -18
  28. package/dist/buildRuns.js +318 -31
  29. package/dist/cli/handoffs.d.ts +2 -0
  30. package/dist/cli/handoffs.js +74 -0
  31. package/dist/cli/start.js +1040 -62
  32. package/dist/cli/tickets.d.ts +10 -1
  33. package/dist/cli/tickets.js +615 -80
  34. package/dist/continuity.d.ts +86 -0
  35. package/dist/continuity.js +365 -0
  36. package/dist/foreman.d.ts +9 -5
  37. package/dist/foreman.js +76 -20
  38. package/dist/handoffs.d.ts +69 -0
  39. package/dist/handoffs.js +326 -0
  40. package/dist/log.d.ts +1 -1
  41. package/dist/permissions/policy.d.ts +5 -1
  42. package/dist/permissions/policy.js +48 -1
  43. package/dist/qaReview.d.ts +7 -6
  44. package/dist/qaReview.js +31 -20
  45. package/dist/sessionAvailability.d.ts +15 -0
  46. package/dist/sessionAvailability.js +57 -0
  47. package/dist/sessionIdentity.d.ts +33 -0
  48. package/dist/sessionIdentity.js +132 -0
  49. package/dist/sessionLifecycle.d.ts +102 -3
  50. package/dist/sessionLifecycle.js +578 -20
  51. package/dist/statusReporter.d.ts +58 -8
  52. package/dist/statusReporter.js +210 -14
  53. package/dist/ticketPlanning.js +8 -0
  54. package/dist/ticketPopulation.js +88 -12
  55. package/dist/tickets/commands.js +20 -12
  56. package/dist/tickets/details.d.ts +17 -0
  57. package/dist/tickets/details.js +94 -0
  58. package/dist/tickets/eligibility.d.ts +28 -0
  59. package/dist/tickets/eligibility.js +71 -0
  60. package/dist/tickets/groupReset.d.ts +25 -0
  61. package/dist/tickets/groupReset.js +279 -0
  62. package/dist/tickets/groups.d.ts +36 -0
  63. package/dist/tickets/groups.js +68 -0
  64. package/dist/tickets/importer.js +27 -3
  65. package/dist/tickets/recommendations.js +31 -7
  66. package/dist/tickets/reset.d.ts +28 -0
  67. package/dist/tickets/reset.js +214 -0
  68. package/dist/tickets/reviewStandards.d.ts +7 -0
  69. package/dist/tickets/reviewStandards.js +53 -0
  70. package/dist/tickets/setupConfig.d.ts +23 -0
  71. package/dist/tickets/setupConfig.js +115 -3
  72. package/dist/tickets/stateDb.d.ts +42 -1
  73. package/dist/tickets/stateDb.js +261 -23
  74. package/dist/tickets/ticketLoader.js +5 -2
  75. package/dist/tickets/validate.js +4 -0
  76. package/dist/workflowDb.d.ts +102 -2
  77. package/dist/workflowDb.js +460 -8
  78. package/package.json +35 -3
package/dist/agentRun.js CHANGED
@@ -6,12 +6,15 @@ import { PermissionPolicy } from "./permissions/policy.js";
6
6
  import { ClaudeAdapter } from "./adapters/claude.js";
7
7
  import { CodexAdapter } from "./adapters/codex.js";
8
8
  import { RecoveringAdapter } from "./adapters/recovering.js";
9
+ import { SessionUnavailableError } from "./adapters/sessionFailure.js";
9
10
  import { Foreman, createPermissionHandler } from "./foreman.js";
10
11
  import { printEvents } from "./cli/events.js";
11
12
  import { loadRoleBundle } from "./roles.js";
12
13
  import { ensureRuntimeReadyForCommand } from "./cli/runtimeAuthPrompt.js";
13
14
  import { resolveAgentForProject } from "./cli/runtimeSelection.js";
14
15
  import { parse as parseYaml } from "yaml";
16
+ import { captureWorkspaceIdentity, createProviderSessionRef, resolveUniqueSessionBinding } from "./sessionIdentity.js";
17
+ import { WorkflowDb } from "./workflowDb.js";
15
18
  export const VALID_EFFORT = ["low", "medium", "high", "xhigh"];
16
19
  export function assertEffortLevel(effort) {
17
20
  if (effort && !VALID_EFFORT.includes(effort)) {
@@ -87,10 +90,29 @@ export async function createRoleBuilder(opts) {
87
90
  throw new Error(`project directory not found: ${opts.projectDir}`);
88
91
  }
89
92
  const saved = readRoleDefaultsForExecution(opts.projectDir, opts.role);
93
+ if (opts.resumeSessionRef && opts.resumeSessionId && opts.resumeSessionRef.sessionId !== opts.resumeSessionId) {
94
+ throw new Error("resume session ID does not match the supplied location-scoped reference");
95
+ }
96
+ let requestedResumeRef = opts.resumeSessionRef;
97
+ if (!requestedResumeRef && opts.resumeSessionId) {
98
+ const bindingDb = new WorkflowDb(opts.projectDir);
99
+ try {
100
+ requestedResumeRef = resolveUniqueSessionBinding(bindingDb.providerSessionBindings(opts.resumeSessionId), opts.resumeSessionId);
101
+ }
102
+ finally {
103
+ bindingDb.close();
104
+ }
105
+ }
90
106
  const configuredEffort = saved?.reasoning && saved.reasoning !== "default" ? saved.reasoning : undefined;
91
107
  const effectiveEffort = opts.effort ?? (VALID_EFFORT.includes(configuredEffort) ? configuredEffort : undefined);
92
108
  assertEffortLevel(effectiveEffort);
93
- let runtime = resolveAgentForProject(opts.projectDir, opts.agent ?? saved?.make);
109
+ let runtime = resolveAgentForProject(opts.projectDir, opts.agent ?? requestedResumeRef?.provider ?? saved?.make);
110
+ if (requestedResumeRef && requestedResumeRef.provider !== runtime) {
111
+ throw new Error(`session ${requestedResumeRef.sessionId} belongs to ${requestedResumeRef.provider}, but ${runtime} was selected`);
112
+ }
113
+ if (requestedResumeRef && requestedResumeRef.role !== configurableRole(opts.role)) {
114
+ throw new Error(`session ${requestedResumeRef.sessionId} belongs to role ${requestedResumeRef.role}, not ${configurableRole(opts.role)}`);
115
+ }
94
116
  const config = loadConfig(join(opts.projectDir, "foreman.yaml"));
95
117
  const log = opts.log ?? new Log(makeLogPath(opts.projectDir, opts.label.replace(/\s+/g, "-")));
96
118
  const roleBundle = loadRoleBundle(opts.role, { projectDir: opts.projectDir });
@@ -99,7 +121,7 @@ export async function createRoleBuilder(opts) {
99
121
  const ready = await ensureRuntimeReadyForCommand(opts.projectDir, runtime, {
100
122
  label: opts.label,
101
123
  yes: Boolean(opts.yes),
102
- allowSwitch: opts.allowSwitch,
124
+ allowSwitch: requestedResumeRef || opts.resumeSessionId ? false : opts.allowSwitch,
103
125
  model: initialModel,
104
126
  });
105
127
  runtime = ready.runtime;
@@ -108,13 +130,42 @@ export async function createRoleBuilder(opts) {
108
130
  const policy = new PermissionPolicy(opts.permissionConfig ?? config.permissions, opts.projectDir);
109
131
  const skills = mergeSkills(roleBundle.skills, opts.extraSkills);
110
132
  const interactive = !opts.yes && Boolean(process.stdin.isTTY && process.stdout.isTTY);
111
- const makeAdapter = async (nextRuntime, resumeSessionId) => {
133
+ const makeAdapter = async (nextRuntime, resumeSessionId, resumeSessionRef) => {
134
+ let scopedRef = resumeSessionRef;
135
+ if (!scopedRef && resumeSessionId) {
136
+ const bindingDb = new WorkflowDb(opts.projectDir);
137
+ try {
138
+ scopedRef = resolveUniqueSessionBinding(bindingDb.providerSessionBindings(resumeSessionId), resumeSessionId);
139
+ }
140
+ finally {
141
+ bindingDb.close();
142
+ }
143
+ scopedRef ??= createProviderSessionRef({
144
+ provider: nextRuntime,
145
+ sessionId: resumeSessionId,
146
+ role: configurableRole(opts.role),
147
+ stream: opts.role,
148
+ generation: 0,
149
+ cwd: opts.projectDir,
150
+ configRoot: opts.projectDir,
151
+ workspaceIdentity: captureWorkspaceIdentity(opts.projectDir),
152
+ source: "legacy-inferred",
153
+ });
154
+ }
155
+ if (scopedRef && scopedRef.provider !== nextRuntime) {
156
+ throw new Error(`session ${scopedRef.sessionId} belongs to ${scopedRef.provider}, but ${nextRuntime} was selected`);
157
+ }
112
158
  const adapterOpts = {
113
159
  cwd: opts.projectDir,
160
+ configRoot: opts.projectDir,
114
161
  runtimeExecutable,
115
162
  runtimePhase: phaseForRole(opts.role),
116
163
  model,
117
- resumeSessionId,
164
+ ...(scopedRef ? { resumeSessionRef: scopedRef } : {}),
165
+ sessionRole: configurableRole(opts.role),
166
+ sessionStream: opts.role,
167
+ sessionGeneration: scopedRef?.generation ?? 0,
168
+ workspaceIdentity: captureWorkspaceIdentity(opts.projectDir),
118
169
  permission: createPermissionHandler(policy, log, {
119
170
  interactive,
120
171
  onAnsweredQuestion: opts.onAnsweredQuestion,
@@ -126,18 +177,46 @@ export async function createRoleBuilder(opts) {
126
177
  systemPromptAppend: roleBundle.system || undefined,
127
178
  skills: skills.length > 0 ? skills : undefined,
128
179
  };
129
- return nextRuntime === "codex"
180
+ const adapter = nextRuntime === "codex"
130
181
  ? new CodexAdapter(adapterOpts)
131
182
  : await ClaudeAdapter.create(adapterOpts);
183
+ if (scopedRef && adapter.agent === "codex") {
184
+ const availability = await adapter.validateSession();
185
+ if (availability.status !== "available") {
186
+ await adapter.close().catch(() => { });
187
+ throw new SessionUnavailableError({
188
+ runtime: "codex",
189
+ phase: "preflight",
190
+ dispatchState: "not-sent",
191
+ executable: runtimeExecutable,
192
+ cwd: opts.projectDir,
193
+ diagnostics: availability.detail ?? `Codex session ${scopedRef.sessionId} is ${availability.status}`,
194
+ availability,
195
+ });
196
+ }
197
+ }
198
+ return adapter;
132
199
  };
133
- const initial = await makeAdapter(runtime, opts.resumeSessionId);
200
+ const initial = await makeAdapter(runtime, opts.resumeSessionId, requestedResumeRef);
201
+ const persistSessionRef = (ref) => {
202
+ const bindingDb = new WorkflowDb(opts.projectDir);
203
+ try {
204
+ bindingDb.recordProviderSessionBinding(ref);
205
+ }
206
+ finally {
207
+ bindingDb.close();
208
+ }
209
+ };
210
+ const initialRef = initial.sessionRef?.();
211
+ if (initialRef)
212
+ persistSessionRef(initialRef);
134
213
  const builder = new RecoveringAdapter({
135
214
  initial,
136
215
  runtime,
137
216
  label: opts.label,
138
217
  enabled: interactive,
139
- allowSwitch: opts.allowSwitch !== false && !opts.resumeSessionId,
140
- recreate: async (nextRuntime, resumeSessionId) => {
218
+ allowSwitch: opts.allowSwitch !== false && !opts.resumeSessionId && !requestedResumeRef,
219
+ recreate: async (nextRuntime, resumeSessionId, resumeSessionRef) => {
141
220
  const nextReady = await ensureRuntimeReadyForCommand(opts.projectDir, nextRuntime, {
142
221
  label: opts.label,
143
222
  allowSwitch: false,
@@ -146,8 +225,9 @@ export async function createRoleBuilder(opts) {
146
225
  runtime = nextReady.runtime;
147
226
  model = nextReady.model;
148
227
  runtimeExecutable = nextReady.executable;
149
- return makeAdapter(runtime, resumeSessionId);
228
+ return makeAdapter(runtime, resumeSessionId, resumeSessionRef);
150
229
  },
230
+ onSessionRef: persistSessionRef,
151
231
  });
152
232
  return { builder, runtime, model, effort, roleBundle, skills, log };
153
233
  }
@@ -225,6 +305,7 @@ export async function runRoleInstruction(opts) {
225
305
  model: builder.agent === roleBuilder.runtime ? roleBuilder.model : undefined,
226
306
  effort,
227
307
  sessionId: builder.sessionId(),
308
+ sessionRef: builder.sessionRef?.(),
228
309
  statusKind: turn.status.kind,
229
310
  summary: turn.status.summary,
230
311
  reason: turn.status.reason,
@@ -238,6 +319,7 @@ export async function runRoleInstruction(opts) {
238
319
  model: builder.agent === roleBuilder.runtime ? roleBuilder.model : undefined,
239
320
  effort,
240
321
  sessionId: builder.sessionId(),
322
+ sessionRef: builder.sessionRef?.(),
241
323
  logPath,
242
324
  roleBundle,
243
325
  skills,
@@ -250,3 +332,8 @@ export async function runRoleInstruction(opts) {
250
332
  throw err;
251
333
  }
252
334
  }
335
+ function configurableRole(role) {
336
+ return ["builder", "qa", "planner", "ticket-maker", "uninstaller"].includes(role)
337
+ ? role
338
+ : "builder";
339
+ }
@@ -0,0 +1,34 @@
1
+ import type { BuilderAdapter, BuilderEvent, CompactResult, ContextUsage, ProviderSessionUsage, ProviderSettingSwitch, TurnResult } from "../adapters/types.js";
2
+ import type { ProviderSessionRefV1, SessionAvailabilityV1 } from "rafi-spec";
3
+ export interface CurrentWorkflowIdentity {
4
+ worktree: string;
5
+ ref: string;
6
+ }
7
+ export declare class CurrentWorkflowChangedError extends Error {
8
+ readonly expected: CurrentWorkflowIdentity;
9
+ readonly actual: CurrentWorkflowIdentity;
10
+ constructor(expected: CurrentWorkflowIdentity, actual: CurrentWorkflowIdentity);
11
+ }
12
+ export declare function captureCurrentWorkflowIdentity(cwd: string): CurrentWorkflowIdentity;
13
+ export declare function assertCurrentWorkflowIdentity(cwd: string, expected: CurrentWorkflowIdentity): void;
14
+ export declare function currentWorkflowIdentityKey(identity: CurrentWorkflowIdentity): string;
15
+ export declare function captureCurrentWorkflowSessionIdentity(cwd: string): string;
16
+ /** Fence every provider boundary against an unexpected user/provider ref switch. */
17
+ export declare class CurrentWorkflowGuardAdapter implements BuilderAdapter {
18
+ private readonly adapter;
19
+ private readonly cwd;
20
+ private readonly expected;
21
+ readonly agent: BuilderAdapter["agent"];
22
+ constructor(adapter: BuilderAdapter, cwd: string, expected?: CurrentWorkflowIdentity);
23
+ sendTurn(text: string): Promise<TurnResult>;
24
+ sessionId(): string | undefined;
25
+ sessionRef(): ProviderSessionRefV1 | undefined;
26
+ adoptSessionRef(ref: ProviderSessionRefV1): void;
27
+ validateSession(): Promise<SessionAvailabilityV1>;
28
+ compact(): Promise<CompactResult>;
29
+ contextUsage(): Promise<ContextUsage | undefined>;
30
+ sessionUsage(): Promise<ProviderSessionUsage | undefined>;
31
+ switchSettings(settings: ProviderSettingSwitch): Promise<CompactResult>;
32
+ events(): AsyncIterable<BuilderEvent>;
33
+ close(): Promise<void>;
34
+ }
@@ -0,0 +1,65 @@
1
+ import { execFileSync } from "node:child_process";
2
+ import { createHash } from "node:crypto";
3
+ import { realpathSync } from "node:fs";
4
+ export class CurrentWorkflowChangedError extends Error {
5
+ expected;
6
+ actual;
7
+ constructor(expected, actual) {
8
+ super(`current-branch workflow paused: active worktree/ref changed (expected ${expected.worktree} @ ${expected.ref}; found ${actual.worktree} @ ${actual.ref})`);
9
+ this.expected = expected;
10
+ this.actual = actual;
11
+ }
12
+ }
13
+ export function captureCurrentWorkflowIdentity(cwd) {
14
+ return { worktree: realpathSync(git(cwd, ["rev-parse", "--show-toplevel"])), ref: currentRef(cwd) };
15
+ }
16
+ export function assertCurrentWorkflowIdentity(cwd, expected) {
17
+ const actual = captureCurrentWorkflowIdentity(cwd);
18
+ if (actual.worktree !== expected.worktree || actual.ref !== expected.ref) {
19
+ throw new CurrentWorkflowChangedError(expected, actual);
20
+ }
21
+ }
22
+ export function currentWorkflowIdentityKey(identity) {
23
+ return `current_${createHash("sha256").update(`${identity.worktree}\0${identity.ref}`).digest("hex")}`;
24
+ }
25
+ export function captureCurrentWorkflowSessionIdentity(cwd) {
26
+ return currentWorkflowIdentityKey(captureCurrentWorkflowIdentity(cwd));
27
+ }
28
+ /** Fence every provider boundary against an unexpected user/provider ref switch. */
29
+ export class CurrentWorkflowGuardAdapter {
30
+ adapter;
31
+ cwd;
32
+ expected;
33
+ agent;
34
+ constructor(adapter, cwd, expected = captureCurrentWorkflowIdentity(cwd)) {
35
+ this.adapter = adapter;
36
+ this.cwd = cwd;
37
+ this.expected = expected;
38
+ this.agent = adapter.agent;
39
+ }
40
+ async sendTurn(text) {
41
+ assertCurrentWorkflowIdentity(this.cwd, this.expected);
42
+ const result = await this.adapter.sendTurn(text);
43
+ assertCurrentWorkflowIdentity(this.cwd, this.expected);
44
+ return result;
45
+ }
46
+ sessionId() { return this.adapter.sessionId(); }
47
+ sessionRef() { return this.adapter.sessionRef?.(); }
48
+ adoptSessionRef(ref) { this.adapter.adoptSessionRef?.(ref); }
49
+ validateSession() { assertCurrentWorkflowIdentity(this.cwd, this.expected); return this.adapter.validateSession?.() ?? Promise.resolve({ version: 1, status: "unknown", checkedAt: new Date().toISOString(), reason: "legacy-unscoped" }); }
50
+ async compact() { assertCurrentWorkflowIdentity(this.cwd, this.expected); const result = await (this.adapter.compact?.() ?? Promise.resolve({ ok: false, error: "native compaction unavailable" })); assertCurrentWorkflowIdentity(this.cwd, this.expected); return result; }
51
+ contextUsage() { return this.adapter.contextUsage?.() ?? Promise.resolve(undefined); }
52
+ sessionUsage() { return this.adapter.sessionUsage?.() ?? Promise.resolve(undefined); }
53
+ switchSettings(settings) { return this.adapter.switchSettings?.(settings) ?? Promise.resolve({ ok: false, error: "settings switch unavailable" }); }
54
+ events() { return this.adapter.events(); }
55
+ close() { return this.adapter.close(); }
56
+ }
57
+ function currentRef(cwd) {
58
+ try {
59
+ return `branch:${git(cwd, ["symbolic-ref", "--quiet", "--short", "HEAD"])}`;
60
+ }
61
+ catch {
62
+ return `detached:${git(cwd, ["rev-parse", "HEAD"])}`;
63
+ }
64
+ }
65
+ function git(cwd, args) { return execFileSync("git", args, { cwd, encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }).trim(); }
@@ -2,6 +2,7 @@ import { spawnSync } from "node:child_process";
2
2
  import { mkdirSync, writeFileSync } from "node:fs";
3
3
  import { join } from "node:path";
4
4
  import { branchNodeFooter } from "./presentation.js";
5
+ import { configuredReviewBody, configuredReviewTitle } from "../tickets/reviewStandards.js";
5
6
  const DEFAULT_COMMAND_TIMEOUT_MS = 15_000;
6
7
  const OUTPUT_LIMIT = 2_000;
7
8
  function runCommand(cwd, bin, args, timeoutMs = DEFAULT_COMMAND_TIMEOUT_MS) {
@@ -134,7 +135,7 @@ export function createOrReusePr(cwd, opts) {
134
135
  const bodyPath = join(cwd, ".foreman", "pr-bodies", opts.runId, `${opts.node.ticket.id}.md`);
135
136
  try {
136
137
  mkdirSync(join(cwd, ".foreman", "pr-bodies", opts.runId), { recursive: true });
137
- writeFileSync(bodyPath, buildPrBody(opts), "utf8");
138
+ writeFileSync(bodyPath, configuredReviewBody(cwd, opts.node, opts.qaEvidence, opts.commit), "utf8");
138
139
  }
139
140
  catch (err) {
140
141
  return prFailure(prBodyWriteFailure(opts.runId, bodyPath, err));
@@ -147,7 +148,7 @@ export function createOrReusePr(cwd, opts) {
147
148
  "--head",
148
149
  opts.node.branch,
149
150
  "--title",
150
- `${opts.node.ticket.id}: ${opts.node.ticket.title}`,
151
+ configuredReviewTitle(cwd, opts.node.ticket),
151
152
  "--body-file",
152
153
  bodyPath,
153
154
  ];
@@ -2,6 +2,7 @@ import { spawnSync } from "node:child_process";
2
2
  import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
3
3
  import { join } from "node:path";
4
4
  import { branchNodeFooter } from "./presentation.js";
5
+ import { configuredReviewBody, configuredReviewTitle } from "../tickets/reviewStandards.js";
5
6
  const DEFAULT_COMMAND_TIMEOUT_MS = 15_000;
6
7
  const OUTPUT_LIMIT = 2_000;
7
8
  export function preflightGlab(cwd) {
@@ -55,7 +56,7 @@ export function createOrReuseMr(cwd, opts) {
55
56
  const bodyPath = join(cwd, ".foreman", "mr-bodies", opts.runId, `${opts.node.ticket.id}.md`);
56
57
  try {
57
58
  mkdirSync(join(cwd, ".foreman", "mr-bodies", opts.runId), { recursive: true });
58
- writeFileSync(bodyPath, buildMrBody(opts), "utf8");
59
+ writeFileSync(bodyPath, configuredReviewBody(cwd, opts.node, opts.qaEvidence, opts.commit), "utf8");
59
60
  }
60
61
  catch (err) {
61
62
  return prFailure(failure("mr_create_failed", `Failed to write GitLab MR body file at ${bodyPath}.`, [
@@ -70,7 +71,7 @@ export function createOrReuseMr(cwd, opts) {
70
71
  "--target-branch",
71
72
  opts.node.baseBranch,
72
73
  "--title",
73
- `${opts.node.ticket.id}: ${opts.node.ticket.title}`,
74
+ configuredReviewTitle(cwd, opts.node.ticket),
74
75
  "--description",
75
76
  readBodyArg(bodyPath),
76
77
  "--yes",
@@ -1,6 +1,8 @@
1
1
  import type { TicketDef } from "../tickets/ticketSchema.js";
2
2
  import type { TicketState } from "../tickets/stateDb.js";
3
3
  import type { BranchPlan } from "./types.js";
4
+ import type { DeliveryConfig } from "../tickets/delivery.js";
5
+ import type { TicketBuildSetupConfig } from "../tickets/setupConfig.js";
4
6
  export interface AuditDependency {
5
7
  ticket: string;
6
8
  dependsOn: string;
@@ -18,5 +20,8 @@ export interface BuildBranchPlanOptions {
18
20
  export declare function buildBranchPlan(tickets: TicketDef[], states: Map<string, TicketState>, opts: BuildBranchPlanOptions): BranchPlan;
19
21
  export declare function selectTicketsForBranchRun(tickets: TicketDef[], states: Map<string, TicketState>, steps: number, ticketIds?: string[]): TicketDef[];
20
22
  export declare function applySharedDeliveryBranch(plan: BranchPlan, unitId: string, allRemainingTicketIds: string[], branchPrefix?: string): BranchPlan;
23
+ /** Apply branch sharing only; this deliberately never creates delivery stacks or dependency edges. */
24
+ export declare function applySizeBranchPolicy(plan: BranchPlan, policy: TicketBuildSetupConfig["branch_policy"], delivery?: DeliveryConfig, branchPrefix?: string): BranchPlan;
21
25
  export declare function parseAuditDependencies(text: string): AuditDependency[];
22
26
  export declare function buildBranchAuditInstruction(tickets: TicketDef[]): string;
27
+ export { normalizeBranchPrefix } from "./prefix.js";
@@ -1,3 +1,4 @@
1
+ import { normalizeBranchPrefix } from "./prefix.js";
1
2
  export function buildBranchPlan(tickets, states, opts) {
2
3
  const selected = selectTicketsForBranchRun(tickets, states, opts.steps, opts.ticketIds);
3
4
  const selectedIds = new Set(selected.map((ticket) => ticket.id));
@@ -99,11 +100,11 @@ export function selectTicketsForBranchRun(tickets, states, steps, ticketIds) {
99
100
  }
100
101
  return selected;
101
102
  }
102
- export function applySharedDeliveryBranch(plan, unitId, allRemainingTicketIds, branchPrefix = "rafi") {
103
+ export function applySharedDeliveryBranch(plan, unitId, allRemainingTicketIds, branchPrefix = "feature") {
103
104
  if (plan.nodes.length === 0)
104
105
  return plan;
105
106
  const slug = unitId.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "") || "group";
106
- const branch = `${branchPrefix.replace(/^\/+|\/+$/g, "") || "rafi"}/${slug}`;
107
+ const branch = `${normalizeBranchPrefix(branchPrefix)}/${slug}`;
107
108
  const finalSelected = plan.nodes.at(-1)?.ticket.id;
108
109
  const completesUnit = plan.nodes.length === allRemainingTicketIds.length
109
110
  && allRemainingTicketIds.every((id) => plan.nodes.some((node) => node.ticket.id === id));
@@ -118,6 +119,38 @@ export function applySharedDeliveryBranch(plan, unitId, allRemainingTicketIds, b
118
119
  })),
119
120
  };
120
121
  }
122
+ /** Apply branch sharing only; this deliberately never creates delivery stacks or dependency edges. */
123
+ export function applySizeBranchPolicy(plan, policy, delivery, branchPrefix = "feature") {
124
+ if (policy.mode !== "size" || plan.nodes.length < 2)
125
+ return plan;
126
+ const explicitlyAssigned = new Set(delivery?.units.flatMap((unit) => unit.tickets) ?? []);
127
+ const candidates = plan.nodes.filter((node) => policy.by_size[node.ticket.size] === "shared" && !explicitlyAssigned.has(node.ticket.id));
128
+ const groups = new Map();
129
+ for (const node of candidates) {
130
+ // Tickets with selected dependency edges need their existing topology. Independent
131
+ // tickets sharing the same base are safe to execute serially in one worktree/review.
132
+ if (node.dependencies.length)
133
+ continue;
134
+ const key = node.baseBranch;
135
+ groups.set(key, [...(groups.get(key) ?? []), node]);
136
+ }
137
+ const replacement = new Map();
138
+ for (const [base, nodes] of groups) {
139
+ if (nodes.length < 2)
140
+ continue;
141
+ const ids = nodes.map((node) => node.ticket.id.toLowerCase()).join("-");
142
+ const groupId = `size-small-${ids}`;
143
+ const branch = `${normalizeBranchPrefix(branchPrefix)}/${groupId}`.slice(0, 120).replace(/-+$/, "");
144
+ nodes.forEach((node, index) => replacement.set(node.ticket.id, {
145
+ ...node,
146
+ branch,
147
+ baseBranch: base,
148
+ deliveryUnitId: groupId,
149
+ deliveryUnitFinal: index === nodes.length - 1,
150
+ }));
151
+ }
152
+ return { ...plan, nodes: plan.nodes.map((node) => replacement.get(node.ticket.id) ?? node) };
153
+ }
121
154
  export function parseAuditDependencies(text) {
122
155
  const deps = [];
123
156
  for (const line of text.split(/\r?\n/)) {
@@ -145,7 +178,7 @@ Only name dependencies where both tickets are in this selected batch. If there a
145
178
  ${body}`;
146
179
  }
147
180
  function uniqueBranchName(prefix, ticket, used) {
148
- const cleanPrefix = prefix.replace(/^\/+|\/+$/g, "") || "rafi";
181
+ const cleanPrefix = normalizeBranchPrefix(prefix);
149
182
  const baseSlug = `${ticket.id}-${ticket.title}`
150
183
  .toLowerCase()
151
184
  .replace(/[^a-z0-9]+/g, "-")
@@ -160,6 +193,7 @@ function uniqueBranchName(prefix, ticket, used) {
160
193
  used.add(branch);
161
194
  return branch;
162
195
  }
196
+ export { normalizeBranchPrefix } from "./prefix.js";
163
197
  function detectSelectedCycles(tickets, deps) {
164
198
  const selected = new Set(tickets.map((ticket) => ticket.id));
165
199
  const visited = new Set();
@@ -0,0 +1,8 @@
1
+ /** Built-in prefix used only when neither a run nor project value exists. */
2
+ export declare const BUILTIN_BRANCH_PREFIX = "feature";
3
+ /**
4
+ * Validate a prefix as the leading components of a Git branch ref. Internal
5
+ * slashes are intentional (for example `team/feature`).
6
+ */
7
+ export declare function validateBranchPrefix(value: string): string;
8
+ export declare function normalizeBranchPrefix(prefix: string | undefined): string;
@@ -0,0 +1,26 @@
1
+ /** Built-in prefix used only when neither a run nor project value exists. */
2
+ export const BUILTIN_BRANCH_PREFIX = "feature";
3
+ /**
4
+ * Validate a prefix as the leading components of a Git branch ref. Internal
5
+ * slashes are intentional (for example `team/feature`).
6
+ */
7
+ export function validateBranchPrefix(value) {
8
+ const prefix = value.trim();
9
+ if (!prefix)
10
+ throw new Error("branch prefix must not be empty");
11
+ if (prefix === "@" || prefix.startsWith("-") || prefix.startsWith("/") || prefix.endsWith("/")) {
12
+ throw new Error(`invalid Git branch prefix: ${value}`);
13
+ }
14
+ if (prefix.includes("//") || prefix.includes("..") || prefix.includes("@{") || /[\x00-\x20\x7f~^:?*\[\\]/.test(prefix)) {
15
+ throw new Error(`invalid Git branch prefix: ${value}`);
16
+ }
17
+ for (const segment of prefix.split("/")) {
18
+ if (!segment || segment === "." || segment === ".." || segment.startsWith(".") || segment.endsWith(".") || segment.endsWith(".lock")) {
19
+ throw new Error(`invalid Git branch prefix: ${value}`);
20
+ }
21
+ }
22
+ return prefix;
23
+ }
24
+ export function normalizeBranchPrefix(prefix) {
25
+ return validateBranchPrefix(prefix === undefined ? BUILTIN_BRANCH_PREFIX : prefix);
26
+ }
@@ -1,10 +1,12 @@
1
1
  import type { BranchRunSummary } from "./types.js";
2
+ import type { ProviderSessionRefV1 } from "rafi-spec";
2
3
  export interface BranchResumeSession {
3
4
  ticket: string;
4
5
  branch: string;
5
6
  base: string;
6
7
  worktreePath: string;
7
8
  sessionId: string;
9
+ sessionRef?: ProviderSessionRefV1;
8
10
  logPath: string;
9
11
  agent?: string;
10
12
  model?: string;
@@ -32,6 +32,7 @@ export function findResumableBranchSessions(foremanDir) {
32
32
  base,
33
33
  worktreePath,
34
34
  sessionId,
35
+ ...(isProviderSessionRef(record.sessionRef) ? { sessionRef: record.sessionRef } : {}),
35
36
  logPath,
36
37
  agent: stringField(record.agent),
37
38
  model: stringField(record.model),
@@ -116,6 +117,13 @@ function stringField(value) {
116
117
  function booleanField(value) {
117
118
  return typeof value === "boolean" ? value : undefined;
118
119
  }
120
+ function isProviderSessionRef(value) {
121
+ if (!value || typeof value !== "object")
122
+ return false;
123
+ const ref = value;
124
+ return ref.version === 1 && (ref.provider === "claude" || ref.provider === "codex") && typeof ref.sessionId === "string"
125
+ && typeof ref.cwd === "string" && typeof ref.configRoot === "string" && typeof ref.workspaceIdentity === "string";
126
+ }
119
127
  function shellQuote(value) {
120
128
  if (/^[A-Za-z0-9_./:@%+=,-]+$/.test(value))
121
129
  return value;
@@ -1,13 +1,16 @@
1
1
  import type { BuilderAdapter, EffortLevel } from "../adapters/types.js";
2
- import type { SessionStrategy } from "rafi-spec";
2
+ import type { ProviderSessionRefV1, SessionStrategy } from "rafi-spec";
3
3
  import { type QaNonconvergenceContext, type QaNonconvergenceDecision } from "../qaReview.js";
4
4
  import type { Log } from "../log.js";
5
5
  import type { BranchPlan, BranchPlanNode, BranchRunSummary, CompletionMode, MergeMethod, ReviewProvider } from "./types.js";
6
+ import { SessionUnavailableError } from "../adapters/sessionFailure.js";
7
+ import { SessionUnavailableContinuityError } from "../continuity.js";
6
8
  export interface DeliveryUnitSession {
7
9
  unitId: string;
8
10
  branch: string;
9
11
  worktreePath: string;
10
12
  sessionId: string;
13
+ sessionRef?: ProviderSessionRefV1;
11
14
  ticket: string;
12
15
  }
13
16
  export type BaseWorktreePolicy = "enforce" | "warn" | "skip";
@@ -41,13 +44,22 @@ export interface BranchRunnerOptions {
41
44
  resumeSessions?: Map<string, {
42
45
  worktreePath: string;
43
46
  sessionId: string;
47
+ sessionRef?: ProviderSessionRefV1;
44
48
  }>;
45
- createBuilder: (cwd: string, sessionId?: string) => Promise<BuilderAdapter>;
49
+ createBuilder: (cwd: string, sessionId?: string, sessionRef?: ProviderSessionRefV1) => Promise<BuilderAdapter>;
50
+ /** Persist every observed Builder binding before later work can supersede it. */
51
+ recordBuilderSession?: (session: string | ProviderSessionRefV1, ticketId: string, worktreePath: string) => void;
52
+ recordQaSession?: (session: string | ProviderSessionRefV1, ticketId: string, worktreePath: string) => void;
53
+ /** Notify the host and stop the branch plan without discarding its worktree. */
54
+ onSessionUnavailable?: (error: SessionUnavailableError | SessionUnavailableContinuityError) => void;
46
55
  createQa?: (cwd: string, sessionId?: string) => Promise<BuilderAdapter>;
47
56
  builderSessionStrategy?: SessionStrategy;
48
57
  qaSessionStrategy?: SessionStrategy;
49
58
  observeBuilder?: (builder: BuilderAdapter) => Promise<void>;
50
59
  qaNonconvergence?: (context: QaNonconvergenceContext) => Promise<QaNonconvergenceDecision>;
60
+ beforeBuilderTurn?: (adapter: BuilderAdapter, frozenAction: string, cwd: string) => Promise<BuilderAdapter>;
61
+ builderSessionBoundary?: (adapter: BuilderAdapter, frozenAction: string, strategy: SessionStrategy, cwd: string) => Promise<BuilderAdapter>;
62
+ qaSessionBoundary?: (adapter: BuilderAdapter, frozenAction: string, strategy: SessionStrategy, cwd: string) => Promise<BuilderAdapter>;
51
63
  }
52
64
  export declare function runBranchPlan(opts: BranchRunnerOptions): Promise<BranchRunSummary[]>;
53
65
  export declare function buildBranchTicketInstruction(node: BranchPlanNode, trackerPaths?: {