lean-agents 0.0.0-stage → 0.8.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.
- package/LICENSE +21 -0
- package/README.es.md +454 -0
- package/README.md +453 -2
- package/core/AGENTS.es.md +60 -0
- package/core/AGENTS.md +60 -0
- package/core/README.md +8 -0
- package/core/agents/debug.md +14 -0
- package/core/agents/explore.md +13 -0
- package/core/agents/planner.md +14 -0
- package/core/agents/research.md +13 -0
- package/core/agents/review.md +13 -0
- package/core/copilot-usage-tui.js +186 -0
- package/core/mcp.json +38 -0
- package/core/pins.json +30 -0
- package/core/tiers.json +4 -0
- package/node_modules/@clack/core/LICENSE +9 -0
- package/node_modules/@clack/core/README.md +22 -0
- package/node_modules/@clack/core/dist/index.d.mts +503 -0
- package/node_modules/@clack/core/dist/index.mjs +1003 -0
- package/node_modules/@clack/core/package.json +63 -0
- package/node_modules/@clack/prompts/LICENSE +9 -0
- package/node_modules/@clack/prompts/README.md +396 -0
- package/node_modules/@clack/prompts/dist/index.d.mts +949 -0
- package/node_modules/@clack/prompts/dist/index.mjs +1423 -0
- package/node_modules/@clack/prompts/package.json +67 -0
- package/node_modules/fast-string-truncated-width/dist/index.d.ts +4 -0
- package/node_modules/fast-string-truncated-width/dist/index.js +111 -0
- package/node_modules/fast-string-truncated-width/dist/types.d.ts +19 -0
- package/node_modules/fast-string-truncated-width/dist/types.js +2 -0
- package/node_modules/fast-string-truncated-width/dist/utils.d.ts +4 -0
- package/node_modules/fast-string-truncated-width/dist/utils.js +20 -0
- package/node_modules/fast-string-truncated-width/license +21 -0
- package/node_modules/fast-string-truncated-width/package.json +35 -0
- package/node_modules/fast-string-truncated-width/readme.md +59 -0
- package/node_modules/fast-string-width/dist/index.d.ts +4 -0
- package/node_modules/fast-string-width/dist/index.js +14 -0
- package/node_modules/fast-string-width/license +21 -0
- package/node_modules/fast-string-width/package.json +34 -0
- package/node_modules/fast-string-width/readme.md +42 -0
- package/node_modules/fast-wrap-ansi/LICENSE +23 -0
- package/node_modules/fast-wrap-ansi/README.md +26 -0
- package/node_modules/fast-wrap-ansi/lib/main.d.ts +6 -0
- package/node_modules/fast-wrap-ansi/lib/main.js +218 -0
- package/node_modules/fast-wrap-ansi/package.json +51 -0
- package/node_modules/jsonc-parser/CHANGELOG.md +76 -0
- package/node_modules/jsonc-parser/LICENSE.md +21 -0
- package/node_modules/jsonc-parser/README.md +364 -0
- package/node_modules/jsonc-parser/SECURITY.md +41 -0
- package/node_modules/jsonc-parser/lib/esm/impl/edit.js +185 -0
- package/node_modules/jsonc-parser/lib/esm/impl/format.js +261 -0
- package/node_modules/jsonc-parser/lib/esm/impl/parser.js +659 -0
- package/node_modules/jsonc-parser/lib/esm/impl/scanner.js +443 -0
- package/node_modules/jsonc-parser/lib/esm/impl/string-intern.js +29 -0
- package/node_modules/jsonc-parser/lib/esm/main.d.ts +351 -0
- package/node_modules/jsonc-parser/lib/esm/main.js +178 -0
- package/node_modules/jsonc-parser/lib/umd/impl/edit.js +201 -0
- package/node_modules/jsonc-parser/lib/umd/impl/format.js +275 -0
- package/node_modules/jsonc-parser/lib/umd/impl/parser.js +682 -0
- package/node_modules/jsonc-parser/lib/umd/impl/scanner.js +456 -0
- package/node_modules/jsonc-parser/lib/umd/impl/string-intern.js +42 -0
- package/node_modules/jsonc-parser/lib/umd/main.d.ts +351 -0
- package/node_modules/jsonc-parser/lib/umd/main.js +194 -0
- package/node_modules/jsonc-parser/package.json +37 -0
- package/node_modules/sisteransi/license +21 -0
- package/node_modules/sisteransi/package.json +34 -0
- package/node_modules/sisteransi/readme.md +113 -0
- package/node_modules/sisteransi/src/index.js +58 -0
- package/node_modules/sisteransi/src/sisteransi.d.ts +35 -0
- package/package.json +29 -4
- package/profiles/README.md +10 -0
- package/profiles/anthropic-api/gsd-defaults.json +26 -0
- package/profiles/anthropic-api.json +18 -0
- package/profiles/antigravity.json +12 -0
- package/profiles/claude-subscription/gsd-defaults.json +26 -0
- package/profiles/claude-subscription.json +17 -0
- package/profiles/github-copilot/gsd-defaults.json +26 -0
- package/profiles/github-copilot.json +17 -0
- package/profiles/openai-api/gsd-defaults.json +26 -0
- package/profiles/openai-api.json +17 -0
- package/profiles/opencode-go/gsd-defaults.json +26 -0
- package/profiles/opencode-go.json +17 -0
- package/profiles/vertex-gemini/gsd-defaults.json +26 -0
- package/profiles/vertex-gemini.json +18 -0
- package/scripts/adhd-skill.mjs +12 -0
- package/scripts/bump-pins.mjs +104 -0
- package/scripts/check-private.sh +52 -0
- package/scripts/check-readme.mjs +131 -0
- package/scripts/ci-download.sh +20 -0
- package/scripts/ci-install-opencode-v2.sh +6 -0
- package/scripts/ci-latest-report.mjs +38 -0
- package/scripts/copilot-context-mode.mjs +33 -0
- package/scripts/labels.sh +55 -0
- package/scripts/lean.mjs +700 -0
- package/scripts/lib/adapters/antigravity.mjs +72 -0
- package/scripts/lib/adapters/claude.mjs +122 -0
- package/scripts/lib/adapters/copilot-cli.mjs +81 -0
- package/scripts/lib/adapters/cursor.mjs +60 -0
- package/scripts/lib/adapters/opencode-v1.mjs +86 -0
- package/scripts/lib/adapters/opencode-v2.mjs +168 -0
- package/scripts/lib/adapters/vscode-copilot.mjs +56 -0
- package/scripts/lib/adhd.mjs +28 -0
- package/scripts/lib/adopt.mjs +474 -0
- package/scripts/lib/agent-lifecycle.mjs +163 -0
- package/scripts/lib/copilot-usage.mjs +49 -0
- package/scripts/lib/doctor.mjs +207 -0
- package/scripts/lib/feedback.mjs +30 -0
- package/scripts/lib/fsops.mjs +291 -0
- package/scripts/lib/gsd.mjs +42 -0
- package/scripts/lib/inventory.mjs +195 -0
- package/scripts/lib/lean-update.mjs +30 -0
- package/scripts/lib/migrate.mjs +407 -0
- package/scripts/lib/network.mjs +79 -0
- package/scripts/lib/node-runtime.mjs +85 -0
- package/scripts/lib/parallel.mjs +265 -0
- package/scripts/lib/pins.mjs +47 -0
- package/scripts/lib/project.mjs +38 -0
- package/scripts/lib/redact.mjs +30 -0
- package/scripts/lib/render.mjs +101 -0
- package/scripts/lib/system.mjs +178 -0
- package/scripts/lib/terminals.mjs +218 -0
- package/scripts/lib/ui.mjs +34 -0
- package/scripts/lib/unverified.mjs +49 -0
- package/scripts/lib/upgrade.mjs +130 -0
- package/scripts/lib/verify-commit.mjs +16 -0
- package/scripts/lib/verify-sha256.mjs +20 -0
- package/scripts/lint-workflows.mjs +25 -0
- package/scripts/models-refresh.mjs +58 -0
- package/scripts/parallel.mjs +59 -0
- package/scripts/pins-latest.mjs +43 -0
- package/scripts/release.mjs +46 -0
- package/scripts/verify-pins.mjs +36 -0
- package/scripts/version-watch.mjs +118 -0
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
// --install-agent / --uninstall-agent: opt-in install of an agent CLI and an undo that removes only what we recorded.
|
|
2
|
+
// Installer scripts are pinned in core/pins.json (url + sha256, checked by verify-sha256.mjs before they run).
|
|
3
|
+
// Install snapshots the install dirs before and after and records the new files with their sha256 in <state>/agents.json.
|
|
4
|
+
// Uninstall deletes a recorded file only while its sha256 still matches, or runs `npm uninstall -g` for npm installs.
|
|
5
|
+
// It never runs an agent's own uninstaller (`opencode uninstall` may delete user data) and never installs OpenCode 1.x (#79).
|
|
6
|
+
import fs from 'node:fs';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import { fileURLToPath } from 'node:url';
|
|
9
|
+
import { PINS, npmSpec } from './pins.mjs';
|
|
10
|
+
import { sha256 } from './verify-sha256.mjs';
|
|
11
|
+
import { run, which, version, antigravityPrereqs, packageManager } from './system.mjs';
|
|
12
|
+
import { writePrivate, privateDir } from './fsops.mjs';
|
|
13
|
+
|
|
14
|
+
const VERIFY = fileURLToPath(new URL('./verify-sha256.mjs', import.meta.url));
|
|
15
|
+
export const LIFECYCLE_AGENTS = ['claude', 'opencode-v2', 'copilot-cli', 'cursor', 'antigravity'];
|
|
16
|
+
|
|
17
|
+
// Pinned installer script: curl, verify, run. `args` go to the script (a version for Claude and OpenCode).
|
|
18
|
+
function script(pinKey, dir, args = []) {
|
|
19
|
+
const p = PINS[pinKey];
|
|
20
|
+
const file = path.join(dir, `${pinKey}.sh`);
|
|
21
|
+
return { version: null, download: file, run: [['curl', '-fsSL', '-o', file, p.url], [process.execPath, VERIFY, file, p.sha256], ['bash', file, ...args]] };
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// One row per agent for this OS: how it is installed, where its binary lands and which dirs the install may write to.
|
|
25
|
+
// `manual` is a reason, not a command: that agent/OS route is not proven yet (VERIFY in docs/compatibility.md).
|
|
26
|
+
export function agentTable({ home, platform, dir, realHome }) {
|
|
27
|
+
const posix = platform !== 'win32';
|
|
28
|
+
const cmds = { claude: 'claude', 'opencode-v2': 'opencode', 'copilot-cli': 'copilot', cursor: 'agent', antigravity: 'agy' };
|
|
29
|
+
const local = path.join(home, '.local');
|
|
30
|
+
const bin = (...n) => n.map((x) => path.join(local, 'bin', x));
|
|
31
|
+
const prefix = home === realHome ? [] : ['--prefix', local];
|
|
32
|
+
const t = {};
|
|
33
|
+
t.claude = posix
|
|
34
|
+
? { method: 'script', ...script('claude-installer', dir, [PINS['claude-code'].version]), bins: bin('claude'), dirs: [path.join(local, 'bin'), path.join(local, 'share', 'claude'), path.join(home, '.claude', 'downloads')] }
|
|
35
|
+
: { manual: 'Claude Code on Windows (claude.ai/install.ps1): not proven yet. VERIFY' };
|
|
36
|
+
t['opencode-v2'] = posix
|
|
37
|
+
// --no-modify-path: the installer must not edit ~/.bashrc, which we could not undo.
|
|
38
|
+
? { method: 'script', ...script('opencode-installer', dir, ['--version', PINS['opencode-v2'].version, '--no-modify-path']), bins: [path.join(home, '.opencode', 'bin', 'opencode')], dirs: [path.join(home, '.opencode')] }
|
|
39
|
+
: { manual: 'OpenCode 2 on Windows (Git Bash installer): install-dir and PATH not proven yet. VERIFY' };
|
|
40
|
+
t['copilot-cli'] = {
|
|
41
|
+
method: 'npm', version: PINS['copilot-cli'].version,
|
|
42
|
+
run: [['npm', 'install', '-g', ...prefix, npmSpec('copilot-cli')]], uninstall: [['npm', 'uninstall', '-g', ...prefix, PINS['copilot-cli'].source]],
|
|
43
|
+
bins: prefix.length ? (posix ? [path.join(local, 'bin', 'copilot')] : [path.join(local, 'copilot.cmd')]) : [], dirs: [],
|
|
44
|
+
};
|
|
45
|
+
t.cursor = posix
|
|
46
|
+
? { method: 'script', ...script('cursor-installer', dir), bins: bin('agent', 'cursor-agent'), dirs: [path.join(local, 'bin'), path.join(local, 'share', 'cursor-agent'), path.join(home, '.cursor')] }
|
|
47
|
+
: { manual: 'Cursor CLI on Windows (cursor.com/install): not proven yet. VERIFY' };
|
|
48
|
+
if (platform === 'darwin' && packageManager() === 'brew') {
|
|
49
|
+
// The engine's own Antigravity install (antigravityPrereqs); brew removes what it installed.
|
|
50
|
+
const c = antigravityPrereqs('brew', platform, home, false).find((x) => x.run.length);
|
|
51
|
+
t.antigravity = { method: 'brew', version: null, run: c?.run ?? [], uninstall: [['brew', 'uninstall', '--cask', 'antigravity-cli']], bins: [], dirs: [] };
|
|
52
|
+
} else {
|
|
53
|
+
t.antigravity = posix
|
|
54
|
+
? { method: 'script', ...script('antigravity-installer', dir), bins: bin('agy'), dirs: [path.join(local, 'bin'), path.join(local, 'share'), path.join(home, '.antigravity'), path.join(home, '.gemini')] }
|
|
55
|
+
: { manual: 'Antigravity CLI on Windows (Google.AntigravityCLI): no documented scope, install it from https://antigravity.google/docs/getting-started. VERIFY' };
|
|
56
|
+
}
|
|
57
|
+
for (const [a, row] of Object.entries(t)) row.cmd = cmds[a];
|
|
58
|
+
return t;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// {files: Map(path -> {sha256} | {link}), dirs: Set} of everything under the given roots.
|
|
62
|
+
export function snapshot(roots) {
|
|
63
|
+
const files = new Map();
|
|
64
|
+
const dirs = new Set();
|
|
65
|
+
const walk = (p) => {
|
|
66
|
+
const st = fs.lstatSync(p, { throwIfNoEntry: false });
|
|
67
|
+
if (!st) return;
|
|
68
|
+
if (st.isSymbolicLink()) files.set(p, { link: fs.readlinkSync(p) });
|
|
69
|
+
else if (st.isDirectory()) { dirs.add(p); for (const n of fs.readdirSync(p)) walk(path.join(p, n)); } else if (st.isFile()) files.set(p, { sha256: sha256(p) });
|
|
70
|
+
};
|
|
71
|
+
for (const r of roots) walk(r);
|
|
72
|
+
return { files, dirs };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const inside = (home, p) => { const r = path.relative(home, p); return r && !r.startsWith('..') && !path.isAbsolute(r); };
|
|
76
|
+
const same = (a, b) => a && b && a.sha256 === b.sha256 && a.link === b.link;
|
|
77
|
+
|
|
78
|
+
export function diffSnapshots(before, after) {
|
|
79
|
+
return {
|
|
80
|
+
files: [...after.files].filter(([p]) => !before.files.has(p)).map(([p, v]) => ({ path: p, ...v })),
|
|
81
|
+
dirs: [...after.dirs].filter((d) => !before.dirs.has(d)),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Removes the recorded files that still match, then the recorded dirs that are now empty. Returns [what, path] rows.
|
|
86
|
+
export function removeRecorded(home, rec) {
|
|
87
|
+
const rows = [];
|
|
88
|
+
for (const f of rec.files) {
|
|
89
|
+
if (!inside(home, f.path)) { rows.push(['skipped (outside home)', f.path]); continue; }
|
|
90
|
+
const st = fs.lstatSync(f.path, { throwIfNoEntry: false });
|
|
91
|
+
if (!st) { rows.push(['already gone', f.path]); continue; }
|
|
92
|
+
const now = st.isSymbolicLink() ? { link: fs.readlinkSync(f.path) } : st.isFile() ? { sha256: sha256(f.path) } : null;
|
|
93
|
+
if (!same(now, f)) { rows.push(['kept (changed)', f.path]); continue; }
|
|
94
|
+
fs.unlinkSync(f.path);
|
|
95
|
+
rows.push(['removed', f.path]);
|
|
96
|
+
}
|
|
97
|
+
for (const d of [...rec.dirs].sort((a, b) => b.length - a.length)) {
|
|
98
|
+
if (inside(home, d) && fs.existsSync(d) && !fs.readdirSync(d).length) fs.rmdirSync(d);
|
|
99
|
+
}
|
|
100
|
+
return rows;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const stateFile = (stateDir) => path.join(stateDir, 'agents.json');
|
|
104
|
+
const readAgents = (stateDir) => (fs.existsSync(stateFile(stateDir)) ? JSON.parse(fs.readFileSync(stateFile(stateDir), 'utf8')) : { agents: {} });
|
|
105
|
+
|
|
106
|
+
// o: { mode: 'install'|'uninstall', agents, home, realHome, platform, stateDir, dryRun, exec, yes, ask, yesNo }
|
|
107
|
+
export async function agentLifecycle(o) {
|
|
108
|
+
const log = console.log;
|
|
109
|
+
for (const a of o.agents) {
|
|
110
|
+
if (a === 'opencode-v1') throw new Error('opencode-v1 is never installed or removed by lean-agents (work PCs run 1.x on purpose, #79)');
|
|
111
|
+
if (!LIFECYCLE_AGENTS.includes(a)) throw new Error(`cannot ${o.mode} "${a}" (available: ${LIFECYCLE_AGENTS.join(', ')})`);
|
|
112
|
+
}
|
|
113
|
+
const state = readAgents(o.stateDir);
|
|
114
|
+
const table = o.table ?? agentTable({ home: o.home, platform: o.platform ?? process.platform, dir: o.stateDir, realHome: o.realHome });
|
|
115
|
+
const todo = [];
|
|
116
|
+
for (const a of o.agents) {
|
|
117
|
+
const row = table[a];
|
|
118
|
+
const mine = state.agents[a];
|
|
119
|
+
if (o.mode === 'install') {
|
|
120
|
+
if (mine) log(`${a}: already installed by lean-agents (${mine.method}); nothing to do.`);
|
|
121
|
+
else if (row.manual) log(`${a}: ${row.manual}`);
|
|
122
|
+
else if (row.bins.some((b) => fs.existsSync(b)) || ((o.home === o.realHome || row.method === 'brew') && which(row.cmd))) log(`${a}: already installed, not by lean-agents; left alone.`);
|
|
123
|
+
else if (a === 'opencode-v2' && version('opencode')?.startsWith('1.')) log('opencode-v2: OpenCode 1.x is on PATH; lean-agents never installs 2.x next to it here (#79). Nothing done.');
|
|
124
|
+
else todo.push(a);
|
|
125
|
+
} else if (!mine) log(`${a}: not installed by lean-agents; nothing done.`);
|
|
126
|
+
else todo.push(a);
|
|
127
|
+
}
|
|
128
|
+
if (!todo.length) return;
|
|
129
|
+
log(`\nlean-agents ${o.mode}-agent plan:`);
|
|
130
|
+
for (const a of todo) {
|
|
131
|
+
log(` - ${a}${o.mode === 'install' ? ` (${table[a].method}${table[a].version ? ` ${table[a].version}` : ''})` : ` (${state.agents[a].files.length} recorded files${state.agents[a].uninstall ? ', npm/brew uninstall' : ''})`}`);
|
|
132
|
+
for (const argv of o.mode === 'install' ? table[a].run : state.agents[a].uninstall ?? []) log(` $ ${argv.join(' ')}`);
|
|
133
|
+
}
|
|
134
|
+
if (o.dryRun || !o.exec) { log(o.dryRun ? 'Dry run: nothing was changed.' : '--no-exec: nothing was run.'); return; }
|
|
135
|
+
if (!o.yesNo(await o.ask(o.mode === 'install' ? 'Download and run these installers?' : 'Remove these agents?', o.yes ? 'y' : 'n'))) { log('Cancelled.'); return; }
|
|
136
|
+
privateDir(o.stateDir, o.stateDir);
|
|
137
|
+
fs.mkdirSync(o.stateDir, { recursive: true });
|
|
138
|
+
const env = {};
|
|
139
|
+
for (const a of todo) {
|
|
140
|
+
if (o.mode === 'install') {
|
|
141
|
+
const row = table[a];
|
|
142
|
+
const before = snapshot(row.dirs);
|
|
143
|
+
const ok = row.run.every((argv) => run(argv, { dryRun: false, log, env }));
|
|
144
|
+
if (row.download) fs.rmSync(row.download, { force: true });
|
|
145
|
+
const rec = diffSnapshots(before, snapshot(row.dirs));
|
|
146
|
+
if (!ok) {
|
|
147
|
+
log(` ! ${a}: install failed. Run the commands above by hand.`);
|
|
148
|
+
process.exitCode = 1;
|
|
149
|
+
// Keep what a half-run installer left, so --uninstall-agent can clean it.
|
|
150
|
+
if (!rec.files.length && !rec.dirs.length) continue;
|
|
151
|
+
}
|
|
152
|
+
state.agents[a] = { method: row.method, version: row.version, installedAt: new Date().toISOString(), uninstall: row.uninstall ?? null, ...rec };
|
|
153
|
+
log(`${a}: installed; ${rec.files.length} files recorded.`);
|
|
154
|
+
} else {
|
|
155
|
+
const rec = state.agents[a];
|
|
156
|
+
if (rec.uninstall && !rec.uninstall.every((argv) => run(argv, { dryRun: false, log, env }))) { log(` ! ${a}: uninstall failed. Run the command above by hand.`); process.exitCode = 1; continue; }
|
|
157
|
+
for (const [what, file] of removeRecorded(o.home, rec)) log(` ${what.padEnd(24)} ${file}`);
|
|
158
|
+
delete state.agents[a];
|
|
159
|
+
log(`${a}: removed. Your config and data were not touched.`);
|
|
160
|
+
}
|
|
161
|
+
writePrivate(stateFile(o.stateDir), `${JSON.stringify(state, null, 2)}\n`);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// One-line Copilot premium-request allowance from `gh api copilot_internal/user` (undocumented endpoint,
|
|
2
|
+
// the one VS Code's meter uses). Any failure is "unavailable", never zero. Dollars: 1 credit = $0.01, an estimate.
|
|
3
|
+
import { quiet } from './system.mjs';
|
|
4
|
+
import { OWNED_MARK } from './fsops.mjs';
|
|
5
|
+
|
|
6
|
+
const money = (credits) => `$${(credits / 100).toFixed(2)}`;
|
|
7
|
+
const n = (v) => String(Math.round(v));
|
|
8
|
+
|
|
9
|
+
export function formatUsage(user) {
|
|
10
|
+
const p = user?.quota_snapshots?.premium_interactions;
|
|
11
|
+
if (!p) return 'unavailable';
|
|
12
|
+
if (p.unlimited) return 'Unlimited premium requests';
|
|
13
|
+
if (![p.entitlement, p.remaining, p.percent_remaining].every(Number.isFinite)) return 'unavailable';
|
|
14
|
+
return `${n(p.remaining)} of ${n(p.entitlement)} credits left (${Math.floor(p.percent_remaining)}%), about ${money(p.remaining)} of ${money(p.entitlement)}, resets ${user.quota_reset_date}`;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// Claude Code status line: a standalone Node script built from the formatter above (same source, so it cannot drift).
|
|
18
|
+
// gh runs at most once per 5 minutes (cache in the temp dir); a failure prints `unavailable`, never zero.
|
|
19
|
+
export const statuslineScript = () => `// ${OWNED_MARK}
|
|
20
|
+
// Claude Code status line: Copilot credits left. One gh call (cached 5 minutes), never a model request.
|
|
21
|
+
import { execFileSync } from 'node:child_process';
|
|
22
|
+
import fs from 'node:fs';
|
|
23
|
+
import os from 'node:os';
|
|
24
|
+
import path from 'node:path';
|
|
25
|
+
|
|
26
|
+
const money = ${money};
|
|
27
|
+
const n = ${n};
|
|
28
|
+
${formatUsage}
|
|
29
|
+
|
|
30
|
+
const cache = path.join(os.tmpdir(), 'lean-copilot-usage-statusline.txt');
|
|
31
|
+
try {
|
|
32
|
+
if (Date.now() - fs.statSync(cache).mtimeMs < 300000) { process.stdout.write(fs.readFileSync(cache, 'utf8')); process.exit(0); }
|
|
33
|
+
} catch {}
|
|
34
|
+
let line;
|
|
35
|
+
try {
|
|
36
|
+
const out = execFileSync('gh', ['api', 'copilot_internal/user'], { timeout: 15000, encoding: 'utf8', windowsHide: true, shell: process.platform === 'win32', stdio: ['ignore', 'pipe', 'ignore'] });
|
|
37
|
+
line = 'Copilot: ' + formatUsage(JSON.parse(out)) + '\\n';
|
|
38
|
+
} catch { line = 'Copilot: ' + formatUsage(null) + '\\n'; }
|
|
39
|
+
// Shared /tmp: never follow a planted symlink; 'wx' (O_EXCL) refuses any existing path.
|
|
40
|
+
try { fs.rmSync(cache, { force: true }); fs.writeFileSync(cache, line, { flag: 'wx' }); } catch {}
|
|
41
|
+
process.stdout.write(line);
|
|
42
|
+
`;
|
|
43
|
+
|
|
44
|
+
// No leading slash: Git Bash rewrites it into a Windows path.
|
|
45
|
+
export function copilotUsage(run = quiet) {
|
|
46
|
+
const r = run(['gh', 'api', 'copilot_internal/user'], 15000);
|
|
47
|
+
if (!r.ok) return formatUsage(null);
|
|
48
|
+
try { return formatUsage(JSON.parse(r.out)); } catch { return formatUsage(null); }
|
|
49
|
+
}
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
// Doctor: compares what lean-agents installed with what is on disk and what the agents load.
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { sha } from './fsops.mjs';
|
|
5
|
+
import { PINS, npmSpec, behindPins, nodeRange } from './pins.mjs';
|
|
6
|
+
import { version, quiet } from './system.mjs';
|
|
7
|
+
import { satisfies, nodeSkipLine, minVersion } from './node-runtime.mjs';
|
|
8
|
+
import { mcpLaunches, classify, duplicateSkills, skillDirs } from './inventory.mjs';
|
|
9
|
+
import { stillNeeded, shellNotice } from './terminals.mjs';
|
|
10
|
+
import { adhdSkillFiles } from './adhd.mjs';
|
|
11
|
+
import { proxyReport, childEnv, fromPrivacy } from './network.mjs';
|
|
12
|
+
import { leftovers } from './parallel.mjs';
|
|
13
|
+
|
|
14
|
+
const CORE_AGENTS = ['debug', 'explore', 'planner', 'research', 'review'];
|
|
15
|
+
const SECRET_KEY = /(key|token|secret|password|auth)/i;
|
|
16
|
+
|
|
17
|
+
export function redact(value) {
|
|
18
|
+
if (Array.isArray(value)) return value.map(redact);
|
|
19
|
+
if (value && typeof value === 'object') {
|
|
20
|
+
return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, SECRET_KEY.test(k) ? '[redacted]' : redact(v)]));
|
|
21
|
+
}
|
|
22
|
+
return value;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// OpenCode 2 prints a JSON array of config sources ({type, path, info}); 1.x printed one object.
|
|
26
|
+
export function resolvedOpenCodeConfig(text) {
|
|
27
|
+
const start = text.search(/^[[{]/m);
|
|
28
|
+
const data = JSON.parse(text.slice(start).trim());
|
|
29
|
+
if (!Array.isArray(data)) return data;
|
|
30
|
+
const merged = {};
|
|
31
|
+
for (const src of data) if (src.type === 'document' && src.info) Object.assign(merged, src.info);
|
|
32
|
+
return merged;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function checkCopilot(o, add, q, withMcp) {
|
|
36
|
+
const mcp = withMcp ? q(['copilot', 'mcp', 'list', '--json']) : { ok: false, out: '' };
|
|
37
|
+
for (const s of ['serena', 'playwright', 'playwright-extension', 'context-mode'].filter((t) => withMcp && o.tools.includes(t))) {
|
|
38
|
+
add(`copilot mcp ${s}`, mcp.ok && mcp.out.includes(`"${s}"`) ? 'ok' : 'error', mcp.ok ? '' : 'copilot mcp list failed');
|
|
39
|
+
}
|
|
40
|
+
const rules = q(['copilot', 'instruction', 'list']);
|
|
41
|
+
add('copilot rules loaded', rules.ok && rules.out.includes('copilot-instructions.md') ? 'ok' : 'error');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function checkCursor(o, add, q) {
|
|
45
|
+
// `mcp list` starts every server, and Serena through uvx can take longer than 30s.
|
|
46
|
+
const mcp = q(['cursor-agent', 'mcp', 'list'], 120000);
|
|
47
|
+
for (const s of ['serena', 'playwright', 'playwright-extension', 'context-mode'].filter((t) => o.tools.includes(t))) {
|
|
48
|
+
add(`cursor mcp ${s}`, mcp.ok && mcp.out.includes(s) ? 'ok' : 'error', mcp.ok ? '' : 'cursor-agent mcp list failed');
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function checkAntigravity(o, core, add, q, withMcp) {
|
|
53
|
+
const agents = q(['agy', 'agents']).out;
|
|
54
|
+
add('agy subagents loaded', core.every((a) => agents.includes(a)) ? 'ok' : 'error');
|
|
55
|
+
const mcp = withMcp ? q(['agy', 'mcp', 'list']) : { ok: false, out: '' };
|
|
56
|
+
for (const s of ['serena', 'playwright'].filter((t) => withMcp && o.tools.includes(t))) {
|
|
57
|
+
add(`agy mcp ${s}`, mcp.ok && mcp.out.includes(s) ? 'ok' : 'error', mcp.ok ? '' : 'agy mcp list failed');
|
|
58
|
+
}
|
|
59
|
+
const plugins = q(['agy', 'plugin', 'list', '--json']).out;
|
|
60
|
+
for (const t of ['rtk'].filter((p) => o.tools.includes(p))) {
|
|
61
|
+
add(`agy plugin ${t}`, plugins.includes(`"${t}"`) ? 'ok' : 'error');
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Which Node runs each registered MCP command of a pinned tool, and skills that two folders of one agent both hold.
|
|
66
|
+
// Read-only, and it also covers machines lean-agents did not set up (#104 points 4-5, #132).
|
|
67
|
+
function checkInventory({ home, exec, run, env, platform, fsx }, add) {
|
|
68
|
+
const rel = (p) => (p && p.startsWith(home) ? `~${p.slice(home.length)}` : p);
|
|
69
|
+
const nodes = mcpLaunches(home, { env, platform, fsx }).map((m) => {
|
|
70
|
+
let nodeVersion = m.launch.nodeVersion;
|
|
71
|
+
// Only with exec: a symlink or PATH Node has no version in its path, so ask it.
|
|
72
|
+
if (!nodeVersion && exec && m.launch.node) nodeVersion = /\d+\.\d+\.\d+/.exec(run([m.launch.node, '--version'], 20000).out ?? '')?.[0] ?? null;
|
|
73
|
+
const launch = { ...m.launch, nodeVersion };
|
|
74
|
+
const v = classify(launch, m.pin);
|
|
75
|
+
const status = v.status === 'wrong-node' ? 'error' : v.status === 'unknown' ? 'note' : 'ok';
|
|
76
|
+
const where = `${m.client} mcp ${m.name}`;
|
|
77
|
+
const major = minVersion(v.range)[0];
|
|
78
|
+
const detail = v.status === 'wrong-node'
|
|
79
|
+
? `${m.pin} needs Node ${v.range} but this entry runs Node ${nodeVersion} (${launch.style}); run --update to register an absolute Node ${major}+ command (docs/manual-install.md)`
|
|
80
|
+
: v.status === 'unknown' ? `${m.pin} needs Node ${v.range}; the Node behind this ${launch.style} entry could not be read`
|
|
81
|
+
: `Node ${nodeVersion} (${launch.style})${v.fragile ? '; follows nvm use or PATH, so it can change' : ''}`;
|
|
82
|
+
add(`node for ${where}`, status, detail);
|
|
83
|
+
return { client: m.client, name: m.name, pin: m.pin, style: launch.style, node: rel(launch.node), nodeVersion, range: v.range, status };
|
|
84
|
+
});
|
|
85
|
+
const ORCA = new Set(['orca-cli', 'orchestration', 'computer-use']);
|
|
86
|
+
const bySkill = new Map();
|
|
87
|
+
for (const agent of ['claude', 'opencode', 'cursor', 'copilot']) {
|
|
88
|
+
for (const d of duplicateSkills(skillDirs(agent, home), fsx)) {
|
|
89
|
+
const k = `${d.name}\0${d.dirs.join('\0')}`;
|
|
90
|
+
bySkill.set(k, { ...(bySkill.get(k) ?? { name: d.name, dirs: d.dirs.map(rel), differs: d.differs, unreadable: d.unreadable, winner: d.winner, agents: [] }), agents: [...(bySkill.get(k)?.agents ?? []), agent] });
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const skills = [...bySkill.values()];
|
|
94
|
+
for (const s of skills) {
|
|
95
|
+
const orca = ORCA.has(s.name) ? ' Orca installs this skill in both folders on purpose.' : '';
|
|
96
|
+
add(`duplicate skill ${s.name}`, s.differs ? 'changed' : 'note',
|
|
97
|
+
`in ${s.dirs.join(' and ')} (read by ${s.agents.join(', ')}); ${s.unreadable ? 'a copy is unreadable, so they could not be compared' : `the copies ${s.differs ? 'differ' : 'are identical'}`}; winner: ${s.winner}. Keep one folder.${orca}`);
|
|
98
|
+
}
|
|
99
|
+
return { nodes, skills };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function doctor({ home, state, exec, nodeVersion = process.versions.node, json = false, run = quiet, env = process.env, platform = process.platform, fsx = fs }) {
|
|
103
|
+
const rows = [];
|
|
104
|
+
const add = (item, status, detail = '') => rows.push({ item, status, detail });
|
|
105
|
+
if (!state) {
|
|
106
|
+
add('lean-agents', 'missing', 'not installed (run the installer)');
|
|
107
|
+
} else {
|
|
108
|
+
const o = { ...state.options };
|
|
109
|
+
const network = o.network ?? fromPrivacy(o.privacy) ?? {};
|
|
110
|
+
const q = (argv, timeout) => run(argv, timeout, childEnv(network));
|
|
111
|
+
// `mcp list` starts every server (Serena through uvx git, Playwright), which is outbound traffic.
|
|
112
|
+
const withMcp = network.outbound !== 'off';
|
|
113
|
+
// A saved toolNode means Context Mode runs on its own Node, so the active one does not matter.
|
|
114
|
+
const nodeNote = o.tools.includes('context-mode') && !state.toolNode && !satisfies(nodeVersion, nodeRange('context-mode')) ? nodeSkipLine('context-mode', nodeRange('context-mode'), nodeVersion) : '';
|
|
115
|
+
if (nodeNote) o.tools = o.tools.filter((t) => t !== 'context-mode');
|
|
116
|
+
add('lean-agents', 'ok', `v${state.version}, agents: ${o.agents.join(', ')}, privacy: ${o.privacy}`);
|
|
117
|
+
if (nodeNote) add('context-mode', 'note', nodeNote);
|
|
118
|
+
if (o.agents.includes('opencode-v1')) add('OpenCode 1.x: supported; lean-agents will not upgrade it', 'ok');
|
|
119
|
+
const shell = shellNotice();
|
|
120
|
+
if (shell) add('shell', shell.ok ? 'ok' : 'note', shell.text);
|
|
121
|
+
if (o.terminals?.length) add('terminals', 'ok', `integrations for ${o.terminals.join(', ')} (commands listed below; app settings are yours)`);
|
|
122
|
+
for (const t of stillNeeded({ home, terminals: o.terminals ?? [], agents: o.agents, exec })) {
|
|
123
|
+
add('still needed', t.check?.() ? 'ok' : 'note', t.text);
|
|
124
|
+
}
|
|
125
|
+
const latest = [...new Map(state.files.map((f) => [f.path, f])).values()];
|
|
126
|
+
for (const f of latest) {
|
|
127
|
+
const rel = f.path.startsWith(home) ? `~${f.path.slice(home.length)}` : f.path;
|
|
128
|
+
if (!fs.existsSync(f.path)) { add(rel, 'error', 'missing'); continue; }
|
|
129
|
+
const same = sha(fs.readFileSync(f.path, 'utf8')) === f.hash;
|
|
130
|
+
add(rel, same ? 'ok' : 'changed', same ? '' : 'edited since install (fine if on purpose)');
|
|
131
|
+
}
|
|
132
|
+
// Drift: the installer pinned other versions than core/pins.json lists now (a newer lean-agents, or an edited pin).
|
|
133
|
+
const drift = Object.entries(state.pins ?? {}).filter(([k, v]) => PINS[k] && PINS[k].version !== v);
|
|
134
|
+
for (const [k, v] of drift) add(`pin ${k}`, 'changed', `installed ${v}, core/pins.json has ${PINS[k].version} (run --update)`);
|
|
135
|
+
if (state.pins && !drift.length) add('pinned versions', 'ok', `${Object.keys(state.pins).length} pins match core/pins.json`);
|
|
136
|
+
for (const c of state.commands) add(c.desc, c.ok ? 'ok' : 'error', c.ok ? '' : 'failed at install time');
|
|
137
|
+
if (o.tools.includes('i-have-adhd')) {
|
|
138
|
+
const reset = adhdSkillFiles(home).filter((f) => fs.readFileSync(f, 'utf8').includes('disable-model-invocation: true'));
|
|
139
|
+
if (reset.length) add('i-have-adhd skill', 'changed', 'a plugin update reset it; run --update');
|
|
140
|
+
}
|
|
141
|
+
if (exec) {
|
|
142
|
+
for (const [bin, tool] of [['claude', 'claude'], ['opencode', 'opencode-v1'], ['opencode', 'opencode-v2'], ['copilot', 'copilot-cli'], ['agy', 'antigravity'], ['rtk', 'rtk'], ['context-mode', 'context-mode'], ['uvx', 'serena']]) {
|
|
143
|
+
const needed = o.agents.includes(tool) || o.tools.includes(tool);
|
|
144
|
+
if (!needed) continue;
|
|
145
|
+
const v = version(bin, undefined, childEnv(network));
|
|
146
|
+
add(`${bin} on PATH`, v ? 'ok' : 'error', v ?? 'not found');
|
|
147
|
+
}
|
|
148
|
+
for (const [bin, agent] of [['code', 'vscode-copilot'], ['cursor-agent', 'cursor']]) {
|
|
149
|
+
if (!o.agents.includes(agent)) continue;
|
|
150
|
+
const v = version(bin, undefined, childEnv(network));
|
|
151
|
+
add(`${bin} on PATH`, v ? 'ok' : 'error', v ?? 'not found');
|
|
152
|
+
}
|
|
153
|
+
if (!withMcp && ['claude', 'cursor', 'copilot-cli', 'antigravity'].some((a) => o.agents.includes(a))) add('mcp servers', 'skipped', 'network off (outbound off): `mcp list` not run');
|
|
154
|
+
if (o.agents.includes('cursor') && withMcp) checkCursor(o, add, q);
|
|
155
|
+
if (o.agents.includes('claude') && withMcp) {
|
|
156
|
+
const mcp = q(['claude', 'mcp', 'list']);
|
|
157
|
+
for (const s of ['serena', 'playwright'].filter((t) => o.tools.includes(t))) {
|
|
158
|
+
add(`claude mcp ${s}`, mcp.ok && mcp.out.includes(s) ? 'ok' : 'error', mcp.ok ? '' : 'claude mcp list failed');
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
if (o.agents.some((a) => a.startsWith('opencode'))) checkOpenCode(o, add, q);
|
|
162
|
+
if (o.agents.includes('copilot-cli')) checkCopilot(o, add, q, withMcp);
|
|
163
|
+
if (o.agents.includes('antigravity')) checkAntigravity(o, CORE_AGENTS, add, q, withMcp);
|
|
164
|
+
// npx may have to download, which takes longer than a local check.
|
|
165
|
+
if (o.tools.includes('codeburn')) add('codeburn runs', q(['npx', '-y', npmSpec('codeburn'), '--version'], 120000).ok ? 'ok' : 'error');
|
|
166
|
+
if (o.tools.includes('autoskills')) add('autoskills runs', q(['npx', '-y', npmSpec('autoskills'), '--help'], 120000).ok ? 'ok' : 'error');
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
const { nodes, skills } = checkInventory({ home, exec, run, env, platform, fsx }, add);
|
|
170
|
+
const left = leftovers(process.cwd());
|
|
171
|
+
if (left.length) add('parallel workers', 'changed', `left in this repo: ${left.join(', ')} (merge them, then run scripts/parallel.mjs clean)`);
|
|
172
|
+
for (const line of proxyReport()) add('network env', 'ok', line);
|
|
173
|
+
const ok = !rows.some((r) => r.status === 'error' || r.status === 'missing');
|
|
174
|
+
const behind = state ? behindPins(state) : [];
|
|
175
|
+
if (json) process.stdout.write(`${JSON.stringify({ ok, behind, nodes, skills, rows }, null, 2)}\n`);
|
|
176
|
+
else print(rows);
|
|
177
|
+
return ok;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function checkOpenCode(o, add, q) {
|
|
181
|
+
const r = q(['opencode', 'debug', 'config']);
|
|
182
|
+
if (!r.ok) return add('opencode resolved config', 'error', '`opencode debug config` failed');
|
|
183
|
+
let cfg;
|
|
184
|
+
try { cfg = resolvedOpenCodeConfig(r.out); } catch { return add('opencode resolved config', 'changed', 'could not parse output; check by hand'); }
|
|
185
|
+
cfg = redact(cfg);
|
|
186
|
+
const servers = cfg.mcp?.servers ?? cfg.mcp ?? {};
|
|
187
|
+
const v1 = o.agents.includes('opencode-v1');
|
|
188
|
+
for (const s of ['context-mode', 'serena', 'playwright', 'playwright-extension'].filter((t) => o.tools.includes(t) && !(v1 && t === 'context-mode'))) {
|
|
189
|
+
add(`opencode mcp ${s}`, servers[s] ? 'ok' : 'error', servers[s] ? '' : 'not in resolved config');
|
|
190
|
+
}
|
|
191
|
+
const plugins = [...(cfg.plugins ?? []), ...(cfg.plugin ?? [])].map(String);
|
|
192
|
+
if (v1 && o.tools.includes('context-mode')) {
|
|
193
|
+
const loaded = plugins.some((p) => p.includes('context-mode'));
|
|
194
|
+
add('opencode plugin context-mode', loaded ? 'ok' : 'error', loaded ? '' : 'not in resolved config');
|
|
195
|
+
}
|
|
196
|
+
if (plugins.some((p) => p.includes('context-mode')) && servers['context-mode']) {
|
|
197
|
+
add('context-mode duplicate', 'error', 'enabled as plugin and MCP; keep one');
|
|
198
|
+
}
|
|
199
|
+
if (plugins.some((p) => p.includes('oh-my-openagent'))) add('oh-my-openagent', 'error', 'present; lean-agents does not support it');
|
|
200
|
+
const instr = (cfg.instructions ?? []).some((i) => String(i).includes('lean-agents'));
|
|
201
|
+
add('opencode rules loaded', instr ? 'ok' : 'error', instr ? '' : 'instructions entry missing');
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function print(rows) {
|
|
205
|
+
const w = Math.min(48, Math.max(...rows.map((r) => r.item.length)));
|
|
206
|
+
for (const r of rows) console.log(`${r.status.padEnd(8)} ${r.item.padEnd(w)} ${r.detail}`);
|
|
207
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// Opt-in feedback after a failed install: a redacted issue draft the agent shows before anything is filed.
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import { redactText } from './redact.mjs';
|
|
4
|
+
|
|
5
|
+
// Filing an issue is an outbound call, so only the `outbound` category decides (strict turns it off).
|
|
6
|
+
export const feedbackAllowed = (network) => network?.outbound === 'on';
|
|
7
|
+
|
|
8
|
+
// Returns null when the preset forbids it. Everything free-form goes through redactText.
|
|
9
|
+
export function draftIssue({ network, home, env = process.env, versions = {}, command, error, kind = 'install', platform = `${os.platform()} ${os.arch()}` }) {
|
|
10
|
+
if (!feedbackAllowed(network)) return null;
|
|
11
|
+
const clean = (t) => redactText(t, { home, env });
|
|
12
|
+
const update = kind === 'update';
|
|
13
|
+
const versionLines = Object.entries(versions).map(([k, v]) => `- ${k}: ${clean(v)}`);
|
|
14
|
+
return {
|
|
15
|
+
title: clean(`${update ? 'Update finding' : 'Install failed'}: ${command}`).slice(0, 100),
|
|
16
|
+
body: [
|
|
17
|
+
update ? '## What the update found' : '## What failed',
|
|
18
|
+
`- OS: ${platform}`,
|
|
19
|
+
...versionLines,
|
|
20
|
+
`- ${update ? 'Tool' : 'Command'}: \`${clean(command)}\``,
|
|
21
|
+
'',
|
|
22
|
+
update ? '## Details' : '## Error',
|
|
23
|
+
'```',
|
|
24
|
+
clean(error).trim(),
|
|
25
|
+
'```',
|
|
26
|
+
'',
|
|
27
|
+
'_Drafted by an agent, redacted by lean-agents, filed only after the user said yes._',
|
|
28
|
+
].join('\n'),
|
|
29
|
+
};
|
|
30
|
+
}
|