@leitwerk-dev/coding 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/README.md +9 -0
  2. package/dist/LICENSE +13 -0
  3. package/dist/actions.d.ts +18 -0
  4. package/dist/actions.d.ts.map +1 -0
  5. package/dist/actions.js +45 -0
  6. package/dist/actions.js.map +1 -0
  7. package/dist/auto-work-branch.d.ts +17 -0
  8. package/dist/auto-work-branch.d.ts.map +1 -0
  9. package/dist/auto-work-branch.js +112 -0
  10. package/dist/auto-work-branch.js.map +1 -0
  11. package/dist/chunk-5LVPKVI2.js +27 -0
  12. package/dist/chunk-5LVPKVI2.js.map +1 -0
  13. package/dist/chunk-67YKGZLM.js +25 -0
  14. package/dist/chunk-67YKGZLM.js.map +1 -0
  15. package/dist/chunk-DSQX3ZHJ.js +60 -0
  16. package/dist/chunk-DSQX3ZHJ.js.map +1 -0
  17. package/dist/chunk-JZU3UVR6.js +573 -0
  18. package/dist/chunk-JZU3UVR6.js.map +1 -0
  19. package/dist/chunk-NIJCT4VK.js +45 -0
  20. package/dist/chunk-NIJCT4VK.js.map +1 -0
  21. package/dist/chunk-OFDNIT5X.js +13 -0
  22. package/dist/chunk-OFDNIT5X.js.map +1 -0
  23. package/dist/chunk-PX6F3LHL.js +40 -0
  24. package/dist/chunk-PX6F3LHL.js.map +1 -0
  25. package/dist/chunk-RWCLBBB2.js +34 -0
  26. package/dist/chunk-RWCLBBB2.js.map +1 -0
  27. package/dist/chunk-RZ54IVY5.js +28 -0
  28. package/dist/chunk-RZ54IVY5.js.map +1 -0
  29. package/dist/chunk-UBVLJV3V.js +23 -0
  30. package/dist/chunk-UBVLJV3V.js.map +1 -0
  31. package/dist/chunk-Y2S6JFVO.js +185 -0
  32. package/dist/chunk-Y2S6JFVO.js.map +1 -0
  33. package/dist/dist-CFKPEMX5.js +546 -0
  34. package/dist/dist-CFKPEMX5.js.map +1 -0
  35. package/dist/finalization-git.d.ts +34 -0
  36. package/dist/finalization-git.d.ts.map +1 -0
  37. package/dist/finalization-git.js +533 -0
  38. package/dist/finalization-git.js.map +1 -0
  39. package/dist/index.d.ts +11 -0
  40. package/dist/index.d.ts.map +1 -0
  41. package/dist/index.js +7 -0
  42. package/dist/index.js.map +1 -0
  43. package/dist/magic-string.es-4IX5OHLL.js +1309 -0
  44. package/dist/magic-string.es-4IX5OHLL.js.map +1 -0
  45. package/dist/repository-change-launch.d.ts +65 -0
  46. package/dist/repository-change-launch.d.ts.map +1 -0
  47. package/dist/repository-change-launch.js +183 -0
  48. package/dist/repository-change-launch.js.map +1 -0
  49. package/dist/repository-change-process.d.ts +38 -0
  50. package/dist/repository-change-process.d.ts.map +1 -0
  51. package/dist/repository-change-process.js +751 -0
  52. package/dist/repository-change-process.js.map +1 -0
  53. package/dist/repository-change-state.d.ts +16 -0
  54. package/dist/repository-change-state.d.ts.map +1 -0
  55. package/dist/repository-change-state.js +52 -0
  56. package/dist/repository-change-state.js.map +1 -0
  57. package/dist/turns/generate-commit-message.d.ts +4 -0
  58. package/dist/turns/generate-commit-message.d.ts.map +1 -0
  59. package/dist/turns/generate-commit-message.js +39 -0
  60. package/dist/turns/generate-commit-message.js.map +1 -0
  61. package/dist/turns/generate-plan.d.ts +3 -0
  62. package/dist/turns/generate-plan.d.ts.map +1 -0
  63. package/dist/turns/generate-plan.js +20 -0
  64. package/dist/turns/generate-plan.js.map +1 -0
  65. package/dist/turns/implement.d.ts +3 -0
  66. package/dist/turns/implement.d.ts.map +1 -0
  67. package/dist/turns/implement.js +18 -0
  68. package/dist/turns/implement.js.map +1 -0
  69. package/dist/turns/resolve-merge-conflict.d.ts +3 -0
  70. package/dist/turns/resolve-merge-conflict.d.ts.map +1 -0
  71. package/dist/turns/resolve-merge-conflict.js +22 -0
  72. package/dist/turns/resolve-merge-conflict.js.map +1 -0
  73. package/dist/turns/review-implementation.d.ts +4 -0
  74. package/dist/turns/review-implementation.d.ts.map +1 -0
  75. package/dist/turns/review-implementation.js +28 -0
  76. package/dist/turns/review-implementation.js.map +1 -0
  77. package/dist/turns/review-plan.d.ts +4 -0
  78. package/dist/turns/review-plan.d.ts.map +1 -0
  79. package/dist/turns/review-plan.js +22 -0
  80. package/dist/turns/review-plan.js.map +1 -0
  81. package/dist/turns/simplify-implementation.d.ts +3 -0
  82. package/dist/turns/simplify-implementation.d.ts.map +1 -0
  83. package/dist/turns/simplify-implementation.js +8 -0
  84. package/dist/turns/simplify-implementation.js.map +1 -0
  85. package/package.json +72 -0
  86. package/src/actions.ts +60 -0
  87. package/src/auto-work-branch.ts +145 -0
  88. package/src/finalization-git.ts +741 -0
  89. package/src/index.ts +9 -0
  90. package/src/repository-change-launch.ts +264 -0
  91. package/src/repository-change-process.ts +933 -0
  92. package/src/repository-change-state.ts +75 -0
  93. package/src/turns/generate-commit-message.ts +49 -0
  94. package/src/turns/generate-plan.ts +23 -0
  95. package/src/turns/implement.ts +21 -0
  96. package/src/turns/resolve-merge-conflict.ts +25 -0
  97. package/src/turns/review-implementation.ts +32 -0
  98. package/src/turns/review-plan.ts +26 -0
  99. package/src/turns/simplify-implementation.ts +11 -0
@@ -0,0 +1,145 @@
1
+ import { execFile } from "node:child_process";
2
+ import { createHash, randomBytes } from "node:crypto";
3
+ import { promisify } from "node:util";
4
+ import { trimToNull } from "@leitwerk-dev/domain";
5
+ import { sanitizeWorkerSubprocessEnv } from "@leitwerk-dev/process-sdk";
6
+ import { resolveGitBinary } from "@leitwerk-dev/process-sdk/git-binary";
7
+
8
+ const DEFAULT_BASE_SHA_SUFFIX_LENGTH = 12;
9
+ const DEFAULT_MAX_BRANCH_SLUG_LENGTH = 48;
10
+ const DEFAULT_PROMPT_WORD_COUNT = 5;
11
+ const AUTO_BRANCH_RANDOM_HEX_LENGTH = 3;
12
+ const DEFAULT_GIT_TIMEOUT_MS = 15_000;
13
+ const execFileAsync = promisify(execFile);
14
+
15
+ export interface ResolveBaseBranchShaInput {
16
+ repoLocator: string;
17
+ baseBranch: string;
18
+ timeoutMs?: number;
19
+ }
20
+
21
+ function truncateSlugAtSeparator(value: string, maxLength: number): string {
22
+ if (value.length <= maxLength) {
23
+ return value;
24
+ }
25
+ return value.slice(0, maxLength).replace(/-+$/g, "");
26
+ }
27
+
28
+ function normalizedBranchWords(value: string): string[] {
29
+ return (
30
+ value
31
+ .normalize("NFKD")
32
+ .replace(/[\u0300-\u036f]/g, "")
33
+ .toLowerCase()
34
+ .replace(/[’']/g, "")
35
+ .match(/[a-z0-9]+/g) ?? []
36
+ );
37
+ }
38
+
39
+ export function slugifyBranchSourceForBranch(sourceText: string): string {
40
+ const words = normalizedBranchWords(sourceText).slice(0, DEFAULT_PROMPT_WORD_COUNT);
41
+ const normalized = words.join("-");
42
+ const slug = truncateSlugAtSeparator(normalized, DEFAULT_MAX_BRANCH_SLUG_LENGTH);
43
+ return slug || "change";
44
+ }
45
+
46
+ /** @deprecated Use slugifyBranchSourceForBranch. */
47
+ export const slugifyPromptForBranch = slugifyBranchSourceForBranch;
48
+ /** @deprecated Use slugifyBranchSourceForBranch. */
49
+ export const slugifyTitleForBranch = slugifyBranchSourceForBranch;
50
+
51
+ function normalizeSha(value: string): string {
52
+ const sha = value.trim().toLowerCase();
53
+ if (!/^[0-9a-f]{7,64}$/.test(sha)) {
54
+ throw new Error(`Expected a git object sha, got '${value}'`);
55
+ }
56
+ return sha;
57
+ }
58
+
59
+ export function generateAutoWorkBranchRandomHex(): string {
60
+ return randomBytes(2).toString("hex").slice(0, AUTO_BRANCH_RANDOM_HEX_LENGTH);
61
+ }
62
+
63
+ function normalizeRandomHex(value: string): string {
64
+ const normalized = value.trim().toLowerCase();
65
+ if (!new RegExp(`^[0-9a-f]{${AUTO_BRANCH_RANDOM_HEX_LENGTH}}$`).test(normalized)) {
66
+ throw new Error(
67
+ `Expected ${AUTO_BRANCH_RANDOM_HEX_LENGTH} random hex characters, got '${value}'`,
68
+ );
69
+ }
70
+ return normalized;
71
+ }
72
+
73
+ export function buildAutoWorkBranch(
74
+ sourceText: string,
75
+ baseBranchSha: string,
76
+ randomHex = generateAutoWorkBranchRandomHex(),
77
+ ): string {
78
+ const slug = slugifyBranchSourceForBranch(sourceText);
79
+ const suffix = normalizeRandomHex(randomHex);
80
+ const sha = normalizeSha(baseBranchSha);
81
+ return `${slug}-${suffix}-${sha.slice(0, DEFAULT_BASE_SHA_SUFFIX_LENGTH)}`;
82
+ }
83
+
84
+ /** Builds the same safe branch shape when authenticated remote SHA lookup must wait for a worker. */
85
+ export function gitShaLikeDigestFromSeed(seed: string): string {
86
+ return createHash("sha256").update(seed).digest("hex");
87
+ }
88
+
89
+ export function buildAutoWorkBranchFromSeed(
90
+ sourceText: string,
91
+ seed: string,
92
+ randomHex = generateAutoWorkBranchRandomHex(),
93
+ ): string {
94
+ return buildAutoWorkBranch(sourceText, gitShaLikeDigestFromSeed(seed), randomHex);
95
+ }
96
+
97
+ function baseBranchRef(baseBranch: string): string {
98
+ const trimmed = trimToNull(baseBranch);
99
+ if (!trimmed) {
100
+ throw new Error("baseBranch is required to resolve an automatic work branch");
101
+ }
102
+ return trimmed.startsWith("refs/heads/") ? trimmed : `refs/heads/${trimmed}`;
103
+ }
104
+
105
+ function outputToString(value: unknown): string {
106
+ return typeof value === "string" ? value : Buffer.isBuffer(value) ? value.toString("utf8") : "";
107
+ }
108
+
109
+ export async function resolveBaseBranchSha(input: ResolveBaseBranchShaInput): Promise<string> {
110
+ const repoLocator = trimToNull(input.repoLocator);
111
+ if (!repoLocator) {
112
+ throw new Error("repoLocator is required to resolve an automatic work branch");
113
+ }
114
+ const ref = baseBranchRef(input.baseBranch);
115
+ let stdout = "";
116
+ try {
117
+ const result = await execFileAsync(resolveGitBinary(), ["ls-remote", repoLocator, ref], {
118
+ encoding: "utf8",
119
+ env: sanitizeWorkerSubprocessEnv(process.env, { GIT_TERMINAL_PROMPT: "0" }),
120
+ timeout: input.timeoutMs ?? DEFAULT_GIT_TIMEOUT_MS,
121
+ });
122
+ stdout = outputToString(result.stdout);
123
+ } catch (error) {
124
+ const errorOutput = error as { message?: string; stdout?: unknown; stderr?: unknown };
125
+ const message =
126
+ trimToNull(outputToString(errorOutput.stderr)) ??
127
+ trimToNull(outputToString(errorOutput.stdout)) ??
128
+ errorOutput.message ??
129
+ "unknown git error";
130
+ throw new Error(`Failed to resolve '${ref}' from '${repoLocator}': ${message}`);
131
+ }
132
+ const match = stdout
133
+ .split(/\r?\n/)
134
+ .map((line) => line.trim())
135
+ .filter(Boolean)
136
+ .map((line) => line.split(/\s+/))
137
+ .find((parts) => parts[1] === ref);
138
+ const sha = match?.[0];
139
+ if (!sha) {
140
+ throw new Error(
141
+ `Repository '${repoLocator}' does not expose base branch '${input.baseBranch}'`,
142
+ );
143
+ }
144
+ return normalizeSha(sha);
145
+ }