ai-agentmanager 1.16.0 → 1.18.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/CHANGELOG.md +14 -0
- package/README.md +50 -6
- package/package.json +1 -1
- package/public/app.js +105 -9
- package/public/goals.js +153 -0
- package/public/index.html +40 -1
- package/public/style.css +28 -0
- package/public/worklog.js +67 -0
- package/src/autopilot.js +27 -18
- package/src/backlog.js +14 -2
- package/src/brief.js +4 -2
- package/src/cli.js +112 -5
- package/src/goals.js +359 -0
- package/src/manager.js +82 -6
- package/src/reminders.js +58 -0
- package/src/server.js +48 -2
- package/src/sessions.js +54 -6
- package/src/worklog.js +265 -0
package/src/worklog.js
ADDED
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
// Work record: what the agents got done, appended to a spreadsheet as it happens, for your own
|
|
2
|
+
// productivity notes and performance reviews. Two CSV files (they open in Excel or Numbers, or
|
|
3
|
+
// File → Import in Google Sheets), in ~/.agentmanager or "worklog": { "dir": ... } in config.json:
|
|
4
|
+
// worklog.csv one row per event: pull request opened, merged or closed, bug fixed (a
|
|
5
|
+
// linked bug whose agent's pull request merged), issue filed (a "Filed:" line
|
|
6
|
+
// in the agent's brief), agent finished (its brief says done)
|
|
7
|
+
// worklog-weekly.csv one row per week (Monday to Sunday), added once the week is over
|
|
8
|
+
// Rows are only ever appended. On first run, the history already recorded is written too, with
|
|
9
|
+
// the dates it happened on. Everything comes from what agentmgr already tracks.
|
|
10
|
+
import fs from 'node:fs';
|
|
11
|
+
import path from 'node:path';
|
|
12
|
+
import { costOf, dayKey } from './budgets.js';
|
|
13
|
+
import { paths } from './config.js';
|
|
14
|
+
|
|
15
|
+
export const EVENT_COLUMNS = ['date', 'event', 'project', 'agent', 'agent_id', 'title', 'link', 'bugs', 'engine', 'model', 'cost_usd', 'hours', 'by'];
|
|
16
|
+
export const WEEK_COLUMNS = ['week_start', 'week_end', 'prs_opened', 'prs_merged', 'prs_closed', 'merge_rate', 'bugs_fixed', 'issues_filed', 'agents_finished', 'median_hours_to_merge', 'cost_usd'];
|
|
17
|
+
export const EVENT_LABEL = { pr_opened: 'Pull request opened', pr_merged: 'Pull request merged', pr_closed: 'Pull request closed', bug_fixed: 'Bug fixed', issue_filed: 'Issue filed', agent_finished: 'Agent finished' };
|
|
18
|
+
const HOUR = 3_600_000;
|
|
19
|
+
const DAY = 24 * HOUR;
|
|
20
|
+
|
|
21
|
+
const round = (n, d = 2) => (n == null || !Number.isFinite(n) ? null : Math.round(n * 10 ** d) / 10 ** d);
|
|
22
|
+
const median = (xs) => {
|
|
23
|
+
if (!xs.length) return null;
|
|
24
|
+
const s = [...xs].sort((a, b) => a - b);
|
|
25
|
+
const m = Math.floor(s.length / 2);
|
|
26
|
+
return s.length % 2 ? s[m] : (s[m - 1] + s[m]) / 2;
|
|
27
|
+
};
|
|
28
|
+
const iso = (t) => (Number.isFinite(t) ? new Date(t).toISOString() : null);
|
|
29
|
+
|
|
30
|
+
/** One CSV field: quoted when it has a comma, quote or line break. */
|
|
31
|
+
export const csvField = (v) => {
|
|
32
|
+
const s = v == null ? '' : String(v);
|
|
33
|
+
return /[",\n\r]/.test(s) ? `"${s.replace(/"/g, '""')}"` : s;
|
|
34
|
+
};
|
|
35
|
+
export const csvRow = (cols, row) => cols.map((c) => csvField(row[c])).join(',');
|
|
36
|
+
|
|
37
|
+
/** Monday 00:00 (local time) of the week holding t. */
|
|
38
|
+
export function weekStart(t) {
|
|
39
|
+
const d = new Date(t);
|
|
40
|
+
d.setHours(0, 0, 0, 0);
|
|
41
|
+
d.setDate(d.getDate() - ((d.getDay() + 6) % 7));
|
|
42
|
+
return d.getTime();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** "Filed: <link> <title>" → { link, title }. */
|
|
46
|
+
function filedRef(line) {
|
|
47
|
+
const url = line.match(/https?:\/\/\S+/)?.[0]?.replace(/[)>\].,]+$/, '') ?? null;
|
|
48
|
+
const title = line.replace(/\[([^\]]*)\]\([^)]*\)/g, '$1').replace(/https?:\/\/\S+/g, '').replace(/^[\s:–—-]+|[\s:–—-]+$/g, '').trim();
|
|
49
|
+
return { link: url, title: title || url || line.trim() };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The events an agent's record holds now: [{ key, at, event, title, link, bugs, hours, cost }].
|
|
54
|
+
* `at` is when it happened when that's recorded (pull request dates, brief updates), else null.
|
|
55
|
+
*/
|
|
56
|
+
export function agentEvents(a) {
|
|
57
|
+
if (a.kind === 'assistant' || a.kind === 'general') return [];
|
|
58
|
+
const out = [];
|
|
59
|
+
const created = Date.parse(a.createdAt);
|
|
60
|
+
const prs = Object.values(a.prs ?? {}).filter((p) => p.source !== 'mentioned' && p.url);
|
|
61
|
+
const bugs = Object.values(a.bugs ?? {});
|
|
62
|
+
const bugKeys = bugs.map((b) => b.key).join(' ');
|
|
63
|
+
for (const p of prs) {
|
|
64
|
+
const opened = Date.parse(p.openedAt);
|
|
65
|
+
const title = p.title ?? p.url;
|
|
66
|
+
out.push({ key: `pr_opened:${p.url}`, at: iso(opened), event: 'pr_opened', title, link: p.url, bugs: bugKeys, hours: created && opened >= created ? round((opened - created) / HOUR, 1) : null });
|
|
67
|
+
if (p.state === 'MERGED') {
|
|
68
|
+
const merged = Date.parse(p.mergedAt);
|
|
69
|
+
out.push({ key: `pr_merged:${p.url}`, at: iso(merged), event: 'pr_merged', title, link: p.url, bugs: bugKeys, hours: opened && merged >= opened ? round((merged - opened) / HOUR, 1) : null });
|
|
70
|
+
}
|
|
71
|
+
if (p.state === 'CLOSED') out.push({ key: `pr_closed:${p.url}`, at: null, event: 'pr_closed', title, link: p.url, bugs: bugKeys, hours: null });
|
|
72
|
+
}
|
|
73
|
+
// A linked bug is fixed when the agent's pull request merged (the first one, if several).
|
|
74
|
+
const firstMerge = prs.filter((p) => p.state === 'MERGED').sort((x, y) => (Date.parse(x.mergedAt) || 0) - (Date.parse(y.mergedAt) || 0))[0];
|
|
75
|
+
if (firstMerge) {
|
|
76
|
+
for (const b of bugs) out.push({ key: `bug_fixed:${a.id}:${b.key}`, at: iso(Date.parse(firstMerge.mergedAt)), event: 'bug_fixed', title: b.title ? `${b.key} ${b.title}` : b.key, link: b.url ?? null, bugs: b.key, hours: null });
|
|
77
|
+
}
|
|
78
|
+
for (const line of a.brief?.results?.filed ?? []) {
|
|
79
|
+
const ref = filedRef(line);
|
|
80
|
+
out.push({ key: `issue_filed:${ref.link ?? `${a.id}:${ref.title}`}`, at: null, event: 'issue_filed', title: ref.title, link: ref.link, bugs: '', hours: null });
|
|
81
|
+
}
|
|
82
|
+
const done = a.brief?.status === 'done' || a.brief?.results?.outcome === 'done';
|
|
83
|
+
if (done) {
|
|
84
|
+
const at = Date.parse(a.brief?.updatedAt);
|
|
85
|
+
out.push({ key: `agent_finished:${a.id}`, at: iso(at), event: 'agent_finished', title: String(a.intent ?? a.name ?? '').split('\n')[0].slice(0, 200), link: prs[0]?.url ?? null, bugs: bugKeys, hours: created && at >= created ? round((at - created) / HOUR, 1) : null, cost: round(costOf(a)) });
|
|
86
|
+
}
|
|
87
|
+
return out;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** A week's summary row from its events (and the spend ledger's days). */
|
|
91
|
+
export function weekRow(start, events, spendDays = {}) {
|
|
92
|
+
const end = start + 7 * DAY;
|
|
93
|
+
const inWeek = events.filter((e) => {
|
|
94
|
+
const t = Date.parse(e.date);
|
|
95
|
+
return t >= start && t < end;
|
|
96
|
+
});
|
|
97
|
+
const n = (k) => inWeek.filter((e) => e.event === k).length;
|
|
98
|
+
const merged = n('pr_merged');
|
|
99
|
+
const closed = n('pr_closed');
|
|
100
|
+
let cost = 0;
|
|
101
|
+
for (let t = start; t < end; t += DAY) cost += Number(spendDays[dayKey(t)]?.total) || 0;
|
|
102
|
+
return {
|
|
103
|
+
week_start: dayKey(start),
|
|
104
|
+
week_end: dayKey(end - DAY),
|
|
105
|
+
prs_opened: n('pr_opened'),
|
|
106
|
+
prs_merged: merged,
|
|
107
|
+
prs_closed: closed,
|
|
108
|
+
merge_rate: merged + closed ? round(merged / (merged + closed), 3) : '',
|
|
109
|
+
bugs_fixed: n('bug_fixed'),
|
|
110
|
+
issues_filed: n('issue_filed'),
|
|
111
|
+
agents_finished: n('agent_finished'),
|
|
112
|
+
median_hours_to_merge: round(median(inWeek.filter((e) => e.event === 'pr_merged' && e.hours !== '' && e.hours != null).map((e) => Number(e.hours))), 1) ?? '',
|
|
113
|
+
cost_usd: round(cost) ?? 0,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export class WorkLog {
|
|
118
|
+
constructor({ manager, store, config = {}, who = null, log = (m) => console.log(`[agentmgr] ${m}`) }) {
|
|
119
|
+
Object.assign(this, { manager, store, config, log });
|
|
120
|
+
this.who = who; // a promise or a name: who the record is about
|
|
121
|
+
store.data.worklog ??= { seen: {}, events: [], weeksUntil: null };
|
|
122
|
+
this.kick = () => this.schedule();
|
|
123
|
+
manager.on('change', this.kick);
|
|
124
|
+
manager.on('prs', this.kick);
|
|
125
|
+
// Week roll-over and anything a change event missed.
|
|
126
|
+
this.timer = setInterval(this.kick, (config.worklogEveryMin ?? 10) * 60_000);
|
|
127
|
+
this.timer.unref?.();
|
|
128
|
+
this.schedule(config.worklogFirstMs ?? 3000);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
close() {
|
|
132
|
+
clearInterval(this.timer);
|
|
133
|
+
clearTimeout(this.queued);
|
|
134
|
+
this.manager.off('change', this.kick);
|
|
135
|
+
this.manager.off('prs', this.kick);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
get state() {
|
|
139
|
+
return this.store.data.worklog;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
get dir() {
|
|
143
|
+
const d = this.config.worklog?.dir;
|
|
144
|
+
return d ? path.resolve(d.replace(/^~(?=$|\/)/, process.env.HOME ?? '')) : paths.home;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
get files() {
|
|
148
|
+
return { events: path.join(this.dir, 'worklog.csv'), weekly: path.join(this.dir, 'worklog-weekly.csv') };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
schedule(ms = 5000) {
|
|
152
|
+
if (this.queued) return;
|
|
153
|
+
this.queued = setTimeout(() => {
|
|
154
|
+
this.queued = null;
|
|
155
|
+
this.record().catch((e) => this.log(`work record: ${e.message}`));
|
|
156
|
+
}, ms);
|
|
157
|
+
this.queued.unref?.();
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
append(file, cols, rows) {
|
|
161
|
+
if (!rows.length) return;
|
|
162
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
163
|
+
const head = fs.existsSync(file) && fs.statSync(file).size > 0 ? '' : `${cols.join(',')}\n`;
|
|
164
|
+
fs.appendFileSync(file, head + rows.map((r) => `${csvRow(cols, r)}\n`).join(''));
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Append what happened since last time, and the summary of each week that has ended. */
|
|
168
|
+
async record(t = Date.now()) {
|
|
169
|
+
if (this.config.worklog?.enabled === false) return { added: 0, weeks: 0 };
|
|
170
|
+
if (this.recording) return this.recording;
|
|
171
|
+
this.recording = (async () => {
|
|
172
|
+
const by = (await this.who) ?? '';
|
|
173
|
+
const st = this.state;
|
|
174
|
+
const fresh = [];
|
|
175
|
+
for (const a of this.store.list()) {
|
|
176
|
+
const v = this.manager.view(a);
|
|
177
|
+
for (const e of agentEvents(a)) {
|
|
178
|
+
if (st.seen[e.key] || fresh.some((x) => x.key === e.key)) continue; // a pull request two agents share: once
|
|
179
|
+
fresh.push({
|
|
180
|
+
date: e.at ?? iso(t),
|
|
181
|
+
event: e.event,
|
|
182
|
+
project: v.projectInfo?.name ?? '',
|
|
183
|
+
agent: a.name,
|
|
184
|
+
agent_id: a.id,
|
|
185
|
+
title: e.title,
|
|
186
|
+
link: e.link ?? '',
|
|
187
|
+
bugs: e.bugs ?? '',
|
|
188
|
+
engine: a.engine ?? 'claude',
|
|
189
|
+
model: a.model ?? '',
|
|
190
|
+
cost_usd: e.cost ?? '',
|
|
191
|
+
hours: e.hours ?? '',
|
|
192
|
+
by,
|
|
193
|
+
key: e.key,
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
fresh.sort((x, y) => x.date.localeCompare(y.date));
|
|
198
|
+
this.append(this.files.events, EVENT_COLUMNS, fresh);
|
|
199
|
+
for (const e of fresh) {
|
|
200
|
+
st.seen[e.key] = e.date;
|
|
201
|
+
st.events.push(Object.fromEntries(['date', 'event', 'project', 'agent', 'agent_id', 'title', 'link', 'bugs', 'hours', 'cost_usd'].map((k) => [k, e[k]])));
|
|
202
|
+
}
|
|
203
|
+
// Weeks that ended: from the first event's week (or the week after the last one written).
|
|
204
|
+
const thisWeek = weekStart(t);
|
|
205
|
+
const first = st.weeksUntil ? Date.parse(`${st.weeksUntil}T00:00:00`) : st.events.length ? weekStart(Math.min(...st.events.map((e) => Date.parse(e.date)))) : thisWeek;
|
|
206
|
+
const weeks = [];
|
|
207
|
+
for (let w = weekStart(first); w < thisWeek; w = weekStart(w + 8 * DAY)) weeks.push(weekRow(w, st.events, this.store.data.spend?.days));
|
|
208
|
+
this.append(this.files.weekly, WEEK_COLUMNS, weeks);
|
|
209
|
+
st.weeksUntil = dayKey(thisWeek);
|
|
210
|
+
if (fresh.length || weeks.length) this.store.save?.();
|
|
211
|
+
return { added: fresh.length, weeks: weeks.length };
|
|
212
|
+
})();
|
|
213
|
+
try {
|
|
214
|
+
return await this.recording;
|
|
215
|
+
} finally {
|
|
216
|
+
this.recording = null;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** For the Work record view: a period's totals, its events, and the weeks (this one so far included). */
|
|
221
|
+
view({ days = 30, t = Date.now() } = {}) {
|
|
222
|
+
const st = this.state;
|
|
223
|
+
const since = days ? t - days * DAY : 0;
|
|
224
|
+
const events = st.events.filter((e) => Date.parse(e.date) >= since).sort((x, y) => y.date.localeCompare(x.date));
|
|
225
|
+
const n = (k) => events.filter((e) => e.event === k).length;
|
|
226
|
+
const merged = n('pr_merged');
|
|
227
|
+
const closed = n('pr_closed');
|
|
228
|
+
const weeks = [];
|
|
229
|
+
for (let w = weekStart(t), i = 0; i < 12; i++, w = weekStart(w - 3 * DAY)) weeks.push({ ...weekRow(w, st.events, this.store.data.spend?.days), current: i === 0 });
|
|
230
|
+
return {
|
|
231
|
+
days,
|
|
232
|
+
totals: {
|
|
233
|
+
prs_opened: n('pr_opened'),
|
|
234
|
+
prs_merged: merged,
|
|
235
|
+
prs_closed: closed,
|
|
236
|
+
merge_rate: merged + closed ? round(merged / (merged + closed), 3) : null,
|
|
237
|
+
bugs_fixed: n('bug_fixed'),
|
|
238
|
+
issues_filed: n('issue_filed'),
|
|
239
|
+
agents_finished: n('agent_finished'),
|
|
240
|
+
median_hours_to_merge: round(median(events.filter((e) => e.event === 'pr_merged' && e.hours !== '' && e.hours != null).map((e) => Number(e.hours))), 1),
|
|
241
|
+
},
|
|
242
|
+
events: events.slice(0, 500),
|
|
243
|
+
weeks: weeks.filter((w, i) => i === 0 || w.prs_opened || w.prs_merged || w.bugs_fixed || w.issues_filed || w.agents_finished || w.cost_usd),
|
|
244
|
+
files: this.files,
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** A period's record as Markdown, for a review document or a status update. */
|
|
250
|
+
export function summaryMarkdown(v, { who = '', from = null, to = null } = {}) {
|
|
251
|
+
const t = v.totals;
|
|
252
|
+
const list = (k) => v.events.filter((e) => e.event === k);
|
|
253
|
+
const line = (e) => `- ${e.link ? `[${e.title}](${e.link})` : e.title}${e.project ? ` (${e.project})` : ''}, ${e.date.slice(0, 10)}`;
|
|
254
|
+
const period = from && to ? `${from} to ${to}` : v.days ? `the last ${v.days} days` : 'all time';
|
|
255
|
+
const parts = [
|
|
256
|
+
`# Work record${who ? `: ${who}` : ''}, ${period}`,
|
|
257
|
+
`- Pull requests: ${t.prs_merged} merged, ${t.prs_opened} opened${t.prs_closed ? `, ${t.prs_closed} closed unmerged` : ''}${t.merge_rate != null ? ` (merge rate ${Math.round(t.merge_rate * 100)}%)` : ''}${t.median_hours_to_merge != null ? `; median ${t.median_hours_to_merge} h from opening to merge` : ''}`,
|
|
258
|
+
`- Bugs fixed: ${t.bugs_fixed}; issues filed: ${t.issues_filed}; agents finished: ${t.agents_finished}`,
|
|
259
|
+
];
|
|
260
|
+
for (const [k, title] of [['pr_merged', 'Merged'], ['bug_fixed', 'Bugs fixed'], ['issue_filed', 'Issues filed']]) {
|
|
261
|
+
const xs = list(k);
|
|
262
|
+
if (xs.length) parts.push('', `## ${title}`, ...xs.slice(0, 50).map(line), ...(xs.length > 50 ? [`- …and ${xs.length - 50} more`] : []));
|
|
263
|
+
}
|
|
264
|
+
return parts.join('\n');
|
|
265
|
+
}
|