skilltally 0.0.0-stage → 1.0.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.
@@ -0,0 +1,78 @@
1
+ import { realpathSync } from 'node:fs';
2
+
3
+ function normalizePathKey(p) {
4
+ if (!p) return '';
5
+ let resolved = String(p);
6
+ try {
7
+ resolved = realpathSync(resolved);
8
+ } catch {}
9
+ return resolved.replace(/\\/g, '/');
10
+ }
11
+
12
+ export function canonicalizeEvent(ev, inventoryIndex = null, aliases = {}) {
13
+ let skill = String(ev.skill || '');
14
+ let source = ev.source || 'unknown';
15
+
16
+ if (inventoryIndex) {
17
+ let entry = null;
18
+ if (ev.skill_path) {
19
+ entry = inventoryIndex.byPath?.get(normalizePathKey(ev.skill_path)) ?? null;
20
+ if (!entry) {
21
+ entry = inventoryIndex.byPath?.get(String(ev.skill_path).replace(/\\/g, '/')) ?? null;
22
+ }
23
+ }
24
+ if (!entry && skill) {
25
+ entry = inventoryIndex.byDirName?.get(skill) ?? inventoryIndex.byName?.get(skill) ?? null;
26
+ }
27
+ if (entry) {
28
+ if (entry.name) skill = entry.name;
29
+ if ((!source || source === 'unknown') && entry.source) source = entry.source;
30
+ }
31
+ }
32
+
33
+ if (aliases && Object.prototype.hasOwnProperty.call(aliases, skill)) {
34
+ skill = String(aliases[skill]);
35
+ }
36
+
37
+ return {
38
+ ...ev,
39
+ skill,
40
+ source,
41
+ };
42
+ }
43
+
44
+ export function dedupeEvents(events, windowMs = 5000) {
45
+ // Prioritize real-time hook events over backfill events when deduplicating.
46
+ const ordered = [
47
+ ...events.filter((e) => e.origin === 'hook'),
48
+ ...events.filter((e) => e.origin !== 'hook'),
49
+ ];
50
+
51
+ const seenIds = new Set();
52
+ const seenWindows = new Map();
53
+ const out = [];
54
+
55
+ for (const ev of ordered) {
56
+ if (ev.id) {
57
+ if (seenIds.has(ev.id)) continue;
58
+ seenIds.add(ev.id);
59
+ }
60
+
61
+ if (ev.session_id && ev.ts) {
62
+ const tsMs = Date.parse(ev.ts);
63
+ if (Number.isFinite(tsMs)) {
64
+ const key = `${ev.host || 'unknown'}\0${ev.session_id}\0${ev.skill}`;
65
+ const prev = seenWindows.get(key);
66
+ if (prev && prev.some((p) => Math.abs(p - tsMs) <= windowMs)) {
67
+ continue;
68
+ }
69
+ if (prev) prev.push(tsMs);
70
+ else seenWindows.set(key, [tsMs]);
71
+ }
72
+ }
73
+
74
+ out.push(ev);
75
+ }
76
+
77
+ return out;
78
+ }
@@ -0,0 +1,99 @@
1
+ import {
2
+ existsSync,
3
+ mkdirSync,
4
+ readdirSync,
5
+ readFileSync,
6
+ renameSync,
7
+ statSync,
8
+ writeFileSync,
9
+ } from 'node:fs';
10
+ import { join } from 'node:path';
11
+
12
+ export const DEFAULT_ROTATE_BYTES = 20 * 1024 * 1024;
13
+
14
+ export function listEventFiles(homeDir) {
15
+ if (!existsSync(homeDir)) return [];
16
+ const entries = readdirSync(homeDir, { withFileTypes: true });
17
+ return entries
18
+ .filter(
19
+ (e) =>
20
+ e.isFile() &&
21
+ (e.name === 'events.jsonl' ||
22
+ e.name === 'events.backfill.jsonl' ||
23
+ /^events-\d{4}-\d{2}(?:-\d+)?\.jsonl$/.test(e.name))
24
+ )
25
+ .map((e) => join(homeDir, e.name))
26
+ .sort();
27
+ }
28
+
29
+ export function rotateEventsIfNeeded(
30
+ homeDir,
31
+ { maxBytes = DEFAULT_ROTATE_BYTES, now = new Date() } = {}
32
+ ) {
33
+ const eventsPath = join(homeDir, 'events.jsonl');
34
+ if (!existsSync(eventsPath)) {
35
+ return { rotated: false, archivePath: null };
36
+ }
37
+
38
+ let size = 0;
39
+ try {
40
+ size = statSync(eventsPath).size;
41
+ } catch {
42
+ return { rotated: false, archivePath: null };
43
+ }
44
+ if (size < maxBytes) {
45
+ return { rotated: false, archivePath: null };
46
+ }
47
+
48
+ const month = now.toISOString().slice(0, 7);
49
+ let archivePath = join(homeDir, `events-${month}.jsonl`);
50
+ let suffix = 1;
51
+ while (existsSync(archivePath)) {
52
+ archivePath = join(homeDir, `events-${month}-${suffix}.jsonl`);
53
+ suffix += 1;
54
+ }
55
+
56
+ renameSync(eventsPath, archivePath);
57
+ return { rotated: true, archivePath };
58
+ }
59
+
60
+ export function readAllEvents(homeDir) {
61
+ const files = listEventFiles(homeDir);
62
+ const events = [];
63
+ let corruptLines = 0;
64
+
65
+ for (const file of files) {
66
+ let raw;
67
+ try {
68
+ raw = readFileSync(file, 'utf8');
69
+ } catch {
70
+ continue;
71
+ }
72
+ for (const line of raw.split('\n')) {
73
+ const trimmed = line.trim();
74
+ if (!trimmed) continue;
75
+ try {
76
+ const obj = JSON.parse(trimmed);
77
+ if (obj && typeof obj === 'object' && obj.skill) {
78
+ events.push(obj);
79
+ } else {
80
+ corruptLines += 1;
81
+ }
82
+ } catch {
83
+ corruptLines += 1;
84
+ }
85
+ }
86
+ }
87
+
88
+ return { events, corruptLines, files };
89
+ }
90
+
91
+ export function writeBackfillEvents(homeDir, events) {
92
+ mkdirSync(homeDir, { recursive: true, mode: 0o700 });
93
+ const target = join(homeDir, 'events.backfill.jsonl');
94
+ const tmp = join(homeDir, `events.backfill.jsonl.tmp.${process.pid}`);
95
+ const body = events.length > 0 ? events.map((e) => JSON.stringify(e)).join('\n') + '\n' : '';
96
+ writeFileSync(tmp, body, { mode: 0o600 });
97
+ renameSync(tmp, target);
98
+ return target;
99
+ }
@@ -0,0 +1,135 @@
1
+ import {
2
+ chmodSync,
3
+ cpSync,
4
+ existsSync,
5
+ lstatSync,
6
+ mkdirSync,
7
+ readFileSync,
8
+ realpathSync,
9
+ rmSync,
10
+ rmdirSync,
11
+ unlinkSync,
12
+ writeFileSync,
13
+ } from 'node:fs';
14
+ import { homedir } from 'node:os';
15
+ import { join } from 'node:path';
16
+ import { getPackageRoot, writeJsonAtomic } from './runtime.mjs';
17
+
18
+ export function detectAntigravity(homeDir = homedir()) {
19
+ return (
20
+ existsSync(join(homeDir, '.gemini', 'config')) ||
21
+ existsSync(join(homeDir, '.gemini', 'antigravity'))
22
+ );
23
+ }
24
+
25
+ export function getAntigravityPluginTarget(homeDir = homedir()) {
26
+ return join(homeDir, '.gemini', 'config', 'plugins', 'skilltally');
27
+ }
28
+
29
+ function buildAntigravityHooksJson() {
30
+ const cmd =
31
+ process.platform === 'win32'
32
+ ? 'node "./bin/track.mjs" --host antigravity'
33
+ : './hooks/agy-fast-track.sh';
34
+ return {
35
+ skilltally: {
36
+ PostToolUse: [
37
+ {
38
+ matcher: 'read_file|view_file',
39
+ hooks: [
40
+ {
41
+ type: 'command',
42
+ command: cmd,
43
+ timeout: 5,
44
+ },
45
+ ],
46
+ },
47
+ ],
48
+ },
49
+ };
50
+ }
51
+
52
+ export function installAntigravity({
53
+ homeDir = homedir(),
54
+ pkgRoot = getPackageRoot(),
55
+ dryRun = false,
56
+ } = {}) {
57
+ const target = getAntigravityPluginTarget(homeDir);
58
+
59
+ let stat = null;
60
+ try {
61
+ stat = lstatSync(target);
62
+ } catch {}
63
+
64
+ if (stat?.isSymbolicLink()) {
65
+ try {
66
+ if (realpathSync(target) === realpathSync(pkgRoot)) {
67
+ return { host: 'antigravity', status: 'unchanged', mode: 'symlink', target };
68
+ }
69
+ } catch {}
70
+ }
71
+
72
+ const existed = Boolean(stat);
73
+ if (!dryRun) {
74
+ mkdirSync(target, { recursive: true });
75
+ for (const rel of ['package.json', 'plugin.json', 'hooks', 'bin', 'src', 'skills']) {
76
+ const srcPath = join(pkgRoot, rel);
77
+ if (existsSync(srcPath)) {
78
+ cpSync(srcPath, join(target, rel), { recursive: true, force: true });
79
+ }
80
+ }
81
+ for (const exe of [
82
+ join(target, 'hooks', 'agy-fast-track.sh'),
83
+ join(target, 'bin', 'track.mjs'),
84
+ join(target, 'bin', 'cli.mjs'),
85
+ ]) {
86
+ if (existsSync(exe)) chmodSync(exe, 0o755);
87
+ }
88
+
89
+ writeJsonAtomic(join(target, 'hooks.json'), buildAntigravityHooksJson(), 0o644);
90
+
91
+ let version = '1.0.0';
92
+ try {
93
+ version = JSON.parse(readFileSync(join(pkgRoot, 'package.json'), 'utf8')).version || version;
94
+ } catch {}
95
+ writeFileSync(
96
+ join(target, '.skilltally-managed'),
97
+ JSON.stringify({ installedAt: new Date().toISOString(), version }, null, 2) + '\n',
98
+ { mode: 0o644 }
99
+ );
100
+ }
101
+
102
+ return {
103
+ host: 'antigravity',
104
+ status: existed ? 'updated' : 'installed',
105
+ mode: 'plugin',
106
+ target,
107
+ };
108
+ }
109
+
110
+ export function uninstallAntigravity({ homeDir = homedir(), dryRun = false } = {}) {
111
+ const target = getAntigravityPluginTarget(homeDir);
112
+
113
+ let stat = null;
114
+ try {
115
+ stat = lstatSync(target);
116
+ } catch {
117
+ return { host: 'antigravity', status: 'not_installed', target };
118
+ }
119
+
120
+ if (stat.isSymbolicLink()) {
121
+ if (!dryRun) {
122
+ try {
123
+ unlinkSync(target);
124
+ } catch {
125
+ rmdirSync(target);
126
+ }
127
+ }
128
+ return { host: 'antigravity', status: 'uninstalled', mode: 'symlink', target };
129
+ }
130
+
131
+ if (!dryRun) {
132
+ rmSync(target, { recursive: true, force: true });
133
+ }
134
+ return { host: 'antigravity', status: 'uninstalled', mode: 'plugin', target };
135
+ }
@@ -0,0 +1,116 @@
1
+ import { existsSync, readdirSync, readFileSync } from 'node:fs';
2
+ import { homedir } from 'node:os';
3
+ import { join } from 'node:path';
4
+ import { getHomeDir } from '../core/config.mjs';
5
+ import {
6
+ backupFile,
7
+ getPackageRoot,
8
+ mergePostToolUseHook,
9
+ readJsonObject,
10
+ removeSkilltallyHooks,
11
+ syncRuntime,
12
+ writeJsonAtomic,
13
+ } from './runtime.mjs';
14
+
15
+ export function detectClaude(homeDir = homedir()) {
16
+ return existsSync(join(homeDir, '.claude'));
17
+ }
18
+
19
+ export function hasClaudePluginInstalled(homeDir = homedir()) {
20
+ const pluginsDir = join(homeDir, '.claude', 'plugins');
21
+ if (existsSync(join(pluginsDir, 'skilltally'))) return true;
22
+
23
+ const cacheDir = join(pluginsDir, 'cache');
24
+ if (existsSync(cacheDir)) {
25
+ try {
26
+ for (const mkt of readdirSync(cacheDir, { withFileTypes: true })) {
27
+ if (!mkt.isDirectory() && !mkt.isSymbolicLink()) continue;
28
+ if (existsSync(join(cacheDir, mkt.name, 'skilltally'))) return true;
29
+ }
30
+ } catch {}
31
+ }
32
+
33
+ const installedFile = join(pluginsDir, 'installed_plugins.json');
34
+ if (existsSync(installedFile)) {
35
+ try {
36
+ const data = JSON.parse(readFileSync(installedFile, 'utf8'));
37
+ const keys = Object.keys(data?.plugins || data || {});
38
+ if (keys.some((k) => k === 'skilltally' || k.startsWith('skilltally@'))) return true;
39
+ } catch {}
40
+ }
41
+
42
+ return false;
43
+ }
44
+
45
+ export function installClaude({
46
+ homeDir = homedir(),
47
+ dataHome = getHomeDir(),
48
+ pkgRoot = getPackageRoot(),
49
+ dryRun = false,
50
+ force = false,
51
+ } = {}) {
52
+ if (hasClaudePluginInstalled(homeDir) && !force) {
53
+ return {
54
+ host: 'claude',
55
+ status: 'skipped',
56
+ reason: 'Claude plugin already installed (use --force to also add settings hook)',
57
+ };
58
+ }
59
+
60
+ const runtime = syncRuntime({ pkgRoot, dataHome, dryRun });
61
+ const command = `node "${runtime.target.replace(/\\/g, '/')}" --host claude`;
62
+ const settingsPath = join(homeDir, '.claude', 'settings.json');
63
+ const doc = readJsonObject(settingsPath);
64
+ const { nextDoc, changed, action } = mergePostToolUseHook(doc, {
65
+ matcher: 'Skill|Read',
66
+ command,
67
+ async: true,
68
+ timeout: 5,
69
+ statusMessage: 'skilltally',
70
+ });
71
+
72
+ let backup = null;
73
+ if (changed) {
74
+ backup = backupFile(settingsPath, dataHome, 'claude-settings', dryRun);
75
+ if (!dryRun) {
76
+ writeJsonAtomic(settingsPath, nextDoc, 0o600);
77
+ }
78
+ }
79
+
80
+ return {
81
+ host: 'claude',
82
+ status: changed ? (action === 'added' ? 'installed' : 'updated') : 'unchanged',
83
+ target: settingsPath,
84
+ backup,
85
+ };
86
+ }
87
+
88
+ export function uninstallClaude({
89
+ homeDir = homedir(),
90
+ dataHome = getHomeDir(),
91
+ dryRun = false,
92
+ } = {}) {
93
+ const settingsPath = join(homeDir, '.claude', 'settings.json');
94
+ if (!existsSync(settingsPath)) {
95
+ return { host: 'claude', status: 'not_installed', target: settingsPath };
96
+ }
97
+
98
+ const doc = readJsonObject(settingsPath);
99
+ const { nextDoc, changed, removedCount } = removeSkilltallyHooks(doc);
100
+ if (!changed) {
101
+ return { host: 'claude', status: 'not_installed', target: settingsPath };
102
+ }
103
+
104
+ const backup = backupFile(settingsPath, dataHome, 'claude-settings', dryRun);
105
+ if (!dryRun) {
106
+ writeJsonAtomic(settingsPath, nextDoc, 0o600);
107
+ }
108
+
109
+ return {
110
+ host: 'claude',
111
+ status: 'uninstalled',
112
+ removedCount,
113
+ target: settingsPath,
114
+ backup,
115
+ };
116
+ }
@@ -0,0 +1,171 @@
1
+ import { existsSync, readdirSync, readFileSync } from 'node:fs';
2
+ import { homedir } from 'node:os';
3
+ import { join } from 'node:path';
4
+ import { getHomeDir } from '../core/config.mjs';
5
+ import {
6
+ backupFile,
7
+ getPackageRoot,
8
+ isSkilltallyHandler,
9
+ mergePostToolUseHook,
10
+ readJsonObject,
11
+ removeSkilltallyHooks,
12
+ syncRuntime,
13
+ writeJsonAtomic,
14
+ } from './runtime.mjs';
15
+
16
+ export function detectCodex(homeDir = homedir()) {
17
+ return existsSync(join(homeDir, '.codex'));
18
+ }
19
+
20
+ function readCodexToml(homeDir) {
21
+ const tomlPath = join(homeDir, '.codex', 'config.toml');
22
+ if (!existsSync(tomlPath)) return '';
23
+ try {
24
+ return readFileSync(tomlPath, 'utf8');
25
+ } catch {
26
+ return '';
27
+ }
28
+ }
29
+
30
+ export function hasCodexPluginInstalled(homeDir = homedir()) {
31
+ const cacheDir = join(homeDir, '.codex', 'plugins', 'cache');
32
+ if (existsSync(cacheDir)) {
33
+ try {
34
+ for (const mkt of readdirSync(cacheDir, { withFileTypes: true })) {
35
+ if (!mkt.isDirectory() && !mkt.isSymbolicLink()) continue;
36
+ if (existsSync(join(cacheDir, mkt.name, 'skilltally'))) return true;
37
+ }
38
+ } catch {}
39
+ }
40
+
41
+ const raw = readCodexToml(homeDir);
42
+ if (raw) {
43
+ let inSkilltally = false;
44
+ for (const line of raw.split(/\r?\n/)) {
45
+ const trimmed = line.replace(/#.*$/, '').trim();
46
+ if (!trimmed) continue;
47
+ if (trimmed.startsWith('[')) {
48
+ inSkilltally = /^\[plugins\.(?:"skilltally@[^"]+"|'skilltally@[^']+'|skilltally)\]$/.test(trimmed);
49
+ continue;
50
+ }
51
+ if (inSkilltally && /^enabled\s*=\s*true\b/.test(trimmed)) return true;
52
+ }
53
+ }
54
+
55
+ return false;
56
+ }
57
+
58
+ export function checkCodexHookTrust(homeDir = homedir()) {
59
+ const raw = readCodexToml(homeDir);
60
+ const hooksPath = join(homeDir, '.codex', 'hooks.json');
61
+ let hasUserHook = false;
62
+ try {
63
+ const doc = readJsonObject(hooksPath);
64
+ for (const g of doc?.hooks?.PostToolUse || []) {
65
+ if (Array.isArray(g?.hooks) && g.hooks.some(isSkilltallyHandler)) {
66
+ hasUserHook = true;
67
+ break;
68
+ }
69
+ }
70
+ } catch {}
71
+
72
+ if (!raw) return hasUserHook ? 'untrusted' : 'unknown';
73
+
74
+ let inRelevantState = false;
75
+ let sawSection = false;
76
+ let sawTrustedHash = false;
77
+
78
+ for (const line of raw.split(/\r?\n/)) {
79
+ const trimmed = line.replace(/#.*$/, '').trim();
80
+ if (!trimmed) continue;
81
+ if (trimmed.startsWith('[')) {
82
+ if (/^\[hooks\.state\./.test(trimmed) && (trimmed.includes('skilltally') || trimmed.includes('hooks.json:post_tool_use'))) {
83
+ inRelevantState = true;
84
+ sawSection = true;
85
+ } else {
86
+ inRelevantState = false;
87
+ }
88
+ continue;
89
+ }
90
+ if (inRelevantState && /^trusted_hash\s*=/.test(trimmed)) {
91
+ sawTrustedHash = true;
92
+ }
93
+ }
94
+
95
+ if (sawSection) return sawTrustedHash ? 'trusted' : 'untrusted';
96
+ return hasUserHook ? 'untrusted' : 'unknown';
97
+ }
98
+
99
+ export function installCodex({
100
+ homeDir = homedir(),
101
+ dataHome = getHomeDir(),
102
+ pkgRoot = getPackageRoot(),
103
+ dryRun = false,
104
+ force = false,
105
+ } = {}) {
106
+ if (hasCodexPluginInstalled(homeDir) && !force) {
107
+ return {
108
+ host: 'codex',
109
+ status: 'skipped',
110
+ reason: 'Codex plugin already installed (use --force to also add hooks.json entry)',
111
+ };
112
+ }
113
+
114
+ const runtime = syncRuntime({ pkgRoot, dataHome, dryRun });
115
+ const command = `node "${runtime.target.replace(/\\/g, '/')}" --host codex`;
116
+ const hooksPath = join(homeDir, '.codex', 'hooks.json');
117
+ const doc = readJsonObject(hooksPath);
118
+ const { nextDoc, changed, action } = mergePostToolUseHook(doc, {
119
+ matcher: 'exec|exec_command|shell|shell_command|local_shell|Bash',
120
+ command,
121
+ async: true,
122
+ timeout: 5,
123
+ statusMessage: 'skilltally',
124
+ });
125
+
126
+ let backup = null;
127
+ if (changed) {
128
+ backup = backupFile(hooksPath, dataHome, 'codex-hooks', dryRun);
129
+ if (!dryRun) {
130
+ writeJsonAtomic(hooksPath, nextDoc, 0o600);
131
+ }
132
+ }
133
+
134
+ return {
135
+ host: 'codex',
136
+ status: changed ? (action === 'added' ? 'installed' : 'updated') : 'unchanged',
137
+ target: hooksPath,
138
+ backup,
139
+ note: 'Run codex and open /hooks to review and trust the skilltally PostToolUse hook.',
140
+ };
141
+ }
142
+
143
+ export function uninstallCodex({
144
+ homeDir = homedir(),
145
+ dataHome = getHomeDir(),
146
+ dryRun = false,
147
+ } = {}) {
148
+ const hooksPath = join(homeDir, '.codex', 'hooks.json');
149
+ if (!existsSync(hooksPath)) {
150
+ return { host: 'codex', status: 'not_installed', target: hooksPath };
151
+ }
152
+
153
+ const doc = readJsonObject(hooksPath);
154
+ const { nextDoc, changed, removedCount } = removeSkilltallyHooks(doc);
155
+ if (!changed) {
156
+ return { host: 'codex', status: 'not_installed', target: hooksPath };
157
+ }
158
+
159
+ const backup = backupFile(hooksPath, dataHome, 'codex-hooks', dryRun);
160
+ if (!dryRun) {
161
+ writeJsonAtomic(hooksPath, nextDoc, 0o600);
162
+ }
163
+
164
+ return {
165
+ host: 'codex',
166
+ status: 'uninstalled',
167
+ removedCount,
168
+ target: hooksPath,
169
+ backup,
170
+ };
171
+ }