farna-bootstrapper 0.1.1 → 0.1.3

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.
@@ -92,6 +92,48 @@ export function formatCheck(result) {
92
92
  lines.push('', 'check: drift');
93
93
  return `${lines.join('\n')}\n`;
94
94
  }
95
+ /**
96
+ * The validation report. Every check listed here ran to completion: `doctor`
97
+ * fails at the first error and never prints a partial success, so a check name
98
+ * in this section is evidence the check did not fail.
99
+ */
100
+ export function formatDoctor(result) {
101
+ const lines = ['farna-bootstrapper doctor'];
102
+ lines.push(` source ${escapeControlCharacters(result.configPath)}`);
103
+ lines.push(` target ${escapeControlCharacters(result.target)} (${modeLabel(result.mode, result.activePersona)})`);
104
+ lines.push(` personas ${result.counts.personas} in source, ${result.counts.live} live`);
105
+ const notLive = notLiveLine(result.mode, result.notLive);
106
+ if (notLive !== undefined) {
107
+ lines.push(notLive);
108
+ }
109
+ lines.push(` checks ${result.checks.length} ok`);
110
+ lines.push(` warnings ${result.warnings.length}`);
111
+ lines.push('', 'checks');
112
+ for (const check of result.checks) {
113
+ lines.push(` ok ${check.name.padEnd(36)}${escapeControlCharacters(check.detail)}`);
114
+ }
115
+ lines.push('', 'personas');
116
+ for (const persona of result.personas) {
117
+ lines.push(` ${escapeControlCharacters(persona.id)}`);
118
+ lines.push(` chain ${persona.chain.map(escapeControlCharacters).join(' -> ')}`);
119
+ lines.push(` mode ${persona.mode ?? 'subagent (default)'}`);
120
+ lines.push(` model ${persona.model === null ? '<none>' : escapeControlCharacters(persona.model)}`);
121
+ for (const resource of persona.resources) {
122
+ const from = resource.fromPersona === persona.id
123
+ ? ''
124
+ : ` (from ${escapeControlCharacters(resource.fromPersona)}, rank ${resource.rank})`;
125
+ lines.push(` resource ${`${resource.kind}/${resource.id}`.padEnd(28)}${escapeControlCharacters(resource.path)}${from}`);
126
+ }
127
+ }
128
+ if (result.warnings.length > 0) {
129
+ lines.push('', 'warnings');
130
+ for (const entry of result.warnings) {
131
+ lines.push(` ${entry.code} ${entry.severity.padEnd(6)} ${escapeControlCharacters(entry.persona)}: ${escapeControlCharacters(entry.message)}`);
132
+ }
133
+ }
134
+ lines.push('', 'doctor: ok');
135
+ return `${lines.join('\n')}\n`;
136
+ }
95
137
  export function formatJson(value) {
96
138
  return `${JSON.stringify(value, null, 2)}\n`;
97
139
  }
@@ -521,7 +521,7 @@ export function looksSecretShaped(value) {
521
521
  /[A-Za-z]/.test(value));
522
522
  }
523
523
  /**
524
- * A `{ fromEnv }` reference inside `args` cannot be expressed: rulesync 43.11.0
524
+ * A `{ fromEnv }` reference inside `args` cannot be expressed: rulesync 43.13.0
525
525
  * accepts only string arguments and copies them into the emitted command
526
526
  * verbatim, so a reference would reach the server as the literal text `${NAME}`
527
527
  * and silently not expand. Refusing is the honest outcome; `env` and `headers`
@@ -206,7 +206,7 @@ function permissionRecords(value) {
206
206
  return records;
207
207
  }
208
208
  /**
209
- * The V2 (`opencode`) per-agent file. Measured at rulesync 43.11.0: the
209
+ * The V2 (`opencode`) per-agent file. Measured at rulesync 43.13.0: the
210
210
  * frontmatter is `description`, `mode`, `model` (when declared) and
211
211
  * `permissions` (when the persona declares one), with no `name` key — OpenCode
212
212
  * 2.x names the agent from the file, and a stray V1 field would make the file
@@ -240,7 +240,7 @@ function verifyOpencodeV2Agent(persona, source, parsed, managedPath) {
240
240
  verifyBody(persona, source, parsed.body, managedPath);
241
241
  }
242
242
  /**
243
- * The V1 (`opencodev1`) per-agent file. Measured at rulesync 43.11.0: the
243
+ * The V1 (`opencodev1`) per-agent file. Measured at rulesync 43.13.0: the
244
244
  * frontmatter is `name`, `description`, `mode`, `model` (when declared) and the
245
245
  * per-tool `permission` map (when the persona declares one). `temperature` and
246
246
  * `tools` are V1 keys this product does not author, so they must never appear;
@@ -428,7 +428,7 @@ const HOOK_EVENT_BY_PHASE = {
428
428
  };
429
429
  /**
430
430
  * The V2 (`opencode`) plugin's registration anchors, measured at rulesync
431
- * 43.11.0: a tool hook (`ctx.tool.hook("execute.before"|"execute.after", ...)`)
431
+ * 43.13.0: a tool hook (`ctx.tool.hook("execute.before"|"execute.after", ...)`)
432
432
  * and an event hook whose `label: "<event>"` has no tool anchor.
433
433
  */
434
434
  const OPENCODE_V2_HOOK_ANCHORS = [
@@ -442,7 +442,7 @@ const OPENCODE_V2_HOOK_ANCHORS = [
442
442
  ];
443
443
  /**
444
444
  * The V1 (`opencodev1`) `RulesyncHooksPlugin` registration anchors, measured at
445
- * rulesync 43.11.0: the returned object keys `"tool.execute.before"` and
445
+ * rulesync 43.13.0: the returned object keys `"tool.execute.before"` and
446
446
  * `"tool.execute.after"`, and the event-loop branches
447
447
  * `event.type === "session.created"` / `event.type === "session.idle"`. The
448
448
  * V1 plugin also carries `label: "sessionStart"|"stop"|"postToolUse"` inside
@@ -459,6 +459,60 @@ const OPENCODE_V1_HOOK_ANCHORS = [
459
459
  },
460
460
  { pattern: /event\.type === "session\.idle"/g, event: () => 'stop', registration: true },
461
461
  ];
462
+ /**
463
+ * The canonical (Claude Code) tool names rulesync rewrites inside a hook
464
+ * matcher, and the id each OpenCode target reports at run time. Measured at
465
+ * rulesync 43.13.0: OpenCode 1.x reports `read`/`write`/`edit` and calls its
466
+ * shell tool `bash`, while OpenCode 2.x calls the same tool `shell`.
467
+ */
468
+ const OPENCODE_V1_HOOK_TOOL_NAMES = {
469
+ Bash: 'bash',
470
+ Read: 'read',
471
+ Write: 'write',
472
+ Edit: 'edit',
473
+ };
474
+ const OPENCODE_V2_HOOK_TOOL_NAMES = {
475
+ Bash: 'shell',
476
+ Read: 'read',
477
+ Write: 'write',
478
+ Edit: 'edit',
479
+ };
480
+ /** The tool-name map for one OpenCode major (see the two tables above). */
481
+ function opencodeHookToolNames(target) {
482
+ return target === 'opencodev1' ? OPENCODE_V1_HOOK_TOOL_NAMES : OPENCODE_V2_HOOK_TOOL_NAMES;
483
+ }
484
+ /**
485
+ * rulesync's whole-name tokenisation of a matcher, measured at 43.13.0: escapes
486
+ * and character classes are skipped, and a name is only a name at a word
487
+ * boundary (or after an escaped word character).
488
+ */
489
+ const MATCHER_TOKEN_PATTERN = /\\.|\[(?:\\.|[^\]\\])*\]|(?<=^|[^\w\\]|\\\w)[A-Za-z_]\w*(?!\w)/gs;
490
+ /** A token that is a whole name, not an escape or a character class. */
491
+ function isMatcherName(token) {
492
+ return !token.startsWith('\\') && !token.startsWith('[');
493
+ }
494
+ /** Rewrite the whole tool names in a matcher, leaving escapes and classes as written. */
495
+ function replaceMatcherNames(matcher, names) {
496
+ return matcher.replace(MATCHER_TOKEN_PATTERN, (token) => isMatcherName(token) && Object.hasOwn(names, token) ? (names[token] ?? token) : token);
497
+ }
498
+ /** A matcher made only of plain names joined by `|`, which Claude Code matches as an exact list. */
499
+ const PLAIN_TOOL_NAME_MATCHER = /^[A-Za-z0-9_]+(?:\|[A-Za-z0-9_]+)*$/;
500
+ /**
501
+ * The matcher rulesync emits for a declared one, measured at 43.13.0. Canonical
502
+ * tool names are translated to the target's ids, and a pure name-list matcher
503
+ * is anchored as `^(?:...)$` so `write` does not also match `todowrite`; a
504
+ * regex-style matcher, or one with no translated name, is written as declared.
505
+ * The source schema refuses newline, carriage return and NUL in a matcher, so
506
+ * the control-character strip rulesync applies to the emitted literal is a
507
+ * no-op here.
508
+ */
509
+ function emittedHookMatcher(matcher, target) {
510
+ const translated = replaceMatcherNames(matcher, opencodeHookToolNames(target));
511
+ if (translated !== matcher && PLAIN_TOOL_NAME_MATCHER.test(matcher)) {
512
+ return `^(?:${translated})$`;
513
+ }
514
+ return translated;
515
+ }
462
516
  /**
463
517
  * The hooks plugin is generated JavaScript, so it is verified at the level that
464
518
  * matters: exactly the declared commands, each bound to its declared event and
@@ -471,7 +525,9 @@ const OPENCODE_V1_HOOK_ANCHORS = [
471
525
  * registration. The anchor set is the only thing that differs between the two
472
526
  * OpenCode majors: V2 registers through `ctx.tool.hook(...)` and a label-only
473
527
  * event loop, V1 through the returned `"tool.execute.*"` keys and
474
- * `event.type === ...` branches.
528
+ * `event.type === ...` branches. The declared matcher is translated the way the
529
+ * renderer translates it (`emittedHookMatcher`) before it is compared, so a
530
+ * target-name rewrite is not mistaken for a dropped or invented matcher.
475
531
  *
476
532
  * An `after-tool` command in V2 is emitted inside `ctx.tool.hook("execute.after",
477
533
  * ...)` *and* carries `label: "postToolUse"` in the same `__rulesyncRunHook(...)`
@@ -480,7 +536,7 @@ const OPENCODE_V1_HOOK_ANCHORS = [
480
536
  * previous command's end, so a matcher-less hook cannot inherit its neighbour's
481
537
  * guard.
482
538
  */
483
- function verifyHookPlugin(source, text, subject, anchorPatterns) {
539
+ function verifyHookPlugin(source, text, subject, target, anchorPatterns) {
484
540
  const anchors = [];
485
541
  for (const { pattern, event, registration } of anchorPatterns) {
486
542
  for (const match of text.matchAll(pattern)) {
@@ -570,7 +626,7 @@ function verifyHookPlugin(source, text, subject, anchorPatterns) {
570
626
  .map((hook) => ({
571
627
  event: HOOK_EVENT_BY_PHASE[hook.phase] ?? '',
572
628
  command: hook.command,
573
- ...(hook.matcher === undefined ? {} : { matcher: hook.matcher }),
629
+ ...(hook.matcher === undefined ? {} : { matcher: emittedHookMatcher(hook.matcher, target) }),
574
630
  }))
575
631
  .sort((a, b) => compareCodepoints(`${a.event}\0${a.command}`, `${b.event}\0${b.command}`));
576
632
  registrations.sort((a, b) => compareCodepoints(`${a.event}\0${a.command}`, `${b.event}\0${b.command}`));
@@ -647,7 +703,7 @@ async function verifyOpencodeWorkspace(source, outputRoot, target) {
647
703
  if (text.length === 0) {
648
704
  throw declaredNotEmitted('hooks plugin is empty', { subject: OPENCODE_HOOKS_PLUGIN });
649
705
  }
650
- verifyHookPlugin(source, text, OPENCODE_HOOKS_PLUGIN, target === 'opencodev1' ? OPENCODE_V1_HOOK_ANCHORS : OPENCODE_V2_HOOK_ANCHORS);
706
+ verifyHookPlugin(source, text, OPENCODE_HOOKS_PLUGIN, target, target === 'opencodev1' ? OPENCODE_V1_HOOK_ANCHORS : OPENCODE_V2_HOOK_ANCHORS);
651
707
  verified.set(OPENCODE_HOOKS_PLUGIN, text);
652
708
  }
653
709
  return verified;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "farna-bootstrapper",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Compile named agent personas and resources into the configuration each coding harness reads, and prove that the output still matches the source.",
5
5
  "license": "MIT",
6
6
  "author": "Farnalabs Ltd",
@@ -68,7 +68,7 @@
68
68
  "dependencies": {
69
69
  "commander": "14.0.1",
70
70
  "jsonc-parser": "3.3.1",
71
- "rulesync": "43.11.0",
71
+ "rulesync": "43.13.0",
72
72
  "yaml": "2.9.1",
73
73
  "zod": "4.6.7"
74
74
  },