@nakafa/aksara-contracts 0.47.1 → 0.47.2
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/dist/release/canonical.js +2 -1
- package/dist/release/head.js +2 -1
- package/dist/release/items.d.ts +5 -4
- package/dist/release/items.js +14 -9
- package/dist/release/result/digest.js +4 -4
- package/dist/release/rollback/spec.js +6 -5
- package/dist/release/route/spec.js +4 -3
- package/dist/release/route/verify.d.ts +4 -3
- package/dist/release/route/verify.js +9 -6
- package/dist/release/signing.js +2 -1
- package/dist/release/snapshot/data.js +3 -2
- package/dist/release/snapshot/evidence/tryout.js +6 -1
- package/dist/release/snapshot/spec.js +7 -6
- package/dist/release/snapshot/verify.d.ts +27 -4
- package/dist/release/snapshot/verify.js +13 -9
- package/dist/tryout/closure/accounting.d.ts +5 -3
- package/dist/tryout/closure/accounting.js +8 -7
- package/dist/tryout/closure/locale.js +32 -24
- package/package.json +1 -1
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { encodeJsonText } from "#contracts/text/json";
|
|
1
2
|
/** Serializes one change with stable fields for item digest computation. */
|
|
2
3
|
export function canonicalizeContentChange(change) {
|
|
3
4
|
if (change.operation === "upsert") {
|
|
@@ -21,7 +22,7 @@ export function canonicalizeContentChange(change) {
|
|
|
21
22
|
}
|
|
22
23
|
/** Serializes one release item with stable identity, order, and content. */
|
|
23
24
|
export function canonicalizeContentReleaseItem(item) {
|
|
24
|
-
return
|
|
25
|
+
return encodeJsonText({
|
|
25
26
|
change: canonicalizeContentChange(item.change),
|
|
26
27
|
index: item.index,
|
|
27
28
|
releaseId: item.releaseId,
|
package/dist/release/head.js
CHANGED
|
@@ -4,6 +4,7 @@ import { ContentDeliveryClassSchema } from "#contracts/delivery";
|
|
|
4
4
|
import { ContentKeySchema, CorpusSourcePathSchema, PublicPathSchema, ReleaseIdSchema, Sha256HashSchema, } from "#contracts/ids";
|
|
5
5
|
import { ArtifactLocaleSchema } from "#contracts/locale";
|
|
6
6
|
import { RendererDomainSchema } from "#contracts/renderer/domain";
|
|
7
|
+
import { encodeJsonText } from "#contracts/text/json";
|
|
7
8
|
import { MAX_HEAD_PAGE_COUNT } from "#contracts/transport/limits";
|
|
8
9
|
const HeadCursorSchema = Schema.NullOr(Schema.Trimmed.check(Schema.isNonEmpty()).pipe(Schema.check(Schema.isMaxLength(4096))));
|
|
9
10
|
const ContentHeadFields = {
|
|
@@ -51,7 +52,7 @@ export const ContentHeadSchema = Schema.Union([
|
|
|
51
52
|
]);
|
|
52
53
|
/** Serializes one compact head in stable catalog field order. */
|
|
53
54
|
export function canonicalizeContentHead(head) {
|
|
54
|
-
return
|
|
55
|
+
return encodeJsonText({
|
|
55
56
|
artifactHash: head.artifactHash,
|
|
56
57
|
artifactLocale: head.artifactLocale,
|
|
57
58
|
compilerConfigHash: head.compilerConfigHash,
|
package/dist/release/items.d.ts
CHANGED
|
@@ -56,11 +56,12 @@ declare const ReleaseItemsDigestMismatchError_base: Schema.Class<ReleaseItemsDig
|
|
|
56
56
|
/** The separate ordered items do not match the signed digest. */
|
|
57
57
|
export declare class ReleaseItemsDigestMismatchError extends ReleaseItemsDigestMismatchError_base {
|
|
58
58
|
}
|
|
59
|
+
declare const VerifiedContentReleaseItemsSchema: Schema.Struct<{
|
|
60
|
+
readonly deleteCount: Schema.Finite;
|
|
61
|
+
readonly upsertCount: Schema.Finite;
|
|
62
|
+
}>;
|
|
59
63
|
/** Counts derived without retaining a complete release-item collection. */
|
|
60
|
-
export
|
|
61
|
-
readonly deleteCount: number;
|
|
62
|
-
readonly upsertCount: number;
|
|
63
|
-
}
|
|
64
|
+
export type VerifiedContentReleaseItems = typeof VerifiedContentReleaseItemsSchema.Type;
|
|
64
65
|
/**
|
|
65
66
|
* Strictly decodes a replayable item stream without retaining its bodies.
|
|
66
67
|
* Every evaluation owns fresh ordering and route-collision state.
|
package/dist/release/items.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Effect, Schema, Stream } from "effect";
|
|
1
|
+
import { Effect, MutableRef, Option, Schema, Stream } from "effect";
|
|
2
2
|
import { compareContentHeads } from "#contracts/content";
|
|
3
3
|
import { ReleaseIdSchema, Sha256HashSchema } from "#contracts/ids";
|
|
4
4
|
import { digestItems } from "#contracts/release/digest";
|
|
@@ -37,6 +37,10 @@ export class ReleaseItemsDigestMismatchError extends Schema.TaggedError()("Relea
|
|
|
37
37
|
releaseId: ReleaseIdSchema,
|
|
38
38
|
}) {
|
|
39
39
|
}
|
|
40
|
+
const VerifiedContentReleaseItemsSchema = Schema.Struct({
|
|
41
|
+
deleteCount: Schema.Finite,
|
|
42
|
+
upsertCount: Schema.Finite,
|
|
43
|
+
});
|
|
40
44
|
/** Verifies one item's signed release identity and sequence position. */
|
|
41
45
|
function validateItemIdentity(manifest, item, expectedIndex) {
|
|
42
46
|
if (item.releaseId !== manifest.releaseId) {
|
|
@@ -57,19 +61,20 @@ function validateItemIdentity(manifest, item, expectedIndex) {
|
|
|
57
61
|
return Effect.void;
|
|
58
62
|
}
|
|
59
63
|
/** Rejects a head that is duplicated or outside canonical head order. */
|
|
60
|
-
function validateItemOrder(
|
|
61
|
-
|
|
62
|
-
|
|
64
|
+
function validateItemOrder(previous, item) {
|
|
65
|
+
const last = MutableRef.get(previous);
|
|
66
|
+
if (Option.isSome(last) &&
|
|
67
|
+
compareContentHeads(last.value.change, item.change) >= 0) {
|
|
63
68
|
return Effect.fail(new ReleaseItemOrderError({ itemOffset: item.index }));
|
|
64
69
|
}
|
|
65
|
-
|
|
70
|
+
MutableRef.set(previous, Option.some(item));
|
|
66
71
|
return Effect.void;
|
|
67
72
|
}
|
|
68
73
|
/** Decodes one item and applies stateful canonical stream invariants. */
|
|
69
|
-
const decodeItem = Effect.fn("AksaraContracts.decodeReleaseItem")(function* (manifest,
|
|
74
|
+
const decodeItem = Effect.fn("AksaraContracts.decodeReleaseItem")(function* (manifest, previous, source, itemOffset) {
|
|
70
75
|
const item = yield* Schema.decodeUnknownEffect(ContentReleaseItemSchema)(source, { onExcessProperty: "error" }).pipe(Effect.mapError(() => new ReleaseItemDecodeError({ itemOffset })));
|
|
71
76
|
yield* validateItemIdentity(manifest, item, itemOffset);
|
|
72
|
-
yield* validateItemOrder(
|
|
77
|
+
yield* validateItemOrder(previous, item);
|
|
73
78
|
return item;
|
|
74
79
|
});
|
|
75
80
|
/**
|
|
@@ -78,8 +83,8 @@ const decodeItem = Effect.fn("AksaraContracts.decodeReleaseItem")(function* (man
|
|
|
78
83
|
*/
|
|
79
84
|
export function decodeContentReleaseItems(input) {
|
|
80
85
|
return Stream.unwrap(Effect.sync(() => {
|
|
81
|
-
const
|
|
82
|
-
return input.items.pipe(Stream.zipWithIndex, Stream.mapEffect(([source, itemOffset]) => decodeItem(input.manifest,
|
|
86
|
+
const previous = MutableRef.make(Option.none());
|
|
87
|
+
return input.items.pipe(Stream.zipWithIndex, Stream.mapEffect(([source, itemOffset]) => decodeItem(input.manifest, previous, source, itemOffset)));
|
|
83
88
|
}));
|
|
84
89
|
}
|
|
85
90
|
/** Authenticates a replayable ordered stream against its signed manifest. */
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
|
-
import { Effect, Schema, Stream } from "effect";
|
|
2
|
+
import { Effect, MutableHashSet, Schema, Stream } from "effect";
|
|
3
3
|
import { compareContentHeads, routeIdentity } from "#contracts/content";
|
|
4
4
|
import { ContentKeySchema, PublicPathSchema, ReleaseIdSchema, Sha256HashSchema, } from "#contracts/ids";
|
|
5
5
|
import { AppLocaleSchema, ArtifactLocaleSchema, } from "#contracts/locale";
|
|
@@ -40,7 +40,7 @@ export class ResultCatalogDigestMismatchError extends Schema.TaggedError()("Resu
|
|
|
40
40
|
/** Keeps mutable hash and ordering state private to one catalog replay. */
|
|
41
41
|
class ResultCatalogDigestState {
|
|
42
42
|
#hash = createHash("sha256");
|
|
43
|
-
#routes =
|
|
43
|
+
#routes = MutableHashSet.empty();
|
|
44
44
|
count = 0;
|
|
45
45
|
previous;
|
|
46
46
|
/** Initializes one domain-separated result-catalog hash. */
|
|
@@ -54,10 +54,10 @@ class ResultCatalogDigestState {
|
|
|
54
54
|
appLocale: AppLocaleSchema.make(artifactLocale),
|
|
55
55
|
publicPath,
|
|
56
56
|
});
|
|
57
|
-
if (this.#routes
|
|
57
|
+
if (MutableHashSet.has(this.#routes, identity)) {
|
|
58
58
|
return false;
|
|
59
59
|
}
|
|
60
|
-
this.#routes
|
|
60
|
+
MutableHashSet.add(this.#routes, identity);
|
|
61
61
|
return true;
|
|
62
62
|
}
|
|
63
63
|
/** Adds one canonical compact head and advances ordering evidence. */
|
|
@@ -5,6 +5,7 @@ import { ContentProjectionSchema, canonicalizeContentProjection, familyForProjec
|
|
|
5
5
|
import { canonicalizeContentChange } from "#contracts/release/canonical";
|
|
6
6
|
import { ArticleHeadSchema, canonicalizeContentHead, MaterialHeadSchema, PageHeadSchema, QuestionHeadSchema, } from "#contracts/release/head";
|
|
7
7
|
import { ContentDeleteSchema, ContentUpsertSchema, ReleaseItemIndexSchema, } from "#contracts/release/spec";
|
|
8
|
+
import { encodeJsonText } from "#contracts/text/json";
|
|
8
9
|
/** Maximum complete body rollback page below the publication wire ceiling. */
|
|
9
10
|
export const MAX_ROLLBACK_PAGE_BYTES = 4 * 1024 * 1024;
|
|
10
11
|
/** Maximum byte-bounded rollback transitions requested from one target page. */
|
|
@@ -54,9 +55,9 @@ export const RollbackSnapshotEntrySchema = Schema.Struct({
|
|
|
54
55
|
/** Serializes one authenticated prior state for incremental digest computation. */
|
|
55
56
|
export function canonicalizeRollbackSnapshotEntry(entry) {
|
|
56
57
|
const snapshot = entry.snapshot.state === "absent"
|
|
57
|
-
? `{"artifactLocale":${
|
|
58
|
-
: `{"head":${canonicalizeContentHead(entry.snapshot.head)},"state":${
|
|
59
|
-
return `{"index":${entry.index},"releaseId":${
|
|
58
|
+
? `{"artifactLocale":${encodeJsonText(entry.snapshot.artifactLocale)},"contentKey":${encodeJsonText(entry.snapshot.contentKey)},"family":${encodeJsonText(entry.snapshot.family)},"state":"absent"}`
|
|
59
|
+
: `{"head":${canonicalizeContentHead(entry.snapshot.head)},"state":${encodeJsonText(entry.snapshot.state)}}`;
|
|
60
|
+
return `{"index":${entry.index},"releaseId":${encodeJsonText(entry.releaseId)},"snapshot":${snapshot}}`;
|
|
60
61
|
}
|
|
61
62
|
/** Checks identity coherence across one upsert, artifact, and projection. */
|
|
62
63
|
function hasBoundRollbackUpsert(input) {
|
|
@@ -138,7 +139,7 @@ export function isRollbackUpsert(state) {
|
|
|
138
139
|
}
|
|
139
140
|
/** Serializes one complete rollback state in stable wire field order. */
|
|
140
141
|
function canonicalizeRollbackState(state) {
|
|
141
|
-
const change =
|
|
142
|
+
const change = encodeJsonText(canonicalizeContentChange(state.change));
|
|
142
143
|
if (!isRollbackUpsert(state)) {
|
|
143
144
|
return `{"change":${change}}`;
|
|
144
145
|
}
|
|
@@ -152,5 +153,5 @@ export function canonicalizeRollbackRecord(record) {
|
|
|
152
153
|
export function canonicalizeRollbackPage(page) {
|
|
153
154
|
return `{"done":${page.done},"nextIndex":${page.nextIndex},"records":[${page.records
|
|
154
155
|
.map(canonicalizeRollbackRecord)
|
|
155
|
-
.join(",")}],"rollbackOfManifestHash":${
|
|
156
|
+
.join(",")}],"rollbackOfManifestHash":${encodeJsonText(page.rollbackOfManifestHash)},"rollbackOf":${encodeJsonText(page.rollbackOf)},"total":${page.total}}`;
|
|
156
157
|
}
|
|
@@ -3,6 +3,7 @@ import { Schema } from "effect";
|
|
|
3
3
|
import { ContentKeySchema, PublicPathSchema, ReleaseIdSchema, } from "#contracts/ids";
|
|
4
4
|
import { AppLocaleSchema } from "#contracts/locale";
|
|
5
5
|
import { ReleaseItemIndexSchema } from "#contracts/release/spec";
|
|
6
|
+
import { encodeJsonText } from "#contracts/text/json";
|
|
6
7
|
/** One public route bound to a stable locale-specific content identity. */
|
|
7
8
|
export const ContentRouteBindSchema = Schema.Struct({
|
|
8
9
|
appLocale: AppLocaleSchema,
|
|
@@ -30,11 +31,11 @@ export const ContentRouteItemSchema = Schema.Struct({
|
|
|
30
31
|
/** Serializes one route change in stable wire field order. */
|
|
31
32
|
export function canonicalizeContentRouteChange(change) {
|
|
32
33
|
if (change.operation === "delete") {
|
|
33
|
-
return `{"appLocale":${
|
|
34
|
+
return `{"appLocale":${encodeJsonText(change.appLocale)},"operation":"delete","publicPath":${encodeJsonText(change.publicPath)}}`;
|
|
34
35
|
}
|
|
35
|
-
return `{"appLocale":${
|
|
36
|
+
return `{"appLocale":${encodeJsonText(change.appLocale)},"contentKey":${encodeJsonText(change.contentKey)},"operation":"bind","publicPath":${encodeJsonText(change.publicPath)}}`;
|
|
36
37
|
}
|
|
37
38
|
/** Serializes one indexed route item for signed digest computation. */
|
|
38
39
|
export function canonicalizeContentRouteItem(item) {
|
|
39
|
-
return `{"change":${canonicalizeContentRouteChange(item.change)},"index":${item.index},"releaseId":${
|
|
40
|
+
return `{"change":${canonicalizeContentRouteChange(item.change)},"index":${item.index},"releaseId":${encodeJsonText(item.releaseId)}}`;
|
|
40
41
|
}
|
|
@@ -34,10 +34,11 @@ declare const RouteDigestError_base: Schema.Class<RouteDigestError, Schema.Tagge
|
|
|
34
34
|
/** The streamed route digest differs from its signed manifest. */
|
|
35
35
|
export declare class RouteDigestError extends RouteDigestError_base {
|
|
36
36
|
}
|
|
37
|
+
declare const VerifiedContentRoutesSchema: Schema.Struct<{
|
|
38
|
+
readonly count: Schema.Finite;
|
|
39
|
+
}>;
|
|
37
40
|
/** Count authenticated without retaining complete route rows. */
|
|
38
|
-
export
|
|
39
|
-
readonly count: number;
|
|
40
|
-
}
|
|
41
|
+
export type VerifiedContentRoutes = typeof VerifiedContentRoutesSchema.Type;
|
|
41
42
|
/** Strictly decodes one replayable canonical route stream. */
|
|
42
43
|
export declare function decodeContentRoutes<E, R>(input: {
|
|
43
44
|
readonly manifest: ContentReleaseManifest;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Effect, Schema, Stream } from "effect";
|
|
1
|
+
import { Effect, MutableHashMap, Option, Schema, Stream } from "effect";
|
|
2
2
|
import { routeIdentity } from "#contracts/content";
|
|
3
3
|
import { PublicPathSchema, ReleaseIdSchema, Sha256HashSchema, } from "#contracts/ids";
|
|
4
4
|
import { digestRoutes } from "#contracts/release/route/digest";
|
|
@@ -23,28 +23,31 @@ export class RouteDigestError extends Schema.TaggedError()("RouteDigestError", {
|
|
|
23
23
|
releaseId: ReleaseIdSchema,
|
|
24
24
|
}) {
|
|
25
25
|
}
|
|
26
|
+
const VerifiedContentRoutesSchema = Schema.Struct({
|
|
27
|
+
count: Schema.Finite,
|
|
28
|
+
});
|
|
26
29
|
/** Decodes one route and applies release, index, and uniqueness invariants. */
|
|
27
|
-
function decodeRoute(manifest,
|
|
30
|
+
function decodeRoute(manifest, firstRouteIndex, source, routeOffset) {
|
|
28
31
|
return Schema.decodeUnknownEffect(ContentRouteItemSchema)(source, {
|
|
29
32
|
onExcessProperty: "error",
|
|
30
33
|
}).pipe(Effect.mapError(() => new RouteDecodeError({ routeOffset })), Effect.filterOrFail((item) => item.releaseId === manifest.releaseId && item.index === routeOffset, () => new RouteIdentityError({ routeOffset })), Effect.flatMap((item) => {
|
|
31
34
|
const identity = routeIdentity(item.change);
|
|
32
|
-
const firstIndex =
|
|
35
|
+
const firstIndex = Option.getOrUndefined(MutableHashMap.get(firstRouteIndex, identity));
|
|
33
36
|
if (firstIndex !== undefined) {
|
|
34
37
|
return Effect.fail(new RouteDuplicateError({
|
|
35
38
|
firstIndex,
|
|
36
39
|
publicPath: item.change.publicPath,
|
|
37
40
|
}));
|
|
38
41
|
}
|
|
39
|
-
|
|
42
|
+
MutableHashMap.set(firstRouteIndex, identity, item.index);
|
|
40
43
|
return Effect.succeed(item);
|
|
41
44
|
}));
|
|
42
45
|
}
|
|
43
46
|
/** Strictly decodes one replayable canonical route stream. */
|
|
44
47
|
export function decodeContentRoutes(input) {
|
|
45
48
|
return Stream.unwrap(Effect.sync(() => {
|
|
46
|
-
const
|
|
47
|
-
return input.routes.pipe(Stream.zipWithIndex, Stream.mapEffect(([source, routeOffset]) => decodeRoute(input.manifest,
|
|
49
|
+
const firstRouteIndex = MutableHashMap.empty();
|
|
50
|
+
return input.routes.pipe(Stream.zipWithIndex, Stream.mapEffect(([source, routeOffset]) => decodeRoute(input.manifest, firstRouteIndex, source, routeOffset)));
|
|
48
51
|
}));
|
|
49
52
|
}
|
|
50
53
|
/** Authenticates a replayable route stream against its signed manifest. */
|
package/dist/release/signing.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { canonicalizeReleaseOrigin } from "#contracts/release/origin";
|
|
2
2
|
import { canonicalizePublicationScope } from "#contracts/release/snapshot/scope";
|
|
3
3
|
import { canonicalizeContentSnapshotSet } from "#contracts/release/snapshot/spec";
|
|
4
|
+
import { encodeJsonText } from "#contracts/text/json";
|
|
4
5
|
const CONTENT_RELEASE_SIGNATURE_DOMAIN = "nakafa.aksara.localized-content-release";
|
|
5
6
|
/** Produces stable manifest bytes including locale and source identity. */
|
|
6
7
|
export function canonicalizeContentReleaseManifest(manifest) {
|
|
7
|
-
return
|
|
8
|
+
return encodeJsonText({
|
|
8
9
|
activeAppLocales: manifest.activeAppLocales,
|
|
9
10
|
baseActiveAppLocales: manifest.baseActiveAppLocales,
|
|
10
11
|
baseManifestHash: manifest.baseManifestHash,
|
|
@@ -4,6 +4,7 @@ import { ProgramSnapshotRowSchema } from "#contracts/program/snapshot/row";
|
|
|
4
4
|
import { ProgramSnapshotSchema } from "#contracts/program/snapshot/spec";
|
|
5
5
|
import { QuranSnapshotRowSchema } from "#contracts/quran/snapshot/row";
|
|
6
6
|
import { QuranSnapshotSchema } from "#contracts/quran/snapshot/spec";
|
|
7
|
+
import { encodeJsonText } from "#contracts/text/json";
|
|
7
8
|
import { TryoutCatalogRecordSchema } from "#contracts/tryout/catalog";
|
|
8
9
|
import { TryoutPlacementRecordSchema } from "#contracts/tryout/placement";
|
|
9
10
|
import { TryoutSnapshotSchema } from "#contracts/tryout/snapshot/spec";
|
|
@@ -64,9 +65,9 @@ export function contentSnapshotId(snapshot) {
|
|
|
64
65
|
/** Serializes one structured row with stable envelope field order. */
|
|
65
66
|
export function canonicalizeContentSnapshotRow(row) {
|
|
66
67
|
if (row.family === "program" || row.family === "quran") {
|
|
67
|
-
return
|
|
68
|
+
return encodeJsonText({ family: row.family, record: row.record });
|
|
68
69
|
}
|
|
69
|
-
return
|
|
70
|
+
return encodeJsonText({
|
|
70
71
|
family: row.family,
|
|
71
72
|
record: row.record,
|
|
72
73
|
rowKind: row.rowKind,
|
|
@@ -1,9 +1,14 @@
|
|
|
1
|
-
import { Effect, Result, Stream } from "effect";
|
|
1
|
+
import { Effect, Result, Schema, Stream } from "effect";
|
|
2
2
|
import { requireSnapshotEvidence, } from "#contracts/release/snapshot/evidence/requirement";
|
|
3
3
|
import { verifyTryoutLocaleClosure } from "#contracts/tryout/closure/locale";
|
|
4
4
|
import { digestTryoutCatalog } from "#contracts/tryout/hash/catalog";
|
|
5
5
|
import { digestTryoutPlacements } from "#contracts/tryout/hash/placement";
|
|
6
6
|
import { makeTryoutSnapshot } from "#contracts/tryout/snapshot/hash";
|
|
7
|
+
import { TryoutCatalogCountsSchema } from "#contracts/tryout/snapshot/spec";
|
|
8
|
+
const TryoutCatalogEvidenceSchema = Schema.Struct({
|
|
9
|
+
counts: TryoutCatalogCountsSchema,
|
|
10
|
+
routeCount: Schema.Finite,
|
|
11
|
+
});
|
|
7
12
|
/** Selects current hierarchy rows while preserving source failures. */
|
|
8
13
|
function currentTryoutCatalog(rows) {
|
|
9
14
|
return rows.pipe(Stream.filterMap((row) => row.family === "tryout" && row.rowKind === "catalog"
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/** Signed structured snapshot transition contracts. */
|
|
2
|
-
import { Schema } from "effect";
|
|
2
|
+
import { Record as Rec, Schema } from "effect";
|
|
3
3
|
import { Sha256HashSchema } from "#contracts/ids";
|
|
4
4
|
import { ContentSnapshotKindSchema, } from "#contracts/release/snapshot/scope";
|
|
5
|
+
import { encodeJsonText } from "#contracts/text/json";
|
|
5
6
|
/** Canonical digest for a release that stages no structured snapshot rows. */
|
|
6
7
|
export const EMPTY_SNAPSHOT_ROW_DIGEST = Sha256HashSchema.make("sha256:eb27aa7f59e41b14a3f76d951c5a50cb954a19f3f6e6c44bc21a733f606e888f");
|
|
7
8
|
const RowCountSchema = Schema.Int.pipe(Schema.check(Schema.isGreaterThanOrEqualTo(0)));
|
|
@@ -97,15 +98,15 @@ export function invertContentSnapshots(snapshots) {
|
|
|
97
98
|
}
|
|
98
99
|
/** Checks that an initial release does not claim an unstated snapshot base. */
|
|
99
100
|
export function hasEmptySnapshotBases(snapshots) {
|
|
100
|
-
return
|
|
101
|
+
return Rec.values(snapshots).every(({ baseSnapshotId }) => baseSnapshotId === null);
|
|
101
102
|
}
|
|
102
103
|
/** Checks that a rollback only inherits or restores immutable snapshots. */
|
|
103
104
|
export function hasRollbackSnapshotModes(snapshots) {
|
|
104
|
-
return
|
|
105
|
+
return Rec.values(snapshots).every(({ mode }) => mode !== "replace");
|
|
105
106
|
}
|
|
106
107
|
/** Checks that reviewed Git sources only inherit or replace snapshots. */
|
|
107
108
|
export function hasGitSnapshotModes(snapshots) {
|
|
108
|
-
return
|
|
109
|
+
return Rec.values(snapshots).every(({ mode }) => mode !== "restore");
|
|
109
110
|
}
|
|
110
111
|
/** Returns the number of rows that must be staged for this release. */
|
|
111
112
|
export function snapshotRowCount(snapshots) {
|
|
@@ -133,6 +134,6 @@ export function canonicalizeContentSnapshotSet(snapshots) {
|
|
|
133
134
|
}
|
|
134
135
|
/** Compares two fixed snapshot sets through their stable signed form. */
|
|
135
136
|
export function hasSameContentSnapshots(left, right) {
|
|
136
|
-
return (
|
|
137
|
-
|
|
137
|
+
return (encodeJsonText(canonicalizeContentSnapshotSet(left)) ===
|
|
138
|
+
encodeJsonText(canonicalizeContentSnapshotSet(right)));
|
|
138
139
|
}
|
|
@@ -41,11 +41,34 @@ declare const SnapshotStagedCountError_base: Schema.Class<SnapshotStagedCountErr
|
|
|
41
41
|
/** The staged row stream count differs from all signed family evidence. */
|
|
42
42
|
export declare class SnapshotStagedCountError extends SnapshotStagedCountError_base {
|
|
43
43
|
}
|
|
44
|
+
declare const VerifiedContentSnapshotsSchema: Schema.Struct<{
|
|
45
|
+
readonly snapshots: Schema.Struct<{
|
|
46
|
+
readonly program: Schema.Struct<{
|
|
47
|
+
readonly baseSnapshotId: Schema.NullOr<Schema.brand<Schema.String, "@NakafaAI/AksaraSha256Hash">>;
|
|
48
|
+
readonly mode: Schema.Literals<readonly ["inherit", "replace", "restore"]>;
|
|
49
|
+
readonly resultSnapshotId: Schema.NullOr<Schema.brand<Schema.String, "@NakafaAI/AksaraSha256Hash">>;
|
|
50
|
+
readonly rowCount: Schema.Int;
|
|
51
|
+
readonly rowDigest: Schema.brand<Schema.String, "@NakafaAI/AksaraSha256Hash">;
|
|
52
|
+
}>;
|
|
53
|
+
readonly quran: Schema.Struct<{
|
|
54
|
+
readonly baseSnapshotId: Schema.NullOr<Schema.brand<Schema.String, "@NakafaAI/AksaraSha256Hash">>;
|
|
55
|
+
readonly mode: Schema.Literals<readonly ["inherit", "replace", "restore"]>;
|
|
56
|
+
readonly resultSnapshotId: Schema.NullOr<Schema.brand<Schema.String, "@NakafaAI/AksaraSha256Hash">>;
|
|
57
|
+
readonly rowCount: Schema.Int;
|
|
58
|
+
readonly rowDigest: Schema.brand<Schema.String, "@NakafaAI/AksaraSha256Hash">;
|
|
59
|
+
}>;
|
|
60
|
+
readonly tryout: Schema.Struct<{
|
|
61
|
+
readonly baseSnapshotId: Schema.NullOr<Schema.brand<Schema.String, "@NakafaAI/AksaraSha256Hash">>;
|
|
62
|
+
readonly mode: Schema.Literals<readonly ["inherit", "replace", "restore"]>;
|
|
63
|
+
readonly resultSnapshotId: Schema.NullOr<Schema.brand<Schema.String, "@NakafaAI/AksaraSha256Hash">>;
|
|
64
|
+
readonly rowCount: Schema.Int;
|
|
65
|
+
readonly rowDigest: Schema.brand<Schema.String, "@NakafaAI/AksaraSha256Hash">;
|
|
66
|
+
}>;
|
|
67
|
+
}>;
|
|
68
|
+
readonly stagedRows: Schema.Finite;
|
|
69
|
+
}>;
|
|
44
70
|
/** Fixed verified snapshot state selected by one global release. */
|
|
45
|
-
export
|
|
46
|
-
readonly snapshots: ContentSnapshotSet;
|
|
47
|
-
readonly stagedRows: number;
|
|
48
|
-
}
|
|
71
|
+
export type VerifiedContentSnapshots = typeof VerifiedContentSnapshotsSchema.Type;
|
|
49
72
|
/** Strictly decodes replayable replacement manifests in canonical order. */
|
|
50
73
|
export declare function decodeContentSnapshotManifests<E, R>(manifests: Stream.Stream<unknown, E, R>): Stream.Stream<{
|
|
51
74
|
readonly family: "program";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Effect, Schema, Stream } from "effect";
|
|
1
|
+
import { Effect, MutableRef, Option, Schema, Stream } from "effect";
|
|
2
2
|
import { ContentSnapshotManifestSchema, ContentSnapshotRowSchema, } from "#contracts/release/snapshot/data";
|
|
3
3
|
import { verifySnapshotRows } from "#contracts/release/snapshot/evidence";
|
|
4
4
|
import { ContentSnapshotKindSchema, } from "#contracts/release/snapshot/scope";
|
|
@@ -34,6 +34,10 @@ export class SnapshotStagedCountError extends Schema.TaggedError()("SnapshotStag
|
|
|
34
34
|
verifiedCount: StreamIndexSchema,
|
|
35
35
|
}) {
|
|
36
36
|
}
|
|
37
|
+
const VerifiedContentSnapshotsSchema = Schema.Struct({
|
|
38
|
+
snapshots: ContentSnapshotSetSchema,
|
|
39
|
+
stagedRows: Schema.Finite,
|
|
40
|
+
});
|
|
37
41
|
const familyOrder = {
|
|
38
42
|
program: 0,
|
|
39
43
|
quran: 1,
|
|
@@ -56,28 +60,28 @@ function snapshotRowEvidence(snapshot) {
|
|
|
56
60
|
return tryoutSnapshotRowEvidence(snapshot.manifest);
|
|
57
61
|
}
|
|
58
62
|
/** Strictly decodes one manifest and advances canonical family order. */
|
|
59
|
-
function decodeManifest(
|
|
63
|
+
function decodeManifest(previous, source, manifestIndex) {
|
|
60
64
|
return Schema.decodeUnknownEffect(ContentSnapshotManifestSchema)(source, {
|
|
61
65
|
onExcessProperty: "error",
|
|
62
66
|
}).pipe(Effect.mapError(() => new SnapshotManifestDecodeError({ manifestIndex })), Effect.tap((manifest) => {
|
|
63
|
-
const
|
|
64
|
-
if (
|
|
65
|
-
familyOrder[
|
|
67
|
+
const last = MutableRef.get(previous);
|
|
68
|
+
if (Option.isSome(last) &&
|
|
69
|
+
familyOrder[last.value] >= familyOrder[manifest.family]) {
|
|
66
70
|
return Effect.fail(new SnapshotManifestOrderError({
|
|
67
71
|
actualFamily: manifest.family,
|
|
68
72
|
manifestIndex,
|
|
69
|
-
previousFamily:
|
|
73
|
+
previousFamily: last.value,
|
|
70
74
|
}));
|
|
71
75
|
}
|
|
72
|
-
|
|
76
|
+
MutableRef.set(previous, Option.some(manifest.family));
|
|
73
77
|
return Effect.void;
|
|
74
78
|
}));
|
|
75
79
|
}
|
|
76
80
|
/** Strictly decodes replayable replacement manifests in canonical order. */
|
|
77
81
|
export function decodeContentSnapshotManifests(manifests) {
|
|
78
82
|
return Stream.unwrap(Effect.sync(() => {
|
|
79
|
-
const
|
|
80
|
-
return manifests.pipe(Stream.zipWithIndex, Stream.mapEffect(([source, manifestIndex]) => decodeManifest(
|
|
83
|
+
const previous = MutableRef.make(Option.none());
|
|
84
|
+
return manifests.pipe(Stream.zipWithIndex, Stream.mapEffect(([source, manifestIndex]) => decodeManifest(previous, source, manifestIndex)));
|
|
81
85
|
}));
|
|
82
86
|
}
|
|
83
87
|
/** Strictly decodes replayable structured rows without retaining bodies. */
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import { Effect, Schema } from "effect";
|
|
1
|
+
import { Effect, MutableHashMap, MutableHashSet, Schema } from "effect";
|
|
2
2
|
import type { ActiveAppLocaleList, AppLocale } from "#contracts/locale";
|
|
3
|
+
/** The app locales seen for each logical row identity. */
|
|
4
|
+
type LocalesByIdentity = MutableHashMap.MutableHashMap<string, MutableHashSet.MutableHashSet<AppLocale>>;
|
|
3
5
|
declare const TryoutClosureError_base: Schema.Class<TryoutClosureError, Schema.TaggedStruct<"TryoutClosureError", {
|
|
4
6
|
readonly actual: Schema.String;
|
|
5
7
|
readonly code: Schema.Literals<readonly ["assessed-language", "duplicate-locale", "fact-mismatch", "inactive-locale", "missing-locale", "missing-section", "question-count"]>;
|
|
@@ -10,7 +12,7 @@ declare const TryoutClosureError_base: Schema.Class<TryoutClosureError, Schema.T
|
|
|
10
12
|
export declare class TryoutClosureError extends TryoutClosureError_base {
|
|
11
13
|
}
|
|
12
14
|
/** Adds one active locale to a logical identity without duplicates. */
|
|
13
|
-
export declare function addLocale(localesByIdentity:
|
|
15
|
+
export declare function addLocale(localesByIdentity: LocalesByIdentity, activeAppLocales: ActiveAppLocaleList, identity: string, appLocale: AppLocale): Effect.Effect<void, never, never> | Effect.Effect<never, TryoutClosureError, never>;
|
|
14
16
|
/** Confirms every logical row closes over the exact active locale list. */
|
|
15
|
-
export declare function validateLocales(localesByIdentity:
|
|
17
|
+
export declare function validateLocales(localesByIdentity: LocalesByIdentity, activeAppLocales: ActiveAppLocaleList): Effect.Effect<void, TryoutClosureError, never>;
|
|
16
18
|
export {};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Effect, Schema } from "effect";
|
|
1
|
+
import { Effect, MutableHashMap, MutableHashSet, Option, Schema } from "effect";
|
|
2
2
|
import { encodeJsonText } from "#contracts/text/json";
|
|
3
3
|
/** A try-out snapshot is incomplete or inconsistent across app locales. */
|
|
4
4
|
export class TryoutClosureError extends Schema.TaggedError()("TryoutClosureError", {
|
|
@@ -18,7 +18,7 @@ export class TryoutClosureError extends Schema.TaggedError()("TryoutClosureError
|
|
|
18
18
|
}
|
|
19
19
|
/** Serializes locale sets through the active list's signed canonical order. */
|
|
20
20
|
function localeSetIdentity(locales, activeAppLocales) {
|
|
21
|
-
return encodeJsonText(activeAppLocales.filter((locale) =>
|
|
21
|
+
return encodeJsonText(activeAppLocales.filter((locale) => MutableHashSet.has(locales, locale)));
|
|
22
22
|
}
|
|
23
23
|
/** Adds one active locale to a logical identity without duplicates. */
|
|
24
24
|
export function addLocale(localesByIdentity, activeAppLocales, identity, appLocale) {
|
|
@@ -30,8 +30,9 @@ export function addLocale(localesByIdentity, activeAppLocales, identity, appLoca
|
|
|
30
30
|
identity,
|
|
31
31
|
}));
|
|
32
32
|
}
|
|
33
|
-
const locales =
|
|
34
|
-
|
|
33
|
+
const locales = Option.getOrUndefined(MutableHashMap.get(localesByIdentity, identity)) ??
|
|
34
|
+
MutableHashSet.empty();
|
|
35
|
+
if (MutableHashSet.has(locales, appLocale)) {
|
|
35
36
|
return Effect.fail(new TryoutClosureError({
|
|
36
37
|
actual: appLocale,
|
|
37
38
|
code: "duplicate-locale",
|
|
@@ -39,13 +40,13 @@ export function addLocale(localesByIdentity, activeAppLocales, identity, appLoca
|
|
|
39
40
|
identity,
|
|
40
41
|
}));
|
|
41
42
|
}
|
|
42
|
-
|
|
43
|
-
|
|
43
|
+
MutableHashSet.add(locales, appLocale);
|
|
44
|
+
MutableHashMap.set(localesByIdentity, identity, locales);
|
|
44
45
|
return Effect.void;
|
|
45
46
|
}
|
|
46
47
|
/** Confirms every logical row closes over the exact active locale list. */
|
|
47
48
|
export function validateLocales(localesByIdentity, activeAppLocales) {
|
|
48
|
-
if (
|
|
49
|
+
if (MutableHashMap.size(localesByIdentity) === 0) {
|
|
49
50
|
return Effect.fail(new TryoutClosureError({
|
|
50
51
|
actual: "[]",
|
|
51
52
|
code: "missing-locale",
|
|
@@ -1,14 +1,32 @@
|
|
|
1
|
-
import { Effect, Stream } from "effect";
|
|
1
|
+
import { Effect, MutableHashMap, Option, Stream, } from "effect";
|
|
2
2
|
import { addLocale, TryoutClosureError, validateLocales, } from "#contracts/tryout/closure/accounting";
|
|
3
3
|
import { canonicalizeAssessedLanguagePlacementFacts, canonicalizeLocaleNeutralPlacementFacts, } from "#contracts/tryout/closure/placement";
|
|
4
4
|
import { canonicalizeTryoutCatalogFacts, tryoutCatalogLogicalIdentity, tryoutSectionLogicalIdentity, } from "#contracts/tryout/hash/catalog";
|
|
5
5
|
import { tryoutPlacementLogicalIdentity } from "#contracts/tryout/identity";
|
|
6
6
|
import { makeTryoutScoringFacts, recordTryoutScoringFacts, validateTryoutPlacementScoring, validateTryoutScoringFacts, } from "#contracts/tryout/scoring";
|
|
7
|
+
/** Creates the catalog facts that one stream of catalog rows shares while it folds. */
|
|
8
|
+
function catalogClosureState() {
|
|
9
|
+
return {
|
|
10
|
+
factsByIdentity: MutableHashMap.empty(),
|
|
11
|
+
localesByIdentity: MutableHashMap.empty(),
|
|
12
|
+
scoring: makeTryoutScoringFacts(),
|
|
13
|
+
sections: MutableHashMap.empty(),
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
/** Creates the placement facts that one stream of placements shares while it folds. */
|
|
17
|
+
function placementClosureState() {
|
|
18
|
+
return {
|
|
19
|
+
assessedFacts: MutableHashMap.empty(),
|
|
20
|
+
countsBySectionLocale: MutableHashMap.empty(),
|
|
21
|
+
factsByIdentity: MutableHashMap.empty(),
|
|
22
|
+
localesByIdentity: MutableHashMap.empty(),
|
|
23
|
+
};
|
|
24
|
+
}
|
|
7
25
|
/** Adds one catalog row and compares its locale-neutral facts. */
|
|
8
26
|
function addCatalogRow(state, activeAppLocales, row) {
|
|
9
27
|
const identity = tryoutCatalogLogicalIdentity(row);
|
|
10
28
|
const facts = canonicalizeTryoutCatalogFacts(row);
|
|
11
|
-
const expectedFacts =
|
|
29
|
+
const expectedFacts = Option.getOrUndefined(MutableHashMap.get(state.factsByIdentity, identity));
|
|
12
30
|
if (expectedFacts !== undefined && expectedFacts !== facts) {
|
|
13
31
|
return Effect.fail(new TryoutClosureError({
|
|
14
32
|
actual: facts,
|
|
@@ -17,10 +35,10 @@ function addCatalogRow(state, activeAppLocales, row) {
|
|
|
17
35
|
identity,
|
|
18
36
|
}));
|
|
19
37
|
}
|
|
20
|
-
state.factsByIdentity
|
|
38
|
+
MutableHashMap.set(state.factsByIdentity, identity, facts);
|
|
21
39
|
recordTryoutScoringFacts(state.scoring, row);
|
|
22
40
|
if (row.kind === "section") {
|
|
23
|
-
state.sections
|
|
41
|
+
MutableHashMap.set(state.sections, identity, row.questionCount);
|
|
24
42
|
}
|
|
25
43
|
return addLocale(state.localesByIdentity, activeAppLocales, identity, row.appLocale).pipe(Effect.as(state));
|
|
26
44
|
}
|
|
@@ -28,7 +46,7 @@ function addCatalogRow(state, activeAppLocales, row) {
|
|
|
28
46
|
function addPlacement(state, catalog, activeAppLocales, row) {
|
|
29
47
|
const identity = tryoutPlacementLogicalIdentity(row);
|
|
30
48
|
const sectionIdentity = tryoutSectionLogicalIdentity(row);
|
|
31
|
-
if (!catalog.sections
|
|
49
|
+
if (!MutableHashMap.has(catalog.sections, sectionIdentity)) {
|
|
32
50
|
return Effect.fail(new TryoutClosureError({
|
|
33
51
|
actual: "missing",
|
|
34
52
|
code: "missing-section",
|
|
@@ -37,7 +55,7 @@ function addPlacement(state, catalog, activeAppLocales, row) {
|
|
|
37
55
|
}));
|
|
38
56
|
}
|
|
39
57
|
const facts = canonicalizeLocaleNeutralPlacementFacts(row);
|
|
40
|
-
const expectedFacts =
|
|
58
|
+
const expectedFacts = Option.getOrUndefined(MutableHashMap.get(state.factsByIdentity, identity));
|
|
41
59
|
if (expectedFacts !== undefined && expectedFacts !== facts) {
|
|
42
60
|
return Effect.fail(new TryoutClosureError({
|
|
43
61
|
actual: facts,
|
|
@@ -46,10 +64,10 @@ function addPlacement(state, catalog, activeAppLocales, row) {
|
|
|
46
64
|
identity,
|
|
47
65
|
}));
|
|
48
66
|
}
|
|
49
|
-
state.factsByIdentity
|
|
67
|
+
MutableHashMap.set(state.factsByIdentity, identity, facts);
|
|
50
68
|
if (row.languagePolicy.kind === "fixed") {
|
|
51
69
|
const assessedFacts = canonicalizeAssessedLanguagePlacementFacts(row);
|
|
52
|
-
const expectedAssessedFacts =
|
|
70
|
+
const expectedAssessedFacts = Option.getOrUndefined(MutableHashMap.get(state.assessedFacts, identity));
|
|
53
71
|
if (expectedAssessedFacts !== undefined &&
|
|
54
72
|
expectedAssessedFacts !== assessedFacts) {
|
|
55
73
|
return Effect.fail(new TryoutClosureError({
|
|
@@ -59,17 +77,17 @@ function addPlacement(state, catalog, activeAppLocales, row) {
|
|
|
59
77
|
identity,
|
|
60
78
|
}));
|
|
61
79
|
}
|
|
62
|
-
state.assessedFacts
|
|
80
|
+
MutableHashMap.set(state.assessedFacts, identity, assessedFacts);
|
|
63
81
|
}
|
|
64
82
|
const sectionLocaleIdentity = `${sectionIdentity}\0${row.appLocale}`;
|
|
65
|
-
|
|
83
|
+
const count = Option.getOrElse(MutableHashMap.get(state.countsBySectionLocale, sectionLocaleIdentity), () => 0);
|
|
84
|
+
MutableHashMap.set(state.countsBySectionLocale, sectionLocaleIdentity, count + 1);
|
|
66
85
|
return validateTryoutPlacementScoring(catalog.scoring, row).pipe(Effect.andThen(addLocale(state.localesByIdentity, activeAppLocales, identity, row.appLocale)), Effect.as(state));
|
|
67
86
|
}
|
|
68
87
|
/** Confirms each localized section owns its declared question inventory. */
|
|
69
88
|
function validateQuestionCounts(catalog, placements, activeAppLocales) {
|
|
70
89
|
return Effect.forEach(catalog.sections, ([identity, questionCount]) => Effect.forEach(activeAppLocales, (appLocale) => {
|
|
71
|
-
const actual =
|
|
72
|
-
0;
|
|
90
|
+
const actual = Option.getOrElse(MutableHashMap.get(placements.countsBySectionLocale, `${identity}\0${appLocale}`), () => 0);
|
|
73
91
|
if (actual === questionCount) {
|
|
74
92
|
return Effect.void;
|
|
75
93
|
}
|
|
@@ -86,20 +104,10 @@ function validateQuestionCounts(catalog, placements, activeAppLocales) {
|
|
|
86
104
|
* locales in one pass over each stream.
|
|
87
105
|
*/
|
|
88
106
|
export const verifyTryoutLocaleClosure = Effect.fn("AksaraContracts.verifyTryoutLocaleClosure")(function* (input) {
|
|
89
|
-
const catalog = yield* input.catalog.pipe(Stream.runFoldEffect(() => (
|
|
90
|
-
factsByIdentity: new Map(),
|
|
91
|
-
localesByIdentity: new Map(),
|
|
92
|
-
scoring: makeTryoutScoringFacts(),
|
|
93
|
-
sections: new Map(),
|
|
94
|
-
}), (state, record) => addCatalogRow(state, input.activeAppLocales, record.row)));
|
|
107
|
+
const catalog = yield* input.catalog.pipe(Stream.runFoldEffect(catalogClosureState, (state, record) => addCatalogRow(state, input.activeAppLocales, record.row)));
|
|
95
108
|
yield* validateLocales(catalog.localesByIdentity, input.activeAppLocales);
|
|
96
109
|
yield* validateTryoutScoringFacts(catalog.scoring);
|
|
97
|
-
const placements = yield* input.placements.pipe(Stream.runFoldEffect(() => (
|
|
98
|
-
assessedFacts: new Map(),
|
|
99
|
-
countsBySectionLocale: new Map(),
|
|
100
|
-
factsByIdentity: new Map(),
|
|
101
|
-
localesByIdentity: new Map(),
|
|
102
|
-
}), (state, record) => addPlacement(state, catalog, input.activeAppLocales, record.row)));
|
|
110
|
+
const placements = yield* input.placements.pipe(Stream.runFoldEffect(placementClosureState, (state, record) => addPlacement(state, catalog, input.activeAppLocales, record.row)));
|
|
103
111
|
yield* validateLocales(placements.localesByIdentity, input.activeAppLocales);
|
|
104
112
|
yield* validateQuestionCounts(catalog, placements, input.activeAppLocales);
|
|
105
113
|
});
|
package/package.json
CHANGED