noonsight 0.0.0-stage → 0.1.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.
Files changed (115) hide show
  1. package/.claude-plugin/marketplace.json +19 -0
  2. package/.claude-plugin/plugin.json +14 -0
  3. package/LICENSE +21 -0
  4. package/README.md +281 -2
  5. package/dist/src/adapters/claude-code/classifier.js +138 -0
  6. package/dist/src/adapters/claude-code/code-edit.js +108 -0
  7. package/dist/src/adapters/claude-code/compat.js +31 -0
  8. package/dist/src/adapters/claude-code/definitions.js +347 -0
  9. package/dist/src/adapters/claude-code/fork.js +132 -0
  10. package/dist/src/adapters/claude-code/hook.js +652 -0
  11. package/dist/src/adapters/claude-code/install.js +186 -0
  12. package/dist/src/adapters/claude-code/launch.js +270 -0
  13. package/dist/src/adapters/claude-code/launcher.js +252 -0
  14. package/dist/src/adapters/claude-code/locations.js +115 -0
  15. package/dist/src/adapters/claude-code/managed.js +75 -0
  16. package/dist/src/adapters/claude-code/plugin-files.js +88 -0
  17. package/dist/src/adapters/claude-code/plugins.js +117 -0
  18. package/dist/src/adapters/claude-code/pointer.js +48 -0
  19. package/dist/src/adapters/claude-code/predict.js +271 -0
  20. package/dist/src/adapters/claude-code/routing.js +88 -0
  21. package/dist/src/adapters/claude-code/settings.js +189 -0
  22. package/dist/src/adapters/claude-code/subagents.js +666 -0
  23. package/dist/src/adapters/claude-code/triggers.js +88 -0
  24. package/dist/src/cli.js +157 -0
  25. package/dist/src/commands/accept.js +115 -0
  26. package/dist/src/commands/check.js +529 -0
  27. package/dist/src/commands/hook.js +73 -0
  28. package/dist/src/commands/init.js +29 -0
  29. package/dist/src/commands/install.js +370 -0
  30. package/dist/src/commands/models.js +213 -0
  31. package/dist/src/commands/receives.js +84 -0
  32. package/dist/src/commands/report.js +386 -0
  33. package/dist/src/commands/resolve.js +32 -0
  34. package/dist/src/commands/run.js +168 -0
  35. package/dist/src/commands/trust.js +70 -0
  36. package/dist/src/commands/usage.js +10 -0
  37. package/dist/src/commands/watch.js +154 -0
  38. package/dist/src/config/create.js +51 -0
  39. package/dist/src/config/json-schema.js +207 -0
  40. package/dist/src/config/load.js +758 -0
  41. package/dist/src/config/paths.js +42 -0
  42. package/dist/src/config/preset-changes.js +190 -0
  43. package/dist/src/config/project.js +354 -0
  44. package/dist/src/config/rewrite.js +112 -0
  45. package/dist/src/core/backtracking.js +330 -0
  46. package/dist/src/core/classify.js +66 -0
  47. package/dist/src/core/normalize.js +24 -0
  48. package/dist/src/core/overlap.js +39 -0
  49. package/dist/src/core/regex-sample.js +278 -0
  50. package/dist/src/core/resolve.js +201 -0
  51. package/dist/src/core/route.js +93 -0
  52. package/dist/src/core/scope.js +77 -0
  53. package/dist/src/core/triggers.js +30 -0
  54. package/dist/src/core/types.js +74 -0
  55. package/dist/src/state/agents.js +200 -0
  56. package/dist/src/state/claims.js +80 -0
  57. package/dist/src/state/files.js +84 -0
  58. package/dist/src/state/installs.js +29 -0
  59. package/dist/src/state/log.js +90 -0
  60. package/dist/src/state/node.js +35 -0
  61. package/dist/src/state/sessions.js +109 -0
  62. package/dist/src/state/triggers.js +57 -0
  63. package/dist/src/version.js +24 -0
  64. package/dist/src/watch/service.js +172 -0
  65. package/dist/src/watch/watcher.js +130 -0
  66. package/dist/src/wording.js +11 -0
  67. package/hooks/hooks.json +118 -0
  68. package/package.json +60 -4
  69. package/presets/claude/SOURCES.md +485 -0
  70. package/presets/claude/preset.json +432 -0
  71. package/presets/claude/rules/fable-5-2/code-ownership.md +1 -0
  72. package/presets/claude/rules/fable-5-2/finish-work.md +1 -0
  73. package/presets/claude/rules/fable-5-2/low-context-report.md +1 -0
  74. package/presets/claude/rules/fable-5-2/progress-updates.md +1 -0
  75. package/presets/claude/rules/haiku-5-5/carry-through.md +1 -0
  76. package/presets/claude/rules/haiku-5-5/search-recent-facts.md +1 -0
  77. package/presets/claude/rules/haiku-5-5/verify-changes.md +1 -0
  78. package/presets/claude/rules/opus-5-5/explore-before-acting.md +1 -0
  79. package/presets/claude/rules/opus-5-5/progress-updates.md +1 -0
  80. package/presets/claude/rules/opus-5-5/unattended-run.md +1 -0
  81. package/presets/claude/rules/sonnet-5-5/carry-through.md +1 -0
  82. package/presets/claude/rules/sonnet-5-5/check-current-facts.md +1 -0
  83. package/presets/claude/rules/sonnet-5-5/ideas-first.md +1 -0
  84. package/presets/claude/rules/sonnet-5-5/no-self-review-rounds.md +1 -0
  85. package/presets/claude/rules/sonnet-5-5/stay-in-scope.md +1 -0
  86. package/presets/claude/rules/sonnet-5-5/verify-changes.md +1 -0
  87. package/presets/claude/versions/1.3.0/preset.json +432 -0
  88. package/presets/claude/versions/1.3.0/rules/fable-5-2/code-ownership.md +1 -0
  89. package/presets/claude/versions/1.3.0/rules/fable-5-2/finish-work.md +1 -0
  90. package/presets/claude/versions/1.3.0/rules/fable-5-2/low-context-report.md +1 -0
  91. package/presets/claude/versions/1.3.0/rules/fable-5-2/progress-updates.md +1 -0
  92. package/presets/claude/versions/1.3.0/rules/haiku-5-5/carry-through.md +1 -0
  93. package/presets/claude/versions/1.3.0/rules/haiku-5-5/search-recent-facts.md +1 -0
  94. package/presets/claude/versions/1.3.0/rules/haiku-5-5/verify-changes.md +1 -0
  95. package/presets/claude/versions/1.3.0/rules/opus-5-5/explore-before-acting.md +1 -0
  96. package/presets/claude/versions/1.3.0/rules/opus-5-5/progress-updates.md +1 -0
  97. package/presets/claude/versions/1.3.0/rules/opus-5-5/unattended-run.md +1 -0
  98. package/presets/claude/versions/1.3.0/rules/sonnet-5-5/carry-through.md +1 -0
  99. package/presets/claude/versions/1.3.0/rules/sonnet-5-5/check-current-facts.md +1 -0
  100. package/presets/claude/versions/1.3.0/rules/sonnet-5-5/ideas-first.md +1 -0
  101. package/presets/claude/versions/1.3.0/rules/sonnet-5-5/no-self-review-rounds.md +1 -0
  102. package/presets/claude/versions/1.3.0/rules/sonnet-5-5/stay-in-scope.md +1 -0
  103. package/presets/claude/versions/1.3.0/rules/sonnet-5-5/verify-changes.md +1 -0
  104. package/schema/noonsight-project.schema.json +394 -0
  105. package/schema/noonsight.schema.json +425 -0
  106. package/schema/preset.schema.json +194 -0
  107. package/scripts/noonsight +68 -0
  108. package/skills/accept/SKILL.md +11 -0
  109. package/skills/check/SKILL.md +11 -0
  110. package/skills/models/SKILL.md +11 -0
  111. package/skills/report/SKILL.md +11 -0
  112. package/skills/resolve/SKILL.md +12 -0
  113. package/skills/setup/SKILL.md +15 -0
  114. package/skills/trust/SKILL.md +12 -0
  115. package/skills/untrust/SKILL.md +12 -0
@@ -0,0 +1,109 @@
1
+ // What each session last received, so a hook can tell a resumed conversation that already holds the
2
+ // rules from one that needs them, and can say "the earlier rules no longer apply" after a switch.
3
+ //
4
+ // Two keys, both structural facts rather than inherited environment variables:
5
+ // - the session id Claude Code puts in every hook input;
6
+ // - the Claude Code process that runs the hook (the hook's parent process). After /clear the new
7
+ // session has a new id and Claude Code may leave the model out, but it is the same process,
8
+ // still on the same model.
9
+ import fs from 'node:fs';
10
+ import path from 'node:path';
11
+ import { writeFileAtomic } from './files.js';
12
+ /** Records older than this are removed; any write refreshes a record. */
13
+ const MAX_AGE_MS = 30 * 24 * 60 * 60 * 1000;
14
+ const SAFE_ID = /^[A-Za-z0-9_-]{1,128}$/;
15
+ export function sessionFile(stateDir, sessionId) {
16
+ return SAFE_ID.test(sessionId) ? path.join(stateDir, 'sessions', `${sessionId}.json`) : null;
17
+ }
18
+ export function processFile(stateDir, pid) {
19
+ return Number.isSafeInteger(pid) && pid > 1 ? path.join(stateDir, 'processes', `${pid}.json`) : null;
20
+ }
21
+ export function readState(file) {
22
+ if (file === null)
23
+ return null;
24
+ try {
25
+ const value = JSON.parse(fs.readFileSync(file, 'utf8'));
26
+ if ((value.model === null || typeof value.model === 'string') &&
27
+ (value.status === 'matched' || value.status === 'none' || value.status === 'undeclared') &&
28
+ (value.sha256 === null || typeof value.sha256 === 'string'))
29
+ return {
30
+ model: value.model,
31
+ status: value.status,
32
+ sha256: value.sha256,
33
+ held: Array.isArray(value.held)
34
+ ? value.held.filter((m) => typeof m === 'string' && m !== '')
35
+ : value.status === 'matched' && value.model
36
+ ? [value.model]
37
+ : [],
38
+ // A record written before resumes were told apart holds true for a switch's delivery.
39
+ ...(value.unconfirmed === true || value.unconfirmed === 'switch'
40
+ ? { unconfirmed: 'switch' }
41
+ : value.unconfirmed === 'resume'
42
+ ? { unconfirmed: 'resume' }
43
+ : {}),
44
+ ...(typeof value.chosen === 'string' && value.chosen !== '' ? { chosen: value.chosen } : {}),
45
+ at: String(value.at),
46
+ };
47
+ }
48
+ catch {
49
+ // Missing or unreadable: the same as never recorded.
50
+ }
51
+ return null;
52
+ }
53
+ /** The models whose rules a conversation may hold, as a record keeps them. */
54
+ export function heldModels(state) {
55
+ if (state === null)
56
+ return [];
57
+ return [...(state.held ?? (state.status === 'matched' && state.model ? [state.model] : []))];
58
+ }
59
+ export function writeState(file, state) {
60
+ if (file === null)
61
+ return;
62
+ fs.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 });
63
+ writeFileAtomic(file, `${JSON.stringify(state)}\n`, 0o600);
64
+ }
65
+ /**
66
+ * Records of subagents only serve while their session runs, and a session makes many, so they are
67
+ * kept for less time. A call whose result never came (another hook denied it) leaves its record here.
68
+ */
69
+ const AGENT_MAX_AGE_MS = 7 * 24 * 60 * 60 * 1000;
70
+ /** A claim (claims.ts) serves only while the copies of one event run. */
71
+ const CLAIM_MAX_AGE_MS = 24 * 60 * 60 * 1000;
72
+ /** Remove records nobody has written for a long time. */
73
+ export function pruneState(stateDir, now) {
74
+ for (const [sub, maxAge] of [
75
+ ['sessions', MAX_AGE_MS],
76
+ ['processes', MAX_AGE_MS],
77
+ // The triggers each session revealed (triggers.ts), kept as long as the session's own record.
78
+ ['triggers', MAX_AGE_MS],
79
+ // The system prompt files of `noonsight run`, which every launch writes again.
80
+ ['launch', MAX_AGE_MS],
81
+ ['agent-calls', AGENT_MAX_AGE_MS],
82
+ ['agent-pools', AGENT_MAX_AGE_MS],
83
+ ['agent-starts', AGENT_MAX_AGE_MS],
84
+ ['agent-links', AGENT_MAX_AGE_MS],
85
+ ['agent-pending', AGENT_MAX_AGE_MS],
86
+ ['agents', AGENT_MAX_AGE_MS],
87
+ ['claims', CLAIM_MAX_AGE_MS],
88
+ ]) {
89
+ const dir = path.join(stateDir, sub);
90
+ let names;
91
+ try {
92
+ names = fs.readdirSync(dir);
93
+ }
94
+ catch {
95
+ continue;
96
+ }
97
+ for (const name of names) {
98
+ const file = path.join(dir, name);
99
+ try {
100
+ if (now - fs.statSync(file).mtimeMs > maxAge)
101
+ fs.rmSync(file, { force: true });
102
+ }
103
+ catch {
104
+ // Removed by another session meanwhile.
105
+ }
106
+ }
107
+ }
108
+ }
109
+ //# sourceMappingURL=sessions.js.map
@@ -0,0 +1,57 @@
1
+ // The triggers each session has revealed: one file per session and trigger, under triggers/, named
2
+ // <session id>.<trigger>.json and holding what revealed it. A file is created once and never replaced,
3
+ // so a session's triggers only grow, and the first hook to create it is the one that delivers the
4
+ // rules: several edits of one response run their hooks at the same time, and exactly one of them wins.
5
+ import fs from 'node:fs';
6
+ import path from 'node:path';
7
+ import { TRIGGERS } from '../core/types.js';
8
+ const SAFE_ID = /^[A-Za-z0-9_-]{1,128}$/;
9
+ export function triggerFile(stateDir, sessionId, trigger) {
10
+ return SAFE_ID.test(sessionId) ? path.join(stateDir, 'triggers', `${sessionId}.${trigger}.json`) : null;
11
+ }
12
+ /** The triggers a session has revealed, in the order of TRIGGERS. */
13
+ export function sessionTriggers(stateDir, sessionId) {
14
+ if (sessionId === null)
15
+ return [];
16
+ return TRIGGERS.filter((t) => {
17
+ const file = triggerFile(stateDir, sessionId, t);
18
+ return file !== null && fs.existsSync(file);
19
+ });
20
+ }
21
+ /**
22
+ * Record that a session revealed a trigger. Returns false when the session had already revealed it,
23
+ * so another hook delivered its rules. Any other failure throws.
24
+ */
25
+ export function claimTrigger(stateDir, sessionId, record) {
26
+ const file = triggerFile(stateDir, sessionId, record.trigger);
27
+ if (file === null)
28
+ return false;
29
+ fs.mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 });
30
+ try {
31
+ fs.writeFileSync(file, `${JSON.stringify(record)}\n`, { flag: 'wx', mode: 0o600 });
32
+ return true;
33
+ }
34
+ catch (e) {
35
+ if (e.code === 'EEXIST')
36
+ return false;
37
+ throw e;
38
+ }
39
+ }
40
+ /**
41
+ * Give a session the triggers another revealed, as when a fork starts with the conversation that
42
+ * revealed them. The records are copied as they are; a trigger the session already has stays.
43
+ */
44
+ export function inheritTriggers(stateDir, from, to) {
45
+ for (const trigger of sessionTriggers(stateDir, from)) {
46
+ const source = triggerFile(stateDir, from, trigger);
47
+ if (source === null)
48
+ continue;
49
+ try {
50
+ claimTrigger(stateDir, to, JSON.parse(fs.readFileSync(source, 'utf8')));
51
+ }
52
+ catch {
53
+ // Without the record, the fork reveals the trigger again on its next edit of tracked code.
54
+ }
55
+ }
56
+ }
57
+ //# sourceMappingURL=triggers.js.map
@@ -0,0 +1,24 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { PACKAGE_ROOT } from './config/paths.js';
4
+ /** The installed package version, as package.json states it. */
5
+ export function packageVersion() {
6
+ const pkg = JSON.parse(fs.readFileSync(path.join(PACKAGE_ROOT, 'package.json'), 'utf8'));
7
+ return pkg.version;
8
+ }
9
+ /** Compare two number.number.number versions; null when either cannot be read. */
10
+ export function compareVersions(a, b) {
11
+ const parse = (v) => {
12
+ const m = /^(\d+)\.(\d+)\.(\d+)$/.exec(v);
13
+ return m ? [Number(m[1]), Number(m[2]), Number(m[3])] : null;
14
+ };
15
+ const x = parse(a);
16
+ const y = parse(b);
17
+ if (!x || !y)
18
+ return null;
19
+ for (let i = 0; i < 3; i++)
20
+ if (x[i] !== y[i])
21
+ return (x[i] ?? 0) - (y[i] ?? 0);
22
+ return 0;
23
+ }
24
+ //# sourceMappingURL=version.js.map
@@ -0,0 +1,172 @@
1
+ // The per-user service that keeps the watcher running: a launchd agent on macOS and a systemd user
2
+ // service on Linux. Both start the watcher at login and restart it if it ends. The service file is
3
+ // the only file `noonsight watch enable` writes, and the only one `noonsight watch disable` removes.
4
+ import { spawnSync } from 'node:child_process';
5
+ import fs from 'node:fs';
6
+ import path from 'node:path';
7
+ import { stateDir } from '../config/paths.js';
8
+ import { packageVersion } from '../version.js';
9
+ /** The launchd label and the systemd unit name. */
10
+ export const SERVICE_LABEL = 'io.github.bpsanti.noonsight.watch';
11
+ export const SERVICE_UNIT = 'noonsight-watch.service';
12
+ /** The file the watcher appends to, through the service's standard output and error. */
13
+ export const WATCH_LOG = 'watch.log';
14
+ /** The line every service file carries, so disable never removes a file noonsight did not write. */
15
+ const MARKER = 'Written by `noonsight watch enable`; `noonsight watch disable` removes it.';
16
+ export class WatchUnsupported extends Error {
17
+ constructor(platform) {
18
+ super(`the watcher runs as a launchd agent on macOS or a systemd user service on Linux; ${platform} is not supported`);
19
+ this.name = 'WatchUnsupported';
20
+ }
21
+ }
22
+ export function watchService(env, home, platform = process.platform) {
23
+ const log = path.join(stateDir(home), WATCH_LOG);
24
+ if (platform === 'darwin')
25
+ return { platform, log, file: path.join(home, 'Library', 'LaunchAgents', `${SERVICE_LABEL}.plist`) };
26
+ if (platform === 'linux') {
27
+ // systemd reads user units from $XDG_CONFIG_HOME/systemd/user, which defaults to ~/.config.
28
+ const config = env.XDG_CONFIG_HOME && path.isAbsolute(env.XDG_CONFIG_HOME) ? env.XDG_CONFIG_HOME : path.join(home, '.config');
29
+ return { platform, log, file: path.join(config, 'systemd', 'user', SERVICE_UNIT) };
30
+ }
31
+ throw new WatchUnsupported(platform);
32
+ }
33
+ /** The service file's content: it runs `noonsight watch run` with this Node.js and this copy of noonsight, by absolute paths. */
34
+ export function serviceContent(service, runtime) {
35
+ if (service.platform === 'darwin') {
36
+ const string = (s) => `<string>${xmlEscape(s)}</string>`;
37
+ return [
38
+ '<?xml version="1.0" encoding="UTF-8"?>',
39
+ '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">',
40
+ `<!-- ${MARKER} -->`,
41
+ `<!-- noonsight ${packageVersion()} -->`,
42
+ '<plist version="1.0">',
43
+ '<dict>',
44
+ ` <key>Label</key>${string(SERVICE_LABEL)}`,
45
+ ' <key>ProgramArguments</key>',
46
+ ` <array>${[runtime.node, runtime.cli, 'watch', 'run'].map(string).join('')}</array>`,
47
+ ' <key>RunAtLoad</key><true/>',
48
+ ' <key>KeepAlive</key><true/>',
49
+ ' <key>ProcessType</key><string>Background</string>',
50
+ ` <key>StandardOutPath</key>${string(service.log)}`,
51
+ ` <key>StandardErrorPath</key>${string(service.log)}`,
52
+ '</dict>',
53
+ '</plist>',
54
+ '',
55
+ ].join('\n');
56
+ }
57
+ return [
58
+ `# ${MARKER}`,
59
+ `# noonsight ${packageVersion()}`,
60
+ '[Unit]',
61
+ 'Description=noonsight: add the Claude Code hooks to each new Claude Code configuration directory',
62
+ '',
63
+ '[Service]',
64
+ `ExecStart=${[runtime.node, runtime.cli, 'watch', 'run'].map(systemdQuote).join(' ')}`,
65
+ 'Restart=always',
66
+ 'RestartSec=10',
67
+ `StandardOutput=append:${systemdPath(service.log)}`,
68
+ `StandardError=append:${systemdPath(service.log)}`,
69
+ '',
70
+ '[Install]',
71
+ 'WantedBy=default.target',
72
+ '',
73
+ ].join('\n');
74
+ }
75
+ /** The service file's content, or null when there is none. */
76
+ export function readServiceFile(service) {
77
+ try {
78
+ return fs.readFileSync(service.file, 'utf8');
79
+ }
80
+ catch (e) {
81
+ if (e.code === 'ENOENT')
82
+ return null;
83
+ throw e;
84
+ }
85
+ }
86
+ /** Whether a service file was written by noonsight. */
87
+ export function isOurs(content) {
88
+ return content.includes(MARKER);
89
+ }
90
+ /** Whether the service manager has the service loaded, whether or not a process runs. */
91
+ export function isLoaded(state) {
92
+ return state === 'running' || state === 'loaded';
93
+ }
94
+ /**
95
+ * The service manager's commands. They are run by name from PATH, as a shell would, so the tests
96
+ * can stand in for them.
97
+ */
98
+ export const manager = {
99
+ /** Start the service now, and at every login from now on. */
100
+ load(service) {
101
+ if (service.platform === 'darwin')
102
+ return run('launchctl', ['bootstrap', domain(), service.file]);
103
+ const reload = run('systemctl', ['--user', 'daemon-reload']);
104
+ if (!reload.ok)
105
+ return reload;
106
+ return run('systemctl', ['--user', 'enable', '--now', SERVICE_UNIT]);
107
+ },
108
+ /**
109
+ * Stop the service and keep it from starting at login. Without its unit file systemd cannot
110
+ * disable the unit, so only the stop is done, and a link to the file may remain.
111
+ */
112
+ unload(service) {
113
+ if (service.platform === 'darwin')
114
+ return run('launchctl', ['bootout', `${domain()}/${SERVICE_LABEL}`]);
115
+ if (!fs.existsSync(service.file))
116
+ return run('systemctl', ['--user', 'stop', SERVICE_UNIT]);
117
+ return run('systemctl', ['--user', 'disable', '--now', SERVICE_UNIT]);
118
+ },
119
+ /** Make the service manager forget a unit file that was removed; macOS has nothing to forget. */
120
+ forget(service) {
121
+ if (service.platform === 'darwin')
122
+ return { ok: true, detail: '' };
123
+ return run('systemctl', ['--user', 'daemon-reload']);
124
+ },
125
+ /** Whether the service is running, loaded without a process, or off; null when the service manager cannot say. */
126
+ running(service) {
127
+ const r = service.platform === 'darwin'
128
+ ? spawnSync('launchctl', ['print', `${domain()}/${SERVICE_LABEL}`], { encoding: 'utf8', timeout: 10000 })
129
+ : spawnSync('systemctl', ['--user', 'is-active', '--quiet', SERVICE_UNIT], { encoding: 'utf8', timeout: 10000 });
130
+ if (r.error)
131
+ return null;
132
+ if (service.platform === 'darwin') {
133
+ // launchctl print exits 113 for an unknown service; a loaded job prints its state, and a pid only while it runs.
134
+ if (r.status === 113)
135
+ return 'off';
136
+ if (r.status !== 0)
137
+ return null;
138
+ return /^\s*state = running$/m.test(r.stdout) && /^\s*pid = \d+$/m.test(r.stdout) ? 'running' : 'loaded';
139
+ }
140
+ // systemctl is-active exits 3 for an inactive unit.
141
+ if (r.status === 0)
142
+ return 'running';
143
+ if (r.status === 3)
144
+ return 'off';
145
+ return null;
146
+ },
147
+ };
148
+ function run(command, args) {
149
+ const r = spawnSync(command, [...args], { encoding: 'utf8', timeout: 30000 });
150
+ const shown = `${command} ${args.join(' ')}`;
151
+ if (r.error)
152
+ return { ok: false, detail: `${shown}: ${r.error.message}` };
153
+ if (r.status !== 0)
154
+ return { ok: false, detail: `${shown} exited ${String(r.status)}: ${(r.stderr || r.stdout).trim()}` };
155
+ return { ok: true, detail: shown };
156
+ }
157
+ /** The launchd domain of this account's login session. */
158
+ function domain() {
159
+ return `gui/${process.getuid?.() ?? 0}`;
160
+ }
161
+ function xmlEscape(s) {
162
+ return s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
163
+ }
164
+ /** One ExecStart word: quoted, with the characters systemd would expand escaped (systemd.service(5), "Command lines"). */
165
+ function systemdQuote(s) {
166
+ return `"${systemdPath(s).replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/\$/g, '$$$$')}"`;
167
+ }
168
+ /** A path in a unit file, with the specifier character escaped (systemd.unit(5), "Specifiers"). */
169
+ function systemdPath(s) {
170
+ return s.replace(/%/g, '%%');
171
+ }
172
+ //# sourceMappingURL=service.js.map
@@ -0,0 +1,130 @@
1
+ // The watcher: a resident process that adds noonsight's hooks to each Claude Code configuration
2
+ // directory made in the home directory after it started, so a new login gets them without anyone
3
+ // running `noonsight install` again.
4
+ //
5
+ // It watches only the entries directly in the home directory, without recursion, so writes below
6
+ // them never wake it. When a new directory appears there, it waits for the file that marks a Claude
7
+ // Code configuration directory, then installs the hooks as `noonsight install --account <dir>` does,
8
+ // for that directory alone and without the rest of the setup. The wait is kept up for as long as the directory stays there, so a marker
9
+ // written late is still picked up.
10
+ import fs from 'node:fs';
11
+ import path from 'node:path';
12
+ import { CONFIG_MARKER } from '../adapters/claude-code/locations.js';
13
+ /**
14
+ * Claude Code writes the marker shortly after it creates a configuration directory; the fast window
15
+ * covers that, and the slow poll keeps a late marker in view at almost no cost.
16
+ */
17
+ const POLL_MS = 100;
18
+ const FAST_WINDOW_MS = 10_000;
19
+ const SLOW_POLL_MS = 5_000;
20
+ /** Start watching; the watch keeps the process alive, and the process ends if the watch fails. */
21
+ export function startWatcher(options) {
22
+ const { home, log } = options;
23
+ const fastWindowMs = options.fastWindowMs ?? FAST_WINDOW_MS;
24
+ const slowPollMs = options.slowPollMs ?? SLOW_POLL_MS;
25
+ const timers = new Set();
26
+ // The entries already there are not new; a directory made while the watcher was not running is
27
+ // left to `noonsight install`, and `noonsight check` lists it. Each entry is known by its inode, so a
28
+ // directory removed and made again under the same name between two events still counts as new.
29
+ const known = new Map();
30
+ for (const name of fs.readdirSync(home)) {
31
+ const ino = inode(path.join(home, name));
32
+ if (ino !== null)
33
+ known.set(name, ino);
34
+ }
35
+ const consider = (name) => {
36
+ const entry = path.join(home, name);
37
+ const ino = inode(entry);
38
+ if (ino === null) {
39
+ known.delete(name);
40
+ return;
41
+ }
42
+ if (known.get(name) === ino)
43
+ return;
44
+ known.set(name, ino);
45
+ if (isDirectory(entry))
46
+ awaitMarker(entry, name, ino, Date.now());
47
+ };
48
+ const awaitMarker = (dir, name, ino, since) => {
49
+ // ~/.claude is Claude Code's default configuration directory; its marker lives in the home directory instead.
50
+ if (name === '.claude' || isFile(path.join(dir, CONFIG_MARKER))) {
51
+ installInto(dir);
52
+ return;
53
+ }
54
+ if (known.get(name) !== ino)
55
+ return;
56
+ const timer = setTimeout(() => {
57
+ timers.delete(timer);
58
+ awaitMarker(dir, name, ino, since);
59
+ }, Date.now() - since < fastWindowMs ? POLL_MS : slowPollMs);
60
+ timers.add(timer);
61
+ };
62
+ const installInto = (dir) => {
63
+ log(`new Claude Code configuration ${dir}: installing`);
64
+ try {
65
+ options.install(dir);
66
+ }
67
+ catch (e) {
68
+ log(`problem: install into ${dir} failed: ${e instanceof Error ? e.message : String(e)}`);
69
+ }
70
+ };
71
+ // Without a name, look at every entry: new ones are considered, vanished ones forgotten.
72
+ const scan = () => {
73
+ let names;
74
+ try {
75
+ names = fs.readdirSync(home);
76
+ }
77
+ catch {
78
+ return;
79
+ }
80
+ for (const name of new Set([...names, ...known.keys()]))
81
+ consider(name);
82
+ };
83
+ const watcher = fs.watch(home, { persistent: true, recursive: false }, (_event, filename) => {
84
+ if (filename)
85
+ consider(filename.toString());
86
+ else
87
+ scan();
88
+ });
89
+ watcher.on('error', (e) => {
90
+ log(`problem: watching ${home} stopped: ${e.message}`);
91
+ close();
92
+ process.exitCode = 1;
93
+ });
94
+ // An entry made after the snapshot above and before the watch started has no event; the scan finds it.
95
+ scan();
96
+ const close = () => {
97
+ watcher.close();
98
+ for (const timer of timers)
99
+ clearTimeout(timer);
100
+ timers.clear();
101
+ };
102
+ log(`watching ${home} for new Claude Code configuration directories (entries directly in it only)`);
103
+ return close;
104
+ }
105
+ /** The entry's own inode, not its target's; null when it does not exist. */
106
+ function inode(entry) {
107
+ try {
108
+ return fs.lstatSync(entry, { bigint: true }).ino;
109
+ }
110
+ catch {
111
+ return null;
112
+ }
113
+ }
114
+ function isDirectory(entry) {
115
+ try {
116
+ return fs.statSync(entry).isDirectory();
117
+ }
118
+ catch {
119
+ return false;
120
+ }
121
+ }
122
+ function isFile(file) {
123
+ try {
124
+ return fs.statSync(file).isFile();
125
+ }
126
+ catch {
127
+ return false;
128
+ }
129
+ }
130
+ //# sourceMappingURL=watcher.js.map
@@ -0,0 +1,11 @@
1
+ // How a message names noonsight's commands. The messages name them as `noonsight check`; a person who has
2
+ // noonsight as a Claude Code plugin has no `noonsight` on PATH, so for them the same message names the
3
+ // plugin's skill, /noonsight:check, or, for a command that is not a skill, `npx noonsight install`.
4
+ /** The plugin's skills, one per command a person runs inside a session; `setup` is init and check. */
5
+ export const SKILL_COMMANDS = ['accept', 'check', 'models', 'report', 'resolve', 'trust', 'untrust'];
6
+ const SKILLS = new Set(SKILL_COMMANDS);
7
+ /** A message with every command in backticks named as a plugin's person runs it. */
8
+ export function forPlugin(text) {
9
+ return text.replace(/`noonsight ([a-z]+)([^`]*)`/g, (_, command, rest) => SKILLS.has(command) ? `\`/noonsight:${command}${rest}\`` : command === 'init' ? `\`/noonsight:setup\`` : `\`npx noonsight ${command}${rest}\``);
10
+ }
11
+ //# sourceMappingURL=wording.js.map
@@ -0,0 +1,118 @@
1
+ {
2
+ "hooks": {
3
+ "SessionStart": [
4
+ {
5
+ "hooks": [
6
+ {
7
+ "type": "command",
8
+ "command": "/bin/sh",
9
+ "args": [
10
+ "${CLAUDE_PLUGIN_ROOT}/scripts/noonsight",
11
+ "hook",
12
+ "session-start"
13
+ ],
14
+ "timeout": 10
15
+ }
16
+ ]
17
+ }
18
+ ],
19
+ "PostModelSwitch": [
20
+ {
21
+ "hooks": [
22
+ {
23
+ "type": "command",
24
+ "command": "/bin/sh",
25
+ "args": [
26
+ "${CLAUDE_PLUGIN_ROOT}/scripts/noonsight",
27
+ "hook",
28
+ "model-switch"
29
+ ],
30
+ "timeout": 10
31
+ }
32
+ ]
33
+ }
34
+ ],
35
+ "PreToolUse": [
36
+ {
37
+ "matcher": "Agent|Task",
38
+ "hooks": [
39
+ {
40
+ "type": "command",
41
+ "command": "/bin/sh",
42
+ "args": [
43
+ "${CLAUDE_PLUGIN_ROOT}/scripts/noonsight",
44
+ "hook",
45
+ "agent-call"
46
+ ],
47
+ "timeout": 10
48
+ }
49
+ ]
50
+ }
51
+ ],
52
+ "SubagentStart": [
53
+ {
54
+ "hooks": [
55
+ {
56
+ "type": "command",
57
+ "command": "/bin/sh",
58
+ "args": [
59
+ "${CLAUDE_PLUGIN_ROOT}/scripts/noonsight",
60
+ "hook",
61
+ "subagent-start"
62
+ ],
63
+ "timeout": 10
64
+ }
65
+ ]
66
+ }
67
+ ],
68
+ "PostToolUse": [
69
+ {
70
+ "matcher": "Agent|Task",
71
+ "hooks": [
72
+ {
73
+ "type": "command",
74
+ "command": "/bin/sh",
75
+ "args": [
76
+ "${CLAUDE_PLUGIN_ROOT}/scripts/noonsight",
77
+ "hook",
78
+ "agent-result"
79
+ ],
80
+ "timeout": 10
81
+ }
82
+ ]
83
+ },
84
+ {
85
+ "matcher": "Edit|Write",
86
+ "hooks": [
87
+ {
88
+ "type": "command",
89
+ "command": "/bin/sh",
90
+ "args": [
91
+ "${CLAUDE_PLUGIN_ROOT}/scripts/noonsight",
92
+ "hook",
93
+ "file-edit"
94
+ ],
95
+ "timeout": 10
96
+ }
97
+ ]
98
+ }
99
+ ],
100
+ "PermissionRequest": [
101
+ {
102
+ "matcher": "Agent|Task",
103
+ "hooks": [
104
+ {
105
+ "type": "command",
106
+ "command": "/bin/sh",
107
+ "args": [
108
+ "${CLAUDE_PLUGIN_ROOT}/scripts/noonsight",
109
+ "hook",
110
+ "agent-held"
111
+ ],
112
+ "timeout": 10
113
+ }
114
+ ]
115
+ }
116
+ ]
117
+ }
118
+ }