oneie 4.0.2 → 4.2.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/README.md +78 -1
- package/dist/package.json +4 -4
- package/dist/src/agent.d.ts +73 -0
- package/dist/src/agent.js +486 -53
- package/dist/src/auth.js +3 -4
- package/dist/src/broadcast.d.ts +2 -0
- package/dist/src/broadcast.js +24 -0
- package/dist/src/compiler.js +27 -3
- package/dist/src/doctor.js +61 -6
- package/dist/src/eval.js +1 -0
- package/dist/src/fn.js +5 -8
- package/dist/src/index.js +8 -1
- package/dist/src/init.js +33 -3
- package/dist/src/lib/agent-env.js +7 -0
- package/dist/src/lib/client.js +16 -7
- package/dist/src/lib/keyfile.d.ts +130 -0
- package/dist/src/lib/keyfile.js +381 -0
- package/dist/src/lib/paths.d.ts +33 -0
- package/dist/src/lib/paths.js +77 -0
- package/dist/src/lib/tasks-digest.d.ts +66 -0
- package/dist/src/lib/tasks-digest.js +200 -0
- package/dist/src/lib/tasks-graph.d.ts +78 -0
- package/dist/src/lib/tasks-graph.js +425 -0
- package/dist/src/lib/vaultfile.d.ts +73 -0
- package/dist/src/lib/vaultfile.js +184 -0
- package/dist/src/media.d.ts +2 -0
- package/dist/src/media.js +256 -0
- package/dist/src/plugin.js +10 -1
- package/dist/src/skill.js +128 -12
- package/dist/src/tasks.d.ts +2 -0
- package/dist/src/tasks.js +424 -0
- package/dist/src/templates.js +8 -0
- package/dist/src/wallet.d.ts +9 -0
- package/dist/src/wallet.js +259 -1
- package/dist/src/workflow.d.ts +2 -0
- package/dist/src/workflow.js +144 -0
- package/package.json +4 -4
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
export const CLOSED = new Set(['done', 'verified', 'dissolved', 'failed']);
|
|
2
|
+
export function kindOf(r) {
|
|
3
|
+
return r.tags?.includes('approval') ? 'approval' : 'work';
|
|
4
|
+
}
|
|
5
|
+
export function filterKind(rows, kind) {
|
|
6
|
+
if (kind === 'all')
|
|
7
|
+
return { rows, excluded: { approval: 0, work: 0 } };
|
|
8
|
+
let approval = 0;
|
|
9
|
+
let work = 0;
|
|
10
|
+
const out = [];
|
|
11
|
+
for (const r of rows) {
|
|
12
|
+
const k = kindOf(r);
|
|
13
|
+
if (k === kind) {
|
|
14
|
+
out.push(r);
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
if (k === 'approval')
|
|
18
|
+
approval += 1;
|
|
19
|
+
else
|
|
20
|
+
work += 1;
|
|
21
|
+
}
|
|
22
|
+
return { rows: out, excluded: { approval, work } };
|
|
23
|
+
}
|
|
24
|
+
const STALE_DONE_RE = /^\s*(DONE|SHIPPED|LANDED)\b/i;
|
|
25
|
+
export function isStaleDone(r) {
|
|
26
|
+
if (kindOf(r) === 'approval')
|
|
27
|
+
return false;
|
|
28
|
+
if (CLOSED.has(r.status))
|
|
29
|
+
return false;
|
|
30
|
+
if (!r.notes)
|
|
31
|
+
return false;
|
|
32
|
+
return STALE_DONE_RE.test(r.notes);
|
|
33
|
+
}
|
|
34
|
+
const byPriorityDesc = (a, b) => (b.priority ?? 0) - (a.priority ?? 0);
|
|
35
|
+
export function digest(rows, opts) {
|
|
36
|
+
const kind = opts?.kind ?? 'work';
|
|
37
|
+
// Blocker lookup is built from ALL rows passed in, before the kind filter —
|
|
38
|
+
// an approval row can still be the blocker a work row is waiting on.
|
|
39
|
+
const byId = new Map();
|
|
40
|
+
for (const r of rows)
|
|
41
|
+
byId.set(r.tid, r);
|
|
42
|
+
const { rows: filtered, excluded } = filterKind(rows, kind);
|
|
43
|
+
const byStatus = {};
|
|
44
|
+
for (const r of filtered)
|
|
45
|
+
byStatus[r.status] = (byStatus[r.status] ?? 0) + 1;
|
|
46
|
+
const ready = filtered.filter((r) => r.ready === true).sort(byPriorityDesc);
|
|
47
|
+
const blocked = [];
|
|
48
|
+
for (const r of filtered) {
|
|
49
|
+
const openBlockers = r.openBlockers ?? 0;
|
|
50
|
+
const hasBlockedStatus = r.status === 'blocked';
|
|
51
|
+
if (openBlockers <= 0 && !hasBlockedStatus)
|
|
52
|
+
continue;
|
|
53
|
+
const ids = r.blockedBy ?? [];
|
|
54
|
+
const blockers = [];
|
|
55
|
+
for (const bid of ids) {
|
|
56
|
+
const b = byId.get(bid);
|
|
57
|
+
if (!b)
|
|
58
|
+
continue;
|
|
59
|
+
if (CLOSED.has(b.status))
|
|
60
|
+
continue;
|
|
61
|
+
blockers.push({ tid: b.tid, name: b.name, status: b.status });
|
|
62
|
+
}
|
|
63
|
+
const outside = Math.max(0, openBlockers - blockers.length);
|
|
64
|
+
const staleStatus = hasBlockedStatus && openBlockers === 0;
|
|
65
|
+
blocked.push({
|
|
66
|
+
tid: r.tid,
|
|
67
|
+
name: r.name,
|
|
68
|
+
priority: r.priority,
|
|
69
|
+
blockers,
|
|
70
|
+
outside,
|
|
71
|
+
staleStatus,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
blocked.sort((a, b) => b.priority - a.priority);
|
|
75
|
+
const overdue = filtered.filter((r) => r.overdue === true).sort(byPriorityDesc);
|
|
76
|
+
const unassigned = filtered.filter((r) => !CLOSED.has(r.status) && !r.assignee).sort(byPriorityDesc);
|
|
77
|
+
const orphans = filtered.filter((r) => !CLOSED.has(r.status) && !r.parent).sort(byPriorityDesc);
|
|
78
|
+
const notesLoaded = filtered.some((r) => 'notes' in r);
|
|
79
|
+
const noNotes = notesLoaded ? filtered.filter((r) => !r.notes) : null;
|
|
80
|
+
const staleDone = filtered.filter(isStaleDone).sort(byPriorityDesc);
|
|
81
|
+
const picked = filtered.filter((r) => r.status === 'picked').sort(byPriorityDesc);
|
|
82
|
+
const nextMoves = [];
|
|
83
|
+
const used = new Set();
|
|
84
|
+
const push = (r, action, why) => {
|
|
85
|
+
if (nextMoves.length >= 5 || used.has(r.tid))
|
|
86
|
+
return;
|
|
87
|
+
used.add(r.tid);
|
|
88
|
+
nextMoves.push({ tid: r.tid, name: r.name, action, why });
|
|
89
|
+
};
|
|
90
|
+
for (const r of staleDone) {
|
|
91
|
+
if (nextMoves.length >= 5)
|
|
92
|
+
break;
|
|
93
|
+
push(r, 'close', `notes say DONE but status is ${r.status}`);
|
|
94
|
+
}
|
|
95
|
+
for (const b of blocked) {
|
|
96
|
+
if (nextMoves.length >= 5)
|
|
97
|
+
break;
|
|
98
|
+
if (!b.staleStatus)
|
|
99
|
+
continue;
|
|
100
|
+
push(b, 'reopen', 'status blocked, no open blocker');
|
|
101
|
+
}
|
|
102
|
+
for (const r of ready) {
|
|
103
|
+
if (nextMoves.length >= 5)
|
|
104
|
+
break;
|
|
105
|
+
if (r.assignee)
|
|
106
|
+
continue;
|
|
107
|
+
push(r, 'assign', 'ready and unowned');
|
|
108
|
+
}
|
|
109
|
+
for (const r of ready) {
|
|
110
|
+
if (nextMoves.length >= 5)
|
|
111
|
+
break;
|
|
112
|
+
if (!r.assignee)
|
|
113
|
+
continue;
|
|
114
|
+
push(r, 'launch', `ready, owned by @${r.assignee}`);
|
|
115
|
+
}
|
|
116
|
+
for (const r of overdue) {
|
|
117
|
+
if (nextMoves.length >= 5)
|
|
118
|
+
break;
|
|
119
|
+
if (CLOSED.has(r.status))
|
|
120
|
+
continue;
|
|
121
|
+
push(r, 'reschedule', 'overdue');
|
|
122
|
+
}
|
|
123
|
+
return {
|
|
124
|
+
kind,
|
|
125
|
+
rows: filtered.length,
|
|
126
|
+
excluded,
|
|
127
|
+
byStatus,
|
|
128
|
+
ready,
|
|
129
|
+
blocked,
|
|
130
|
+
overdue,
|
|
131
|
+
unassigned,
|
|
132
|
+
orphans,
|
|
133
|
+
noNotes,
|
|
134
|
+
staleDone,
|
|
135
|
+
picked,
|
|
136
|
+
nextMoves,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
function truncateName(name, max = 60) {
|
|
140
|
+
return name.length > max ? `${name.slice(0, max - 1)}…` : name;
|
|
141
|
+
}
|
|
142
|
+
export function renderDigest(d, meta, opts) {
|
|
143
|
+
const top = opts?.top ?? 5;
|
|
144
|
+
const lines = [];
|
|
145
|
+
lines.push(`${d.rows} ${d.kind} rows of ${meta.total ?? '?'} total · asOf ${meta.asOf ?? '?'}`);
|
|
146
|
+
if (meta.truncated) {
|
|
147
|
+
const t = meta.truncated;
|
|
148
|
+
const parts = [];
|
|
149
|
+
if (t.rows !== undefined)
|
|
150
|
+
parts.push(`at least ${t.rows} rows exist (total is a floor)`);
|
|
151
|
+
if (t.edges)
|
|
152
|
+
parts.push('edges capped (blocked/orphan counts may be wrong)');
|
|
153
|
+
if (t.tags !== undefined)
|
|
154
|
+
parts.push(`${t.tags} rows may be missing tags — do not plan from tags alone`);
|
|
155
|
+
lines.push(`TRUNCATED: ${parts.join('; ')}`);
|
|
156
|
+
}
|
|
157
|
+
if (meta.nextCursor) {
|
|
158
|
+
lines.push('SHORT: more rows exist beyond this read — rerun with --all');
|
|
159
|
+
}
|
|
160
|
+
if (d.excluded.approval > 0) {
|
|
161
|
+
lines.push(`${d.excluded.approval} approvals excluded (parked workflow runs — resolve with tasks_approve / workflow:stop, never by status). --kind all to include.`);
|
|
162
|
+
}
|
|
163
|
+
const statusEntries = Object.entries(d.byStatus).sort((a, b) => b[1] - a[1]);
|
|
164
|
+
lines.push(`status ${statusEntries.map(([k, v]) => `${k}=${v}`).join(' ')}`);
|
|
165
|
+
lines.push(`ready=${d.ready.length} blocked=${d.blocked.length} overdue=${d.overdue.length} unassigned=${d.unassigned.length} orphans=${d.orphans.length} noNotes=${d.noNotes === null ? 'unknown (notes not loaded)' : d.noNotes.length} staleDone=${d.staleDone.length} picked=${d.picked.length}`);
|
|
166
|
+
if (d.ready.length) {
|
|
167
|
+
lines.push(`ready (top ${top})`);
|
|
168
|
+
for (const r of d.ready.slice(0, top)) {
|
|
169
|
+
lines.push(` ${r.tid} p${r.priority} @${r.assignee ?? '-'} ${truncateName(r.name)}`);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
if (d.blocked.length) {
|
|
173
|
+
lines.push(`blocked (top ${top})`);
|
|
174
|
+
for (const b of d.blocked.slice(0, top)) {
|
|
175
|
+
let line = ` ${b.tid} ${truncateName(b.name)}`;
|
|
176
|
+
if (b.blockers.length) {
|
|
177
|
+
const blockerParts = b.blockers.map((bl) => `${bl.tid} (${bl.status ?? '?'})`).join(', ');
|
|
178
|
+
line += ` ← ${blockerParts}`;
|
|
179
|
+
}
|
|
180
|
+
if (b.outside > 0)
|
|
181
|
+
line += ` +${b.outside} outside view`;
|
|
182
|
+
if (b.staleStatus)
|
|
183
|
+
line += ' STALE STATUS';
|
|
184
|
+
lines.push(line);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
if (d.staleDone.length) {
|
|
188
|
+
lines.push(`stale DONE (top ${top})`);
|
|
189
|
+
for (const r of d.staleDone.slice(0, top)) {
|
|
190
|
+
lines.push(` ${r.tid} ${truncateName(r.name)}`);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
if (d.nextMoves.length) {
|
|
194
|
+
lines.push('next moves');
|
|
195
|
+
d.nextMoves.forEach((m, i) => {
|
|
196
|
+
lines.push(` ${i + 1}. ${m.action} ${m.tid} ${truncateName(m.name)} — ${m.why}`);
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
return lines.join('\n');
|
|
200
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { BoardRes, BulkEdit } from '@oneie/sdk/tasks-bulk';
|
|
2
|
+
export type Row = NonNullable<BoardRes['tasks']>[number];
|
|
3
|
+
export declare const CLOSED: ReadonlySet<string>;
|
|
4
|
+
export interface Hints {
|
|
5
|
+
model?: string;
|
|
6
|
+
effort?: string;
|
|
7
|
+
touches: string[];
|
|
8
|
+
accept?: string;
|
|
9
|
+
}
|
|
10
|
+
export declare function parseHints(notes?: string): Hints;
|
|
11
|
+
export declare function subtree(rows: Row[], rootTid: string): Row[];
|
|
12
|
+
export interface Implied {
|
|
13
|
+
tid: string;
|
|
14
|
+
waitsFor: string;
|
|
15
|
+
reason: 'containment' | 'touches';
|
|
16
|
+
path?: string;
|
|
17
|
+
}
|
|
18
|
+
export interface PlanResult {
|
|
19
|
+
waves: {
|
|
20
|
+
n: number;
|
|
21
|
+
tids: string[];
|
|
22
|
+
}[];
|
|
23
|
+
criticalPath: {
|
|
24
|
+
hops: number;
|
|
25
|
+
tids: string[];
|
|
26
|
+
};
|
|
27
|
+
cycles: string[][];
|
|
28
|
+
missing: {
|
|
29
|
+
tid: string;
|
|
30
|
+
blockedBy: string;
|
|
31
|
+
}[];
|
|
32
|
+
outside: {
|
|
33
|
+
tid: string;
|
|
34
|
+
open: number;
|
|
35
|
+
}[];
|
|
36
|
+
ownerless: string[];
|
|
37
|
+
implied: Implied[];
|
|
38
|
+
closed: string[];
|
|
39
|
+
}
|
|
40
|
+
export declare function planWaves(rows: Row[]): PlanResult;
|
|
41
|
+
export interface DispatchRow {
|
|
42
|
+
tid: string;
|
|
43
|
+
name: string;
|
|
44
|
+
assignee?: string;
|
|
45
|
+
model?: string;
|
|
46
|
+
effort?: string;
|
|
47
|
+
touches: string[];
|
|
48
|
+
accept?: string;
|
|
49
|
+
}
|
|
50
|
+
export interface Dispatch {
|
|
51
|
+
goal: string;
|
|
52
|
+
asOf?: string;
|
|
53
|
+
truncated?: unknown;
|
|
54
|
+
waves: {
|
|
55
|
+
n: number;
|
|
56
|
+
rows: DispatchRow[];
|
|
57
|
+
}[];
|
|
58
|
+
criticalPath: {
|
|
59
|
+
hops: number;
|
|
60
|
+
tids: string[];
|
|
61
|
+
};
|
|
62
|
+
warnings: string[];
|
|
63
|
+
}
|
|
64
|
+
export declare function toDispatch(res: PlanResult, rows: Row[], meta: {
|
|
65
|
+
goal: string;
|
|
66
|
+
asOf?: string;
|
|
67
|
+
truncated?: unknown;
|
|
68
|
+
}): Dispatch;
|
|
69
|
+
export declare function sequenceEdits(tids: string[]): {
|
|
70
|
+
ok: true;
|
|
71
|
+
edits: BulkEdit[];
|
|
72
|
+
} | {
|
|
73
|
+
ok: false;
|
|
74
|
+
error: string;
|
|
75
|
+
};
|
|
76
|
+
export declare function renderPlan(res: PlanResult, rows: Row[], opts?: {
|
|
77
|
+
goal?: string;
|
|
78
|
+
}): string;
|
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
// CLOSED = satisfied. A closed row is never scheduled into a wave and every
|
|
2
|
+
// edge pointing AT one is dropped before the graph is built.
|
|
3
|
+
export const CLOSED = new Set(['done', 'verified', 'dissolved', 'failed']);
|
|
4
|
+
const HINT_LINE = /^\s*(model|effort|touches|accept)\s*:\s*(.*)$/i;
|
|
5
|
+
// Dispatch hints live in a row's notes as labelled lines, one per line,
|
|
6
|
+
// case-insensitive key, anywhere in the prose.
|
|
7
|
+
export function parseHints(notes) {
|
|
8
|
+
const hints = { touches: [] };
|
|
9
|
+
if (!notes)
|
|
10
|
+
return hints;
|
|
11
|
+
for (const line of notes.split('\n')) {
|
|
12
|
+
const m = HINT_LINE.exec(line);
|
|
13
|
+
if (!m)
|
|
14
|
+
continue;
|
|
15
|
+
const key = m[1].toLowerCase();
|
|
16
|
+
const value = m[2].trim();
|
|
17
|
+
if (!value)
|
|
18
|
+
continue;
|
|
19
|
+
if (key === 'model')
|
|
20
|
+
hints.model = value;
|
|
21
|
+
else if (key === 'effort')
|
|
22
|
+
hints.effort = value;
|
|
23
|
+
else if (key === 'accept')
|
|
24
|
+
hints.accept = value;
|
|
25
|
+
else if (key === 'touches')
|
|
26
|
+
hints.touches = value.split(',').map((s) => s.trim()).filter(Boolean);
|
|
27
|
+
}
|
|
28
|
+
return hints;
|
|
29
|
+
}
|
|
30
|
+
// Root + all descendants via `parent`, root included; [] if root absent.
|
|
31
|
+
export function subtree(rows, rootTid) {
|
|
32
|
+
const byTid = new Map(rows.map((r) => [r.tid, r]));
|
|
33
|
+
if (!byTid.has(rootTid))
|
|
34
|
+
return [];
|
|
35
|
+
const children = new Map();
|
|
36
|
+
for (const r of rows) {
|
|
37
|
+
if (r.parent) {
|
|
38
|
+
const arr = children.get(r.parent) ?? [];
|
|
39
|
+
arr.push(r);
|
|
40
|
+
children.set(r.parent, arr);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const out = [];
|
|
44
|
+
const seen = new Set();
|
|
45
|
+
const queue = [rootTid];
|
|
46
|
+
while (queue.length > 0) {
|
|
47
|
+
const tid = queue.shift();
|
|
48
|
+
if (seen.has(tid))
|
|
49
|
+
continue;
|
|
50
|
+
seen.add(tid);
|
|
51
|
+
const r = byTid.get(tid);
|
|
52
|
+
if (!r)
|
|
53
|
+
continue;
|
|
54
|
+
out.push(r);
|
|
55
|
+
for (const c of children.get(tid) ?? [])
|
|
56
|
+
queue.push(c.tid);
|
|
57
|
+
}
|
|
58
|
+
return out;
|
|
59
|
+
}
|
|
60
|
+
export function planWaves(rows) {
|
|
61
|
+
const fullMap = new Map();
|
|
62
|
+
for (const r of rows)
|
|
63
|
+
fullMap.set(r.tid, r);
|
|
64
|
+
const closed = [];
|
|
65
|
+
const nodeIds = [];
|
|
66
|
+
for (const r of rows) {
|
|
67
|
+
if (CLOSED.has(r.status))
|
|
68
|
+
closed.push(r.tid);
|
|
69
|
+
else
|
|
70
|
+
nodeIds.push(r.tid);
|
|
71
|
+
}
|
|
72
|
+
const nodeSet = new Set(nodeIds);
|
|
73
|
+
const missing = [];
|
|
74
|
+
const outsideMarked = new Set();
|
|
75
|
+
const outside = [];
|
|
76
|
+
const ownerless = [];
|
|
77
|
+
const implied = [];
|
|
78
|
+
const preds = new Map();
|
|
79
|
+
const succs = new Map();
|
|
80
|
+
for (const tid of nodeIds) {
|
|
81
|
+
preds.set(tid, new Set());
|
|
82
|
+
succs.set(tid, new Set());
|
|
83
|
+
}
|
|
84
|
+
const addEdge = (prereq, waiter) => {
|
|
85
|
+
preds.get(waiter)?.add(prereq);
|
|
86
|
+
succs.get(prereq)?.add(waiter);
|
|
87
|
+
};
|
|
88
|
+
// blockedBy edges + missing/outside accounting
|
|
89
|
+
for (const tid of nodeIds) {
|
|
90
|
+
const r = fullMap.get(tid);
|
|
91
|
+
if (!r.assignee)
|
|
92
|
+
ownerless.push(tid);
|
|
93
|
+
const blockedByArr = r.blockedBy ?? [];
|
|
94
|
+
const absent = [];
|
|
95
|
+
let inSetOpen = 0;
|
|
96
|
+
for (const b of blockedByArr) {
|
|
97
|
+
const br = fullMap.get(b);
|
|
98
|
+
if (!br) {
|
|
99
|
+
absent.push(b);
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (CLOSED.has(br.status))
|
|
103
|
+
continue;
|
|
104
|
+
inSetOpen += 1;
|
|
105
|
+
if (nodeSet.has(b))
|
|
106
|
+
addEdge(b, tid);
|
|
107
|
+
}
|
|
108
|
+
// A blocker absent from the rows is ambiguous: the board read is usually
|
|
109
|
+
// ACTIVE rows only, so a CLOSED blocker is absent too, and so is an open one
|
|
110
|
+
// outside this goal's subtree. The server's openBlockers settles it — never
|
|
111
|
+
// the raw blockedBy (a closed blocker once read as "absent", which is the
|
|
112
|
+
// defect this module exists to stop repeating). Only when the server gave no
|
|
113
|
+
// count is an absent blocker unknowable, and only then is it `missing`.
|
|
114
|
+
if (r.openBlockers === undefined) {
|
|
115
|
+
for (const b of absent)
|
|
116
|
+
missing.push({ tid, blockedBy: b });
|
|
117
|
+
}
|
|
118
|
+
else if (r.openBlockers > inSetOpen) {
|
|
119
|
+
outsideMarked.add(tid);
|
|
120
|
+
outside.push({ tid, open: r.openBlockers - inSetOpen });
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
// containment: a parent waits for every non-closed child present in the set.
|
|
124
|
+
for (const tid of nodeIds) {
|
|
125
|
+
const r = fullMap.get(tid);
|
|
126
|
+
if (r.parent && nodeSet.has(r.parent)) {
|
|
127
|
+
addEdge(tid, r.parent);
|
|
128
|
+
implied.push({ tid: r.parent, waitsFor: tid, reason: 'containment' });
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
const priorityOf = (tid) => fullMap.get(tid)?.priority ?? 0;
|
|
132
|
+
const runKahn = () => {
|
|
133
|
+
const inDeg = new Map();
|
|
134
|
+
for (const tid of nodeIds)
|
|
135
|
+
inDeg.set(tid, preds.get(tid)?.size ?? 0);
|
|
136
|
+
const scheduled = new Set();
|
|
137
|
+
const waves = [];
|
|
138
|
+
let current = nodeIds.filter((t) => inDeg.get(t) === 0 && !outsideMarked.has(t));
|
|
139
|
+
while (current.length > 0) {
|
|
140
|
+
for (const t of current)
|
|
141
|
+
scheduled.add(t);
|
|
142
|
+
waves.push(current);
|
|
143
|
+
const nextCandidates = new Set();
|
|
144
|
+
for (const t of current) {
|
|
145
|
+
for (const s of succs.get(t) ?? []) {
|
|
146
|
+
const d = (inDeg.get(s) ?? 0) - 1;
|
|
147
|
+
inDeg.set(s, d);
|
|
148
|
+
if (d === 0)
|
|
149
|
+
nextCandidates.add(s);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
current = [...nextCandidates].filter((t) => !outsideMarked.has(t) && !scheduled.has(t));
|
|
153
|
+
}
|
|
154
|
+
return { waves, scheduled };
|
|
155
|
+
};
|
|
156
|
+
// Fixpoint: run Kahn's algorithm, look for same-wave `touches:` collisions,
|
|
157
|
+
// add the implied serialising edge, and recompute until stable.
|
|
158
|
+
let kahn = runKahn();
|
|
159
|
+
const seenTouchesEdge = new Set();
|
|
160
|
+
const maxIters = nodeIds.length + 5;
|
|
161
|
+
for (let iter = 0; iter < maxIters; iter++) {
|
|
162
|
+
let added = false;
|
|
163
|
+
for (const wave of kahn.waves) {
|
|
164
|
+
if (wave.length < 2)
|
|
165
|
+
continue;
|
|
166
|
+
const touchesOf = new Map();
|
|
167
|
+
for (const tid of wave)
|
|
168
|
+
touchesOf.set(tid, parseHints(fullMap.get(tid)?.notes).touches);
|
|
169
|
+
for (let i = 0; i < wave.length; i++) {
|
|
170
|
+
for (let j = i + 1; j < wave.length; j++) {
|
|
171
|
+
const a = wave[i];
|
|
172
|
+
const b = wave[j];
|
|
173
|
+
const aTouches = touchesOf.get(a) ?? [];
|
|
174
|
+
const bTouches = touchesOf.get(b) ?? [];
|
|
175
|
+
const shared = aTouches.find((p) => bTouches.includes(p));
|
|
176
|
+
if (!shared)
|
|
177
|
+
continue;
|
|
178
|
+
if (preds.get(a)?.has(b) || preds.get(b)?.has(a))
|
|
179
|
+
continue;
|
|
180
|
+
const pa = priorityOf(a);
|
|
181
|
+
const pb = priorityOf(b);
|
|
182
|
+
let winner;
|
|
183
|
+
let loser;
|
|
184
|
+
if (pa !== pb) {
|
|
185
|
+
winner = pa > pb ? a : b;
|
|
186
|
+
loser = pa > pb ? b : a;
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
// tie -> lexically larger tid waits
|
|
190
|
+
winner = a < b ? a : b;
|
|
191
|
+
loser = a < b ? b : a;
|
|
192
|
+
}
|
|
193
|
+
const key = `${loser}->${winner}`;
|
|
194
|
+
if (seenTouchesEdge.has(key))
|
|
195
|
+
continue;
|
|
196
|
+
seenTouchesEdge.add(key);
|
|
197
|
+
addEdge(winner, loser);
|
|
198
|
+
implied.push({ tid: loser, waitsFor: winner, reason: 'touches', path: shared });
|
|
199
|
+
added = true;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
if (!added)
|
|
204
|
+
break;
|
|
205
|
+
kahn = runKahn();
|
|
206
|
+
}
|
|
207
|
+
// Leftover = never scheduled and not already explicitly marked outside.
|
|
208
|
+
const leftover = nodeIds.filter((t) => !kahn.scheduled.has(t) && !outsideMarked.has(t));
|
|
209
|
+
const leftoverSet = new Set(leftover);
|
|
210
|
+
const cycles = [];
|
|
211
|
+
{
|
|
212
|
+
let index = 0;
|
|
213
|
+
const indices = new Map();
|
|
214
|
+
const lowlink = new Map();
|
|
215
|
+
const onStack = new Set();
|
|
216
|
+
const stack = [];
|
|
217
|
+
const sccs = [];
|
|
218
|
+
const strongconnect = (v) => {
|
|
219
|
+
indices.set(v, index);
|
|
220
|
+
lowlink.set(v, index);
|
|
221
|
+
index += 1;
|
|
222
|
+
stack.push(v);
|
|
223
|
+
onStack.add(v);
|
|
224
|
+
for (const w of preds.get(v) ?? []) {
|
|
225
|
+
if (!leftoverSet.has(w))
|
|
226
|
+
continue;
|
|
227
|
+
if (!indices.has(w)) {
|
|
228
|
+
strongconnect(w);
|
|
229
|
+
lowlink.set(v, Math.min(lowlink.get(v), lowlink.get(w)));
|
|
230
|
+
}
|
|
231
|
+
else if (onStack.has(w)) {
|
|
232
|
+
lowlink.set(v, Math.min(lowlink.get(v), indices.get(w)));
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
if (lowlink.get(v) === indices.get(v)) {
|
|
236
|
+
const comp = [];
|
|
237
|
+
let w;
|
|
238
|
+
do {
|
|
239
|
+
w = stack.pop();
|
|
240
|
+
onStack.delete(w);
|
|
241
|
+
comp.push(w);
|
|
242
|
+
} while (w !== v);
|
|
243
|
+
sccs.push(comp);
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
for (const v of leftover) {
|
|
247
|
+
if (!indices.has(v))
|
|
248
|
+
strongconnect(v);
|
|
249
|
+
}
|
|
250
|
+
const cycleMembers = new Set();
|
|
251
|
+
for (const comp of sccs) {
|
|
252
|
+
const isCycle = comp.length > 1 || preds.get(comp[0])?.has(comp[0]);
|
|
253
|
+
if (isCycle) {
|
|
254
|
+
cycles.push(comp.slice().sort());
|
|
255
|
+
for (const t of comp)
|
|
256
|
+
cycleMembers.add(t);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
// Downstream of a cycle (or of an outside-marked node) but not in a
|
|
260
|
+
// cycle itself: still excluded from waves, reported as `outside`.
|
|
261
|
+
for (const t of leftover) {
|
|
262
|
+
if (cycleMembers.has(t))
|
|
263
|
+
continue;
|
|
264
|
+
const unresolved = [...(preds.get(t) ?? [])].filter((p) => !kahn.scheduled.has(p)).length;
|
|
265
|
+
outside.push({ tid: t, open: unresolved });
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
// Longest path (by node count) over the final DAG, in execution order.
|
|
269
|
+
const longest = new Map();
|
|
270
|
+
const pathParent = new Map();
|
|
271
|
+
for (const wave of kahn.waves) {
|
|
272
|
+
for (const t of wave) {
|
|
273
|
+
let best = 1;
|
|
274
|
+
let bestParent;
|
|
275
|
+
for (const p of preds.get(t) ?? []) {
|
|
276
|
+
if (!kahn.scheduled.has(p))
|
|
277
|
+
continue;
|
|
278
|
+
const val = (longest.get(p) ?? 1) + 1;
|
|
279
|
+
if (val > best) {
|
|
280
|
+
best = val;
|
|
281
|
+
bestParent = p;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
longest.set(t, best);
|
|
285
|
+
pathParent.set(t, bestParent);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
let bestTid;
|
|
289
|
+
let bestLen = 0;
|
|
290
|
+
for (const t of kahn.waves.flat()) {
|
|
291
|
+
const l = longest.get(t) ?? 1;
|
|
292
|
+
if (l > bestLen) {
|
|
293
|
+
bestLen = l;
|
|
294
|
+
bestTid = t;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
const pathTids = [];
|
|
298
|
+
let cur = bestTid;
|
|
299
|
+
while (cur !== undefined) {
|
|
300
|
+
pathTids.unshift(cur);
|
|
301
|
+
cur = pathParent.get(cur);
|
|
302
|
+
}
|
|
303
|
+
const criticalPath = { hops: Math.max(pathTids.length - 1, 0), tids: pathTids };
|
|
304
|
+
const waves = kahn.waves.map((tids, i) => ({
|
|
305
|
+
n: i + 1,
|
|
306
|
+
tids: tids.slice().sort((a, b) => priorityOf(b) - priorityOf(a)),
|
|
307
|
+
}));
|
|
308
|
+
const seenOutside = new Set();
|
|
309
|
+
const outsideDeduped = [];
|
|
310
|
+
for (const o of outside) {
|
|
311
|
+
if (seenOutside.has(o.tid))
|
|
312
|
+
continue;
|
|
313
|
+
seenOutside.add(o.tid);
|
|
314
|
+
outsideDeduped.push(o);
|
|
315
|
+
}
|
|
316
|
+
return {
|
|
317
|
+
waves,
|
|
318
|
+
criticalPath,
|
|
319
|
+
cycles,
|
|
320
|
+
missing,
|
|
321
|
+
outside: outsideDeduped,
|
|
322
|
+
ownerless,
|
|
323
|
+
implied,
|
|
324
|
+
closed,
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
export function toDispatch(res, rows, meta) {
|
|
328
|
+
const byTid = new Map(rows.map((r) => [r.tid, r]));
|
|
329
|
+
const waves = res.waves.map((w) => ({
|
|
330
|
+
n: w.n,
|
|
331
|
+
rows: w.tids.map((tid) => {
|
|
332
|
+
const r = byTid.get(tid);
|
|
333
|
+
const hints = parseHints(r?.notes);
|
|
334
|
+
return {
|
|
335
|
+
tid,
|
|
336
|
+
name: r?.name ?? '',
|
|
337
|
+
assignee: r?.assignee,
|
|
338
|
+
model: hints.model,
|
|
339
|
+
effort: hints.effort,
|
|
340
|
+
touches: hints.touches,
|
|
341
|
+
accept: hints.accept,
|
|
342
|
+
};
|
|
343
|
+
}),
|
|
344
|
+
}));
|
|
345
|
+
const warnings = [];
|
|
346
|
+
for (const c of res.cycles)
|
|
347
|
+
warnings.push(`cycle: ${c.join(', ')}`);
|
|
348
|
+
for (const m of res.missing)
|
|
349
|
+
warnings.push(`${m.tid} blocked by missing ${m.blockedBy}`);
|
|
350
|
+
for (const o of res.outside)
|
|
351
|
+
warnings.push(`${o.tid} waits outside plan (open=${o.open})`);
|
|
352
|
+
if (res.ownerless.length > 0)
|
|
353
|
+
warnings.push(`${res.ownerless.length} rows ownerless`);
|
|
354
|
+
return {
|
|
355
|
+
goal: meta.goal,
|
|
356
|
+
asOf: meta.asOf,
|
|
357
|
+
truncated: meta.truncated,
|
|
358
|
+
waves,
|
|
359
|
+
criticalPath: res.criticalPath,
|
|
360
|
+
warnings,
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
// Chains tids in order: edits[i] = { tid: tids[i+1], addBlockedBy: [tids[i]] }.
|
|
364
|
+
export function sequenceEdits(tids) {
|
|
365
|
+
if (tids.length < 2)
|
|
366
|
+
return { ok: false, error: 'need at least two tids' };
|
|
367
|
+
const seen = new Set();
|
|
368
|
+
for (const t of tids) {
|
|
369
|
+
if (seen.has(t))
|
|
370
|
+
return { ok: false, error: `duplicate tid ${t}` };
|
|
371
|
+
seen.add(t);
|
|
372
|
+
}
|
|
373
|
+
const edits = [];
|
|
374
|
+
for (let i = 0; i < tids.length - 1; i++) {
|
|
375
|
+
edits.push({ tid: tids[i + 1], addBlockedBy: [tids[i]] });
|
|
376
|
+
}
|
|
377
|
+
return { ok: true, edits };
|
|
378
|
+
}
|
|
379
|
+
export function renderPlan(res, rows, opts) {
|
|
380
|
+
const byTid = new Map(rows.map((r) => [r.tid, r]));
|
|
381
|
+
const cpSet = new Set(res.criticalPath.tids);
|
|
382
|
+
const lines = [];
|
|
383
|
+
const goal = opts?.goal ?? '';
|
|
384
|
+
lines.push(`plan ${goal} — ${res.waves.length} waves · critical path ${res.criticalPath.hops} hops · ${rows.length} rows (${res.closed.length} closed skipped)`);
|
|
385
|
+
for (const wave of res.waves) {
|
|
386
|
+
lines.push(`wave ${wave.n}`);
|
|
387
|
+
for (const tid of wave.tids) {
|
|
388
|
+
const r = byTid.get(tid);
|
|
389
|
+
const name = (r?.name ?? '').slice(0, 60);
|
|
390
|
+
const assignee = r?.assignee ?? '-';
|
|
391
|
+
const priority = r?.priority ?? 0;
|
|
392
|
+
const marker = cpSet.has(tid) ? ' *' : '';
|
|
393
|
+
lines.push(` ${tid} p${priority} @${assignee} ${name}${marker}`);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
if (res.cycles.length > 0) {
|
|
397
|
+
lines.push('cycles');
|
|
398
|
+
for (const c of res.cycles)
|
|
399
|
+
lines.push(` ${c.join(', ')}`);
|
|
400
|
+
}
|
|
401
|
+
if (res.missing.length > 0) {
|
|
402
|
+
lines.push('missing blockers');
|
|
403
|
+
for (const m of res.missing)
|
|
404
|
+
lines.push(` ${m.tid} → ${m.blockedBy}`);
|
|
405
|
+
}
|
|
406
|
+
if (res.outside.length > 0) {
|
|
407
|
+
lines.push('waits outside plan');
|
|
408
|
+
for (const o of res.outside)
|
|
409
|
+
lines.push(` ${o.tid} (open=${o.open})`);
|
|
410
|
+
}
|
|
411
|
+
if (res.ownerless.length > 0) {
|
|
412
|
+
lines.push('ownerless');
|
|
413
|
+
for (const tid of res.ownerless)
|
|
414
|
+
lines.push(` ${tid}`);
|
|
415
|
+
}
|
|
416
|
+
if (res.implied.length > 0) {
|
|
417
|
+
lines.push('implied order');
|
|
418
|
+
for (const i of res.implied) {
|
|
419
|
+
const suffix = i.reason === 'touches' && i.path ? `: ${i.path}` : '';
|
|
420
|
+
lines.push(` ${i.tid} waits for ${i.waitsFor} (${i.reason}${suffix})`);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
lines.push(`critical path: ${res.criticalPath.tids.length > 0 ? res.criticalPath.tids.join(' → ') : '(none)'}`);
|
|
424
|
+
return lines.join('\n');
|
|
425
|
+
}
|