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.
- package/.claude-plugin/marketplace.json +19 -0
- package/.claude-plugin/plugin.json +14 -0
- package/LICENSE +21 -0
- package/README.md +281 -2
- 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 +60 -4
- 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,88 @@
|
|
|
1
|
+
// The Claude Code hook adapter for triggers: PostToolUse on Edit and Write.
|
|
2
|
+
//
|
|
3
|
+
// A session whose scope nobody named still shows what kind of task it is on by what it does. When the
|
|
4
|
+
// session first edits a code file the project's git already tracks (code-edit.ts), it is building, so
|
|
5
|
+
// the rules that list the code-edit trigger are delivered with that edit, as additionalContext, which
|
|
6
|
+
// Claude Code adds to the conversation right after the tool's result.
|
|
7
|
+
//
|
|
8
|
+
// - Only the session's own edits count. An edit inside a subagent carries the subagent's agent_id and
|
|
9
|
+
// is left alone: a subagent's scope comes from its own task, and its rules from its start.
|
|
10
|
+
// - A session reveals each trigger once. The trigger's record is created once, so of several edits
|
|
11
|
+
// running their hooks at once exactly one delivers, and every later edit costs one file lookup.
|
|
12
|
+
// - Only the rules the trigger adds are delivered: the session's conversation already holds the rest.
|
|
13
|
+
// - The session's record is updated with the rules it now holds, so a resume does not deliver them
|
|
14
|
+
// again, and SessionStart after compaction and PostModelSwitch resolve with the triggers revealed.
|
|
15
|
+
import { resolve, resolveAdded } from '../../core/resolve.js';
|
|
16
|
+
import { sessionPlace, sessionScope } from '../../core/scope.js';
|
|
17
|
+
import { hasCodeExtension } from '../../core/triggers.js';
|
|
18
|
+
import { TRIGGERS } from '../../core/types.js';
|
|
19
|
+
import { claimTrigger, sessionTriggers } from '../../state/triggers.js';
|
|
20
|
+
import { codeEdit, namedFile } from './code-edit.js';
|
|
21
|
+
import { choiceFields, decisionFields, sessionModel } from './subagents.js';
|
|
22
|
+
/**
|
|
23
|
+
* Whether an edit could reveal a trigger, from what costs no configuration and no process: an edit of
|
|
24
|
+
* the session itself, of a code file that is there, in a session that has not revealed every trigger.
|
|
25
|
+
* Every edit runs the hook, so the hook reads the configuration only past this check.
|
|
26
|
+
*/
|
|
27
|
+
export function mayReveal(input, state) {
|
|
28
|
+
if (str(input.agent_id) !== null)
|
|
29
|
+
return false;
|
|
30
|
+
if (sessionTriggers(state, str(input.session_id)).length === TRIGGERS.length)
|
|
31
|
+
return false;
|
|
32
|
+
const file = namedFile(input);
|
|
33
|
+
return file !== null && hasCodeExtension(file);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* PostToolUse on Edit and Write. Returns null, with nothing to log, for an edit that reveals no new
|
|
37
|
+
* trigger: one inside a subagent, a trigger no rule lists, a
|
|
38
|
+
* trigger the session already revealed, or an edit that is not a code edit.
|
|
39
|
+
*/
|
|
40
|
+
export function fileEdit(input, config, ctx) {
|
|
41
|
+
const trigger = 'code-edit';
|
|
42
|
+
if (str(input.agent_id) !== null)
|
|
43
|
+
return null;
|
|
44
|
+
const sessionId = str(input.session_id);
|
|
45
|
+
if (sessionId === null || !listsTrigger(config, trigger))
|
|
46
|
+
return null;
|
|
47
|
+
const held = sessionTriggers(ctx.state, sessionId);
|
|
48
|
+
if (held.includes(trigger))
|
|
49
|
+
return null;
|
|
50
|
+
const edit = codeEdit(input, ctx.env);
|
|
51
|
+
if (edit.kind !== 'code')
|
|
52
|
+
return null;
|
|
53
|
+
if (!claimTrigger(ctx.state, sessionId, { trigger, tool: edit.tool, file: edit.file, at: ctx.now.toISOString() }))
|
|
54
|
+
return null;
|
|
55
|
+
const model = sessionModel(input, ctx);
|
|
56
|
+
const scope = sessionScope(ctx.env);
|
|
57
|
+
const place = sessionPlace(config, ctx.env);
|
|
58
|
+
const triggers = TRIGGERS.filter((t) => t === trigger || held.includes(t));
|
|
59
|
+
const request = { model: model.model, scope: scope.scope, place: place.place, triggers };
|
|
60
|
+
const added = resolveAdded(config, request, held);
|
|
61
|
+
const whole = resolve(config, request);
|
|
62
|
+
return {
|
|
63
|
+
record: {
|
|
64
|
+
trigger,
|
|
65
|
+
file: edit.file,
|
|
66
|
+
...decisionFields(added),
|
|
67
|
+
...choiceFields(scope, place),
|
|
68
|
+
model_source: model.source,
|
|
69
|
+
triggers,
|
|
70
|
+
delivered: added.status === 'matched' ? 'rules' : 'nothing',
|
|
71
|
+
// The session's start already warned about an undeclared or unknown model.
|
|
72
|
+
warned: false,
|
|
73
|
+
},
|
|
74
|
+
context: added.status === 'matched' ? added.text : null,
|
|
75
|
+
state: whole.model === null ? null : { model: whole.model, status: whole.status, sha256: whole.status === 'matched' ? whole.sha256 : null },
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
/** Whether any rule of any declared model lists the trigger. */
|
|
79
|
+
function listsTrigger(config, trigger) {
|
|
80
|
+
for (const entry of config.exact.values())
|
|
81
|
+
if (entry.use === 'rules' && entry.rules.some((r) => r.triggers.includes(trigger)))
|
|
82
|
+
return true;
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
function str(value) {
|
|
86
|
+
return typeof value === 'string' && value !== '' ? value : null;
|
|
87
|
+
}
|
|
88
|
+
//# sourceMappingURL=triggers.js.map
|
package/dist/src/cli.js
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// noonsight command line. Exit codes: 0 for success and for every decision (matched, none,
|
|
3
|
+
// undeclared); 1 when install, uninstall, watch or check finds a problem; 2 for a usage error or a
|
|
4
|
+
// refused configuration. `noonsight init` exits 1 when the configuration file already exists, and
|
|
5
|
+
// `noonsight accept` when it cannot write it, and `noonsight trust` and `untrust` when they find no
|
|
6
|
+
// project configuration or cannot record the decision.
|
|
7
|
+
// `noonsight hook` always exits 0, `noonsight watch run` keeps running, and `noonsight run` exits with
|
|
8
|
+
// claude's exit status.
|
|
9
|
+
import fs from 'node:fs';
|
|
10
|
+
import { fileURLToPath } from 'node:url';
|
|
11
|
+
import { ACCEPT_USAGE, runAccept } from './commands/accept.js';
|
|
12
|
+
import { CHECK_USAGE, runCheck } from './commands/check.js';
|
|
13
|
+
import { HOOK_USAGE, runHookCommand } from './commands/hook.js';
|
|
14
|
+
import { INIT_USAGE, runInit } from './commands/init.js';
|
|
15
|
+
import { INSTALL_USAGE, runInstall } from './commands/install.js';
|
|
16
|
+
import { MODELS_USAGE, runModels } from './commands/models.js';
|
|
17
|
+
import { REPORT_USAGE, runReport } from './commands/report.js';
|
|
18
|
+
import { RESOLVE_USAGE, runResolve } from './commands/resolve.js';
|
|
19
|
+
import { runRun } from './commands/run.js';
|
|
20
|
+
import { runTrust, TRUST_USAGE } from './commands/trust.js';
|
|
21
|
+
import { UsageError } from './commands/usage.js';
|
|
22
|
+
import { runWatch, WATCH_USAGE } from './commands/watch.js';
|
|
23
|
+
import { ConfigError } from './config/load.js';
|
|
24
|
+
import { runsFromPlugin } from './adapters/claude-code/plugins.js';
|
|
25
|
+
import { packageVersion } from './version.js';
|
|
26
|
+
import { forPlugin } from './wording.js';
|
|
27
|
+
const USAGE = `usage: noonsight <command>
|
|
28
|
+
|
|
29
|
+
Commands:
|
|
30
|
+
init create the configuration file from the preset (install does it too)
|
|
31
|
+
install [--account <dir>|--managed]
|
|
32
|
+
set noonsight up: write the configuration if there is none,
|
|
33
|
+
install the Claude Code hooks (by default for every login of this
|
|
34
|
+
account, or one directory, or machine-wide), then check
|
|
35
|
+
uninstall [--account <dir>|--managed]
|
|
36
|
+
remove them
|
|
37
|
+
watch enable|disable turn on or off the watcher that installs the hooks into each
|
|
38
|
+
new Claude Code login as it appears; off unless turned on
|
|
39
|
+
check check the configuration, the installs, the watcher and Claude Code
|
|
40
|
+
accept accept the installed preset version: list what it changes, then
|
|
41
|
+
record it as "preset_version" in the configuration
|
|
42
|
+
trust [<dir>] apply the project configuration (.noonsight/noonsight.json) found
|
|
43
|
+
from the directory to the sessions that run in that project
|
|
44
|
+
untrust [<dir>] stop applying it
|
|
45
|
+
models [--json] list the declared models and their rules, with source and cost
|
|
46
|
+
resolve <model> [scope] [place] show the rules a model receives
|
|
47
|
+
report [--json] [--log <file>]
|
|
48
|
+
report from the decision log how often each rule was delivered,
|
|
49
|
+
and to which models, scopes and places
|
|
50
|
+
run [--scope <scope>] [--] [claude arguments...]
|
|
51
|
+
start claude with the rules for its model in the system prompt
|
|
52
|
+
hook <event> the hook adapter that Claude Code runs; not run by hand
|
|
53
|
+
|
|
54
|
+
Options:
|
|
55
|
+
--help show this help
|
|
56
|
+
--version print the version
|
|
57
|
+
|
|
58
|
+
The configuration is read from $NOONSIGHT_CONFIG, else ~/.config/noonsight/noonsight.json. One file serves every Claude Code account on the machine.
|
|
59
|
+
A project can commit its own, .noonsight/noonsight.json, laid over it for the sessions that run in the project once trusted.`;
|
|
60
|
+
const COMMAND_USAGE = {
|
|
61
|
+
init: INIT_USAGE,
|
|
62
|
+
models: MODELS_USAGE,
|
|
63
|
+
resolve: RESOLVE_USAGE,
|
|
64
|
+
report: REPORT_USAGE,
|
|
65
|
+
install: INSTALL_USAGE,
|
|
66
|
+
uninstall: INSTALL_USAGE,
|
|
67
|
+
check: CHECK_USAGE,
|
|
68
|
+
accept: ACCEPT_USAGE,
|
|
69
|
+
trust: TRUST_USAGE,
|
|
70
|
+
untrust: TRUST_USAGE,
|
|
71
|
+
watch: WATCH_USAGE,
|
|
72
|
+
hook: HOOK_USAGE,
|
|
73
|
+
};
|
|
74
|
+
/** The commands whose whole output is data, rule text included, which is never reworded. */
|
|
75
|
+
const DATA_COMMANDS = new Set(['hook', 'resolve', 'run']);
|
|
76
|
+
export function main(argv, env, stdio) {
|
|
77
|
+
const [command, ...rest] = argv;
|
|
78
|
+
const io = command !== undefined && !DATA_COMMANDS.has(command) && !rest.includes('--json') && runsFromPlugin(env)
|
|
79
|
+
? { out: (t) => stdio.out(forPlugin(t)), err: (t) => stdio.err(forPlugin(t)) }
|
|
80
|
+
: stdio;
|
|
81
|
+
// The hook never prints usage or fails: Claude Code would show it as a hook error.
|
|
82
|
+
if (command === 'hook')
|
|
83
|
+
return runHookCommand(rest, env, io.out);
|
|
84
|
+
try {
|
|
85
|
+
// Its arguments are claude's, so it reads --help itself, only among its own options.
|
|
86
|
+
if (command === 'run')
|
|
87
|
+
return runRun(rest, env, io).catch((e) => handleError(e, io));
|
|
88
|
+
const usage = command !== undefined ? COMMAND_USAGE[command] : undefined;
|
|
89
|
+
if (usage !== undefined && (rest.includes('--help') || rest.includes('-h'))) {
|
|
90
|
+
io.out(usage);
|
|
91
|
+
return 0;
|
|
92
|
+
}
|
|
93
|
+
switch (command) {
|
|
94
|
+
case '--version':
|
|
95
|
+
case '-v':
|
|
96
|
+
io.out(packageVersion());
|
|
97
|
+
return 0;
|
|
98
|
+
case '--help':
|
|
99
|
+
case '-h':
|
|
100
|
+
case 'help':
|
|
101
|
+
io.out(USAGE);
|
|
102
|
+
return 0;
|
|
103
|
+
case 'init':
|
|
104
|
+
return runInit(rest, env, io);
|
|
105
|
+
case 'models':
|
|
106
|
+
return runModels(rest, env, io.out);
|
|
107
|
+
case 'resolve':
|
|
108
|
+
io.out(JSON.stringify(runResolve(rest, env), null, 2));
|
|
109
|
+
return 0;
|
|
110
|
+
case 'report':
|
|
111
|
+
return runReport(rest, env, io.out);
|
|
112
|
+
case 'install':
|
|
113
|
+
case 'uninstall':
|
|
114
|
+
return runInstall(command, rest, env, io);
|
|
115
|
+
case 'check':
|
|
116
|
+
return runCheck(rest, env, io.out);
|
|
117
|
+
case 'accept':
|
|
118
|
+
return runAccept(rest, env, io);
|
|
119
|
+
case 'trust':
|
|
120
|
+
case 'untrust':
|
|
121
|
+
return runTrust(command, rest, env, io);
|
|
122
|
+
case 'watch':
|
|
123
|
+
return runWatch(rest, env, io);
|
|
124
|
+
case undefined:
|
|
125
|
+
throw new UsageError('missing the command', USAGE);
|
|
126
|
+
default:
|
|
127
|
+
throw new UsageError(`unknown command "${command}"`, USAGE);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
catch (e) {
|
|
131
|
+
return handleError(e, io);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
function handleError(e, io) {
|
|
135
|
+
if (e instanceof UsageError) {
|
|
136
|
+
io.err(`noonsight: ${e.message}\n\n${e.usage}`);
|
|
137
|
+
return 2;
|
|
138
|
+
}
|
|
139
|
+
if (e instanceof ConfigError) {
|
|
140
|
+
io.err(`noonsight: ${e.message}\nNothing from this configuration was applied.`);
|
|
141
|
+
return 2;
|
|
142
|
+
}
|
|
143
|
+
throw e;
|
|
144
|
+
}
|
|
145
|
+
if (process.argv[1] && fs.realpathSync(process.argv[1]) === fileURLToPath(import.meta.url)) {
|
|
146
|
+
const code = main(process.argv.slice(2), process.env, {
|
|
147
|
+
out: (t) => process.stdout.write(`${t}\n`),
|
|
148
|
+
err: (t) => process.stderr.write(`${t}\n`),
|
|
149
|
+
});
|
|
150
|
+
if (typeof code === 'number')
|
|
151
|
+
process.exitCode = code;
|
|
152
|
+
else
|
|
153
|
+
void code.then((c) => {
|
|
154
|
+
process.exitCode = c;
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
//# sourceMappingURL=cli.js.map
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// noonsight accept: record the installed preset version as the one the configuration accepted. It
|
|
2
|
+
// lists what that version changes since the accepted one, checks that the configuration loads with
|
|
3
|
+
// it, and rewrites only the "preset_version" value of the file, keeping its formatting.
|
|
4
|
+
import fs from 'node:fs';
|
|
5
|
+
import { ConfigError, DEFAULT_PRESET, loadConfigDetails, readPreset } from '../config/load.js';
|
|
6
|
+
import { configPath } from '../config/paths.js';
|
|
7
|
+
import { comparePresets, describeChanges } from '../config/preset-changes.js';
|
|
8
|
+
import { setTopLevelString } from '../config/rewrite.js';
|
|
9
|
+
import { isPermissionError, modeOf, writeFileAtomic } from '../state/files.js';
|
|
10
|
+
import { UsageError } from './usage.js';
|
|
11
|
+
export const ACCEPT_USAGE = `usage: noonsight accept
|
|
12
|
+
|
|
13
|
+
Accept the installed preset version: list what it changes since the version the configuration
|
|
14
|
+
accepted ("preset_version"), then write the installed version there. Only that value of the file
|
|
15
|
+
changes; the rest of the file stays as it is. The configuration must load with the installed version,
|
|
16
|
+
so an enable, disable or replaces entry that names a rule the installed preset no longer has is
|
|
17
|
+
removed first.
|
|
18
|
+
Exits 0 when the version was written or already was the installed one, 1 when the file cannot be
|
|
19
|
+
written, and 2 for a usage error, a refused configuration, or a preset that is not installed.`;
|
|
20
|
+
export function runAccept(args, env, io) {
|
|
21
|
+
if (args.length)
|
|
22
|
+
throw new UsageError(`unexpected argument "${args[0]}"`, ACCEPT_USAGE);
|
|
23
|
+
const file = configPath(env);
|
|
24
|
+
let text;
|
|
25
|
+
try {
|
|
26
|
+
text = fs.readFileSync(file, 'utf8');
|
|
27
|
+
}
|
|
28
|
+
catch (e) {
|
|
29
|
+
const code = e.code;
|
|
30
|
+
if (code !== 'ENOENT')
|
|
31
|
+
throw e;
|
|
32
|
+
io.err(`noonsight: there is no configuration file at ${file}; \`noonsight install\` writes one`);
|
|
33
|
+
return 2;
|
|
34
|
+
}
|
|
35
|
+
let raw;
|
|
36
|
+
try {
|
|
37
|
+
raw = JSON.parse(text);
|
|
38
|
+
}
|
|
39
|
+
catch (e) {
|
|
40
|
+
throw new ConfigError(file, [`not valid JSON: ${e.message}`]);
|
|
41
|
+
}
|
|
42
|
+
if (typeof raw !== 'object' || raw === null || Array.isArray(raw))
|
|
43
|
+
throw new ConfigError(file, ['the file is not a JSON object']);
|
|
44
|
+
const name = typeof raw.preset === 'string' ? raw.preset : DEFAULT_PRESET;
|
|
45
|
+
const installed = readPreset(name, {}, env);
|
|
46
|
+
const to = installed.version;
|
|
47
|
+
const accepted = typeof raw.preset_version === 'string' ? raw.preset_version : null;
|
|
48
|
+
let next;
|
|
49
|
+
try {
|
|
50
|
+
next = accepted === to ? text : setTopLevelString(text, 'preset_version', to, ['preset', 'version']);
|
|
51
|
+
}
|
|
52
|
+
catch (e) {
|
|
53
|
+
io.err(`noonsight: ${file}: ${e.message}; set "preset_version" to "${to}" by hand`);
|
|
54
|
+
return 1;
|
|
55
|
+
}
|
|
56
|
+
let details;
|
|
57
|
+
try {
|
|
58
|
+
details = loadConfigDetails(file, { content: next }, env);
|
|
59
|
+
}
|
|
60
|
+
catch (e) {
|
|
61
|
+
if (!(e instanceof ConfigError))
|
|
62
|
+
throw e;
|
|
63
|
+
io.err(`noonsight: with preset ${name} ${to}, ${e.message}\n` +
|
|
64
|
+
`Nothing was written. Fix these, then run \`noonsight accept\` again; \`noonsight check\` lists what ${to} changes.`);
|
|
65
|
+
return 2;
|
|
66
|
+
}
|
|
67
|
+
// What changes, read as the configuration stood: from the version it accepted, delivered as it is now.
|
|
68
|
+
let comparison = null;
|
|
69
|
+
let copyProblem = null;
|
|
70
|
+
try {
|
|
71
|
+
comparison = comparePresets({ ...details.presetVersions, accepted, onChange: 'warn' }, details.declared, {}, env);
|
|
72
|
+
}
|
|
73
|
+
catch (e) {
|
|
74
|
+
if (!(e instanceof ConfigError))
|
|
75
|
+
throw e;
|
|
76
|
+
copyProblem = e.problems.join('; ');
|
|
77
|
+
}
|
|
78
|
+
const lines = [];
|
|
79
|
+
if (accepted === to) {
|
|
80
|
+
lines.push(`${file}: preset ${name} ${to} is already the accepted version; nothing to change`);
|
|
81
|
+
for (const m of comparison?.models ?? [])
|
|
82
|
+
lines.push(` ${m.model}: ${describeChanges(m)}`);
|
|
83
|
+
io.out(lines.join('\n'));
|
|
84
|
+
return 0;
|
|
85
|
+
}
|
|
86
|
+
lines.push(`preset ${name} ${to}, accepted until now: ${accepted ?? 'not recorded'}`);
|
|
87
|
+
if (accepted === null)
|
|
88
|
+
lines.push(` ${file} records no version, so nothing says what changed; \`noonsight models\` lists the rules of ${to}`);
|
|
89
|
+
else if (copyProblem !== null)
|
|
90
|
+
lines.push(` the copy of ${accepted} the package keeps was refused, so what changed cannot be listed: ${copyProblem}`);
|
|
91
|
+
else if (comparison?.from === null)
|
|
92
|
+
lines.push(` the package keeps no copy of ${accepted}, so what changed cannot be listed; \`noonsight models\` lists the rules of ${to}`);
|
|
93
|
+
else if (comparison?.models.length === 0)
|
|
94
|
+
lines.push(' no rule a declared model receives changes');
|
|
95
|
+
else
|
|
96
|
+
for (const m of comparison?.models ?? [])
|
|
97
|
+
lines.push(` ${m.model}: ${describeChanges(m)}`);
|
|
98
|
+
// Written through a symbolic link, so a configuration kept elsewhere (dotfiles) stays a link.
|
|
99
|
+
const target = fs.realpathSync(file);
|
|
100
|
+
try {
|
|
101
|
+
writeFileAtomic(target, next, modeOf(target, 0o644));
|
|
102
|
+
}
|
|
103
|
+
catch (e) {
|
|
104
|
+
if (!isPermissionError(e))
|
|
105
|
+
throw e;
|
|
106
|
+
io.out(lines.join('\n'));
|
|
107
|
+
io.err(`noonsight: cannot write ${target} (${e.code}); set "preset_version" to "${to}" by hand`);
|
|
108
|
+
return 1;
|
|
109
|
+
}
|
|
110
|
+
lines.push(`wrote "preset_version": "${to}" to ${file}` +
|
|
111
|
+
(details.presetVersions.onChange === 'hold' && accepted !== null ? `; sessions now receive the rules of ${to}` : ''));
|
|
112
|
+
io.out(lines.join('\n'));
|
|
113
|
+
return 0;
|
|
114
|
+
}
|
|
115
|
+
//# sourceMappingURL=accept.js.map
|