agentlane 0.0.0 → 0.4.0-rc.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 (146) hide show
  1. package/CHANGELOG.md +118 -0
  2. package/LICENSE +27 -0
  3. package/README.md +167 -2
  4. package/dist/agents/claude-plugin.js +79 -0
  5. package/dist/agents/cli.js +258 -0
  6. package/dist/agents/detect.js +88 -0
  7. package/dist/agents/init.js +251 -0
  8. package/dist/agents/launcher.js +32 -0
  9. package/dist/agents/package.js +119 -0
  10. package/dist/agents/skills.js +59 -0
  11. package/dist/agents/toml.js +325 -0
  12. package/dist/agents/wiring-check.js +117 -0
  13. package/dist/agents/wiring.js +374 -0
  14. package/dist/bench/host.js +104 -0
  15. package/dist/bench/live.js +511 -0
  16. package/dist/bench/mcp-client.js +91 -0
  17. package/dist/bench/model.js +15 -0
  18. package/dist/bench/report.js +162 -0
  19. package/dist/bench/run.js +121 -0
  20. package/dist/bench/schedule.js +140 -0
  21. package/dist/bench/stats.js +59 -0
  22. package/dist/bench-cli.js +167 -0
  23. package/dist/browser/aria.js +184 -0
  24. package/dist/browser/control.js +203 -0
  25. package/dist/browser/env.js +118 -0
  26. package/dist/browser/evidence.js +44 -0
  27. package/dist/browser/install.js +361 -0
  28. package/dist/browser/kinds.js +10 -0
  29. package/dist/browser/lane-browser.js +224 -0
  30. package/dist/browser/libraries.js +56 -0
  31. package/dist/browser/paths.js +29 -0
  32. package/dist/browser/release.js +121 -0
  33. package/dist/browser/screen.js +19 -0
  34. package/dist/browser/signin.js +154 -0
  35. package/dist/browser/stop.js +163 -0
  36. package/dist/browser/tools.js +25 -0
  37. package/dist/cache/build.js +154 -0
  38. package/dist/cache/config.js +186 -0
  39. package/dist/cache/index.js +4 -0
  40. package/dist/cache/key.js +196 -0
  41. package/dist/cache/store.js +303 -0
  42. package/dist/cli.js +178 -0
  43. package/dist/clock.js +4 -0
  44. package/dist/devices/adb.js +160 -0
  45. package/dist/devices/android.js +214 -0
  46. package/dist/devices/backend.js +7 -0
  47. package/dist/devices/bundler.js +46 -0
  48. package/dist/devices/common.js +51 -0
  49. package/dist/devices/controls.js +65 -0
  50. package/dist/devices/dial.js +56 -0
  51. package/dist/devices/emulator.js +396 -0
  52. package/dist/devices/hygiene.js +202 -0
  53. package/dist/devices/index.js +5 -0
  54. package/dist/devices/launch.js +76 -0
  55. package/dist/devices/simctl.js +119 -0
  56. package/dist/devices/tools.js +1 -0
  57. package/dist/devices/variant.js +99 -0
  58. package/dist/devices/xcrun.js +265 -0
  59. package/dist/driver/absence.js +54 -0
  60. package/dist/driver/caller.js +51 -0
  61. package/dist/driver/client.js +312 -0
  62. package/dist/driver/diff.js +180 -0
  63. package/dist/driver/elements.js +70 -0
  64. package/dist/driver/engine-main.js +13 -0
  65. package/dist/driver/engine.js +499 -0
  66. package/dist/driver/errors.js +11 -0
  67. package/dist/driver/exec.js +66 -0
  68. package/dist/driver/failure.js +156 -0
  69. package/dist/driver/flow.js +94 -0
  70. package/dist/driver/idle.js +100 -0
  71. package/dist/driver/images.js +68 -0
  72. package/dist/driver/install.js +272 -0
  73. package/dist/driver/lock.js +101 -0
  74. package/dist/driver/look.js +223 -0
  75. package/dist/driver/maestro-child.js +194 -0
  76. package/dist/driver/maestro-runs.js +46 -0
  77. package/dist/driver/paths.js +105 -0
  78. package/dist/driver/pixels.js +144 -0
  79. package/dist/driver/plan.js +108 -0
  80. package/dist/driver/release.js +7 -0
  81. package/dist/driver/runner.js +42 -0
  82. package/dist/driver/selector.js +91 -0
  83. package/dist/driver/session.js +474 -0
  84. package/dist/driver/suggest.js +86 -0
  85. package/dist/driver/supervisor.js +251 -0
  86. package/dist/driver/tree.js +426 -0
  87. package/dist/driver-cli.js +865 -0
  88. package/dist/evidence/device.js +177 -0
  89. package/dist/evidence/evidence.js +304 -0
  90. package/dist/evidence/files.js +92 -0
  91. package/dist/hooks/env.js +81 -0
  92. package/dist/hooks/run.js +101 -0
  93. package/dist/mcp/server.js +69 -0
  94. package/dist/mcp/tools.js +246 -0
  95. package/dist/name.js +22 -0
  96. package/dist/plugin/aims.js +465 -0
  97. package/dist/plugin/conventions.js +73 -0
  98. package/dist/plugin/guard.js +163 -0
  99. package/dist/plugin/hook-cli.js +216 -0
  100. package/dist/plugin/session-name.js +28 -0
  101. package/dist/plugin/shell.js +254 -0
  102. package/dist/processes.js +77 -0
  103. package/dist/project/config.js +316 -0
  104. package/dist/project/schema.js +335 -0
  105. package/dist/project/validate.js +224 -0
  106. package/dist/registry/config.js +142 -0
  107. package/dist/registry/format.js +22 -0
  108. package/dist/registry/owner.js +265 -0
  109. package/dist/registry/paths.js +50 -0
  110. package/dist/registry/ports.js +26 -0
  111. package/dist/registry/prune.js +160 -0
  112. package/dist/registry/refusal.js +53 -0
  113. package/dist/registry/registry.js +1027 -0
  114. package/dist/registry/resources.js +117 -0
  115. package/dist/registry/rows.js +115 -0
  116. package/dist/registry/status.js +85 -0
  117. package/dist/registry/store.js +274 -0
  118. package/dist/registry/types.js +1 -0
  119. package/dist/registry-cli.js +544 -0
  120. package/dist/services/services.js +268 -0
  121. package/dist/services/system.js +189 -0
  122. package/dist/up/deps.js +1 -0
  123. package/dist/up/doctor.js +424 -0
  124. package/dist/up/exec.js +133 -0
  125. package/dist/up/lane-files.js +75 -0
  126. package/dist/up/lane-services.js +38 -0
  127. package/dist/up/maestro-env.js +172 -0
  128. package/dist/up/preflight.js +232 -0
  129. package/dist/up/release.js +148 -0
  130. package/dist/up/resources.js +178 -0
  131. package/dist/up/restart.js +87 -0
  132. package/dist/up/setup.js +28 -0
  133. package/dist/up/up.js +610 -0
  134. package/dist/up-cli.js +258 -0
  135. package/dist/usage.js +16 -0
  136. package/package.json +58 -3
  137. package/schema/agentlane.schema.json +675 -0
  138. package/skills/create-verification-skill/LICENSE +21 -0
  139. package/skills/create-verification-skill/SKILL.md +88 -0
  140. package/skills/dev-loop/LICENSE +21 -0
  141. package/skills/dev-loop/SKILL.md +61 -0
  142. package/skills/device/SKILL.md +8 -0
  143. package/skills/drive/SKILL.md +64 -0
  144. package/skills/maintain-verification-skill/LICENSE +21 -0
  145. package/skills/maintain-verification-skill/SKILL.md +42 -0
  146. package/skills/setup/SKILL.md +80 -0
@@ -0,0 +1,119 @@
1
+ import { spawnSync } from 'node:child_process';
2
+ import fs from 'node:fs';
3
+ import path from 'node:path';
4
+ import { envName, TOOL } from "../name.js";
5
+ import { jsonObjectAt } from "./claude-plugin.js";
6
+ export const PACKAGE_MANAGERS = ['pnpm', 'yarn', 'bun', 'npm'];
7
+ export const PACKAGE_SPEC_ENV = envName('INIT_PACKAGE');
8
+ const LOCKFILES = [
9
+ ['pnpm-lock.yaml', 'pnpm'],
10
+ ['yarn.lock', 'yarn'],
11
+ ['bun.lock', 'bun'],
12
+ ['bun.lockb', 'bun'],
13
+ ['package-lock.json', 'npm'],
14
+ ['npm-shrinkwrap.json', 'npm'],
15
+ ];
16
+ const DEPENDENCY_FIELDS = ['dependencies', 'devDependencies', 'optionalDependencies'];
17
+ export function toolVersion() {
18
+ const manifest = JSON.parse(fs.readFileSync(new URL('../../package.json', import.meta.url), 'utf8'));
19
+ return manifest.version;
20
+ }
21
+ function isPackageManager(name) {
22
+ return PACKAGE_MANAGERS.includes(name);
23
+ }
24
+ function folders(from, top) {
25
+ const out = [from];
26
+ let dir = from;
27
+ while (dir !== top && path.dirname(dir) !== dir && dir.startsWith(`${top}${path.sep}`)) {
28
+ dir = path.dirname(dir);
29
+ out.push(dir);
30
+ }
31
+ return out;
32
+ }
33
+ function classicYarn(dir, field) {
34
+ if (field !== null)
35
+ return field.startsWith('yarn@1.');
36
+ try {
37
+ return fs.readFileSync(path.join(dir, 'yarn.lock'), 'utf8').slice(0, 200).includes('yarn lockfile v1');
38
+ }
39
+ catch {
40
+ return false;
41
+ }
42
+ }
43
+ export function packageManagerOf(packageDir, top, env) {
44
+ for (const dir of folders(packageDir, top)) {
45
+ const field = jsonObjectAt(path.join(dir, 'package.json'))?.packageManager;
46
+ if (typeof field === 'string') {
47
+ const name = field.split('@', 1)[0] ?? '';
48
+ if (isPackageManager(name))
49
+ return {
50
+ manager: name,
51
+ source: `the packageManager field in ${path.relative(packageDir, path.join(dir, 'package.json'))} says ${name}`,
52
+ classicYarn: classicYarn(dir, field),
53
+ };
54
+ }
55
+ for (const [lockfile, manager] of LOCKFILES)
56
+ if (fs.existsSync(path.join(dir, lockfile)))
57
+ return {
58
+ manager,
59
+ source: `${path.relative(packageDir, path.join(dir, lockfile))} is its lockfile`,
60
+ classicYarn: manager === 'yarn' && classicYarn(dir, null),
61
+ };
62
+ }
63
+ const agent = /^(\w+)\//.exec(env.npm_config_user_agent ?? '')?.[1] ?? '';
64
+ if (isPackageManager(agent))
65
+ return { manager: agent, source: `${agent} ran init`, classicYarn: false };
66
+ return { manager: 'npm', source: 'no lockfile or packageManager field names another', classicYarn: false };
67
+ }
68
+ export function addCommand(choice, packageDir, spec) {
69
+ const manifest = jsonObjectAt(path.join(packageDir, 'package.json'));
70
+ switch (choice.manager) {
71
+ case 'pnpm': {
72
+ const workspaceRoot = fs.existsSync(path.join(packageDir, 'pnpm-workspace.yaml'));
73
+ return { program: 'pnpm', args: ['add', '--save-dev', ...(workspaceRoot ? ['--workspace-root'] : []), spec] };
74
+ }
75
+ case 'yarn': {
76
+ const workspaceRoot = choice.classicYarn && manifest?.workspaces !== undefined;
77
+ return { program: 'yarn', args: ['add', '--dev', ...(workspaceRoot ? ['--ignore-workspace-root-check'] : []), spec] };
78
+ }
79
+ case 'bun':
80
+ return { program: 'bun', args: ['add', '--dev', spec] };
81
+ case 'npm':
82
+ return { program: 'npm', args: ['install', '--save-dev', spec] };
83
+ }
84
+ }
85
+ function quote(word) {
86
+ return /^[\w@%+=:,./-]+$/.test(word) ? word : `'${word.replaceAll("'", `'\\''`)}'`;
87
+ }
88
+ export function shellLine(command) {
89
+ return [command.program, ...command.args].map(quote).join(' ');
90
+ }
91
+ export function nearestPackage(root, top) {
92
+ for (const dir of folders(root, top))
93
+ if (fs.existsSync(path.join(dir, 'package.json')))
94
+ return dir;
95
+ return null;
96
+ }
97
+ export function listedRange(packageDir) {
98
+ const manifest = jsonObjectAt(path.join(packageDir, 'package.json'));
99
+ for (const field of DEPENDENCY_FIELDS) {
100
+ const deps = manifest?.[field];
101
+ if (typeof deps === 'object' && deps !== null && TOOL in deps) {
102
+ const range = deps[TOOL];
103
+ return typeof range === 'string' ? range : '';
104
+ }
105
+ }
106
+ return null;
107
+ }
108
+ export function installedVersion(packageDir) {
109
+ const version = jsonObjectAt(path.join(packageDir, 'node_modules', TOOL, 'package.json'))?.version;
110
+ return typeof version === 'string' ? version : null;
111
+ }
112
+ export const realInstaller = (command, cwd) => {
113
+ const result = spawnSync(command.program, command.args, { cwd, stdio: ['ignore', process.stderr, process.stderr] });
114
+ if (result.error)
115
+ return { ok: false, problem: result.error.message };
116
+ if (result.status !== 0)
117
+ return { ok: false, problem: result.signal ? `killed by ${result.signal}` : `exit ${String(result.status)}` };
118
+ return { ok: true };
119
+ };
@@ -0,0 +1,59 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { fileURLToPath } from 'node:url';
4
+ import { parse } from 'yaml';
5
+ import { CLI, FORMER, TOOL } from "../name.js";
6
+ export const SKILLS_DIR = fileURLToPath(new URL('../../skills/', import.meta.url));
7
+ const FRONTMATTER = /^---\n([\s\S]*?)\n---\n/;
8
+ export function parseSkill(text, file) {
9
+ const match = FRONTMATTER.exec(text);
10
+ if (!match?.[1])
11
+ throw new Error(`${file} has no frontmatter`);
12
+ const fields = parse(match[1]);
13
+ if (typeof fields?.name !== 'string' || typeof fields.description !== 'string')
14
+ throw new Error(`${file} needs a name and a description in its frontmatter`);
15
+ return {
16
+ name: fields.name,
17
+ description: fields.description,
18
+ frontmatter: match[1],
19
+ body: text.slice(match[0].length).replace(/^\n+/, ''),
20
+ };
21
+ }
22
+ export function listSkills(dir = SKILLS_DIR) {
23
+ return fs
24
+ .readdirSync(dir, { withFileTypes: true })
25
+ .filter(entry => entry.isDirectory() && fs.existsSync(path.join(dir, entry.name, 'SKILL.md')))
26
+ .map(entry => entry.name)
27
+ .sort()
28
+ .map(name => {
29
+ const file = path.join(dir, name, 'SKILL.md');
30
+ const skill = parseSkill(fs.readFileSync(file, 'utf8'), file);
31
+ if (skill.name !== name)
32
+ throw new Error(`${file} names itself ${skill.name}, not ${name}`);
33
+ return skill;
34
+ });
35
+ }
36
+ export function fetchCommand(name, cli = CLI) {
37
+ return `${cli} skills get ${name}`;
38
+ }
39
+ export const SETUP_SKILL = 'setup';
40
+ export function setupFix(byHand) {
41
+ return `${byHand}; or let an agent do it with the ${SETUP_SKILL} skill: /${TOOL}:${SETUP_SKILL} in Claude Code with the plugin, and in any agent, ask it to run '${fetchCommand(SETUP_SKILL)}' at the project root and follow what it prints`;
42
+ }
43
+ export function stubOf(skill) {
44
+ return `---
45
+ ${skill.frontmatter}
46
+ ---
47
+
48
+ Run \`${fetchCommand(skill.name)}\` from the project root and follow what it prints. It prints this skill for the version of the toolkit installed in this project.
49
+ `;
50
+ }
51
+ export function isStubOf(text, name) {
52
+ try {
53
+ const skill = parseSkill(text, name);
54
+ return skill.name === name && [CLI, FORMER.cli].some(cli => skill.body.includes(fetchCommand(name, cli)));
55
+ }
56
+ catch {
57
+ return false;
58
+ }
59
+ }
@@ -0,0 +1,325 @@
1
+ export class TomlError extends Error {
2
+ line;
3
+ constructor(line, message) {
4
+ super(`line ${String(line)}: ${message}`);
5
+ this.name = 'TomlError';
6
+ this.line = line;
7
+ }
8
+ }
9
+ const BARE = /[A-Za-z0-9_-]/;
10
+ const SCALAR = /[A-Za-z0-9_+\-.:]/;
11
+ class Scanner {
12
+ pos = 0;
13
+ line = 1;
14
+ text;
15
+ constructor(text) {
16
+ this.text = text;
17
+ }
18
+ peek(offset = 0) {
19
+ return this.text[this.pos + offset] ?? '';
20
+ }
21
+ startsWith(token) {
22
+ return this.text.startsWith(token, this.pos);
23
+ }
24
+ fail(message, line = this.line) {
25
+ throw new TomlError(line, message);
26
+ }
27
+ advance(count = 1) {
28
+ for (let i = 0; i < count; i++) {
29
+ if (this.text[this.pos] === '\n')
30
+ this.line++;
31
+ this.pos++;
32
+ }
33
+ }
34
+ spaces() {
35
+ while (this.peek() === ' ' || this.peek() === '\t')
36
+ this.advance();
37
+ }
38
+ comment() {
39
+ if (this.peek() !== '#')
40
+ return;
41
+ while (this.pos < this.text.length && this.peek() !== '\n')
42
+ this.advance();
43
+ }
44
+ newline() {
45
+ if (this.peek() === '\n') {
46
+ this.advance();
47
+ return true;
48
+ }
49
+ if (this.startsWith('\r\n')) {
50
+ this.advance(2);
51
+ return true;
52
+ }
53
+ return false;
54
+ }
55
+ gap() {
56
+ for (;;) {
57
+ this.spaces();
58
+ this.comment();
59
+ if (!this.newline())
60
+ return;
61
+ }
62
+ }
63
+ endOfLine() {
64
+ this.spaces();
65
+ this.comment();
66
+ if (this.pos < this.text.length && !this.newline())
67
+ this.fail(`unexpected '${this.peek()}' after a value`);
68
+ }
69
+ basic() {
70
+ this.advance();
71
+ let out = '';
72
+ for (;;) {
73
+ const c = this.peek();
74
+ if (c === '' || c === '\n')
75
+ this.fail('a string is not closed');
76
+ if (c === '"') {
77
+ this.advance();
78
+ return out;
79
+ }
80
+ if (c === '\\') {
81
+ out += this.peek(1);
82
+ this.advance(2);
83
+ continue;
84
+ }
85
+ out += c;
86
+ this.advance();
87
+ }
88
+ }
89
+ literal() {
90
+ this.advance();
91
+ const end = this.text.indexOf("'", this.pos);
92
+ const newline = this.text.indexOf('\n', this.pos);
93
+ if (end === -1 || (newline !== -1 && newline < end))
94
+ this.fail('a string is not closed');
95
+ const out = this.text.slice(this.pos, end);
96
+ this.advance(end - this.pos + 1);
97
+ return out;
98
+ }
99
+ multiline(quote) {
100
+ const start = this.line;
101
+ this.advance(3);
102
+ for (;;) {
103
+ if (this.pos >= this.text.length)
104
+ this.fail('a multi-line string is not closed', start);
105
+ if (quote === '"""' && this.peek() === '\\') {
106
+ this.advance(2);
107
+ continue;
108
+ }
109
+ if (this.startsWith(quote)) {
110
+ this.advance(3);
111
+ while (this.peek() === quote[0])
112
+ this.advance();
113
+ return;
114
+ }
115
+ this.advance();
116
+ }
117
+ }
118
+ key() {
119
+ const parts = [];
120
+ for (;;) {
121
+ this.spaces();
122
+ const c = this.peek();
123
+ if (c === '"')
124
+ parts.push(this.basic());
125
+ else if (c === "'")
126
+ parts.push(this.literal());
127
+ else if (BARE.test(c)) {
128
+ let part = '';
129
+ while (BARE.test(this.peek())) {
130
+ part += this.peek();
131
+ this.advance();
132
+ }
133
+ parts.push(part);
134
+ }
135
+ else
136
+ this.fail(c === '' ? 'a key is missing' : `'${c}' can't start a key`);
137
+ this.spaces();
138
+ if (this.peek() !== '.')
139
+ return parts;
140
+ this.advance();
141
+ }
142
+ }
143
+ value() {
144
+ const c = this.peek();
145
+ if (this.startsWith('"""') || this.startsWith("'''"))
146
+ this.multiline(this.text.slice(this.pos, this.pos + 3));
147
+ else if (c === '"')
148
+ this.basic();
149
+ else if (c === "'")
150
+ this.literal();
151
+ else if (c === '[')
152
+ this.array();
153
+ else if (c === '{')
154
+ this.inlineTable();
155
+ else if (SCALAR.test(c)) {
156
+ const start = this.pos;
157
+ while (SCALAR.test(this.peek()))
158
+ this.advance();
159
+ if (/^\d{4}-\d{2}-\d{2}$/.test(this.text.slice(start, this.pos)) && /^ \d{2}:/.test(this.text.slice(this.pos))) {
160
+ this.advance();
161
+ while (SCALAR.test(this.peek()))
162
+ this.advance();
163
+ }
164
+ }
165
+ else
166
+ this.fail(c === '' || c === '\n' ? 'a value is missing' : `'${c}' can't start a value`);
167
+ }
168
+ array() {
169
+ const start = this.line;
170
+ this.advance();
171
+ for (;;) {
172
+ this.gap();
173
+ if (this.peek() === ']') {
174
+ this.advance();
175
+ return;
176
+ }
177
+ if (this.pos >= this.text.length)
178
+ this.fail('an array is not closed', start);
179
+ this.value();
180
+ this.gap();
181
+ if (this.peek() === ',')
182
+ this.advance();
183
+ else if (this.peek() !== ']')
184
+ this.fail('an array is not closed', start);
185
+ }
186
+ }
187
+ inlineTable() {
188
+ this.advance();
189
+ for (;;) {
190
+ this.gap();
191
+ if (this.peek() === '}') {
192
+ this.advance();
193
+ return;
194
+ }
195
+ this.key();
196
+ if (this.peek() !== '=')
197
+ this.fail("a key in an inline table needs '='");
198
+ this.advance();
199
+ this.spaces();
200
+ this.value();
201
+ this.gap();
202
+ if (this.peek() === ',')
203
+ this.advance();
204
+ else if (this.peek() !== '}')
205
+ this.fail('an inline table is not closed');
206
+ }
207
+ }
208
+ outline() {
209
+ const headers = [];
210
+ const keys = [];
211
+ let table = [];
212
+ for (;;) {
213
+ this.gap();
214
+ if (this.pos >= this.text.length)
215
+ return { headers, keys };
216
+ const line = this.line;
217
+ if (this.peek() === '[') {
218
+ const array = this.peek(1) === '[';
219
+ this.advance(array ? 2 : 1);
220
+ table = this.key();
221
+ if (!this.startsWith(array ? ']]' : ']'))
222
+ this.fail('a table header is not closed');
223
+ this.advance(array ? 2 : 1);
224
+ headers.push({ path: table, array, line });
225
+ this.endOfLine();
226
+ continue;
227
+ }
228
+ const key = this.key();
229
+ if (this.peek() !== '=')
230
+ this.fail("a key needs '=' and a value");
231
+ this.advance();
232
+ this.spaces();
233
+ this.value();
234
+ keys.push({ path: [...table, ...key], table, line });
235
+ this.endOfLine();
236
+ }
237
+ }
238
+ }
239
+ export function outlineToml(text) {
240
+ return new Scanner(text).outline();
241
+ }
242
+ function startsWith(path, prefix) {
243
+ return prefix.every((part, i) => path[i] === part);
244
+ }
245
+ function same(a, b) {
246
+ return a.length === b.length && startsWith(a, b);
247
+ }
248
+ function bare(part) {
249
+ return /^[A-Za-z0-9_-]+$/.test(part) ? part : JSON.stringify(part);
250
+ }
251
+ function lines(block) {
252
+ return block
253
+ .split('\n')
254
+ .map(line => line.trim())
255
+ .filter(line => line !== '' && !line.startsWith('#'));
256
+ }
257
+ /**
258
+ * Sets one table, `[a.b]` with `body` under it, and leaves every other line of the file as it was. The table's own
259
+ * sub-tables (`[a.b.env]`) stay.
260
+ */
261
+ export function upsertTable(text, table, body) {
262
+ let outline;
263
+ try {
264
+ outline = outlineToml(text);
265
+ }
266
+ catch (error) {
267
+ return { kind: 'refused', reason: `it isn't valid TOML (${error.message})` };
268
+ }
269
+ const name = table.map(bare).join('.');
270
+ const ours = outline.headers.filter(h => same(h.path, table));
271
+ if (ours.length > 1 || ours.some(h => h.array))
272
+ return { kind: 'refused', reason: `it declares [${name}] more than once` };
273
+ const own = ours[0];
274
+ const next = own ? outline.headers.find(h => h.line > own.line) : undefined;
275
+ const foreign = outline.keys.find(k => (startsWith(k.path, table) || startsWith(table, k.path)) && !startsWith(k.table, table));
276
+ if (foreign)
277
+ return { kind: 'refused', reason: `line ${String(foreign.line)} sets ${foreign.path.join('.')} outside [${name}]` };
278
+ const all = text.split('\n');
279
+ const block = `[${name}]\n${body}`;
280
+ if (!own) {
281
+ const head = text === '' ? '' : text.endsWith('\n') ? `${text}\n` : `${text}\n\n`;
282
+ return { kind: 'changed', text: `${head}${block}\n` };
283
+ }
284
+ let end = next ? next.line - 1 : all.length;
285
+ while (end > own.line && /^\s*(#.*)?$/.test(all[end - 1] ?? ''))
286
+ end--;
287
+ const current = all.slice(own.line - 1, end).join('\n');
288
+ if (lines(current).join('\n') === lines(block).join('\n'))
289
+ return { kind: 'unchanged' };
290
+ return { kind: 'changed', text: [...all.slice(0, own.line - 1), ...block.split('\n'), ...all.slice(end)].join('\n') };
291
+ }
292
+ /** Removes one table, `[a.b]`, with its own sub-tables, and leaves every other line of the file as it was. */
293
+ export function removeTable(text, table) {
294
+ let outline;
295
+ try {
296
+ outline = outlineToml(text);
297
+ }
298
+ catch (error) {
299
+ return { kind: 'refused', reason: `it isn't valid TOML (${error.message})` };
300
+ }
301
+ const name = table.map(bare).join('.');
302
+ const ours = outline.headers.filter(h => startsWith(h.path, table));
303
+ const foreign = outline.keys.find(k => startsWith(k.path, table) && !startsWith(k.table, table));
304
+ if (foreign)
305
+ return { kind: 'refused', reason: `line ${String(foreign.line)} sets ${foreign.path.join('.')} outside [${name}]` };
306
+ if (ours.some(h => h.array))
307
+ return { kind: 'refused', reason: `it declares [[${name}]], an array of tables` };
308
+ if (ours.length === 0)
309
+ return { kind: 'unchanged' };
310
+ const all = text.split('\n');
311
+ const blank = (line) => line < 1 || line > all.length || (all[line - 1] ?? '').trim() === '';
312
+ const dropped = new Set();
313
+ for (const own of ours) {
314
+ const next = outline.headers.find(h => h.line > own.line);
315
+ let end = next ? next.line - 1 : all.length;
316
+ while (end > own.line && /^\s*(#.*)?$/.test(all[end - 1] ?? ''))
317
+ end--;
318
+ for (let line = own.line; line <= end; line++)
319
+ dropped.add(line);
320
+ if (blank(own.line - 1) && blank(end + 1) && end + 1 <= all.length)
321
+ dropped.add(end + 1);
322
+ }
323
+ const kept = all.filter((_, i) => !dropped.has(i + 1));
324
+ return { kind: 'changed', text: kept.join('\n').trim() === '' ? '' : kept.join('\n') };
325
+ }
@@ -0,0 +1,117 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { CLI, TOOL } from "../name.js";
4
+ import { AGENTS } from "../registry/owner.js";
5
+ import { CLAUDE_SETTINGS, describePluginSource, jsonObjectAt } from "./claude-plugin.js";
6
+ import { planInit } from "./init.js";
7
+ import { listSkills, SKILLS_DIR } from "./skills.js";
8
+ import { agentEdits, disablesOurServer, hasClaudeHooks, pluginEnabled } from "./wiring.js";
9
+ const SHARED = new Set(['.mcp.json']);
10
+ function holds(root, edit) {
11
+ if (edit.part === 'skills' || SHARED.has(edit.file))
12
+ return false;
13
+ let text;
14
+ try {
15
+ text = fs.readFileSync(path.join(root, edit.file), 'utf8');
16
+ }
17
+ catch {
18
+ return false;
19
+ }
20
+ if (edit.kind === 'toml')
21
+ return text.includes(`[${edit.table.join('.')}]`);
22
+ if (edit.kind !== 'json')
23
+ return false;
24
+ try {
25
+ return edit.present(JSON.parse(text));
26
+ }
27
+ catch {
28
+ return true;
29
+ }
30
+ }
31
+ export function agentsSetUp(root) {
32
+ return AGENTS.filter(agent => [...agentEdits(agent, false), ...(agent === 'claude' ? agentEdits(agent, true) : [])].some(edit => holds(root, edit)));
33
+ }
34
+ function problem(file) {
35
+ const parts = [...new Set(file.edits.map(e => e.part))].join(', ');
36
+ switch (file.status) {
37
+ case 'create':
38
+ return `missing ${file.file} (${parts})`;
39
+ case 'update':
40
+ return `stale ${file.file} (${parts})`;
41
+ case 'remove':
42
+ return `duplicate ${file.file} (${parts})`;
43
+ case 'refused':
44
+ return file.reason ?? file.file;
45
+ case 'unchanged':
46
+ return '';
47
+ }
48
+ }
49
+ function check(name, problems, fine, fix) {
50
+ return problems.length === 0
51
+ ? { name, ok: true, warning: false, detail: fine, fix: '' }
52
+ : { name, ok: true, warning: true, detail: problems.map(problem).join('; '), fix };
53
+ }
54
+ function claudeCopies(root, claude) {
55
+ const copies = [];
56
+ const settings = jsonObjectAt(path.join(root, CLAUDE_SETTINGS));
57
+ const servers = jsonObjectAt(path.join(root, '.mcp.json'))?.mcpServers;
58
+ if (typeof servers === 'object' && servers !== null && TOOL in servers && !(settings && disablesOurServer(settings)))
59
+ copies.push('MCP server in .mcp.json');
60
+ if (settings !== null && hasClaudeHooks(settings))
61
+ copies.push(`hooks in ${CLAUDE_SETTINGS}`);
62
+ const links = claude.filter(f => f.status === 'remove').length;
63
+ if (links > 0)
64
+ copies.push(`${String(links)} toolkit skill${links === 1 ? '' : 's'} in .claude/skills`);
65
+ return copies;
66
+ }
67
+ function leftToAbsentPlugin(root) {
68
+ const settings = jsonObjectAt(path.join(root, CLAUDE_SETTINGS));
69
+ return settings !== null && disablesOurServer(settings) && !pluginEnabled(settings);
70
+ }
71
+ function bothWired(source, copies) {
72
+ const listed = copies.length === 1 ? copies.join('') : `${copies.slice(0, -1).join(', ')} and ${copies.at(-1) ?? ''}`;
73
+ return `${describePluginSource(source)}, and the project's own ${listed} wire Claude Code a second time, so a session gets each of them twice`;
74
+ }
75
+ /** One line per agent the project has set up with `init`, plus one for the skill stubs. Nothing when none is set up. */
76
+ export function agentWiringChecks(root, env, skills = listSkills(SKILLS_DIR)) {
77
+ try {
78
+ const agents = agentsSetUp(root);
79
+ if (agents.length === 0)
80
+ return [];
81
+ const plan = planInit(root, agents, skills, env);
82
+ const off = plan.files.filter(f => f.status !== 'unchanged');
83
+ const fix = `${CLI} init --agents ${agents.join(',')}`;
84
+ const checks = [];
85
+ if (plan.files.some(f => f.edits.some(e => e.kind === 'stub')))
86
+ checks.push(check('wiring skills', off.filter(f => f.edits.every(e => e.agent === null)), `${String(skills.length)} skill stubs in .agents/skills`, fix));
87
+ for (const agent of agents) {
88
+ const mine = off.filter(f => f.edits.some(e => e.agent === agent));
89
+ const plugin = agent === 'claude' ? plan.claudePlugin : null;
90
+ const fine = plugin ? `through the plugin: ${describePluginSource(plugin)}` : 'MCP server and hooks in place';
91
+ const line = check(`wiring ${agent}`, mine, fine, `${CLI} init --agents ${agent}`);
92
+ const copies = plugin ? claudeCopies(root, mine) : [];
93
+ if (plugin && copies.length > 0)
94
+ checks.push({ ...line, detail: bothWired(plugin, copies) });
95
+ else if (agent === 'claude' && !plugin && line.warning && leftToAbsentPlugin(root))
96
+ checks.push({
97
+ ...line,
98
+ detail: `the project leaves Claude Code to the ${TOOL} plugin, and here no settings file enables it and no session loads it: ${line.detail}`,
99
+ fix: `load the plugin, or run ${CLI} init --agents claude to wire Claude Code without it`,
100
+ });
101
+ else
102
+ checks.push(line);
103
+ }
104
+ return checks;
105
+ }
106
+ catch (error) {
107
+ return [
108
+ {
109
+ name: 'wiring',
110
+ ok: true,
111
+ warning: true,
112
+ detail: `could not read the agents' files: ${error.message}`,
113
+ fix: `${CLI} init --check`,
114
+ },
115
+ ];
116
+ }
117
+ }