ShtukbaseStandard
package file→ .shtukbase/packages/taskstart/hooks/brief.ts

hooks/brief.ts

58 lines, 3.0 KB.

  1. // TS-R01 (SessionStart). The agent's first context in the project: what the project is, which packages stand, the
  2. // norms that do not let through (MUST, mode enforce), the work in progress and the questions still open for the owner.
  3. // Its stdout is added to the agent's context by Claude Code. Short on purpose: names and paths, not page bodies.
  4. import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
  5. import { join } from 'node:path';
  6. import { parseYaml, pystr, splitFrontMatter } from '../../collections/checks/lib/pages.ts';
  7. import { projectRoot, readPayload } from '../../hooks/lib/hooklib.ts';
  8. const MAX = 25;
  9. const { p } = readPayload();
  10. if (!p) process.exit(0);
  11. const root = projectRoot(p);
  12. const head = (file: string) => parseYaml(splitFrontMatter(readFileSync(file, 'utf8'))[0]);
  13. /** Records of a folder (recursively): [address, front matter]. */
  14. function records(dir: string): [string, Record<string, any>][] {
  15. const out: [string, Record<string, any>][] = [];
  16. const walk = (rel: string) => {
  17. const full = join(root, rel);
  18. if (!existsSync(full)) return;
  19. for (const n of readdirSync(full).sort()) {
  20. if (n.startsWith('.')) continue;
  21. const r = `${rel}/${n}`;
  22. if (statSync(join(root, r)).isDirectory()) walk(r);
  23. else if (n.endsWith('.md')) out.push([`/${r}`, head(join(root, r))]);
  24. }
  25. };
  26. walk(dir);
  27. return out;
  28. }
  29. const st = (h: Record<string, any>) => pystr(h.status ?? '').trim().toLowerCase();
  30. const cap = (xs: string[]) => (xs.length > MAX ? [...xs.slice(0, MAX), `… and ${xs.length - MAX} more`] : xs);
  31. const lines: string[] = [];
  32. let lock: Record<string, any> = {};
  33. try {
  34. lock = (Bun.YAML.parse(readFileSync(join(root, '.shtukbase', 'packages.lock'), 'utf8')) as Record<string, any>) ?? {};
  35. } catch {}
  36. const pkgs = Object.entries(lock.packages ?? {}).map(([id, v]: [string, any]) => `${id} ${v?.version ?? ''}`.trim());
  37. lines.push(`Project "${lock.name ?? lock.project ?? root}" (Shtukbase). Packages: ${pkgs.join(', ') || 'none'}.`);
  38. lines.push('Norms are the pages in /norms/rules; the check of all norms is shtuk check. Write collection records with a head and a title.');
  39. const must = records('norms/rules').filter(([, h]) => st(h) === 'active' && pystr(h.level) === 'MUST' && pystr(h.mode) === 'enforce');
  40. if (must.length) {
  41. lines.push('', `Enforcing norms (MUST), ${must.length}:`);
  42. lines.push(...cap(must.map(([a, h]) => `- ${pystr(h.ruleId ?? a)}: ${pystr(h.requirement ?? h.title ?? '')}`)));
  43. }
  44. const doing = records('work/tasks').filter(([, h]) => st(h) === 'doing');
  45. if (doing.length) {
  46. lines.push('', `Tasks in progress, ${doing.length}:`);
  47. lines.push(...cap(doing.map(([a, h]) => `- ${a}: ${pystr(h.title ?? '')}`)));
  48. }
  49. const open = records('knowledge/questions').filter(([, h]) => st(h) === 'open');
  50. if (open.length) {
  51. lines.push('', `Open questions to the owner, ${open.length} (do not decide for the owner, the answer goes into the question record):`);
  52. lines.push(...cap(open.map(([a, h]) => `- ${a}: ${pystr(h.title ?? '')}`)));
  53. }
  54. console.log(lines.join('\n'));