oneie 5.2.0 → 5.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -0
- package/dist/package.json +2 -2
- package/dist/src/config.d.ts +1 -1
- package/dist/src/config.js +1 -1
- package/dist/src/group.js +3 -1
- package/dist/src/index.js +7 -6
- package/dist/src/join.d.ts +11 -0
- package/dist/src/join.js +292 -0
- package/dist/src/key.d.ts +23 -0
- package/dist/src/key.js +270 -0
- package/dist/src/lib/asker.d.ts +34 -0
- package/dist/src/lib/asker.js +126 -0
- package/dist/src/lib/flow-tag.d.ts +86 -0
- package/dist/src/lib/flow-tag.js +230 -0
- package/dist/src/lib/plan-draw.d.ts +56 -0
- package/dist/src/lib/plan-draw.js +219 -0
- package/dist/src/lib/plan-live.d.ts +20 -0
- package/dist/src/lib/plan-live.js +192 -0
- package/dist/src/lib/roster.d.ts +20 -0
- package/dist/src/lib/roster.js +79 -0
- package/dist/src/lib/tasks-lens.d.ts +31 -0
- package/dist/src/lib/tasks-lens.js +47 -0
- package/dist/src/lib/tasks-next.d.ts +25 -0
- package/dist/src/lib/tasks-next.js +103 -0
- package/dist/src/lib/tasks-route.d.ts +29 -0
- package/dist/src/lib/tasks-route.js +151 -0
- package/dist/src/lib/tasks-score.d.ts +36 -0
- package/dist/src/lib/tasks-score.js +152 -0
- package/dist/src/lib/tasks-workspace.d.ts +18 -0
- package/dist/src/lib/tasks-workspace.js +73 -0
- package/dist/src/substrate.js +2 -1
- package/dist/src/tasks.js +352 -37
- package/dist/src/wallet.d.ts +2 -0
- package/dist/src/wallet.js +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { emitKeypressEvents } from 'node:readline';
|
|
2
|
+
import { createInterface } from 'node:readline/promises';
|
|
3
|
+
import { drawPlan, planDelta, planHeader, shortName, withOverrides } from './plan-draw.js';
|
|
4
|
+
const ESC = '\x1b[';
|
|
5
|
+
const PAINT = {
|
|
6
|
+
selected: `${ESC}7m`, // inverse
|
|
7
|
+
critical: `${ESC}33m`, // yellow
|
|
8
|
+
ready: `${ESC}32m`, // green
|
|
9
|
+
changed: `${ESC}1;35m`, // bold magenta
|
|
10
|
+
dim: `${ESC}2m`,
|
|
11
|
+
};
|
|
12
|
+
const RESET = `${ESC}0m`;
|
|
13
|
+
const HELP = '←→↑↓ select · c claim · d done · o reopen · m comment · enter notes · r refresh · q quit';
|
|
14
|
+
export async function runLivePlan(o) {
|
|
15
|
+
const input = o.input ?? process.stdin;
|
|
16
|
+
const out = o.output ?? process.stdout;
|
|
17
|
+
let data = await o.load();
|
|
18
|
+
const overrides = new Map();
|
|
19
|
+
let changed = new Set();
|
|
20
|
+
let message = 'Pick a row with the arrow keys, then act on it.';
|
|
21
|
+
let showNotes = false;
|
|
22
|
+
let selected;
|
|
23
|
+
let busy = false;
|
|
24
|
+
const view = () => withOverrides(data.rows, overrides);
|
|
25
|
+
const render = () => {
|
|
26
|
+
const rows = view();
|
|
27
|
+
const world = data.world; // the snapshot as read — never the local overrides
|
|
28
|
+
const d = drawPlan(rows, { selected, changed, world });
|
|
29
|
+
if (!selected || !d.boxes.has(selected))
|
|
30
|
+
selected = d.columns.flat()[0];
|
|
31
|
+
const d2 = selected ? drawPlan(rows, { selected, changed, world }) : d;
|
|
32
|
+
const cols = out.columns || 120;
|
|
33
|
+
const height = (out.rows || 40) - 9;
|
|
34
|
+
const sel = selected ? d2.boxes.get(selected) : undefined;
|
|
35
|
+
// Scroll so the selected box is always on screen.
|
|
36
|
+
const x0 = sel ? Math.max(0, Math.min(sel.x - 4, sel.x + 30 - cols)) : 0;
|
|
37
|
+
const y0 = sel ? Math.max(0, sel.y + 6 - height) : 0;
|
|
38
|
+
const body = [];
|
|
39
|
+
for (let y = y0; y < Math.min(d2.lines.length, y0 + height); y++) {
|
|
40
|
+
const line = d2.lines[y] ?? '';
|
|
41
|
+
let s = '';
|
|
42
|
+
let cur;
|
|
43
|
+
for (let x = x0; x < Math.min(line.length, x0 + cols); x++) {
|
|
44
|
+
const st = d2.styles.get(`${x},${y}`);
|
|
45
|
+
if (st !== cur) {
|
|
46
|
+
s += RESET + (st ? PAINT[st] : '');
|
|
47
|
+
cur = st;
|
|
48
|
+
}
|
|
49
|
+
s += line[x];
|
|
50
|
+
}
|
|
51
|
+
body.push(s + RESET);
|
|
52
|
+
}
|
|
53
|
+
const r = selected ? rows.find((t) => t.tid === selected) : undefined;
|
|
54
|
+
const header = planHeader(rows, d2.plan, { title: o.title, asOf: data.asOf, world });
|
|
55
|
+
const detail = r ? [`${ESC}1m${r.name}${RESET}`, `${r.tid} · ${r.status}${r.assignee ? ' · @' + r.assignee : ''} · priority ${r.priority ?? 0}`] : [''];
|
|
56
|
+
const notes = showNotes && r ? (String(r.notes ?? '(no notes)')).split('\n').slice(0, 12) : [];
|
|
57
|
+
out.write(`${ESC}2J${ESC}H`);
|
|
58
|
+
out.write([...header, '', ...body, '', ...detail, ...notes, '', `${ESC}1m${message}${RESET}`, `${ESC}2m${HELP}${RESET}`].join('\n') + '\n');
|
|
59
|
+
};
|
|
60
|
+
const move = (dx, dy) => {
|
|
61
|
+
const rows = view();
|
|
62
|
+
const d = drawPlan(rows, { world: data.world });
|
|
63
|
+
const here = selected ? d.boxes.get(selected) : undefined;
|
|
64
|
+
if (!here) {
|
|
65
|
+
selected = d.columns.flat()[0];
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const col = Math.max(0, Math.min(d.columns.length - 1, here.col + dx));
|
|
69
|
+
const list = d.columns[col] ?? [];
|
|
70
|
+
const idx = dx !== 0 ? Math.min(here.idx, list.length - 1) : Math.max(0, Math.min(list.length - 1, here.idx + dy));
|
|
71
|
+
selected = list[idx] ?? selected;
|
|
72
|
+
};
|
|
73
|
+
const doAct = async (label, receiver, extra, nextStatus) => {
|
|
74
|
+
if (!selected || busy)
|
|
75
|
+
return;
|
|
76
|
+
const tid = selected;
|
|
77
|
+
const before = view();
|
|
78
|
+
busy = true;
|
|
79
|
+
message = `${label} ${shortName(before.find((r) => r.tid === tid))} …`;
|
|
80
|
+
render();
|
|
81
|
+
try {
|
|
82
|
+
const res = await o.act(receiver, { tid, ...(o.workspace ? { workspace: o.workspace } : {}), ...extra });
|
|
83
|
+
if (!res.ok) {
|
|
84
|
+
message = `✗ ${label} refused: ${res.error ?? 'unknown'}${res.detail ? ' — ' + res.detail : ''} (nothing changed)`;
|
|
85
|
+
changed = new Set();
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
if (nextStatus)
|
|
89
|
+
overrides.set(tid, nextStatus);
|
|
90
|
+
const after = view();
|
|
91
|
+
const delta = planDelta(before, after, data.world);
|
|
92
|
+
changed = new Set([tid, ...delta.nowReady]);
|
|
93
|
+
const name = (t) => shortName(after.find((r) => r.tid === t));
|
|
94
|
+
const parts = [`✓ ${label} ${name(tid)}`];
|
|
95
|
+
if (delta.nowReady.length)
|
|
96
|
+
parts.push(`now ready: ${delta.nowReady.map(name).join(', ')}`);
|
|
97
|
+
if (delta.critBefore.join() !== delta.critAfter.join()) {
|
|
98
|
+
parts.push(`critical path ${delta.critBefore.length} → ${delta.critAfter.length} steps`);
|
|
99
|
+
}
|
|
100
|
+
parts.push('(from the receipt; the board snapshot catches up within 60s — r to re-read)');
|
|
101
|
+
message = parts.join(' · ');
|
|
102
|
+
}
|
|
103
|
+
catch (e) {
|
|
104
|
+
message = `✗ ${label} failed: ${e.message} (did not land — nothing changed)`;
|
|
105
|
+
}
|
|
106
|
+
finally {
|
|
107
|
+
busy = false;
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
const ask = async (prompt) => {
|
|
111
|
+
input.setRawMode?.(false);
|
|
112
|
+
const rl = createInterface({ input, output: out });
|
|
113
|
+
const answer = await rl.question(prompt);
|
|
114
|
+
rl.close();
|
|
115
|
+
input.setRawMode?.(true);
|
|
116
|
+
return answer.trim();
|
|
117
|
+
};
|
|
118
|
+
emitKeypressEvents(input);
|
|
119
|
+
input.setRawMode?.(true);
|
|
120
|
+
input.resume();
|
|
121
|
+
out.write(`${ESC}?25l`); // hide cursor
|
|
122
|
+
render();
|
|
123
|
+
await new Promise((resolve) => {
|
|
124
|
+
const quit = () => {
|
|
125
|
+
input.setRawMode?.(false);
|
|
126
|
+
input.pause();
|
|
127
|
+
input.removeListener('keypress', onKey);
|
|
128
|
+
out.write(`${ESC}?25h\n`);
|
|
129
|
+
resolve();
|
|
130
|
+
};
|
|
131
|
+
const onKey = async (_s, key) => {
|
|
132
|
+
if (busy)
|
|
133
|
+
return;
|
|
134
|
+
if (key.ctrl && key.name === 'c')
|
|
135
|
+
return quit();
|
|
136
|
+
switch (key.name) {
|
|
137
|
+
case 'q': return quit();
|
|
138
|
+
case 'left':
|
|
139
|
+
move(-1, 0);
|
|
140
|
+
break;
|
|
141
|
+
case 'right':
|
|
142
|
+
move(1, 0);
|
|
143
|
+
break;
|
|
144
|
+
case 'up':
|
|
145
|
+
move(0, -1);
|
|
146
|
+
break;
|
|
147
|
+
case 'down':
|
|
148
|
+
move(0, 1);
|
|
149
|
+
break;
|
|
150
|
+
case 'return':
|
|
151
|
+
showNotes = !showNotes;
|
|
152
|
+
break;
|
|
153
|
+
case 'c':
|
|
154
|
+
await doAct('claimed', 'tasks:claim', {}, 'picked');
|
|
155
|
+
break;
|
|
156
|
+
case 'd':
|
|
157
|
+
await doAct('done', 'tasks:status', { status: 'done' }, 'done');
|
|
158
|
+
break;
|
|
159
|
+
case 'o':
|
|
160
|
+
await doAct('reopened', 'tasks:status', { status: 'open' }, 'open');
|
|
161
|
+
break;
|
|
162
|
+
case 'm': {
|
|
163
|
+
const body = await ask('comment (enter to post, empty to cancel): ');
|
|
164
|
+
if (body)
|
|
165
|
+
await doAct('commented on', 'tasks:comment', { body });
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
case 'r': {
|
|
169
|
+
message = 're-reading the board …';
|
|
170
|
+
render();
|
|
171
|
+
try {
|
|
172
|
+
data = await o.load();
|
|
173
|
+
// A receipt stays applied until the snapshot agrees with it.
|
|
174
|
+
for (const [tid, st] of overrides)
|
|
175
|
+
if (data.rows.find((r) => r.tid === tid)?.status === st)
|
|
176
|
+
overrides.delete(tid);
|
|
177
|
+
message = overrides.size
|
|
178
|
+
? `board re-read · ${overrides.size} change(s) still ahead of the snapshot, shown from their receipts`
|
|
179
|
+
: 'board re-read · the snapshot now matches every change you made';
|
|
180
|
+
}
|
|
181
|
+
catch (e) {
|
|
182
|
+
message = `✗ re-read failed: ${e.message} — still showing the last read`;
|
|
183
|
+
}
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
default: return;
|
|
187
|
+
}
|
|
188
|
+
render();
|
|
189
|
+
};
|
|
190
|
+
input.on('keypress', onKey);
|
|
191
|
+
});
|
|
192
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Seat } from './tasks-lens.js';
|
|
2
|
+
export type RosterResult = {
|
|
3
|
+
ok: true;
|
|
4
|
+
seats: Seat[];
|
|
5
|
+
source: string;
|
|
6
|
+
} | {
|
|
7
|
+
ok: false;
|
|
8
|
+
error: string;
|
|
9
|
+
};
|
|
10
|
+
/** Keep only well-formed seats; a role with no id is not addressable. */
|
|
11
|
+
export declare function toSeats(raw: unknown): Seat[];
|
|
12
|
+
/** The roster route answers on the web host only — never the gateway. */
|
|
13
|
+
export declare function webBase(base: string): string;
|
|
14
|
+
export declare function loadRoster(opts: {
|
|
15
|
+
base: string;
|
|
16
|
+
workspace?: string;
|
|
17
|
+
file?: string;
|
|
18
|
+
headers: Record<string, string>;
|
|
19
|
+
fetchImpl?: typeof fetch;
|
|
20
|
+
}): Promise<RosterResult>;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// roster — where a seat's stake is read from, for `brief --as` / `route` / `next`.
|
|
2
|
+
//
|
|
3
|
+
// The stake is the persona's `subscribes:` and its place in the chart is `reports_to:`.
|
|
4
|
+
// Both are served by ONE route, GET /api/agents/roster (one.ie/web — the same parse of
|
|
5
|
+
// `{slug}/agents/*.md` that world:sync-subscriptions turns into follows rows), so the
|
|
6
|
+
// lens and the router read one declaration. Three facts measured 2026-09-28 shape this:
|
|
7
|
+
// - the route lives on the web host only: api.one.ie answers it 404, so the base is
|
|
8
|
+
// rewritten to one.ie whatever --api says;
|
|
9
|
+
// - `source=workspace` needs authority over the group (403 for a key that lacks it),
|
|
10
|
+
// `source=fleet` needs only a world key — so we fall back to the bundled fleet and SAY
|
|
11
|
+
// so, because fleet is not what the router reads for a group with its own personas;
|
|
12
|
+
// - the shared gateway service key is not a world key (401 no_session on both), so
|
|
13
|
+
// `--roster <file>` (a saved response) is the door for a caller holding only that.
|
|
14
|
+
import { readFileSync } from 'node:fs';
|
|
15
|
+
/** Keep only well-formed seats; a role with no id is not addressable. */
|
|
16
|
+
export function toSeats(raw) {
|
|
17
|
+
const list = Array.isArray(raw) ? raw : raw && typeof raw === 'object' && Array.isArray(raw.roles) ? raw.roles : [];
|
|
18
|
+
const out = [];
|
|
19
|
+
for (const r of list) {
|
|
20
|
+
if (!r || typeof r.id !== 'string' || !r.id)
|
|
21
|
+
continue;
|
|
22
|
+
out.push({
|
|
23
|
+
id: r.id,
|
|
24
|
+
...(typeof r.tier === 'string' ? { tier: r.tier } : {}),
|
|
25
|
+
reportsTo: typeof r.reportsTo === 'string' && r.reportsTo ? r.reportsTo : null,
|
|
26
|
+
subscribes: Array.isArray(r.subscribes) ? r.subscribes.filter((t) => typeof t === 'string') : [],
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
return out;
|
|
30
|
+
}
|
|
31
|
+
/** The roster route answers on the web host only — never the gateway. */
|
|
32
|
+
export function webBase(base) {
|
|
33
|
+
return new URL(base).hostname === 'api.one.ie' ? 'https://one.ie' : base;
|
|
34
|
+
}
|
|
35
|
+
export async function loadRoster(opts) {
|
|
36
|
+
if (opts.file) {
|
|
37
|
+
try {
|
|
38
|
+
const seats = toSeats(JSON.parse(readFileSync(opts.file, 'utf8')));
|
|
39
|
+
if (!seats.length)
|
|
40
|
+
return { ok: false, error: `--roster ${opts.file}: no roles in it (want {roles:[{id,reportsTo,subscribes}]})` };
|
|
41
|
+
return { ok: true, seats, source: `file ${opts.file}` };
|
|
42
|
+
}
|
|
43
|
+
catch (e) {
|
|
44
|
+
return { ok: false, error: `--roster: ${e instanceof Error ? e.message : String(e)}` };
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (!opts.workspace)
|
|
48
|
+
return { ok: false, error: 'reading a seat lens needs --workspace (whose roster?), --roster <file>, or --tags' };
|
|
49
|
+
const f = opts.fetchImpl ?? fetch;
|
|
50
|
+
const web = webBase(opts.base);
|
|
51
|
+
const url = (source) => `${web}/api/agents/roster?slug=${encodeURIComponent(opts.workspace)}${source ? `&source=${source}` : ''}`;
|
|
52
|
+
const refusals = [];
|
|
53
|
+
for (const source of [undefined, 'fleet']) {
|
|
54
|
+
let res;
|
|
55
|
+
try {
|
|
56
|
+
res = await f(url(source), { headers: opts.headers });
|
|
57
|
+
}
|
|
58
|
+
catch (e) {
|
|
59
|
+
refusals.push(`${source ?? 'workspace'}: ${e instanceof Error ? e.message : String(e)}`);
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
const text = await res.text();
|
|
63
|
+
if (!res.ok) {
|
|
64
|
+
refusals.push(`${source ?? 'workspace'}: ${res.status} ${text.slice(0, 80)}`);
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
try {
|
|
68
|
+
const seats = toSeats(JSON.parse(text));
|
|
69
|
+
if (seats.length) {
|
|
70
|
+
return { ok: true, seats, source: source === 'fleet' ? 'fleet (bundled agents — NOT this group\'s own personas; the router reads those)' : `workspace ${opts.workspace}` };
|
|
71
|
+
}
|
|
72
|
+
refusals.push(`${source ?? 'workspace'}: empty roster`);
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
refusals.push(`${source ?? 'workspace'}: ${res.status} not JSON`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return { ok: false, error: `could not read the roster (${refusals.join('; ')}) — pass --roster <file> or --tags` };
|
|
79
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Row } from './tasks-digest.js';
|
|
2
|
+
/** One seat from the org roster: its id, tier, who it reports to, and its staked (subscribed) bare tags. */
|
|
3
|
+
export interface Seat {
|
|
4
|
+
id: string;
|
|
5
|
+
tier?: string;
|
|
6
|
+
reportsTo: string | null;
|
|
7
|
+
subscribes: string[];
|
|
8
|
+
}
|
|
9
|
+
/** A seat's lens: its bare tags (sorted, never '*'), itself + every seat below it (sorted), and all=true when it staked '*'. */
|
|
10
|
+
export interface Lens {
|
|
11
|
+
seat: string;
|
|
12
|
+
tags: string[];
|
|
13
|
+
seats: string[];
|
|
14
|
+
all: boolean;
|
|
15
|
+
}
|
|
16
|
+
export type LensResult = {
|
|
17
|
+
ok: true;
|
|
18
|
+
lens: Lens;
|
|
19
|
+
} | {
|
|
20
|
+
ok: false;
|
|
21
|
+
error: string;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* BUILD LENS — A director owns a stake, not just its own assignments.
|
|
25
|
+
* Walks the reporting tree downward to find every direct/indirect report.
|
|
26
|
+
*/
|
|
27
|
+
export declare function buildLens(roster: Seat[], seat: string): LensResult;
|
|
28
|
+
/**
|
|
29
|
+
* LENS ROWS — Keep rows that match the lens (tag stake or assignee descended from the seat).
|
|
30
|
+
*/
|
|
31
|
+
export declare function lensRows(rows: Row[], lens: Lens): Row[];
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* BUILD LENS — A director owns a stake, not just its own assignments.
|
|
3
|
+
* Walks the reporting tree downward to find every direct/indirect report.
|
|
4
|
+
*/
|
|
5
|
+
export function buildLens(roster, seat) {
|
|
6
|
+
const entry = roster.find((s) => s.id === seat);
|
|
7
|
+
if (!entry)
|
|
8
|
+
return { ok: false, error: `unknown seat '${seat}' — not in the roster (${roster.length} seats)` };
|
|
9
|
+
const rawTags = entry.subscribes.filter((t) => t !== '*');
|
|
10
|
+
const tags = [...new Set(rawTags)].sort();
|
|
11
|
+
const all = entry.subscribes.includes('*');
|
|
12
|
+
// Breadth-first descent from the seat; visited guards cycles.
|
|
13
|
+
const seats = [seat];
|
|
14
|
+
const visited = new Set([seat]);
|
|
15
|
+
const queue = [seat];
|
|
16
|
+
while (queue.length) {
|
|
17
|
+
const current = queue.shift();
|
|
18
|
+
for (const s of roster) {
|
|
19
|
+
if (s.reportsTo === current && !visited.has(s.id)) {
|
|
20
|
+
visited.add(s.id);
|
|
21
|
+
seats.push(s.id);
|
|
22
|
+
queue.push(s.id);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
seats.sort();
|
|
27
|
+
// Reject an empty lens: no tags, not all, no reports but itself.
|
|
28
|
+
if (tags.length === 0 && !all && seats.length === 1)
|
|
29
|
+
return { ok: false, error: `seat '${seat}' stakes no tags and has no reports — its lens would be empty` };
|
|
30
|
+
return { ok: true, lens: { seat, tags, seats, all } };
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* LENS ROWS — Keep rows that match the lens (tag stake or assignee descended from the seat).
|
|
34
|
+
*/
|
|
35
|
+
export function lensRows(rows, lens) {
|
|
36
|
+
if (lens.all)
|
|
37
|
+
return rows;
|
|
38
|
+
const tagSet = new Set(lens.tags);
|
|
39
|
+
const seatSet = new Set(lens.seats);
|
|
40
|
+
return rows.filter((row) => {
|
|
41
|
+
if ((row.tags ?? []).some((t) => tagSet.has(t)))
|
|
42
|
+
return true;
|
|
43
|
+
if (row.assignee && seatSet.has(row.assignee))
|
|
44
|
+
return true;
|
|
45
|
+
return false;
|
|
46
|
+
});
|
|
47
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Asker } from '@oneie/sdk/tasks-bulk';
|
|
2
|
+
import { type Row } from './tasks-digest.js';
|
|
3
|
+
import { type Lens } from './tasks-lens.js';
|
|
4
|
+
export declare function nextCandidates(rows: Row[], lens: Lens): {
|
|
5
|
+
candidates: Row[];
|
|
6
|
+
refusal?: string;
|
|
7
|
+
};
|
|
8
|
+
export type ClaimNextResult = {
|
|
9
|
+
ok: true;
|
|
10
|
+
row: Row;
|
|
11
|
+
claimed: boolean;
|
|
12
|
+
claimant?: string;
|
|
13
|
+
reassigned?: boolean;
|
|
14
|
+
} | {
|
|
15
|
+
ok: false;
|
|
16
|
+
error: string;
|
|
17
|
+
tried: {
|
|
18
|
+
tid: string;
|
|
19
|
+
reason: string;
|
|
20
|
+
}[];
|
|
21
|
+
};
|
|
22
|
+
export declare function claimNext(ask: Asker, candidates: Row[], seat: string, opts?: {
|
|
23
|
+
peek?: boolean;
|
|
24
|
+
tries?: number;
|
|
25
|
+
}): Promise<ClaimNextResult>;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { CLOSED, kindOf } from './tasks-digest.js';
|
|
2
|
+
import { lensRows } from './tasks-lens.js';
|
|
3
|
+
export function nextCandidates(rows, lens) {
|
|
4
|
+
const inLens = lensRows(rows, lens).filter(r => kindOf(r) === 'work');
|
|
5
|
+
const candidates = inLens
|
|
6
|
+
.filter(r => r.status === 'open' && r.ready === true && (!r.assignee || r.assignee === lens.seat))
|
|
7
|
+
.sort((a, b) => b.priority - a.priority || a.tid.localeCompare(b.tid));
|
|
8
|
+
if (candidates.length > 0) {
|
|
9
|
+
return { candidates };
|
|
10
|
+
}
|
|
11
|
+
// No ready candidates – build a refusal message.
|
|
12
|
+
const waiting = inLens
|
|
13
|
+
.filter(r => (r.status === 'open' || r.status === 'blocked') && r.ready !== true)
|
|
14
|
+
.sort((a, b) => b.priority - a.priority);
|
|
15
|
+
if (waiting.length > 0) {
|
|
16
|
+
const w = waiting[0];
|
|
17
|
+
const byId = new Map(rows.map(r => [r.tid, r]));
|
|
18
|
+
const open = (w.blockedBy ?? [])
|
|
19
|
+
.map(tid => byId.get(tid))
|
|
20
|
+
.filter((b) => b !== undefined && !CLOSED.has(b.status));
|
|
21
|
+
if (open.length > 0) {
|
|
22
|
+
return {
|
|
23
|
+
candidates: [],
|
|
24
|
+
refusal: `nothing ready in ${lens.seat}'s lens — highest waiting ${w.tid} "${w.name}" is blocked by ${open.map(b => `${b.tid} "${b.name}" (${b.status})`).join(', ')}`
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
return {
|
|
29
|
+
candidates: [],
|
|
30
|
+
refusal: `nothing ready in ${lens.seat}'s lens — highest waiting ${w.tid} "${w.name}" has ${w.openBlockers ?? 0} open blocker(s) outside this read`
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
// No waiting rows – check if other seats hold ready rows in the lens.
|
|
35
|
+
const heldByOther = inLens.filter(r => r.status === 'open' && r.ready === true && r.assignee && r.assignee !== lens.seat);
|
|
36
|
+
if (heldByOther.length > 0) {
|
|
37
|
+
return {
|
|
38
|
+
candidates: [],
|
|
39
|
+
refusal: `nothing ready in ${lens.seat}'s lens — ${heldByOther.length} ready row(s) are held by other seats`
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
// Nothing open at all.
|
|
43
|
+
return {
|
|
44
|
+
candidates: [],
|
|
45
|
+
refusal: `nothing open in ${lens.seat}'s lens (${inLens.length} rows read, none open and ready)`
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
export async function claimNext(ask, candidates, seat, opts = {}) {
|
|
49
|
+
if (candidates.length === 0) {
|
|
50
|
+
return { ok: false, error: 'no candidates', tried: [] };
|
|
51
|
+
}
|
|
52
|
+
// --- peek mode: just return the first candidate without calling ask
|
|
53
|
+
if (opts.peek) {
|
|
54
|
+
return { ok: true, row: candidates[0], claimed: false };
|
|
55
|
+
}
|
|
56
|
+
const tries = opts.tries ?? 5;
|
|
57
|
+
const tried = [];
|
|
58
|
+
for (let i = 0; i < Math.min(candidates.length, tries); i++) {
|
|
59
|
+
const c = candidates[i];
|
|
60
|
+
// --- step 1: claim
|
|
61
|
+
let claimRes;
|
|
62
|
+
try {
|
|
63
|
+
claimRes = (await ask('tasks:claim', { tid: c.tid, workspace: c.workspace }));
|
|
64
|
+
}
|
|
65
|
+
catch (e) {
|
|
66
|
+
tried.push({ tid: c.tid, reason: e instanceof Error ? e.message : String(e) });
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
const r = claimRes;
|
|
70
|
+
if (r.ok !== true) {
|
|
71
|
+
tried.push({ tid: c.tid, reason: r.error ?? 'refused' });
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
// --- successfully claimed by someone
|
|
75
|
+
if (r.claimant === seat) {
|
|
76
|
+
return { ok: true, row: c, claimed: true, claimant: seat };
|
|
77
|
+
}
|
|
78
|
+
// --- claimed by shared key, need to reassign to `seat`
|
|
79
|
+
let reassignRes;
|
|
80
|
+
try {
|
|
81
|
+
reassignRes = (await ask('tasks:reassign', { tid: c.tid, assignee: seat, workspace: c.workspace }));
|
|
82
|
+
}
|
|
83
|
+
catch (e) {
|
|
84
|
+
return stranded(c.tid, r.claimant, seat, e instanceof Error ? e.message : String(e), tried);
|
|
85
|
+
}
|
|
86
|
+
const a = reassignRes;
|
|
87
|
+
if (a.ok === true && a.unchanged !== true && a.previous !== seat) {
|
|
88
|
+
return { ok: true, row: c, claimed: true, claimant: r.claimant, reassigned: true };
|
|
89
|
+
}
|
|
90
|
+
// A refused reassign leaves the row picked under the shared claimant: stop here
|
|
91
|
+
// rather than strand more rows the same way.
|
|
92
|
+
if (a.ok !== true)
|
|
93
|
+
return stranded(c.tid, r.claimant, seat, a.error ?? 'reassign refused', tried);
|
|
94
|
+
tried.push({ tid: c.tid, reason: `taken by another ${seat} between claim and reassign` });
|
|
95
|
+
}
|
|
96
|
+
// --- exhausted tries
|
|
97
|
+
const error = `no row claimed after ${tried.length} tries: ` + tried.map(t => `${t.tid} ${t.reason}`).join('; ');
|
|
98
|
+
return { ok: false, error, tried };
|
|
99
|
+
}
|
|
100
|
+
function stranded(tid, claimant, seat, reason, tried) {
|
|
101
|
+
tried.push({ tid, reason: `reassign failed: ${reason}` });
|
|
102
|
+
return { ok: false, error: `claimed ${tid} as @${claimant ?? '?'} but could not hand it to @${seat} (${reason}) — row stranded picked; reassign it by hand`, tried };
|
|
103
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type Row } from './tasks-digest.js';
|
|
2
|
+
import type { Seat } from './tasks-lens.js';
|
|
3
|
+
export interface Proposal {
|
|
4
|
+
tid: string;
|
|
5
|
+
assignee: string;
|
|
6
|
+
matched: string[];
|
|
7
|
+
workspace: string;
|
|
8
|
+
why: string;
|
|
9
|
+
}
|
|
10
|
+
export interface Unrouted {
|
|
11
|
+
tid: string;
|
|
12
|
+
tags: string[];
|
|
13
|
+
reason: 'no-stake' | 'tie';
|
|
14
|
+
candidates?: string[];
|
|
15
|
+
}
|
|
16
|
+
export interface RoutePlan {
|
|
17
|
+
unassigned: number;
|
|
18
|
+
proposals: Proposal[];
|
|
19
|
+
unrouted: Unrouted[];
|
|
20
|
+
unroutedByTag: Record<string, number>;
|
|
21
|
+
}
|
|
22
|
+
export declare function proposeOwners(rows: Row[], seats: Seat[]): RoutePlan;
|
|
23
|
+
export interface RouteEdit {
|
|
24
|
+
tid: string;
|
|
25
|
+
assignee: string;
|
|
26
|
+
workspace: string;
|
|
27
|
+
comment: string;
|
|
28
|
+
}
|
|
29
|
+
export declare function routeEdits(plan: RoutePlan): RouteEdit[];
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { CLOSED } from './tasks-digest.js';
|
|
2
|
+
/** The one tied seat every other tied seat reports up to (walking reportsTo), if any. */
|
|
3
|
+
function managerOf(tied, seats) {
|
|
4
|
+
const byId = new Map(seats.map((s) => [s.id, s]));
|
|
5
|
+
const reachesUp = (from, to) => {
|
|
6
|
+
const seen = new Set();
|
|
7
|
+
let cur = from.reportsTo;
|
|
8
|
+
while (cur && !seen.has(cur)) {
|
|
9
|
+
if (cur === to)
|
|
10
|
+
return true;
|
|
11
|
+
seen.add(cur);
|
|
12
|
+
cur = byId.get(cur)?.reportsTo ?? null;
|
|
13
|
+
}
|
|
14
|
+
return false;
|
|
15
|
+
};
|
|
16
|
+
return tied.find((m) => tied.every((o) => o.id === m.id || reachesUp(o, m.id)));
|
|
17
|
+
}
|
|
18
|
+
// WHY: never guesses — zero overlap stays unassigned, and N + unrouted == unassigned.
|
|
19
|
+
export function proposeOwners(rows, seats) {
|
|
20
|
+
const eligibleSeats = seats.filter((s) => s.tier !== 'chairman' && s.tier !== 'ceo');
|
|
21
|
+
const stakeBySeat = new Map();
|
|
22
|
+
for (const s of eligibleSeats) {
|
|
23
|
+
stakeBySeat.set(s.id, s.subscribes.filter((x) => x !== '*'));
|
|
24
|
+
}
|
|
25
|
+
const inScope = rows.filter((r) => {
|
|
26
|
+
if (CLOSED.has(r.status))
|
|
27
|
+
return false;
|
|
28
|
+
if (r.assignee && r.assignee !== '')
|
|
29
|
+
return false;
|
|
30
|
+
const tags = r.tags ?? [];
|
|
31
|
+
if (tags.includes('approval'))
|
|
32
|
+
return false;
|
|
33
|
+
return true;
|
|
34
|
+
});
|
|
35
|
+
const unassigned = inScope.length;
|
|
36
|
+
const proposals = [];
|
|
37
|
+
const unrouted = [];
|
|
38
|
+
const unroutedByTag = {};
|
|
39
|
+
const incTag = (tag) => {
|
|
40
|
+
unroutedByTag[tag] = (unroutedByTag[tag] ?? 0) + 1;
|
|
41
|
+
};
|
|
42
|
+
for (const r of inScope) {
|
|
43
|
+
const tags = r.tags ?? [];
|
|
44
|
+
const candidates = [];
|
|
45
|
+
for (const s of eligibleSeats) {
|
|
46
|
+
const stake = stakeBySeat.get(s.id);
|
|
47
|
+
const matched = tags.filter((t) => stake.includes(t));
|
|
48
|
+
if (matched.length > 0) {
|
|
49
|
+
const dedup = [...new Set(matched)].sort();
|
|
50
|
+
candidates.push({ seat: s, matched: dedup });
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (candidates.length === 0) {
|
|
54
|
+
unrouted.push({ tid: r.tid, tags, reason: 'no-stake' });
|
|
55
|
+
if (tags.length === 0) {
|
|
56
|
+
incTag('(untagged)');
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
for (const t of tags)
|
|
60
|
+
incTag(t);
|
|
61
|
+
}
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
let remaining = candidates;
|
|
65
|
+
// rule 5: if row has 'goal' or 'story' and at least one director candidate
|
|
66
|
+
if ((tags.includes('goal') || tags.includes('story')) &&
|
|
67
|
+
remaining.some((c) => c.seat.tier === 'director')) {
|
|
68
|
+
remaining = remaining.filter((c) => c.seat.tier === 'director');
|
|
69
|
+
}
|
|
70
|
+
// rule 6: keep only highest matched.length; then if tie among directors, keep directors
|
|
71
|
+
{
|
|
72
|
+
const maxLen = Math.max(...remaining.map((c) => c.matched.length));
|
|
73
|
+
remaining = remaining.filter((c) => c.matched.length === maxLen);
|
|
74
|
+
}
|
|
75
|
+
if (remaining.length > 1 && remaining.some((c) => c.seat.tier === 'director')) {
|
|
76
|
+
remaining = remaining.filter((c) => c.seat.tier === 'director');
|
|
77
|
+
}
|
|
78
|
+
if (remaining.length === 1) {
|
|
79
|
+
const { seat, matched } = remaining[0];
|
|
80
|
+
proposals.push({
|
|
81
|
+
tid: r.tid,
|
|
82
|
+
assignee: seat.id,
|
|
83
|
+
matched,
|
|
84
|
+
workspace: r.workspace,
|
|
85
|
+
why: `overlap ${matched.length}`,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
else if (remaining.length > 1 && managerOf(remaining.map((c) => c.seat), seats)) {
|
|
89
|
+
// A tied seat that manages every other tied seat takes it — it can hand it down.
|
|
90
|
+
const mgr = managerOf(remaining.map((c) => c.seat), seats);
|
|
91
|
+
proposals.push({
|
|
92
|
+
tid: r.tid,
|
|
93
|
+
assignee: mgr.id,
|
|
94
|
+
matched: [...new Set(remaining.flatMap((c) => c.matched))].sort(),
|
|
95
|
+
workspace: r.workspace,
|
|
96
|
+
why: `tie; @${mgr.id} manages the other ${remaining.length - 1}`,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
else if (remaining.length > 1) {
|
|
100
|
+
// rule 8: check if they all report to same eligible seat
|
|
101
|
+
const reportsTo = remaining[0].seat.reportsTo;
|
|
102
|
+
const allSame = reportsTo !== null && remaining.every((c) => c.seat.reportsTo === reportsTo);
|
|
103
|
+
let managerEligible = false;
|
|
104
|
+
if (allSame) {
|
|
105
|
+
const mgr = seats.find((s) => s.id === reportsTo);
|
|
106
|
+
managerEligible =
|
|
107
|
+
mgr !== undefined &&
|
|
108
|
+
mgr.tier !== 'chairman' &&
|
|
109
|
+
mgr.tier !== 'ceo';
|
|
110
|
+
}
|
|
111
|
+
if (allSame && managerEligible) {
|
|
112
|
+
// union of matched
|
|
113
|
+
const union = [
|
|
114
|
+
...new Set(remaining.flatMap((c) => c.matched)),
|
|
115
|
+
].sort();
|
|
116
|
+
proposals.push({
|
|
117
|
+
tid: r.tid,
|
|
118
|
+
assignee: reportsTo,
|
|
119
|
+
matched: union,
|
|
120
|
+
workspace: r.workspace,
|
|
121
|
+
why: `tie among ${remaining.length} reports of ${reportsTo}`,
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
// rule 9: tie unrouted
|
|
126
|
+
const candidateIds = remaining.map((c) => c.seat.id).sort();
|
|
127
|
+
unrouted.push({
|
|
128
|
+
tid: r.tid,
|
|
129
|
+
tags,
|
|
130
|
+
reason: 'tie',
|
|
131
|
+
candidates: candidateIds,
|
|
132
|
+
});
|
|
133
|
+
// do NOT touch unroutedByTag
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return {
|
|
138
|
+
unassigned,
|
|
139
|
+
proposals,
|
|
140
|
+
unrouted,
|
|
141
|
+
unroutedByTag,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
export function routeEdits(plan) {
|
|
145
|
+
return plan.proposals.map((p) => ({
|
|
146
|
+
tid: p.tid,
|
|
147
|
+
assignee: p.assignee,
|
|
148
|
+
workspace: p.workspace,
|
|
149
|
+
comment: `routed by stake (one tasks route): tags [${p.matched.join(', ')}] match @${p.assignee}'s subscribes — ${p.why}`,
|
|
150
|
+
}));
|
|
151
|
+
}
|