convergedev 0.0.0-stage → 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 (46) hide show
  1. package/LICENSE.md +34 -0
  2. package/THIRD_PARTY_NOTICES.md +149 -0
  3. package/dist/agent-execution.js +103 -0
  4. package/dist/agent-process.js +70 -0
  5. package/dist/agent.js +2 -0
  6. package/dist/anchors.js +101 -0
  7. package/dist/applications.js +69 -0
  8. package/dist/apply-proposal.js +236 -0
  9. package/dist/bridge-health.js +57 -0
  10. package/dist/bridge.js +203 -0
  11. package/dist/change-proposals.js +51 -0
  12. package/dist/claude.js +277 -0
  13. package/dist/cli.js +278 -0
  14. package/dist/commands.js +80 -0
  15. package/dist/comment-file.js +41 -0
  16. package/dist/comment-host.js +96 -0
  17. package/dist/config.js +43 -0
  18. package/dist/converge.js +36 -0
  19. package/dist/exec.js +52 -0
  20. package/dist/follow-branch.js +70 -0
  21. package/dist/git.js +169 -0
  22. package/dist/hunks.js +558 -0
  23. package/dist/implementation-results.js +98 -0
  24. package/dist/implementation-runner.js +262 -0
  25. package/dist/queries/ecmascript.scm +35 -0
  26. package/dist/queries/javascript.scm +6 -0
  27. package/dist/queries/python.scm +10 -0
  28. package/dist/queries/typescript.scm +16 -0
  29. package/dist/relay.js +32 -0
  30. package/dist/runner.js +104 -0
  31. package/dist/sandbox.js +53 -0
  32. package/dist/session-state.js +36 -0
  33. package/dist/setup.js +328 -0
  34. package/dist/share-review.js +34 -0
  35. package/dist/shared.js +193 -0
  36. package/dist/skill/converge/SKILL.md +25 -0
  37. package/dist/skill/converge/converge.mjs +35 -0
  38. package/dist/skill-link.js +100 -0
  39. package/dist/worktree.js +231 -0
  40. package/grammars/SOURCES.json +22 -0
  41. package/grammars/tree-sitter-javascript.wasm +0 -0
  42. package/grammars/tree-sitter-python.wasm +0 -0
  43. package/grammars/tree-sitter-tsx.wasm +0 -0
  44. package/grammars/tree-sitter-typescript.wasm +0 -0
  45. package/package.json +45 -4
  46. package/README.md +0 -3
@@ -0,0 +1,100 @@
1
+ // The /converge skill's link in Claude Code's skills folder (docs/review-room.md, decision 40): what is there now, and
2
+ // putting a link to this package there. Only a link Converge itself made is ever replaced, and a replacement can be
3
+ // undone at every step. Used by setup.ts (`converge` and `converge doctor`).
4
+ import { existsSync, lstatSync, mkdirSync, readFileSync, readlinkSync, realpathSync, renameSync, rmdirSync, unlinkSync } from 'node:fs';
5
+ import { dirname, join, resolve, sep } from 'node:path';
6
+ export const skillPath = (m) => join(m.claudeDir, 'skills', 'converge');
7
+ const isConvergeSkill = (dir) => {
8
+ try {
9
+ return /^name: converge$/m.test(readFileSync(join(dir, 'SKILL.md'), 'utf8')) && existsSync(join(dir, 'converge.mjs'));
10
+ }
11
+ catch {
12
+ return false;
13
+ }
14
+ };
15
+ /**
16
+ * Where Converge puts its skill: a package's dist/skill/converge, or a checkout's integrations/claude-code/skills/converge.
17
+ * A link is only ever taken for an earlier Converge when it was made to one of these, whatever it points at now.
18
+ */
19
+ const CONVERGE_SKILL_LOCATION = /(?:^|[\\/])(?:dist[\\/]skill|integrations[\\/]claude-code[\\/]skills)[\\/]converge[\\/]?$/;
20
+ /** Where the link at `path` was made to point, resolved as the system would; null if it can't be read. */
21
+ function linkDestination(path) {
22
+ try {
23
+ return resolve(dirname(path), readlinkSync(path).replace(/^\\\\\?\\/, ''));
24
+ }
25
+ catch {
26
+ return null;
27
+ }
28
+ }
29
+ /**
30
+ * What is at ~/.claude/skills/converge now. Only a link made to a Converge skill location is ever replaced: one that
31
+ * still holds a Converge skill (`other`), or one whose destination is gone (`broken`, Converge moved or uninstalled).
32
+ * Anything else, a broken link made elsewhere included, is `foreign` and left alone.
33
+ */
34
+ export function skillState(m) {
35
+ // npx runs the package from a cache npm may clear, so a link into it would break later (decision 40).
36
+ if (m.skillSource.includes(`${sep}_npx${sep}`))
37
+ return { kind: 'npx' };
38
+ const target = skillPath(m);
39
+ let link;
40
+ try {
41
+ link = lstatSync(target);
42
+ }
43
+ catch {
44
+ return { kind: 'missing' };
45
+ }
46
+ // A Windows junction is a link here too.
47
+ if (!link.isSymbolicLink())
48
+ return { kind: 'foreign', target };
49
+ const destination = linkDestination(target);
50
+ const madeByConverge = destination !== null && CONVERGE_SKILL_LOCATION.test(destination);
51
+ let real;
52
+ try {
53
+ real = realpathSync(target);
54
+ }
55
+ catch {
56
+ return madeByConverge ? { kind: 'broken', target: destination } : { kind: 'foreign', target };
57
+ }
58
+ if (real === realpathSync(m.skillSource))
59
+ return { kind: 'linked' };
60
+ return madeByConverge && isConvergeSkill(real) ? { kind: 'other', target: real } : { kind: 'foreign', target };
61
+ }
62
+ /** Removes a link only, never what it points at; a Windows junction is removed as a directory. */
63
+ function removeLink(path) {
64
+ try {
65
+ unlinkSync(path);
66
+ }
67
+ catch {
68
+ rmdirSync(path);
69
+ }
70
+ }
71
+ /**
72
+ * Links the skill, replacing an earlier Converge's link so it can be undone: the new link is made beside the old one
73
+ * first, the old one is moved aside, and the new one takes its name. If any step fails, the old link is put back as
74
+ * it was and the error is thrown.
75
+ */
76
+ export function linkSkill(m, replace) {
77
+ const target = skillPath(m);
78
+ mkdirSync(dirname(target), { recursive: true });
79
+ if (!replace)
80
+ return m.link(m.skillSource, target);
81
+ const fresh = `${target}.new-${process.pid}`;
82
+ const previous = `${target}.previous-${process.pid}`;
83
+ m.link(m.skillSource, fresh);
84
+ try {
85
+ renameSync(target, previous);
86
+ }
87
+ catch (err) {
88
+ removeLink(fresh);
89
+ throw err;
90
+ }
91
+ try {
92
+ renameSync(fresh, target);
93
+ }
94
+ catch (err) {
95
+ renameSync(previous, target);
96
+ removeLink(fresh);
97
+ throw err;
98
+ }
99
+ removeLink(previous);
100
+ }
@@ -0,0 +1,231 @@
1
+ // Isolated worktrees for implementation requests (docs/review-room.md, "Implementation requests"). A request's agent
2
+ // works in its own checkout of the pinned commit, under ~/.agent-review/worktrees/<room>/<request>, never in the
3
+ // developer's checkout: creating one adds a detached worktree and touches neither the checkout's files, its index,
4
+ // its branch nor its stash.
5
+ //
6
+ // Cleanup removes only what Converge can prove it made, and never deletes a folder's contents itself:
7
+ // - Before anything is created, an ownership marker `<room>/<request>.owner` is written exclusively, naming the
8
+ // repository and the process. A folder without its marker is never touched, whatever its name.
9
+ // - The worktrees folder, the room folder and the request folder must be real folders, not links, all the way down.
10
+ // - A registered worktree (its `.git` file and git's record point at each other) is removed by
11
+ // `git worktree remove`, which checks that link again as it deletes. A folder git never registered (creation
12
+ // interrupted first) is removed only if empty, with a non-recursive rmdir.
13
+ // - Anything else is left as it is, with its marker, and reported. A swap made between the check and the removal
14
+ // meets git's own check or rmdir's refusal of a link or a non-empty folder, so it fails rather than deletes.
15
+ import { lstat, mkdir, readdir, readFile, realpath, rmdir, unlink, writeFile } from 'node:fs/promises';
16
+ import { devNull } from 'node:os';
17
+ import { dirname, join, relative, resolve, sep } from 'node:path';
18
+ import { configPath } from './config.js';
19
+ import { git } from './git.js';
20
+ /** Room and request ids as the relay makes them (apps/api tokens.new_id); the same rule as config.ts parseRoomRef. */
21
+ const ID = /^[A-Za-z0-9_-]{1,64}$/;
22
+ /** A full commit id, SHA-1 or SHA-256 (apps/api implementation_context.FULL_SHA): never a name that could move. */
23
+ const FULL_SHA = /^([0-9a-f]{40}|[0-9a-f]{64})$/;
24
+ const MARKER = '.owner';
25
+ export const worktreesDir = () => join(dirname(configPath()), 'worktrees');
26
+ /** `message` is written for the member who made the request; the relay shows it as the request's reason. */
27
+ export class WorktreeError extends Error {
28
+ kind;
29
+ constructor(kind, message) {
30
+ super(message);
31
+ this.kind = kind;
32
+ }
33
+ }
34
+ const checkedId = (id) => {
35
+ if (!ID.test(id))
36
+ throw new WorktreeError('invalid', 'The request has an invalid id.');
37
+ return id;
38
+ };
39
+ export const worktreePath = (roomId, requestId) => join(worktreesDir(), checkedId(roomId), checkedId(requestId));
40
+ const markerPath = (roomId, requestId) => `${worktreePath(roomId, requestId)}${MARKER}`;
41
+ const notOwned = (path, why) => new WorktreeError('not-owned', `Left ${path} alone: ${why}`);
42
+ const exists = (path) => lstat(path).then(() => true, () => false);
43
+ /** The repository's top folder, or a WorktreeError when `dir` is not in one. */
44
+ async function topLevel(dir) {
45
+ try {
46
+ return await realpath((await git(dir, ['rev-parse', '--show-toplevel'])).trim());
47
+ }
48
+ catch {
49
+ throw new WorktreeError('not-a-repo', "Your agent's folder is not a git repository any more.");
50
+ }
51
+ }
52
+ const isInside = (parent, child) => {
53
+ const path = relative(parent, child);
54
+ return path !== '' && !path.startsWith('..') && !path.startsWith(sep) && !/^[A-Za-z]:/.test(path);
55
+ };
56
+ /**
57
+ * The room's folder, checked: the worktrees folder and the room folder are real folders (not links), and the room
58
+ * folder resolves to exactly where it should. Everything under it is then reached without passing through a link.
59
+ */
60
+ async function realRoomDir(roomId, create) {
61
+ const root = worktreesDir();
62
+ const roomDir = join(root, checkedId(roomId));
63
+ if (create)
64
+ await mkdir(roomDir, { recursive: true, mode: 0o700 });
65
+ for (const dir of [root, roomDir]) {
66
+ const stat = await lstat(dir);
67
+ if (stat.isSymbolicLink() || !stat.isDirectory())
68
+ throw notOwned(dir, 'it is a link or not a folder');
69
+ }
70
+ if (await realpath(roomDir) !== join(await realpath(root), roomId))
71
+ throw notOwned(roomDir, 'it resolves elsewhere');
72
+ return roomDir;
73
+ }
74
+ async function readMarker(path) {
75
+ const stat = await lstat(path).catch(() => undefined);
76
+ if (!stat?.isFile())
77
+ return undefined;
78
+ try {
79
+ const marker = JSON.parse(await readFile(path, 'utf8'));
80
+ return typeof marker?.request === 'string' && typeof marker.repo === 'string' && Number.isInteger(marker.pid) ? marker : undefined;
81
+ }
82
+ catch {
83
+ return undefined;
84
+ }
85
+ }
86
+ /**
87
+ * Whether `path` is a worktree of the repository whose common git folder is `commonDir`, as git links them: the
88
+ * folder's `.git` file names an entry under `<commonDir>/worktrees/`, and that entry's `gitdir` names the folder back.
89
+ */
90
+ async function isRegistered(path, commonDir) {
91
+ const dotGit = join(path, '.git');
92
+ if (!(await lstat(dotGit).catch(() => undefined))?.isFile())
93
+ return false;
94
+ const pointer = (await readFile(dotGit, 'utf8')).match(/^gitdir: (.+)$/m)?.[1]?.trim();
95
+ if (!pointer)
96
+ return false;
97
+ const entry = await realpath(resolve(path, pointer)).catch(() => '');
98
+ if (!isInside(join(commonDir, 'worktrees'), entry))
99
+ return false;
100
+ const back = (await readFile(join(entry, 'gitdir'), 'utf8').catch(() => '')).trim();
101
+ return !!back && await realpath(back).catch(() => '') === await realpath(dotGit);
102
+ }
103
+ /**
104
+ * A worktree of `repoDir` at `commit`, detached, for one request. Checks the commit is in the repository first.
105
+ * The marker and the folder are both created exclusively, so a second attempt at the same request (another process,
106
+ * a retry) fails before touching anything, and an existing folder is never reused. A creation that fails part-way
107
+ * is removed before the error is thrown. Git hooks do not run: the repository's own content may have installed them.
108
+ */
109
+ export async function createWorktree(opts) {
110
+ const { repoDir, roomId, requestId, commit } = opts;
111
+ if (!FULL_SHA.test(commit))
112
+ throw new WorktreeError('invalid', 'The request does not name a commit to work from.');
113
+ const path = worktreePath(roomId, requestId);
114
+ const top = await topLevel(repoDir);
115
+ if (isInside(top, path) || isInside(path, top) || path === top) {
116
+ throw new WorktreeError('invalid', "Converge's worktrees folder is inside this repository; move ~/.agent-review out of it.");
117
+ }
118
+ const found = await git(top, ['cat-file', '-e', `${commit}^{commit}`]).then(() => true, () => false);
119
+ if (!found) {
120
+ throw new WorktreeError('missing-commit', `Commit ${commit.slice(0, 7)} is not in your agent's checkout; fetch it, then prepare the request again.`);
121
+ }
122
+ const leftBehind = new WorktreeError('exists', 'An earlier attempt at this request left files behind; they are removed when your agent restarts.');
123
+ await realRoomDir(roomId, true);
124
+ const marker = { request: requestId, repo: top, pid: process.pid };
125
+ try {
126
+ await writeFile(markerPath(roomId, requestId), JSON.stringify(marker), { flag: 'wx', mode: 0o600 });
127
+ }
128
+ catch (err) {
129
+ if (err?.code === 'EEXIST')
130
+ throw leftBehind;
131
+ throw err;
132
+ }
133
+ try {
134
+ await mkdir(path, { mode: 0o700 });
135
+ }
136
+ catch (err) {
137
+ // The folder is not ours (it was there before the marker); only the marker just written is.
138
+ await unlink(markerPath(roomId, requestId));
139
+ if (err?.code === 'EEXIST')
140
+ throw leftBehind;
141
+ throw err;
142
+ }
143
+ try {
144
+ await git(top, ['-c', `core.hooksPath=${devNull}`, 'worktree', 'add', '--detach', '--', path, commit]);
145
+ const head = (await git(path, ['rev-parse', 'HEAD'])).trim();
146
+ if (head !== commit)
147
+ throw new Error(`worktree is at ${head}, not ${commit}`);
148
+ }
149
+ catch (err) {
150
+ await removeWorktree(repoDir, roomId, requestId).catch((cleanup) => console.warn(cleanup.message));
151
+ throw err;
152
+ }
153
+ return path;
154
+ }
155
+ /**
156
+ * Removes one request's worktree of `repoDir`, with whatever its agent left in it, then its marker. Throws a
157
+ * WorktreeError('not-owned') and leaves everything in place unless ownership is proven (see the top of this file).
158
+ */
159
+ export async function removeWorktree(repoDir, roomId, requestId) {
160
+ const roomDir = await realRoomDir(roomId, false);
161
+ const path = join(roomDir, checkedId(requestId));
162
+ const markerFile = `${path}${MARKER}`;
163
+ const top = await topLevel(repoDir);
164
+ const marker = await readMarker(markerFile);
165
+ if (!marker || marker.request !== requestId)
166
+ throw notOwned(path, 'Converge has no record of making it');
167
+ if (marker.repo !== top)
168
+ throw notOwned(path, `it belongs to ${marker.repo}`);
169
+ const stat = await lstat(path).catch(() => undefined);
170
+ if (stat) {
171
+ if (stat.isSymbolicLink() || !stat.isDirectory())
172
+ throw notOwned(path, 'it is a link or not a folder');
173
+ const commonDir = await realpath(resolve(top, (await git(top, ['rev-parse', '--git-common-dir'])).trim()));
174
+ if (await isRegistered(path, commonDir)) {
175
+ // Twice --force: the worktree is ours, changes its agent left are meant to go with it, and an interrupted
176
+ // `worktree add` leaves it locked. git checks the worktree's link again as it removes it.
177
+ await git(top, ['worktree', 'remove', '--force', '--force', '--', path]);
178
+ }
179
+ else if ((await readdir(path)).length === 0) {
180
+ // Creation stopped before git registered it. Non-recursive: anything put in it since makes this fail.
181
+ await rmdir(path);
182
+ }
183
+ else {
184
+ throw notOwned(path, 'git does not know it as a worktree and it is not empty');
185
+ }
186
+ }
187
+ if (await exists(path))
188
+ throw notOwned(path, 'it is still there');
189
+ await unlink(markerFile);
190
+ }
191
+ const alive = (pid) => {
192
+ try {
193
+ process.kill(pid, 0);
194
+ return true;
195
+ }
196
+ catch (err) {
197
+ return err?.code === 'EPERM';
198
+ }
199
+ };
200
+ /**
201
+ * Removes the room's worktrees left by runs that did not finish (the agent stopped, the machine slept). Only those
202
+ * with a marker are considered, and of those it skips the ones in `keep` (in use here, asked as each is reached, so
203
+ * a run that starts meanwhile is kept) and the ones another live process made (it may still be creating or using
204
+ * them). What cannot be proven Converge's is left alone and reported; the rest are still swept.
205
+ */
206
+ export async function sweepWorktrees(repoDir, roomId, keep = new Set()) {
207
+ let roomDir;
208
+ try {
209
+ roomDir = await realRoomDir(roomId, false);
210
+ }
211
+ catch (err) {
212
+ if (err?.code === 'ENOENT')
213
+ return;
214
+ console.warn(err.message);
215
+ return;
216
+ }
217
+ for (const entry of await readdir(roomDir)) {
218
+ const requestId = entry.endsWith(MARKER) ? entry.slice(0, -MARKER.length) : '';
219
+ if (!ID.test(requestId) || keep.has(requestId))
220
+ continue;
221
+ const marker = await readMarker(join(roomDir, entry));
222
+ if (marker && marker.pid !== process.pid && alive(marker.pid))
223
+ continue;
224
+ try {
225
+ await removeWorktree(repoDir, roomId, requestId);
226
+ }
227
+ catch (err) {
228
+ console.warn(err.message);
229
+ }
230
+ }
231
+ }
@@ -0,0 +1,22 @@
1
+ {
2
+ "tree-sitter-javascript.wasm": {
3
+ "package": "tree-sitter-javascript",
4
+ "version": "0.25.0",
5
+ "sha256": "5fb488d0cabb4775a594bab85682de5ad6ce83c0d6ac997a9f82dd084d571240"
6
+ },
7
+ "tree-sitter-typescript.wasm": {
8
+ "package": "tree-sitter-typescript",
9
+ "version": "0.23.2",
10
+ "sha256": "778025db5a8be0e70f8ccc3671e486dfeddd048c25d9e8a70c26de2e1bf6f97d"
11
+ },
12
+ "tree-sitter-tsx.wasm": {
13
+ "package": "tree-sitter-typescript",
14
+ "version": "0.23.2",
15
+ "sha256": "79e5da75ea62855a0cd67177685f0164eac87d5f630b3cbe1e0a099751ad30f8"
16
+ },
17
+ "tree-sitter-python.wasm": {
18
+ "package": "tree-sitter-python",
19
+ "version": "0.25.0",
20
+ "sha256": "16108b50df4ee9a30168794252ab55e7c93bfc5765d7fa0aa3e335752c515f47"
21
+ }
22
+ }
Binary file
Binary file
package/package.json CHANGED
@@ -1,6 +1,47 @@
1
1
  {
2
2
  "name": "convergedev",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Converge: review a branch or plan a task together in a shared room, each with your own Claude Code connected.",
5
+ "license": "UNLICENSED",
6
+ "type": "module",
7
+ "bin": {
8
+ "converge": "dist/converge.js"
9
+ },
10
+ "files": [
11
+ "dist",
12
+ "grammars/*.wasm",
13
+ "grammars/SOURCES.json",
14
+ "LICENSE.md",
15
+ "THIRD_PARTY_NOTICES.md"
16
+ ],
17
+ "engines": {
18
+ "node": ">=20"
19
+ },
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/dzeitgeist/converge.git",
23
+ "directory": "apps/bridge"
24
+ },
25
+ "scripts": {
26
+ "build": "node scripts/build.mjs",
27
+ "test": "node --import tsx --test src/*.test.ts",
28
+ "test:e2e": "AGENT_REVIEW_E2E=1 node --import tsx --test src/e2e.test.ts",
29
+ "typecheck": "tsc --noEmit",
30
+ "grammars": "node scripts/fetch-grammars.mjs",
31
+ "grammars:verify": "node scripts/fetch-grammars.mjs --verify",
32
+ "test:real-claude": "AGENT_REVIEW_REAL_CLAUDE=1 node --import tsx --test src/claude.real.test.ts"
33
+ },
34
+ "dependencies": {
35
+ "web-tree-sitter": "0.27.2",
36
+ "ws": "8.23.1"
37
+ },
38
+ "devDependencies": {
39
+ "@types/node": "^22.0.0",
40
+ "@types/ws": "^8.5.13",
41
+ "tsx": "^4.19.0",
42
+ "typescript": "^5.7.0"
43
+ },
44
+ "publishConfig": {
45
+ "access": "public"
46
+ }
47
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.