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,278 @@
|
|
|
1
|
+
// Example texts for a regular expression: short strings the expression matches, used to show that
|
|
2
|
+
// two scope matchers overlap. Every example is tested against the expression itself before it is
|
|
3
|
+
// returned, so an example is always a real match. Constructs the generator cannot reproduce (back
|
|
4
|
+
// references, lookaround that a sample happens to violate) only cost examples, never correctness.
|
|
5
|
+
/** At most this many examples per expression, and per part of one while they are combined. */
|
|
6
|
+
const MAX_SAMPLES = 24;
|
|
7
|
+
/** Repetition counts above this produce no examples. */
|
|
8
|
+
const MAX_REPEAT = 32;
|
|
9
|
+
/** Characters tried, in order, for a character class or a class escape. */
|
|
10
|
+
const CANDIDATES = ['a', 'e', 's', 'x', '0', '1', ' ', '-', '_', '.', '/', ':', 'A', 'Z', 'é'];
|
|
11
|
+
/**
|
|
12
|
+
* Up to MAX_SAMPLES distinct texts that the compiled expression matches. The pattern must already
|
|
13
|
+
* compile with the given flags. Returns no texts when no example can be built.
|
|
14
|
+
*/
|
|
15
|
+
export function regexSamples(source, flags) {
|
|
16
|
+
const regex = new RegExp(source, flags.replace(/[gy]/g, ''));
|
|
17
|
+
let tree;
|
|
18
|
+
try {
|
|
19
|
+
tree = new Parser(source, flags).parse();
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return { texts: [], complete: false };
|
|
23
|
+
}
|
|
24
|
+
const state = { complete: true };
|
|
25
|
+
const texts = [];
|
|
26
|
+
for (const raw of samplesOf(tree, state)) {
|
|
27
|
+
// Padding satisfies a word boundary at either end.
|
|
28
|
+
const found = [raw, ` ${raw} `].find((t) => regex.test(t));
|
|
29
|
+
if (found === undefined)
|
|
30
|
+
state.complete = false;
|
|
31
|
+
else if (!texts.includes(found))
|
|
32
|
+
texts.push(found);
|
|
33
|
+
}
|
|
34
|
+
return { texts, complete: state.complete };
|
|
35
|
+
}
|
|
36
|
+
class Unsupported extends Error {
|
|
37
|
+
}
|
|
38
|
+
class Parser {
|
|
39
|
+
s;
|
|
40
|
+
flags;
|
|
41
|
+
i = 0;
|
|
42
|
+
constructor(s, flags) {
|
|
43
|
+
this.s = s;
|
|
44
|
+
this.flags = flags;
|
|
45
|
+
}
|
|
46
|
+
parse() {
|
|
47
|
+
const alt = this.alternation();
|
|
48
|
+
if (this.i < this.s.length)
|
|
49
|
+
throw new Unsupported(`unexpected ${this.s[this.i]}`);
|
|
50
|
+
return alt;
|
|
51
|
+
}
|
|
52
|
+
alternation() {
|
|
53
|
+
const branches = [this.sequence()];
|
|
54
|
+
while (this.s[this.i] === '|') {
|
|
55
|
+
this.i++;
|
|
56
|
+
branches.push(this.sequence());
|
|
57
|
+
}
|
|
58
|
+
return { kind: 'alt', branches };
|
|
59
|
+
}
|
|
60
|
+
sequence() {
|
|
61
|
+
const items = [];
|
|
62
|
+
while (this.i < this.s.length && this.s[this.i] !== '|' && this.s[this.i] !== ')') {
|
|
63
|
+
const atom = this.atom();
|
|
64
|
+
items.push(this.quantified(atom));
|
|
65
|
+
}
|
|
66
|
+
return items;
|
|
67
|
+
}
|
|
68
|
+
quantified(atom) {
|
|
69
|
+
const c = this.s[this.i];
|
|
70
|
+
let min;
|
|
71
|
+
let max;
|
|
72
|
+
if (c === '*')
|
|
73
|
+
[min, max] = [0, Infinity];
|
|
74
|
+
else if (c === '+')
|
|
75
|
+
[min, max] = [1, Infinity];
|
|
76
|
+
else if (c === '?')
|
|
77
|
+
[min, max] = [0, 1];
|
|
78
|
+
else if (c === '{') {
|
|
79
|
+
const m = /^\{(\d+)(,(\d*))?\}/.exec(this.s.slice(this.i));
|
|
80
|
+
if (!m)
|
|
81
|
+
return atom;
|
|
82
|
+
min = Number(m[1]);
|
|
83
|
+
max = m[2] === undefined ? min : m[3] === '' ? Infinity : Number(m[3]);
|
|
84
|
+
this.i += m[0].length - 1;
|
|
85
|
+
}
|
|
86
|
+
else
|
|
87
|
+
return atom;
|
|
88
|
+
this.i++;
|
|
89
|
+
if (this.s[this.i] === '?')
|
|
90
|
+
this.i++;
|
|
91
|
+
return { kind: 'repeat', node: atom, min, max };
|
|
92
|
+
}
|
|
93
|
+
atom() {
|
|
94
|
+
const c = this.s[this.i++];
|
|
95
|
+
switch (c) {
|
|
96
|
+
case '^':
|
|
97
|
+
case '$':
|
|
98
|
+
return { kind: 'empty' };
|
|
99
|
+
case '.':
|
|
100
|
+
return this.pick('.');
|
|
101
|
+
case '[':
|
|
102
|
+
return this.characterClass();
|
|
103
|
+
case '(':
|
|
104
|
+
return this.group();
|
|
105
|
+
case '\\':
|
|
106
|
+
return this.escape();
|
|
107
|
+
default:
|
|
108
|
+
return { kind: 'text', value: this.codePointFrom(c) };
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/** The rest of a character whose first UTF-16 unit was already read. */
|
|
112
|
+
codePointFrom(first) {
|
|
113
|
+
const code = first.charCodeAt(0);
|
|
114
|
+
if (code >= 0xd800 && code <= 0xdbff && this.i < this.s.length)
|
|
115
|
+
return first + this.s[this.i++];
|
|
116
|
+
return first;
|
|
117
|
+
}
|
|
118
|
+
group() {
|
|
119
|
+
let negative = false;
|
|
120
|
+
if (this.s[this.i] === '?') {
|
|
121
|
+
const rest = this.s.slice(this.i);
|
|
122
|
+
const m = /^\?(?::|=|!|<=|<!|<[A-Za-z_$][\w$]*>|[ims]*-?[ims]*:)/.exec(rest);
|
|
123
|
+
if (!m)
|
|
124
|
+
throw new Unsupported('group');
|
|
125
|
+
negative = m[0] === '?!' || m[0] === '?<!';
|
|
126
|
+
this.i += m[0].length;
|
|
127
|
+
}
|
|
128
|
+
const inner = this.alternation();
|
|
129
|
+
if (this.s[this.i] !== ')')
|
|
130
|
+
throw new Unsupported('unclosed group');
|
|
131
|
+
this.i++;
|
|
132
|
+
// A positive lookaround contributes the text it looks for, which is where that text must be;
|
|
133
|
+
// a negative one contributes nothing. The final test of each example decides whether it holds.
|
|
134
|
+
return negative ? { kind: 'empty' } : inner;
|
|
135
|
+
}
|
|
136
|
+
escape() {
|
|
137
|
+
const c = this.s[this.i++];
|
|
138
|
+
if (c === undefined)
|
|
139
|
+
throw new Unsupported('trailing backslash');
|
|
140
|
+
switch (c) {
|
|
141
|
+
case 'b':
|
|
142
|
+
case 'B':
|
|
143
|
+
return { kind: 'empty' };
|
|
144
|
+
case 'd':
|
|
145
|
+
case 'D':
|
|
146
|
+
case 'w':
|
|
147
|
+
case 'W':
|
|
148
|
+
case 's':
|
|
149
|
+
case 'S':
|
|
150
|
+
return this.pick(`\\${c}`);
|
|
151
|
+
case 'p':
|
|
152
|
+
case 'P': {
|
|
153
|
+
const m = /^\{[^}]*\}/.exec(this.s.slice(this.i));
|
|
154
|
+
if (!m)
|
|
155
|
+
throw new Unsupported('property escape');
|
|
156
|
+
this.i += m[0].length;
|
|
157
|
+
return this.pick(`\\${c}${m[0]}`);
|
|
158
|
+
}
|
|
159
|
+
case 'k':
|
|
160
|
+
throw new Unsupported('back reference');
|
|
161
|
+
case 'n':
|
|
162
|
+
return { kind: 'text', value: '\n' };
|
|
163
|
+
case 't':
|
|
164
|
+
return { kind: 'text', value: '\t' };
|
|
165
|
+
case 'r':
|
|
166
|
+
return { kind: 'text', value: '\r' };
|
|
167
|
+
case 'f':
|
|
168
|
+
return { kind: 'text', value: '\f' };
|
|
169
|
+
case 'v':
|
|
170
|
+
return { kind: 'text', value: '\v' };
|
|
171
|
+
case '0':
|
|
172
|
+
return { kind: 'text', value: '\0' };
|
|
173
|
+
case 'c': {
|
|
174
|
+
const letter = this.s[this.i++] ?? '';
|
|
175
|
+
return { kind: 'text', value: String.fromCharCode(letter.charCodeAt(0) % 32) };
|
|
176
|
+
}
|
|
177
|
+
case 'x':
|
|
178
|
+
case 'u':
|
|
179
|
+
return { kind: 'text', value: this.hexEscape(c) };
|
|
180
|
+
default:
|
|
181
|
+
if (/[1-9]/.test(c))
|
|
182
|
+
throw new Unsupported('back reference');
|
|
183
|
+
return { kind: 'text', value: this.codePointFrom(c) };
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
hexEscape(kind) {
|
|
187
|
+
const rest = this.s.slice(this.i);
|
|
188
|
+
const m = kind === 'x' ? /^[0-9a-fA-F]{2}/.exec(rest) : (/^\{([0-9a-fA-F]+)\}/.exec(rest) ?? /^[0-9a-fA-F]{4}/.exec(rest));
|
|
189
|
+
if (!m)
|
|
190
|
+
throw new Unsupported('hex escape');
|
|
191
|
+
this.i += m[0].length;
|
|
192
|
+
return String.fromCodePoint(parseInt(m[1] ?? m[0], 16));
|
|
193
|
+
}
|
|
194
|
+
/** A character class: its source is handed to the engine, which keeps every candidate character it accepts. */
|
|
195
|
+
characterClass() {
|
|
196
|
+
const start = this.i - 1;
|
|
197
|
+
let depth = 1;
|
|
198
|
+
while (this.i < this.s.length && depth > 0) {
|
|
199
|
+
const c = this.s[this.i++];
|
|
200
|
+
if (c === '\\')
|
|
201
|
+
this.i++;
|
|
202
|
+
else if (c === '[' && this.flags.includes('v'))
|
|
203
|
+
depth++;
|
|
204
|
+
else if (c === ']')
|
|
205
|
+
depth--;
|
|
206
|
+
}
|
|
207
|
+
if (depth !== 0)
|
|
208
|
+
throw new Unsupported('unclosed class');
|
|
209
|
+
const source = this.s.slice(start, this.i);
|
|
210
|
+
// Include the characters the class names directly, so a class like [qz] still yields an example.
|
|
211
|
+
const named = [...source.slice(1, -1).replace(/\\./g, '')].filter((ch) => /\S/.test(ch) && !'^-[]'.includes(ch));
|
|
212
|
+
return this.pick(source, named);
|
|
213
|
+
}
|
|
214
|
+
pick(classSource, extra = []) {
|
|
215
|
+
const test = new RegExp(`^(?:${classSource})$`, this.flags.replace(/[gy]/g, ''));
|
|
216
|
+
const chars = [...new Set([...CANDIDATES, ...extra])].filter((candidate) => test.test(candidate));
|
|
217
|
+
if (chars.length === 0)
|
|
218
|
+
return { kind: 'none' };
|
|
219
|
+
return { kind: 'alt', branches: chars.map((ch) => [{ kind: 'text', value: ch }]) };
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
/** Adds a text once; a new text that does not fit under MAX_SAMPLES is dropped and noted. */
|
|
223
|
+
function add(out, text, state) {
|
|
224
|
+
if (out.includes(text))
|
|
225
|
+
return;
|
|
226
|
+
if (out.length >= MAX_SAMPLES)
|
|
227
|
+
state.complete = false;
|
|
228
|
+
else
|
|
229
|
+
out.push(text);
|
|
230
|
+
}
|
|
231
|
+
function expand(nodes, state) {
|
|
232
|
+
let acc = [''];
|
|
233
|
+
for (const node of nodes) {
|
|
234
|
+
const options = samplesOf(node, state);
|
|
235
|
+
const next = [];
|
|
236
|
+
for (const a of acc)
|
|
237
|
+
for (const b of options)
|
|
238
|
+
add(next, a + b, state);
|
|
239
|
+
acc = next;
|
|
240
|
+
if (acc.length === 0)
|
|
241
|
+
return [];
|
|
242
|
+
}
|
|
243
|
+
return acc;
|
|
244
|
+
}
|
|
245
|
+
function samplesOf(node, state) {
|
|
246
|
+
switch (node.kind) {
|
|
247
|
+
case 'text':
|
|
248
|
+
return [node.value];
|
|
249
|
+
case 'empty':
|
|
250
|
+
return [''];
|
|
251
|
+
case 'none':
|
|
252
|
+
state.complete = false;
|
|
253
|
+
return [];
|
|
254
|
+
case 'alt': {
|
|
255
|
+
const out = [];
|
|
256
|
+
for (const branch of node.branches)
|
|
257
|
+
for (const s of expand(branch, state))
|
|
258
|
+
add(out, s, state);
|
|
259
|
+
return out;
|
|
260
|
+
}
|
|
261
|
+
case 'repeat': {
|
|
262
|
+
if (node.min > MAX_REPEAT) {
|
|
263
|
+
state.complete = false;
|
|
264
|
+
return [];
|
|
265
|
+
}
|
|
266
|
+
const inner = samplesOf(node.node, state);
|
|
267
|
+
const counts = node.max > node.min ? [node.min, node.min + 1] : [node.min];
|
|
268
|
+
if (node.max > node.min + 1)
|
|
269
|
+
state.complete = false;
|
|
270
|
+
const out = node.min === 0 ? [''] : [];
|
|
271
|
+
for (const k of counts)
|
|
272
|
+
for (const s of inner)
|
|
273
|
+
add(out, s.repeat(k), state);
|
|
274
|
+
return out;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
//# sourceMappingURL=regex-sample.js.map
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { normalizeModel } from './normalize.js';
|
|
3
|
+
import { ANY_MODEL, ANY_SCOPE, SWITCH_NOTE_RESERVE } from './types.js';
|
|
4
|
+
/**
|
|
5
|
+
* Decide which rules a model receives for a scope and a place.
|
|
6
|
+
* Pure: the same configuration and request always give the same decision, text and hash.
|
|
7
|
+
*
|
|
8
|
+
* 1. The model picks the entry: an exact key wins over any wildcard; among wildcards, more
|
|
9
|
+
* literal text wins. No entry means "undeclared".
|
|
10
|
+
* 2. The scope narrows the rules: rules for every scope, plus rules for the current scope, plus
|
|
11
|
+
* rules that list a trigger the session revealed.
|
|
12
|
+
* An unknown scope only means "general rules"; it never blocks and never warns.
|
|
13
|
+
* 3. The place narrows them again.
|
|
14
|
+
*/
|
|
15
|
+
export function resolve(config, request) {
|
|
16
|
+
return decide(config, request, null);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The rules a session receives when it reveals more triggers: the decision for the request's
|
|
20
|
+
* triggers, less every rule the session already received with only the triggers in `held`. A session's
|
|
21
|
+
* scope only grows, and the rules it received stay in its conversation, so only the new ones are sent.
|
|
22
|
+
* No new rule gives the status "none" with the reason "no-rule-applies".
|
|
23
|
+
*/
|
|
24
|
+
export function resolveAdded(config, request, held) {
|
|
25
|
+
return decide(config, request, held);
|
|
26
|
+
}
|
|
27
|
+
function decide(config, request, held) {
|
|
28
|
+
const model = normalizeModel(request.model);
|
|
29
|
+
const askedScope = request.scope ?? null;
|
|
30
|
+
const scope = askedScope !== null && config.scopes.has(askedScope) ? askedScope : null;
|
|
31
|
+
const base = {
|
|
32
|
+
scope,
|
|
33
|
+
...(askedScope !== null && scope === null ? { unknown_scope: askedScope } : {}),
|
|
34
|
+
place: request.place,
|
|
35
|
+
};
|
|
36
|
+
if (!model)
|
|
37
|
+
return undeclared(config, { ...base, model: null, reason: 'model-not-reported' });
|
|
38
|
+
const match = matchModel(config, model);
|
|
39
|
+
if (!match)
|
|
40
|
+
return undeclared(config, { ...base, model, reason: 'no-entry' });
|
|
41
|
+
const { key, entry } = match;
|
|
42
|
+
if (entry.use === 'none')
|
|
43
|
+
return { status: 'none', reason: 'declared-none', model, key, ...base };
|
|
44
|
+
const triggers = request.triggers ?? [];
|
|
45
|
+
let picked = selectRules(entry.rules, scope, request.place, triggers);
|
|
46
|
+
if (held !== null) {
|
|
47
|
+
const before = new Set(selectRules(entry.rules, scope, request.place, held).map((r) => r.id));
|
|
48
|
+
picked = picked.filter((r) => !before.has(r.id));
|
|
49
|
+
}
|
|
50
|
+
if (picked.length === 0)
|
|
51
|
+
return { status: 'none', reason: 'no-rule-applies', model, key, ...base };
|
|
52
|
+
const delivery = deliveryText(model, picked, scope, triggers);
|
|
53
|
+
return {
|
|
54
|
+
status: 'matched',
|
|
55
|
+
model,
|
|
56
|
+
key,
|
|
57
|
+
...base,
|
|
58
|
+
rules: delivery.ids,
|
|
59
|
+
...(delivery.triggered.length ? { triggered: delivery.triggered } : {}),
|
|
60
|
+
sha256: shortHash(joinRules(picked)),
|
|
61
|
+
text: delivery.text,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Whether the scope of a task can change the rules a model receives in a place: some rule the model
|
|
66
|
+
* receives there is for a list of scopes rather than every scope. Without one, every scope gives the
|
|
67
|
+
* same rules.
|
|
68
|
+
*/
|
|
69
|
+
export function scopeChangesRules(config, model, place) {
|
|
70
|
+
const normalized = normalizeModel(model);
|
|
71
|
+
const entry = normalized ? matchModel(config, normalized)?.entry : undefined;
|
|
72
|
+
return entry?.use === 'rules' && entry.rules.some((r) => !r.scopes.includes(ANY_SCOPE) && r.where.includes(place));
|
|
73
|
+
}
|
|
74
|
+
/** Whether a rule applies to a scope by its scopes alone. */
|
|
75
|
+
function inScope(rule, scope) {
|
|
76
|
+
return rule.scopes.includes(ANY_SCOPE) || (scope !== null && rule.scopes.includes(scope));
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* The rules of a rule list that apply to a scope and a place, in declaration order: those for the
|
|
80
|
+
* scope, and those that list one of the triggers.
|
|
81
|
+
*/
|
|
82
|
+
export function selectRules(rules, scope, place, triggers = []) {
|
|
83
|
+
return rules.filter((r) => (inScope(r, scope) || r.triggers.some((t) => triggers.includes(t))) && r.where.includes(place));
|
|
84
|
+
}
|
|
85
|
+
/** The text delivered for these rules, with their ids, and those of them only a trigger brought. */
|
|
86
|
+
export function deliveryText(model, rules, scope, triggers) {
|
|
87
|
+
const ids = rules.map((r) => r.id);
|
|
88
|
+
const triggeredRules = rules.filter((r) => !inScope(r, scope));
|
|
89
|
+
const shown = triggers.filter((t) => triggeredRules.some((r) => r.triggers.includes(t)));
|
|
90
|
+
return { text: envelope(model, scope, ids, joinRules(rules), shown), ids, triggered: triggeredRules.map((r) => r.id) };
|
|
91
|
+
}
|
|
92
|
+
export function joinRules(rules) {
|
|
93
|
+
return rules.map((r) => r.text).join('\n\n');
|
|
94
|
+
}
|
|
95
|
+
/** How the envelope describes a session that revealed a trigger. */
|
|
96
|
+
const TRIGGER_TEXT = {
|
|
97
|
+
'code-edit': "a session that has edited the project's code",
|
|
98
|
+
};
|
|
99
|
+
/**
|
|
100
|
+
* The delivered text. It says where the text came from, written as facts, because Claude Code
|
|
101
|
+
* documents that hook context which reads like a system command can trip prompt-injection defenses.
|
|
102
|
+
* `triggers` are the triggers that brought some of the rules; the envelope names them.
|
|
103
|
+
*/
|
|
104
|
+
export function envelope(model, scope, ruleIds, joined, triggers = []) {
|
|
105
|
+
const scopeAttr = scope !== null ? ` scope="${scope}"` : '';
|
|
106
|
+
const scopeText = scope !== null ? `, task scope "${scope}"` : '';
|
|
107
|
+
const triggerAttr = triggers.length ? ` trigger="${triggers.join(' ')}"` : '';
|
|
108
|
+
const triggerText = triggers.length ? `, for ${triggers.map((t) => TRIGGER_TEXT[t]).join(' and ')}` : '';
|
|
109
|
+
return (`<noonsight-rules model="${model}"${scopeAttr}${triggerAttr}>\n` +
|
|
110
|
+
`Working rules the user declared for ${model}${scopeText}${triggerText} (delivered by noonsight, rules ${ruleIds.join(', ')}):\n` +
|
|
111
|
+
`${joined}\n` +
|
|
112
|
+
`</noonsight-rules>`);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* The note that says which working rules in the conversation no longer apply: those noonsight
|
|
116
|
+
* delivered for every model in `retired`, after the session's model changed from `from` to `to`, or,
|
|
117
|
+
* when `from` is null or `to` itself, while the session runs on `to`. Rules delivered earlier stay in
|
|
118
|
+
* the conversation, so the model is told, as a fact, that they no longer apply. The `from` attribute
|
|
119
|
+
* lists the retired models. Model names are cut to a fixed length, and when the list of retired
|
|
120
|
+
* models would not fit, the note retires the rules of every model but `to`, so it always fits
|
|
121
|
+
* SWITCH_NOTE_RESERVE.
|
|
122
|
+
*/
|
|
123
|
+
export function switchNote(from, to, retired = from === null ? [] : [from]) {
|
|
124
|
+
const target = noteName(to);
|
|
125
|
+
const change = from !== null && from !== to ? `The session's model changed from ${noteName(from)} to ${target}. ` : `This session runs on ${target}. `;
|
|
126
|
+
const build = (attribute, names) => `<noonsight-rules-change from="${attribute}" to="${target}">\n` +
|
|
127
|
+
change +
|
|
128
|
+
`The working rules noonsight delivered earlier in this conversation for ${names} no longer apply.\n` +
|
|
129
|
+
`</noonsight-rules-change>`;
|
|
130
|
+
const names = [...new Set(retired.map(noteName))];
|
|
131
|
+
const every = build(ANY_MODEL, `every model other than ${target}`);
|
|
132
|
+
const listed = retired.includes(ANY_MODEL) ? every : build(names.join(' '), listText(names));
|
|
133
|
+
// The room holds the note and the separator before the rules, with the time a resume stamps on both.
|
|
134
|
+
const fits = (n) => n.length + 2 + 2 * STAMP_LENGTH <= SWITCH_NOTE_RESERVE;
|
|
135
|
+
const note = fits(listed) ? listed : every;
|
|
136
|
+
if (!fits(note))
|
|
137
|
+
throw new Error('switchNote: the note outgrew its reserve');
|
|
138
|
+
return note;
|
|
139
|
+
}
|
|
140
|
+
/** The length of the attribute stampDelivery adds to each opening tag. */
|
|
141
|
+
export const STAMP_LENGTH = ' at="2026-01-01T00:00:00.000Z"'.length;
|
|
142
|
+
/**
|
|
143
|
+
* Mark a delivery with the time it was made, in the opening tag of each envelope and note. Claude
|
|
144
|
+
* Code leaves out a SessionStart context identical to one an earlier SessionStart added to the same
|
|
145
|
+
* conversation, so a resume or a fork that delivers again what a later note retired must not repeat the
|
|
146
|
+
* text of an earlier delivery. `at` is an ISO time, as Date.toISOString writes it.
|
|
147
|
+
*/
|
|
148
|
+
export function stampDelivery(text, at) {
|
|
149
|
+
return text.replace(/<noonsight-rules(?:-change)? [^>]*/g, (tag) => `${tag} at="${at}"`);
|
|
150
|
+
}
|
|
151
|
+
/** "a", "a and b", "a, b and c". */
|
|
152
|
+
function listText(names) {
|
|
153
|
+
return names.length <= 1 ? (names[0] ?? '') : `${names.slice(0, -1).join(', ')} and ${names.at(-1)}`;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* The note delivered when `noonsight run` put one model's rules in the system prompt and the session
|
|
157
|
+
* runs on another model. Like switchNote, it states as a fact that those rules do not apply, and it
|
|
158
|
+
* always fits SWITCH_NOTE_RESERVE.
|
|
159
|
+
*/
|
|
160
|
+
export function launcherNote(promptModel, sessionModel) {
|
|
161
|
+
const from = noteName(promptModel);
|
|
162
|
+
const to = noteName(sessionModel);
|
|
163
|
+
const note = `<noonsight-rules-change from="${from}" to="${to}">\n` +
|
|
164
|
+
`This session runs on ${to}. ` +
|
|
165
|
+
`The working rules for ${from} that noonsight added to the system prompt when it started the session do not apply.\n` +
|
|
166
|
+
`</noonsight-rules-change>`;
|
|
167
|
+
if (note.length + 2 > SWITCH_NOTE_RESERVE)
|
|
168
|
+
throw new Error('launcherNote: the note outgrew its reserve');
|
|
169
|
+
return note;
|
|
170
|
+
}
|
|
171
|
+
/** Joins the model-change note and the new model's rules into one delivery. */
|
|
172
|
+
export function joinDelivery(parts) {
|
|
173
|
+
return parts.join('\n\n');
|
|
174
|
+
}
|
|
175
|
+
const NOTE_NAME_LENGTH = 48;
|
|
176
|
+
function noteName(model) {
|
|
177
|
+
const safe = model.replace(/[^a-z0-9._:@\[\]-]/gi, '?');
|
|
178
|
+
return safe.length > NOTE_NAME_LENGTH ? `${safe.slice(0, NOTE_NAME_LENGTH - 3)}...` : safe || '(not reported)';
|
|
179
|
+
}
|
|
180
|
+
/** Whether the configuration declares a normalized model, by an exact key or a wildcard. */
|
|
181
|
+
export function declares(config, model) {
|
|
182
|
+
return matchModel(config, model) !== null;
|
|
183
|
+
}
|
|
184
|
+
function matchModel(config, model) {
|
|
185
|
+
const exact = config.exact.get(model);
|
|
186
|
+
if (exact)
|
|
187
|
+
return { key: model, entry: exact };
|
|
188
|
+
// Wildcards are pre-sorted by precedence, and they all mean "no rules", so the first hit decides.
|
|
189
|
+
const hit = config.wildcards.find((w) => w.pattern.test(model));
|
|
190
|
+
return hit ? { key: hit.key, entry: { use: 'none' } } : null;
|
|
191
|
+
}
|
|
192
|
+
function undeclared(config, d) {
|
|
193
|
+
const warning = config.onUndeclared === 'warn'
|
|
194
|
+
? `noonsight: model ${d.model ?? '(not reported)'} has no declared rules (${d.reason}, ${d.place})`
|
|
195
|
+
: undefined;
|
|
196
|
+
return { status: 'undeclared', ...d, ...(warning !== undefined ? { warning } : {}) };
|
|
197
|
+
}
|
|
198
|
+
function shortHash(text) {
|
|
199
|
+
return createHash('sha256').update(text, 'utf8').digest('hex').slice(0, 16);
|
|
200
|
+
}
|
|
201
|
+
//# sourceMappingURL=resolve.js.map
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// Routing: from a task's scope to a model and an effort. Pure, like the rest of the core: the adapter
|
|
2
|
+
// passes the environment, what the caller already chose, and how an option's model is written for the
|
|
3
|
+
// target, as data.
|
|
4
|
+
//
|
|
5
|
+
// Routing acts only when it is on, for a target it lists, outside an environment that makes it yield,
|
|
6
|
+
// and where the caller has not already chosen the model. Then the scope's row for the target, else
|
|
7
|
+
// routing.default, lists options in order of preference, and the first whose model is declared under
|
|
8
|
+
// "models" wins. In every other case the call is left as it is, and the decision says why. The scope
|
|
9
|
+
// and the rules never depend on routing: they are decided the same way whether a route applies or not.
|
|
10
|
+
import { declares } from './resolve.js';
|
|
11
|
+
/** The environment variable that turns routing off for a session and everything it starts. */
|
|
12
|
+
export const ROUTING_ENV = 'NOONSIGHT_ROUTING';
|
|
13
|
+
/** NOONSIGHT_ROUTING values that turn routing off. */
|
|
14
|
+
const OFF_VALUES = ['off', '0', 'false', 'no'];
|
|
15
|
+
/** Whether NOONSIGHT_ROUTING turns routing off. */
|
|
16
|
+
export function routingEnvOff(env) {
|
|
17
|
+
return OFF_VALUES.includes((env[ROUTING_ENV] ?? '').trim().toLowerCase());
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Whether routing can act at all for a target in an environment: the decision every task there gets
|
|
21
|
+
* before its scope or its model is looked at, or null when routing may act.
|
|
22
|
+
*/
|
|
23
|
+
export function routingGate(config, target, env) {
|
|
24
|
+
const r = config.routing;
|
|
25
|
+
if (!r.enabled)
|
|
26
|
+
return { routing: 'off', reason: 'disabled' };
|
|
27
|
+
if (!r.targets.includes(target))
|
|
28
|
+
return { routing: 'yielded', reason: 'target-off' };
|
|
29
|
+
if (routingEnvOff(env))
|
|
30
|
+
return { routing: 'yielded', reason: 'routing-env', env: ROUTING_ENV };
|
|
31
|
+
const yieldEnv = r.yieldWhenEnv.find((name) => (env[name] ?? '') !== '');
|
|
32
|
+
if (yieldEnv !== undefined)
|
|
33
|
+
return { routing: 'yielded', reason: 'yield-env', env: yieldEnv };
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
/** Whether a model the call already has keeps routing out. */
|
|
37
|
+
function holds(config, held) {
|
|
38
|
+
return held !== null && (!held.explicit || config.routing.respectExplicit);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Whether routing would read the task's scope for a call: it may act for the target in the
|
|
42
|
+
* environment, and no model the call already has keeps it out.
|
|
43
|
+
*/
|
|
44
|
+
export function routeReadsScope(config, request) {
|
|
45
|
+
return routingGate(config, request.target, request.env) === null && !holds(config, request.held);
|
|
46
|
+
}
|
|
47
|
+
/** Decide the model and effort a task is routed to, or why it is left as it is. */
|
|
48
|
+
export function route(config, request) {
|
|
49
|
+
const r = config.routing;
|
|
50
|
+
const gate = routingGate(config, request.target, request.env);
|
|
51
|
+
if (gate !== null)
|
|
52
|
+
return gate;
|
|
53
|
+
if (request.held !== null && holds(config, request.held))
|
|
54
|
+
return { routing: 'yielded', reason: request.held.reason };
|
|
55
|
+
const fromTable = request.scope !== null ? rowFor(r.table.get(request.scope), request.target) : undefined;
|
|
56
|
+
const fromDefault = rowFor(r.default ?? undefined, request.target);
|
|
57
|
+
const [options, from] = fromTable !== undefined ? [fromTable, 'table'] : [fromDefault, 'default'];
|
|
58
|
+
if (options === undefined)
|
|
59
|
+
return { routing: 'inherit', reason: request.scope === null ? 'no-scope' : 'no-row', skipped: [] };
|
|
60
|
+
if (options.length === 0)
|
|
61
|
+
return { routing: 'inherit', reason: 'empty-row', from, skipped: [] };
|
|
62
|
+
const skipped = [];
|
|
63
|
+
for (const [i, option] of options.entries()) {
|
|
64
|
+
const t = request.translate(option.model);
|
|
65
|
+
if ('why' in t) {
|
|
66
|
+
skipped.push({ model: option.model, why: t.why });
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (!declares(config, t.model)) {
|
|
70
|
+
skipped.push({ model: option.model, why: `${t.model} is not declared under "models"` });
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
routing: 'routed',
|
|
75
|
+
reason: from,
|
|
76
|
+
value: t.value,
|
|
77
|
+
model: t.model,
|
|
78
|
+
...effortOf(option, request.explicitEffort && r.respectExplicit),
|
|
79
|
+
option: i,
|
|
80
|
+
skipped,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
return { routing: 'inherit', reason: 'no-declared-option', from, skipped };
|
|
84
|
+
}
|
|
85
|
+
function rowFor(row, target) {
|
|
86
|
+
return row?.[target];
|
|
87
|
+
}
|
|
88
|
+
function effortOf(option, keep) {
|
|
89
|
+
if (option.effort === undefined)
|
|
90
|
+
return {};
|
|
91
|
+
return keep ? { effortKept: true } : { effort: option.effort };
|
|
92
|
+
}
|
|
93
|
+
//# sourceMappingURL=route.js.map
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// Where a decision's scope and place come from. Pure, like the rest of the core: the adapters pass
|
|
2
|
+
// the environment and the task text in as data.
|
|
3
|
+
//
|
|
4
|
+
// Session scope: NOONSIGHT_SCOPE, when whoever starts the session exports it; else none.
|
|
5
|
+
// place: unattended when a variable named in session.unattended_when_env is set, or when
|
|
6
|
+
// Claude Code runs headless (claude -p, the Agent SDK) and session.headless_is_unattended
|
|
7
|
+
// is on, as it is by default; else attended.
|
|
8
|
+
// Subagent scope: the scope whose matcher matches earliest in the task text; else none. Never
|
|
9
|
+
// inherited from the session.
|
|
10
|
+
//
|
|
11
|
+
// With the classifier on, a task without an explicit scope may take its scope from the classifier
|
|
12
|
+
// instead (classify.ts).
|
|
13
|
+
// place: always subagent.
|
|
14
|
+
/** The environment variable that names a session's task scope. */
|
|
15
|
+
export const SCOPE_ENV = 'NOONSIGHT_SCOPE';
|
|
16
|
+
/**
|
|
17
|
+
* How much of a subagent's task text the matchers read. The matchers run inside the hook before every
|
|
18
|
+
* agent call, and a matcher's time grows with the text it reads, so the bound keeps that time limited.
|
|
19
|
+
*/
|
|
20
|
+
export const TASK_TEXT_LIMIT = 100_000;
|
|
21
|
+
/**
|
|
22
|
+
* The variable Claude Code sets in the environment of a session's hooks to "0" when nobody can answer the
|
|
23
|
+
* session mid-turn: a headless run, such as `claude -p` or the Agent SDK, or a background session. An
|
|
24
|
+
* interactive session, in the terminal, an IDE, the desktop app or remotely, gets "1". Claude Code does
|
|
25
|
+
* not document it, so a version that renames or drops it leaves every session in the place it had
|
|
26
|
+
* before this signal was read: anything other than "0" is not headless.
|
|
27
|
+
*/
|
|
28
|
+
export const ATTENDED_ENV = 'CLAUDE_CODE_SESSION_ATTENDED';
|
|
29
|
+
const NO_SCOPE = { scope: null, source: null };
|
|
30
|
+
/** A session's scope: NOONSIGHT_SCOPE, trimmed; empty or unset means none. */
|
|
31
|
+
export function sessionScope(env) {
|
|
32
|
+
const value = (env[SCOPE_ENV] ?? '').trim();
|
|
33
|
+
return value ? { scope: value, source: 'env' } : NO_SCOPE;
|
|
34
|
+
}
|
|
35
|
+
/** Whether Claude Code marked the environment as a headless run's: ATTENDED_ENV is "0", and nothing else. */
|
|
36
|
+
export function headlessEnv(env) {
|
|
37
|
+
return env[ATTENDED_ENV] === '0';
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* A session's place: unattended when any variable in session.unattended_when_env is set and not empty,
|
|
41
|
+
* which is named first, or when the session runs headless and session.headless_is_unattended is on;
|
|
42
|
+
* else attended. Whether it runs headless is read from the environment its hooks get, unless the caller
|
|
43
|
+
* knows better, as the launcher does for the session it is about to start.
|
|
44
|
+
*/
|
|
45
|
+
export function sessionPlace(config, env, headless = headlessEnv(env)) {
|
|
46
|
+
const name = config.session.unattendedWhenEnv.find((n) => (env[n] ?? '') !== '');
|
|
47
|
+
if (name !== undefined)
|
|
48
|
+
return { place: 'unattended', because: 'env', env: name };
|
|
49
|
+
return headless && config.session.headlessIsUnattended ? { place: 'unattended', because: 'headless' } : { place: 'attended' };
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The scope whose matcher matches earliest in a subagent's task text. Two scopes that match at the
|
|
53
|
+
* same place go to the one declared first: the preset's scopes before the user's.
|
|
54
|
+
*/
|
|
55
|
+
export function taskScope(config, text) {
|
|
56
|
+
let best = null;
|
|
57
|
+
for (const scope of config.scopes.values())
|
|
58
|
+
for (const m of scope.match) {
|
|
59
|
+
const at = text.search(m.regex);
|
|
60
|
+
if (at >= 0 && (best === null || at < best.at))
|
|
61
|
+
best = { scope: scope.name, matcher: m.source, at };
|
|
62
|
+
}
|
|
63
|
+
return best === null ? NO_SCOPE : { scope: best.scope, source: 'task-text', matcher: best.matcher };
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The text the matchers read: the description, then the task text, without any noonsight envelope a
|
|
67
|
+
* parent copied into it (the rules' own words must not pick the scope), cut to TASK_TEXT_LIMIT.
|
|
68
|
+
*/
|
|
69
|
+
export function taskText(description, prompt) {
|
|
70
|
+
const strip = (s) => s.replace(/<noonsight-rules(-change)?\b[^>]*>[\s\S]*?<\/noonsight-rules\1>/g, '');
|
|
71
|
+
const whole = [description, prompt]
|
|
72
|
+
.filter((s) => s !== null && s !== '')
|
|
73
|
+
.map(strip)
|
|
74
|
+
.join('\n\n');
|
|
75
|
+
return { text: whole.slice(0, TASK_TEXT_LIMIT), cut: whole.length > TASK_TEXT_LIMIT };
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=scope.js.map
|