noonsight 0.0.1 → 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.
- package/.claude-plugin/marketplace.json +19 -0
- package/.claude-plugin/plugin.json +14 -0
- package/README.md +271 -9
- package/dist/src/adapters/claude-code/classifier.js +138 -0
- package/dist/src/adapters/claude-code/code-edit.js +108 -0
- package/dist/src/adapters/claude-code/compat.js +31 -0
- package/dist/src/adapters/claude-code/definitions.js +347 -0
- package/dist/src/adapters/claude-code/fork.js +132 -0
- package/dist/src/adapters/claude-code/hook.js +652 -0
- package/dist/src/adapters/claude-code/install.js +186 -0
- package/dist/src/adapters/claude-code/launch.js +270 -0
- package/dist/src/adapters/claude-code/launcher.js +252 -0
- package/dist/src/adapters/claude-code/locations.js +115 -0
- package/dist/src/adapters/claude-code/managed.js +75 -0
- package/dist/src/adapters/claude-code/plugin-files.js +88 -0
- package/dist/src/adapters/claude-code/plugins.js +117 -0
- package/dist/src/adapters/claude-code/pointer.js +48 -0
- package/dist/src/adapters/claude-code/predict.js +271 -0
- package/dist/src/adapters/claude-code/routing.js +88 -0
- package/dist/src/adapters/claude-code/settings.js +189 -0
- package/dist/src/adapters/claude-code/subagents.js +666 -0
- package/dist/src/adapters/claude-code/triggers.js +88 -0
- package/dist/src/cli.js +157 -0
- package/dist/src/commands/accept.js +115 -0
- package/dist/src/commands/check.js +529 -0
- package/dist/src/commands/hook.js +73 -0
- package/dist/src/commands/init.js +29 -0
- package/dist/src/commands/install.js +370 -0
- package/dist/src/commands/models.js +213 -0
- package/dist/src/commands/receives.js +84 -0
- package/dist/src/commands/report.js +386 -0
- package/dist/src/commands/resolve.js +32 -0
- package/dist/src/commands/run.js +168 -0
- package/dist/src/commands/trust.js +70 -0
- package/dist/src/commands/usage.js +10 -0
- package/dist/src/commands/watch.js +154 -0
- package/dist/src/config/create.js +51 -0
- package/dist/src/config/json-schema.js +207 -0
- package/dist/src/config/load.js +758 -0
- package/dist/src/config/paths.js +42 -0
- package/dist/src/config/preset-changes.js +190 -0
- package/dist/src/config/project.js +354 -0
- package/dist/src/config/rewrite.js +112 -0
- package/dist/src/core/backtracking.js +330 -0
- package/dist/src/core/classify.js +66 -0
- package/dist/src/core/normalize.js +24 -0
- package/dist/src/core/overlap.js +39 -0
- package/dist/src/core/regex-sample.js +278 -0
- package/dist/src/core/resolve.js +201 -0
- package/dist/src/core/route.js +93 -0
- package/dist/src/core/scope.js +77 -0
- package/dist/src/core/triggers.js +30 -0
- package/dist/src/core/types.js +74 -0
- package/dist/src/state/agents.js +200 -0
- package/dist/src/state/claims.js +80 -0
- package/dist/src/state/files.js +84 -0
- package/dist/src/state/installs.js +29 -0
- package/dist/src/state/log.js +90 -0
- package/dist/src/state/node.js +35 -0
- package/dist/src/state/sessions.js +109 -0
- package/dist/src/state/triggers.js +57 -0
- package/dist/src/version.js +24 -0
- package/dist/src/watch/service.js +172 -0
- package/dist/src/watch/watcher.js +130 -0
- package/dist/src/wording.js +11 -0
- package/hooks/hooks.json +118 -0
- package/package.json +57 -7
- package/presets/claude/SOURCES.md +485 -0
- package/presets/claude/preset.json +432 -0
- package/presets/claude/rules/fable-5-2/code-ownership.md +1 -0
- package/presets/claude/rules/fable-5-2/finish-work.md +1 -0
- package/presets/claude/rules/fable-5-2/low-context-report.md +1 -0
- package/presets/claude/rules/fable-5-2/progress-updates.md +1 -0
- package/presets/claude/rules/haiku-5-5/carry-through.md +1 -0
- package/presets/claude/rules/haiku-5-5/search-recent-facts.md +1 -0
- package/presets/claude/rules/haiku-5-5/verify-changes.md +1 -0
- package/presets/claude/rules/opus-5-5/explore-before-acting.md +1 -0
- package/presets/claude/rules/opus-5-5/progress-updates.md +1 -0
- package/presets/claude/rules/opus-5-5/unattended-run.md +1 -0
- package/presets/claude/rules/sonnet-5-5/carry-through.md +1 -0
- package/presets/claude/rules/sonnet-5-5/check-current-facts.md +1 -0
- package/presets/claude/rules/sonnet-5-5/ideas-first.md +1 -0
- package/presets/claude/rules/sonnet-5-5/no-self-review-rounds.md +1 -0
- package/presets/claude/rules/sonnet-5-5/stay-in-scope.md +1 -0
- package/presets/claude/rules/sonnet-5-5/verify-changes.md +1 -0
- package/presets/claude/versions/1.3.0/preset.json +432 -0
- package/presets/claude/versions/1.3.0/rules/fable-5-2/code-ownership.md +1 -0
- package/presets/claude/versions/1.3.0/rules/fable-5-2/finish-work.md +1 -0
- package/presets/claude/versions/1.3.0/rules/fable-5-2/low-context-report.md +1 -0
- package/presets/claude/versions/1.3.0/rules/fable-5-2/progress-updates.md +1 -0
- package/presets/claude/versions/1.3.0/rules/haiku-5-5/carry-through.md +1 -0
- package/presets/claude/versions/1.3.0/rules/haiku-5-5/search-recent-facts.md +1 -0
- package/presets/claude/versions/1.3.0/rules/haiku-5-5/verify-changes.md +1 -0
- package/presets/claude/versions/1.3.0/rules/opus-5-5/explore-before-acting.md +1 -0
- package/presets/claude/versions/1.3.0/rules/opus-5-5/progress-updates.md +1 -0
- package/presets/claude/versions/1.3.0/rules/opus-5-5/unattended-run.md +1 -0
- package/presets/claude/versions/1.3.0/rules/sonnet-5-5/carry-through.md +1 -0
- package/presets/claude/versions/1.3.0/rules/sonnet-5-5/check-current-facts.md +1 -0
- package/presets/claude/versions/1.3.0/rules/sonnet-5-5/ideas-first.md +1 -0
- package/presets/claude/versions/1.3.0/rules/sonnet-5-5/no-self-review-rounds.md +1 -0
- package/presets/claude/versions/1.3.0/rules/sonnet-5-5/stay-in-scope.md +1 -0
- package/presets/claude/versions/1.3.0/rules/sonnet-5-5/verify-changes.md +1 -0
- package/schema/noonsight-project.schema.json +394 -0
- package/schema/noonsight.schema.json +425 -0
- package/schema/preset.schema.json +194 -0
- package/scripts/noonsight +68 -0
- package/skills/accept/SKILL.md +11 -0
- package/skills/check/SKILL.md +11 -0
- package/skills/models/SKILL.md +11 -0
- package/skills/report/SKILL.md +11 -0
- package/skills/resolve/SKILL.md +12 -0
- package/skills/setup/SKILL.md +15 -0
- package/skills/trust/SKILL.md +12 -0
- package/skills/untrust/SKILL.md +12 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import os from 'node:os';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
/** The installed package's root directory (this file lives in dist/src/config/). */
|
|
5
|
+
export const PACKAGE_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', '..', '..');
|
|
6
|
+
/** The command line's entry point in the installed package; hooks run this file. */
|
|
7
|
+
export const CLI_FILE = path.join(PACKAGE_ROOT, 'dist', 'src', 'cli.js');
|
|
8
|
+
/**
|
|
9
|
+
* Where the configuration lives. One file serves the whole machine: it never depends on which
|
|
10
|
+
* Claude Code account or configuration directory (CLAUDE_CONFIG_DIR) is running, because rules
|
|
11
|
+
* follow the model and not the account.
|
|
12
|
+
*
|
|
13
|
+
* 1. $NOONSIGHT_CONFIG
|
|
14
|
+
* 2. ~/.config/noonsight/noonsight.json
|
|
15
|
+
*/
|
|
16
|
+
export function configPath(env = process.env, home = os.homedir()) {
|
|
17
|
+
if (env.NOONSIGHT_CONFIG)
|
|
18
|
+
return path.resolve(env.NOONSIGHT_CONFIG);
|
|
19
|
+
return path.join(home, '.config', 'noonsight', 'noonsight.json');
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Where presets are read from: the installed package, so a preset follows the package version,
|
|
23
|
+
* along with the copies of earlier versions it keeps for configurations that hold one.
|
|
24
|
+
* NOONSIGHT_PRESETS_DIR replaces the directory for tests and preset development.
|
|
25
|
+
*/
|
|
26
|
+
export function presetsDir(env = process.env) {
|
|
27
|
+
return env.NOONSIGHT_PRESETS_DIR ? path.resolve(env.NOONSIGHT_PRESETS_DIR) : path.join(PACKAGE_ROOT, 'presets');
|
|
28
|
+
}
|
|
29
|
+
export const SCHEMA_DIR = path.join(PACKAGE_ROOT, 'schema');
|
|
30
|
+
/**
|
|
31
|
+
* The account's own state: the decision log, what each session last received, and the list of
|
|
32
|
+
* Claude Code configuration directories noonsight was installed into. Hooks run as the account that
|
|
33
|
+
* owns the session, so a machine-wide install still keeps state per account.
|
|
34
|
+
*/
|
|
35
|
+
export function stateDir(home = os.homedir()) {
|
|
36
|
+
return path.join(home, '.local', 'state', 'noonsight');
|
|
37
|
+
}
|
|
38
|
+
/** Files noonsight writes for one account, such as the hook pointer of a per-account install. */
|
|
39
|
+
export function dataDir(home = os.homedir()) {
|
|
40
|
+
return path.join(home, '.local', 'share', 'noonsight');
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=paths.js.map
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
// What a preset update changes for a configuration. The configuration records the preset version it
|
|
2
|
+
// was checked against ("preset_version"); the package ships the installed version and keeps a copy of
|
|
3
|
+
// each earlier one, so the rules each model declared with "use": "preset" receives can be compared
|
|
4
|
+
// rule by rule: rules turned on or off, renamed, or changed, changes a rule that replaces a preset
|
|
5
|
+
// rule takes from it, and enable, disable or replaces entries that no longer name a rule. noonsight
|
|
6
|
+
// check, noonsight accept and the session-start warning all read it.
|
|
7
|
+
import { readPresetVersion, } from './load.js';
|
|
8
|
+
/**
|
|
9
|
+
* Compare the accepted version with the installed one for every model declared with "use": "preset",
|
|
10
|
+
* or only `only`. Without an accepted version the package keeps, the installed version is compared
|
|
11
|
+
* with itself, which still finds entries that name a rule by an id it had before a rename. Returns
|
|
12
|
+
* null when the preset is not installed. A kept copy that fails its checks throws a ConfigError.
|
|
13
|
+
*/
|
|
14
|
+
export function comparePresets(versions, declared, options = {}, env = process.env, only) {
|
|
15
|
+
const installed = versions.installed;
|
|
16
|
+
if (installed === null)
|
|
17
|
+
return null;
|
|
18
|
+
const from = versions.accepted === null ? null : readPresetVersion(versions.name, versions.accepted, options, env);
|
|
19
|
+
const base = from ?? installed;
|
|
20
|
+
const models = [];
|
|
21
|
+
for (const [model, entry] of declared) {
|
|
22
|
+
if (entry.use !== 'preset' || entry.wildcard || (only !== undefined && model !== only))
|
|
23
|
+
continue;
|
|
24
|
+
const changes = modelChanges(model, base.models.get(model) ?? [], installed.models.get(model), entry);
|
|
25
|
+
if (hasChanges(changes))
|
|
26
|
+
models.push(changes);
|
|
27
|
+
}
|
|
28
|
+
return {
|
|
29
|
+
name: versions.name,
|
|
30
|
+
accepted: versions.accepted,
|
|
31
|
+
installed,
|
|
32
|
+
from,
|
|
33
|
+
held: versions.onChange === 'hold' && versions.accepted !== null && versions.accepted !== installed.version,
|
|
34
|
+
models,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/** Whether the comparison names a version change, as opposed to entries alone. */
|
|
38
|
+
export function versionChanged(c) {
|
|
39
|
+
return c.accepted !== null && c.accepted !== c.installed.version;
|
|
40
|
+
}
|
|
41
|
+
export function hasChanges(m) {
|
|
42
|
+
return m.dropped || [m.nowOn, m.nowOff, m.renamed, m.changed, m.inherited, m.newOff, m.staleEntries].some((list) => list.length > 0);
|
|
43
|
+
}
|
|
44
|
+
/** Whether the change alters what the model is delivered, or drops a choice the user made. */
|
|
45
|
+
export function changesDelivery(m) {
|
|
46
|
+
return (m.dropped ||
|
|
47
|
+
m.nowOn.length > 0 ||
|
|
48
|
+
m.nowOff.length > 0 ||
|
|
49
|
+
m.changed.length > 0 ||
|
|
50
|
+
m.inherited.length > 0 ||
|
|
51
|
+
m.staleEntries.some((e) => e.now === null));
|
|
52
|
+
}
|
|
53
|
+
function modelChanges(model, from, to, entry) {
|
|
54
|
+
const empty = { nowOn: [], nowOff: [], renamed: [], changed: [], inherited: [], newOff: [], staleEntries: [] };
|
|
55
|
+
if (to === undefined)
|
|
56
|
+
return { model, dropped: true, ...empty };
|
|
57
|
+
const predecessor = (r) => from.find((p) => p.id === r.id) ?? from.find((p) => r.renamedFrom.includes(p.id));
|
|
58
|
+
const asBefore = (id) => {
|
|
59
|
+
if (from.some((p) => p.id === id || p.renamedFrom.includes(id)))
|
|
60
|
+
return id;
|
|
61
|
+
const r = to.find((r) => r.id === id || r.renamedFrom.includes(id));
|
|
62
|
+
return (r === undefined ? undefined : predecessor(r)?.id) ?? id;
|
|
63
|
+
};
|
|
64
|
+
// A replaced rule is off, as a disabled one is.
|
|
65
|
+
const replacing = entry.add.flatMap((u) => (u.replaces === undefined ? [] : [{ rule: u.id, ...u.replaces }]));
|
|
66
|
+
const enable = entry.enable;
|
|
67
|
+
const off = [...entry.disable, ...replacing.map((x) => x.id)];
|
|
68
|
+
const onBefore = onRules(from, enable.map(asBefore), off.map(asBefore));
|
|
69
|
+
const onAfter = onRules(to, enable, off);
|
|
70
|
+
const successor = (p) => to.find((r) => predecessor(r) === p);
|
|
71
|
+
const nowOn = [];
|
|
72
|
+
const renamed = [];
|
|
73
|
+
const changed = [];
|
|
74
|
+
const newOff = [];
|
|
75
|
+
for (const r of to) {
|
|
76
|
+
const p = predecessor(r);
|
|
77
|
+
if (p !== undefined && p.id !== r.id)
|
|
78
|
+
renamed.push({ from: p.id, to: r.id });
|
|
79
|
+
if (onAfter.has(r.id)) {
|
|
80
|
+
if (p === undefined)
|
|
81
|
+
nowOn.push({ id: r.id, why: 'new' });
|
|
82
|
+
else if (!onBefore.has(p.id))
|
|
83
|
+
nowOn.push({ id: r.id, why: 'ships-on' });
|
|
84
|
+
else {
|
|
85
|
+
const what = differences(p, r);
|
|
86
|
+
if (what.length)
|
|
87
|
+
changed.push({ id: r.id, what });
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
else if (p === undefined)
|
|
91
|
+
newOff.push(r.id);
|
|
92
|
+
}
|
|
93
|
+
const nowOff = [];
|
|
94
|
+
for (const p of from) {
|
|
95
|
+
if (!onBefore.has(p.id))
|
|
96
|
+
continue;
|
|
97
|
+
const r = successor(p);
|
|
98
|
+
if (r === undefined)
|
|
99
|
+
nowOff.push({ id: p.id, why: 'removed' });
|
|
100
|
+
else if (!onAfter.has(r.id))
|
|
101
|
+
nowOff.push({ id: r.id, why: 'ships-off' });
|
|
102
|
+
}
|
|
103
|
+
// A rule that replaces a preset rule takes the keys it does not give from that rule, so their changes reach it.
|
|
104
|
+
const inherited = [];
|
|
105
|
+
for (const x of replacing) {
|
|
106
|
+
const r = to.find((r) => r.id === x.id || r.renamedFrom.includes(x.id));
|
|
107
|
+
const p = r === undefined ? undefined : predecessor(r);
|
|
108
|
+
if (r === undefined || p === undefined)
|
|
109
|
+
continue;
|
|
110
|
+
const what = differences(p, r).filter((w) => w !== 'text' && x.inherits.includes(w));
|
|
111
|
+
if (what.length)
|
|
112
|
+
inherited.push({ id: r.id, by: x.rule, what });
|
|
113
|
+
}
|
|
114
|
+
const ids = new Set(to.map((r) => r.id));
|
|
115
|
+
const now = (id) => to.find((r) => r.renamedFrom.includes(id))?.id ?? null;
|
|
116
|
+
const staleEntries = [];
|
|
117
|
+
for (const [list, entries] of [['enable', enable], ['disable', entry.disable]])
|
|
118
|
+
for (const id of entries)
|
|
119
|
+
if (!ids.has(id))
|
|
120
|
+
staleEntries.push({ list, id, now: now(id) });
|
|
121
|
+
for (const x of replacing)
|
|
122
|
+
if (!ids.has(x.id))
|
|
123
|
+
staleEntries.push({ list: 'replaces', rule: x.rule, id: x.id, now: now(x.id) });
|
|
124
|
+
return { model, dropped: false, nowOn, nowOff, renamed, changed, inherited, newOff, staleEntries };
|
|
125
|
+
}
|
|
126
|
+
/** The ids of the rules that are on, with the entries mapped through renames. */
|
|
127
|
+
function onRules(rules, enable, disable) {
|
|
128
|
+
const current = (id) => rules.find((r) => r.id === id)?.id ?? rules.find((r) => r.renamedFrom.includes(id))?.id;
|
|
129
|
+
const on = new Set(enable.map(current));
|
|
130
|
+
const off = new Set(disable.map(current));
|
|
131
|
+
return new Set(rules.filter((r) => (r.enabled || on.has(r.id)) && !off.has(r.id)).map((r) => r.id));
|
|
132
|
+
}
|
|
133
|
+
function differences(a, b) {
|
|
134
|
+
const same = (x, y) => x.length === y.length && x.every((v, i) => v === y[i]);
|
|
135
|
+
return [
|
|
136
|
+
...(a.text !== b.text ? ['text'] : []),
|
|
137
|
+
...(!same(a.scopes, b.scopes) ? ['scopes'] : []),
|
|
138
|
+
...(!same(a.where, b.where) ? ['places'] : []),
|
|
139
|
+
...(!same(a.triggers, b.triggers) ? ['triggers'] : []),
|
|
140
|
+
];
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* One model's changes, as one line. Held, the changes are those accepting the installed version would
|
|
144
|
+
* bring, so the entries are named as they would read then.
|
|
145
|
+
*/
|
|
146
|
+
export function describeChanges(m, held = false) {
|
|
147
|
+
if (m.dropped)
|
|
148
|
+
return `the installed preset has no rules for it${held ? '' : ' any more'}; declare it with "use": "custom" or "use": "none"${held ? ' before accepting it' : ''}`;
|
|
149
|
+
const parts = [];
|
|
150
|
+
if (m.nowOn.length)
|
|
151
|
+
parts.push(`now on: ${m.nowOn.map((r) => `${r.id} (${r.why === 'new' ? 'new' : 'ships on now'})`).join(', ')}`);
|
|
152
|
+
if (m.nowOff.length)
|
|
153
|
+
parts.push(`now off: ${m.nowOff.map((r) => `${r.id} (${r.why === 'removed' ? 'removed' : 'ships off now'})`).join(', ')}`);
|
|
154
|
+
if (m.renamed.length)
|
|
155
|
+
parts.push(`renamed: ${m.renamed.map((r) => `${r.from} to ${r.to}`).join(', ')}`);
|
|
156
|
+
if (m.changed.length)
|
|
157
|
+
parts.push(`changed: ${m.changed.map((r) => `${r.id} (${r.what.join(', ')})`).join(', ')}`);
|
|
158
|
+
for (const r of m.inherited)
|
|
159
|
+
parts.push(`changed in a replaced rule: ${r.id} (${r.what.join(', ')}), which ${r.by} takes`);
|
|
160
|
+
if (m.newOff.length)
|
|
161
|
+
parts.push(`new and off unless enabled: ${m.newOff.join(', ')}`);
|
|
162
|
+
for (const e of m.staleEntries)
|
|
163
|
+
parts.push(describeStale(e, held));
|
|
164
|
+
return parts.join('; ');
|
|
165
|
+
}
|
|
166
|
+
/** An entry that no longer names a rule of the installed version, as one part of a line. */
|
|
167
|
+
function describeStale(e, held) {
|
|
168
|
+
if (e.list === 'replaces')
|
|
169
|
+
return describeStaleReplaces(e.rule, e.id, e.now, held);
|
|
170
|
+
if (e.now !== null)
|
|
171
|
+
return held
|
|
172
|
+
? `"${e.list}" names "${e.id}", which the installed preset renames "${e.now}"; it will still apply to it, and after accepting, "${e.now}" is the name to write`
|
|
173
|
+
: `"${e.list}" names "${e.id}", which is now "${e.now}"; it still applies to it, and "${e.now}" is the name to write`;
|
|
174
|
+
return held
|
|
175
|
+
? `"${e.list}" names "${e.id}", which the installed preset no longer has; remove it before accepting`
|
|
176
|
+
: `"${e.list}" names "${e.id}", which the preset no longer has, so that choice no longer applies; remove it`;
|
|
177
|
+
}
|
|
178
|
+
/** A "replaces" that no longer names a rule of the installed version, as one part of a line. */
|
|
179
|
+
function describeStaleReplaces(rule, id, now, held) {
|
|
180
|
+
const lead = `rule "${rule}" replaces "${id}", which`;
|
|
181
|
+
if (now !== null)
|
|
182
|
+
return held
|
|
183
|
+
? `${lead} the installed preset renames "${now}"; it will still replace it, and after accepting, "${now}" is the name to write`
|
|
184
|
+
: `${lead} is now "${now}"; it still replaces it, and "${now}" is the name to write`;
|
|
185
|
+
const keep = `write in "${rule}" any scopes, places or triggers it took from "${id}" and should keep`;
|
|
186
|
+
return held
|
|
187
|
+
? `${lead} the installed preset no longer has; remove "replaces" before accepting, and ${keep}`
|
|
188
|
+
: `${lead} the preset no longer has; "${rule}" is still delivered, after the rules of "all_models", with what it took from "${id}"; remove "replaces", and ${keep}`;
|
|
189
|
+
}
|
|
190
|
+
//# sourceMappingURL=preset-changes.js.map
|
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
// Project configuration: a file a repository commits at .noonsight/noonsight.json, laid over the
|
|
2
|
+
// machine's configuration for the sessions that run in that project. The machine's configuration
|
|
3
|
+
// decides how far a project may change it ("projects.allow"), and a project applies only once this
|
|
4
|
+
// account trusts its directory (`noonsight trust`). This module finds the file, keeps the trust
|
|
5
|
+
// records, and merges the two files; load.ts checks the result as it checks any configuration.
|
|
6
|
+
import fs from 'node:fs';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import { writeFileAtomic } from '../state/files.js';
|
|
9
|
+
/** Where a project keeps its configuration, relative to the project directory. */
|
|
10
|
+
export const PROJECT_FILE = path.join('.noonsight', 'noonsight.json');
|
|
11
|
+
/**
|
|
12
|
+
* How far a project may change the machine's configuration, from least to most.
|
|
13
|
+
* none project files are ignored.
|
|
14
|
+
* add a project only adds rules, for models the machine declares with "preset" or "custom".
|
|
15
|
+
* rules a project also changes which rules a model receives, and declares scopes.
|
|
16
|
+
* all a project also sets routing, the classifier and the subagent settings.
|
|
17
|
+
*/
|
|
18
|
+
export const PROJECT_ALLOW = ['none', 'add', 'rules', 'all'];
|
|
19
|
+
/** Projects may add rules, and nothing else, unless the machine's configuration says otherwise. */
|
|
20
|
+
export const DEFAULT_PROJECT_ALLOW = 'add';
|
|
21
|
+
/** The top-level keys a project file may write at each level; "version" and "$schema" always. */
|
|
22
|
+
const KEYS = {
|
|
23
|
+
add: ['all_models', 'models'],
|
|
24
|
+
rules: ['all_models', 'models', 'scopes', 'session', 'on_undeclared'],
|
|
25
|
+
all: ['all_models', 'models', 'scopes', 'session', 'on_undeclared', 'routing', 'classifier', 'subagents'],
|
|
26
|
+
};
|
|
27
|
+
/** The lowest level that allows a top-level key of a project file. */
|
|
28
|
+
function levelFor(key) {
|
|
29
|
+
return KEYS.add.includes(key) ? 'add' : KEYS.rules.includes(key) ? 'rules' : 'all';
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The directory holding `.git` (a directory, or a file in a worktree) above `start`, or `start`
|
|
33
|
+
* itself when there is none.
|
|
34
|
+
*/
|
|
35
|
+
export function repositoryRoot(start) {
|
|
36
|
+
for (let d = start;; d = path.dirname(d)) {
|
|
37
|
+
if (exists(path.join(d, '.git')))
|
|
38
|
+
return d;
|
|
39
|
+
if (path.dirname(d) === d)
|
|
40
|
+
return start;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The nearest project file from a directory up to its repository root, the closest first; outside
|
|
45
|
+
* a repository, only the directory itself is looked at.
|
|
46
|
+
*/
|
|
47
|
+
export function findProject(start) {
|
|
48
|
+
if (start === null || start === '')
|
|
49
|
+
return null;
|
|
50
|
+
const from = path.resolve(start);
|
|
51
|
+
const top = repositoryRoot(from);
|
|
52
|
+
for (let d = from;; d = path.dirname(d)) {
|
|
53
|
+
const file = path.join(d, PROJECT_FILE);
|
|
54
|
+
if (isFile(file))
|
|
55
|
+
return { dir: d, file };
|
|
56
|
+
if (d === top || path.dirname(d) === d)
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* The project file for a session or a command started in `start`, with this account's trust in it;
|
|
62
|
+
* null when there is none, or when it is the machine's configuration file itself.
|
|
63
|
+
*/
|
|
64
|
+
export function projectFor(start, machineFile, stateDir) {
|
|
65
|
+
const found = findProject(start);
|
|
66
|
+
if (found === null || real(found.file) === real(machineFile))
|
|
67
|
+
return null;
|
|
68
|
+
return { ...found, trusted: trustOf(stateDir, found.dir) };
|
|
69
|
+
}
|
|
70
|
+
// Trust. One record per project directory, by its real path, in the account's state directory:
|
|
71
|
+
// hooks run as the account that owns the session, so each account decides for itself.
|
|
72
|
+
const TRUST_FILE = 'projects.json';
|
|
73
|
+
function trustFile(stateDir) {
|
|
74
|
+
return path.join(stateDir, TRUST_FILE);
|
|
75
|
+
}
|
|
76
|
+
function readTrust(stateDir) {
|
|
77
|
+
try {
|
|
78
|
+
const value = JSON.parse(fs.readFileSync(trustFile(stateDir), 'utf8'));
|
|
79
|
+
if (!Array.isArray(value.projects))
|
|
80
|
+
return [];
|
|
81
|
+
return value.projects.filter((r) => typeof r === 'object' && r !== null && typeof r.dir === 'string' && typeof r.trusted === 'boolean');
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
// Missing or unreadable: no project is trusted.
|
|
85
|
+
return [];
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/** Whether this account trusts a project directory: true, false when it declined, null when it never decided. */
|
|
89
|
+
export function trustOf(stateDir, dir) {
|
|
90
|
+
const key = real(dir);
|
|
91
|
+
return readTrust(stateDir).find((r) => r.dir === key)?.trusted ?? null;
|
|
92
|
+
}
|
|
93
|
+
/** Record this account's decision for a project directory; returns the decision it had before. */
|
|
94
|
+
export function setTrust(stateDir, dir, trusted, now) {
|
|
95
|
+
const key = real(dir);
|
|
96
|
+
const records = readTrust(stateDir);
|
|
97
|
+
const before = records.find((r) => r.dir === key)?.trusted ?? null;
|
|
98
|
+
const next = [...records.filter((r) => r.dir !== key), { dir: key, trusted, at: now.toISOString() }].sort((a, b) => a.dir < b.dir ? -1 : a.dir > b.dir ? 1 : 0);
|
|
99
|
+
fs.mkdirSync(stateDir, { recursive: true, mode: 0o700 });
|
|
100
|
+
writeFileAtomic(trustFile(stateDir), `${JSON.stringify({ projects: next }, null, 2)}\n`, 0o600);
|
|
101
|
+
return before;
|
|
102
|
+
}
|
|
103
|
+
/** Whether `file`, once symlinks are followed, is inside `dir`; a file that cannot be resolved is judged by its path. */
|
|
104
|
+
function insideProject(dir, file) {
|
|
105
|
+
let rel;
|
|
106
|
+
try {
|
|
107
|
+
rel = path.relative(fs.realpathSync(dir), fs.realpathSync(file));
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
rel = path.relative(dir, file);
|
|
111
|
+
}
|
|
112
|
+
return rel !== '' && rel !== '..' && !rel.startsWith(`..${path.sep}`) && !path.isAbsolute(rel);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Lay a project file over the machine's configuration; the project's rule files are resolved
|
|
116
|
+
* against `baseDir`, the project file's directory, and must stay inside `projectDir`. Every value the project writes replaces the
|
|
117
|
+
* machine's, key by key, with these exceptions:
|
|
118
|
+
* - rule lists ("all_models.add", "models.<id>.add") add up, the machine's rules first; above
|
|
119
|
+
* "add", a project rule with the id of a machine rule in the same list replaces it in its place,
|
|
120
|
+
* taking its "replaces" unless it gives its own, and a project rule that replaces the preset rule
|
|
121
|
+
* a machine rule replaces takes that machine rule's place;
|
|
122
|
+
* - a model entry with the machine's "use", or none, is merged into the machine's entry: "enable"
|
|
123
|
+
* and "disable" by rule id, the project's choice winning, and "add" as above; an entry with
|
|
124
|
+
* another "use" replaces the machine's entry;
|
|
125
|
+
* - a scope keeps its place and takes only the keys the project gives;
|
|
126
|
+
* - routing rows replace the machine's row of the same scope.
|
|
127
|
+
* An entry without "use" for a model the machine does not declare with "preset" or "custom" leaves
|
|
128
|
+
* the model as the machine declares it, and is reported as a note.
|
|
129
|
+
*/
|
|
130
|
+
export function mergeProject(machine, project, allow, baseDir, projectDir, presetId) {
|
|
131
|
+
const problems = [];
|
|
132
|
+
const notes = [];
|
|
133
|
+
const origins = new WeakMap();
|
|
134
|
+
const refuse = (at, what, level) => {
|
|
135
|
+
problems.push(`${at}: ${what} needs "projects": { "allow": "${level}" } in the machine's configuration, which allows "${allow}"`);
|
|
136
|
+
};
|
|
137
|
+
for (const key of Object.keys(project))
|
|
138
|
+
if (key !== 'version' && key !== '$schema' && !KEYS[allow].includes(key))
|
|
139
|
+
refuse(`/${key}`, `"${key}"`, levelFor(key));
|
|
140
|
+
if (allow === 'add')
|
|
141
|
+
for (const [model, entry] of Object.entries(project.models ?? {})) {
|
|
142
|
+
for (const key of Object.keys(entry))
|
|
143
|
+
if (key !== 'add')
|
|
144
|
+
refuse(`/models/${model}/${key}`, `"${key}" for a model`, 'rules');
|
|
145
|
+
(entry.add ?? []).forEach((u, i) => {
|
|
146
|
+
if (u.replaces !== undefined)
|
|
147
|
+
refuse(`/models/${model}/add/${i}/replaces`, '"replaces" for a rule', 'rules');
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
if (problems.length)
|
|
151
|
+
return { raw: machine, problems, notes, origins };
|
|
152
|
+
const replace = allow !== 'add';
|
|
153
|
+
const rules = (base, added, at, current = (id) => id) => {
|
|
154
|
+
const out = [...(base ?? [])];
|
|
155
|
+
(added ?? []).forEach((u, i) => {
|
|
156
|
+
const rule = { ...u, file: path.resolve(baseDir, u.file) };
|
|
157
|
+
if (path.isAbsolute(u.file))
|
|
158
|
+
problems.push(`${at}/${i}/file: "${u.file}" is an absolute path; a project rule file must be relative to the project file`);
|
|
159
|
+
else if (!insideProject(projectDir, rule.file))
|
|
160
|
+
problems.push(`${at}/${i}/file: "${u.file}" is outside the project directory ${projectDir}; a project rule file must stay inside it`);
|
|
161
|
+
origins.set(rule, { at: `${at}/${i}`, file: u.file });
|
|
162
|
+
// Only one of the machine's rules is replaced; a project rule that repeats an id is left for the
|
|
163
|
+
// loader to report, as a repeated id always is. A project rule that takes the place of a machine
|
|
164
|
+
// rule keeps the preset rule that machine rule replaces off, unless it names its own.
|
|
165
|
+
const same = replace ? out.findIndex((r) => r.id === u.id && !origins.has(r)) : -1;
|
|
166
|
+
if (same >= 0 && rule.replaces === undefined && out[same]?.replaces !== undefined)
|
|
167
|
+
rule.replaces = out[same].replaces;
|
|
168
|
+
// A project rule that replaces the preset rule a machine rule replaces takes that machine rule's place.
|
|
169
|
+
const replaced = rule.replaces === undefined ? undefined : current(rule.replaces);
|
|
170
|
+
const rival = replace && replaced !== undefined
|
|
171
|
+
? out.findIndex((r, j) => j !== same && r.replaces !== undefined && current(r.replaces) === replaced && !origins.has(r))
|
|
172
|
+
: -1;
|
|
173
|
+
if (same >= 0)
|
|
174
|
+
out[same] = rule;
|
|
175
|
+
else if (rival >= 0)
|
|
176
|
+
out[rival] = rule;
|
|
177
|
+
else
|
|
178
|
+
out.push(rule);
|
|
179
|
+
if (same >= 0 && rival >= 0)
|
|
180
|
+
out.splice(rival, 1);
|
|
181
|
+
});
|
|
182
|
+
return out;
|
|
183
|
+
};
|
|
184
|
+
const raw = { ...machine };
|
|
185
|
+
if (project.all_models)
|
|
186
|
+
raw.all_models = { add: rules(machine.all_models?.add, project.all_models.add, '/all_models/add') };
|
|
187
|
+
if (project.models) {
|
|
188
|
+
const models = { ...(machine.models ?? {}) };
|
|
189
|
+
for (const [key, entry] of Object.entries(project.models)) {
|
|
190
|
+
const at = `/models/${key}`;
|
|
191
|
+
const mine = machine.models?.[key];
|
|
192
|
+
const current = (id) => presetId(key, id);
|
|
193
|
+
if (entry.use === undefined) {
|
|
194
|
+
if (mine === undefined || mine.use === 'none') {
|
|
195
|
+
notes.push(`${at}: the machine's configuration ${mine === undefined ? `does not declare ${key} with its exact key` : `declares ${key} with "use": "none"`}, ` +
|
|
196
|
+
`so the project's entry for it does not apply here; ${allow === 'add' ? 'declaring the model is for the machine' : 'give the entry a "use" to declare it for this project'}`);
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
models[key] = mergeEntry(mine, entry, mine.use, (list) => rules(mine.add, list, `${at}/add`, current), current);
|
|
200
|
+
}
|
|
201
|
+
else if (mine === undefined || mine.use !== entry.use) {
|
|
202
|
+
models[key] = { ...entry, use: entry.use, ...(entry.add ? { add: rules([], entry.add, `${at}/add`) } : {}) };
|
|
203
|
+
}
|
|
204
|
+
else
|
|
205
|
+
models[key] = mergeEntry(mine, entry, entry.use, (list) => rules(mine.add, list, `${at}/add`, current), current);
|
|
206
|
+
}
|
|
207
|
+
raw.models = models;
|
|
208
|
+
}
|
|
209
|
+
if (project.scopes) {
|
|
210
|
+
const scopes = { ...(machine.scopes ?? {}) };
|
|
211
|
+
for (const [name, scope] of Object.entries(project.scopes))
|
|
212
|
+
scopes[name] = { ...(scopes[name] ?? {}), ...scope };
|
|
213
|
+
raw.scopes = scopes;
|
|
214
|
+
}
|
|
215
|
+
if (project.on_undeclared !== undefined)
|
|
216
|
+
raw.on_undeclared = project.on_undeclared;
|
|
217
|
+
if (project.session)
|
|
218
|
+
raw.session = { ...machine.session, ...project.session };
|
|
219
|
+
if (project.subagents)
|
|
220
|
+
raw.subagents = { ...machine.subagents, ...project.subagents };
|
|
221
|
+
if (project.classifier)
|
|
222
|
+
raw.classifier = { ...machine.classifier, ...project.classifier };
|
|
223
|
+
if (project.routing) {
|
|
224
|
+
const { table, ...rest } = project.routing;
|
|
225
|
+
raw.routing = { ...machine.routing, ...rest, ...(table || machine.routing?.table ? { table: { ...machine.routing?.table, ...table } } : {}) };
|
|
226
|
+
}
|
|
227
|
+
return { raw, problems, notes, origins };
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* A project entry merged into the machine's entry of the same "use", the project's choice winning
|
|
231
|
+
* where it meets one of the machine's "replaces": a project "enable" of a preset rule the machine
|
|
232
|
+
* replaced brings the rule back, and the machine's rule that replaced it stays as a rule of "add";
|
|
233
|
+
* a project "disable" of such a rule changes nothing; a project "replaces" drops the machine's
|
|
234
|
+
* "enable" or "disable" of the rule.
|
|
235
|
+
*/
|
|
236
|
+
function mergeEntry(mine, theirs, use, addRules, current) {
|
|
237
|
+
const replacedBy = (list) => new Set((list ?? []).flatMap((r) => (r.replaces === undefined ? [] : [current(r.replaces)])));
|
|
238
|
+
const projectReplaces = replacedBy(theirs.add);
|
|
239
|
+
const add = addRules(theirs.add);
|
|
240
|
+
const machineReplaces = new Set([...replacedBy(add)].filter((id) => !projectReplaces.has(id)));
|
|
241
|
+
const on = new Set((theirs.enable ?? []).map(current));
|
|
242
|
+
const off = new Set((theirs.disable ?? []).filter((id) => !machineReplaces.has(current(id))));
|
|
243
|
+
const offCurrent = new Set([...off].map(current));
|
|
244
|
+
const enable = unique([...(mine.enable ?? []).filter((id) => !offCurrent.has(current(id)) && !projectReplaces.has(current(id))), ...(theirs.enable ?? [])]);
|
|
245
|
+
const disable = unique([...(mine.disable ?? []).filter((id) => !on.has(current(id)) && !projectReplaces.has(current(id))), ...off]);
|
|
246
|
+
add.forEach((r, i) => {
|
|
247
|
+
if (r.replaces === undefined || !on.has(current(r.replaces)) || projectReplaces.has(current(r.replaces)))
|
|
248
|
+
return;
|
|
249
|
+
if (mine.add?.includes(r)) {
|
|
250
|
+
const { replaces: _replaces, ...rest } = r;
|
|
251
|
+
add[i] = rest;
|
|
252
|
+
}
|
|
253
|
+
else
|
|
254
|
+
delete r.replaces;
|
|
255
|
+
});
|
|
256
|
+
return { use, ...(enable.length ? { enable } : {}), ...(disable.length ? { disable } : {}), ...(add.length || mine.add ? { add } : {}) };
|
|
257
|
+
}
|
|
258
|
+
/** The top-level keys a project file writes, and the models whose declaration it changes, as commands describe it. */
|
|
259
|
+
export function projectKeys(project) {
|
|
260
|
+
const keys = Object.keys(project).filter((k) => k !== 'version' && k !== '$schema' && k !== 'all_models' && k !== 'models');
|
|
261
|
+
const changed = Object.entries(project.models ?? {}).filter(([, e]) => Object.keys(e).some((k) => k !== 'add'));
|
|
262
|
+
return [...keys, ...changed.map(([model]) => `models.${model}`)];
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* The project's rules a merged configuration gives at least one model, as commands describe them:
|
|
266
|
+
* those of "all_models" once each, then each model's own. A rule the project writes for a model the
|
|
267
|
+
* machine leaves out, or for a model that receives no rules, is not one of them.
|
|
268
|
+
*/
|
|
269
|
+
export function projectRules(config) {
|
|
270
|
+
const shared = new Set();
|
|
271
|
+
const own = [];
|
|
272
|
+
for (const [model, entry] of config.exact) {
|
|
273
|
+
if (entry.use !== 'rules')
|
|
274
|
+
continue;
|
|
275
|
+
for (const rule of entry.rules) {
|
|
276
|
+
if (!rule.project)
|
|
277
|
+
continue;
|
|
278
|
+
if (rule.allModels)
|
|
279
|
+
shared.add(`${rule.id} (all_models)`);
|
|
280
|
+
else
|
|
281
|
+
own.push(`${rule.id} (${model})`);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return [...shared, ...own];
|
|
285
|
+
}
|
|
286
|
+
function unique(ids) {
|
|
287
|
+
return [...new Set(ids)];
|
|
288
|
+
}
|
|
289
|
+
function real(file) {
|
|
290
|
+
try {
|
|
291
|
+
return fs.realpathSync(file);
|
|
292
|
+
}
|
|
293
|
+
catch {
|
|
294
|
+
return path.resolve(file);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
function exists(file) {
|
|
298
|
+
try {
|
|
299
|
+
fs.statSync(file);
|
|
300
|
+
return true;
|
|
301
|
+
}
|
|
302
|
+
catch {
|
|
303
|
+
return false;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
function isFile(file) {
|
|
307
|
+
try {
|
|
308
|
+
return fs.statSync(file).isFile();
|
|
309
|
+
}
|
|
310
|
+
catch {
|
|
311
|
+
return false;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* The warning for a project left out that the person can act on: one never trusted, or refused.
|
|
316
|
+
* A project declined with `noonsight untrust`, or ignored by "projects.allow", warns of nothing.
|
|
317
|
+
*/
|
|
318
|
+
export function projectWarning(layer) {
|
|
319
|
+
if (layer === null)
|
|
320
|
+
return null;
|
|
321
|
+
if (layer.status === 'untrusted')
|
|
322
|
+
return (`noonsight: this project has its own noonsight configuration (${layer.file}), which this account has not trusted, ` +
|
|
323
|
+
`so only the machine's configuration applies here. Review it, then run \`noonsight trust\` in ${layer.dir} to apply it, ` +
|
|
324
|
+
'or `noonsight untrust` there to stop this warning.');
|
|
325
|
+
if (layer.status === 'refused')
|
|
326
|
+
return (`noonsight: the project's configuration at ${layer.file} was refused (${layer.problems.length} ${layer.problems.length === 1 ? 'problem' : 'problems'}), ` +
|
|
327
|
+
`so only the machine's configuration applies here. Run \`noonsight check\` in ${layer.dir} to see the problems.`);
|
|
328
|
+
return null;
|
|
329
|
+
}
|
|
330
|
+
/** The decision log's fields for a project: its directory and what became of it. */
|
|
331
|
+
export function projectFields(layer) {
|
|
332
|
+
return layer === null ? {} : { project: layer.dir, project_status: layer.status };
|
|
333
|
+
}
|
|
334
|
+
/** What became of a project, in one line, as `noonsight check`, `models`, `trust` and `report` print it. */
|
|
335
|
+
export function describeProject(layer) {
|
|
336
|
+
const only = `so sessions in ${layer.dir} get only the machine's configuration`;
|
|
337
|
+
switch (layer.status) {
|
|
338
|
+
case 'applied': {
|
|
339
|
+
const keys = layer.raw ? projectKeys(layer.raw) : [];
|
|
340
|
+
return (`applied over the machine's configuration ("projects.allow": "${layer.allow}"), trusted for this account` +
|
|
341
|
+
(layer.rules.length ? `; rules: ${layer.rules.join(', ')}` : '') +
|
|
342
|
+
(keys.length ? `; sets ${keys.join(', ')}` : ''));
|
|
343
|
+
}
|
|
344
|
+
case 'untrusted':
|
|
345
|
+
return `not trusted for this account, ${only}; review it, then run \`noonsight trust\` there`;
|
|
346
|
+
case 'declined':
|
|
347
|
+
return `declined for this account (\`noonsight untrust\`), ${only}; \`noonsight trust\` there applies it`;
|
|
348
|
+
case 'ignored':
|
|
349
|
+
return `ignored, since the machine's configuration sets "projects.allow" to "none"`;
|
|
350
|
+
case 'refused':
|
|
351
|
+
return `refused, ${only}: ${layer.problems.join('; ')}`;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
//# sourceMappingURL=project.js.map
|