ShtukbaseStandard
archivo del paquete→ .shtukbase/packages/norms-core/checks/context.ts

checks/context.ts

29 líneas, 1.5 KB.

El archivo tal como está en el paquete: el código y sus comentarios no se traducen.

  1. // Prints the project's rules in force (status: active) as a Markdown list for an agent's context: number, level,
  2. // and the rail (the short line for an agent) or else the requirement. Not a check: always exits 0.
  3. import { records, status } from './pagelib.ts';
  4. import { pystr, truthy } from '../../collections/checks/lib/pages.ts';
  5. const ORDER: Record<string, number> = { MUST: 0, SHOULD: 1, MAY: 2 };
  6. export const RULE = { id: 'ns-context', title: 'Active rules as a list for the agent context' };
  7. const text = (v: unknown): string => (truthy(v) ? pystr(v) : '');
  8. /** The lines to print: a heading and one list item per active rule, MUST first, then by number. */
  9. export function check(): string[] {
  10. const rows: [number, string, string, string][] = [];
  11. for (const [addr, head] of records('norms/rules')) {
  12. if (status(head) !== 'active') continue;
  13. const level = text(head.level).trim();
  14. const body = (truthy(head.rail) ? text(head.rail) : truthy(head.requirement) ? text(head.requirement) : text(head.title)).trim();
  15. rows.push([Object.hasOwn(ORDER, level) ? ORDER[level] : 3, truthy(head.ruleId) ? text(head.ruleId) : addr, level, body]);
  16. }
  17. const cmp = (a: string, b: string) => (a < b ? -1 : a > b ? 1 : 0);
  18. rows.sort((a, b) => a[0] - b[0] || cmp(a[1], b[1]) || cmp(a[2], b[2]) || cmp(a[3], b[3]));
  19. return ['# Active rules of the project', ...rows.map(([, rid, level, body]) => `- ${rid} (${level || '?'}): ${body}`)];
  20. }
  21. if (import.meta.main) {
  22. console.log(check().join('\n'));
  23. process.exitCode = 0;
  24. }