@leitwerk-dev/coding 0.4.0 → 0.4.1

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 (46) hide show
  1. package/README.md +67 -11
  2. package/dist/{chunk-Q24RPXOA.js → chunk-3WJRJ3Q7.js} +68 -7
  3. package/dist/chunk-3WJRJ3Q7.js.map +1 -0
  4. package/dist/chunk-B3PE6DPF.js +820 -0
  5. package/dist/chunk-B3PE6DPF.js.map +1 -0
  6. package/dist/chunk-E3DB4TIZ.js +49 -0
  7. package/dist/chunk-E3DB4TIZ.js.map +1 -0
  8. package/dist/{chunk-BI45R2FH.js → chunk-QD6FI4HR.js} +1 -1
  9. package/dist/chunk-QD6FI4HR.js.map +1 -0
  10. package/dist/finalization-git-internal.d.ts +6 -0
  11. package/dist/finalization-git-internal.d.ts.map +1 -1
  12. package/dist/finalization-git.js +2 -1
  13. package/dist/index.js +3 -2
  14. package/dist/index.js.map +1 -1
  15. package/dist/pull-request-publication-internal.d.ts +8 -0
  16. package/dist/pull-request-publication-internal.d.ts.map +1 -1
  17. package/dist/repository-change-process.d.ts.map +1 -1
  18. package/dist/repository-change-publication.d.ts +16 -3
  19. package/dist/repository-change-publication.d.ts.map +1 -1
  20. package/dist/repository-change-publication.js +283 -69
  21. package/dist/repository-change-publication.js.map +1 -1
  22. package/dist/repository-publication-receipt.d.ts +23 -0
  23. package/dist/repository-publication-receipt.d.ts.map +1 -0
  24. package/dist/repository-rebase/git.d.ts +8 -0
  25. package/dist/repository-rebase/git.d.ts.map +1 -1
  26. package/dist/repository-reconciliation.d.ts +47 -0
  27. package/dist/repository-reconciliation.d.ts.map +1 -0
  28. package/dist/repository-record.d.ts +6 -0
  29. package/dist/repository-record.d.ts.map +1 -0
  30. package/dist/turns/implement.d.ts +1 -1
  31. package/dist/turns/implement.d.ts.map +1 -1
  32. package/dist/turns/implement.js +1 -1
  33. package/package.json +5 -5
  34. package/src/finalization-git-internal.ts +75 -6
  35. package/src/pull-request-publication-internal.ts +3 -2
  36. package/src/repository-change-process.ts +19 -2
  37. package/src/repository-change-publication.ts +243 -57
  38. package/src/repository-publication-receipt.ts +71 -0
  39. package/src/repository-rebase/git.ts +108 -29
  40. package/src/repository-reconciliation.ts +600 -0
  41. package/src/repository-record.ts +14 -0
  42. package/src/turns/implement.ts +4 -1
  43. package/dist/chunk-BI45R2FH.js.map +0 -1
  44. package/dist/chunk-OM2AMDTJ.js +0 -347
  45. package/dist/chunk-OM2AMDTJ.js.map +0 -1
  46. package/dist/chunk-Q24RPXOA.js.map +0 -1
@@ -0,0 +1 @@
1
+ {"version":3,"file":"repository-reconciliation.d.ts","sourceRoot":"","sources":["../src/repository-reconciliation.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAUzD,gBAAgB;AAChB,MAAM,WAAW,mBAAmB;IACnC,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;IACnB,wDAAwD;IACxD,WAAW,EAAE,MAAM,CAAC;IACpB,kEAAkE;IAClE,WAAW,CAAC,EAAE,WAAW,CAAC;CAC1B;AAED,gBAAgB;AAChB,MAAM,WAAW,oBAAoB;IACpC,MAAM,EAAE,OAAO,GAAG,WAAW,CAAC;IAC9B,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,OAAO,CAAC;CACjB;AA6JD;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,mBAAmB,GAAG,oBAAoB,CA2PpF;AAED,mGAAmG;AACnG,wBAAgB,8BAA8B,CAC7C,KAAK,EAAE,IAAI,CAAC,mBAAmB,EAAE,UAAU,GAAG,YAAY,CAAC,EAC3D,SAAS,EAAE,MAAM,GACf,IAAI,CAKN;AAED,6FAA6F;AAC7F,wBAAgB,sBAAsB,CACrC,KAAK,EAAE,mBAAmB,GAAG;IAC5B,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,WAAW,CAAC;CACzB,GACC,IAAI,CA6CN;AAED,gBAAgB;AAChB,wBAAgB,yBAAyB,CACxC,KAAK,EAAE,IAAI,CAAC,mBAAmB,EAAE,UAAU,GAAG,YAAY,CAAC,GACzD,OAAO,CAET;AAED,uGAAuG;AACvG,wBAAgB,wBAAwB,CACvC,KAAK,EAAE,IAAI,CAAC,mBAAmB,EAAE,aAAa,CAAC,GAC7C,oBAAoB,CAWtB;AAED,4EAA4E;AAC5E,wBAAgB,8BAA8B,CAC7C,KAAK,EAAE,IAAI,CAAC,mBAAmB,EAAE,aAAa,CAAC,GAAG;IAAE,WAAW,CAAC,EAAE,MAAM,CAAA;CAAE,GACxE,oBAAoB,CAmCtB;AAED,oGAAoG;AACpG,wBAAgB,2BAA2B,CAAC,KAAK,EAAE,mBAAmB,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI,CAgB7F;AAED,gBAAgB;AAChB,eAAO,MAAM,oBAAoB,8iCAA8iC,CAAC"}
@@ -0,0 +1,6 @@
1
+ /** Repository-local JSON with atomic replacement. Callers own validation and locking. @internal */
2
+ export declare function repositoryRecord<T>(gitDirectory: string, name: string): {
3
+ read: () => T | null;
4
+ save: (record: T) => void;
5
+ };
6
+ //# sourceMappingURL=repository-record.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"repository-record.d.ts","sourceRoot":"","sources":["../src/repository-record.ts"],"names":[],"mappings":"AAGA,mGAAmG;AACnG,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,YAAY,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM;gBAG1D,CAAC,GAAG,IAAI;mBACH,CAAC,KAAG,IAAI;EAKxB"}
@@ -1,4 +1,4 @@
1
1
  import type { FlowPromptContext, StructuralProcessState } from "@leitwerk-dev/process-sdk";
2
2
  /** @internal */
3
- export declare function buildImplementPrompt(ctx: FlowPromptContext<unknown, StructuralProcessState, "plan">): string;
3
+ export declare function buildImplementPrompt(ctx: Pick<FlowPromptContext<unknown, StructuralProcessState, "plan">, "prompts" | "input" | "repo">): string;
4
4
  //# sourceMappingURL=implement.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"implement.d.ts","sourceRoot":"","sources":["../../src/turns/implement.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,MAAM,2BAA2B,CAAC;AAE3F,gBAAgB;AAChB,wBAAgB,oBAAoB,CACnC,GAAG,EAAE,iBAAiB,CAAC,OAAO,EAAE,sBAAsB,EAAE,MAAM,CAAC,GAC7D,MAAM,CAuBR"}
1
+ {"version":3,"file":"implement.d.ts","sourceRoot":"","sources":["../../src/turns/implement.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,MAAM,2BAA2B,CAAC;AAE3F,gBAAgB;AAChB,wBAAgB,oBAAoB,CACnC,GAAG,EAAE,IAAI,CACR,iBAAiB,CAAC,OAAO,EAAE,sBAAsB,EAAE,MAAM,CAAC,EAC1D,SAAS,GAAG,OAAO,GAAG,MAAM,CAC5B,GACC,MAAM,CAuBR"}
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  buildImplementPrompt
3
- } from "../chunk-BI45R2FH.js";
3
+ } from "../chunk-QD6FI4HR.js";
4
4
  import "../chunk-PX6F3LHL.js";
5
5
  export {
6
6
  buildImplementPrompt
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leitwerk-dev/coding",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/leitwerk-dev/leitwerk.git",
@@ -65,12 +65,12 @@
65
65
  "build": "tsup --silent"
66
66
  },
67
67
  "dependencies": {
68
- "@leitwerk-dev/domain": "0.4.0",
69
- "@leitwerk-dev/process-sdk": "0.4.0"
68
+ "@leitwerk-dev/domain": "0.4.1",
69
+ "@leitwerk-dev/process-sdk": "0.4.1"
70
70
  },
71
71
  "devDependencies": {
72
- "@leitwerk-dev/extension-runtime": "0.4.0",
73
- "@leitwerk-dev/test-support": "0.4.0",
72
+ "@leitwerk-dev/extension-runtime": "0.4.1",
73
+ "@leitwerk-dev/test-support": "0.4.1",
74
74
  "tsup": "^8.5.1"
75
75
  },
76
76
  "license": "Apache-2.0",
@@ -4,6 +4,14 @@ import path from "node:path";
4
4
  import { trimToNull, type WorkerErrorClass } from "@leitwerk-dev/domain";
5
5
  import { repositoryGitArgs, repositoryGitSubprocessEnv } from "@leitwerk-dev/process-sdk";
6
6
  import { resolveGitBinary } from "@leitwerk-dev/process-sdk/git-binary";
7
+ import {
8
+ pushWasRejected,
9
+ type RepositoryPublicationReceipt,
10
+ readPublicationReceipt,
11
+ readRepositoryBinding,
12
+ repositoryBindingMatches,
13
+ savePublicationReceipt,
14
+ } from "./repository-publication-receipt.js";
7
15
 
8
16
  /** @public */
9
17
  export interface GitIdentity {
@@ -25,6 +33,13 @@ export class DeterministicGitError extends Error {
25
33
  }
26
34
  }
27
35
 
36
+ /** @internal */
37
+ export class RemoteBranchChangedError extends DeterministicGitError {
38
+ constructor() {
39
+ super("The remote source branch changed before publication; reconcile and validate again");
40
+ }
41
+ }
42
+
28
43
  interface GitExecResult {
29
44
  ok: boolean;
30
45
  stdout: string;
@@ -147,16 +162,68 @@ function repositoryGit(projectKey: string, repoPath: string) {
147
162
  return git("rev-parse", "HEAD");
148
163
  }
149
164
 
150
- function pushAndVerify(branch: string, expectedHeadSha: string): string {
165
+ function pushAndVerify(
166
+ branch: string,
167
+ expectedHeadSha: string,
168
+ expectedRemoteHead?: string | null,
169
+ ): string {
151
170
  const ref = `refs/heads/${branch}`;
152
171
  const pushTarget = `origin/${branch}`;
153
- checkedGit(["push", "origin", `HEAD:${ref}`], `Failed to push HEAD to '${pushTarget}'`);
154
- const remoteHead = gitOrNull("ls-remote", "--heads", "origin", ref)?.split(/\s+/)[0]?.trim();
155
- if (remoteHead !== expectedHeadSha) {
172
+ const observed = git("ls-remote", "--heads", "origin", ref).split(/\s+/)[0] || null;
173
+ const lease = expectedRemoteHead === undefined ? observed : expectedRemoteHead;
174
+ const directory = git("rev-parse", "--absolute-git-dir");
175
+ const binding = readRepositoryBinding(git, branch);
176
+ if (binding.origin !== binding.pushOrigin)
177
+ throw new DeterministicGitError("Fetch and push endpoints must match for publication");
178
+ const previous = readPublicationReceipt(directory);
179
+ if (!repositoryBindingMatches(previous, binding))
180
+ throw new DeterministicGitError("Repository publication receipt binding changed");
181
+ const receipt: RepositoryPublicationReceipt = {
182
+ ...binding,
183
+ expectedHead: lease,
184
+ candidate: expectedHeadSha,
185
+ status: "attempting",
186
+ };
187
+ // Recover a successful push whose acknowledgement was lost.
188
+ if (observed === expectedHeadSha) {
189
+ savePublicationReceipt(directory, { ...receipt, status: "accepted" });
190
+ return pushTarget;
191
+ }
192
+ if (previous?.status === "attempting")
156
193
  throw new DeterministicGitError(
157
- `Published '${pushTarget}' resolved to '${remoteHead ?? "missing"}', expected '${expectedHeadSha}'`,
194
+ "A prior publication is unresolved; retain local work and reconcile its exact candidate first",
158
195
  );
196
+ if (previous?.status === "accepted" && previous.candidate === expectedHeadSha)
197
+ throw new RemoteBranchChangedError();
198
+ if (observed !== lease) throw new RemoteBranchChangedError();
199
+ if (lease && !runGit(["merge-base", "--is-ancestor", lease, expectedHeadSha]).ok)
200
+ throw new DeterministicGitError(
201
+ "Ordinary publication must preserve the captured remote head",
202
+ );
203
+ savePublicationReceipt(directory, receipt);
204
+ const pushed = runGit([
205
+ "push",
206
+ "--porcelain",
207
+ "--no-follow-tags",
208
+ "--recurse-submodules=no",
209
+ `--force-with-lease=${ref}:${lease ?? ""}`,
210
+ "origin",
211
+ `${expectedHeadSha}:${ref}`,
212
+ ]);
213
+ if (!pushed.ok) {
214
+ if (pushWasRejected(pushed.stdout))
215
+ savePublicationReceipt(directory, { ...receipt, status: "rejected" });
216
+ const latest = git("ls-remote", "--heads", "origin", ref).split(/\s+/)[0] || null;
217
+ if (latest === expectedHeadSha) {
218
+ savePublicationReceipt(directory, { ...receipt, status: "accepted" });
219
+ return pushTarget;
220
+ }
221
+ if (latest !== lease) throw new RemoteBranchChangedError();
222
+ throw new DeterministicGitError(`Failed to push HEAD to '${pushTarget}': ${pushed.stderr}`);
159
223
  }
224
+ savePublicationReceipt(directory, { ...receipt, status: "accepted" });
225
+ const remoteHead = gitOrNull("ls-remote", "--heads", "origin", ref)?.split(/\s+/)[0]?.trim();
226
+ if (remoteHead !== expectedHeadSha) throw new RemoteBranchChangedError();
160
227
  return pushTarget;
161
228
  }
162
229
 
@@ -187,6 +254,8 @@ export function commitAndPushWorkBranch(input: {
187
254
  commitMessage: string;
188
255
  /** @public */
189
256
  gitIdentity: GitIdentity;
257
+ /** Captured remote head; null requires branch creation. @internal */
258
+ expectedRemoteHead?: string | null;
190
259
  }) {
191
260
  const repoPath = path.resolve(input.repoPath);
192
261
  const { runGit, git, conflictedFiles, mergeInProgress, commitIfDirty, pushAndVerify } =
@@ -216,6 +285,6 @@ export function commitAndPushWorkBranch(input: {
216
285
  /** @public */
217
286
  headSha,
218
287
  /** @internal */
219
- pushTarget: pushAndVerify(input.workBranch, headSha),
288
+ pushTarget: pushAndVerify(input.workBranch, headSha, input.expectedRemoteHead),
220
289
  };
221
290
  }
@@ -18,6 +18,7 @@ import type {
18
18
  PublicationRequest,
19
19
  PublicationSource,
20
20
  PublicationState,
21
+ PublicationReconciliationContext as RemoteContext,
21
22
  RepositoryChangePublicationAdapter,
22
23
  } from "./repository-change-publication.js";
23
24
  import {
@@ -85,7 +86,7 @@ function pullRequestDeliveryTools(provider: string): string[] {
85
86
  }
86
87
 
87
88
  const callFor =
88
- <P>(ctx: PublicationContext<P>, provider: string) =>
89
+ (ctx: Pick<PublicationContext<unknown>, "callIntegrationTool">, provider: string) =>
89
90
  <T>(name: string, args: Record<string, unknown>) =>
90
91
  ctx.callIntegrationTool(`${provider}_${name}`, { projectKey: "repo", ...args }) as Promise<T>;
91
92
 
@@ -222,7 +223,7 @@ export function pullRequestPublicationDefaults<P extends PublicationParams>(
222
223
  }),
223
224
  read: () => ({ kind: "cancelled" }),
224
225
  };
225
- async function finalizeIssue(ctx: PublicationContext<P>, pr: PublicationRequest | null) {
226
+ async function finalizeIssue(ctx: RemoteContext<P>, pr: PublicationRequest | null) {
226
227
  const params = issueOrigin(ctx.params);
227
228
  if (!params) return;
228
229
  const call = callFor(ctx, provider);
@@ -1,4 +1,4 @@
1
- import { trimString as normalizeMessage } from "@leitwerk-dev/domain";
1
+ import { asUnknownRecord, trimString as normalizeMessage } from "@leitwerk-dev/domain";
2
2
  import {
3
3
  type Codec,
4
4
  type ExternalActionSource,
@@ -16,6 +16,7 @@ import {
16
16
  type RepositoryChangeState,
17
17
  repositoryChangeStateCodec,
18
18
  } from "./repository-change-state-internal.js";
19
+ import { prepareInitialRepository, reconciliationPrompt } from "./repository-reconciliation.js";
19
20
  import { codingPurposes } from "./settings.js";
20
21
  import { prepareRepositoryCommitMessageEvidence } from "./turns/commit-message-evidence.js";
21
22
  import {
@@ -279,7 +280,23 @@ export function createRepositoryChangeProcess<TParams extends RepositoryChangePa
279
280
  .freshSeededPrimary()
280
281
  .startFromRoot()
281
282
  .consume("plan")
282
- .buildPrompt(buildImplementPrompt)
283
+ .prepare((ctx) => ({
284
+ reconciliation: ctx.repo.all().map((repo) => {
285
+ const identity = asUnknownRecord(
286
+ ctx.projects.find((p) => p.key === repo.key)?.metadata?.["leitwerk.gitIdentity"],
287
+ );
288
+ return prepareInitialRepository({
289
+ projectKey: repo.key,
290
+ repoPath: repo.fsPath,
291
+ workBranch: repo.workBranch,
292
+ gitIdentity:
293
+ typeof identity?.name === "string" && typeof identity.email === "string"
294
+ ? { name: identity.name, email: identity.email }
295
+ : undefined,
296
+ });
297
+ }),
298
+ }))
299
+ .buildPrompt((ctx) => `${buildImplementPrompt(ctx)}\n\n${reconciliationPrompt}`)
283
300
  .outcomeTool("implementation_ready", (tool) =>
284
301
  tool
285
302
  .description("Implementation and checks are complete; continue to delivery")