@okfit/engine 0.14.0 → 0.15.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.
@@ -1,4 +1,5 @@
1
1
  import { documentNewline, locate, locateTopLevelScalar, stripBom } from "../verify/locate.js";
2
+ import { formatDigest, locateIntegrity, spliceIntegrityVerified } from "../verify/integrity.js";
2
3
  import { orderVerifyEdits, splice, spliceTopLevelScalar } from "../verify/splice.js";
3
4
  import { DiagnosticRange } from "@okfit/core";
4
5
  import { Effect, Result, Runtime, Schema } from "effect";
@@ -62,25 +63,41 @@ var FrontmatterEdits = class {
62
63
  const edit = spliceTopLevelScalar(target, "status", status, documentNewline(text));
63
64
  return [shift(edit, bom.length)];
64
65
  });
65
- /** Edits that append one verified entry, the same splice `okfit verify` performs. */
66
- static verified = (source, entry) => Effect.gen(function* () {
66
+ /**
67
+ * Edits that append one verified entry and bind it with `integrity.verified`
68
+ * (`content` is the content digest of the source as passed), the same splice
69
+ * `okfit verify` performs. Only the `verified` slot of an existing `integrity`
70
+ * block is rewritten.
71
+ */
72
+ static verified = (source, entry, content) => Effect.gen(function* () {
67
73
  const { text, bom } = stripBom(source);
68
74
  const target = yield* locate(text);
69
75
  if (target._tag === "unsupported") return yield* new UnsupportedFrontmatterError({
70
76
  key: "verified",
71
77
  shape: target.shape
72
78
  });
73
- const edit = splice(target, entry, documentNewline(text));
74
- return [shift(edit, bom.length)];
79
+ const newline = documentNewline(text);
80
+ const verifiedEdit = splice(target, entry, newline);
81
+ const integrity = yield* locateIntegrity(text);
82
+ if (integrity._tag === "unsupported") return yield* new UnsupportedFrontmatterError({
83
+ key: "integrity",
84
+ shape: integrity.shape
85
+ });
86
+ const integrityEdit = spliceIntegrityVerified(integrity, text, {
87
+ ...entry,
88
+ content
89
+ }, newline);
90
+ return orderVerifyEdits(verifiedEdit, void 0, integrityEdit).map((edit) => shift(edit, bom.length));
75
91
  });
76
92
  /**
77
- * Edits that append one verified entry AND set top-level `status`, the combined write
78
- * `okfit verify --stable` performs. A `status` scalar already equal to `status` makes no
79
- * status edit, and equal-offset inserts are merged with `status:` first. Unlike the CLI,
80
- * which reads the parsed status, this always locates `status`, so an unsupported shape
81
- * on it fails even when its value already matches.
93
+ * Edits that append one verified entry, bind it with `integrity.verified` AND set top-level
94
+ * `status`, the combined write `okfit verify --stable` performs. A `status` scalar already
95
+ * equal to `status` makes no status edit, and equal-offset inserts are merged with `status:`
96
+ * first, then `verified:`, then `integrity:`. Unlike the CLI, which reads the parsed status,
97
+ * this always locates `status`, so an unsupported shape on it fails even when its value
98
+ * already matches.
82
99
  */
83
- static verifiedWithStatus = (source, entry, status) => Effect.gen(function* () {
100
+ static verifiedWithStatus = (source, entry, status, content) => Effect.gen(function* () {
84
101
  const { text, bom } = stripBom(source);
85
102
  const located = yield* locate(text);
86
103
  if (located._tag === "unsupported") return yield* new UnsupportedFrontmatterError({
@@ -96,12 +113,23 @@ var FrontmatterEdits = class {
96
113
  shape: target.shape
97
114
  });
98
115
  if (target._tag !== "replaceScalar" || unquote(text.slice(target.start, target.end), target.quote) !== status) statusEdit = spliceTopLevelScalar(target, "status", status, newline);
99
- return orderVerifyEdits(verifiedEdit, statusEdit).map((edit) => shift(edit, bom.length));
116
+ const integrity = yield* locateIntegrity(text);
117
+ if (integrity._tag === "unsupported") return yield* new UnsupportedFrontmatterError({
118
+ key: "integrity",
119
+ shape: integrity.shape
120
+ });
121
+ const integrityEdit = spliceIntegrityVerified(integrity, text, {
122
+ ...entry,
123
+ content
124
+ }, newline);
125
+ return orderVerifyEdits(verifiedEdit, statusEdit, integrityEdit).map((edit) => shift(edit, bom.length));
100
126
  });
101
127
  /**
102
- * Edits that stamp `renders[i].body_sha256` with `digests[i]`: an existing value is replaced in
103
- * place, a missing key is inserted on the line after `renders[i].path` at that key's column.
104
- * Block-style `renders` only; flow style fails with `UnsupportedFrontmatterError`.
128
+ * Edits that stamp `renders[i].body` with `digests[i]` (16 hex, quoted when numeric-looking): an
129
+ * existing value is replaced in place, a missing key is inserted on the line after
130
+ * `renders[i].path` at that key's column. An `undefined` digest skips its entry. A legacy `renders[i].body_sha256` line in the same
131
+ * entry is removed, so an entry never ends up with both keys. Block-style `renders` only; flow
132
+ * style fails with `UnsupportedFrontmatterError`.
105
133
  */
106
134
  static rendersDigests = (source, digests) => Effect.gen(function* () {
107
135
  const { text, bom } = stripBom(source);
@@ -119,13 +147,33 @@ var FrontmatterEdits = class {
119
147
  const newline = documentNewline(text);
120
148
  const edits = [];
121
149
  for (const [index, digest] of digests.entries()) {
122
- const digestSpan = DiagnosticRange.forFrontmatterPath(document, [
150
+ if (digest === void 0) continue;
151
+ const formatted = formatDigest(digest);
152
+ const legacySpan = DiagnosticRange.forFrontmatterPath(document, [
123
153
  "renders",
124
154
  index,
125
155
  "body_sha256"
126
156
  ]);
157
+ if (legacySpan !== void 0 && legacySpan.offset !== block.offset) {
158
+ const start = text.lastIndexOf("\n", legacySpan.offset - 1) + 1;
159
+ const nl = text.indexOf("\n", legacySpan.offset + legacySpan.length);
160
+ const end = nl === -1 ? text.length : nl + 1;
161
+ const prefix = text.slice(start, legacySpan.offset);
162
+ if (!/^[ ]*(?:-[ ]+)*(?:"body_sha256"|'body_sha256'|body_sha256)[ ]*:[ ]*$/u.test(prefix)) return yield* unsupported("flow-mapping");
163
+ if (prefix.includes("-")) return yield* unsupported("dash-prefixed-key");
164
+ edits.push(shift(MarkdownEdit.make({
165
+ offset: start,
166
+ length: end - start,
167
+ content: ""
168
+ }), bom.length));
169
+ }
170
+ const digestSpan = DiagnosticRange.forFrontmatterPath(document, [
171
+ "renders",
172
+ index,
173
+ "body"
174
+ ]);
127
175
  if (digestSpan !== void 0 && digestSpan.offset !== block.offset) {
128
- const content = digestSpan.length === 0 ? ` ${digest}` : digest;
176
+ const content = digestSpan.length === 0 ? ` ${formatted}` : formatted;
129
177
  edits.push(shift(MarkdownEdit.make({
130
178
  offset: digestSpan.offset,
131
179
  length: digestSpan.length,
@@ -148,10 +196,10 @@ var FrontmatterEdits = class {
148
196
  edits.push(shift(MarkdownEdit.make({
149
197
  offset: lineEnd,
150
198
  length: 0,
151
- content: `${newline}${" ".repeat(column)}body_sha256: ${digest}`
199
+ content: `${newline}${" ".repeat(column)}body: ${formatted}`
152
200
  }), bom.length));
153
201
  }
154
- return edits;
202
+ return edits.toSorted((a, b) => a.offset - b.offset);
155
203
  });
156
204
  };
157
205
 
package/errors.js CHANGED
@@ -75,7 +75,8 @@ var VerifyUnsupportedFrontmatterError = class extends Schema.TaggedError()("Veri
75
75
  key: Schema.optionalKey(Schema.Literals([
76
76
  "verified",
77
77
  "status",
78
- "stale_after"
78
+ "stale_after",
79
+ "integrity"
79
80
  ]))
80
81
  }) {
81
82
  [Runtime.errorExitCode] = 3;
@@ -83,6 +84,22 @@ var VerifyUnsupportedFrontmatterError = class extends Schema.TaggedError()("Veri
83
84
  return `"${this.id}"'s ${this.key ?? "verified"} value is a shape okfit verify cannot edit (${this.shape}); edit it by hand`;
84
85
  }
85
86
  };
87
+ /**
88
+ * `okfit verify --revoke` found no `verified` entry by the resolved actor on
89
+ * the concept, so there is nothing to remove. Exit 3, like any other verify
90
+ * outcome that cannot proceed on the concept.
91
+ *
92
+ * @public
93
+ */
94
+ var VerifyNothingToRevokeError = class extends Schema.TaggedError()("VerifyNothingToRevokeError", {
95
+ id: Schema.String,
96
+ actor: Schema.String
97
+ }) {
98
+ [Runtime.errorExitCode] = 3;
99
+ get message() {
100
+ return `"${this.id}" has no verified entry by ${this.actor} to revoke`;
101
+ }
102
+ };
86
103
  /** The error's `message` when it has one as a string, else `String(error)`. */
87
104
  const messageOf = (error) => {
88
105
  if (typeof error === "object" && error !== null && "message" in error) {
@@ -161,7 +178,9 @@ var VerifySelectionError = class extends Schema.TaggedError()("VerifySelectionEr
161
178
  "unknown-type",
162
179
  "status-conflict",
163
180
  "status-and-batch",
164
- "dry-run-needs-verify"
181
+ "dry-run-needs-verify",
182
+ "revoke-needs-id",
183
+ "revoke-conflict"
165
184
  ]),
166
185
  detail: Schema.optionalKey(Schema.String)
167
186
  }) {
@@ -169,6 +188,8 @@ var VerifySelectionError = class extends Schema.TaggedError()("VerifySelectionEr
169
188
  get message() {
170
189
  if (this.reason === "no-selection") return "verify needs a concept id, --all, or --type <Type> (run in a terminal to pick interactively)";
171
190
  if (this.reason === "id-and-batch") return "verify takes either a concept id or --all/--type, not both";
191
+ if (this.reason === "revoke-needs-id") return "--revoke needs a concept id";
192
+ if (this.reason === "revoke-conflict") return `--revoke removes your own verification of one concept; it cannot combine with ${this.detail ?? "other selectors"}`;
172
193
  if (this.reason === "status-conflict") return "verify takes --stable or --draft, not both";
173
194
  if (this.reason === "dry-run-needs-verify") return "--dry-run needs --verify; bare okfit stale writes nothing";
174
195
  if (this.reason === "status-and-batch") return "--stable and --draft need a concept id; batch mode never changes status";
@@ -253,4 +274,4 @@ var QuerySelectionError = class extends Schema.TaggedError()("QuerySelectionErro
253
274
  };
254
275
 
255
276
  //#endregion
256
- export { ConfigMalformedError, ConfigPathNotFoundError, DocumentPathError, InitOverwriteError, QueryConceptNotFoundError, QuerySelectionError, QueryUnknownVocabularyError, SyncPlanStaleError, SyncPublicationConflictError, SyncStagedLogError, VerifyConceptNotFoundError, VerifySelectionError, VerifyUnsupportedFrontmatterError };
277
+ export { ConfigMalformedError, ConfigPathNotFoundError, DocumentPathError, InitOverwriteError, QueryConceptNotFoundError, QuerySelectionError, QueryUnknownVocabularyError, SyncPlanStaleError, SyncPublicationConflictError, SyncStagedLogError, VerifyConceptNotFoundError, VerifyNothingToRevokeError, VerifySelectionError, VerifyUnsupportedFrontmatterError };