ShtukbaseStandard
package file→ .shtukbase/packages/tasks/checks/w08_init_closure.ts

checks/w08_init_closure.ts

50 lines, 2.4 KB.

  1. // W8. Closing an initiative: "done" — all its tasks (by the field initiative in /work/tasks) are in done or dropped
  2. // (postponed count as dropped, as in w02). The reverse is a hint, not a hole: all tasks are done and the initiative is
  3. // not "done" yet → the hint "can be closed".
  4. import { type Project, type Rec, type Result, addr } from '../../collections/checks/lib/speclib.ts';
  5. import { main } from '../../collections/checks/lib/runner.ts';
  6. import { cat, fld, status } from './lib/worklib.ts';
  7. export const RULE = {
  8. id: 'w08', title: 'Closing an initiative: its tasks are closed',
  9. scope: 'initiatives with the status "done" and initiatives whose tasks are all closed',
  10. };
  11. const CLOSED = ['done', 'dropped'];
  12. export function check(p: Project, res: Result): void {
  13. const reason = p.need('INIT', 'TASK');
  14. if (reason) {
  15. res.part('scope', reason);
  16. return;
  17. }
  18. const fTi = fld(p, 'TASK', 'INIT');
  19. const byInit = new Map<string, Rec[]>();
  20. for (const t of p.records('TASK'))
  21. for (const i of p.links(t, fTi, 'INIT')) {
  22. if (!byInit.has(i)) byInit.set(i, []);
  23. byInit.get(i)!.push(t);
  24. }
  25. const inits = p.records('INIT').filter((r) => cat(p, r, 'INIT') !== 'dropped');
  26. res.scope = inits.filter((r) => cat(p, r, 'INIT') === 'done' || (byInit.get(r.id) ?? []).length).map((r) => addr(r.id));
  27. res.scopeText = `initiatives ${p.coll('INIT')}: "done" — tasks are closed; all tasks closed — can be closed (a hint)`;
  28. const hints: string[] = [];
  29. if (res.part('done → tasks closed', null)) {
  30. for (const r of inits) {
  31. const ts = byInit.get(r.id) ?? [];
  32. const openT = ts.filter((t) => !CLOSED.includes(cat(p, t, 'TASK') ?? '\0')).map((t) => p.label(t, 'TASK'));
  33. if (cat(p, r, 'INIT') === 'done') {
  34. if (openT.length)
  35. res.hole(addr(r.id), `${p.label(r, 'INIT')} is done, but tasks are open: ${openT.join(', ')}`,
  36. 'close the tasks with evidence or return the initiative to under-way', 'done-open-tasks');
  37. } else if (ts.length && !openT.length && ts.some((t) => cat(p, t, 'TASK') === 'done')) {
  38. hints.push(`${p.label(r, 'INIT')} (${status(r)})`);
  39. }
  40. }
  41. }
  42. if (hints.length)
  43. (res.info.lines ??= []).push(`all tasks are closed, but the initiative is not — it can be closed (${hints.length}): ${hints.join(', ')}`);
  44. res.info.canClose = hints;
  45. }
  46. if (import.meta.main) main({ RULE, check });