glm-coding-router 0.2.0 → 0.4.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.
@@ -0,0 +1,118 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import { runGit } from "./git.js";
4
+ import { Errors } from "./errors.js";
5
+ import { logger } from "./logging.js";
6
+ /** Branch prefix for delegate worktrees (specs/delegate-worktrees.md). */
7
+ export const DELEGATE_BRANCH_PREFIX = "glm/delegate/";
8
+ /** Valid delegate slugs: no path separators, spaces, or leading dash (specs/delegate-worktrees.md). */
9
+ export function validateDelegateName(name) {
10
+ if (!/^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(name) || name === "." || name === ".." || name.toLowerCase() === ".git") {
11
+ throw Errors.invalidDelegateName(name);
12
+ }
13
+ }
14
+ export function delegateBranch(name) {
15
+ return `${DELEGATE_BRANCH_PREFIX}${name}`;
16
+ }
17
+ /**
18
+ * Worktrees live outside the repo so the main checkout's status stays clean
19
+ * and no .gitignore edit is ever needed: <parent-of-root>/<repo>.glm-worktrees/<name>
20
+ */
21
+ export function worktreeBaseDir(repoRoot) {
22
+ return path.join(path.dirname(repoRoot), `${path.basename(repoRoot)}.glm-worktrees`);
23
+ }
24
+ export function delegateWorktreePath(repoRoot, name) {
25
+ return path.join(worktreeBaseDir(repoRoot), name);
26
+ }
27
+ function ok(result) {
28
+ return result.code === 0;
29
+ }
30
+ /** True when a local branch already exists (rev-parse --verify --quiet). */
31
+ export async function branchExists(repoRoot, branch, deps = {}) {
32
+ const run = deps.runGit ?? runGit;
33
+ const result = await run(["rev-parse", "--verify", "--quiet", `refs/heads/${branch}`], repoRoot);
34
+ return ok(result);
35
+ }
36
+ /** True when HEAD resolves (the repo has at least one commit). */
37
+ export async function headIsBorn(repoRoot, deps = {}) {
38
+ const run = deps.runGit ?? runGit;
39
+ const result = await run(["rev-parse", "--verify", "--quiet", "HEAD"], repoRoot);
40
+ return ok(result);
41
+ }
42
+ /**
43
+ * Pre-flight collision checks shared by the real run and --dry-run
44
+ * (specs/delegate-worktrees.md): fail before creating anything.
45
+ */
46
+ export async function assertWorktreeAvailable(repoRoot, name, deps = {}) {
47
+ const existsSync = deps.existsSync ?? fs.existsSync;
48
+ const branch = delegateBranch(name);
49
+ const worktreePath = delegateWorktreePath(repoRoot, name);
50
+ if (await branchExists(repoRoot, branch, deps)) {
51
+ throw Errors.worktreeFailed("branch", `branch ${branch} already exists`, [
52
+ "Inspect or merge it first:",
53
+ "",
54
+ ` git log ${branch}`,
55
+ ` git merge ${branch}`,
56
+ "",
57
+ `or delete it if it is unwanted:`,
58
+ "",
59
+ ` git branch -D ${branch}`,
60
+ ]);
61
+ }
62
+ if (existsSync(worktreePath)) {
63
+ throw Errors.worktreeFailed("worktree add", `path already exists: ${worktreePath}`, [
64
+ "A previous worktree directory is in the way:",
65
+ "",
66
+ ` ${worktreePath}`,
67
+ "",
68
+ "Remove it (or run `git worktree prune`) and re-run the delegate command.",
69
+ ]);
70
+ }
71
+ if (!(await headIsBorn(repoRoot, deps))) {
72
+ throw Errors.worktreeFailed("worktree add", "HEAD does not point at a commit (repository has no commits yet)", [
73
+ "delegate creates the worktree from HEAD, so the repository needs at least one commit first:",
74
+ "",
75
+ " git add -A && git commit -m \"initial commit\"",
76
+ ]);
77
+ }
78
+ return worktreePath;
79
+ }
80
+ /** Create the delegate worktree + branch from HEAD. Returns the worktree path. */
81
+ export async function createDelegateWorktree(repoRoot, name, deps = {}) {
82
+ const run = deps.runGit ?? runGit;
83
+ const worktreePath = await assertWorktreeAvailable(repoRoot, name, deps);
84
+ const branch = delegateBranch(name);
85
+ logger.debug(`git worktree add -b ${branch} ${worktreePath}`);
86
+ const result = await run(["worktree", "add", "-b", branch, worktreePath], repoRoot);
87
+ if (!ok(result)) {
88
+ throw Errors.worktreeFailed("worktree add", result.stderr || result.stdout, ["The worktree was not created; the repository was left as it was."]);
89
+ }
90
+ return worktreePath;
91
+ }
92
+ /**
93
+ * Remove a delegate worktree with plain `git worktree remove` — git refuses
94
+ * on dirty/untracked trees, which is exactly the safety we want
95
+ * (specs/delegate-worktrees.md, `--remove`). Never touches the branch.
96
+ * Returns undefined when removal succeeded, or git's complaint when not.
97
+ */
98
+ export async function removeDelegateWorktree(repoRoot, worktreePath, deps = {}) {
99
+ const run = deps.runGit ?? runGit;
100
+ const result = await run(["worktree", "remove", worktreePath], repoRoot);
101
+ if (ok(result)) {
102
+ return undefined;
103
+ }
104
+ return result.stderr || result.stdout || "git worktree remove failed";
105
+ }
106
+ /**
107
+ * Delete a branch this invocation created and never advanced (worker never
108
+ * started). Only ever called on the spawn-failure rollback path, where the
109
+ * branch provably sits at HEAD — never on branches a worker may have moved.
110
+ */
111
+ export async function rollbackDelegateBranch(repoRoot, branch, deps = {}) {
112
+ const run = deps.runGit ?? runGit;
113
+ const result = await run(["branch", "-D", branch], repoRoot);
114
+ if (ok(result)) {
115
+ return undefined;
116
+ }
117
+ return result.stderr || result.stdout || "git branch -D failed";
118
+ }
@@ -1,46 +1,46 @@
1
1
  /** Managed block content for AGENTS.md (spec §24). Keep in sync with the spec. */
2
- export const AGENTS_MANAGED_BLOCK = `<!-- glm-coding-router:start -->
3
-
4
- ## GLM Worker Delegation
5
-
6
- Available commands:
7
-
8
- - \`glm-worker "<task>"\`
9
- - \`glm-review "<task>"\`
10
-
11
- Codex is the primary orchestrator.
12
-
13
- Delegate:
14
- - CRUD
15
- - boilerplate
16
- - tests
17
- - documentation
18
- - mechanical refactoring
19
- - repository exploration
20
- - straightforward implementation
21
-
22
- Keep in Codex:
23
- - requirements
24
- - planning
25
- - architecture
26
- - ambiguous business logic
27
- - complex debugging
28
- - security decisions
29
- - integration
30
- - final review
31
-
32
- Before delegation create a task packet:
33
-
34
- TASK
35
- SCOPE
36
- FILES ALLOWED TO MODIFY
37
- FILES NOT TO MODIFY
38
- REQUIREMENTS
39
- CONSTRAINTS
40
- ACCEPTANCE CRITERIA
41
- VALIDATION
42
- EXPECTED OUTPUT
43
-
44
- Never trust a worker's success report without inspecting the resulting changes.
45
-
2
+ export const AGENTS_MANAGED_BLOCK = `<!-- glm-coding-router:start -->
3
+
4
+ ## GLM Worker Delegation
5
+
6
+ Available commands:
7
+
8
+ - \`glm-worker "<task>"\`
9
+ - \`glm-review "<task>"\`
10
+
11
+ Codex is the primary orchestrator.
12
+
13
+ Delegate:
14
+ - CRUD
15
+ - boilerplate
16
+ - tests
17
+ - documentation
18
+ - mechanical refactoring
19
+ - repository exploration
20
+ - straightforward implementation
21
+
22
+ Keep in Codex:
23
+ - requirements
24
+ - planning
25
+ - architecture
26
+ - ambiguous business logic
27
+ - complex debugging
28
+ - security decisions
29
+ - integration
30
+ - final review
31
+
32
+ Before delegation create a task packet:
33
+
34
+ TASK
35
+ SCOPE
36
+ FILES ALLOWED TO MODIFY
37
+ FILES NOT TO MODIFY
38
+ REQUIREMENTS
39
+ CONSTRAINTS
40
+ ACCEPTANCE CRITERIA
41
+ VALIDATION
42
+ EXPECTED OUTPUT
43
+
44
+ Never trust a worker's success report without inspecting the resulting changes.
45
+
46
46
  <!-- glm-coding-router:end -->`;
@@ -0,0 +1,70 @@
1
+ export const BENCHMARK_TASKS = [
2
+ {
3
+ id: "fn-reverse",
4
+ description: "implement reverseWords from a stub until node test.js passes",
5
+ files: {
6
+ "src/util.js": [
7
+ "// Implement reverseWords(str): reverse the ORDER of the words in str.",
8
+ '// Example: reverseWords("hello world") === "world hello".',
9
+ "// Collapse extra whitespace between words and trim the ends.",
10
+ "function reverseWords(str) {",
11
+ " // TODO: implement",
12
+ "}",
13
+ "",
14
+ "module.exports = { reverseWords };",
15
+ "",
16
+ ].join("\n"),
17
+ "test.js": [
18
+ 'const assert = require("node:assert");',
19
+ 'const { reverseWords } = require("./src/util.js");',
20
+ 'assert.strictEqual(reverseWords("hello world"), "world hello");',
21
+ 'assert.strictEqual(reverseWords("a"), "a");',
22
+ 'assert.strictEqual(reverseWords(" spaced out "), "out spaced");',
23
+ 'assert.strictEqual(reverseWords(""), "");',
24
+ 'console.log("PASS");',
25
+ "",
26
+ ].join("\n"),
27
+ },
28
+ prompt: [
29
+ "Implement reverseWords in src/util.js so that `node test.js` passes.",
30
+ "Words are separated by whitespace; collapse repeats and trim the ends.",
31
+ "Do not modify test.js. Verify by running `node test.js` with Bash.",
32
+ ].join(" "),
33
+ validate: ["node", "test.js"],
34
+ },
35
+ {
36
+ id: "fix-bug",
37
+ description: "repair an even-length median bug until node test.js passes",
38
+ files: {
39
+ "stats.js": [
40
+ "// median(values) returns the median of a non-empty list of numbers.",
41
+ "function median(values) {",
42
+ " const sorted = [...values].sort((a, b) => a - b);",
43
+ " const mid = Math.floor(sorted.length / 2);",
44
+ " return sorted[mid]; // BUG: wrong for even-length lists",
45
+ "}",
46
+ "",
47
+ "module.exports = { median };",
48
+ "",
49
+ ].join("\n"),
50
+ "test.js": [
51
+ 'const assert = require("node:assert");',
52
+ 'const { median } = require("./stats.js");',
53
+ "assert.strictEqual(median([3, 1, 2]), 2);",
54
+ "assert.strictEqual(median([4, 1, 3, 2]), 2.5);",
55
+ "assert.strictEqual(median([5]), 5);",
56
+ 'console.log("PASS");',
57
+ "",
58
+ ].join("\n"),
59
+ },
60
+ prompt: [
61
+ "stats.js has a bug: median() returns the wrong result for even-length lists.",
62
+ "Fix it so that `node test.js` passes (even lists return the average of the two middle values).",
63
+ "Do not modify test.js. Verify by running `node test.js` with Bash.",
64
+ ].join(" "),
65
+ validate: ["node", "test.js"],
66
+ },
67
+ ];
68
+ export function benchmarkTaskById(id) {
69
+ return BENCHMARK_TASKS.find((task) => task.id === id);
70
+ }
@@ -1,49 +1,49 @@
1
1
  /** Managed block content for CLAUDE.md (spec §20). Keep in sync with the spec. */
2
- export const CLAUDE_MANAGED_BLOCK = `<!-- glm-coding-router:start -->
3
-
4
- ## GLM Worker Delegation
5
-
6
- GLM workers available:
7
-
8
- - \`glm-worker "<task>"\`
9
- - \`glm-review "<task>"\`
10
-
11
- Delegate well-scoped, implementation-heavy work to GLM.
12
-
13
- Use GLM for:
14
- - repository exploration
15
- - CRUD
16
- - boilerplate
17
- - tests
18
- - documentation
19
- - mechanical refactoring
20
- - straightforward implementation
21
-
22
- Claude remains responsible for:
23
- - requirements
24
- - architecture
25
- - ambiguous business rules
26
- - security-sensitive decisions
27
- - complex debugging
28
- - integration
29
- - final review
30
-
31
- Before delegation, define:
32
- - task
33
- - scope
34
- - allowed files
35
- - forbidden files
36
- - requirements
37
- - constraints
38
- - acceptance criteria
39
- - validation command
40
- - expected output
41
-
42
- After worker completion:
43
- 1. inspect the actual diff
44
- 2. validate against requirements
45
- 3. run relevant tests
46
- 4. resolve integration problems
47
- 5. accept only after verification
48
-
2
+ export const CLAUDE_MANAGED_BLOCK = `<!-- glm-coding-router:start -->
3
+
4
+ ## GLM Worker Delegation
5
+
6
+ GLM workers available:
7
+
8
+ - \`glm-worker "<task>"\`
9
+ - \`glm-review "<task>"\`
10
+
11
+ Delegate well-scoped, implementation-heavy work to GLM.
12
+
13
+ Use GLM for:
14
+ - repository exploration
15
+ - CRUD
16
+ - boilerplate
17
+ - tests
18
+ - documentation
19
+ - mechanical refactoring
20
+ - straightforward implementation
21
+
22
+ Claude remains responsible for:
23
+ - requirements
24
+ - architecture
25
+ - ambiguous business rules
26
+ - security-sensitive decisions
27
+ - complex debugging
28
+ - integration
29
+ - final review
30
+
31
+ Before delegation, define:
32
+ - task
33
+ - scope
34
+ - allowed files
35
+ - forbidden files
36
+ - requirements
37
+ - constraints
38
+ - acceptance criteria
39
+ - validation command
40
+ - expected output
41
+
42
+ After worker completion:
43
+ 1. inspect the actual diff
44
+ 2. validate against requirements
45
+ 3. run relevant tests
46
+ 4. resolve integration problems
47
+ 5. accept only after verification
48
+
49
49
  <!-- glm-coding-router:end -->`;
@@ -1,68 +1,68 @@
1
1
  /** Codex skill definition (spec §26). Keep in sync with the spec. */
2
2
  export const GLM_DELEGATION_SKILL_NAME = "glm-delegation";
3
- export const GLM_DELEGATION_SKILL_MD = `---
4
- name: glm-delegation
5
- description: >
6
- Delegate well-scoped implementation, testing,
7
- repository exploration, boilerplate, CRUD,
8
- documentation, and mechanical refactoring to
9
- GLM Coding Plan workers.
10
- ---
11
-
12
- # GLM Delegation
13
-
14
- Available commands:
15
-
16
- glm-worker "<task>"
17
- glm-review "<task>"
18
-
19
- ## Use glm-review for
20
-
21
- - repository exploration
22
- - dependency analysis
23
- - locating implementations
24
- - call-chain discovery
25
- - code review
26
-
27
- ## Use glm-worker for
28
-
29
- - CRUD
30
- - unit tests
31
- - implementation
32
- - documentation
33
- - repetitive changes
34
- - mechanical refactoring
35
-
36
- ## Keep in primary Codex agent
37
-
38
- - requirements
39
- - architecture
40
- - ambiguous rules
41
- - security-sensitive design
42
- - difficult debugging
43
- - integration
44
- - final acceptance
45
-
46
- ## Delegation packet
47
-
48
- Always provide:
49
-
50
- TASK
51
- SCOPE
52
- ALLOWED FILES
53
- FORBIDDEN FILES
54
- REQUIREMENTS
55
- CONSTRAINTS
56
- ACCEPTANCE CRITERIA
57
- VALIDATION
58
- EXPECTED OUTPUT
59
-
60
- ## Verification
61
-
62
- After GLM finishes:
63
-
64
- - inspect the actual diff
65
- - independently run relevant validation
66
- - compare implementation with requirements
67
- - reject or correct worker output when needed
3
+ export const GLM_DELEGATION_SKILL_MD = `---
4
+ name: glm-delegation
5
+ description: >
6
+ Delegate well-scoped implementation, testing,
7
+ repository exploration, boilerplate, CRUD,
8
+ documentation, and mechanical refactoring to
9
+ GLM Coding Plan workers.
10
+ ---
11
+
12
+ # GLM Delegation
13
+
14
+ Available commands:
15
+
16
+ glm-worker "<task>"
17
+ glm-review "<task>"
18
+
19
+ ## Use glm-review for
20
+
21
+ - repository exploration
22
+ - dependency analysis
23
+ - locating implementations
24
+ - call-chain discovery
25
+ - code review
26
+
27
+ ## Use glm-worker for
28
+
29
+ - CRUD
30
+ - unit tests
31
+ - implementation
32
+ - documentation
33
+ - repetitive changes
34
+ - mechanical refactoring
35
+
36
+ ## Keep in primary Codex agent
37
+
38
+ - requirements
39
+ - architecture
40
+ - ambiguous rules
41
+ - security-sensitive design
42
+ - difficult debugging
43
+ - integration
44
+ - final acceptance
45
+
46
+ ## Delegation packet
47
+
48
+ Always provide:
49
+
50
+ TASK
51
+ SCOPE
52
+ ALLOWED FILES
53
+ FORBIDDEN FILES
54
+ REQUIREMENTS
55
+ CONSTRAINTS
56
+ ACCEPTANCE CRITERIA
57
+ VALIDATION
58
+ EXPECTED OUTPUT
59
+
60
+ ## Verification
61
+
62
+ After GLM finishes:
63
+
64
+ - inspect the actual diff
65
+ - independently run relevant validation
66
+ - compare implementation with requirements
67
+ - reject or correct worker output when needed
68
68
  `;
package/package.json CHANGED
@@ -1,47 +1,47 @@
1
- {
2
- "name": "glm-coding-router",
3
- "version": "0.2.0",
4
- "description": "GLM Coding Plan workers for Claude Code and Codex",
5
- "type": "module",
6
- "license": "MIT",
7
- "author": "hieu9721",
8
- "repository": {
9
- "type": "git",
10
- "url": "git+https://github.com/hieu9721/GLM-coding-router.git"
11
- },
12
- "bin": {
13
- "glm-router": "./dist/cli.js",
14
- "glm-chat": "./dist/bin/glm-chat.js",
15
- "glm-worker": "./dist/bin/glm-worker.js",
16
- "glm-review": "./dist/bin/glm-review.js",
17
- "glm-fast": "./dist/bin/glm-fast.js"
18
- },
19
- "files": [
20
- "dist"
21
- ],
22
- "scripts": {
23
- "dev": "tsx src/cli.ts",
24
- "build": "tsc",
25
- "test": "vitest run",
26
- "test:watch": "vitest",
27
- "lint": "eslint src tests",
28
- "prepublishOnly": "npm run build && npm test"
29
- },
30
- "engines": {
31
- "node": ">=20"
32
- },
33
- "dependencies": {
34
- "commander": "^15.0.0",
35
- "prompts": "^2.4.2",
36
- "zod": "^4.6.5"
37
- },
38
- "devDependencies": {
39
- "@types/node": "^22.20.3",
40
- "@types/prompts": "^2.4.9",
41
- "eslint": "^9.39.5",
42
- "tsx": "^4.23.13",
43
- "typescript": "^5.9.3",
44
- "typescript-eslint": "^8.70.0",
45
- "vitest": "^5.0.1"
46
- }
47
- }
1
+ {
2
+ "name": "glm-coding-router",
3
+ "version": "0.4.0",
4
+ "description": "GLM Coding Plan workers for Claude Code and Codex",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "author": "hieu9721",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/hieu9721/GLM-coding-router.git"
11
+ },
12
+ "bin": {
13
+ "glm-router": "./dist/cli.js",
14
+ "glm-chat": "./dist/bin/glm-chat.js",
15
+ "glm-worker": "./dist/bin/glm-worker.js",
16
+ "glm-review": "./dist/bin/glm-review.js",
17
+ "glm-fast": "./dist/bin/glm-fast.js"
18
+ },
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "scripts": {
23
+ "dev": "tsx src/cli.ts",
24
+ "build": "tsc",
25
+ "test": "vitest run",
26
+ "test:watch": "vitest",
27
+ "lint": "eslint src tests",
28
+ "prepublishOnly": "npm run build && npm test"
29
+ },
30
+ "engines": {
31
+ "node": ">=20"
32
+ },
33
+ "dependencies": {
34
+ "commander": "^15.0.0",
35
+ "prompts": "^2.4.2",
36
+ "zod": "^4.6.5"
37
+ },
38
+ "devDependencies": {
39
+ "@types/node": "^22.20.3",
40
+ "@types/prompts": "^2.4.9",
41
+ "eslint": "^9.39.5",
42
+ "tsx": "^4.23.13",
43
+ "typescript": "^5.9.3",
44
+ "typescript-eslint": "^8.70.0",
45
+ "vitest": "^5.0.1"
46
+ }
47
+ }