agent-personalizer 0.8.3 → 0.8.5
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.
- package/CHANGELOG.md +40 -2
- package/CONTRIBUTING.md +2 -2
- package/README.md +18 -8
- package/RELEASING.md +1 -1
- package/SECURITY.md +4 -2
- package/bin/agent-personalizer.js +132 -82
- package/bin/git-exclude.cjs +1 -1
- package/bin/global.cjs +201 -0
- package/bin/home.cjs +15 -0
- package/bin/install-plan.cjs +7 -2
- package/bin/known-hashes.json +78 -0
- package/bin/learn.cjs +4 -2
- package/bin/sessions.cjs +64 -16
- package/bin/uninstall.cjs +2 -1
- package/docs/guarantees.md +2 -2
- package/docs/install.md +29 -5
- package/docs/learning.md +9 -6
- package/docs/paste-guide.md +5 -3
- package/docs/tiers.md +1 -1
- package/examples/freelance-illustrator/.agent-personalizer.json +3 -3
- package/examples/freelance-illustrator/AGENTS.md +1 -1
- package/examples/freelance-illustrator/AGENT_ONBOARDING.md +2 -2
- package/examples/freelance-illustrator/CLAUDE.md +1 -1
- package/examples/freelance-illustrator/rules/30-folder-index-is-part-of-the-change.md +2 -2
- package/examples/freelance-illustrator/rules/README.md +2 -0
- package/examples/lab-technician/.agent-personalizer.json +3 -3
- package/examples/lab-technician/AGENTS.md +1 -1
- package/examples/lab-technician/AGENT_ONBOARDING.md +1 -1
- package/examples/lab-technician/CLAUDE.md +1 -1
- package/examples/lab-technician/rules/30-folder-index-is-part-of-the-change.md +2 -2
- package/examples/lab-technician/rules/README.md +2 -0
- package/examples/product-designer/.agent-personalizer.json +3 -3
- package/examples/product-designer/AGENTS.md +1 -1
- package/examples/product-designer/AGENT_ONBOARDING.md +1 -1
- package/examples/product-designer/CLAUDE.md +1 -1
- package/examples/product-designer/rules/30-folder-index-is-part-of-the-change.md +2 -2
- package/examples/product-designer/rules/README.md +2 -0
- package/hooks/README.md +5 -3
- package/package.json +1 -1
- package/render/onboarding.cjs +6 -3
- package/render/render.cjs +14 -6
- package/rules/30-folder-index-is-part-of-the-change.md +2 -2
- package/rules/README.md +2 -0
package/bin/global.cjs
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// Global targets have fixed home-relative names; profile ownership stays inside its own folder.
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const { resolveHome } = require('./home.cjs');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const renderer = require('../render/render.cjs');
|
|
7
|
+
const onboarding = require('../render/onboarding.cjs');
|
|
8
|
+
const { sha256, validateInventory } = require('./install-plan.cjs');
|
|
9
|
+
const SOURCE = '.agent-personalizer';
|
|
10
|
+
const CONFIG = `${SOURCE}/.agent-personalizer.json`;
|
|
11
|
+
const TARGETS = { claude: '.claude/CLAUDE.md', agents: '.codex/AGENTS.md', gemini: '.gemini/GEMINI.md' };
|
|
12
|
+
const HASH = /^[a-f0-9]{64}$/;
|
|
13
|
+
const refuse = message => { throw new renderer.Refusal(message); };
|
|
14
|
+
|
|
15
|
+
function decode(bytes, rel) {
|
|
16
|
+
try { return new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(bytes); }
|
|
17
|
+
catch (_) { refuse(`${rel} is not valid UTF-8; nothing was written`); }
|
|
18
|
+
}
|
|
19
|
+
function blockHash(text, rel) {
|
|
20
|
+
const ms = renderer.markerState(text, rel);
|
|
21
|
+
if (ms.kind === 'malformed') refuse(`${rel}: malformed marker block; nothing was changed`);
|
|
22
|
+
return ms.kind === 'one' ? sha256(Buffer.from(text.slice(ms.bStart, ms.eEnd))) : null;
|
|
23
|
+
}
|
|
24
|
+
function validateGlobal(cfg) {
|
|
25
|
+
if (!cfg.global || cfg.level !== 1) refuse('global profile config must identify a level-1 global install');
|
|
26
|
+
for (const target of cfg.targets || []) if (!Object.hasOwn(TARGETS, target) && target !== 'onboarding') refuse(`unsupported global target: ${target}`);
|
|
27
|
+
if (cfg.globalFiles !== undefined && (!cfg.globalFiles || typeof cfg.globalFiles !== 'object' || Array.isArray(cfg.globalFiles))) refuse('invalid global target inventory');
|
|
28
|
+
for (const [key, info] of Object.entries(cfg.globalFiles || {})) {
|
|
29
|
+
if (!Object.hasOwn(TARGETS, key) || !info || typeof info.created !== 'boolean' || !HASH.test(info.blockHash) || !HASH.test(info.fileHash)
|
|
30
|
+
|| !['', '\n', '\n\n'].includes(info.separator) || typeof info.appended !== 'boolean') refuse('invalid global target ownership');
|
|
31
|
+
}
|
|
32
|
+
if (cfg.globalDirectories !== undefined && (!Array.isArray(cfg.globalDirectories) || cfg.globalDirectories.some(rel => ![SOURCE, '.claude', '.codex', '.gemini'].includes(rel)))) refuse('invalid global directory inventory');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async function run({ args, commitPlan, probe, readConfig, ask }) {
|
|
36
|
+
const prior = renderer.DIE_THROWS;
|
|
37
|
+
renderer.DIE_THROWS = true;
|
|
38
|
+
try { return await execute({ args, commitPlan, probe, readConfig, ask }); }
|
|
39
|
+
finally { renderer.DIE_THROWS = prior; }
|
|
40
|
+
}
|
|
41
|
+
async function execute({ args, commitPlan, probe, readConfig, ask }) {
|
|
42
|
+
if ('--dir' in args) refuse('--global cannot be combined with --dir');
|
|
43
|
+
if ('--level' in args && args['--level'] !== '1') refuse('--global supports only --level 1');
|
|
44
|
+
const home = resolveHome();
|
|
45
|
+
const info = fs.lstatSync(home);
|
|
46
|
+
if (info.isSymbolicLink() || !info.isDirectory()) refuse('global home must be a real directory, never a symlink');
|
|
47
|
+
const root = fs.realpathSync(home);
|
|
48
|
+
const source = probe(root, SOURCE);
|
|
49
|
+
if (source.exists && !fs.lstatSync(source.full).isDirectory()) refuse(`${SOURCE} is not a directory`);
|
|
50
|
+
const profileRoot = path.join(root, SOURCE);
|
|
51
|
+
const stored = readConfig(profileRoot, 1);
|
|
52
|
+
if (stored.cfgExists) validateGlobal(stored.cfg);
|
|
53
|
+
if (args.command === 'learn') {
|
|
54
|
+
for (const opt of ['--ai', '--level', '--answers', '--defaults', '--quick', '--full', '--uninstall', '--dry']) if (opt in args) refuse(`${opt} is not a learn option`);
|
|
55
|
+
if (args['--forget'] && (args['--all-projects'] || args['--yes'])) refuse('--forget cannot be combined with --all-projects or --yes');
|
|
56
|
+
if (!stored.cfgExists) refuse(`missing config: expected ${path.join(root, CONFIG)}`);
|
|
57
|
+
const learning = require('./learn.cjs');
|
|
58
|
+
return args['--forget'] ? learning.forget(profileRoot) : learning.learn({ dir: profileRoot, yes: !!args['--yes'], allProjects: true });
|
|
59
|
+
}
|
|
60
|
+
if (args['--forget'] || args['--all-projects']) refuse('--forget and --all-projects need learn');
|
|
61
|
+
if (args['--uninstall']) {
|
|
62
|
+
for (const opt of ['--ai', '--level', '--answers', '--defaults', '--quick', '--full']) if (opt in args) refuse(`${opt} is an install option; --uninstall reads the stored config`);
|
|
63
|
+
if (!stored.cfgExists) refuse(`missing config: expected ${path.join(root, CONFIG)}`);
|
|
64
|
+
return uninstall({ root, cfg: stored.cfg, dry: !!args['--dry'], commitPlan, probe });
|
|
65
|
+
}
|
|
66
|
+
if (args['--dry']) refuse('--dry needs --uninstall');
|
|
67
|
+
const interactive = !!process.stdin.isTTY && !args['--yes'];
|
|
68
|
+
if (!stored.cfgExists && !interactive && (!args['--yes'] || !args['--ai'])) refuse('non-interactive --global needs --ai and --yes');
|
|
69
|
+
let ais = args['--ai'] || (stored.cfgExists ? stored.cfg.targets.filter(t => t !== 'onboarding').join(',') : 'claude,agents');
|
|
70
|
+
if (interactive && !stored.cfgExists) ais = await ask('Global AIs (claude,agents,gemini)', ais);
|
|
71
|
+
const targets = ais.split(',').map(s => s.trim()).filter(Boolean);
|
|
72
|
+
if (!targets.length || new Set(targets).size !== targets.length) refuse('choose each global AI once');
|
|
73
|
+
for (const key of targets) if (!Object.hasOwn(TARGETS, key)) refuse(`--global supports only claude, agents and gemini (unsupported target: ${key})`);
|
|
74
|
+
if (args['--answers'] && args['--defaults']) refuse('--answers and --defaults are exclusive');
|
|
75
|
+
let answers = stored.prevAnswers || onboarding.defaults();
|
|
76
|
+
if (args['--answers']) {
|
|
77
|
+
const file = args['--answers'];
|
|
78
|
+
try { answers = onboarding.validate(JSON.parse(fs.readFileSync(file === '-' ? 0 : path.resolve(file), 'utf8'))); }
|
|
79
|
+
catch (e) { refuse(`--answers: ${e.message}`); }
|
|
80
|
+
}
|
|
81
|
+
const old = stored.cfg;
|
|
82
|
+
const installed = { ...(old.installed || {}) };
|
|
83
|
+
const writes = [];
|
|
84
|
+
const read = rel => {
|
|
85
|
+
const found = probe(root, `${SOURCE}/${rel}`);
|
|
86
|
+
if (!found.exists) return null;
|
|
87
|
+
if (!fs.lstatSync(found.full).isFile()) refuse(`${SOURCE}/${rel} is not a regular file`);
|
|
88
|
+
return fs.readFileSync(found.full);
|
|
89
|
+
};
|
|
90
|
+
// Validate the complete previous inventory, including paths omitted by this run.
|
|
91
|
+
for (const rel of Object.keys(installed)) read(rel);
|
|
92
|
+
for (const rel of old.createdDirectories || []) {
|
|
93
|
+
const found = probe(root, `${SOURCE}/${rel}`);
|
|
94
|
+
if (found.exists && !fs.lstatSync(found.full).isDirectory()) refuse(`${rel} is not a directory`);
|
|
95
|
+
}
|
|
96
|
+
renderer.preflightSources(profileRoot);
|
|
97
|
+
const learnedBytes = read('LEARNED.md');
|
|
98
|
+
const plannedLearned = learnedBytes || fs.readFileSync(path.join(__dirname, '../templates/LEARNED.md'));
|
|
99
|
+
const learned = { ...onboarding.parseLearned(decode(plannedLearned, 'LEARNED.md'), 'LEARNED.md'), hash: sha256(plannedLearned) };
|
|
100
|
+
const profile = read('USER.md');
|
|
101
|
+
const user = !profile || (installed['USER.md'] && sha256(profile) === installed['USER.md'])
|
|
102
|
+
? Buffer.from(onboarding.renderUser(answers, { notesScaffolded: false, entries: learned })) : profile;
|
|
103
|
+
function writeSource(rel, bytes, firstOnly = false) {
|
|
104
|
+
const existing = read(rel);
|
|
105
|
+
if (existing && (firstOnly || installed[rel] !== sha256(existing))) { console.log(`kept ${SOURCE}/${rel} (edited or pre-existing)`); return; }
|
|
106
|
+
installed[rel] = sha256(bytes);
|
|
107
|
+
writes.push({ rel: `${SOURCE}/${rel}`, bytes });
|
|
108
|
+
}
|
|
109
|
+
writeSource('USER.md', user);
|
|
110
|
+
if (!learnedBytes) writeSource('LEARNED.md', plannedLearned, true);
|
|
111
|
+
const cfg = { ...old, global: true, targets: [...targets, 'onboarding'], level: 1, ruleSet: 'core-5', onboarding: onboarding.sparse(answers), installed,
|
|
112
|
+
createdDirectories: old.createdDirectories || [], globalFiles: { ...(old.globalFiles || {}) }, toolingVersion: require('../package.json').version };
|
|
113
|
+
const renderCfg = { ...cfg, onboarding: answers };
|
|
114
|
+
const rendered = renderer.renderedContents(profileRoot, renderCfg, { profileSource: decode(user, 'USER.md'), learned });
|
|
115
|
+
const qualify = text => text
|
|
116
|
+
.replace(/`USER\.md` sits next to `(?:CLAUDE|AGENTS)\.md`\./g, 'Read `USER.md` from the global profile source.')
|
|
117
|
+
.replace(/"npx agent-personalizer --dir \.", or "node render\/render\.cjs --dir \." at level 3/g, '"npx agent-personalizer --global"')
|
|
118
|
+
.replace(/(^|[^A-Za-z0-9_./-])(USER\.md|LEARNED\.md|AGENT_ONBOARDING\.md|\.agent-personalizer\.json|\.agent-personalizer\/digest\.md)(?![A-Za-z0-9_/-])/g, (_, prefix, rel) => prefix + path.join(profileRoot, rel));
|
|
119
|
+
for (const entry of rendered) {
|
|
120
|
+
if (entry.key === 'onboarding') { writeSource(entry.rel, Buffer.from(renderer.splice(null, qualify(entry.block), entry.rel))); continue; }
|
|
121
|
+
const rel = TARGETS[entry.key];
|
|
122
|
+
const found = probe(root, rel);
|
|
123
|
+
if (found.exists && !fs.lstatSync(found.full).isFile()) refuse(`${rel} is not a regular file`);
|
|
124
|
+
const text = found.exists ? decode(fs.readFileSync(found.full), rel) : null;
|
|
125
|
+
const ms = text === null ? { kind: 'none' } : renderer.markerState(text, rel);
|
|
126
|
+
if (ms.kind === 'malformed') refuse(`${rel}: malformed marker block`);
|
|
127
|
+
if (ms.kind === 'one' && !Object.hasOwn(cfg.globalFiles, entry.key)) {
|
|
128
|
+
refuse(`${rel}: foreign agent-personalizer block; remove the block from <!-- agent-personalizer:begin --> through <!-- agent-personalizer:end -->, then re-run --global. Nothing was written`);
|
|
129
|
+
}
|
|
130
|
+
const block = `Profile source: \`${path.join(profileRoot, 'USER.md')}\`. Read \`${path.join(profileRoot, 'AGENT_ONBOARDING.md')}\` for working preferences.\n\n` + qualify(entry.block);
|
|
131
|
+
const output = renderer.splice(text, block, rel);
|
|
132
|
+
if (renderer.markerState(output, rel).kind !== 'one' || renderer.between(output, rel) !== block) refuse(`${rel}: generated block would not re-parse; nothing was written`);
|
|
133
|
+
const prior = ms.kind === 'one' ? cfg.globalFiles[entry.key] : null;
|
|
134
|
+
cfg.globalFiles[entry.key] = { created: !found.exists || !!cfg.globalFiles[entry.key]?.created, blockHash: blockHash(output, rel), fileHash: sha256(Buffer.from(output)),
|
|
135
|
+
appended: prior ? prior.appended : ms.kind === 'none', separator: prior ? prior.separator : (text === null ? '' : text.endsWith('\n') ? '\n' : '\n\n') };
|
|
136
|
+
writes.push({ rel, bytes: Buffer.from(output), report: `update ${rel} (global profile)` });
|
|
137
|
+
}
|
|
138
|
+
const learnRequested = interactive && !stored.cfgExists ? await require('./learn.cjs').requestConsent({ allProjects: true }) : false;
|
|
139
|
+
const outputPaths = new Set([CONFIG, ...writes.map(p => p.rel)]);
|
|
140
|
+
await commitPlan(root, { writes, outputPaths,
|
|
141
|
+
prepare(newDirectories) {
|
|
142
|
+
cfg.globalDirectories = [...new Set([...(old.globalDirectories || []), ...[...newDirectories].filter(rel => [SOURCE, '.claude', '.codex', '.gemini'].includes(rel))])].sort();
|
|
143
|
+
writes.push({ rel: CONFIG, bytes: Buffer.from(JSON.stringify(cfg, null, 2) + '\n') });
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
if (learnRequested) await require('./learn.cjs').learn({ dir: profileRoot, consented: true, allProjects: true });
|
|
147
|
+
console.log(`Global profile installed. Re-run with --global after editing ${profileRoot}.`);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
async function uninstall({ root, cfg, dry, commitPlan, probe }) {
|
|
151
|
+
validateInventory(cfg);
|
|
152
|
+
const writes = [], removals = [], retained = [];
|
|
153
|
+
const read = rel => {
|
|
154
|
+
const found = probe(root, rel);
|
|
155
|
+
if (!found.exists) return null;
|
|
156
|
+
if (!fs.lstatSync(found.full).isFile()) refuse(`${rel} is not a regular file; nothing was removed`);
|
|
157
|
+
return fs.readFileSync(found.full);
|
|
158
|
+
};
|
|
159
|
+
for (const [key, owned] of Object.entries(cfg.globalFiles || {})) {
|
|
160
|
+
const rel = TARGETS[key], bytes = read(rel);
|
|
161
|
+
if (!bytes) continue;
|
|
162
|
+
const text = decode(bytes, rel), ms = renderer.markerState(text, rel);
|
|
163
|
+
const currentHash = blockHash(text, rel);
|
|
164
|
+
if (ms.kind === 'none') continue;
|
|
165
|
+
if (currentHash !== owned.blockHash) { retained.push(rel); continue; }
|
|
166
|
+
let before = text.slice(0, ms.bStart), after = text.slice(ms.eEnd);
|
|
167
|
+
if (after.startsWith('\r\n')) after = after.slice(2);
|
|
168
|
+
else if (after.startsWith('\n')) after = after.slice(1);
|
|
169
|
+
if (owned.appended) {
|
|
170
|
+
if (owned.separator && before.endsWith(owned.separator)) before = before.slice(0, -owned.separator.length);
|
|
171
|
+
}
|
|
172
|
+
const output = before + after;
|
|
173
|
+
if (owned.created && output === '') removals.push({ rel });
|
|
174
|
+
else writes.push({ rel, bytes: Buffer.from(output) });
|
|
175
|
+
}
|
|
176
|
+
for (const [rel, hash] of Object.entries(cfg.installed || {})) {
|
|
177
|
+
const fullRel = `${SOURCE}/${rel}`, bytes = read(fullRel);
|
|
178
|
+
if (!bytes) continue;
|
|
179
|
+
if (sha256(bytes) === hash) removals.push({ rel: fullRel });
|
|
180
|
+
else retained.push(fullRel);
|
|
181
|
+
}
|
|
182
|
+
const directories = [...(cfg.globalDirectories || []), ...(cfg.createdDirectories || []).map(rel => `${SOURCE}/${rel}`)];
|
|
183
|
+
for (const rel of directories) {
|
|
184
|
+
const found = probe(root, rel);
|
|
185
|
+
if (found.exists && !fs.lstatSync(found.full).isDirectory()) refuse(`${rel} is not a directory`);
|
|
186
|
+
}
|
|
187
|
+
if (!retained.length) removals.push({ rel: CONFIG });
|
|
188
|
+
for (const rel of retained) console.log(`kept ${rel} (edited)`);
|
|
189
|
+
if (dry) {
|
|
190
|
+
for (const p of [...writes, ...removals]) console.log(`would ${p.bytes ? 'update' : 'remove'} ${p.rel}`);
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
const outputPaths = new Set([...writes, ...removals].map(p => p.rel));
|
|
194
|
+
await commitPlan(root, { writes, removals, outputPaths });
|
|
195
|
+
for (const rel of directories.sort((a, b) => b.split('/').length - a.split('/').length)) {
|
|
196
|
+
const found = probe(root, rel);
|
|
197
|
+
if (found.exists && fs.readdirSync(found.full).length === 0) fs.rmdirSync(found.full);
|
|
198
|
+
}
|
|
199
|
+
console.log(retained.length ? 'Global configuration kept for review of edited files.' : 'Global profile uninstalled.');
|
|
200
|
+
}
|
|
201
|
+
module.exports = { run };
|
package/bin/home.cjs
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const { Refusal } = require('../render/render.cjs');
|
|
4
|
+
|
|
5
|
+
function resolveHome(home = process.env.HOME) {
|
|
6
|
+
if (process.platform === 'win32' && (home === undefined || home === '')) home = process.env.USERPROFILE;
|
|
7
|
+
// On Windows a root-relative path such as \foo is "absolute" but lands on the current drive; require a drive or UNC root.
|
|
8
|
+
const rooted = path.sep !== '\\' || /^(?:[A-Za-z]:[\\/]|[\\/]{2}[^\\/?.]+[\\/][^\\/]+)/.test(home || '');
|
|
9
|
+
if (typeof home !== 'string' || !home || !path.isAbsolute(home) || !rooted) {
|
|
10
|
+
throw new Refusal('HOME (or USERPROFILE on Windows) must be set to a non-empty absolute path; nothing was written');
|
|
11
|
+
}
|
|
12
|
+
return home;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
module.exports = { resolveHome };
|
package/bin/install-plan.cjs
CHANGED
|
@@ -90,7 +90,7 @@ function migrateHome(current, previous, next, markerState) {
|
|
|
90
90
|
return { text: lines.map(line => line.raw).join(''), edited, changed };
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
-
function installPlan({ answers, level, targets, notesScaffolded }) {
|
|
93
|
+
function installPlan({ answers, level, targets, notesScaffolded, legacyRules = false }) {
|
|
94
94
|
const base = onboarding.baseFor(answers);
|
|
95
95
|
const kind = onboarding.kindOf(answers);
|
|
96
96
|
const plan = []; // { rel, src | text }
|
|
@@ -127,7 +127,12 @@ function installPlan({ answers, level, targets, notesScaffolded }) {
|
|
|
127
127
|
let t = fs.readFileSync(path.join(PKG, 'templates', name), 'utf8');
|
|
128
128
|
if (level < 3) {
|
|
129
129
|
// no local rules/: the pointers point at the rendered block below, which carries the full text
|
|
130
|
-
|
|
130
|
+
const core = require('../render/render.cjs').CORE_RULE_FILES;
|
|
131
|
+
t = t.split('\n').filter(l => {
|
|
132
|
+
if (/Rules, one file each, the owning copy/.test(l)) return false;
|
|
133
|
+
const rule = /\[owner: rules\/([^\]]+)\]/.exec(l);
|
|
134
|
+
return legacyRules || !rule || core.has(rule[1]);
|
|
135
|
+
}).join('\n');
|
|
131
136
|
t = t.replace(/`\[owner: rules\/[^\]]+\]`/g, '`[owner: the rendered block below]`');
|
|
132
137
|
}
|
|
133
138
|
if (notesWhy) {
|
package/bin/known-hashes.json
CHANGED
|
@@ -52,6 +52,9 @@
|
|
|
52
52
|
},
|
|
53
53
|
"v0.7.2": {
|
|
54
54
|
"original": "ba6495eedfff5c5ea3658e4b7c4072f7505b0860414d67150e090366ca87e772"
|
|
55
|
+
},
|
|
56
|
+
"v0.8.4": {
|
|
57
|
+
"original": "ba6495eedfff5c5ea3658e4b7c4072f7505b0860414d67150e090366ca87e772"
|
|
55
58
|
}
|
|
56
59
|
},
|
|
57
60
|
"check/gate.cjs": {
|
|
@@ -99,6 +102,9 @@
|
|
|
99
102
|
},
|
|
100
103
|
"v0.7.2": {
|
|
101
104
|
"original": "52e959a266d68e1e9f0de1373786bdfb5a7792c093145813623714d7d4f83c86"
|
|
105
|
+
},
|
|
106
|
+
"v0.8.4": {
|
|
107
|
+
"original": "f9e1fb60929fd97cc52df93662b6faa39a022d95d83115c925b7c2047b12ab7c"
|
|
102
108
|
}
|
|
103
109
|
},
|
|
104
110
|
"hooks/README.md": {
|
|
@@ -169,6 +175,10 @@
|
|
|
169
175
|
"v0.7.2": {
|
|
170
176
|
"original": "fb1daf41acd72d3bf01fb620b8f8f09f118154b983d634547352b944c07f72bc",
|
|
171
177
|
"withoutSignature": "fb1daf41acd72d3bf01fb620b8f8f09f118154b983d634547352b944c07f72bc"
|
|
178
|
+
},
|
|
179
|
+
"v0.8.4": {
|
|
180
|
+
"original": "7dbb140c6b56a895500d43c71a95e36b2c8ab90b0a240e2f8a042368fed3ea67",
|
|
181
|
+
"withoutSignature": "7dbb140c6b56a895500d43c71a95e36b2c8ab90b0a240e2f8a042368fed3ea67"
|
|
172
182
|
}
|
|
173
183
|
},
|
|
174
184
|
"hooks/claude-code/session-start.sh": {
|
|
@@ -222,6 +232,9 @@
|
|
|
222
232
|
},
|
|
223
233
|
"v0.7.2": {
|
|
224
234
|
"original": "282024ff74e8a7168c49dcd75e45d61da6709c8901f5f91938fc02cdc7c5cfed"
|
|
235
|
+
},
|
|
236
|
+
"v0.8.4": {
|
|
237
|
+
"original": "282024ff74e8a7168c49dcd75e45d61da6709c8901f5f91938fc02cdc7c5cfed"
|
|
225
238
|
}
|
|
226
239
|
},
|
|
227
240
|
"render/onboarding.cjs": {
|
|
@@ -269,6 +282,9 @@
|
|
|
269
282
|
},
|
|
270
283
|
"v0.7.2": {
|
|
271
284
|
"original": "0a06b8fec458b98ed9a5219c02a592d3b3692187d279f2809db6058f7cb2d395"
|
|
285
|
+
},
|
|
286
|
+
"v0.8.4": {
|
|
287
|
+
"original": "eda6c3c068afed1ad195b6a5824e3feacdafb030c4678320634db1a29f74f090"
|
|
272
288
|
}
|
|
273
289
|
},
|
|
274
290
|
"render/render.cjs": {
|
|
@@ -316,6 +332,9 @@
|
|
|
316
332
|
},
|
|
317
333
|
"v0.7.2": {
|
|
318
334
|
"original": "a1ed005b082508c357569212d09f3e72b4b6cf3062c76d18796c6902a7195d1a"
|
|
335
|
+
},
|
|
336
|
+
"v0.8.4": {
|
|
337
|
+
"original": "b460b61a32f1fad0335cdd8cba6aaba25b8da52d0ddaebfd0ad8b06f9b8bedd6"
|
|
319
338
|
}
|
|
320
339
|
},
|
|
321
340
|
"render/targets.json": {
|
|
@@ -369,6 +388,9 @@
|
|
|
369
388
|
},
|
|
370
389
|
"v0.7.2": {
|
|
371
390
|
"original": "070209f11e929c89384d507975086955824014974886e2f84353d8b91d08fc83"
|
|
391
|
+
},
|
|
392
|
+
"v0.8.4": {
|
|
393
|
+
"original": "070209f11e929c89384d507975086955824014974886e2f84353d8b91d08fc83"
|
|
372
394
|
}
|
|
373
395
|
},
|
|
374
396
|
"rules/00-read-the-profile-first.md": {
|
|
@@ -439,6 +461,10 @@
|
|
|
439
461
|
"v0.7.2": {
|
|
440
462
|
"original": "3806ff9a3f4bd539d1689c56bfa6397eb29938f9400117cecc1f487cdf6506a2",
|
|
441
463
|
"withoutSignature": "3806ff9a3f4bd539d1689c56bfa6397eb29938f9400117cecc1f487cdf6506a2"
|
|
464
|
+
},
|
|
465
|
+
"v0.8.4": {
|
|
466
|
+
"original": "b937d12b1987a8b13e8d80ae143e1d7b74f84dda4541c361813367bd2f6002a9",
|
|
467
|
+
"withoutSignature": "b937d12b1987a8b13e8d80ae143e1d7b74f84dda4541c361813367bd2f6002a9"
|
|
442
468
|
}
|
|
443
469
|
},
|
|
444
470
|
"rules/10-evidence-before-assertion.md": {
|
|
@@ -509,6 +535,10 @@
|
|
|
509
535
|
"v0.7.2": {
|
|
510
536
|
"original": "ce6f7862af2078617433899aec3419227c116dd2722d47e7234d5b7615e1559d",
|
|
511
537
|
"withoutSignature": "ce6f7862af2078617433899aec3419227c116dd2722d47e7234d5b7615e1559d"
|
|
538
|
+
},
|
|
539
|
+
"v0.8.4": {
|
|
540
|
+
"original": "ce6f7862af2078617433899aec3419227c116dd2722d47e7234d5b7615e1559d",
|
|
541
|
+
"withoutSignature": "ce6f7862af2078617433899aec3419227c116dd2722d47e7234d5b7615e1559d"
|
|
512
542
|
}
|
|
513
543
|
},
|
|
514
544
|
"rules/20-one-owner-per-rule.md": {
|
|
@@ -579,6 +609,10 @@
|
|
|
579
609
|
"v0.7.2": {
|
|
580
610
|
"original": "5268e490404da41e4474a368e9b4ff38ba6375bcbc5c643ee742ceacd9c8535c",
|
|
581
611
|
"withoutSignature": "5268e490404da41e4474a368e9b4ff38ba6375bcbc5c643ee742ceacd9c8535c"
|
|
612
|
+
},
|
|
613
|
+
"v0.8.4": {
|
|
614
|
+
"original": "5268e490404da41e4474a368e9b4ff38ba6375bcbc5c643ee742ceacd9c8535c",
|
|
615
|
+
"withoutSignature": "5268e490404da41e4474a368e9b4ff38ba6375bcbc5c643ee742ceacd9c8535c"
|
|
582
616
|
}
|
|
583
617
|
},
|
|
584
618
|
"rules/30-folder-index-is-part-of-the-change.md": {
|
|
@@ -649,6 +683,10 @@
|
|
|
649
683
|
"v0.7.2": {
|
|
650
684
|
"original": "50863f6b0ea933c6c7209bf4bbfbfc28c7481aabe9001cdd49b730f58dd3010f",
|
|
651
685
|
"withoutSignature": "50863f6b0ea933c6c7209bf4bbfbfc28c7481aabe9001cdd49b730f58dd3010f"
|
|
686
|
+
},
|
|
687
|
+
"v0.8.4": {
|
|
688
|
+
"original": "50863f6b0ea933c6c7209bf4bbfbfc28c7481aabe9001cdd49b730f58dd3010f",
|
|
689
|
+
"withoutSignature": "50863f6b0ea933c6c7209bf4bbfbfc28c7481aabe9001cdd49b730f58dd3010f"
|
|
652
690
|
}
|
|
653
691
|
},
|
|
654
692
|
"rules/40-sign-every-edit.md": {
|
|
@@ -719,6 +757,10 @@
|
|
|
719
757
|
"v0.7.2": {
|
|
720
758
|
"original": "ec73f0d745eb106a3632f22383ecf4ab5f7b0f89657a9dd5f1bf3657c7d96562",
|
|
721
759
|
"withoutSignature": "ec73f0d745eb106a3632f22383ecf4ab5f7b0f89657a9dd5f1bf3657c7d96562"
|
|
760
|
+
},
|
|
761
|
+
"v0.8.4": {
|
|
762
|
+
"original": "ec73f0d745eb106a3632f22383ecf4ab5f7b0f89657a9dd5f1bf3657c7d96562",
|
|
763
|
+
"withoutSignature": "ec73f0d745eb106a3632f22383ecf4ab5f7b0f89657a9dd5f1bf3657c7d96562"
|
|
722
764
|
}
|
|
723
765
|
},
|
|
724
766
|
"rules/50-output-style.md": {
|
|
@@ -789,6 +831,10 @@
|
|
|
789
831
|
"v0.7.2": {
|
|
790
832
|
"original": "56a8f916863e730185c092c5b78750c12fadf926d41e435f151f251d94d933c4",
|
|
791
833
|
"withoutSignature": "56a8f916863e730185c092c5b78750c12fadf926d41e435f151f251d94d933c4"
|
|
834
|
+
},
|
|
835
|
+
"v0.8.4": {
|
|
836
|
+
"original": "56a8f916863e730185c092c5b78750c12fadf926d41e435f151f251d94d933c4",
|
|
837
|
+
"withoutSignature": "56a8f916863e730185c092c5b78750c12fadf926d41e435f151f251d94d933c4"
|
|
792
838
|
}
|
|
793
839
|
},
|
|
794
840
|
"rules/60-act-or-ask.md": {
|
|
@@ -859,6 +905,10 @@
|
|
|
859
905
|
"v0.7.2": {
|
|
860
906
|
"original": "77e3fc8f0064c5f537f4ee6991aef77b865fee756e20c9cab8ccc1137f520b30",
|
|
861
907
|
"withoutSignature": "77e3fc8f0064c5f537f4ee6991aef77b865fee756e20c9cab8ccc1137f520b30"
|
|
908
|
+
},
|
|
909
|
+
"v0.8.4": {
|
|
910
|
+
"original": "77e3fc8f0064c5f537f4ee6991aef77b865fee756e20c9cab8ccc1137f520b30",
|
|
911
|
+
"withoutSignature": "77e3fc8f0064c5f537f4ee6991aef77b865fee756e20c9cab8ccc1137f520b30"
|
|
862
912
|
}
|
|
863
913
|
},
|
|
864
914
|
"rules/README.md": {
|
|
@@ -929,6 +979,34 @@
|
|
|
929
979
|
"v0.7.2": {
|
|
930
980
|
"original": "d5a069dca7b498041992df74bbffd9907b68157709b239cece3d47106862eb48",
|
|
931
981
|
"withoutSignature": "e6bf5ac7404aaa264a0b7a1889389e891cb759667d99dd5bef803d96440b8607"
|
|
982
|
+
},
|
|
983
|
+
"v0.8.4": {
|
|
984
|
+
"original": "ef9e995f8a72fcaaac48eb1d2bde567a9f39559f64a6b921f924e7f460e76db8",
|
|
985
|
+
"withoutSignature": "534c18e439aced0f80ddaea04db31cf0df01f0cfcce17cfb84e88bffb740ff53"
|
|
986
|
+
}
|
|
987
|
+
},
|
|
988
|
+
"rules/65-ask-before-it-repeats.md": {
|
|
989
|
+
"v0.8.4": {
|
|
990
|
+
"original": "cbb8a463ab3ad3ac0d27a77fc99f98b773b355ce336f9c727ce8bb56c3a27dab",
|
|
991
|
+
"withoutSignature": "cbb8a463ab3ad3ac0d27a77fc99f98b773b355ce336f9c727ce8bb56c3a27dab"
|
|
992
|
+
}
|
|
993
|
+
},
|
|
994
|
+
"rules/70-their-words-point-at-the-thing.md": {
|
|
995
|
+
"v0.8.4": {
|
|
996
|
+
"original": "7d2765cc47762494634a5ec691f7f706ddc6e6f672a99ab8b60eb9d6bdcea505",
|
|
997
|
+
"withoutSignature": "7d2765cc47762494634a5ec691f7f706ddc6e6f672a99ab8b60eb9d6bdcea505"
|
|
998
|
+
}
|
|
999
|
+
},
|
|
1000
|
+
"rules/80-symptoms-are-not-causes.md": {
|
|
1001
|
+
"v0.8.4": {
|
|
1002
|
+
"original": "a6b67644715fa291acfa7db16d169b58599b1b70975a5488b901b332c609cc5d",
|
|
1003
|
+
"withoutSignature": "a6b67644715fa291acfa7db16d169b58599b1b70975a5488b901b332c609cc5d"
|
|
1004
|
+
}
|
|
1005
|
+
},
|
|
1006
|
+
"rules/90-surface-what-they-cant-see.md": {
|
|
1007
|
+
"v0.8.4": {
|
|
1008
|
+
"original": "27f16a59ab223195e8bdec69a92bf6edbd84423ecd85c797bb3288658b5982e1",
|
|
1009
|
+
"withoutSignature": "27f16a59ab223195e8bdec69a92bf6edbd84423ecd85c797bb3288658b5982e1"
|
|
932
1010
|
}
|
|
933
1011
|
}
|
|
934
1012
|
}
|
package/bin/learn.cjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Consent separates project inspection from access to the AI apps' private history.
|
|
3
3
|
const fs = require('fs');
|
|
4
4
|
const path = require('path');
|
|
5
|
-
const
|
|
5
|
+
const { resolveHome } = require('./home.cjs');
|
|
6
6
|
const crypto = require('crypto');
|
|
7
7
|
const readline = require('readline');
|
|
8
8
|
const renderer = require('../render/render.cjs');
|
|
@@ -21,12 +21,14 @@ agent-personalizer can read up to 20 of your most recent sessions from ${scope}
|
|
|
21
21
|
that Claude Code, Codex and Gemini CLI still keep, to find corrections you repeat.
|
|
22
22
|
· ${scopeHint}
|
|
23
23
|
· Only what you typed, never the AI's replies.
|
|
24
|
+
· Messages that look like secrets are skipped.
|
|
24
25
|
· Stays on this computer, saved to .agent-personalizer/digest.md.
|
|
25
26
|
· Your AI reads that file next time you open it, like anything you share with it.
|
|
26
27
|
· Other people's details in there? Add their names to your private list first.
|
|
27
28
|
· Delete it any time: npx agent-personalizer learn --forget
|
|
28
29
|
(your AI app's own history stays as it is)
|
|
29
30
|
· Codex's own memory notes are included when you choose Codex.
|
|
31
|
+
They are global to your Codex account, not limited to this folder.
|
|
30
32
|
· When your AI reads the file, its provider sees these messages again.
|
|
31
33
|
· Work or client accounts: check your agreement before reusing their sessions.
|
|
32
34
|
· Not affiliated with Anthropic, OpenAI or Google. Their formats can change.
|
|
@@ -155,7 +157,7 @@ async function learn({ dir = '.', yes = false, consented = false, allProjects =
|
|
|
155
157
|
const terms = list.exists ? require('../check/gate.cjs').loadList(list.full, { allowEmpty: true }).terms : [];
|
|
156
158
|
const entries = renderer.readLearned(root);
|
|
157
159
|
const sources = [...new Set((cfg.targets || []).map(target => SOURCE[target]).filter(Boolean))];
|
|
158
|
-
const home =
|
|
160
|
+
const home = resolveHome();
|
|
159
161
|
const data = await require('./sessions.cjs').readSessions({ home, projectDir: root, allProjects, sources, limit: 20, privateTerms: terms });
|
|
160
162
|
const now = new Date().toISOString();
|
|
161
163
|
const text = digestText(data, entries, { sources, home, terms, now });
|
package/bin/sessions.cjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const fs = require('node:fs');
|
|
4
4
|
const path = require('node:path');
|
|
5
|
-
const
|
|
5
|
+
const { resolveHome } = require('./home.cjs');
|
|
6
6
|
const crypto = require('node:crypto');
|
|
7
7
|
|
|
8
8
|
const MAX_LINE = 8 * 1024 * 1024;
|
|
@@ -17,7 +17,7 @@ function bump(state, reason, count = 1) {
|
|
|
17
17
|
|
|
18
18
|
function claudeProjectNames(projectDir) {
|
|
19
19
|
const absolute = path.resolve(projectDir);
|
|
20
|
-
return [...new Set([absolute.replace(/[\\/]/g, '-'), absolute.replace(/[:\\/]/g, '-')])];
|
|
20
|
+
return [...new Set([absolute.replace(/[\\/]/g, '-'), absolute.replace(/[:\\/]/g, '-'), absolute.replace(/[^a-zA-Z0-9]/g, '-'), absolute.replace(/[^a-zA-Z0-9_]/g, '-')])];
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
function belongsToProject(cwd, projectDir) {
|
|
@@ -29,6 +29,17 @@ function belongsToProject(cwd, projectDir) {
|
|
|
29
29
|
/* Both sides are compared in NFC and lower case: macOS often stores accents decomposed (NFD),
|
|
30
30
|
which would otherwise let a listed name through. Audit 0.8.0 F2. */
|
|
31
31
|
const CTRL = /[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/g;
|
|
32
|
+
const SECRETS = [
|
|
33
|
+
/-----BEGIN (?:[A-Z0-9]+ )*PRIVATE KEY-----/i,
|
|
34
|
+
/sk-[A-Za-z0-9_-]{20,}/,
|
|
35
|
+
/(?:gh[pousr]_|github_pat_)[A-Za-z0-9_]+/,
|
|
36
|
+
/AKIA[A-Z0-9]{16}/,
|
|
37
|
+
/xox[baprs]-[A-Za-z0-9-]+/,
|
|
38
|
+
/AIza[A-Za-z0-9_-]{20,}/,
|
|
39
|
+
/Bearer\s+[A-Za-z0-9._~+\/-]{20,}/i,
|
|
40
|
+
/(?:api[_-]?key|secret|token|password|passwd)["']?\s*[:=]\s*\S{8,}/i
|
|
41
|
+
];
|
|
42
|
+
const secretMatch = text => SECRETS.some(pattern => pattern.test(String(text).replace(CTRL, '')));
|
|
32
43
|
const fold = (text) => String(text).replace(CTRL, '').normalize('NFC').toLowerCase();
|
|
33
44
|
const compact = (text) => fold(text).replace(/\s+/g, '');
|
|
34
45
|
function privateMatch(text, terms) {
|
|
@@ -60,6 +71,7 @@ function cutContextBlocks(text) {
|
|
|
60
71
|
|
|
61
72
|
function typedText(items, state) {
|
|
62
73
|
if (!items.length) { bump(state, 'non_typed'); return null; }
|
|
74
|
+
if (secretMatch(items.join('\n')) || secretMatch(items.join(''))) { bump(state, 'secret'); return null; }
|
|
63
75
|
// A private match drops the whole message, including its other text items.
|
|
64
76
|
if (privateMatch(items.join('\n'), state.privateTerms) || privateMatch(items.join(''), state.privateTerms)) { bump(state, 'private_match'); return null; }
|
|
65
77
|
const kept = [];
|
|
@@ -73,6 +85,7 @@ function typedText(items, state) {
|
|
|
73
85
|
if (text) kept.push(text);
|
|
74
86
|
}
|
|
75
87
|
const text = kept.join('\n');
|
|
88
|
+
if (secretMatch(text) || secretMatch(kept.join(''))) { bump(state, 'secret'); return null; }
|
|
76
89
|
if (privateMatch(text, state.privateTerms) || privateMatch(kept.join(''), state.privateTerms)) { bump(state, 'private_match'); return null; }
|
|
77
90
|
if (!text) { bump(state, 'empty_message'); return null; }
|
|
78
91
|
if (text.length > 2000) { bump(state, 'long_message'); return null; }
|
|
@@ -249,7 +262,21 @@ function parsedLine(line, state) {
|
|
|
249
262
|
}
|
|
250
263
|
|
|
251
264
|
function baseSession(file) {
|
|
252
|
-
return { source: file.source, date: new Date(file.mtimeMs).toISOString(), project: file.project, sessionId: path.basename(file.file), file: file.file, mtimeMs: file.mtimeMs, messages: [] };
|
|
265
|
+
return { source: file.source, date: new Date(file.mtimeMs).toISOString(), project: file.project, sessionId: path.basename(file.file), file: file.file, mtimeMs: file.mtimeMs, timestampMs: null, messages: [] };
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function messageTimestamp(session, timestamp) {
|
|
269
|
+
const iso = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:?\d{2})$/;
|
|
270
|
+
let parsed = NaN;
|
|
271
|
+
if (typeof timestamp === 'string' && iso.test(timestamp)) {
|
|
272
|
+
const calendar = new Date(`${timestamp.slice(0, 10)}T00:00:00Z`);
|
|
273
|
+
if (Number.isFinite(calendar.getTime()) && calendar.toISOString().slice(0, 10) === timestamp.slice(0, 10)) parsed = Date.parse(timestamp);
|
|
274
|
+
} else if (typeof timestamp === 'number' && timestamp >= 1e11) parsed = new Date(timestamp).getTime();
|
|
275
|
+
const time = Math.min(parsed, session.mtimeMs);
|
|
276
|
+
if (Number.isFinite(time) && (session.timestampMs === null || time > session.timestampMs)) {
|
|
277
|
+
session.timestampMs = time;
|
|
278
|
+
session.date = new Date(time).toISOString();
|
|
279
|
+
}
|
|
253
280
|
}
|
|
254
281
|
|
|
255
282
|
async function readClaude(file, state) {
|
|
@@ -261,6 +288,7 @@ async function readClaude(file, state) {
|
|
|
261
288
|
if (!record) return;
|
|
262
289
|
if (typeof record.cwd === 'string') sessionCwd = record.cwd;
|
|
263
290
|
if (typeof record.entrypoint === 'string' && record.entrypoint.startsWith('sdk')) automation = true;
|
|
291
|
+
if (record.type === 'user' || record.type === 'assistant') messageTimestamp(session, record.timestamp);
|
|
264
292
|
if (record.type !== 'user') return;
|
|
265
293
|
known = true;
|
|
266
294
|
if (!state.allProjects && !belongsToProject(sessionCwd, state.projectDir)) {
|
|
@@ -299,7 +327,9 @@ async function readCodex(file, state) {
|
|
|
299
327
|
if (typeof meta.cwd === 'string') session.project = path.basename(meta.cwd);
|
|
300
328
|
return;
|
|
301
329
|
}
|
|
302
|
-
if (record.type !== 'response_item' || !record.payload || record.payload.type !== 'message'
|
|
330
|
+
if (record.type !== 'response_item' || !record.payload || record.payload.type !== 'message') return;
|
|
331
|
+
messageTimestamp(session, record.timestamp);
|
|
332
|
+
if (record.payload.role !== 'user') return;
|
|
303
333
|
known = true;
|
|
304
334
|
if (!projectMatches) {
|
|
305
335
|
if (!state.allProjects) bump(state, 'project_unknown');
|
|
@@ -330,7 +360,9 @@ async function readGemini(file, state) {
|
|
|
330
360
|
});
|
|
331
361
|
if (!ok) return [];
|
|
332
362
|
for (const message of messages) {
|
|
333
|
-
if (!message
|
|
363
|
+
if (!message) continue;
|
|
364
|
+
messageTimestamp(session, message.timestamp);
|
|
365
|
+
if (message.type !== 'user') continue;
|
|
334
366
|
const items = Array.isArray(message.content) ? message.content.filter(item => item && typeof item.text === 'string' && (!item.type || item.type === 'text')).map(item => item.text) : [];
|
|
335
367
|
const text = typedText(items, state);
|
|
336
368
|
if (text) session.messages.push(text);
|
|
@@ -346,16 +378,19 @@ async function readGeminiLogs(file, state) {
|
|
|
346
378
|
function accept() {
|
|
347
379
|
if (oversize) { bump(state, 'oversize_line'); return; }
|
|
348
380
|
const record = parsedLine(entry, state);
|
|
349
|
-
if (!record ||
|
|
350
|
-
if (typeof record.sessionId !== 'string' || typeof record.message !== 'string') { bump(state, 'unknown_format'); return; }
|
|
351
|
-
const text = typedText([record.message], state);
|
|
352
|
-
if (!text) return;
|
|
381
|
+
if (!record || !['user', 'gemini', 'assistant'].includes(record.type)) return;
|
|
382
|
+
if (typeof record.sessionId !== 'string' || (record.type === 'user' && typeof record.message !== 'string')) { bump(state, 'unknown_format'); return; }
|
|
353
383
|
if (!grouped.has(record.sessionId)) {
|
|
354
384
|
const session = baseSession(file);
|
|
355
385
|
session.sessionId = record.sessionId;
|
|
356
386
|
grouped.set(record.sessionId, session);
|
|
357
387
|
}
|
|
358
|
-
grouped.get(record.sessionId)
|
|
388
|
+
const session = grouped.get(record.sessionId);
|
|
389
|
+
messageTimestamp(session, record.timestamp);
|
|
390
|
+
if (record.type === 'user') {
|
|
391
|
+
const text = typedText([record.message], state);
|
|
392
|
+
if (text) session.messages.push(text);
|
|
393
|
+
}
|
|
359
394
|
}
|
|
360
395
|
const ok = await scanLines(file.file, state, line => {
|
|
361
396
|
for (const character of `${line}\n`) {
|
|
@@ -393,7 +428,7 @@ async function readGeminiLogs(file, state) {
|
|
|
393
428
|
if (!ok) return [];
|
|
394
429
|
if (!started || invalid) { bump(state, 'unknown_format'); return []; }
|
|
395
430
|
if (!finished || depth) bump(state, 'malformed_line');
|
|
396
|
-
return [...grouped.values()];
|
|
431
|
+
return [...grouped.values()].filter(session => session.messages.length);
|
|
397
432
|
}
|
|
398
433
|
|
|
399
434
|
async function readMemory(file, state) {
|
|
@@ -401,16 +436,18 @@ async function readMemory(file, state) {
|
|
|
401
436
|
if (!await scanLines(file.file, state, line => lines.push(line))) return null;
|
|
402
437
|
const text = lines.join('\n').replace(CTRL, '').trim();
|
|
403
438
|
if (!text) return null;
|
|
439
|
+
if (secretMatch(text)) { bump(state, 'secret'); return null; }
|
|
404
440
|
if (privateMatch(text, state.privateTerms)) { bump(state, 'private_match'); return null; }
|
|
441
|
+
if (text.length > 2000) { bump(state, 'long_message'); return null; }
|
|
405
442
|
return { file: file.file, text, date: new Date(file.mtimeMs).toISOString() };
|
|
406
443
|
}
|
|
407
444
|
|
|
408
|
-
async function readSessions({ home =
|
|
445
|
+
async function readSessions({ home = resolveHome(), projectDir = process.cwd(), allProjects = false, sources = [], limit = 20, privateTerms = [] } = {}) {
|
|
409
446
|
const selectedSources = new Set(Array.isArray(sources) ? sources.filter(source => SOURCES.has(source)) : []);
|
|
410
447
|
const maximum = Number.isInteger(limit) && limit >= 0 ? Math.min(limit, 20) : 20;
|
|
411
448
|
projectDir = path.resolve(projectDir);
|
|
412
449
|
const state = {
|
|
413
|
-
home: path.resolve(home), projectDir, allProjects,
|
|
450
|
+
home: path.resolve(resolveHome(home)), projectDir, allProjects,
|
|
414
451
|
geminiProjectHash: crypto.createHash('sha256').update(projectDir).digest('hex'),
|
|
415
452
|
candidates: [], memoryFiles: [], filesRead: [], filesChecked: 0, skipped: {},
|
|
416
453
|
privateTerms: Array.isArray(privateTerms) ? privateTerms.filter(term => typeof term === 'string' && fold(term).trim()).map(fold) : []
|
|
@@ -418,22 +455,33 @@ async function readSessions({ home = os.homedir(), projectDir = process.cwd(), a
|
|
|
418
455
|
await discover(state.home, selectedSources, state);
|
|
419
456
|
state.candidates.sort((a, b) => b.mtimeMs - a.mtimeMs || a.file.localeCompare(b.file));
|
|
420
457
|
const sessions = [], geminiSessions = new Map();
|
|
458
|
+
// Multiple Gemini files can contribute to the same session, including its fallback timestamp.
|
|
459
|
+
// Keep that full merge; independent sessions admit an mtime bound.
|
|
460
|
+
const bounded = state.candidates.filter(file => file.source === 'gemini-cli').length < 2;
|
|
461
|
+
const sortKey = session => session.timestampMs ?? session.mtimeMs;
|
|
462
|
+
const compare = (a, b) => sortKey(b) - sortKey(a) || a.file.localeCompare(b.file) || a.sessionId.localeCompare(b.sessionId);
|
|
421
463
|
const readers = { claude: readClaude, codex: readCodex, gemini: readGemini, 'gemini-logs': readGeminiLogs };
|
|
422
464
|
for (const file of state.candidates) {
|
|
423
|
-
if (
|
|
465
|
+
if (!maximum) break;
|
|
466
|
+
if (bounded && sessions.length === maximum && file.mtimeMs < sortKey(sessions[maximum - 1])) break;
|
|
424
467
|
const found = await readers[file.format](file, state);
|
|
425
468
|
for (const session of found) {
|
|
426
469
|
if (session.source === 'gemini-cli' && geminiSessions.has(session.sessionId)) {
|
|
427
470
|
const existing = geminiSessions.get(session.sessionId);
|
|
471
|
+
if (session.timestampMs !== null) messageTimestamp(existing, session.timestampMs);
|
|
428
472
|
for (const text of session.messages) {
|
|
429
473
|
if (existing.messages.includes(text)) bump(state, 'duplicate_message');
|
|
430
474
|
else existing.messages.push(text);
|
|
431
475
|
}
|
|
432
|
-
} else
|
|
476
|
+
} else {
|
|
433
477
|
if (session.source === 'gemini-cli') geminiSessions.set(session.sessionId, session);
|
|
434
478
|
sessions.push(session);
|
|
435
479
|
}
|
|
436
480
|
}
|
|
481
|
+
sessions.sort(compare);
|
|
482
|
+
if (bounded) for (const session of sessions.splice(maximum)) {
|
|
483
|
+
if (session.source === 'gemini-cli') geminiSessions.delete(session.sessionId);
|
|
484
|
+
}
|
|
437
485
|
}
|
|
438
486
|
const memories = [];
|
|
439
487
|
state.memoryFiles.sort((a, b) => b.mtimeMs - a.mtimeMs || a.file.localeCompare(b.file));
|
|
@@ -442,7 +490,7 @@ async function readSessions({ home = os.homedir(), projectDir = process.cwd(), a
|
|
|
442
490
|
const memory = await readMemory(file, state);
|
|
443
491
|
if (memory) memories.push(memory);
|
|
444
492
|
}
|
|
445
|
-
return { sessions, memories, filesRead: state.filesRead, filesChecked: state.filesChecked, skipped: state.skipped };
|
|
493
|
+
return { sessions: sessions.slice(0, maximum), memories, filesRead: state.filesRead, filesChecked: state.filesChecked, skipped: state.skipped };
|
|
446
494
|
}
|
|
447
495
|
|
|
448
496
|
module.exports = { readSessions };
|