@anureo/refactor 0.3.0 → 0.3.2

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 (86) hide show
  1. package/bin/anureo-refactor.ts +5 -0
  2. package/package.json +21 -23
  3. package/src/agent-work.ts +184 -0
  4. package/src/checks.ts +82 -0
  5. package/src/cli.ts +214 -0
  6. package/src/client.ts +93 -0
  7. package/src/errors.ts +70 -0
  8. package/src/git-path.ts +47 -0
  9. package/src/git-tree.ts +55 -0
  10. package/src/git.ts +55 -0
  11. package/src/graph-artifacts.ts +73 -0
  12. package/src/graph.ts +145 -0
  13. package/src/index.ts +40 -0
  14. package/src/options.ts +65 -0
  15. package/src/plan-validation.ts +38 -0
  16. package/src/{process.d.ts → process.ts} +4 -2
  17. package/src/{prompts.js → prompts.ts} +145 -67
  18. package/src/refactor.ts +191 -0
  19. package/src/{report-store.js → report-store.ts} +10 -8
  20. package/src/report.ts +60 -0
  21. package/src/results.ts +83 -0
  22. package/src/run-lifecycle.ts +117 -0
  23. package/src/{schema.d.ts → schema.ts} +29 -23
  24. package/src/source-inventory.ts +17 -0
  25. package/src/source-policy.ts +50 -0
  26. package/src/worktree.ts +322 -0
  27. package/tests/agent-contract.test.ts +180 -0
  28. package/tests/cli.test.ts +378 -0
  29. package/tests/fixtures/acp.mjs +38 -0
  30. package/tests/fixtures/ahp.mjs +50 -0
  31. package/tests/fixtures/answer.mjs +15 -0
  32. package/tests/fixtures/codex.mjs +44 -0
  33. package/tests/fixtures/repograph.ts +24 -0
  34. package/tests/graph.integration.test.ts +110 -0
  35. package/tests/helpers.ts +109 -0
  36. package/tests/refactor.test.ts +536 -0
  37. package/tests/run-lifecycle.test.ts +136 -0
  38. package/tests/tree.test.ts +210 -0
  39. package/tsconfig.json +12 -0
  40. package/LICENSE +0 -201
  41. package/bin/anureo-refactor.d.ts +0 -2
  42. package/bin/anureo-refactor.js +0 -4
  43. package/src/agent-work.d.ts +0 -41
  44. package/src/agent-work.js +0 -131
  45. package/src/checks.d.ts +0 -26
  46. package/src/checks.js +0 -66
  47. package/src/cli.d.ts +0 -27
  48. package/src/cli.js +0 -189
  49. package/src/client.d.ts +0 -26
  50. package/src/client.js +0 -73
  51. package/src/errors.d.ts +0 -9
  52. package/src/errors.js +0 -22
  53. package/src/git-path.d.ts +0 -15
  54. package/src/git-path.js +0 -46
  55. package/src/git-tree.d.ts +0 -18
  56. package/src/git-tree.js +0 -56
  57. package/src/git.d.ts +0 -23
  58. package/src/git.js +0 -40
  59. package/src/graph-artifacts.d.ts +0 -19
  60. package/src/graph-artifacts.js +0 -58
  61. package/src/graph.d.ts +0 -47
  62. package/src/graph.js +0 -71
  63. package/src/index.d.ts +0 -11
  64. package/src/index.js +0 -4
  65. package/src/options.d.ts +0 -55
  66. package/src/options.js +0 -15
  67. package/src/plan-validation.d.ts +0 -10
  68. package/src/plan-validation.js +0 -38
  69. package/src/process.js +0 -10
  70. package/src/prompts.d.ts +0 -47
  71. package/src/refactor.d.ts +0 -17
  72. package/src/refactor.js +0 -168
  73. package/src/report-store.d.ts +0 -9
  74. package/src/report.d.ts +0 -6
  75. package/src/report.js +0 -52
  76. package/src/results.d.ts +0 -80
  77. package/src/results.js +0 -0
  78. package/src/run-lifecycle.d.ts +0 -18
  79. package/src/run-lifecycle.js +0 -100
  80. package/src/schema.js +0 -31
  81. package/src/source-inventory.d.ts +0 -9
  82. package/src/source-inventory.js +0 -17
  83. package/src/source-policy.d.ts +0 -11
  84. package/src/source-policy.js +0 -56
  85. package/src/worktree.d.ts +0 -57
  86. package/src/worktree.js +0 -195
package/src/worktree.js DELETED
@@ -1,195 +0,0 @@
1
- // Materializes Git-visible source and transfers edits between independent worktrees.
2
- import { copyFile, cp, mkdir, mkdtemp, readlink, realpath, rm, symlink, writeFile } from 'node:fs/promises';
3
- import { tmpdir } from 'node:os';
4
- import { dirname, join, relative, sep } from 'node:path';
5
- import { inspectGitPath, isSnapshotPath } from "./git-path.js";
6
- import { fileFingerprint } from "./git-tree.js";
7
- import { git, gitFiles, gitProcessOptions } from "./git.js";
8
- import { exec } from "./process.js";
9
- /** Snapshot the index and Git-visible working files once before invoking a phase callback.
10
- * runDesign uses its snapshot for the design session and integration. refactorCode takes a new
11
- * one for planning, module work and ordered integration. The callback must await all worktree
12
- * use and removal before settling. Capture is sequential and assumes a stable source tree.
13
- * Returns the callback's result after scratch removal. Setup and callback failures reject, and
14
- * a scratch removal failure takes precedence. */
15
- export async function withWorktreeBaseline(cwd, use) {
16
- const root = await realpath((await git(cwd, 'rev-parse', '--show-toplevel')).replace(/\r?\n$/, ''));
17
- const scope = relative(root, cwd);
18
- const directory = await temporary('baseline');
19
- try {
20
- const baseline = await snapshotBaseline({ root, scope, directory });
21
- return await use(baseline);
22
- }
23
- finally {
24
- await rm(directory, { recursive: true, force: true });
25
- }
26
- }
27
- /** Register a detached checkout, restore the frozen index if present and copy the frozen working files.
28
- * Git skips checkout so hooks and filters cannot transform the copied files. inAgentWorktree passes
29
- * the scoped cwd to planning, and runAgentWork fingerprints the full root before starting the agent.
30
- * Both await removeAgentWorktree after use. Setup failures reject after attempting cleanup, and a
31
- * cleanup failure takes precedence. */
32
- export async function createAgentWorktree(baseline) {
33
- const directory = await temporary('agent');
34
- const root = join(directory, 'checkout');
35
- const worktree = { cwd: join(root, baseline.scope), root, directory };
36
- try {
37
- await baseline.updateWorktreeRegistry(() => git(baseline.root, '-c', 'core.hooksPath=' + join(directory, 'hooks'), 'worktree', 'add', '--detach', '--no-checkout', root, baseline.head));
38
- }
39
- catch (cause) {
40
- await rm(directory, { recursive: true, force: true });
41
- throw cause;
42
- }
43
- try {
44
- if (baseline.hasIndex) {
45
- const index = await gitIndexPath(root);
46
- await copyFile(join(baseline.directory, 'index'), index);
47
- }
48
- await cp(baseline.source, root, { recursive: true, verbatimSymlinks: true });
49
- return worktree;
50
- }
51
- catch (cause) {
52
- await removeAgentWorktree(baseline, worktree);
53
- throw cause;
54
- }
55
- }
56
- /** Release a checkout after the session and any patch capture settle. Unregister it through Git
57
- * before deleting its scratch directory. Failures reject with RESOURCE_RELEASE_FAILED and the
58
- * original cause. runAgentWork records the cleanup failure on the step, while inAgentWorktree
59
- * propagates it to planning. */
60
- export async function removeAgentWorktree(baseline, worktree) {
61
- try {
62
- await baseline.updateWorktreeRegistry(() => git(baseline.root, 'worktree', 'remove', '--force', worktree.root));
63
- await rm(worktree.directory, { recursive: true, force: true });
64
- }
65
- catch (cause) {
66
- throw Object.assign(new Error(`Could not remove refactor worktree at ${worktree.root}`, { cause }), {
67
- code: 'RESOURCE_RELEASE_FAILED',
68
- });
69
- }
70
- }
71
- /** Compare only the supplied scope-relative paths against the frozen baseline and return Git patch bytes.
72
- * For observed edits in edit mode, runAgentWork persists those bytes before removing the checkout,
73
- * even if the agent failed.
74
- * Requires a fresh checkout scratch directory for the before/after trees. Copy/diff failures and
75
- * empty output reject. The caller owns scratch cleanup through removeAgentWorktree. */
76
- export async function captureWorktreePatch(baseline, worktree, files) {
77
- const before = join(worktree.directory, 'before');
78
- const after = join(worktree.directory, 'after');
79
- const source = join(baseline.source, baseline.scope);
80
- await mkdir(before);
81
- await mkdir(after);
82
- for (const file of files) {
83
- await copyEntry(source, before, file);
84
- await copyEntry(worktree.cwd, after, file);
85
- }
86
- const patch = await diffPatchTrees(worktree.directory);
87
- if (!patch.length)
88
- throw new Error('Git could not represent the observed worktree changes');
89
- return patch;
90
- }
91
- /** Apply a saved patch to the scoped working files after checking the supplied paths against the
92
- * baseline, including ignored local paths. The caller must pair the patch with its observed path
93
- * list and coordinate writers through the check and apply. Fingerprint errors, mismatches and
94
- * Git failures reject. integrateAgentWork records failures on the step and only runs checks after
95
- * a successful apply to a successful step. */
96
- export async function applyWorktreePatch(baseline, files, patchPath) {
97
- const target = join(baseline.root, baseline.scope);
98
- const source = join(baseline.source, baseline.scope);
99
- for (const file of files) {
100
- if (await fileFingerprint(target, file) !== await fileFingerprint(source, file)) {
101
- throw new Error(`Source changed during refactor: ${file}`);
102
- }
103
- }
104
- const scope = baseline.scope ? ['--directory=' + baseline.scope.split(sep).join('/')] : [];
105
- await git(baseline.root, 'apply', '-p2', ...scope, '--whitespace=nowarn', patchPath);
106
- }
107
- /** Allocate a scratch directory under the canonical temporary root. Callers own removal. */
108
- async function temporary(kind) {
109
- return mkdtemp(join(await realpath(tmpdir()), `anureo-refactor-${kind}-`));
110
- }
111
- /** Populate the caller-owned scratch directory before exposing the baseline. Capture the index
112
- * before walking the working files, preserving Git's tracked and unignored untracked view.
113
- * Filesystem and Git failures reject so withWorktreeBaseline can remove partial snapshots. */
114
- async function snapshotBaseline({ root, scope, directory }) {
115
- const source = join(directory, 'source');
116
- await mkdir(join(source, scope), { recursive: true });
117
- const head = await baselineHead(root, directory);
118
- const index = await gitIndexPath(root);
119
- const hasIndex = await copyFile(index, join(directory, 'index')).then(() => true, (error) => {
120
- if (error.code === 'ENOENT')
121
- return false;
122
- throw error;
123
- });
124
- for (const file of await gitFiles(root)) {
125
- if (isSnapshotPath(file))
126
- await copyEntry(root, source, file);
127
- }
128
- return { root, scope, head, directory, source, hasIndex, updateWorktreeRegistry: createRegistryQueue() };
129
- }
130
- /** Resolve the index for the specific checkout, since linked worktrees have their own index paths.
131
- * Git failures reject. */
132
- async function gitIndexPath(cwd) {
133
- return (await git(cwd, 'rev-parse', '--path-format=absolute', '--git-path', 'index')).replace(/\r?\n$/, '');
134
- }
135
- /** Serialize worktree add and remove callbacks within one baseline. Give each caller its own
136
- * outcome while allowing later registry operations to proceed after a rejection. */
137
- function createRegistryQueue() {
138
- let registryReady = Promise.resolve();
139
- return update => {
140
- const result = registryReady.then(update);
141
- registryReady = result.then(() => { }, () => { });
142
- return result;
143
- };
144
- }
145
- /** Resolve HEAD for snapshotBaseline. If Git reports it missing, write an empty tree and a
146
- * synthetic commit to the repository object store so linked checkouts can detach at a commit.
147
- * Ref pointers stay in place. Other Git and filesystem failures reject. */
148
- async function baselineHead(root, directory) {
149
- try {
150
- return (await git(root, 'rev-parse', '--verify', '--quiet', 'HEAD')).trim();
151
- }
152
- catch (cause) {
153
- if (cause.code !== 1)
154
- throw cause;
155
- }
156
- const empty = join(directory, 'empty');
157
- await writeFile(empty, '');
158
- const tree = (await git(root, 'hash-object', '-w', '-t', 'tree', empty)).trim();
159
- return (await git(root, '-c', 'user.name=Anureo', '-c', 'user.email=anureo@localhost', '-c', 'commit.gpgSign=false', 'commit-tree', tree, '-m', 'Empty refactor baseline')).trim();
160
- }
161
- /** Copy a Git path into a caller-owned staging tree, skipping missing entries so deletions stay
162
- * visible. Preserve final symlink text and represent directory entries without recursing into
163
- * submodules. Linked ancestors, unsupported entry types and filesystem failures reject. */
164
- async function copyEntry(from, to, file) {
165
- const { path, info, linkedAncestor } = await inspectGitPath(from, file);
166
- if (linkedAncestor)
167
- throw new Error(`Symlink ancestors are unsupported: ${file}`);
168
- if (!info)
169
- return;
170
- const destination = join(to, file);
171
- await mkdir(dirname(destination), { recursive: true });
172
- if (info.isFile())
173
- await copyFile(path, destination);
174
- else if (info.isSymbolicLink())
175
- await symlink(await readlink(path), destination);
176
- else if (info.isDirectory())
177
- await mkdir(destination, { recursive: true, mode: info.mode });
178
- else
179
- throw new Error(`Unsupported Git path: ${file}`);
180
- }
181
- /** Diff the before/after trees for captureWorktreePatch. Git status 1 denotes a difference and
182
- * returns the buffered patch bytes. Other process failures propagate. */
183
- async function diffPatchTrees(cwd) {
184
- try {
185
- const { stdout } = await exec('git', ['diff', '--no-index', '--no-renames', '--no-ext-diff',
186
- '--no-textconv', '--no-color', '--binary', '--src-prefix=a/', '--dst-prefix=b/', '--', 'before', 'after'], { ...gitProcessOptions(), cwd, encoding: 'buffer' });
187
- return stdout;
188
- }
189
- catch (cause) {
190
- const error = cause;
191
- if (error.code === 1 && Buffer.isBuffer(error.stdout))
192
- return error.stdout;
193
- throw cause;
194
- }
195
- }