@mmerterden/multi-agent-pipeline 23.3.0 → 23.5.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/CHANGELOG.md +73 -0
- package/README.md +27 -8
- package/README.tr.md +28 -9
- package/SECURITY.md +16 -0
- package/docs/adr/0008-installer-modularization-and-secret-leak-defense.md +1 -1
- package/docs/adr/0010-own-code-graph.md +299 -8
- package/docs/architecture.md +3 -3
- package/docs/ecosystem.md +10 -10
- package/docs/engineering.md +4 -4
- package/docs/facts.json +8 -8
- package/docs/features.md +5 -5
- package/docs/performance.md +17 -17
- package/install/_dev-only-files.mjs +9 -0
- package/install/_platform-filter.mjs +2 -55
- package/install/claude.mjs +28 -2
- package/install/codex.mjs +1 -3
- package/install/copilot.mjs +4 -8
- package/install/templates/claude-hooks.json +32 -1
- package/manifest.json +236 -167
- package/package.json +11 -3
- package/pipeline/commands/multi-agent/SKILL.md +1 -1
- package/pipeline/commands/multi-agent/design-check/SKILL.md +3 -2
- package/pipeline/commands/multi-agent/diff-explain/SKILL.md +3 -3
- package/pipeline/commands/multi-agent/graph/SKILL.md +57 -24
- package/pipeline/commands/multi-agent/pair/SKILL.md +92 -0
- package/pipeline/commands/multi-agent/prune-prompts/SKILL.md +1 -1
- package/pipeline/commands/multi-agent/search/SKILL.md +7 -1
- package/pipeline/commands/multi-agent/status/SKILL.md +4 -0
- package/pipeline/commands/multi-agent/sync/SKILL.md +3 -3
- package/pipeline/contract/CHANGELOG.md +63 -0
- package/pipeline/contract/README.md +1 -0
- package/pipeline/contract/build.mjs +13 -1
- package/pipeline/contract/fixtures/error-invalid-request.json +1 -1
- package/pipeline/contract/fixtures/error-unauthorized.json +1 -1
- package/pipeline/contract/fixtures/runs-awaiting-question.json +4 -1
- package/pipeline/contract/fixtures/runs-empty.json +1 -1
- package/pipeline/contract/fixtures/runs-failed.json +4 -1
- package/pipeline/contract/fixtures/runs-old-schema.json +3 -0
- package/pipeline/contract/fixtures/runs-pr-opened-redacted.json +4 -1
- package/pipeline/contract/fixtures/runs-pr-opened.json +4 -1
- package/pipeline/contract/fixtures/runs-running.json +4 -1
- package/pipeline/contract/manifest.json +41 -15
- package/pipeline/contract/types/index.d.ts +81 -3
- package/pipeline/lib/detach-self.mjs +95 -0
- package/pipeline/lib/public-mcp-servers.mjs +33 -0
- package/pipeline/lib/rotated-log.mjs +93 -0
- package/pipeline/lib/stack-detect.sh +1 -1
- package/pipeline/lib/tls-trust.mjs +29 -0
- package/pipeline/lib/usage-lifecycle.mjs +9 -11
- package/pipeline/lib/usage-names.generated.mjs +613 -0
- package/pipeline/lib/usage-run-key.mjs +235 -0
- package/pipeline/lib/usage-vocab.mjs +210 -0
- package/pipeline/multi-agent-refs/analysis/intake.md +2 -2
- package/pipeline/multi-agent-refs/analysis/render.md +1 -1
- package/pipeline/multi-agent-refs/channels/generator.md +4 -5
- package/pipeline/multi-agent-refs/cross-cli-contract.md +3 -3
- package/pipeline/multi-agent-refs/design-check/init-inventory.md +11 -1
- package/pipeline/multi-agent-refs/design-check/scope-and-ledger.md +1 -1
- package/pipeline/multi-agent-refs/features/code-graph.md +146 -11
- package/pipeline/multi-agent-refs/features/code-intelligence.md +16 -14
- package/pipeline/multi-agent-refs/features/dev-critic.md +7 -7
- package/pipeline/multi-agent-refs/features/model-fallback.md +6 -4
- package/pipeline/multi-agent-refs/features/remote-control.md +253 -0
- package/pipeline/multi-agent-refs/features/review-delta.md +1 -1
- package/pipeline/multi-agent-refs/features/review-file-set.md +3 -3
- package/pipeline/multi-agent-refs/features/review-multi-repo.md +1 -1
- package/pipeline/multi-agent-refs/features/session-brief.md +42 -0
- package/pipeline/multi-agent-refs/features/skill-conformance.md +3 -3
- package/pipeline/multi-agent-refs/features/usage-reporting.md +255 -27
- package/pipeline/multi-agent-refs/help/en.md +3 -2
- package/pipeline/multi-agent-refs/help/tr.md +3 -2
- package/pipeline/multi-agent-refs/knowledge.md +1 -1
- package/pipeline/multi-agent-refs/orchestrator/dispatcher.md +2 -2
- package/pipeline/multi-agent-refs/phases/operations.md +2 -2
- package/pipeline/multi-agent-refs/phases/phase-0-init.md +13 -12
- package/pipeline/multi-agent-refs/phases/phase-1-plan.md +4 -4
- package/pipeline/multi-agent-refs/phases/phase-3-review.md +2 -0
- package/pipeline/multi-agent-refs/phases/phase-5-report.md +1 -1
- package/pipeline/multi-agent-refs/platform-parity.md +2 -2
- package/pipeline/multi-agent-refs/wiki-capture.md +3 -17
- package/pipeline/office/index.html +1309 -236
- package/pipeline/office/toolkit.json +1 -1
- package/pipeline/rules/outside-the-pipeline.md +1 -1
- package/pipeline/schemas/activity.schema.json +23 -0
- package/pipeline/schemas/agent-state.schema.json +6 -1
- package/pipeline/schemas/code-graph-calls.schema.json +254 -0
- package/pipeline/schemas/code-graph-docs.schema.json +223 -0
- package/pipeline/schemas/code-graph-infra.schema.json +227 -0
- package/pipeline/schemas/code-graph.schema.json +2 -2
- package/pipeline/schemas/examples/design-check-config.json +27 -0
- package/pipeline/schemas/learning-curve.schema.json +67 -0
- package/pipeline/schemas/mcp-health.schema.json +96 -0
- package/pipeline/schemas/office-jobs.json +69 -2
- package/pipeline/schemas/office-jobs.schema.json +8 -1
- package/pipeline/schemas/phases.json +5 -0
- package/pipeline/schemas/phases.schema.json +8 -0
- package/pipeline/schemas/prefs.schema.json +46 -12
- package/pipeline/schemas/runs-index.schema.json +17 -0
- package/pipeline/schemas/toolkit-path-params.json +78 -8
- package/pipeline/scripts/README.md +1 -0
- package/pipeline/scripts/_code-graph.mjs +184 -103
- package/pipeline/scripts/_contract-core.mjs +1 -1
- package/pipeline/scripts/_contract-routes-office.mjs +33 -0
- package/pipeline/scripts/_diff-impact.mjs +21 -2
- package/pipeline/scripts/_graph-sidecars.mjs +372 -0
- package/pipeline/scripts/_http-post.mjs +103 -115
- package/pipeline/scripts/_safe-read.mjs +57 -0
- package/pipeline/scripts/_skill-practices.mjs +26 -0
- package/pipeline/scripts/_strip-code.mjs +380 -0
- package/pipeline/scripts/activity-snapshot.mjs +13 -1
- package/pipeline/scripts/aggregate-metrics.mjs +3 -5
- package/pipeline/scripts/build-stack-plugins.mjs +1 -47
- package/pipeline/scripts/call-graph/index.mjs +78 -0
- package/pipeline/scripts/call-graph/lang/cpp.mjs +308 -0
- package/pipeline/scripts/call-graph/lang/csharp.mjs +130 -0
- package/pipeline/scripts/call-graph/lang/go.mjs +303 -0
- package/pipeline/scripts/call-graph/lang/java.mjs +370 -0
- package/pipeline/scripts/call-graph/lang/kotlin.mjs +397 -0
- package/pipeline/scripts/call-graph/lang/python.mjs +399 -0
- package/pipeline/scripts/call-graph/lang/rust.mjs +345 -0
- package/pipeline/scripts/call-graph/lang/shared.mjs +230 -0
- package/pipeline/scripts/call-graph/lang/swift.mjs +432 -0
- package/pipeline/scripts/call-graph/lang/typescript.mjs +692 -0
- package/pipeline/scripts/call-graph/lex.mjs +396 -0
- package/pipeline/scripts/call-graph/platform.mjs +86 -0
- package/pipeline/scripts/call-graph/profiles.mjs +89 -0
- package/pipeline/scripts/call-graph/resolve.mjs +1125 -0
- package/pipeline/scripts/call-graph/scan.mjs +267 -0
- package/pipeline/scripts/code-graph-rules/android.json +7 -2
- package/pipeline/scripts/code-graph-rules/cpp.json +99 -0
- package/pipeline/scripts/code-graph-rules/csharp.json +102 -0
- package/pipeline/scripts/code-graph-rules/go.json +2 -1
- package/pipeline/scripts/code-graph-rules/ios.json +3 -1
- package/pipeline/scripts/code-graph-rules/java.json +76 -0
- package/pipeline/scripts/code-graph-rules/node.json +7 -2
- package/pipeline/scripts/code-graph-rules/python.json +6 -2
- package/pipeline/scripts/code-graph-rules/rust.json +105 -0
- package/pipeline/scripts/contract-server.mjs +3 -0
- package/pipeline/scripts/design-check-config.mjs +205 -0
- package/pipeline/scripts/feedback-send.mjs +26 -26
- package/pipeline/scripts/gate-ledger.mjs +2 -1
- package/pipeline/scripts/gen-facts.mjs +1 -2
- package/pipeline/scripts/gen-usage-names.mjs +124 -0
- package/pipeline/scripts/graph-affected.mjs +109 -20
- package/pipeline/scripts/graph-build.mjs +95 -15
- package/pipeline/scripts/graph-export.mjs +272 -0
- package/pipeline/scripts/graph-hint.mjs +15 -2
- package/pipeline/scripts/graph-query.mjs +305 -26
- package/pipeline/scripts/graph-report.mjs +187 -1
- package/pipeline/scripts/health-report.mjs +3 -4
- package/pipeline/scripts/infra/docs.mjs +317 -0
- package/pipeline/scripts/infra/extract.mjs +998 -0
- package/pipeline/scripts/infra/graph.mjs +245 -0
- package/pipeline/scripts/infra/yaml-lite.mjs +311 -0
- package/pipeline/scripts/job-launch.mjs +67 -2
- package/pipeline/scripts/learning-curve.mjs +288 -178
- package/pipeline/scripts/log-metric.sh +10 -1
- package/pipeline/scripts/mcp-health.mjs +309 -0
- package/pipeline/scripts/note-agent-event.mjs +3 -4
- package/pipeline/scripts/note-command-use.mjs +333 -0
- package/pipeline/scripts/note-command-use.sh +126 -0
- package/pipeline/scripts/note-mcp-call.sh +89 -0
- package/pipeline/scripts/note-mcp-failure.mjs +272 -1
- package/pipeline/scripts/note-session.sh +62 -3
- package/pipeline/scripts/note-skill-use.mjs +58 -2
- package/pipeline/scripts/office-build.mjs +115 -1
- package/pipeline/scripts/office-memory.mjs +14 -5
- package/pipeline/scripts/pair.mjs +333 -0
- package/pipeline/scripts/pre-push-check.sh +1 -1
- package/pipeline/scripts/remote/cbor.mjs +128 -0
- package/pipeline/scripts/remote/contract.mjs +98 -0
- package/pipeline/scripts/remote/handler.mjs +330 -0
- package/pipeline/scripts/remote/handles.mjs +57 -0
- package/pipeline/scripts/remote/lifecycle.mjs +186 -0
- package/pipeline/scripts/remote/listener.mjs +204 -0
- package/pipeline/scripts/remote/lockout.mjs +103 -0
- package/pipeline/scripts/remote/nonces.mjs +61 -0
- package/pipeline/scripts/remote/pairing.mjs +519 -0
- package/pipeline/scripts/remote/qr-encode.mjs +459 -0
- package/pipeline/scripts/remote/store.mjs +191 -0
- package/pipeline/scripts/remote/transport.mjs +211 -0
- package/pipeline/scripts/remote/verbs.json +70 -0
- package/pipeline/scripts/remote/verbs.mjs +217 -0
- package/pipeline/scripts/remote/webauthn.mjs +169 -0
- package/pipeline/scripts/remote/wire.mjs +364 -0
- package/pipeline/scripts/remote-listener.mjs +83 -0
- package/pipeline/scripts/route-learn.mjs +4 -4
- package/pipeline/scripts/run-kill.mjs +1 -1
- package/pipeline/scripts/run-write.mjs +13 -2
- package/pipeline/scripts/runs-index.mjs +30 -3
- package/pipeline/scripts/search-logs.sh +9 -0
- package/pipeline/scripts/session-brief.mjs +458 -0
- package/pipeline/scripts/session-brief.sh +130 -0
- package/pipeline/scripts/skill-usage.mjs +16 -5
- package/pipeline/scripts/test-gap-rules/cpp.json +25 -0
- package/pipeline/scripts/test-gap-rules/csharp.json +20 -0
- package/pipeline/scripts/test-gap-rules/java.json +20 -0
- package/pipeline/scripts/test-gap-rules/rust.json +14 -0
- package/pipeline/scripts/token-budget-report.mjs +3 -2
- package/pipeline/scripts/uninstall.mjs +29 -0
- package/pipeline/scripts/usage-access.mjs +16 -31
- package/pipeline/scripts/usage-health.mjs +31 -62
- package/pipeline/scripts/usage-register-beacon.mjs +11 -22
- package/pipeline/scripts/usage-register.mjs +39 -29
- package/pipeline/scripts/usage-report.mjs +579 -178
- package/pipeline/scripts/validate-code-graph.mjs +361 -111
- package/pipeline/scripts/write-state.mjs +39 -0
- package/pipeline/skills/.skill-manifest.json +21 -17
- package/pipeline/skills/.skills-index.json +13 -2
- package/pipeline/skills/shared/README.md +5 -4
- package/pipeline/skills/shared/core/multi-agent-design-check/SKILL.md +1 -1
- package/pipeline/skills/shared/core/multi-agent-diff-explain/SKILL.md +3 -3
- package/pipeline/skills/shared/core/multi-agent-graph/SKILL.md +55 -21
- package/pipeline/skills/shared/core/multi-agent-pair/SKILL.md +41 -0
- package/pipeline/skills/shared/core/multi-agent-prune-prompts/SKILL.md +1 -1
- package/pipeline/skills/shared/core/multi-agent-sync/SKILL.md +3 -3
- package/pipeline/skills/shared/external/app-store-optimization/SKILL.md +2 -3
- package/pipeline/skills/shared/external/app-store-optimization/references/keyword-research-methodology.md +2 -2
- package/pipeline/skills/shared/external/app-store-optimization/references/product-page-variants.md +2 -2
- package/pipeline/skills/shared/external/app-store-review/SKILL.md +11 -4
- package/pipeline/skills/shared/external/build-settings-audit/SKILL.md +1 -1
- package/pipeline/skills/shared/external/build-time-benchmark/SKILL.md +5 -5
- package/pipeline/skills/shared/external/build-time-fixes/SKILL.md +5 -5
- package/pipeline/skills/shared/external/build-time-optimizer/SKILL.md +6 -6
- package/pipeline/skills/shared/external/free-llm-apis/references/providers.md +1 -0
- package/pipeline/skills/shared/external/ios-coding-standard/references/STANDARD.md +2 -2
- package/pipeline/skills/shared/external/ios-haptics/references/core-haptics.md +1 -1
- package/pipeline/skills/shared/external/localization-reuse-map/references/publish-and-snapshot.md +1 -1
- package/pipeline/skills/shared/external/package-build-audit/SKILL.md +3 -3
- package/pipeline/skills/shared/external/reverse-engineer-own-app/SKILL.md +2 -3
- package/pipeline/skills/shared/external/swift-compile-hotspots/SKILL.md +2 -2
- package/pipeline/skills/shared/external/weatherkit/SKILL.md +5 -6
- package/pipeline/skills/skills-index.md +3 -2
- package/docs/FIGMA_PIPELINE.md +0 -137
- package/pipeline/lib/identifier-scan.mjs +0 -156
- package/pipeline/scripts/_renamed-skills.mjs +0 -164
- package/pipeline/scripts/publish-staged.mjs +0 -324
- package/pipeline/skills/renamed.json +0 -30
- package/pipeline/skills/shared/external/ios-coding-standard/scripts/check_structure.py +0 -765
|
@@ -0,0 +1,459 @@
|
|
|
1
|
+
// Zero-dependency QR Code encoder (ISO/IEC 18004) for short payloads such as
|
|
2
|
+
// pairing links: byte mode, UTF-8 input, error-correction level M, versions 1
|
|
3
|
+
// to 10 (up to 213 bytes). The smallest fitting version is chosen
|
|
4
|
+
// automatically and the mask is the one with the lowest penalty under rules
|
|
5
|
+
// N1 to N4; ties go to the lowest mask number.
|
|
6
|
+
//
|
|
7
|
+
// renderHalfBlocks turns the module matrix into plain text using Unicode half
|
|
8
|
+
// blocks, two module rows per text line, with no escape codes.
|
|
9
|
+
|
|
10
|
+
const ECC_LEVEL_M_BITS = 0;
|
|
11
|
+
const MAX_VERSION = 10;
|
|
12
|
+
const FORMAT_GENERATOR = 0x537;
|
|
13
|
+
const FORMAT_XOR_MASK = 0x5412;
|
|
14
|
+
const VERSION_GENERATOR = 0x1f25;
|
|
15
|
+
const GF_REDUCTION = 0x11d;
|
|
16
|
+
const PAD_BYTES = [0xec, 0x11];
|
|
17
|
+
const FINDER_SIZE = 7;
|
|
18
|
+
const TIMING_INDEX = 6;
|
|
19
|
+
const ALIGNMENT_RADIUS = 2;
|
|
20
|
+
|
|
21
|
+
const PENALTY_RUN_BASE = 3;
|
|
22
|
+
const PENALTY_RUN_MIN = 5;
|
|
23
|
+
const PENALTY_BLOCK = 3;
|
|
24
|
+
const PENALTY_FINDER_LIKE = 40;
|
|
25
|
+
const PENALTY_BALANCE_STEP = 10;
|
|
26
|
+
const FINDER_LIKE_LENGTH = 11;
|
|
27
|
+
const FINDER_LIKE_A = [1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0];
|
|
28
|
+
const FINDER_LIKE_B = [0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1];
|
|
29
|
+
|
|
30
|
+
// Per version: error-correction codewords per block, data block groups as
|
|
31
|
+
// [block count, data codewords per block], alignment pattern centre
|
|
32
|
+
// coordinates, and remainder bits after the last codeword.
|
|
33
|
+
const VERSION_TABLE = [
|
|
34
|
+
null,
|
|
35
|
+
{ ecc: 10, groups: [[1, 16]], align: [], remainder: 0 },
|
|
36
|
+
{ ecc: 16, groups: [[1, 28]], align: [6, 18], remainder: 7 },
|
|
37
|
+
{ ecc: 26, groups: [[1, 44]], align: [6, 22], remainder: 7 },
|
|
38
|
+
{ ecc: 18, groups: [[2, 32]], align: [6, 26], remainder: 7 },
|
|
39
|
+
{ ecc: 24, groups: [[2, 43]], align: [6, 30], remainder: 7 },
|
|
40
|
+
{ ecc: 16, groups: [[4, 27]], align: [6, 34], remainder: 7 },
|
|
41
|
+
{ ecc: 18, groups: [[4, 31]], align: [6, 22, 38], remainder: 0 },
|
|
42
|
+
{
|
|
43
|
+
ecc: 22,
|
|
44
|
+
groups: [
|
|
45
|
+
[2, 38],
|
|
46
|
+
[2, 39],
|
|
47
|
+
],
|
|
48
|
+
align: [6, 24, 42],
|
|
49
|
+
remainder: 0,
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
ecc: 22,
|
|
53
|
+
groups: [
|
|
54
|
+
[3, 36],
|
|
55
|
+
[2, 37],
|
|
56
|
+
],
|
|
57
|
+
align: [6, 26, 46],
|
|
58
|
+
remainder: 0,
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
ecc: 26,
|
|
62
|
+
groups: [
|
|
63
|
+
[4, 43],
|
|
64
|
+
[1, 44],
|
|
65
|
+
],
|
|
66
|
+
align: [6, 28, 50],
|
|
67
|
+
remainder: 0,
|
|
68
|
+
},
|
|
69
|
+
];
|
|
70
|
+
|
|
71
|
+
const MASK_PREDICATES = [
|
|
72
|
+
(r, c) => (r + c) % 2 === 0,
|
|
73
|
+
(r) => r % 2 === 0,
|
|
74
|
+
(_r, c) => c % 3 === 0,
|
|
75
|
+
(r, c) => (r + c) % 3 === 0,
|
|
76
|
+
(r, c) => (Math.floor(r / 2) + Math.floor(c / 3)) % 2 === 0,
|
|
77
|
+
(r, c) => ((r * c) % 2) + ((r * c) % 3) === 0,
|
|
78
|
+
(r, c) => (((r * c) % 2) + ((r * c) % 3)) % 2 === 0,
|
|
79
|
+
(r, c) => (((r + c) % 2) + ((r * c) % 3)) % 2 === 0,
|
|
80
|
+
];
|
|
81
|
+
|
|
82
|
+
function dataCapacity(version) {
|
|
83
|
+
return VERSION_TABLE[version].groups.reduce((sum, [count, size]) => sum + count * size, 0);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function countFieldBits(version) {
|
|
87
|
+
return version < MAX_VERSION ? 8 : 16;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function gfMultiply(a, b) {
|
|
91
|
+
let product = 0;
|
|
92
|
+
let x = a;
|
|
93
|
+
let y = b;
|
|
94
|
+
while (y > 0) {
|
|
95
|
+
if (y & 1) product ^= x;
|
|
96
|
+
x <<= 1;
|
|
97
|
+
if (x & 0x100) x ^= GF_REDUCTION;
|
|
98
|
+
y >>= 1;
|
|
99
|
+
}
|
|
100
|
+
return product;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Monic generator polynomial of the given degree, highest power first.
|
|
104
|
+
function generatorPolynomial(degree) {
|
|
105
|
+
let poly = [1];
|
|
106
|
+
let root = 1;
|
|
107
|
+
for (let i = 0; i < degree; i++) {
|
|
108
|
+
const next = new Array(poly.length + 1).fill(0);
|
|
109
|
+
for (let j = 0; j < poly.length; j++) {
|
|
110
|
+
next[j] ^= poly[j];
|
|
111
|
+
next[j + 1] ^= gfMultiply(poly[j], root);
|
|
112
|
+
}
|
|
113
|
+
poly = next;
|
|
114
|
+
root = gfMultiply(root, 2);
|
|
115
|
+
}
|
|
116
|
+
return poly;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Remainder of data(x) * x^degree divided by the generator, by shift register.
|
|
120
|
+
function reedSolomonRemainder(data, generator) {
|
|
121
|
+
const degree = generator.length - 1;
|
|
122
|
+
const register = new Array(degree).fill(0);
|
|
123
|
+
for (const byte of data) {
|
|
124
|
+
const factor = byte ^ register.shift();
|
|
125
|
+
register.push(0);
|
|
126
|
+
for (let i = 0; i < degree; i++) register[i] ^= gfMultiply(generator[i + 1], factor);
|
|
127
|
+
}
|
|
128
|
+
return register;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function appendBits(bits, value, count) {
|
|
132
|
+
for (let i = count - 1; i >= 0; i--) bits.push((value >>> i) & 1);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function pickVersion(byteLength) {
|
|
136
|
+
for (let version = 1; version <= MAX_VERSION; version++) {
|
|
137
|
+
if (4 + countFieldBits(version) + 8 * byteLength <= 8 * dataCapacity(version)) return version;
|
|
138
|
+
}
|
|
139
|
+
throw new RangeError("qr: payload too long");
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function buildDataCodewords(bytes, version) {
|
|
143
|
+
const capacityBits = 8 * dataCapacity(version);
|
|
144
|
+
const bits = [];
|
|
145
|
+
appendBits(bits, 0b0100, 4);
|
|
146
|
+
appendBits(bits, bytes.length, countFieldBits(version));
|
|
147
|
+
for (const byte of bytes) appendBits(bits, byte, 8);
|
|
148
|
+
appendBits(bits, 0, Math.min(4, capacityBits - bits.length));
|
|
149
|
+
while (bits.length % 8 !== 0) bits.push(0);
|
|
150
|
+
const codewords = [];
|
|
151
|
+
for (let i = 0; i < bits.length; i += 8) {
|
|
152
|
+
let value = 0;
|
|
153
|
+
for (let j = 0; j < 8; j++) value = (value << 1) | bits[i + j];
|
|
154
|
+
codewords.push(value);
|
|
155
|
+
}
|
|
156
|
+
for (let i = 0; codewords.length < dataCapacity(version); i++) {
|
|
157
|
+
codewords.push(PAD_BYTES[i % 2]);
|
|
158
|
+
}
|
|
159
|
+
return codewords;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Splits the data into blocks, appends each block's error-correction
|
|
163
|
+
// codewords, and interleaves everything into transmission order.
|
|
164
|
+
function interleaveWithEcc(dataCodewords, version) {
|
|
165
|
+
const { ecc, groups } = VERSION_TABLE[version];
|
|
166
|
+
const generator = generatorPolynomial(ecc);
|
|
167
|
+
const dataBlocks = [];
|
|
168
|
+
let offset = 0;
|
|
169
|
+
for (const [count, size] of groups) {
|
|
170
|
+
for (let i = 0; i < count; i++) {
|
|
171
|
+
dataBlocks.push(dataCodewords.slice(offset, offset + size));
|
|
172
|
+
offset += size;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
const eccBlocks = dataBlocks.map((block) => reedSolomonRemainder(block, generator));
|
|
176
|
+
const longest = Math.max(...dataBlocks.map((block) => block.length));
|
|
177
|
+
const out = [];
|
|
178
|
+
for (let i = 0; i < longest; i++) {
|
|
179
|
+
for (const block of dataBlocks) if (i < block.length) out.push(block[i]);
|
|
180
|
+
}
|
|
181
|
+
for (let i = 0; i < ecc; i++) {
|
|
182
|
+
for (const block of eccBlocks) out.push(block[i]);
|
|
183
|
+
}
|
|
184
|
+
return out;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Appends the BCH remainder: returns (data << shift) | (remainder of data << shift by generator).
|
|
188
|
+
function bchEncode(data, generator, shift) {
|
|
189
|
+
let value = data << shift;
|
|
190
|
+
const generatorLength = 32 - Math.clz32(generator);
|
|
191
|
+
for (let i = 32 - Math.clz32(value) - generatorLength; i >= 0; i--) {
|
|
192
|
+
if ((value >>> (i + generatorLength - 1)) & 1) value ^= generator << i;
|
|
193
|
+
}
|
|
194
|
+
return (data << shift) | value;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function createGrid(size) {
|
|
198
|
+
return {
|
|
199
|
+
size,
|
|
200
|
+
dark: Array.from({ length: size }, () => new Array(size).fill(false)),
|
|
201
|
+
isFunction: Array.from({ length: size }, () => new Array(size).fill(false)),
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function setFunction(grid, row, col, dark) {
|
|
206
|
+
grid.dark[row][col] = dark;
|
|
207
|
+
grid.isFunction[row][col] = true;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function drawFinder(grid, top, left) {
|
|
211
|
+
for (let dr = -1; dr <= FINDER_SIZE; dr++) {
|
|
212
|
+
for (let dc = -1; dc <= FINDER_SIZE; dc++) {
|
|
213
|
+
const row = top + dr;
|
|
214
|
+
const col = left + dc;
|
|
215
|
+
if (row < 0 || col < 0 || row >= grid.size || col >= grid.size) continue;
|
|
216
|
+
const distance = Math.max(Math.abs(dr - 3), Math.abs(dc - 3));
|
|
217
|
+
setFunction(grid, row, col, distance !== 2 && distance !== 4);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function drawAlignment(grid, centreRow, centreCol) {
|
|
223
|
+
for (let dr = -ALIGNMENT_RADIUS; dr <= ALIGNMENT_RADIUS; dr++) {
|
|
224
|
+
for (let dc = -ALIGNMENT_RADIUS; dc <= ALIGNMENT_RADIUS; dc++) {
|
|
225
|
+
const distance = Math.max(Math.abs(dr), Math.abs(dc));
|
|
226
|
+
setFunction(grid, centreRow + dr, centreCol + dc, distance !== 1);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function drawFunctionPatterns(grid, version) {
|
|
232
|
+
const { size } = grid;
|
|
233
|
+
drawFinder(grid, 0, 0);
|
|
234
|
+
drawFinder(grid, 0, size - FINDER_SIZE);
|
|
235
|
+
drawFinder(grid, size - FINDER_SIZE, 0);
|
|
236
|
+
for (let i = FINDER_SIZE + 1; i < size - FINDER_SIZE - 1; i++) {
|
|
237
|
+
setFunction(grid, TIMING_INDEX, i, i % 2 === 0);
|
|
238
|
+
setFunction(grid, i, TIMING_INDEX, i % 2 === 0);
|
|
239
|
+
}
|
|
240
|
+
const centres = VERSION_TABLE[version].align;
|
|
241
|
+
const last = centres.length - 1;
|
|
242
|
+
for (let i = 0; i <= last; i++) {
|
|
243
|
+
for (let j = 0; j <= last; j++) {
|
|
244
|
+
const overlapsFinder =
|
|
245
|
+
(i === 0 && j === 0) || (i === 0 && j === last) || (i === last && j === 0);
|
|
246
|
+
if (!overlapsFinder) drawAlignment(grid, centres[i], centres[j]);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
reserveFormatAreas(grid);
|
|
250
|
+
if (version >= 7) reserveVersionAreas(grid);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function reserveFormatAreas(grid) {
|
|
254
|
+
const { size } = grid;
|
|
255
|
+
for (let i = 0; i <= 8; i++) {
|
|
256
|
+
grid.isFunction[8][i] = true;
|
|
257
|
+
grid.isFunction[i][8] = true;
|
|
258
|
+
}
|
|
259
|
+
for (let i = 0; i < 8; i++) {
|
|
260
|
+
grid.isFunction[8][size - 1 - i] = true;
|
|
261
|
+
grid.isFunction[size - 1 - i][8] = true;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function reserveVersionAreas(grid) {
|
|
266
|
+
const { size } = grid;
|
|
267
|
+
for (let i = 0; i < 6; i++) {
|
|
268
|
+
for (let j = 0; j < 3; j++) {
|
|
269
|
+
grid.isFunction[i][size - 11 + j] = true;
|
|
270
|
+
grid.isFunction[size - 11 + j][i] = true;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function writeVersionInfo(grid, version) {
|
|
276
|
+
if (version < 7) return;
|
|
277
|
+
const { size } = grid;
|
|
278
|
+
const bits = bchEncode(version, VERSION_GENERATOR, 12);
|
|
279
|
+
for (let i = 0; i < 18; i++) {
|
|
280
|
+
const bit = ((bits >>> i) & 1) === 1;
|
|
281
|
+
setFunction(grid, Math.floor(i / 3), size - 11 + (i % 3), bit);
|
|
282
|
+
setFunction(grid, size - 11 + (i % 3), Math.floor(i / 3), bit);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// Writes both copies of the 15 format bits and the always-dark module.
|
|
287
|
+
function writeFormatInfo(grid, mask) {
|
|
288
|
+
const { size } = grid;
|
|
289
|
+
const bits = bchEncode((ECC_LEVEL_M_BITS << 3) | mask, FORMAT_GENERATOR, 10) ^ FORMAT_XOR_MASK;
|
|
290
|
+
const bit = (i) => ((bits >>> i) & 1) === 1;
|
|
291
|
+
for (let i = 0; i <= 5; i++) setFunction(grid, i, 8, bit(i));
|
|
292
|
+
setFunction(grid, 7, 8, bit(6));
|
|
293
|
+
setFunction(grid, 8, 8, bit(7));
|
|
294
|
+
setFunction(grid, 8, 7, bit(8));
|
|
295
|
+
for (let i = 9; i < 15; i++) setFunction(grid, 8, 14 - i, bit(i));
|
|
296
|
+
for (let i = 0; i < 8; i++) setFunction(grid, 8, size - 1 - i, bit(i));
|
|
297
|
+
for (let i = 8; i < 15; i++) setFunction(grid, size - 15 + i, 8, bit(i));
|
|
298
|
+
setFunction(grid, size - 8, 8, true);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// Places codeword bits in the two-column zig-zag, bottom right upwards,
|
|
302
|
+
// skipping function modules and the vertical timing column.
|
|
303
|
+
function placeData(grid, codewords, remainderBits) {
|
|
304
|
+
const bits = [];
|
|
305
|
+
for (const codeword of codewords) appendBits(bits, codeword, 8);
|
|
306
|
+
for (let i = 0; i < remainderBits; i++) bits.push(0);
|
|
307
|
+
let index = 0;
|
|
308
|
+
let upward = true;
|
|
309
|
+
for (let right = grid.size - 1; right >= 1; right -= 2) {
|
|
310
|
+
if (right === TIMING_INDEX) right = TIMING_INDEX - 1;
|
|
311
|
+
for (let step = 0; step < grid.size; step++) {
|
|
312
|
+
const row = upward ? grid.size - 1 - step : step;
|
|
313
|
+
for (let k = 0; k < 2; k++) {
|
|
314
|
+
const col = right - k;
|
|
315
|
+
if (grid.isFunction[row][col]) continue;
|
|
316
|
+
grid.dark[row][col] = bits[index++] === 1;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
upward = !upward;
|
|
320
|
+
}
|
|
321
|
+
if (index !== bits.length) throw new Error("qr: data placement mismatch");
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function applyMask(grid, mask) {
|
|
325
|
+
const predicate = MASK_PREDICATES[mask];
|
|
326
|
+
for (let row = 0; row < grid.size; row++) {
|
|
327
|
+
for (let col = 0; col < grid.size; col++) {
|
|
328
|
+
if (!grid.isFunction[row][col] && predicate(row, col)) {
|
|
329
|
+
grid.dark[row][col] = !grid.dark[row][col];
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function lineRunPenalty(line) {
|
|
336
|
+
let penalty = 0;
|
|
337
|
+
let run = 1;
|
|
338
|
+
for (let i = 1; i <= line.length; i++) {
|
|
339
|
+
if (i < line.length && line[i] === line[i - 1]) {
|
|
340
|
+
run++;
|
|
341
|
+
continue;
|
|
342
|
+
}
|
|
343
|
+
if (run >= PENALTY_RUN_MIN) penalty += PENALTY_RUN_BASE + run - PENALTY_RUN_MIN;
|
|
344
|
+
run = 1;
|
|
345
|
+
}
|
|
346
|
+
return penalty;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
function matchesAt(line, start, pattern) {
|
|
350
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
351
|
+
if ((line[start + i] ? 1 : 0) !== pattern[i]) return false;
|
|
352
|
+
}
|
|
353
|
+
return true;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
function lineFinderPenalty(line) {
|
|
357
|
+
let penalty = 0;
|
|
358
|
+
for (let start = 0; start + FINDER_LIKE_LENGTH <= line.length; start++) {
|
|
359
|
+
if (matchesAt(line, start, FINDER_LIKE_A)) penalty += PENALTY_FINDER_LIKE;
|
|
360
|
+
if (matchesAt(line, start, FINDER_LIKE_B)) penalty += PENALTY_FINDER_LIKE;
|
|
361
|
+
}
|
|
362
|
+
return penalty;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
function penaltyScore(dark) {
|
|
366
|
+
const size = dark.length;
|
|
367
|
+
let penalty = 0;
|
|
368
|
+
for (let i = 0; i < size; i++) {
|
|
369
|
+
const column = dark.map((row) => row[i]);
|
|
370
|
+
for (const line of [dark[i], column]) {
|
|
371
|
+
penalty += lineRunPenalty(line) + lineFinderPenalty(line);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
for (let r = 0; r < size - 1; r++) {
|
|
375
|
+
for (let c = 0; c < size - 1; c++) {
|
|
376
|
+
const value = dark[r][c];
|
|
377
|
+
if (dark[r][c + 1] === value && dark[r + 1][c] === value && dark[r + 1][c + 1] === value) {
|
|
378
|
+
penalty += PENALTY_BLOCK;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
const total = size * size;
|
|
383
|
+
const darkCount = dark.reduce((sum, row) => sum + row.filter(Boolean).length, 0);
|
|
384
|
+
const deviation = Math.abs(darkCount * 20 - total * 10);
|
|
385
|
+
const steps = Math.max(0, Math.ceil(deviation / total) - 1);
|
|
386
|
+
return penalty + steps * PENALTY_BALANCE_STEP;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Encodes text as a QR Code symbol.
|
|
391
|
+
* @param {string} text Encoded as UTF-8 in byte mode.
|
|
392
|
+
* @param {{level?: string}} [options] Only level "M" is supported.
|
|
393
|
+
* @returns {{version: number, size: number, modules: boolean[][]}} modules[row][col] is true for dark.
|
|
394
|
+
*/
|
|
395
|
+
export function encodeQr(text, { level = "M" } = {}) {
|
|
396
|
+
if (typeof text !== "string") throw new TypeError("qr: text must be a string");
|
|
397
|
+
if (level !== "M") throw new RangeError("qr: only error-correction level M is supported");
|
|
398
|
+
const bytes = Buffer.from(text, "utf8");
|
|
399
|
+
const version = pickVersion(bytes.length);
|
|
400
|
+
const codewords = interleaveWithEcc(buildDataCodewords(bytes, version), version);
|
|
401
|
+
const size = 17 + 4 * version;
|
|
402
|
+
|
|
403
|
+
const base = createGrid(size);
|
|
404
|
+
drawFunctionPatterns(base, version);
|
|
405
|
+
placeData(base, codewords, VERSION_TABLE[version].remainder);
|
|
406
|
+
|
|
407
|
+
let best = null;
|
|
408
|
+
for (let mask = 0; mask < MASK_PREDICATES.length; mask++) {
|
|
409
|
+
const candidate = {
|
|
410
|
+
size,
|
|
411
|
+
dark: base.dark.map((row) => row.slice()),
|
|
412
|
+
isFunction: base.isFunction,
|
|
413
|
+
};
|
|
414
|
+
applyMask(candidate, mask);
|
|
415
|
+
writeFormatInfo(candidate, mask);
|
|
416
|
+
writeVersionInfo(candidate, version);
|
|
417
|
+
const score = penaltyScore(candidate.dark);
|
|
418
|
+
if (best === null || score < best.score) best = { score, dark: candidate.dark };
|
|
419
|
+
}
|
|
420
|
+
return { version, size, modules: best.dark };
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* Renders a module matrix as text, two module rows per line, using " ",
|
|
425
|
+
* upper half, lower half and full block characters. No escape codes.
|
|
426
|
+
*
|
|
427
|
+
* Default convention: light modules and the quiet zone are drawn as filled
|
|
428
|
+
* blocks and dark modules as blank cells. On a dark terminal the filled
|
|
429
|
+
* blocks are bright, so the symbol shows dark modules on a bright field,
|
|
430
|
+
* which is what a scanner expects. With invert: true dark modules are the
|
|
431
|
+
* filled blocks, which is correct on a light terminal. Rows past the end of
|
|
432
|
+
* the matrix count as quiet zone.
|
|
433
|
+
* @param {boolean[][]} modules
|
|
434
|
+
* @param {{quiet?: number, invert?: boolean}} [options] quiet is the border width in modules.
|
|
435
|
+
* @returns {string} Lines joined by "\n" with no trailing newline.
|
|
436
|
+
*/
|
|
437
|
+
export function renderHalfBlocks(modules, { quiet = 4, invert = false } = {}) {
|
|
438
|
+
const size = modules.length;
|
|
439
|
+
const total = size + 2 * quiet;
|
|
440
|
+
const filled = (row, col) => {
|
|
441
|
+
const r = row - quiet;
|
|
442
|
+
const c = col - quiet;
|
|
443
|
+
const inside = r >= 0 && c >= 0 && r < size && c < size;
|
|
444
|
+
const dark = inside && modules[r][c] === true;
|
|
445
|
+
return invert ? dark : !dark;
|
|
446
|
+
};
|
|
447
|
+
const glyphs = [" ", "\u2584", "\u2580", "\u2588"];
|
|
448
|
+
const lines = [];
|
|
449
|
+
for (let row = 0; row < total; row += 2) {
|
|
450
|
+
let line = "";
|
|
451
|
+
for (let col = 0; col < total; col++) {
|
|
452
|
+
const top = filled(row, col) ? 2 : 0;
|
|
453
|
+
const bottom = filled(row + 1, col) ? 1 : 0;
|
|
454
|
+
line += glyphs[top | bottom];
|
|
455
|
+
}
|
|
456
|
+
lines.push(line);
|
|
457
|
+
}
|
|
458
|
+
return lines.join("\n");
|
|
459
|
+
}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* store.mjs - the local state of remote control: the devices this computer
|
|
3
|
+
* confirmed, and the audit log.
|
|
4
|
+
*
|
|
5
|
+
* Everything lives in one 0700 directory under the autopilot root:
|
|
6
|
+
*
|
|
7
|
+
* devices.json 0600 the pinned devices and their session keys,
|
|
8
|
+
* counters and reply sequence; every change is a
|
|
9
|
+
* locked read-modify-write, replaced atomically
|
|
10
|
+
* audit.jsonl 0600 one line per request: time, device, verb, run handle, outcome;
|
|
11
|
+
* never content, rotated at a size cap
|
|
12
|
+
* listener.json 0600 the background listener's pid record (lifecycle.mjs)
|
|
13
|
+
*
|
|
14
|
+
* A device is added only by pinDevice(), which pairing.mjs calls after the
|
|
15
|
+
* person confirmed the number. Nothing reads a device list from the site
|
|
16
|
+
* into this file.
|
|
17
|
+
*
|
|
18
|
+
* @module pipeline/scripts/remote/store
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { chmodSync, existsSync, mkdirSync, renameSync, rmSync, statSync } from "node:fs";
|
|
22
|
+
import { join } from "node:path";
|
|
23
|
+
import { updateJsonFileSync } from "../../lib/json-file-lock.mjs";
|
|
24
|
+
import { appendFileSafe } from "../../lib/safe-write.mjs";
|
|
25
|
+
import { readRegularJson } from "../../lib/regular-file.mjs";
|
|
26
|
+
import { autopilotRoot } from "../../lib/pr-request-location.mjs";
|
|
27
|
+
import { HANDLE_PATTERN } from "./handles.mjs";
|
|
28
|
+
|
|
29
|
+
export const MAX_DEVICES = 5;
|
|
30
|
+
export const AUDIT_CAP_BYTES = 256 * 1024;
|
|
31
|
+
const STORE_VERSION = 1;
|
|
32
|
+
const DEVICE_ID = /^[A-Za-z0-9-]{8,64}$/;
|
|
33
|
+
|
|
34
|
+
export const remoteDir = (env = process.env) => join(autopilotRoot(env), "remote");
|
|
35
|
+
export const devicesFile = (env = process.env) => join(remoteDir(env), "devices.json");
|
|
36
|
+
export const auditFile = (env = process.env) => join(remoteDir(env), "audit.jsonl");
|
|
37
|
+
export const listenerFile = (env = process.env) => join(remoteDir(env), "listener.json");
|
|
38
|
+
export const ackFile = (env = process.env) => join(remoteDir(env), "ack.json");
|
|
39
|
+
|
|
40
|
+
export const MAX_ACK_IDS = 10;
|
|
41
|
+
const ACK_ID = /^[A-Za-z0-9_-]{1,64}$/;
|
|
42
|
+
|
|
43
|
+
/** The ids of `ids` that fit the wire pattern, at most 10. */
|
|
44
|
+
export const acceptableAcks = (ids) =>
|
|
45
|
+
(Array.isArray(ids) ? ids : [])
|
|
46
|
+
.filter((id) => typeof id === "string" && ACK_ID.test(id))
|
|
47
|
+
.slice(0, MAX_ACK_IDS);
|
|
48
|
+
|
|
49
|
+
/** The request ids the last answer carried that still have to be acknowledged to the site (wire specification 7). */
|
|
50
|
+
export function readPendingAck(env = process.env) {
|
|
51
|
+
const doc = readRegularJson(ackFile(env), null);
|
|
52
|
+
return acceptableAcks(doc?.v === STORE_VERSION ? doc.ids : []);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Replaces the pending acknowledgement; an empty list removes the file. */
|
|
56
|
+
export function writePendingAck(env, ids) {
|
|
57
|
+
const kept = acceptableAcks(ids);
|
|
58
|
+
if (kept.length === 0) {
|
|
59
|
+
rmSync(ackFile(env), { force: true });
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
ensureDir(env);
|
|
63
|
+
updateJsonFileSync(ackFile(env), () => ({ v: STORE_VERSION, ids: kept }), {
|
|
64
|
+
fallback: null,
|
|
65
|
+
mode: 0o600,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function ensureDir(env = process.env) {
|
|
70
|
+
const dir = remoteDir(env);
|
|
71
|
+
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
72
|
+
chmodSync(dir, 0o700);
|
|
73
|
+
return dir;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const empty = () => ({ v: STORE_VERSION, devices: {} });
|
|
77
|
+
|
|
78
|
+
function normalise(doc) {
|
|
79
|
+
if (!doc || typeof doc !== "object" || doc.v !== STORE_VERSION) return empty();
|
|
80
|
+
const devices = {};
|
|
81
|
+
for (const [id, d] of Object.entries(doc.devices ?? {})) {
|
|
82
|
+
if (DEVICE_ID.test(id) && d && typeof d === "object") devices[id] = d;
|
|
83
|
+
}
|
|
84
|
+
return { v: STORE_VERSION, devices };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function readDevices(env = process.env) {
|
|
88
|
+
return normalise(readRegularJson(devicesFile(env), null));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Runs `fn(doc)` under the lock; `fn` mutates the document in place and returns a value. */
|
|
92
|
+
export function mutateDevices(env, fn) {
|
|
93
|
+
ensureDir(env);
|
|
94
|
+
let result;
|
|
95
|
+
updateJsonFileSync(
|
|
96
|
+
devicesFile(env),
|
|
97
|
+
(current) => {
|
|
98
|
+
const doc = normalise(current);
|
|
99
|
+
result = fn(doc);
|
|
100
|
+
return doc;
|
|
101
|
+
},
|
|
102
|
+
{ fallback: null, mode: 0o600 },
|
|
103
|
+
);
|
|
104
|
+
return result;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Pins a confirmed device. `device` carries the identity and keys; the
|
|
109
|
+
* counters start at their initial values. An id that is already pinned is
|
|
110
|
+
* refused so that a second pairing cannot replace a device; it has to be
|
|
111
|
+
* revoked first. The id is part of the pairing transcript, so the phone and
|
|
112
|
+
* this computer agree on it.
|
|
113
|
+
*/
|
|
114
|
+
export function pinDevice(env, id, device) {
|
|
115
|
+
if (!DEVICE_ID.test(id)) throw new Error("device id is not acceptable");
|
|
116
|
+
return mutateDevices(env, (doc) => {
|
|
117
|
+
if (doc.devices[id]) throw new Error("a device with this id is already pinned");
|
|
118
|
+
if (Object.keys(doc.devices).length >= MAX_DEVICES) {
|
|
119
|
+
throw new Error(`at most ${MAX_DEVICES} devices can be paired`);
|
|
120
|
+
}
|
|
121
|
+
doc.devices[id] = {
|
|
122
|
+
...device,
|
|
123
|
+
lastSeq: 0,
|
|
124
|
+
lastCounter: device.lastCounter ?? 0,
|
|
125
|
+
nextReplySeq: 1,
|
|
126
|
+
pairedAt: new Date().toISOString(),
|
|
127
|
+
};
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function removeDevice(env, id) {
|
|
132
|
+
return mutateDevices(env, (doc) => {
|
|
133
|
+
const had = Boolean(doc.devices[id]);
|
|
134
|
+
delete doc.devices[id];
|
|
135
|
+
return had;
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function removeAllDevices(env = process.env) {
|
|
140
|
+
rmSync(devicesFile(env), { force: true });
|
|
141
|
+
rmSync(`${devicesFile(env)}.lock`, { force: true });
|
|
142
|
+
rmSync(ackFile(env), { force: true });
|
|
143
|
+
rmSync(`${ackFile(env)}.lock`, { force: true });
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const AUDIT_OUTCOMES = new Set([
|
|
147
|
+
"ok",
|
|
148
|
+
"refused",
|
|
149
|
+
"dropped",
|
|
150
|
+
"revoked",
|
|
151
|
+
"paired",
|
|
152
|
+
"pair-cancelled",
|
|
153
|
+
"pair-mismatch",
|
|
154
|
+
"off",
|
|
155
|
+
"site-off",
|
|
156
|
+
"listener-start",
|
|
157
|
+
"listener-stop",
|
|
158
|
+
]);
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* One audit line. Only the first 8 characters of the device id, the verb
|
|
162
|
+
* name when it is a contract verb, a fixed outcome, a short reason code and,
|
|
163
|
+
* for a verb about one run, that run's opaque handle (the value the phone
|
|
164
|
+
* holds, never the run id) are written: no argument, answer, key or token
|
|
165
|
+
* ever reaches this function's output.
|
|
166
|
+
*/
|
|
167
|
+
export function audit(env, { device = "", verb = "", outcome, reason = "", run = "" }) {
|
|
168
|
+
const line = {
|
|
169
|
+
t: new Date().toISOString(),
|
|
170
|
+
device: String(device).slice(0, 8),
|
|
171
|
+
verb: /^[a-z][a-z0-9_]{0,31}$/.test(verb) ? verb : verb ? "unknown" : "",
|
|
172
|
+
outcome: AUDIT_OUTCOMES.has(outcome) ? outcome : "dropped",
|
|
173
|
+
reason: /^[a-z0-9:_-]{0,40}$/.test(reason) ? reason : "",
|
|
174
|
+
run: HANDLE_PATTERN.test(run) ? run : "",
|
|
175
|
+
};
|
|
176
|
+
try {
|
|
177
|
+
ensureDir(env);
|
|
178
|
+
rotateAudit(env);
|
|
179
|
+
appendFileSafe(auditFile(env), `${JSON.stringify(line)}\n`, { mode: 0o600 });
|
|
180
|
+
} catch {
|
|
181
|
+
/* an unwritable log never blocks a refusal */
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function rotateAudit(env) {
|
|
186
|
+
const file = auditFile(env);
|
|
187
|
+
if (!existsSync(file)) return;
|
|
188
|
+
if (statSync(file).size < AUDIT_CAP_BYTES) return;
|
|
189
|
+
renameSync(file, `${file}.1`);
|
|
190
|
+
chmodSync(`${file}.1`, 0o600);
|
|
191
|
+
}
|