@oneie/cli 5.2.1 → 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/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 +1 -1
- 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/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 +301 -38
- package/package.json +2 -2
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type Row } from './tasks-digest.js';
|
|
2
|
+
export interface ScoreSnapshot {
|
|
3
|
+
seq: number;
|
|
4
|
+
asOf?: string;
|
|
5
|
+
unassigned: number;
|
|
6
|
+
orphans: number;
|
|
7
|
+
staleReady: number;
|
|
8
|
+
widestTag: {
|
|
9
|
+
tag: string;
|
|
10
|
+
count: number;
|
|
11
|
+
share: number;
|
|
12
|
+
};
|
|
13
|
+
closedBySeat: Record<string, number>;
|
|
14
|
+
readyStreak: Record<string, number>;
|
|
15
|
+
openOwner: Record<string, string>;
|
|
16
|
+
}
|
|
17
|
+
export type Metric = 'unassigned' | 'orphans' | 'staleReady' | 'widestShare' | 'closed';
|
|
18
|
+
export interface Delta {
|
|
19
|
+
metric: Metric;
|
|
20
|
+
now: number;
|
|
21
|
+
prev?: number;
|
|
22
|
+
delta?: number;
|
|
23
|
+
red: boolean;
|
|
24
|
+
}
|
|
25
|
+
export interface Score {
|
|
26
|
+
snap: ScoreSnapshot;
|
|
27
|
+
deltas: Delta[];
|
|
28
|
+
}
|
|
29
|
+
export declare function scoreboard(rows: Row[], prev: ScoreSnapshot | undefined, opts?: {
|
|
30
|
+
staleAfter?: number;
|
|
31
|
+
asOf?: string;
|
|
32
|
+
}): Score;
|
|
33
|
+
export declare function renderScore(s: Score, opts: {
|
|
34
|
+
color: boolean;
|
|
35
|
+
staleAfter?: number;
|
|
36
|
+
}): string;
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// tasks-score — the orchestration scoreboard `brief --as ceo` prints and remembers.
|
|
2
|
+
// Locked rule 3 (deterministic results): the chairman grades from these five numbers,
|
|
3
|
+
// not from prose. Time is structural: 'older than N' means N consecutive briefs.
|
|
4
|
+
import { CLOSED, kindOf } from './tasks-digest.js';
|
|
5
|
+
export function scoreboard(rows, prev, opts = {}) {
|
|
6
|
+
const staleAfter = opts.staleAfter ?? 3;
|
|
7
|
+
const work = rows.filter(r => kindOf(r) === 'work');
|
|
8
|
+
const open = work.filter(r => !CLOSED.has(r.status));
|
|
9
|
+
const unassigned = open.filter(r => !r.assignee).length;
|
|
10
|
+
const orphans = open.filter(r => !r.parent).length;
|
|
11
|
+
// readyStreak
|
|
12
|
+
const readyStreak = {};
|
|
13
|
+
for (const r of open) {
|
|
14
|
+
if (r.status === 'open' && r.ready === true && !r.assignee) {
|
|
15
|
+
const prevStreak = prev?.readyStreak?.[r.tid] ?? 0;
|
|
16
|
+
readyStreak[r.tid] = prevStreak + 1;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
const staleReady = Object.values(readyStreak).filter(v => v >= staleAfter).length;
|
|
20
|
+
// widestTag
|
|
21
|
+
let widestTag = { tag: '', count: 0, share: 0 };
|
|
22
|
+
if (open.length > 0) {
|
|
23
|
+
const tagCounts = {};
|
|
24
|
+
for (const r of open) {
|
|
25
|
+
for (const tag of r.tags ?? []) {
|
|
26
|
+
tagCounts[tag] = (tagCounts[tag] || 0) + 1;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
let bestTag = '';
|
|
30
|
+
let bestCount = 0;
|
|
31
|
+
for (const [tag, count] of Object.entries(tagCounts)) {
|
|
32
|
+
if (count > bestCount || (count === bestCount && tag < bestTag)) {
|
|
33
|
+
bestTag = tag;
|
|
34
|
+
bestCount = count;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (bestTag !== '') {
|
|
38
|
+
widestTag = { tag: bestTag, count: bestCount, share: bestCount / open.length };
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
// closedBySeat — based on prev openOwner
|
|
42
|
+
const closedBySeat = {};
|
|
43
|
+
if (prev?.openOwner) {
|
|
44
|
+
const byTid = new Map(work.map((r) => [r.tid, r]));
|
|
45
|
+
for (const [tid, owner] of Object.entries(prev.openOwner)) {
|
|
46
|
+
const row = byTid.get(tid);
|
|
47
|
+
if (row && CLOSED.has(row.status)) {
|
|
48
|
+
const seat = owner || '(unassigned)';
|
|
49
|
+
closedBySeat[seat] = (closedBySeat[seat] || 0) + 1;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
// openOwner for next time
|
|
54
|
+
const openOwner = {};
|
|
55
|
+
for (const r of open) {
|
|
56
|
+
openOwner[r.tid] = r.assignee ?? '';
|
|
57
|
+
}
|
|
58
|
+
const seq = (prev?.seq ?? 0) + 1;
|
|
59
|
+
const snap = {
|
|
60
|
+
seq,
|
|
61
|
+
asOf: opts.asOf,
|
|
62
|
+
unassigned,
|
|
63
|
+
orphans,
|
|
64
|
+
staleReady,
|
|
65
|
+
widestTag,
|
|
66
|
+
closedBySeat,
|
|
67
|
+
readyStreak,
|
|
68
|
+
openOwner,
|
|
69
|
+
};
|
|
70
|
+
// delta helpers
|
|
71
|
+
const closedNow = Object.values(closedBySeat).reduce((a, b) => a + b, 0);
|
|
72
|
+
const prevClosed = prev ? Object.values(prev.closedBySeat).reduce((a, b) => a + b, 0) : 0;
|
|
73
|
+
const deltas = [];
|
|
74
|
+
const metrics = [
|
|
75
|
+
{ metric: 'unassigned', now: unassigned, prevNow: prev?.unassigned },
|
|
76
|
+
{ metric: 'orphans', now: orphans, prevNow: prev?.orphans },
|
|
77
|
+
{ metric: 'staleReady', now: staleReady, prevNow: prev?.staleReady },
|
|
78
|
+
{ metric: 'widestShare', now: widestTag.share, prevNow: prev?.widestTag?.share },
|
|
79
|
+
{ metric: 'closed', now: closedNow, prevPrev: prevClosed },
|
|
80
|
+
];
|
|
81
|
+
for (const m of metrics) {
|
|
82
|
+
if (!prev) {
|
|
83
|
+
deltas.push({ metric: m.metric, now: m.now, red: false });
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
const p = m.metric === 'closed' ? m.prevPrev : m.prevNow;
|
|
87
|
+
const delta = m.now - p;
|
|
88
|
+
const red = m.metric === 'closed' ? delta < 0 : delta > 0;
|
|
89
|
+
deltas.push({ metric: m.metric, now: m.now, prev: p, delta, red });
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return { snap, deltas };
|
|
93
|
+
}
|
|
94
|
+
function RED(text, color) {
|
|
95
|
+
return color ? `\u001b[31m${text}\u001b[0m` : text;
|
|
96
|
+
}
|
|
97
|
+
function formatDelta(d, isShare) {
|
|
98
|
+
if (d === 0)
|
|
99
|
+
return '=';
|
|
100
|
+
const sign = d > 0 ? '+' : '';
|
|
101
|
+
if (isShare) {
|
|
102
|
+
// d is already a fraction (e.g. 0.015), convert to percentage points
|
|
103
|
+
const pt = (d * 100).toFixed(1);
|
|
104
|
+
return `${sign}${pt}pt`;
|
|
105
|
+
}
|
|
106
|
+
return `${sign}${d}`;
|
|
107
|
+
}
|
|
108
|
+
export function renderScore(s, opts) {
|
|
109
|
+
const { snap, deltas } = s;
|
|
110
|
+
const staleAfter = opts.staleAfter ?? 3;
|
|
111
|
+
const color = opts.color;
|
|
112
|
+
// prefix
|
|
113
|
+
const prefix = snap.seq === 1
|
|
114
|
+
? `scoreboard #${snap.seq} (first — deltas from the next brief)`
|
|
115
|
+
: `scoreboard #${snap.seq} vs #${snap.seq - 1}`;
|
|
116
|
+
// build parts
|
|
117
|
+
const parts = [];
|
|
118
|
+
for (const delta of deltas) {
|
|
119
|
+
const now = delta.now;
|
|
120
|
+
const isShare = delta.metric === 'widestShare';
|
|
121
|
+
const d = delta.prev === undefined ? '' : ` (${formatDelta(delta.delta, isShare)}${delta.red ? ' RED' : ''})`;
|
|
122
|
+
const finalPart = delta.red ? RED(`${now}${d}`, color) : `${now}${d}`;
|
|
123
|
+
switch (delta.metric) {
|
|
124
|
+
case 'unassigned':
|
|
125
|
+
parts.push(`unassigned ${finalPart}`);
|
|
126
|
+
break;
|
|
127
|
+
case 'orphans':
|
|
128
|
+
parts.push(`orphans ${finalPart}`);
|
|
129
|
+
break;
|
|
130
|
+
case 'staleReady':
|
|
131
|
+
parts.push(`ready-unclaimed ≥${staleAfter} briefs ${finalPart}`);
|
|
132
|
+
break;
|
|
133
|
+
case 'widestShare': {
|
|
134
|
+
const tag = snap.widestTag.tag || '-';
|
|
135
|
+
const share = (snap.widestTag.share * 100).toFixed(1);
|
|
136
|
+
parts.push(`widest tag ${tag} ${share}%${delta.red ? RED(d, color) : d}`);
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
case 'closed':
|
|
140
|
+
parts.push(`closed since last brief ${finalPart}`);
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
// closedBySeat suffix
|
|
145
|
+
const closedEntries = Object.entries(snap.closedBySeat);
|
|
146
|
+
if (closedEntries.length > 0) {
|
|
147
|
+
const sorted = closedEntries.sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]));
|
|
148
|
+
const suffix = sorted.map(([k, v]) => `${k}=${v}`).join(' ');
|
|
149
|
+
parts.push(`[${suffix}]`);
|
|
150
|
+
}
|
|
151
|
+
return prefix + ' · ' + parts.join(' · ');
|
|
152
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type Asker, type BulkEdit, type BulkReq } from '@oneie/sdk/tasks-bulk';
|
|
2
|
+
import type { Row } from './tasks-digest.js';
|
|
3
|
+
export declare function workspaceSpread(rows: Row[]): Record<string, number>;
|
|
4
|
+
export declare function spreadWarning(spread: Record<string, number>): string | undefined;
|
|
5
|
+
export declare function fillEditWorkspaces(edits: BulkEdit[], rows: Row[]): {
|
|
6
|
+
edits: BulkEdit[];
|
|
7
|
+
filled: number;
|
|
8
|
+
unknown: string[];
|
|
9
|
+
};
|
|
10
|
+
export declare function resolveBulkWorkspaces(ask: Asker, req: BulkReq): Promise<{
|
|
11
|
+
ok: true;
|
|
12
|
+
req: BulkReq;
|
|
13
|
+
filled: number;
|
|
14
|
+
unknown: string[];
|
|
15
|
+
} | {
|
|
16
|
+
ok: false;
|
|
17
|
+
error: string;
|
|
18
|
+
}>;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// tasks-workspace — A tid carries no workspace, so the board row is the only place a client can learn it; the receiver accepts a per-edit workspace override.
|
|
2
|
+
import { boardAllWith } from '@oneie/sdk/tasks-bulk';
|
|
3
|
+
export function workspaceSpread(rows) {
|
|
4
|
+
const counts = {};
|
|
5
|
+
for (const row of rows) {
|
|
6
|
+
counts[row.workspace] = (counts[row.workspace] ?? 0) + 1;
|
|
7
|
+
}
|
|
8
|
+
return counts;
|
|
9
|
+
}
|
|
10
|
+
export function spreadWarning(spread) {
|
|
11
|
+
const keys = Object.keys(spread);
|
|
12
|
+
if (keys.length < 2)
|
|
13
|
+
return undefined;
|
|
14
|
+
const n = keys.length;
|
|
15
|
+
const pairs = keys
|
|
16
|
+
.sort((a, b) => {
|
|
17
|
+
const diff = spread[b] - spread[a];
|
|
18
|
+
if (diff !== 0)
|
|
19
|
+
return diff;
|
|
20
|
+
return a.localeCompare(b);
|
|
21
|
+
})
|
|
22
|
+
.map((k) => `${k}=${spread[k]}`)
|
|
23
|
+
.join(' ');
|
|
24
|
+
return `warning this read spans ${n} workspaces (${pairs}) — an edit sent with one --workspace answers not_found for rows in the others; tasks bulk resolves each row's own`;
|
|
25
|
+
}
|
|
26
|
+
export function fillEditWorkspaces(edits, rows) {
|
|
27
|
+
const rowMap = new Map();
|
|
28
|
+
for (const row of rows) {
|
|
29
|
+
rowMap.set(row.tid, row);
|
|
30
|
+
}
|
|
31
|
+
const resultEdits = [];
|
|
32
|
+
let filled = 0;
|
|
33
|
+
const unknown = [];
|
|
34
|
+
for (const edit of edits) {
|
|
35
|
+
if (edit.workspace && edit.workspace.length > 0) {
|
|
36
|
+
resultEdits.push(edit);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
const row = rowMap.get(edit.tid);
|
|
40
|
+
if (row) {
|
|
41
|
+
resultEdits.push({ ...edit, workspace: row.workspace });
|
|
42
|
+
filled++;
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
resultEdits.push(edit);
|
|
46
|
+
unknown.push(edit.tid);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return { edits: resultEdits, filled, unknown };
|
|
51
|
+
}
|
|
52
|
+
export async function resolveBulkWorkspaces(ask, req) {
|
|
53
|
+
const missingWorkspace = req.edits?.some((e) => !e.workspace || e.workspace.length === 0);
|
|
54
|
+
if (!missingWorkspace) {
|
|
55
|
+
return { ok: true, req, filled: 0, unknown: [] };
|
|
56
|
+
}
|
|
57
|
+
const boardReq = {
|
|
58
|
+
...(req.workspace ? { workspace: req.workspace } : {}),
|
|
59
|
+
scope: 'tree',
|
|
60
|
+
status: 'all',
|
|
61
|
+
limit: 2000,
|
|
62
|
+
};
|
|
63
|
+
const res = await boardAllWith(ask, boardReq);
|
|
64
|
+
if (res.ok === false) {
|
|
65
|
+
return {
|
|
66
|
+
ok: false,
|
|
67
|
+
error: `could not read the board to resolve each edit's workspace: ${res.error ?? 'tasks:board failed'} — nothing sent`,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
const tasks = res.tasks ?? [];
|
|
71
|
+
const f = fillEditWorkspaces(req.edits, tasks);
|
|
72
|
+
return { ok: true, req: { ...req, edits: f.edits }, filled: f.filled, unknown: f.unknown };
|
|
73
|
+
}
|
package/dist/src/substrate.js
CHANGED
|
@@ -125,7 +125,8 @@ export function substrateCommands(program) {
|
|
|
125
125
|
.option('--exploration <n>', 'Exploration factor 0–1')
|
|
126
126
|
.option('--api <url>', 'API base URL', DEFAULT_API)
|
|
127
127
|
.action(async (type, opts) => {
|
|
128
|
-
|
|
128
|
+
// The route reads `tag` (api/select.ts:11); sending `type` was silently ignored.
|
|
129
|
+
const q = new URLSearchParams({ tag: type });
|
|
129
130
|
if (opts.exploration)
|
|
130
131
|
q.set('exploration', opts.exploration);
|
|
131
132
|
out(await api(`${base(opts)}/api/select?${q}`));
|