@eocrm/cli 0.0.1-bootstrap.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.
- package/LICENSE +202 -0
- package/README.md +141 -2
- package/hooks/dispatch.mjs +34 -0
- package/lib/api.mjs +151 -0
- package/lib/cli/attribute.mjs +28 -0
- package/lib/cli/hooks.mjs +210 -0
- package/lib/cli/openspec.mjs +102 -0
- package/lib/cli/run.mjs +229 -0
- package/lib/codex.mjs +78 -0
- package/lib/credentials.mjs +207 -0
- package/lib/detect.mjs +279 -0
- package/lib/endpoints.mjs +46 -0
- package/lib/flush.mjs +260 -0
- package/lib/gate.mjs +87 -0
- package/lib/git.mjs +101 -0
- package/lib/ids.mjs +3 -0
- package/lib/login.mjs +283 -0
- package/lib/outbox.mjs +130 -0
- package/lib/paths.mjs +53 -0
- package/lib/plans.mjs +265 -0
- package/lib/reconcile.mjs +213 -0
- package/lib/remind.mjs +170 -0
- package/lib/run.mjs +560 -0
- package/lib/scrub.mjs +515 -0
- package/lib/state.mjs +219 -0
- package/lib/usage.mjs +117 -0
- package/lib/workspace.mjs +109 -0
- package/package.json +32 -3
- package/scripts/cli.mjs +8 -0
- package/scripts/eocrm.mjs +395 -0
package/lib/state.mjs
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { mkdirSync, rmSync, renameSync, statSync, readFileSync, readdirSync, writeFileSync, realpathSync, openSync, closeSync } from 'node:fs';
|
|
2
|
+
import { dirname, join, resolve } from 'node:path';
|
|
3
|
+
import { createHash, randomBytes } from 'node:crypto';
|
|
4
|
+
import { execFileSync } from 'node:child_process';
|
|
5
|
+
|
|
6
|
+
const POLL_MS = 25;
|
|
7
|
+
const STALE_MS = 30000;
|
|
8
|
+
const GIVE_UP_MS = 5000;
|
|
9
|
+
|
|
10
|
+
export class LockTimeout extends Error {
|
|
11
|
+
constructor(dir) {
|
|
12
|
+
super(`eocrm: lock timeout in ${dir}`);
|
|
13
|
+
this.name = 'LockTimeout';
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
18
|
+
|
|
19
|
+
const owner = (lock) => {
|
|
20
|
+
try {
|
|
21
|
+
return readFileSync(join(lock, 'owner'), 'utf8');
|
|
22
|
+
} catch {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const remove = (path) => {
|
|
28
|
+
try {
|
|
29
|
+
rmSync(path, { recursive: true, force: true, maxRetries: 3, retryDelay: 50 });
|
|
30
|
+
} catch {
|
|
31
|
+
// a leftover lock goes stale; never mask fn's outcome
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export function breakStale(lock, seen) {
|
|
36
|
+
const grave = `${lock}.${process.pid}.${randomBytes(4).toString('hex')}`;
|
|
37
|
+
renameSync(lock, grave);
|
|
38
|
+
if (owner(grave) === seen && Date.now() - statSync(grave).mtimeMs > STALE_MS) {
|
|
39
|
+
remove(grave);
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
try {
|
|
43
|
+
renameSync(grave, lock);
|
|
44
|
+
} catch {
|
|
45
|
+
remove(grave);
|
|
46
|
+
}
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// ponytail: lock mtime is never refreshed, so an fn longer than STALE_MS can be broken; fine for short hooks, add a heartbeat if not.
|
|
51
|
+
export async function withLock(dir, fn, { giveUpMs = GIVE_UP_MS } = {}) {
|
|
52
|
+
mkdirSync(dir, { recursive: true });
|
|
53
|
+
const lock = join(dir, '.lock');
|
|
54
|
+
const token = randomBytes(16).toString('hex');
|
|
55
|
+
const deadline = Date.now() + giveUpMs;
|
|
56
|
+
for (;;) {
|
|
57
|
+
try {
|
|
58
|
+
mkdirSync(lock);
|
|
59
|
+
} catch (e) {
|
|
60
|
+
if (e.code !== 'EEXIST' && e.code !== 'EPERM' && e.code !== 'EACCES') throw e;
|
|
61
|
+
try {
|
|
62
|
+
const seen = owner(lock);
|
|
63
|
+
if (Date.now() - statSync(lock).mtimeMs > STALE_MS && breakStale(lock, seen)) continue;
|
|
64
|
+
} catch {
|
|
65
|
+
// lock vanished or unreadable: just retry
|
|
66
|
+
}
|
|
67
|
+
if (Date.now() >= deadline) throw new LockTimeout(dir);
|
|
68
|
+
await sleep(POLL_MS);
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
writeFileSync(join(lock, 'owner'), token);
|
|
73
|
+
break;
|
|
74
|
+
} catch (e) {
|
|
75
|
+
remove(lock);
|
|
76
|
+
throw e;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
try {
|
|
80
|
+
return await fn();
|
|
81
|
+
} finally {
|
|
82
|
+
if (owner(lock) === token) remove(lock);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function readJson(file, fallback) {
|
|
87
|
+
try {
|
|
88
|
+
const value = JSON.parse(readFileSync(file, 'utf8'));
|
|
89
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value) ? value : fallback;
|
|
90
|
+
} catch {
|
|
91
|
+
return fallback;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const nap = (ms) => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
96
|
+
const RETRY = new Set(['EPERM', 'EBUSY', 'EACCES']);
|
|
97
|
+
|
|
98
|
+
export const writeJsonAtomic = (file, value, opts) => writeFileAtomic(file, JSON.stringify(value), opts);
|
|
99
|
+
|
|
100
|
+
export function writeFileAtomic(file, text, { mode = 0o666 } = {}) {
|
|
101
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
102
|
+
const tmp = `${file}.${process.pid}.${randomBytes(4).toString('hex')}.tmp`;
|
|
103
|
+
const fd = openSync(tmp, 'wx', mode);
|
|
104
|
+
try {
|
|
105
|
+
try {
|
|
106
|
+
writeFileSync(fd, text);
|
|
107
|
+
} finally {
|
|
108
|
+
closeSync(fd);
|
|
109
|
+
}
|
|
110
|
+
for (let i = 0; ; i++) {
|
|
111
|
+
try {
|
|
112
|
+
renameSync(tmp, file);
|
|
113
|
+
return;
|
|
114
|
+
} catch (e) {
|
|
115
|
+
if (process.platform !== 'win32' || !RETRY.has(e.code) || i >= 3) throw e;
|
|
116
|
+
nap(20 * (i + 1));
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
} catch (e) {
|
|
120
|
+
remove(tmp);
|
|
121
|
+
throw e;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// deadline (epoch ms), when set, caps every git call after it: a hook's whole budget, not one call's.
|
|
126
|
+
export const gitTimeout = { ms: 5000, deadline: null };
|
|
127
|
+
|
|
128
|
+
export const git = (cwd, args, run = execFileSync) => {
|
|
129
|
+
const left = gitTimeout.deadline === null ? gitTimeout.ms : Math.min(gitTimeout.ms, gitTimeout.deadline - Date.now());
|
|
130
|
+
if (left <= 0) return '';
|
|
131
|
+
try {
|
|
132
|
+
return run('git', args, { cwd, encoding: 'utf8', timeout: left, windowsHide: true, stdio: ['ignore', 'pipe', 'ignore'] }).trim();
|
|
133
|
+
} catch {
|
|
134
|
+
return '';
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
export function worktreeOf(cwd, run, top = git(cwd, ['rev-parse', '--show-toplevel'], run)) {
|
|
139
|
+
const root = resolve(top || cwd);
|
|
140
|
+
let real = root;
|
|
141
|
+
try {
|
|
142
|
+
real = realpathSync.native(root);
|
|
143
|
+
} catch {
|
|
144
|
+
// path vanished: hash as given
|
|
145
|
+
}
|
|
146
|
+
if (process.platform === 'win32') real = real.toLowerCase();
|
|
147
|
+
return { root, key: createHash('sha256').update(real).digest('hex').slice(0, 16) };
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const hookFile = (data, key) => join(data, 'hooks', key);
|
|
151
|
+
|
|
152
|
+
export const stampHook = (data, key, at) => writeFileAtomic(hookFile(data, key), String(at));
|
|
153
|
+
|
|
154
|
+
export function lastHookAt(data, key) {
|
|
155
|
+
try {
|
|
156
|
+
return Number(readFileSync(hookFile(data, key), 'utf8')) || null;
|
|
157
|
+
} catch {
|
|
158
|
+
return null;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const worktreeFile = (data, key) => join(data, 'worktrees', `${key}.json`);
|
|
163
|
+
const runFile = (data, sessionId) => {
|
|
164
|
+
if (typeof sessionId !== 'string' || !/^[\w-]+$/.test(sessionId)) throw new Error('eocrm: invalid session id');
|
|
165
|
+
return join(data, 'runs', `${sessionId}.json`);
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
export function loadWorktree(data, key) {
|
|
169
|
+
return { root: '', taskKey: null, linkAnswered: false, reportedCommits: [], openPrs: [], ...readJson(worktreeFile(data, key), {}) };
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export function saveWorktree(data, key, patch) {
|
|
173
|
+
const next = { ...loadWorktree(data, key), ...patch };
|
|
174
|
+
writeJsonAtomic(worktreeFile(data, key), next);
|
|
175
|
+
return next;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export function loadRun(data, sessionId) {
|
|
179
|
+
return { sessionId, acked: {}, rejected: [], blockedFingerprints: [], serverEventsRead: false, ...readJson(runFile(data, sessionId), {}) };
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export function saveRun(data, sessionId, patch) {
|
|
183
|
+
const next = { ...loadRun(data, sessionId), ...patch };
|
|
184
|
+
writeJsonAtomic(runFile(data, sessionId), next);
|
|
185
|
+
return next;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Worktree → live plugin session, kept in the worktree's own git dir so the CLI finds it whatever its data dir.
|
|
189
|
+
export const LIVE_MS = 30 * 60 * 1000;
|
|
190
|
+
export const liveDirOf = (cwd, run) => {
|
|
191
|
+
const gitDir = git(cwd, ['rev-parse', '--absolute-git-dir'], run);
|
|
192
|
+
return gitDir ? join(gitDir, 'eocrm-live') : null;
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
export const markLive = (dir, sessionId, at) => writeFileAtomic(join(dir, sessionId), String(at));
|
|
196
|
+
|
|
197
|
+
export const clearLive = (dir, sessionId) => remove(join(dir, sessionId));
|
|
198
|
+
|
|
199
|
+
export function liveSession(dir, now) {
|
|
200
|
+
let names;
|
|
201
|
+
try {
|
|
202
|
+
names = readdirSync(dir);
|
|
203
|
+
} catch {
|
|
204
|
+
return null;
|
|
205
|
+
}
|
|
206
|
+
let live = null;
|
|
207
|
+
for (const n of names) {
|
|
208
|
+
if (n.endsWith('.tmp')) continue;
|
|
209
|
+
let at;
|
|
210
|
+
try {
|
|
211
|
+
at = Number(readFileSync(join(dir, n), 'utf8'));
|
|
212
|
+
} catch {
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
if (now - at <= LIVE_MS) live ??= n;
|
|
216
|
+
else remove(join(dir, n));
|
|
217
|
+
}
|
|
218
|
+
return live;
|
|
219
|
+
}
|
package/lib/usage.mjs
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { closeSync, fstatSync, openSync, readSync, readdirSync, statSync } from 'node:fs';
|
|
2
|
+
import { basename, dirname, join } from 'node:path';
|
|
3
|
+
|
|
4
|
+
const MAX_FILES = 64;
|
|
5
|
+
|
|
6
|
+
export function readUsage(path, offset, maxBytes = 8 * 1024 * 1024, seen = [], lineCap = maxBytes) {
|
|
7
|
+
const models = {};
|
|
8
|
+
const ids = new Set(seen);
|
|
9
|
+
const fresh = [];
|
|
10
|
+
let buf;
|
|
11
|
+
let size;
|
|
12
|
+
let reset = false;
|
|
13
|
+
try {
|
|
14
|
+
const fd = openSync(path, 'r');
|
|
15
|
+
try {
|
|
16
|
+
size = fstatSync(fd).size;
|
|
17
|
+
if (size < offset) {
|
|
18
|
+
offset = 0;
|
|
19
|
+
reset = true;
|
|
20
|
+
}
|
|
21
|
+
const n = Math.max(0, Math.min(maxBytes, size - offset));
|
|
22
|
+
buf = Buffer.alloc(n);
|
|
23
|
+
readSync(fd, buf, 0, n, offset);
|
|
24
|
+
} finally {
|
|
25
|
+
closeSync(fd);
|
|
26
|
+
}
|
|
27
|
+
} catch {
|
|
28
|
+
return { from: offset, offset, models, ids: fresh, reset };
|
|
29
|
+
}
|
|
30
|
+
const end = buf.lastIndexOf(10) + 1 || (buf.length >= lineCap && offset + buf.length < size ? buf.length : 0);
|
|
31
|
+
for (const line of buf.subarray(0, end).toString('utf8').split('\n')) {
|
|
32
|
+
if (!line.includes('"usage"')) continue;
|
|
33
|
+
let o;
|
|
34
|
+
try {
|
|
35
|
+
o = JSON.parse(line);
|
|
36
|
+
} catch {
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
const m = o?.message;
|
|
40
|
+
const u = m?.usage;
|
|
41
|
+
if (o?.type !== 'assistant' || !u || typeof m.model !== 'string' || !m.model || m.model === '<synthetic>') continue;
|
|
42
|
+
if (m.id) {
|
|
43
|
+
if (ids.has(m.id)) continue;
|
|
44
|
+
ids.add(m.id);
|
|
45
|
+
fresh.push(m.id);
|
|
46
|
+
}
|
|
47
|
+
const t = (models[m.model] ??= { input_tokens: 0, output_tokens: 0, cache_read_tokens: 0, cache_write_tokens: 0 });
|
|
48
|
+
const add = (k, v) => { if (Number.isSafeInteger(v) && v > 0) t[k] += v; };
|
|
49
|
+
add('input_tokens', u.input_tokens);
|
|
50
|
+
add('output_tokens', u.output_tokens);
|
|
51
|
+
add('cache_read_tokens', u.cache_read_input_tokens);
|
|
52
|
+
add('cache_write_tokens', u.cache_creation_input_tokens);
|
|
53
|
+
}
|
|
54
|
+
return { from: offset, offset: offset + end, models, ids: fresh, reset };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// A subagent's directory never changes, so its first line's cwd is the agent's.
|
|
58
|
+
export function fileCwd(path) {
|
|
59
|
+
try {
|
|
60
|
+
const fd = openSync(path, 'r');
|
|
61
|
+
const buf = Buffer.alloc(256 * 1024);
|
|
62
|
+
try {
|
|
63
|
+
const n = readSync(fd, buf, 0, buf.length, 0);
|
|
64
|
+
const cwd = JSON.parse(buf.subarray(0, n).toString('utf8').split('\n')[0])?.cwd;
|
|
65
|
+
return typeof cwd === 'string' && cwd ? cwd : null;
|
|
66
|
+
} finally {
|
|
67
|
+
closeSync(fd);
|
|
68
|
+
}
|
|
69
|
+
} catch {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function transcriptFiles(path) {
|
|
75
|
+
const dir = join(dirname(path), basename(path, '.jsonl'), 'subagents');
|
|
76
|
+
let subs = [];
|
|
77
|
+
try {
|
|
78
|
+
subs = readdirSync(dir).filter((f) => f.endsWith('.jsonl')).sort().map((f) => join(dir, f));
|
|
79
|
+
} catch {
|
|
80
|
+
// no subagents
|
|
81
|
+
}
|
|
82
|
+
return [path, ...subs];
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function sizesOf(path) {
|
|
86
|
+
const out = {};
|
|
87
|
+
for (const f of transcriptFiles(path)) {
|
|
88
|
+
try {
|
|
89
|
+
out[f] = statSync(f).size;
|
|
90
|
+
} catch {
|
|
91
|
+
// gone
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return out;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function readSession(path, offsets = {}, seen = [], maxTotal = 8 * 1024 * 1024, epochs = {}) {
|
|
98
|
+
const next = { ...offsets };
|
|
99
|
+
const nextEpochs = { ...epochs };
|
|
100
|
+
const ids = [];
|
|
101
|
+
const reads = [];
|
|
102
|
+
let left = maxTotal;
|
|
103
|
+
for (const f of transcriptFiles(path)) {
|
|
104
|
+
if (left <= 0) break;
|
|
105
|
+
const r = readUsage(f, offsets[f] ?? 0, left, [...seen, ...ids], maxTotal);
|
|
106
|
+
left -= r.offset - r.from;
|
|
107
|
+
ids.push(...r.ids);
|
|
108
|
+
delete next[f];
|
|
109
|
+
next[f] = r.offset;
|
|
110
|
+
if (r.reset) nextEpochs[f] = (nextEpochs[f] ?? 0) + 1;
|
|
111
|
+
reads.push({ file: f, from: r.from, epoch: nextEpochs[f] ?? 0, models: r.models });
|
|
112
|
+
}
|
|
113
|
+
const subs = Object.keys(next).filter((f) => f !== path).slice(-MAX_FILES);
|
|
114
|
+
const keep = path in next ? [path, ...subs] : subs;
|
|
115
|
+
const pick = (m) => Object.fromEntries(keep.filter((f) => f in m).map((f) => [f, m[f]]));
|
|
116
|
+
return { offsets: pick(next), epochs: pick(nextEpochs), ids, reads };
|
|
117
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { realpathSync } from 'node:fs';
|
|
2
|
+
import { isAbsolute, join, resolve } from 'node:path';
|
|
3
|
+
import { SLUG, REGIONS } from './endpoints.mjs';
|
|
4
|
+
import { readJson, writeJsonAtomic, worktreeOf, loadWorktree, saveWorktree, git } from './state.mjs';
|
|
5
|
+
|
|
6
|
+
export const accountFile = (data) => join(data, 'account.json');
|
|
7
|
+
export const loadAccount = (data) => readJson(accountFile(data), {});
|
|
8
|
+
const reposFile = (data) => join(data, 'repos.json');
|
|
9
|
+
const isSlug = (s) => typeof s === 'string' && SLUG.test(s);
|
|
10
|
+
|
|
11
|
+
const SCHEME = /^[a-z][a-z0-9+.-]*:\/\//i;
|
|
12
|
+
const SCP = /^[^/\\]{2,}:/;
|
|
13
|
+
|
|
14
|
+
export function normaliseRemote(url) {
|
|
15
|
+
const t = url.trim();
|
|
16
|
+
const scheme = SCHEME.exec(t);
|
|
17
|
+
const rest = t.slice(scheme ? scheme[0].length : 0).replace(/^[^/]*@/, '');
|
|
18
|
+
return (scheme ? rest.replace(/^([^/:]+):\d*\//, '$1/') : rest.replace(/^([^/:]+):/, '$1/'))
|
|
19
|
+
.replace(/\/+$/, '')
|
|
20
|
+
.replace(/\.git$/i, '')
|
|
21
|
+
.toLowerCase();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function repoKey(cwd, origin = git(cwd, ['remote', 'get-url', 'origin'])) {
|
|
25
|
+
if (origin && (SCHEME.test(origin) || SCP.test(origin) || isAbsolute(origin))) return normaliseRemote(origin);
|
|
26
|
+
return cloneKey(cwd);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// One clone and its linked worktrees, whatever its remote.
|
|
30
|
+
export function cloneKey(cwd) {
|
|
31
|
+
const common = git(cwd, ['rev-parse', '--git-common-dir']);
|
|
32
|
+
if (!common) return null;
|
|
33
|
+
let dir = resolve(cwd, common);
|
|
34
|
+
try {
|
|
35
|
+
dir = realpathSync.native(dir);
|
|
36
|
+
} catch {
|
|
37
|
+
// keep as resolved
|
|
38
|
+
}
|
|
39
|
+
return process.platform === 'win32' ? dir.toLowerCase() : dir;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Only a slug and a known region are taken from a repo's file: it is not trusted to name hosts.
|
|
43
|
+
function committedFile(root) {
|
|
44
|
+
const f = readJson(join(root, '.claude', 'eocrm.json'), null);
|
|
45
|
+
return f && isSlug(f.workspace) && REGIONS.includes(f.region) ? { workspace: f.workspace, region: f.region } : null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function resolveWorkspace({ data, cwd, wt = worktreeOf(cwd), origin }) {
|
|
49
|
+
const account = loadAccount(data);
|
|
50
|
+
const key = repoKey(cwd, origin);
|
|
51
|
+
const entry = (key && readJson(reposFile(data), {})[key]) || {};
|
|
52
|
+
const pick = (workspace, source, region = account.region ?? null, pending = null) => ({ workspace, region, source, pending });
|
|
53
|
+
|
|
54
|
+
const override = loadWorktree(data, wt.key).workspace;
|
|
55
|
+
if (isSlug(override)) return pick(override, 'worktree');
|
|
56
|
+
if (isSlug(entry.workspace)) return pick(entry.workspace, 'repo');
|
|
57
|
+
const file = committedFile(wt.root);
|
|
58
|
+
const answer = file && entry.committed?.workspace === file.workspace && entry.committed.region === file.region ? entry.committed.accepted : undefined;
|
|
59
|
+
if (answer === true) return pick(file.workspace, 'committed', file.region);
|
|
60
|
+
const differs = file && (file.workspace !== account.workspace || file.region !== account.region);
|
|
61
|
+
return pick(isSlug(account.workspace) ? account.workspace : null, 'default', undefined, differs && answer === undefined ? file : null);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const clonesFile = (data) => join(data, 'clones.json');
|
|
65
|
+
|
|
66
|
+
export const cloneEntry = (data, cwd) => {
|
|
67
|
+
const key = cloneKey(cwd);
|
|
68
|
+
return (key && readJson(clonesFile(data), {})[key]) || {};
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export function updateClone(data, cwd, patch) {
|
|
72
|
+
const key = cloneKey(cwd);
|
|
73
|
+
if (!key) throw Object.assign(new Error('not a git repository'), { exitCode: 2 });
|
|
74
|
+
const clones = readJson(clonesFile(data), {});
|
|
75
|
+
clones[key] = { ...clones[key], ...patch };
|
|
76
|
+
writeJsonAtomic(clonesFile(data), clones);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export const repoEntry = (data, cwd, origin) => {
|
|
80
|
+
const key = repoKey(cwd, origin);
|
|
81
|
+
return (key && readJson(reposFile(data), {})[key]) || {};
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export function updateRepo(data, cwd, patch) {
|
|
85
|
+
const key = repoKey(cwd);
|
|
86
|
+
if (!key) throw Object.assign(new Error('not a git repository: use --here'), { exitCode: 2 });
|
|
87
|
+
const repos = readJson(reposFile(data), {});
|
|
88
|
+
repos[key] = { ...repos[key], ...patch };
|
|
89
|
+
writeJsonAtomic(reposFile(data), repos);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function useWorkspace({ data, cwd, workspace, here = false }) {
|
|
93
|
+
if (!isSlug(workspace)) throw Object.assign(new Error('invalid workspace slug'), { exitCode: 2 });
|
|
94
|
+
if (here) saveWorktree(data, worktreeOf(cwd).key, { workspace });
|
|
95
|
+
else updateRepo(data, cwd, { workspace });
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function answerCommitted({ data, cwd, accepted }) {
|
|
99
|
+
const file = committedFile(worktreeOf(cwd).root);
|
|
100
|
+
if (!file) throw Object.assign(new Error('no valid .claude/eocrm.json in this repository'), { exitCode: 2 });
|
|
101
|
+
updateRepo(data, cwd, { committed: { ...file, accepted } });
|
|
102
|
+
return file;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export const command = (harness, c) => (harness === 'cli' ? `eocrm ${c}` : `/eocrm:${c}`);
|
|
106
|
+
|
|
107
|
+
export function committedLine({ workspace, region }, harness) {
|
|
108
|
+
return `.claude/eocrm.json names ${workspace} (${region}): ask the member, then run ${command(harness, 'use --accept')} or ${command(harness, 'use --decline')}`;
|
|
109
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eocrm/cli",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "eocrm work tracking for any coding agent (placeholder; real releases are published from GitHub Actions)",
|
|
3
|
+
"version": "0.4.0",
|
|
5
4
|
"license": "Apache-2.0",
|
|
6
|
-
"repository": {
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/eocrm/agent-plugin.git"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"bin": {
|
|
11
|
+
"eocrm": "scripts/cli.mjs"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"scripts/cli.mjs",
|
|
15
|
+
"scripts/eocrm.mjs",
|
|
16
|
+
"hooks/dispatch.mjs",
|
|
17
|
+
"lib/",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE"
|
|
20
|
+
],
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">=20"
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
25
|
+
"lint": "eslint .",
|
|
26
|
+
"test": "node scripts/run-tests.mjs test/unit",
|
|
27
|
+
"test:contract": "node scripts/run-tests.mjs test/contract",
|
|
28
|
+
"test:budget": "node scripts/run-tests.mjs test/budget --optional"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@eslint/js": "10.0.1",
|
|
32
|
+
"ajv": "8.20.0",
|
|
33
|
+
"ajv-formats": "3.0.1",
|
|
34
|
+
"eslint": "10.14.0"
|
|
35
|
+
}
|
|
7
36
|
}
|
package/scripts/cli.mjs
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { main } from './eocrm.mjs';
|
|
3
|
+
|
|
4
|
+
// A hook exits as soon as it is done or out of time: nothing it left pending may hold git.
|
|
5
|
+
main({ harness: 'cli' }).then((code) => {
|
|
6
|
+
if (process.argv[2] === 'hook') process.exit(code);
|
|
7
|
+
process.exitCode = code;
|
|
8
|
+
});
|