@countinghouse/ch-local 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +361 -2
- package/bin/ch-local.js +472 -0
- package/clients/claude-code/.claude-plugin/plugin.json +6 -0
- package/clients/claude-code/.mcp.json +11 -0
- package/clients/claude-code/bin/launch.js +235 -0
- package/clients/claude-code/bin/win-acl.js +420 -0
- package/clients/claude-code/skills/ci-failure-triage/SKILL.md +21 -0
- package/clients/claude-code/skills/countinghouse-module/SKILL.md +177 -0
- package/clients/claude-code/skills/countinghouse-setup/SKILL.md +74 -0
- package/lib/allowlist.js +65 -0
- package/lib/aside.js +16 -0
- package/lib/atomic.js +45 -0
- package/lib/auto-apply.js +245 -0
- package/lib/auto-upgrade.js +98 -0
- package/lib/availability.js +307 -0
- package/lib/ceiling.js +280 -0
- package/lib/clean-text.js +20 -0
- package/lib/cli-change.js +38 -0
- package/lib/client-conditions-fixtures.js +143 -0
- package/lib/client-conditions.js +656 -0
- package/lib/clients.js +179 -0
- package/lib/command-line.js +202 -0
- package/lib/conditions-command.js +116 -0
- package/lib/control.js +134 -0
- package/lib/credential-store.js +414 -0
- package/lib/credentials-command.js +344 -0
- package/lib/daemon-requests.js +137 -0
- package/lib/daemon.js +888 -0
- package/lib/describe-module.js +137 -0
- package/lib/description-check.js +175 -0
- package/lib/failed-json.js +43 -0
- package/lib/gc.js +274 -0
- package/lib/github-token.js +57 -0
- package/lib/handoff-io.js +73 -0
- package/lib/held-back.js +116 -0
- package/lib/hidden-input.js +64 -0
- package/lib/history.js +49 -0
- package/lib/home.js +372 -0
- package/lib/in-place.js +1036 -0
- package/lib/install-plan.js +142 -0
- package/lib/install.js +846 -0
- package/lib/installed-description.js +153 -0
- package/lib/integrity.js +7 -0
- package/lib/lock.js +456 -0
- package/lib/lockfile-rules.js +98 -0
- package/lib/machine.js +20 -0
- package/lib/module-credentials.js +403 -0
- package/lib/module-record.js +298 -0
- package/lib/module-roles.js +44 -0
- package/lib/module-seams.js +26 -0
- package/lib/module-store.js +202 -0
- package/lib/module-values.js +334 -0
- package/lib/names.js +31 -0
- package/lib/next-steps.js +8 -0
- package/lib/node-check.js +56 -0
- package/lib/npm-fetch.js +123 -0
- package/lib/npm-release.js +99 -0
- package/lib/npm-upgrade.js +146 -0
- package/lib/npm.js +41 -0
- package/lib/outside-edits.js +162 -0
- package/lib/own-applied.js +175 -0
- package/lib/own-command.js +380 -0
- package/lib/own-file.js +120 -0
- package/lib/own-record.js +96 -0
- package/lib/own-tree.js +336 -0
- package/lib/pack-table.js +290 -0
- package/lib/packs-command.js +570 -0
- package/lib/packs.js +219 -0
- package/lib/pins.js +64 -0
- package/lib/pipe-probe.js +82 -0
- package/lib/plain-read.js +63 -0
- package/lib/private-file.js +90 -0
- package/lib/private-write.js +33 -0
- package/lib/provenance.js +234 -0
- package/lib/record.js +82 -0
- package/lib/registry.js +22 -0
- package/lib/release-pins.js +350 -0
- package/lib/release-pins.json +4225 -0
- package/lib/release-tags.js +12 -0
- package/lib/release.js +36 -0
- package/lib/restart-command.js +161 -0
- package/lib/rollback.js +227 -0
- package/lib/run-liveness.js +15 -0
- package/lib/runtime-log.js +287 -0
- package/lib/sandbox-support.js +17 -0
- package/lib/semver-order.js +40 -0
- package/lib/session-socket.js +69 -0
- package/lib/session-table.js +238 -0
- package/lib/session.js +281 -0
- package/lib/settings-command.js +82 -0
- package/lib/settings.js +564 -0
- package/lib/shim-binary.js +152 -0
- package/lib/shim-config.js +159 -0
- package/lib/shim-install.js +365 -0
- package/lib/sigstore/README.md +44 -0
- package/lib/sigstore/npm-keys.json +18 -0
- package/lib/sigstore/trusted-root.json +126 -0
- package/lib/snippets.js +76 -0
- package/lib/start.js +721 -0
- package/lib/store-files.js +48 -0
- package/lib/store-paths.js +33 -0
- package/lib/swap-preload.js +38 -0
- package/lib/tar.js +220 -0
- package/lib/uninstall.js +314 -0
- package/lib/unpack.js +41 -0
- package/lib/upgrade-stamp.js +63 -0
- package/lib/upgrade.js +281 -0
- package/lib/watch.js +40 -0
- package/lib/win-acl.js +420 -0
- package/node_modules/@sigstore/bundle/LICENSE +202 -0
- package/node_modules/@sigstore/bundle/README.md +19 -0
- package/node_modules/@sigstore/bundle/dist/build.d.ts +18 -0
- package/node_modules/@sigstore/bundle/dist/build.js +100 -0
- package/node_modules/@sigstore/bundle/dist/bundle.d.ts +74 -0
- package/node_modules/@sigstore/bundle/dist/bundle.js +24 -0
- package/node_modules/@sigstore/bundle/dist/error.d.ts +4 -0
- package/node_modules/@sigstore/bundle/dist/error.js +25 -0
- package/node_modules/@sigstore/bundle/dist/index.d.ts +8 -0
- package/node_modules/@sigstore/bundle/dist/index.js +43 -0
- package/node_modules/@sigstore/bundle/dist/serialized.d.ts +74 -0
- package/node_modules/@sigstore/bundle/dist/serialized.js +49 -0
- package/node_modules/@sigstore/bundle/dist/utility.d.ts +14 -0
- package/node_modules/@sigstore/bundle/dist/utility.js +2 -0
- package/node_modules/@sigstore/bundle/dist/validate.d.ts +7 -0
- package/node_modules/@sigstore/bundle/dist/validate.js +199 -0
- package/node_modules/@sigstore/bundle/package.json +35 -0
- package/node_modules/@sigstore/core/LICENSE +202 -0
- package/node_modules/@sigstore/core/README.md +10 -0
- package/node_modules/@sigstore/core/dist/asn1/error.d.ts +4 -0
- package/node_modules/@sigstore/core/dist/asn1/error.js +24 -0
- package/node_modules/@sigstore/core/dist/asn1/index.d.ts +1 -0
- package/node_modules/@sigstore/core/dist/asn1/index.js +20 -0
- package/node_modules/@sigstore/core/dist/asn1/length.d.ts +3 -0
- package/node_modules/@sigstore/core/dist/asn1/length.js +62 -0
- package/node_modules/@sigstore/core/dist/asn1/obj.d.ts +14 -0
- package/node_modules/@sigstore/core/dist/asn1/obj.js +155 -0
- package/node_modules/@sigstore/core/dist/asn1/parse.d.ts +6 -0
- package/node_modules/@sigstore/core/dist/asn1/parse.js +124 -0
- package/node_modules/@sigstore/core/dist/asn1/tag.d.ts +16 -0
- package/node_modules/@sigstore/core/dist/asn1/tag.js +89 -0
- package/node_modules/@sigstore/core/dist/crypto.d.ts +6 -0
- package/node_modules/@sigstore/core/dist/crypto.js +69 -0
- package/node_modules/@sigstore/core/dist/dsse.d.ts +1 -0
- package/node_modules/@sigstore/core/dist/dsse.js +29 -0
- package/node_modules/@sigstore/core/dist/encoding.d.ts +2 -0
- package/node_modules/@sigstore/core/dist/encoding.js +27 -0
- package/node_modules/@sigstore/core/dist/index.d.ts +9 -0
- package/node_modules/@sigstore/core/dist/index.js +66 -0
- package/node_modules/@sigstore/core/dist/json.d.ts +1 -0
- package/node_modules/@sigstore/core/dist/json.js +60 -0
- package/node_modules/@sigstore/core/dist/oid.d.ts +3 -0
- package/node_modules/@sigstore/core/dist/oid.js +20 -0
- package/node_modules/@sigstore/core/dist/pem.d.ts +2 -0
- package/node_modules/@sigstore/core/dist/pem.js +43 -0
- package/node_modules/@sigstore/core/dist/rfc3161/error.d.ts +2 -0
- package/node_modules/@sigstore/core/dist/rfc3161/error.js +21 -0
- package/node_modules/@sigstore/core/dist/rfc3161/index.d.ts +1 -0
- package/node_modules/@sigstore/core/dist/rfc3161/index.js +20 -0
- package/node_modules/@sigstore/core/dist/rfc3161/timestamp.d.ts +36 -0
- package/node_modules/@sigstore/core/dist/rfc3161/timestamp.js +212 -0
- package/node_modules/@sigstore/core/dist/rfc3161/tstinfo.d.ts +12 -0
- package/node_modules/@sigstore/core/dist/rfc3161/tstinfo.js +72 -0
- package/node_modules/@sigstore/core/dist/stream.d.ts +21 -0
- package/node_modules/@sigstore/core/dist/stream.js +117 -0
- package/node_modules/@sigstore/core/dist/x509/cert.d.ts +43 -0
- package/node_modules/@sigstore/core/dist/x509/cert.js +245 -0
- package/node_modules/@sigstore/core/dist/x509/ext.d.ts +41 -0
- package/node_modules/@sigstore/core/dist/x509/ext.js +146 -0
- package/node_modules/@sigstore/core/dist/x509/index.d.ts +2 -0
- package/node_modules/@sigstore/core/dist/x509/index.js +23 -0
- package/node_modules/@sigstore/core/dist/x509/sct.d.ts +25 -0
- package/node_modules/@sigstore/core/dist/x509/sct.js +158 -0
- package/node_modules/@sigstore/core/package.json +31 -0
- package/node_modules/@sigstore/protobuf-specs/LICENSE +202 -0
- package/node_modules/@sigstore/protobuf-specs/README.md +5 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/envelope.d.ts +44 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/envelope.js +59 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/events.d.ts +64 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/events.js +174 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/api/field_behavior.d.ts +74 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/api/field_behavior.js +141 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/protobuf/any.d.ts +74 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/protobuf/any.js +35 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/protobuf/descriptor.d.ts +1309 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/protobuf/descriptor.js +2191 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/protobuf/timestamp.d.ts +113 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/protobuf/timestamp.js +29 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/dsse.d.ts +23 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/dsse.js +55 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/entry.d.ts +43 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/entry.js +81 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/hashedrekord.d.ts +22 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/hashedrekord.js +56 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/verifier.d.ts +35 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/verifier.js +74 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_bundle.d.ts +146 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_bundle.js +103 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_common.d.ts +304 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_common.js +612 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_monitor.d.ts +33 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_monitor.js +55 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_rekor.d.ts +144 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_rekor.js +137 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_trustroot.d.ts +386 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_trustroot.js +284 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_verification.d.ts +191 -0
- package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_verification.js +281 -0
- package/node_modules/@sigstore/protobuf-specs/dist/index.d.ts +6 -0
- package/node_modules/@sigstore/protobuf-specs/dist/index.js +37 -0
- package/node_modules/@sigstore/protobuf-specs/dist/rekor/v2/index.d.ts +4 -0
- package/node_modules/@sigstore/protobuf-specs/dist/rekor/v2/index.js +35 -0
- package/node_modules/@sigstore/protobuf-specs/package.json +35 -0
- package/node_modules/@sigstore/verify/README.md +9 -0
- package/node_modules/@sigstore/verify/dist/bundle/dsse.d.ts +12 -0
- package/node_modules/@sigstore/verify/dist/bundle/dsse.js +44 -0
- package/node_modules/@sigstore/verify/dist/bundle/index.d.ts +4 -0
- package/node_modules/@sigstore/verify/dist/bundle/index.js +59 -0
- package/node_modules/@sigstore/verify/dist/bundle/message.d.ts +13 -0
- package/node_modules/@sigstore/verify/dist/bundle/message.js +54 -0
- package/node_modules/@sigstore/verify/dist/error.d.ts +16 -0
- package/node_modules/@sigstore/verify/dist/error.js +34 -0
- package/node_modules/@sigstore/verify/dist/index.d.ts +6 -0
- package/node_modules/@sigstore/verify/dist/index.js +28 -0
- package/node_modules/@sigstore/verify/dist/key/certificate.d.ts +21 -0
- package/node_modules/@sigstore/verify/dist/key/certificate.js +216 -0
- package/node_modules/@sigstore/verify/dist/key/index.d.ts +10 -0
- package/node_modules/@sigstore/verify/dist/key/index.js +75 -0
- package/node_modules/@sigstore/verify/dist/key/sct.d.ts +4 -0
- package/node_modules/@sigstore/verify/dist/key/sct.js +78 -0
- package/node_modules/@sigstore/verify/dist/policy.d.ts +5 -0
- package/node_modules/@sigstore/verify/dist/policy.js +50 -0
- package/node_modules/@sigstore/verify/dist/shared.types.d.ts +50 -0
- package/node_modules/@sigstore/verify/dist/shared.types.js +2 -0
- package/node_modules/@sigstore/verify/dist/timestamp/index.d.ts +11 -0
- package/node_modules/@sigstore/verify/dist/timestamp/index.js +24 -0
- package/node_modules/@sigstore/verify/dist/timestamp/tsa.d.ts +3 -0
- package/node_modules/@sigstore/verify/dist/timestamp/tsa.js +63 -0
- package/node_modules/@sigstore/verify/dist/tlog/checkpoint.d.ts +11 -0
- package/node_modules/@sigstore/verify/dist/tlog/checkpoint.js +154 -0
- package/node_modules/@sigstore/verify/dist/tlog/dsse.d.ts +6 -0
- package/node_modules/@sigstore/verify/dist/tlog/dsse.js +106 -0
- package/node_modules/@sigstore/verify/dist/tlog/hashedrekord.d.ts +6 -0
- package/node_modules/@sigstore/verify/dist/tlog/hashedrekord.js +94 -0
- package/node_modules/@sigstore/verify/dist/tlog/index.d.ts +5 -0
- package/node_modules/@sigstore/verify/dist/tlog/index.js +92 -0
- package/node_modules/@sigstore/verify/dist/tlog/intoto.d.ts +3 -0
- package/node_modules/@sigstore/verify/dist/tlog/intoto.js +62 -0
- package/node_modules/@sigstore/verify/dist/tlog/merkle.d.ts +3 -0
- package/node_modules/@sigstore/verify/dist/tlog/merkle.js +104 -0
- package/node_modules/@sigstore/verify/dist/tlog/set.d.ts +3 -0
- package/node_modules/@sigstore/verify/dist/tlog/set.js +60 -0
- package/node_modules/@sigstore/verify/dist/trust/filter.d.ts +8 -0
- package/node_modules/@sigstore/verify/dist/trust/filter.js +23 -0
- package/node_modules/@sigstore/verify/dist/trust/index.d.ts +5 -0
- package/node_modules/@sigstore/verify/dist/trust/index.js +90 -0
- package/node_modules/@sigstore/verify/dist/trust/trust.types.d.ts +29 -0
- package/node_modules/@sigstore/verify/dist/trust/trust.types.js +2 -0
- package/node_modules/@sigstore/verify/dist/verifier.d.ts +31 -0
- package/node_modules/@sigstore/verify/dist/verifier.js +160 -0
- package/node_modules/@sigstore/verify/package.json +36 -0
- package/package.json +37 -4
package/lib/settings.js
ADDED
|
@@ -0,0 +1,564 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// The launcher's settings (packs in place, part 2, plan Task B4; countinghouse
|
|
3
|
+
// #168's spec, section 3): <home>/settings.json, {version: 1, values}, read by
|
|
4
|
+
// start at spawn and watched while it runs. The same allowlist and bounds as
|
|
5
|
+
// countinghouse's countinghouse_settings, which may only tighten a guarding
|
|
6
|
+
// setting; `ch-local settings set` is the user at a terminal and may loosen
|
|
7
|
+
// one too (whether a change loosens: loosens(), one function, which part 3
|
|
8
|
+
// wraps with its confirmation). Every change is appended to
|
|
9
|
+
// <home>/history.jsonl, by: 'cli'.
|
|
10
|
+
const fs = require('node:fs');
|
|
11
|
+
const os = require('node:os');
|
|
12
|
+
const path = require('node:path');
|
|
13
|
+
const {writeAtomic} = require('./atomic');
|
|
14
|
+
const {appendHistory} = require('./history');
|
|
15
|
+
const {chLocal} = require('./command-line');
|
|
16
|
+
|
|
17
|
+
const MiB = 1024 * 1024;
|
|
18
|
+
const size = (v) => {
|
|
19
|
+
const m = /^(\d+)\s*(MiB|M|KiB|K|B)?$/i.exec(String(v).trim());
|
|
20
|
+
if (m == null) return NaN;
|
|
21
|
+
const unit = (m[2] || 'B').toLowerCase();
|
|
22
|
+
return Number(m[1]) * (unit.startsWith('m') ? MiB : unit.startsWith('k') ? 1024 : 1);
|
|
23
|
+
};
|
|
24
|
+
const ms = (v) => {
|
|
25
|
+
const m = /^(\d+)\s*(ms|s)?$/i.exec(String(v).trim());
|
|
26
|
+
if (m == null) return NaN;
|
|
27
|
+
return Number(m[1]) * ((m[2] || 'ms').toLowerCase() === 's' ? 1000 : 1);
|
|
28
|
+
};
|
|
29
|
+
const onOff = (v) => (v === true || v === 'on' ? true : v === false || v === 'off' ? false : null);
|
|
30
|
+
|
|
31
|
+
// The sandbox setting: enforce and audit on every platform, enforce the
|
|
32
|
+
// default, as countinghouse's lib/settings-tool.js has it since #195 (enforce
|
|
33
|
+
// on Windows). With an older runtime on Windows, which cannot run enforce,
|
|
34
|
+
// start runs audit whatever is stored, and says so (lib/sandbox-support.js).
|
|
35
|
+
// (platform: kept for callers; the entry is the same everywhere.)
|
|
36
|
+
const sandboxEntry = () => ({parse: String, values: ['enforce', 'audit'], default: 'enforce', guarding: true, tighter: (o, n) => n === 'enforce' || o === n});
|
|
37
|
+
|
|
38
|
+
// name -> {parse, check(value) -> bounds text or null, default, guarding,
|
|
39
|
+
// tighter(old, new) -> true when the change tightens or keeps}
|
|
40
|
+
const ALLOWLIST = {
|
|
41
|
+
// 16 MiB: the runtime's own default (countinghouse lib/cli-options.js and
|
|
42
|
+
// its settings tool's allowlist; maintainer's decision, 2026-11-15). The
|
|
43
|
+
// two are kept in step: with no value in the file the runtime starts on its
|
|
44
|
+
// default, and this is what
|
|
45
|
+
// `settings get`, a loosening and the start's ceiling check go by.
|
|
46
|
+
maxOutputBytes: {parse: size, min: MiB, max: 256 * MiB, default: 16 * MiB, guarding: true, tighter: (o, n) => n <= o},
|
|
47
|
+
requestTimeout: {parse: ms, min: 5000, max: 120000, default: 30000, guarding: true, tighter: (o, n) => n <= o},
|
|
48
|
+
moduleSandbox: sandboxEntry(process.platform),
|
|
49
|
+
autoUpgrade: {parse: onOff, values: [true, false], default: false, guarding: true, tighter: (o, n) => n === false || o === n},
|
|
50
|
+
upgradeCheck: {parse: String, values: ['daily', 'weekly', 'never'], default: 'daily', guarding: false}
|
|
51
|
+
};
|
|
52
|
+
// On and off by module (#413 section 5.1): enabled.<package> for a
|
|
53
|
+
// top-level module, default on, guarding, off the tighter value. A part has
|
|
54
|
+
// no switch (refused at a set, ignored in the file). pack.<name>, the
|
|
55
|
+
// earlier form, is still read as its pack's scenario module's entry (off
|
|
56
|
+
// wins: isOn), never written (a set names the enabled.* command instead).
|
|
57
|
+
const PACK = /^pack\.([a-z0-9][a-z0-9-]{0,63})$/;
|
|
58
|
+
const ENABLED = /^enabled\.((?:@[a-z0-9][a-z0-9._-]*\/)?[a-z0-9][a-z0-9._-]*)$/;
|
|
59
|
+
const enabledName = (name) => { const m = ENABLED.exec(String(name)); return m != null && m[1].length <= 214 ? m[1] : null; };
|
|
60
|
+
const onOffEntry = {parse: onOff, values: [true, false], default: true, guarding: true, tighter: (o, n) => n === false || o === n};
|
|
61
|
+
const entryOf = (name) => (Object.prototype.hasOwnProperty.call(ALLOWLIST, name) ? ALLOWLIST[name]
|
|
62
|
+
: PACK.test(name) || enabledName(name) != null ? onOffEntry : null);
|
|
63
|
+
const boundsOf = (e) => (e.values != null ? e.values.map((v) => (v === true ? 'on' : v === false ? 'off' : v)).join(', ') : `${e.min} to ${e.max}`);
|
|
64
|
+
|
|
65
|
+
// A value as stored is valid for its setting (the stored form: numbers,
|
|
66
|
+
// booleans, the listed strings).
|
|
67
|
+
function validStored(name, v) {
|
|
68
|
+
const e = entryOf(name);
|
|
69
|
+
if (e == null) return false;
|
|
70
|
+
if (e.values != null) return (e.stored || e.values).includes(v);
|
|
71
|
+
return Number.isInteger(v) && v >= e.min && v <= e.max;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// -> {values, kept, order} as written, an earlier {autoUpgrade} migrated; or,
|
|
75
|
+
// when the file cannot be read or holds anything not valid, {values: {},
|
|
76
|
+
// kept, order, error} (and `unreadable` when it is there but could not be
|
|
77
|
+
// read): a caller fails closed (the defaults at start, the last good
|
|
78
|
+
// settings while running) and says so, never applies part of it (the review
|
|
79
|
+
// of ch-local #13).
|
|
80
|
+
// kept: every name this ch-local does not know, with its value as stored
|
|
81
|
+
// (the runtime's settings tool writes module.* values here; a newer runtime
|
|
82
|
+
// its own names). Never applied, and written back as it is by every write
|
|
83
|
+
// (module-level configuration design, 2026-11-18, section 4.2). {} for a
|
|
84
|
+
// file that is not a readable version 1 settings file.
|
|
85
|
+
function readSettings(where) {
|
|
86
|
+
const none = {values: {}, kept: {}, order: []};
|
|
87
|
+
let text;
|
|
88
|
+
// (a file there that could not be read is `unreadable`: what it holds is
|
|
89
|
+
// not known, and a reset never removes it)
|
|
90
|
+
try { text = fs.readFileSync(where.settings, 'utf8'); } catch (err) { return {...none, ...(err.code === 'ENOENT' ? {} : {error: `${where.settings} cannot be read (${err.code})`, unreadable: true})}; }
|
|
91
|
+
let raw = null;
|
|
92
|
+
try { raw = JSON.parse(text); } catch (err) { return {...none, error: `${where.settings} is not JSON`}; }
|
|
93
|
+
if (raw == null || typeof raw !== 'object' || Array.isArray(raw)) return {...none, error: `${where.settings} is not a settings file`};
|
|
94
|
+
let values;
|
|
95
|
+
if (raw.version === 1 && raw.values != null && typeof raw.values === 'object' && !Array.isArray(raw.values)) values = raw.values;
|
|
96
|
+
else if (raw.version == null && Object.keys(raw).every((k) => k === 'autoUpgrade')) values = typeof raw.autoUpgrade === 'boolean' ? {autoUpgrade: raw.autoUpgrade} : {};
|
|
97
|
+
else return {...none, error: `${where.settings} is not a version 1 settings file`};
|
|
98
|
+
// A name this ch-local does not know (a module's setting, which the
|
|
99
|
+
// runtime's settings tool writes; a newer runtime's) is kept as stored and
|
|
100
|
+
// never applied; a known one with a value not allowed makes the whole file
|
|
101
|
+
// bad. order: the file's own order of names, which a write keeps.
|
|
102
|
+
// an enabled.<part> (written by hand: a part has no switch) is kept as
|
|
103
|
+
// stored, never applied, and warned about (section 5.1)
|
|
104
|
+
const partOf = partTest(where);
|
|
105
|
+
const ignoredParts = Object.keys(values).filter((name) => enabledName(name) != null && partOf(enabledName(name)));
|
|
106
|
+
const entries = Object.entries(values);
|
|
107
|
+
const known = Object.fromEntries(entries.filter(([name]) => entryOf(name) != null && !ignoredParts.includes(name)));
|
|
108
|
+
const kept = Object.fromEntries(entries.filter(([name]) => entryOf(name) == null || ignoredParts.includes(name)));
|
|
109
|
+
const order = entries.map(([name]) => name);
|
|
110
|
+
const bad = Object.entries(known).find(([name, v]) => !validStored(name, v));
|
|
111
|
+
if (bad != null) return {values: {}, kept, order, error: `${where.settings} has ${bad[0]} = ${JSON.stringify(bad[1]).slice(0, 40)}, which is not allowed`};
|
|
112
|
+
// module.<package>.<setting> is the runtime's, expected here: not warned
|
|
113
|
+
// about (the warning is for a name nobody here expects)
|
|
114
|
+
const unknown = Object.keys(kept).filter((name) => !name.startsWith('module.') && !ignoredParts.includes(name));
|
|
115
|
+
const warnings = [
|
|
116
|
+
...(unknown.length > 0 ? [`${where.settings} has settings this ch-local does not know, ignored: ${unknown.slice(0, 5).join(', ')}`] : []),
|
|
117
|
+
...ignoredParts.map((name) => `${where.settings}: ${name} names a part, which has no switch: ignored (it runs while a module that calls it is on)`)
|
|
118
|
+
];
|
|
119
|
+
return {values: known, kept, order, ...(warnings.length > 0 ? {warning: warnings.join('; ')} : {})};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// The home's record (modules.json), read again only when the file changes
|
|
123
|
+
// (the medium on L3, 10: readSettings is on the applier's path). null when
|
|
124
|
+
// there is none or it cannot be read.
|
|
125
|
+
let recordCache = null;
|
|
126
|
+
function recordOf(where) {
|
|
127
|
+
if (where == null || where.modulesRecord == null) return null;
|
|
128
|
+
let stamp;
|
|
129
|
+
try { const st = fs.statSync(where.modulesRecord); stamp = `${st.ino}:${st.size}:${st.mtimeMs}`; } catch (err) { return null; }
|
|
130
|
+
if (recordCache != null && recordCache.file === where.modulesRecord && recordCache.stamp === stamp) return recordCache.record;
|
|
131
|
+
let record = null;
|
|
132
|
+
try { record = require('./module-record').readModulesRecord(where); } catch (err) { record = null; }
|
|
133
|
+
const roles = require('./module-roles');
|
|
134
|
+
recordCache = {file: where.modulesRecord, stamp, record, top: roles.topLevel(record)};
|
|
135
|
+
return record;
|
|
136
|
+
}
|
|
137
|
+
// whether a module is a part, by the home's record (none, or one that cannot
|
|
138
|
+
// be read: no module is taken for a part)
|
|
139
|
+
function partTest(where) {
|
|
140
|
+
const record = recordOf(where);
|
|
141
|
+
const top = recordCache != null && recordCache.record === record ? recordCache.top : require('./module-roles').topLevel(record);
|
|
142
|
+
// (packs' entryOf, under its own name: this module has an entryOf of its
|
|
143
|
+
// own, for the allowlist)
|
|
144
|
+
const recorded = require('./packs').entryOf;
|
|
145
|
+
return (m) => recorded(record, m) != null && !top.has(m);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Whether a top-level module is on (sections 5.1 and 10): its enabled.*
|
|
149
|
+
// entry (default on) and every leftover pack.<name> whose scenario it is;
|
|
150
|
+
// any one off is off (off wins).
|
|
151
|
+
function isOn(settings, record, module) {
|
|
152
|
+
const values = (settings && settings.values) || {};
|
|
153
|
+
if (values[`enabled.${module}`] === false) return false;
|
|
154
|
+
for (const pack of require('./module-roles').packsOf(record, module)) if (values[`pack.${pack}`] === false) return false;
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
// The values to write: the file's names in its own order (known ones from
|
|
158
|
+
// `values`, the rest from `kept`), each in `changes` replaced, and a changed
|
|
159
|
+
// name new to the file last.
|
|
160
|
+
const REMOVE = Symbol('remove');
|
|
161
|
+
function inFileOrder(settings, changes = {}) {
|
|
162
|
+
const has = (o, n) => o != null && Object.prototype.hasOwnProperty.call(o, n);
|
|
163
|
+
const out = [];
|
|
164
|
+
for (const name of settings.order || []) {
|
|
165
|
+
if (has(changes, name)) { if (changes[name] !== REMOVE) out.push([name, changes[name]]); }
|
|
166
|
+
else if (has(settings.values, name)) out.push([name, settings.values[name]]);
|
|
167
|
+
else if (has(settings.kept, name)) out.push([name, settings.kept[name]]);
|
|
168
|
+
}
|
|
169
|
+
for (const [name, v] of Object.entries(changes)) if (!(settings.order || []).includes(name) && v !== REMOVE) out.push([name, v]);
|
|
170
|
+
return Object.fromEntries(out);
|
|
171
|
+
}
|
|
172
|
+
const valueOf = (settings, name) => {
|
|
173
|
+
const e = entryOf(name);
|
|
174
|
+
return Object.prototype.hasOwnProperty.call(settings.values, name) ? settings.values[name] : (e != null ? e.default : undefined);
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
// Whether setting `name` from `old` to `next` loosens a guard (part 3 wraps
|
|
178
|
+
// this with its confirmation; this tool's own callers decide what to do).
|
|
179
|
+
function loosens(name, old, next) {
|
|
180
|
+
const e = entryOf(name);
|
|
181
|
+
return e != null && e.guarding === true && !e.tighter(old, next);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// name and raw value -> the value in its stored form, or throws
|
|
185
|
+
// SETTING_UNKNOWN or SETTING_OUT_OF_BOUNDS. where: the home's layout, for
|
|
186
|
+
// the command a refusal names (lib/command-line.js).
|
|
187
|
+
function parseSetting(name, raw, where) {
|
|
188
|
+
const e = entryOf(name);
|
|
189
|
+
if (e == null) throw Object.assign(new Error(`there is no setting named ${name}; \`${chLocal(where, 'settings get')}\` lists them`), {code: 'SETTING_UNKNOWN'});
|
|
190
|
+
const next = e.parse(raw);
|
|
191
|
+
const ok = e.values != null ? e.values.includes(next) : (Number.isInteger(next) && next >= e.min && next <= e.max);
|
|
192
|
+
if (!ok) throw Object.assign(new Error(`the value ${raw} for ${name} is outside the allowed range (${boundsOf(e)}); nothing was changed`), {code: 'SETTING_OUT_OF_BOUNDS'});
|
|
193
|
+
return next;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// A setting's value in effect: for enabled.<m>, whether the module is on,
|
|
197
|
+
// a leftover pack.* off counted (off wins), so turning it on past one is
|
|
198
|
+
// the loosening it is (the medium on L3, 2); otherwise the stored value or
|
|
199
|
+
// the default.
|
|
200
|
+
function inEffect(where, settings, name) {
|
|
201
|
+
const m = enabledName(name);
|
|
202
|
+
return m != null ? isOn(settings, recordOf(where), m) : valueOf(settings, name);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// A name a set refuses before anything else, by the home's record: a part's
|
|
206
|
+
// enabled.* (it has no switch: section 5.1), and pack.<name> (renamed: the
|
|
207
|
+
// enabled.* of its scenario module; L3, the Reviewer's Q1). Throws.
|
|
208
|
+
function refuseName(where, name, raw) {
|
|
209
|
+
const short = (m) => String(m).replace(/^@[^/]+\//, '');
|
|
210
|
+
const record = recordOf(where);
|
|
211
|
+
const roles = require('./module-roles');
|
|
212
|
+
const m = enabledName(name);
|
|
213
|
+
if (m != null && roles.isPart(record, m)) {
|
|
214
|
+
const callers = roles.callersOf(record, m).map(short);
|
|
215
|
+
throw Object.assign(new Error(`${short(m)} is a part: it runs while a module that calls it is on (${callers.join(', ') || 'none installed'}). Turn that module off instead.`), {code: 'SETTING_IS_A_PART'});
|
|
216
|
+
}
|
|
217
|
+
const pack = PACK.exec(name);
|
|
218
|
+
if (pack != null) {
|
|
219
|
+
const {packScenario} = require('./packs');
|
|
220
|
+
const scenario = packScenario(record, pack[1]);
|
|
221
|
+
const value = onOff(raw) === false ? 'off' : 'on';
|
|
222
|
+
throw Object.assign(new Error(scenario != null
|
|
223
|
+
? `${name} is no longer a setting: run \`${chLocal(where, `settings set enabled.${scenario} ${value}`)}\``
|
|
224
|
+
: `${name} is no longer a setting: a module is turned on and off with enabled.<module>; \`${chLocal(where, 'settings get')}\` lists them`), {code: 'SETTING_RENAMED'});
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// What a set would do, without the lock (to ask the user before taking it):
|
|
229
|
+
// -> {next, old, loosened}, or throws as setSetting does.
|
|
230
|
+
function planSetting(where, name, raw) {
|
|
231
|
+
refuseName(where, name, raw);
|
|
232
|
+
const next = parseSetting(name, raw, where);
|
|
233
|
+
const settings = readSettings(where);
|
|
234
|
+
if (settings.error != null) throw Object.assign(new Error(`${settings.error}; fix or remove it first; nothing was changed`), {code: 'SETTINGS_UNREADABLE'});
|
|
235
|
+
const old = inEffect(where, settings, name);
|
|
236
|
+
return {next, old, loosened: loosens(name, old, next)};
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// A setting's value as ch-local shows it: on/off, a size in MiB and a
|
|
240
|
+
// timeout in s when they are whole, as countinghouse's messages give them.
|
|
241
|
+
function shown(name, v) {
|
|
242
|
+
if (v === true) return 'on';
|
|
243
|
+
if (v === false) return 'off';
|
|
244
|
+
if (name === 'maxOutputBytes' && Number.isInteger(v)) return v % MiB === 0 ? `${v / MiB}MiB` : `${v}B`;
|
|
245
|
+
if (name === 'requestTimeout' && Number.isInteger(v)) return v % 1000 === 0 ? `${v / 1000}s` : `${v}ms`;
|
|
246
|
+
return String(v);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// What a reset would loosen: [{name, old, new}] each setting whose default is
|
|
250
|
+
// looser than its value; {unknown: true} when the file is not valid (its
|
|
251
|
+
// values cannot be known). expect: what resetSettings compares with.
|
|
252
|
+
function planReset(where) {
|
|
253
|
+
const read = readSettings(where);
|
|
254
|
+
if (read.error != null) return {unknown: true, loosens: [], expect: {error: read.error}};
|
|
255
|
+
const out = [];
|
|
256
|
+
for (const name of Object.keys(read.values)) {
|
|
257
|
+
if (entryOf(name) == null) continue;
|
|
258
|
+
const old = valueOf(read, name);
|
|
259
|
+
const next = valueOf({values: {}}, name);
|
|
260
|
+
if (loosens(name, old, next)) out.push({name, old, new: next});
|
|
261
|
+
}
|
|
262
|
+
return {unknown: false, loosens: out, expect: read.values};
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// -> {name, old, new, loosened} or throws {code, message, fields}.
|
|
266
|
+
// expect: the old value the caller planned with (planSetting): a value
|
|
267
|
+
// changed since is not overwritten (SETTINGS_CHANGED). confirmed: how a
|
|
268
|
+
// loosening was confirmed ('cli'), recorded with it (part 3, section 5).
|
|
269
|
+
function setSetting(where, name, raw, {by = 'cli', mayLoosen = false, expect, confirmed} = {}) {
|
|
270
|
+
refuseName(where, name, raw);
|
|
271
|
+
const next = parseSetting(name, raw, where);
|
|
272
|
+
return withSettingsLock(where, () => {
|
|
273
|
+
const settings = readSettings(where);
|
|
274
|
+
if (settings.error != null) throw Object.assign(new Error(`${settings.error}; fix or remove it first; nothing was changed`), {code: 'SETTINGS_UNREADABLE'});
|
|
275
|
+
const old = inEffect(where, settings, name);
|
|
276
|
+
if (expect !== undefined && old !== expect) throw Object.assign(new Error(`${name} was changed by another writer meanwhile; nothing was changed; run it again`), {code: 'SETTINGS_CHANGED'});
|
|
277
|
+
const loosened = loosens(name, old, next);
|
|
278
|
+
if (loosened && !mayLoosen) throw Object.assign(new Error(`changing ${name} to ${raw} would loosen a guarding setting; nothing was changed`), {code: 'SETTING_LOOSENS_A_GUARD'});
|
|
279
|
+
// a module turned on: every leftover pack.<name> off whose scenario it
|
|
280
|
+
// is goes in the same write, so the "on" holds (off wins: section 10)
|
|
281
|
+
const changes = {[name]: next};
|
|
282
|
+
const leftovers = [];
|
|
283
|
+
const m = enabledName(name);
|
|
284
|
+
if (m != null && next === true) {
|
|
285
|
+
const record = recordOf(where);
|
|
286
|
+
for (const pack of require('./module-roles').packsOf(record, m)) {
|
|
287
|
+
if (settings.values[`pack.${pack}`] === false) { changes[`pack.${pack}`] = REMOVE; leftovers.push(`pack.${pack}`); }
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
// recorded first: a change the history cannot hold is not made; the
|
|
291
|
+
// lines written before one that could not be are undone (the Reviewer's
|
|
292
|
+
// high review of #90, 5)
|
|
293
|
+
const lines = [{by, setting: name, old, new: next, ...(loosened ? {loosened: true} : {}), ...(loosened && confirmed ? {confirmed} : {})},
|
|
294
|
+
...leftovers.map((p) => ({by, setting: p, old: false, change: `removed with ${name} on`}))];
|
|
295
|
+
const written = [];
|
|
296
|
+
for (const line of lines) {
|
|
297
|
+
if (!appendHistory(where, line, {strict: true})) {
|
|
298
|
+
for (const setting of written) appendHistory(where, {by, setting, change: 'not made', problem: 'SETTING_NOT_RECORDED'});
|
|
299
|
+
throw Object.assign(new Error(`the change could not be recorded in ${where.home}/history.jsonl; nothing was changed`), {code: 'SETTING_NOT_RECORDED'});
|
|
300
|
+
}
|
|
301
|
+
written.push(line.setting);
|
|
302
|
+
}
|
|
303
|
+
try {
|
|
304
|
+
// (the names it does not know kept as stored, the file's order kept:
|
|
305
|
+
// section 4.2)
|
|
306
|
+
writeAtomic(where.settings, JSON.stringify({version: 1, values: inFileOrder(settings, changes)}, null, 2) + '\n');
|
|
307
|
+
} catch (err) {
|
|
308
|
+
// said in the history too, so it never shows a change not made (and
|
|
309
|
+
// start's outside-edit check does not report one): every line written
|
|
310
|
+
// for it, a leftover's removal included (the medium on L3, 6)
|
|
311
|
+
for (const setting of [name, ...leftovers]) appendHistory(where, {by, setting, change: 'not made', problem: err.code || 'error'});
|
|
312
|
+
throw err;
|
|
313
|
+
}
|
|
314
|
+
return {name, old, new: next, loosened};
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// `ch-local settings reset`: every setting this ch-local knows back to its
|
|
319
|
+
// default, recorded first, under the settings lock. The names it does not
|
|
320
|
+
// know are kept (a module's values, section 4.2): the file is rewritten
|
|
321
|
+
// holding only them, or removed when there are none. A file that is not a
|
|
322
|
+
// readable version 1 settings file holds no names to keep, and is removed
|
|
323
|
+
// whole, as a bad one always was.
|
|
324
|
+
// expect: the values the caller planned with (planReset): changed since, it
|
|
325
|
+
// is not made (SETTINGS_CHANGED). confirmed: how its loosenings were.
|
|
326
|
+
// -> {kept: how many names it kept}
|
|
327
|
+
function resetSettings(where, {by = 'cli', expect, confirmed} = {}) {
|
|
328
|
+
return withSettingsLock(where, () => {
|
|
329
|
+
// one read, under the lock: what is kept is what was compared
|
|
330
|
+
const now = readSettings(where);
|
|
331
|
+
// a file there that could not be read may hold a module's values: never
|
|
332
|
+
// removed blind (a reader holding it on Windows, say)
|
|
333
|
+
if (now.unreadable === true) throw Object.assign(new Error(`${now.error}; nothing was changed; run it again`), {code: 'SETTINGS_UNREADABLE'});
|
|
334
|
+
if (expect !== undefined) {
|
|
335
|
+
if (JSON.stringify(now.error != null ? {error: now.error} : now.values) !== JSON.stringify(expect)) {
|
|
336
|
+
throw Object.assign(new Error('the settings were changed by another writer meanwhile; nothing was changed; run it again'), {code: 'SETTINGS_CHANGED'});
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
if (!appendHistory(where, {by, setting: '*', change: 'reset', ...(confirmed ? {confirmed} : {})}, {strict: true})) {
|
|
340
|
+
throw Object.assign(new Error(`the reset could not be recorded in ${where.home}/history.jsonl; nothing was changed`), {code: 'SETTING_NOT_RECORDED'});
|
|
341
|
+
}
|
|
342
|
+
const kept = Object.fromEntries(now.order.filter((name) => Object.prototype.hasOwnProperty.call(now.kept, name)).map((name) => [name, now.kept[name]]));
|
|
343
|
+
try {
|
|
344
|
+
if (Object.keys(kept).length > 0) writeAtomic(where.settings, JSON.stringify({version: 1, values: kept}, null, 2) + '\n');
|
|
345
|
+
else fs.rmSync(where.settings, {force: true});
|
|
346
|
+
} catch (err) {
|
|
347
|
+
// said in the history too, so it never shows a reset not made (start's
|
|
348
|
+
// outside-edit check reads the history: lib/outside-edits.js)
|
|
349
|
+
appendHistory(where, {by, setting: '*', change: 'not made', problem: err.code || 'error'});
|
|
350
|
+
throw err;
|
|
351
|
+
}
|
|
352
|
+
return {kept: Object.keys(kept).length};
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// A value as the history records it (#413 section 4.2; the rule agreed with
|
|
357
|
+
// countinghouse, its shared historyCut vector): only a string is cut, when
|
|
358
|
+
// it holds more than 64 code points (never a surrogate pair split), and
|
|
359
|
+
// <key>Sha256 is the lowercase hex SHA-256 of the UTF-8 bytes of
|
|
360
|
+
// JSON.stringify(the whole value). Exactly 64 is written whole.
|
|
361
|
+
// -> fields for the line ({[key]: v} or {[key]: cut, [key + 'Sha256']: hash}).
|
|
362
|
+
// the first 64 code points of a string, or null when it holds no more (a
|
|
363
|
+
// string of at most 64 UTF-16 units never does: no array made for it)
|
|
364
|
+
function cutPoints(v, max = 64) {
|
|
365
|
+
if (typeof v !== 'string' || v.length <= max) return null;
|
|
366
|
+
const points = Array.from(v);
|
|
367
|
+
return points.length <= max ? null : points.slice(0, max).join('');
|
|
368
|
+
}
|
|
369
|
+
function historyValue(key, v) {
|
|
370
|
+
const cut = cutPoints(v);
|
|
371
|
+
if (cut == null) return {[key]: v};
|
|
372
|
+
return {[key]: cut, [`${key}Sha256`]: require('node:crypto').createHash('sha256').update(JSON.stringify(v), 'utf8').digest('hex')};
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// A module's values (module.<package>.<setting>, kept names: never this
|
|
376
|
+
// file's own entries), written under the settings lock, each change's
|
|
377
|
+
// history line first (lib/module-values.js plans and checks them).
|
|
378
|
+
// changes: {name: value | REMOVE}; expect: {name: the old value planned
|
|
379
|
+
// with}, a value changed since is not overwritten. -> {changed: [names]}
|
|
380
|
+
// inEffect: {name: the value in effect before} (a default when none was
|
|
381
|
+
// stored), what the history's `old` says.
|
|
382
|
+
function writeModuleValues(where, changes, {by = 'cli', expect = null, change = null, inEffect = null} = {}) {
|
|
383
|
+
return withSettingsLock(where, () => {
|
|
384
|
+
const settings = readSettings(where);
|
|
385
|
+
if (settings.error != null) throw Object.assign(new Error(`${settings.error}; fix or remove it first; nothing was changed`), {code: 'SETTINGS_UNREADABLE'});
|
|
386
|
+
const has = (n) => Object.prototype.hasOwnProperty.call(settings.kept, n);
|
|
387
|
+
const out = {};
|
|
388
|
+
for (const [name, v] of Object.entries(changes)) {
|
|
389
|
+
const old = has(name) ? settings.kept[name] : undefined;
|
|
390
|
+
if (expect != null && Object.prototype.hasOwnProperty.call(expect, name) && JSON.stringify(old) !== JSON.stringify(expect[name])) {
|
|
391
|
+
throw Object.assign(new Error(`${name} was changed by another writer meanwhile; nothing was changed; run it again`), {code: 'SETTINGS_CHANGED'});
|
|
392
|
+
}
|
|
393
|
+
if (v === REMOVE && old === undefined) continue;
|
|
394
|
+
out[name] = v;
|
|
395
|
+
}
|
|
396
|
+
const written = [];
|
|
397
|
+
for (const [name, v] of Object.entries(out)) {
|
|
398
|
+
const old = has(name) ? settings.kept[name] : (inEffect != null ? inEffect[name] : undefined);
|
|
399
|
+
const line = v === REMOVE ? {by, setting: name, ...historyValue('old', old), change: change || 'reset'}
|
|
400
|
+
: {by, setting: name, ...historyValue('old', old), ...historyValue('new', v), ...(change != null ? {change} : {})};
|
|
401
|
+
if (!appendHistory(where, line, {strict: true})) {
|
|
402
|
+
// the lines written before it are undone (the Reviewer's high
|
|
403
|
+
// review of #90, 5)
|
|
404
|
+
for (const setting of written) appendHistory(where, {by, setting, change: 'not made', problem: 'SETTING_NOT_RECORDED'});
|
|
405
|
+
throw Object.assign(new Error(`the change could not be recorded in ${where.home}/history.jsonl; nothing was changed`), {code: 'SETTING_NOT_RECORDED'});
|
|
406
|
+
}
|
|
407
|
+
written.push(name);
|
|
408
|
+
}
|
|
409
|
+
if (Object.keys(out).length === 0) return {changed: []};
|
|
410
|
+
try {
|
|
411
|
+
writeAtomic(where.settings, JSON.stringify({version: 1, values: inFileOrder(settings, out)}, null, 2) + '\n');
|
|
412
|
+
} catch (err) {
|
|
413
|
+
for (const name of Object.keys(out)) appendHistory(where, {by, setting: name, change: 'not made', problem: err.code || 'error'});
|
|
414
|
+
throw err;
|
|
415
|
+
}
|
|
416
|
+
return {changed: Object.keys(out)};
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// The migration of a home's pack.* settings (#413 section 10): in the
|
|
421
|
+
// install or upgrade that writes modules.json, each pack.<name> whose pack
|
|
422
|
+
// the new record has becomes enabled.<its scenario module>, under the
|
|
423
|
+
// settings lock, a history line {by: 'migration', setting, was} for each.
|
|
424
|
+
// Off wins: a pack.* off, or an enabled.* off already there, stays off. A
|
|
425
|
+
// pack.<name> whose pack the record doesn't have is kept as it is (an
|
|
426
|
+
// unknown name to a later ch-local). A file that can't be read is left.
|
|
427
|
+
// -> [{setting, was, to}] what was moved
|
|
428
|
+
function migratePackSettings(where, record) {
|
|
429
|
+
const {packScenario} = require('./packs');
|
|
430
|
+
return withSettingsLock(where, () => {
|
|
431
|
+
const settings = readSettings(where);
|
|
432
|
+
if (settings.error != null) return [];
|
|
433
|
+
const changes = {};
|
|
434
|
+
const moved = [];
|
|
435
|
+
for (const name of Object.keys(settings.values)) {
|
|
436
|
+
const m = PACK.exec(name);
|
|
437
|
+
if (m == null) continue;
|
|
438
|
+
const scenario = packScenario(record, m[1]);
|
|
439
|
+
if (scenario == null) continue;
|
|
440
|
+
const to = `enabled.${scenario}`;
|
|
441
|
+
const now = Object.prototype.hasOwnProperty.call(changes, to) ? changes[to] : settings.values[to];
|
|
442
|
+
changes[to] = now === false || settings.values[name] === false ? false : true;
|
|
443
|
+
changes[name] = REMOVE;
|
|
444
|
+
moved.push({setting: name, was: settings.values[name], to});
|
|
445
|
+
}
|
|
446
|
+
if (moved.length === 0) return [];
|
|
447
|
+
// every line first; a line that can't be written, or a write that
|
|
448
|
+
// fails, is undone in the history ('not made'), as a set's is
|
|
449
|
+
const written = [];
|
|
450
|
+
const undo = (problem) => { for (const setting of written) appendHistory(where, {by: 'migration', setting, change: 'not made', problem}); };
|
|
451
|
+
const lines = [
|
|
452
|
+
...moved.map((mv) => ({by: 'migration', setting: mv.setting, was: mv.was, change: `moved to ${mv.to}`})),
|
|
453
|
+
...Object.entries(changes).filter(([, v]) => v !== REMOVE).map(([to, v]) => ({by: 'migration', setting: to, old: settings.values[to] === undefined ? true : settings.values[to], new: v}))
|
|
454
|
+
];
|
|
455
|
+
for (const line of lines) {
|
|
456
|
+
if (!appendHistory(where, line, {strict: true})) {
|
|
457
|
+
undo('SETTING_NOT_RECORDED');
|
|
458
|
+
throw Object.assign(new Error(`the migration could not be recorded in ${where.home}/history.jsonl; nothing was changed`), {code: 'SETTING_NOT_RECORDED'});
|
|
459
|
+
}
|
|
460
|
+
written.push(line.setting);
|
|
461
|
+
}
|
|
462
|
+
try {
|
|
463
|
+
writeAtomic(where.settings, JSON.stringify({version: 1, values: inFileOrder(settings, changes)}, null, 2) + '\n');
|
|
464
|
+
} catch (err) {
|
|
465
|
+
undo(err.code || 'error');
|
|
466
|
+
throw err;
|
|
467
|
+
}
|
|
468
|
+
return moved;
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
// One lock around every settings write, shared by the CLI and countinghouse's
|
|
473
|
+
// settings tool: the protocol is docs/development/settings.md's, which
|
|
474
|
+
// countinghouse follows too. <home>/settings.lock is created whole (written
|
|
475
|
+
// to a temporary file and linked into place), held only for the read, the
|
|
476
|
+
// history line and the write, and released by its holder. One whose mtime is
|
|
477
|
+
// SETTINGS_LOCK_STALE_MS old is a holder that died, and is removed only if it
|
|
478
|
+
// is still that file (same inode). Waits up to SETTINGS_LOCK_WAIT_MS.
|
|
479
|
+
const SETTINGS_LOCK_STALE_MS = 30000;
|
|
480
|
+
const SETTINGS_LOCK_WAIT_MS = 5000;
|
|
481
|
+
// waitMs: how long to wait for another writer (0: not at all, for start's
|
|
482
|
+
// event loop, which must not stall; SETTINGS_BUSY then).
|
|
483
|
+
// On Windows a lock name another process is deleting refuses the link (and a
|
|
484
|
+
// stat) with EPERM or EACCES until its handle closes (lib/lock.js
|
|
485
|
+
// pendingRefusal): busy within this wait, and at its end that refusal is
|
|
486
|
+
// thrown, the lock named, since a genuine permission error answers EPERM
|
|
487
|
+
// too. Every pass keeps the deadline, and a wait of 0 never sleeps. A
|
|
488
|
+
// stale lock is judged by its real age (Date.now). platform, now, sleep: a
|
|
489
|
+
// test's.
|
|
490
|
+
const sleepSync = (ms) => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
491
|
+
function withSettingsLock(where, fn, {waitMs = SETTINGS_LOCK_WAIT_MS, platform = process.platform, now = Date.now, sleep = sleepSync} = {}) {
|
|
492
|
+
const {createWhole, pendingRefusal} = require('./lock');
|
|
493
|
+
const lock = path.join(where.home, 'settings.lock');
|
|
494
|
+
const mine = {pid: process.pid, host: os.hostname(), at: new Date().toISOString()};
|
|
495
|
+
const until = now() + waitMs;
|
|
496
|
+
let released = false;
|
|
497
|
+
const busy = () => Object.assign(new Error(`another change to the settings is under way (${lock}); try again`), {code: 'SETTINGS_BUSY'});
|
|
498
|
+
// (a pending name: past the wait, the refusal itself, the lock named)
|
|
499
|
+
const pending = (err) => {
|
|
500
|
+
released = false;
|
|
501
|
+
if (now() >= until) throw Object.assign(new Error(`${err.code}: the settings lock ${lock} could not be taken`, {cause: err}), {code: err.code, path: lock});
|
|
502
|
+
sleep(50);
|
|
503
|
+
};
|
|
504
|
+
require('./home').ensureHome(where.home);
|
|
505
|
+
for (;;) {
|
|
506
|
+
let made;
|
|
507
|
+
try { made = createWhole(lock, mine, platform); } catch (err) { if (!pendingRefusal(err, platform)) throw err; pending(err); continue; }
|
|
508
|
+
if (made) break;
|
|
509
|
+
let st = null;
|
|
510
|
+
try { st = fs.statSync(lock); } catch (err) {
|
|
511
|
+
if (pendingRefusal(err, platform)) { pending(err); continue; }
|
|
512
|
+
if (err.code !== 'ENOENT') throw err;
|
|
513
|
+
// released: try again at once, within the wait; released again (a
|
|
514
|
+
// name the link still finds), after a step
|
|
515
|
+
if (now() >= until) throw busy();
|
|
516
|
+
if (released) sleep(50);
|
|
517
|
+
released = true;
|
|
518
|
+
continue;
|
|
519
|
+
}
|
|
520
|
+
released = false;
|
|
521
|
+
if (Date.now() - st.mtimeMs > SETTINGS_LOCK_STALE_MS) {
|
|
522
|
+
let removed = false;
|
|
523
|
+
try { const again = fs.statSync(lock); if (again.ino === st.ino) { fs.rmSync(lock, {force: true}); removed = true; } } catch (err) { /* gone, or not removed */ }
|
|
524
|
+
if (removed) continue;
|
|
525
|
+
// (a stale lock not removed: waited on, as a held one)
|
|
526
|
+
}
|
|
527
|
+
if (now() >= until) throw busy();
|
|
528
|
+
sleep(50);
|
|
529
|
+
}
|
|
530
|
+
try {
|
|
531
|
+
return fn();
|
|
532
|
+
} finally {
|
|
533
|
+
let held = null;
|
|
534
|
+
try { held = JSON.parse(fs.readFileSync(lock, 'utf8')); } catch (err) { held = null; }
|
|
535
|
+
if (held != null && held.pid === mine.pid && held.at === mine.at) fs.rmSync(lock, {force: true});
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
// A setting's value as countinghouse's option takes it, at start and live
|
|
540
|
+
// alike: requestTimeout is kept in milliseconds and passed in whole seconds,
|
|
541
|
+
// rounded down.
|
|
542
|
+
function runtimeValue(name, value) {
|
|
543
|
+
// rounded down, at least 1 s: a tightened timeout never comes out looser
|
|
544
|
+
// than asked (the review of countinghouse #183)
|
|
545
|
+
if (name === 'requestTimeout') return Math.max(1, Math.floor(value / 1000));
|
|
546
|
+
return value;
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
// Every allowlisted setting with its value, default and bounds, and each
|
|
550
|
+
// installed top-level module's on/off (its value as isOn reads it, a
|
|
551
|
+
// leftover pack.* included). -> [{name, value, default, bounds, guarding}]
|
|
552
|
+
function listSettings(where) {
|
|
553
|
+
const settings = readSettings(where);
|
|
554
|
+
const record = recordOf(where);
|
|
555
|
+
const modules = [...require('./module-roles').topLevel(record)].sort();
|
|
556
|
+
const rows = Object.keys(ALLOWLIST).map((name) => {
|
|
557
|
+
const e = entryOf(name);
|
|
558
|
+
return {name, value: valueOf(settings, name), default: e.default, bounds: boundsOf(e), guarding: e.guarding};
|
|
559
|
+
});
|
|
560
|
+
for (const m of modules) rows.push({name: `enabled.${m}`, value: isOn(settings, record, m), default: true, bounds: boundsOf(onOffEntry), guarding: true});
|
|
561
|
+
return rows;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
module.exports = {ALLOWLIST, sandboxEntry, readSettings, valueOf, loosens, parseSetting, planSetting, planReset, setSetting, shown, resetSettings, listSettings, entryOf, enabledName, isOn, withSettingsLock, runtimeValue, writeModuleValues, historyValue, cutPoints, migratePackSettings, REMOVE};
|