agent-personalizer 0.6.4 → 0.7.0

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.
@@ -1,9 +1,94 @@
1
1
  'use strict';
2
- // Shared, read-only install recipe. Uninstall compares against the same templates and copies.
2
+ // Shared, read-only recipe and ownership checks. Recorded hashes are the owning inventory;
3
+ // the static release table recognizes untouched copies from before hashes were recorded.
3
4
  const fs = require('fs');
4
5
  const path = require('path');
6
+ const crypto = require('crypto');
5
7
  const PKG = path.resolve(__dirname, '..');
6
8
  const onboarding = require('../render/onboarding.cjs');
9
+ const RUNTIME_FILES = ['render/render.cjs', 'render/targets.json', 'render/onboarding.cjs', 'hooks/README.md', 'hooks/claude-code/session-start.sh', 'check/gate.cjs', 'check/forbidden.example.txt'];
10
+ const sha256 = bytes => crypto.createHash('sha256').update(bytes).digest('hex');
11
+ const stripSignature = text => text.split('\n').filter(line => !line.includes('40-sign-every-edit.md') && !/^`?Last edited by:/.test(line)).join('\n');
12
+
13
+ function recordedHash(cfg, rel) {
14
+ return cfg.installed && Object.hasOwn(cfg.installed, rel) ? cfg.installed[rel] : undefined;
15
+ }
16
+ function validateInventory(cfg) {
17
+ if ('installed' in cfg && (!cfg.installed || typeof cfg.installed !== 'object' || Array.isArray(cfg.installed))) throw new Error('"installed" must be a path-to-sha256 object');
18
+ const validPath = rel => typeof rel === 'string' && rel && !path.isAbsolute(rel) && !rel.includes('\\') && !rel.split('/').some(p => !p || p === '.' || p === '..');
19
+ for (const [rel, hash] of Object.entries(cfg.installed || {})) {
20
+ if (!validPath(rel) || rel === '.agent-personalizer.json') throw new Error(`invalid installed path ${JSON.stringify(rel)}`);
21
+ if (typeof hash !== 'string' || !/^[a-f0-9]{64}$/.test(hash)) throw new Error(`invalid installed sha256 for ${rel}`);
22
+ }
23
+ if ('createdDirectories' in cfg && (!Array.isArray(cfg.createdDirectories) || cfg.createdDirectories.some(rel => !validPath(rel)) || new Set(cfg.createdDirectories).size !== cfg.createdDirectories.length)) throw new Error('"createdDirectories" must be a unique list of relative paths');
24
+ if ('toolingVersions' in cfg && (!cfg.toolingVersions || typeof cfg.toolingVersions !== 'object' || Array.isArray(cfg.toolingVersions))) throw new Error('"toolingVersions" must be an object');
25
+ }
26
+ function knownVersions(rel, bytes) {
27
+ const hashes = require('./known-hashes.json').files[rel] || {};
28
+ const hash = sha256(bytes);
29
+ return Object.entries(hashes).filter(([, variants]) => Object.values(variants).includes(hash)).map(([version]) => version.slice(1));
30
+ }
31
+ function ownership(cfg, rel, bytes, { legacy = false, expected } = {}) {
32
+ const recorded = recordedHash(cfg, rel);
33
+ const versions = knownVersions(rel, bytes);
34
+ const version = (cfg.toolingVersions || {})[rel] || cfg.toolingVersion || versions.at(-1);
35
+ return { unedited: recorded !== undefined ? sha256(bytes) === recorded : legacy && (versions.length > 0 || (expected && bytes.equals(expected))), version };
36
+ }
37
+
38
+ // Match one semantic pointer slot at a time, then compare the whole original line.
39
+ // Splitting retains every line terminator so a kept user line is byte-for-byte unchanged.
40
+ function migrateHome(current, previous, next, markerState) {
41
+ const key = line => {
42
+ if (/^- Notes(?: the|:)/.test(line)) return 'notes';
43
+ if (/^- Session log/.test(line)) return 'sessions';
44
+ if (/^- Decisions log/.test(line)) return 'decisions';
45
+ if (/^- Inbox/.test(line)) return 'inbox';
46
+ if (/^- Rules, one file each/.test(line)) return 'rules';
47
+ const standing = /^- ([^.]+)\..*\[owner:/.exec(line);
48
+ return standing ? `rule:${standing[1]}` : null;
49
+ };
50
+ const template = text => text.split('\n').map(line => ({ key: key(line), text: line })).filter(line => line.key);
51
+ const before = template(previous), after = template(next);
52
+ const old = new Map(before.map(line => [line.key, line.text]));
53
+ const fresh = new Map(after.map(line => [line.key, line.text]));
54
+ const ms = markerState(current);
55
+ let offset = 0;
56
+ const lines = (current.match(/[^\n]*(?:\n|$)/g) || []).filter(Boolean).map(raw => {
57
+ const line = { raw, text: raw.endsWith('\n') ? raw.slice(0, -1) : raw, outside: ms.kind !== 'one' || offset < ms.bStart || offset >= ms.eEnd };
58
+ offset += raw.length;
59
+ return line;
60
+ });
61
+ const edited = [];
62
+ let changed = false;
63
+ for (const [id, text] of old) {
64
+ const replacement = fresh.get(id);
65
+ const matches = lines.filter(line => line.outside && line.text === text);
66
+ if (!matches.length) {
67
+ for (const line of lines) if (line.outside && key(line.text) === id) edited.push(line.text);
68
+ continue;
69
+ }
70
+ if (text === replacement) continue;
71
+ for (const line of matches) {
72
+ line.raw = replacement === undefined ? '' : replacement + (line.raw.endsWith('\n') ? '\n' : '');
73
+ line.text = replacement || '';
74
+ changed = true;
75
+ }
76
+ }
77
+ for (let i = 0; i < after.length; i++) {
78
+ const entry = after[i];
79
+ if (old.has(entry.key) || lines.some(line => line.outside && line.text === entry.text)) continue;
80
+ let anchor = -1;
81
+ for (let j = i - 1; j >= 0 && anchor === -1; j--) anchor = lines.findLastIndex(line => line.outside && key(line.text) === after[j].key && line.raw);
82
+ // The rules-directory pointer goes immediately after the onboarding pointer.
83
+ if (anchor === -1 && entry.key === 'rules') anchor = lines.findIndex(line => line.outside && /^- Agent onboarding /.test(line.text));
84
+ if (anchor === -1) { edited.push(`missing pointer: ${entry.text}`); continue; }
85
+ const ending = lines[anchor].raw.endsWith('\n') ? '\n' : '';
86
+ if (!ending) { edited.push(`missing pointer: ${entry.text}`); continue; }
87
+ lines.splice(anchor + 1, 0, { raw: entry.text + '\n', text: entry.text, outside: true });
88
+ changed = true;
89
+ }
90
+ return { text: lines.map(line => line.raw).join(''), edited, changed };
91
+ }
7
92
 
8
93
  function installPlan({ answers, level, targets, notesScaffolded }) {
9
94
  const base = onboarding.baseFor(answers);
@@ -12,7 +97,7 @@ function installPlan({ answers, level, targets, notesScaffolded }) {
12
97
  // signature=no: the signature rule file is not installed (a `requires:` rule that is absent cannot drift back in),
13
98
  // and every copied markdown loses its `Last edited by:` template line and its pointer to the rule.
14
99
  const noSig = answers.signature === 'no';
15
- const stripSig = (t) => noSig ? t.split('\n').filter(l => !l.includes('40-sign-every-edit.md') && !/^`?Last edited by:/.test(l)).join('\n') : t;
100
+ const stripSig = (t) => noSig ? stripSignature(t) : t;
16
101
  const mdCopy = (rel, src) => plan.push({ rel, text: stripSig(fs.readFileSync(src, 'utf8')) });
17
102
  // rules/ is copied at level 3, where it becomes yours to edit; levels 1 and 2 render from the package's rules
18
103
  if (level >= 3) for (const f of fs.readdirSync(path.join(PKG, 'rules')).sort()) {
@@ -59,11 +144,11 @@ function installPlan({ answers, level, targets, notesScaffolded }) {
59
144
  mdCopy(`${base}/inbox/README.md`, path.join(PKG, 'templates', 'INBOX_README.md'));
60
145
  }
61
146
  if (level >= 3) {
62
- for (const rel of ['render/render.cjs', 'render/targets.json', 'render/onboarding.cjs', 'hooks/README.md', 'hooks/claude-code/session-start.sh', 'check/gate.cjs', 'check/forbidden.example.txt'])
147
+ for (const rel of RUNTIME_FILES)
63
148
  plan.push({ rel, src: path.join(PKG, rel) });
64
149
  }
65
150
 
66
151
  return { plan, stripSig, notesWhy, NOTES_ONE_LINE, NOTES_LATER };
67
152
  }
68
153
 
69
- module.exports = { installPlan };
154
+ module.exports = { installPlan, RUNTIME_FILES, stripSignature, sha256, recordedHash, validateInventory, knownVersions, ownership, migrateHome };