@h-rig/bundle-default-lifecycle 0.0.6-alpha.135 → 0.0.6-alpha.137

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.
@@ -1,325 +0,0 @@
1
- // @bun
2
- // packages/bundle-default-lifecycle/src/stagedCloseout.ts
3
- import { resolve } from "path";
4
- import { loadConfig } from "@rig/core/load-config";
5
- import { buildPluginHostContext } from "@rig/runtime/control-plane/plugin-host-context";
6
- import { CloseoutValidationError } from "@rig/runtime/control-plane/native/in-process-closeout";
7
- import { taskValidate } from "@rig/runtime/control-plane/native/task-ops";
8
-
9
- // packages/bundle-default-lifecycle/src/defaultPipeline.ts
10
- import { resolveKernelStages } from "@rig/kernel/resolver";
11
-
12
- // packages/bundle-default-lifecycle/src/stages/auto-merge.ts
13
- import { runRepoDefaultMerge } from "@rig/runtime/control-plane/native/pr-automation";
14
-
15
- // packages/bundle-default-lifecycle/src/stages/types.ts
16
- function defineDefaultLifecycleStage(input) {
17
- return {
18
- ...input,
19
- protected: input.protected ?? false,
20
- priority: input.priority ?? 0
21
- };
22
- }
23
-
24
- // packages/bundle-default-lifecycle/src/stages/auto-merge.ts
25
- var autoMergeStage = defineDefaultLifecycleStage({
26
- id: "auto-merge",
27
- kind: "transform",
28
- description: "Merge an approved PR using the repository default merge method through the runtime helper.",
29
- calls: ["runRepoDefaultMerge"]
30
- });
31
-
32
- // packages/bundle-default-lifecycle/src/stages/commit.ts
33
- import { commitRunChanges } from "@rig/runtime/control-plane/native/pr-automation";
34
- var commitStage = defineDefaultLifecycleStage({
35
- id: "commit",
36
- kind: "transform",
37
- description: "Commit the agent worktree changes using the runtime git closeout helper.",
38
- calls: ["commitRunChanges"]
39
- });
40
- async function runCommitStage(input) {
41
- return await commitRunChanges(input);
42
- }
43
-
44
- // packages/bundle-default-lifecycle/src/stages/isolation.ts
45
- import { ensureAgentRuntime } from "@rig/runtime/control-plane/runtime/isolation";
46
- var isolationStage = defineDefaultLifecycleStage({
47
- id: "isolation",
48
- kind: "transform",
49
- description: "Provision the isolated runtime worktree through the runtime isolation subsystem.",
50
- calls: ["ensureAgentRuntime"],
51
- protected: true
52
- });
53
-
54
- // packages/bundle-default-lifecycle/src/stages/journal-append.ts
55
- var journalAppendStage = defineDefaultLifecycleStage({
56
- id: "journal-append",
57
- kind: "observe",
58
- description: "Record resolved pipeline and per-stage outcome entries through the kernel journal capability.",
59
- calls: ["journalCapability.append"],
60
- protected: true
61
- });
62
-
63
- // packages/bundle-default-lifecycle/src/stages/merge-gate.ts
64
- var mergeGateStage = defineDefaultLifecycleStage({
65
- id: "merge-gate",
66
- kind: "gate",
67
- description: "Enforce GitHub review state, required checks, and configured review gates through runtime PR automation.",
68
- calls: ["runStrictPrMergeGate"],
69
- protected: true
70
- });
71
-
72
- // packages/bundle-default-lifecycle/src/stages/open-pr.ts
73
- import { runPrAutomation } from "@rig/runtime/control-plane/native/pr-automation";
74
- var openPrStage = defineDefaultLifecycleStage({
75
- id: "open-pr",
76
- kind: "transform",
77
- description: "Open or reuse the closeout PR through the existing runtime PR automation seam.",
78
- calls: ["runPrAutomation"]
79
- });
80
- async function runOpenPrStage(input) {
81
- return await runPrAutomation({
82
- projectRoot: input.workspace,
83
- taskId: input.taskId,
84
- runId: input.runId,
85
- branch: input.branch,
86
- sourceTask: input.sourceTask ? { title: typeof input.sourceTask.title === "string" ? input.sourceTask.title : null } : null,
87
- command: input.command,
88
- gitCommand: input.gitCommand,
89
- steerPi: input.steerPi,
90
- ...input.config ? { config: input.config } : {},
91
- ...input.artifactRoot !== undefined ? { artifactRoot: input.artifactRoot } : {},
92
- ...input.greptileApi !== undefined ? { greptileApi: input.greptileApi } : {},
93
- ...input.lifecycle !== undefined ? { lifecycle: input.lifecycle } : {},
94
- ...input.uploadedSnapshot !== undefined ? { uploadedSnapshot: input.uploadedSnapshot } : {}
95
- });
96
- }
97
-
98
- // packages/bundle-default-lifecycle/src/stages/push.ts
99
- import { pushBranchSyncedWithOrigin } from "@rig/runtime/control-plane/native/pr-automation";
100
- var pushStage = defineDefaultLifecycleStage({
101
- id: "push",
102
- kind: "transform",
103
- description: "Synchronize and push the task branch using the runtime closeout git helper.",
104
- calls: ["pushBranchSyncedWithOrigin"]
105
- });
106
- async function runPushStage(input) {
107
- await pushBranchSyncedWithOrigin(input);
108
- }
109
-
110
- // packages/bundle-default-lifecycle/src/stages/source-closeout.ts
111
- import { closeIssueAfterMergedPr } from "@rig/runtime/control-plane/native/pr-automation";
112
- var sourceCloseoutStage = defineDefaultLifecycleStage({
113
- id: "source-closeout",
114
- kind: "transform",
115
- description: "Reflect the merged PR into the task source using the existing runtime closeout helper.",
116
- calls: ["closeIssueAfterMergedPr"]
117
- });
118
- async function runSourceCloseoutStage(input) {
119
- if (input.pr.status !== "merged" || !input.pr.prUrl)
120
- return;
121
- await closeIssueAfterMergedPr({
122
- projectRoot: input.projectRoot,
123
- taskId: input.taskId,
124
- runId: input.runId,
125
- prUrl: input.pr.prUrl,
126
- updateTaskSource: input.updateTaskSource,
127
- ...input.sourceTask !== undefined ? { sourceTask: input.sourceTask } : {}
128
- });
129
- }
130
-
131
- // packages/bundle-default-lifecycle/src/stages/validate.ts
132
- var validateStage = defineDefaultLifecycleStage({
133
- id: "validate",
134
- kind: "transform",
135
- description: "Run plugin-host validators against the isolated worktree before closeout side effects.",
136
- calls: ["taskValidate"]
137
- });
138
- async function runValidateStage(input, runner) {
139
- return await runner(input);
140
- }
141
-
142
- // packages/bundle-default-lifecycle/src/stages/verify.ts
143
- var verifyStage = defineDefaultLifecycleStage({
144
- id: "verify",
145
- kind: "gate",
146
- description: "Run the local verifier preflight and block closeout when it rejects the worktree.",
147
- calls: ["verifierPreflight"]
148
- });
149
-
150
- // packages/bundle-default-lifecycle/src/defaultPipeline.ts
151
- var defaultLifecycleStages = [
152
- validateStage,
153
- verifyStage,
154
- commitStage,
155
- pushStage,
156
- openPrStage,
157
- mergeGateStage,
158
- autoMergeStage,
159
- sourceCloseoutStage,
160
- isolationStage,
161
- journalAppendStage
162
- ];
163
- function resolveDefaultLifecycle(input = {}) {
164
- const grants = { protectedStageGrants: input.protectedStageGrants ?? [] };
165
- return resolveKernelStages(defaultLifecycleStages, input.mutations ?? [], grants);
166
- }
167
-
168
- // packages/bundle-default-lifecycle/src/stagedCloseout.ts
169
- function cleanString(value) {
170
- return typeof value === "string" && value.trim().length > 0 ? value.trim() : null;
171
- }
172
- function closeoutOutcome(status) {
173
- switch (status) {
174
- case "completed":
175
- return "completed";
176
- case "failed":
177
- case "needs-attention":
178
- return "failed";
179
- case "pending":
180
- case "running":
181
- return "started";
182
- }
183
- }
184
- function normalizePrStatus(status) {
185
- return status === "needs_attention" ? "needs-attention" : status;
186
- }
187
- async function loadRigAutomationConfig(projectRoot) {
188
- try {
189
- return await loadConfig(projectRoot);
190
- } catch {
191
- return null;
192
- }
193
- }
194
- async function runRigProjectValidation({ projectRoot, taskId }) {
195
- const pluginHostCtx = await buildPluginHostContext(projectRoot);
196
- return taskValidate(projectRoot, taskId, pluginHostCtx?.validatorRegistry ?? undefined);
197
- }
198
- async function executeStagedCloseout(input) {
199
- const taskId = cleanString(input.taskId);
200
- if (!taskId) {
201
- throw new Error("Staged closeout requires a task id.");
202
- }
203
- const loadedConfig = input.config ?? await loadRigAutomationConfig(input.projectRoot);
204
- const prMode = loadedConfig?.pr?.mode ?? "off";
205
- const reviewProvider = loadedConfig?.review?.provider ?? "github";
206
- const effectiveConfig = {
207
- ...loadedConfig ?? {},
208
- pr: {
209
- ...loadedConfig?.pr ?? {},
210
- mode: prMode
211
- },
212
- review: {
213
- ...loadedConfig?.review ?? {},
214
- provider: reviewProvider
215
- }
216
- };
217
- const workspace = input.workspace;
218
- const artifactRoot = input.artifactRoot ?? resolve(input.projectRoot, "artifacts", taskId);
219
- let branch = input.branch;
220
- let currentPhase = "queued";
221
- const journal = async (phase, status, detail) => {
222
- currentPhase = phase;
223
- await input.journalPhase(phase, closeoutOutcome(status), detail ?? null);
224
- };
225
- const complete = async (result, detail) => {
226
- await journal("completed", "completed", detail);
227
- return result;
228
- };
229
- try {
230
- if (prMode === "off" || prMode === "ask") {
231
- const reason = prMode === "ask" ? "PR creation awaits operator approval." : "PR automation disabled.";
232
- await input.reflect("under_review", reason);
233
- return await complete({ status: "skipped", iterations: 0, feedback: [] }, reason);
234
- }
235
- await input.onValidationStart?.();
236
- await journal("queued", "running", `Validating task ${taskId} before closeout.`);
237
- let validationPassed = false;
238
- try {
239
- validationPassed = await runValidateStage({ projectRoot: input.projectRoot, taskId }, input.runValidation ?? runRigProjectValidation);
240
- } catch (error) {
241
- const detail2 = `Rig validation failed before closeout: ${error instanceof Error ? error.message : String(error)}`;
242
- await input.reflect("needs_attention", "Rig validation failed before closeout; commit/push/PR automation is blocked.", { errorText: detail2 });
243
- throw new CloseoutValidationError(detail2);
244
- }
245
- if (!validationPassed) {
246
- const detail2 = `Rig validation failed for task ${taskId}; closeout blocked before commit.`;
247
- await input.reflect("needs_attention", "Rig validation failed before closeout; commit/push/PR automation is blocked.", { errorText: detail2 });
248
- throw new CloseoutValidationError(detail2);
249
- }
250
- await journal("queued", "completed", `Validation passed for task ${taskId}.`);
251
- const workspaceBranch = await input.gitCommand(["rev-parse", "--abbrev-ref", "HEAD"], { cwd: workspace });
252
- const currentWorkspaceBranch = workspaceBranch.exitCode === 0 ? cleanString(workspaceBranch.stdout) : null;
253
- if (currentWorkspaceBranch && currentWorkspaceBranch !== "HEAD" && currentWorkspaceBranch !== branch) {
254
- branch = currentWorkspaceBranch;
255
- }
256
- await journal("commit", "running", `Committing changes in ${workspace}.`);
257
- await runCommitStage({ cwd: workspace, message: `rig: complete task ${taskId}`, command: input.gitCommand });
258
- await journal("push", "running", `Pushing branch ${branch}.`);
259
- await runPushStage({ projectRoot: workspace, branch, gitCommand: input.gitCommand });
260
- await journal("pr-review-merge", "running", `Opening and merging a pull request for ${branch}.`);
261
- const pr = await runOpenPrStage({
262
- ...input,
263
- taskId,
264
- branch,
265
- artifactRoot,
266
- config: effectiveConfig,
267
- sourceTask: { title: cleanString(input.sourceTask?.title) },
268
- lifecycle: {
269
- onPrOpened: async ({ prUrl }) => {
270
- await journal("pr-opened", "running", prUrl);
271
- await input.reflect("under_review", "Rig opened a pull request for this task.");
272
- },
273
- onFeedback: async ({ feedback }) => {
274
- await input.reflect("ci_fixing", "Rig is fixing CI/review feedback for this task.", { errorText: feedback.join(`
275
- `) || null });
276
- },
277
- onMergeStarted: async ({ prUrl }) => {
278
- await journal("merge", "running", prUrl);
279
- await input.reflect("merging", "Rig is merging the pull request for this task.");
280
- },
281
- onMerged: async ({ prUrl }) => {
282
- await journal("close-source", "running", prUrl);
283
- }
284
- }
285
- });
286
- if (pr.status === "merged" && pr.prUrl) {
287
- await runSourceCloseoutStage({
288
- projectRoot: input.projectRoot,
289
- taskId,
290
- runId: input.runId,
291
- pr,
292
- ...input.sourceTask !== undefined ? { sourceTask: input.sourceTask } : {},
293
- updateTaskSource: async () => {
294
- await input.reflect("closed", "Rig merged the pull request and closed this task source.");
295
- return { updated: true, taskId, status: "closed", source: "runtime", sourceKind: "runtime" };
296
- }
297
- });
298
- return await complete({ status: "merged", prUrl: pr.prUrl, iterations: pr.iterations, feedback: pr.actionableFeedback }, `PR merged and issue closed: ${pr.prUrl}`);
299
- }
300
- if (pr.status === "opened" && pr.prUrl) {
301
- return await complete({ status: "opened", prUrl: pr.prUrl, iterations: pr.iterations, feedback: pr.actionableFeedback }, `PR ready without merge: ${pr.prUrl}`);
302
- }
303
- const detail = pr.actionableFeedback.join(`
304
- `) || "PR automation did not merge the PR.";
305
- await input.reflect("needs_attention", "Rig needs operator attention before this task can proceed.", { errorText: detail });
306
- await journal("pr-review-merge", "needs-attention", detail);
307
- return { status: normalizePrStatus(pr.status), ...pr.prUrl ? { prUrl: pr.prUrl } : {}, iterations: pr.iterations, feedback: pr.actionableFeedback };
308
- } catch (error) {
309
- const detail = error instanceof Error ? error.message : String(error);
310
- await journal(currentPhase, "failed", detail);
311
- throw error;
312
- }
313
- }
314
- async function runStagedCloseout(input) {
315
- const resolved = resolveDefaultLifecycle();
316
- const result = await executeStagedCloseout(input);
317
- return {
318
- mode: "staged",
319
- pipelineStageIds: [...resolved.order],
320
- result
321
- };
322
- }
323
- export {
324
- runStagedCloseout
325
- };