@commonlyai/cli 0.1.306 → 0.1.307
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.
|
@@ -127,6 +127,25 @@ const TOOL_NAME = /^[^\s*?]{1,256}$/;
|
|
|
127
127
|
const RFC3339_UTC = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,9})?Z$/;
|
|
128
128
|
const KIND_SHAPE = /^[a-z][a-z0-9_]*(?:\.[a-z][a-z0-9_]*)*$/;
|
|
129
129
|
const isTimestamp = (value) => typeof value === 'string' && RFC3339_UTC.test(value) && !Number.isNaN(Date.parse(value));
|
|
130
|
+
// Section 8: 128 random bits make a line's hash a commitment nobody can guess at.
|
|
131
|
+
const SALT = /^[0-9a-f]{32}$/;
|
|
132
|
+
const isSalt = (value) => typeof value === 'string' && SALT.test(value);
|
|
133
|
+
const LINE_HASH = /^[0-9a-f]{64}$/;
|
|
134
|
+
// Section 9: an id held beside the line. It is a valid id token, so every
|
|
135
|
+
// writer check and every older reader takes it as an opaque id.
|
|
136
|
+
const COMMITMENT = /^c1:[0-9a-f]{64}$/;
|
|
137
|
+
export const isCommitment = (value) => typeof value === 'string' && COMMITMENT.test(value);
|
|
138
|
+
const isOpenings = (value) => isPlainObject(value) && Object.entries(value).every(([token, opening]) => isCommitment(token)
|
|
139
|
+
&& isPlainObject(opening) && isText(opening.id) && isSalt(opening.salt) && Object.keys(opening).length === 2);
|
|
140
|
+
// Every string on a line, at any depth: where a commitment can sit.
|
|
141
|
+
const idTokens = (value, found = new Set()) => {
|
|
142
|
+
if (typeof value === 'string') found.add(value);
|
|
143
|
+
else if (Array.isArray(value)) value.forEach((item) => idTokens(item, found));
|
|
144
|
+
else if (isPlainObject(value)) Object.values(value).forEach((item) => idTokens(item, found));
|
|
145
|
+
return found;
|
|
146
|
+
};
|
|
147
|
+
const isRedaction = (value) => isPlainObject(value) && Array.isArray(value.fields) && value.fields.every(isText)
|
|
148
|
+
&& (value.hash === null || (typeof value.hash === 'string' && LINE_HASH.test(value.hash)));
|
|
130
149
|
|
|
131
150
|
/**
|
|
132
151
|
* RFC 8785 (JCS) serialization. For the JSON values a record holds, JCS is
|
|
@@ -182,6 +201,15 @@ export const argsDigestOf = (args) => {
|
|
|
182
201
|
};
|
|
183
202
|
export const isArgsDigest = (value) => typeof value === 'string' && ARGS_DIGEST.test(value);
|
|
184
203
|
|
|
204
|
+
/**
|
|
205
|
+
* What a line holds in place of an id it may later have to forget: nobody can
|
|
206
|
+
* test a guess at the id without the salt, which is kept beside the record.
|
|
207
|
+
*/
|
|
208
|
+
export const commitId = (id, salt) => {
|
|
209
|
+
if (!isSalt(salt)) fail('salt must be 32 lowercase hex characters');
|
|
210
|
+
return `c1:${hashLine(`seats.record/commitment/v1\n${salt}\n${id}`)}`;
|
|
211
|
+
};
|
|
212
|
+
|
|
185
213
|
/** null when the actor is well formed, else the reason. */
|
|
186
214
|
const actorProblem = (actor) => {
|
|
187
215
|
if (actor === null) return null;
|
|
@@ -509,8 +537,8 @@ const parseLine = (line) => {
|
|
|
509
537
|
* appendEntry(prevLine, fields, { runActor }?) → the next line, canonical,
|
|
510
538
|
* without its newline.
|
|
511
539
|
*
|
|
512
|
-
* fields: { runId, taskId?, podId?, ts, kind, actor?, data, seq? }. seq
|
|
513
|
-
* prev come from prevLine (null for the first line); a given seq must agree.
|
|
540
|
+
* fields: { runId, taskId?, podId?, ts, kind, actor?, data, seq?, salt? }. seq
|
|
541
|
+
* and prev come from prevLine (null for the first line); a given seq must agree.
|
|
514
542
|
* An absent actor takes the kind's default; null records "unknown".
|
|
515
543
|
*/
|
|
516
544
|
export const appendEntry = (prevLine, fields = {}, { runActor = null } = {}) => {
|
|
@@ -528,6 +556,7 @@ export const appendEntry = (prevLine, fields = {}, { runActor = null } = {}) =>
|
|
|
528
556
|
if (!RUN_ID.test(String(fields.runId))) fail('runId must be 6-64 characters of [A-Za-z0-9_-]');
|
|
529
557
|
optionalText(fields.taskId, 'taskId');
|
|
530
558
|
optionalText(fields.podId, 'podId');
|
|
559
|
+
if (fields.salt !== undefined && !isSalt(fields.salt)) fail('salt must be 32 lowercase hex characters');
|
|
531
560
|
if (fields.taskId && !fields.podId) fail('a taskId needs a podId: task ids repeat across pods');
|
|
532
561
|
const ts = fields.ts instanceof Date ? fields.ts.toISOString() : fields.ts;
|
|
533
562
|
if (!isTimestamp(ts)) fail('ts must be an RFC 3339 UTC timestamp');
|
|
@@ -540,6 +569,7 @@ export const appendEntry = (prevLine, fields = {}, { runActor = null } = {}) =>
|
|
|
540
569
|
runId: fields.runId,
|
|
541
570
|
...(fields.taskId ? { taskId: fields.taskId } : {}),
|
|
542
571
|
...(fields.podId ? { podId: fields.podId } : {}),
|
|
572
|
+
...(fields.salt ? { salt: fields.salt } : {}),
|
|
543
573
|
seq,
|
|
544
574
|
ts,
|
|
545
575
|
kind: fields.kind,
|
|
@@ -569,7 +599,10 @@ const envelopeProblem = (entry) => {
|
|
|
569
599
|
if (typeof entry.kind !== 'string' || !KIND_SHAPE.test(entry.kind)) return 'kind';
|
|
570
600
|
if (actorProblem(entry.actor)) return 'actor';
|
|
571
601
|
if (!isPlainObject(entry.data)) return 'data';
|
|
572
|
-
if (entry.prev !== null && !(typeof entry.prev === 'string' &&
|
|
602
|
+
if (entry.prev !== null && !(typeof entry.prev === 'string' && LINE_HASH.test(entry.prev))) return 'prev';
|
|
603
|
+
if (entry.salt !== undefined && !isSalt(entry.salt)) return 'salt';
|
|
604
|
+
if (entry.redaction !== undefined && !isRedaction(entry.redaction)) return 'redaction';
|
|
605
|
+
if (entry.openings !== undefined && !isOpenings(entry.openings)) return 'openings';
|
|
573
606
|
return null;
|
|
574
607
|
};
|
|
575
608
|
|
|
@@ -589,22 +622,38 @@ const envelopeProblem = (entry) => {
|
|
|
589
622
|
* this one's prev, was changed, removed or moved
|
|
590
623
|
* run_mismatch runId, taskId or podId differs from the first line
|
|
591
624
|
* after_settled a line follows run.settled
|
|
625
|
+
* bad_opening an opening does not match its commitment
|
|
626
|
+
* stray_opening an opening's commitment is not on the line
|
|
592
627
|
* The chain re-syncs after each problem, so one edit is reported once.
|
|
628
|
+
*
|
|
629
|
+
* A redacted view (spec section 8) passes this check by what it can show. A
|
|
630
|
+
* line with a `redaction` marker is not the stored line: its marker names the
|
|
631
|
+
* stored line's hash, which the next line is checked against, or null when
|
|
632
|
+
* that hash is withheld and the next line links to nothing. Only a view adds
|
|
633
|
+
* `partial: true`, `redacted` (the seqs whose content the chain doesn't cover)
|
|
634
|
+
* and `unlinked` (the subset with no hash to link through); `ok` then says no
|
|
635
|
+
* tampering was found in what could be checked, not that every line was.
|
|
636
|
+
*
|
|
637
|
+
* A line with `openings` (section 9) is checked as the stored line it was
|
|
638
|
+
* built from: the map comes off, and the bytes left are what `prev` covers. A
|
|
639
|
+
* commitment nobody opened is not a problem. A deleted id looks exactly so.
|
|
593
640
|
*/
|
|
594
641
|
export const verifyRecord = (input) => {
|
|
595
642
|
const { lines, torn } = Array.isArray(input) ? { lines: input.map(String), torn: null } : splitRecord(input);
|
|
596
643
|
const errors = [];
|
|
597
644
|
const report = (seq, reason, detail) => errors.push(detail ? { seq, reason, detail } : { seq, reason });
|
|
598
645
|
let expected = 1;
|
|
599
|
-
let
|
|
646
|
+
let prevHash = null;
|
|
600
647
|
let first = null;
|
|
601
648
|
let settled = false;
|
|
649
|
+
const redacted = [];
|
|
650
|
+
const unlinked = [];
|
|
602
651
|
for (const line of lines) {
|
|
603
652
|
let entry;
|
|
604
653
|
try { entry = JSON.parse(line); } catch { entry = undefined; }
|
|
605
654
|
if (!isPlainObject(entry)) {
|
|
606
655
|
report(expected, 'unparseable');
|
|
607
|
-
|
|
656
|
+
prevHash = hashLine(line);
|
|
608
657
|
expected += 1;
|
|
609
658
|
continue;
|
|
610
659
|
}
|
|
@@ -616,13 +665,30 @@ export const verifyRecord = (input) => {
|
|
|
616
665
|
try { canonicalForm = canonicalLine(entry); } catch { /* reported below */ }
|
|
617
666
|
if (canonicalForm !== line) report(seq, 'not_canonical');
|
|
618
667
|
if (seq !== expected) report(seq, 'seq_mismatch', `expected ${expected}`);
|
|
619
|
-
|
|
620
|
-
if (entry.prev !== expectedPrev) report(seq, 'prev_mismatch');
|
|
668
|
+
if (entry.prev !== prevHash) report(seq, 'prev_mismatch');
|
|
621
669
|
if (first === null) first = entry;
|
|
622
670
|
else if (entry.runId !== first.runId || entry.taskId !== first.taskId || entry.podId !== first.podId) report(seq, 'run_mismatch');
|
|
623
671
|
if (settled) report(seq, 'after_settled');
|
|
624
672
|
if (entry.kind === 'run.settled') settled = true;
|
|
625
|
-
|
|
673
|
+
const { openings, ...stored } = entry;
|
|
674
|
+
if (isOpenings(openings)) {
|
|
675
|
+
const onLine = idTokens(stored);
|
|
676
|
+
for (const [token, { id, salt }] of Object.entries(openings)) {
|
|
677
|
+
if (commitId(id, salt) !== token) report(seq, 'bad_opening');
|
|
678
|
+
if (!onLine.has(token)) report(seq, 'stray_opening');
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
const marker = isRedaction(entry.redaction) ? entry.redaction : null;
|
|
682
|
+
if (marker) {
|
|
683
|
+
redacted.push(seq);
|
|
684
|
+
if (marker.hash === null) unlinked.push(seq);
|
|
685
|
+
}
|
|
686
|
+
if (marker) prevHash = marker.hash;
|
|
687
|
+
else if (openings === undefined) prevHash = hashLine(line);
|
|
688
|
+
else {
|
|
689
|
+
// What can't be serialized was reported as not_canonical above.
|
|
690
|
+
try { prevHash = hashLine(canonicalLine(stored)); } catch { prevHash = hashLine(line); }
|
|
691
|
+
}
|
|
626
692
|
expected = seq + 1;
|
|
627
693
|
}
|
|
628
694
|
if (torn !== null) report(expected, 'torn');
|
|
@@ -630,16 +696,31 @@ export const verifyRecord = (input) => {
|
|
|
630
696
|
ok: errors.length === 0,
|
|
631
697
|
errors,
|
|
632
698
|
entries: lines.length,
|
|
633
|
-
head:
|
|
699
|
+
head: prevHash,
|
|
634
700
|
settled,
|
|
701
|
+
...(redacted.length > 0 ? { partial: true, redacted, unlinked } : {}),
|
|
702
|
+
};
|
|
703
|
+
};
|
|
704
|
+
|
|
705
|
+
/** An entry with each opened commitment replaced by its id, and no `openings`. */
|
|
706
|
+
const openEntry = ({ openings, ...entry }) => {
|
|
707
|
+
if (!isOpenings(openings)) return entry;
|
|
708
|
+
const open = (value) => {
|
|
709
|
+
if (typeof value === 'string') return Object.hasOwn(openings, value) ? openings[value].id : value;
|
|
710
|
+
if (Array.isArray(value)) return value.map(open);
|
|
711
|
+
return isPlainObject(value) ? Object.fromEntries(Object.entries(value).map(([key, item]) => [key, open(item)])) : value;
|
|
635
712
|
};
|
|
713
|
+
return open(entry);
|
|
636
714
|
};
|
|
637
715
|
|
|
638
|
-
/**
|
|
716
|
+
/**
|
|
717
|
+
* Parse a record's text into entries, skipping a torn tail and unparseable
|
|
718
|
+
* lines. Opened ids read in place; a commitment nobody opened stays as it is.
|
|
719
|
+
*/
|
|
639
720
|
export const readRecord = (text) => splitRecord(text).lines.flatMap((line) => {
|
|
640
721
|
try {
|
|
641
722
|
const entry = JSON.parse(line);
|
|
642
|
-
return isPlainObject(entry) ? [entry] : [];
|
|
723
|
+
return isPlainObject(entry) ? [openEntry(entry)] : [];
|
|
643
724
|
} catch { return []; }
|
|
644
725
|
});
|
|
645
726
|
|