superbee 0.4.0-pre.1 → 0.4.0-pre.3

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/SKILL.md CHANGED
@@ -59,7 +59,7 @@ becoming a second source of truth. Add only the layer justified by current evide
59
59
  explicit publication decision.
60
60
  - Never silently rewrite an established Kind, recipe, or its instances. Inspect dependencies and
61
61
  explain migration consequences first.
62
- - Writes carry an actor (`--actor`/`SUPERBEE_ACTOR`); OKF v0.2 bundles accept only `human:<id>`, `process:<id>`, or `<producer>/<version>` (e.g. `openai/codex`); a bare name is refused with the fix.
62
+ - Writes carry an actor (`--actor`/`SUPERBEE_ACTOR`); OKF v0.2 bundles accept only `human:<id>`, `process:<id>`, or `<producer>/<version>` (e.g. `openai/codex`); a bare name is refused with the fix. After a raw file edit, run `superbee status`: it names a document whose frontmatter no longer parses (quote values containing `: `); sync publishes nothing until it parses.
63
63
 
64
64
  ## Deliver after acceptance
65
65
 
@@ -2997,6 +2997,16 @@ function stringifyDoc(frontmatter2, body) {
2997
2997
  return stringifyWithData(frontmatter2, body);
2998
2998
  }
2999
2999
 
3000
+ // ../core/src/meaningful-change-time.ts
3001
+ function meaningfulChangeTimeField(frontmatter2) {
3002
+ const generated = frontmatter2.generated;
3003
+ if (generated !== null && typeof generated === "object" && !Array.isArray(generated) && generated.at !== void 0) return "generated.at";
3004
+ return "timestamp";
3005
+ }
3006
+ function meaningfulChangeTimeValue(frontmatter2) {
3007
+ return meaningfulChangeTimeField(frontmatter2) === "generated.at" ? frontmatter2.generated.at : frontmatter2.timestamp;
3008
+ }
3009
+
3000
3010
  // ../core/src/errors.ts
3001
3011
  var InvalidInputError = class extends Error {
3002
3012
  constructor(message) {
@@ -3028,6 +3038,58 @@ var ConcurrentReplacementError = class extends Error {
3028
3038
  }
3029
3039
  };
3030
3040
 
3041
+ // ../core/src/verification.ts
3042
+ var TRUST_TIERS = Object.freeze([
3043
+ "unverified",
3044
+ "machine-confirmed",
3045
+ "human-reviewed"
3046
+ ]);
3047
+ var ISO_INSTANT = /^(\d{4})-(\d{2})-(\d{2})[Tt](\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{1,9}))?)?(?:([Zz])|([+-])(\d{2})(?::?(\d{2}))?)$/;
3048
+ function parseIsoInstant(raw, rounding = "truncate") {
3049
+ const match = ISO_INSTANT.exec(raw);
3050
+ if (!match) return null;
3051
+ const [, y, mo, d, h, mi, sec = "0", frac = "0", zulu, sign, oh, om = "0"] = match;
3052
+ const year = Number(y);
3053
+ const month = Number(mo);
3054
+ const day = Number(d);
3055
+ const hour = Number(h);
3056
+ const minute = Number(mi);
3057
+ const second = Number(sec);
3058
+ if (month < 1 || month > 12) return null;
3059
+ const probe = /* @__PURE__ */ new Date(0);
3060
+ probe.setUTCFullYear(year, month, 0);
3061
+ if (day < 1 || day > probe.getUTCDate()) return null;
3062
+ if (hour > 23 || minute > 59 || second > 59) return null;
3063
+ let offsetMinutes = 0;
3064
+ if (!zulu) {
3065
+ const offsetHours = Number(oh);
3066
+ const offsetMins = Number(om);
3067
+ if (offsetHours > 23 || offsetMins > 59) return null;
3068
+ offsetMinutes = (sign === "-" ? -1 : 1) * (offsetHours * 60 + offsetMins);
3069
+ }
3070
+ const millis = Number(frac.padEnd(3, "0").slice(0, 3));
3071
+ const instant = /* @__PURE__ */ new Date(0);
3072
+ instant.setUTCFullYear(year, month - 1, day);
3073
+ instant.setUTCHours(hour, minute, second, millis);
3074
+ const remainder = rounding === "ceil" && /[1-9]/.test(frac.slice(3)) ? 1 : 0;
3075
+ return instant.getTime() - offsetMinutes * 6e4 + remainder;
3076
+ }
3077
+
3078
+ // ../core/src/freshness.ts
3079
+ function parseTimestamp(ts, okfVersion) {
3080
+ if (okfVersion === "0.2") return typeof ts === "string" ? parseIsoInstant(ts) : null;
3081
+ if (ts instanceof Date) {
3082
+ const ms2 = ts.getTime();
3083
+ return Number.isNaN(ms2) ? null : ms2;
3084
+ }
3085
+ if (typeof ts === "number") {
3086
+ return Number.isFinite(ts) ? ts : null;
3087
+ }
3088
+ if (typeof ts !== "string" || ts.trim() === "") return null;
3089
+ const ms = Date.parse(ts);
3090
+ return Number.isNaN(ms) ? null : ms;
3091
+ }
3092
+
3031
3093
  // ../core/src/okf-lifecycle.ts
3032
3094
  var OKF_LIFECYCLE_STATUSES = Object.freeze(["draft", "stable", "deprecated"]);
3033
3095
  function isOkfLifecycleStatus(value) {
@@ -3308,7 +3370,7 @@ async function query(backend, filter = {}, options = {}) {
3308
3370
  }
3309
3371
  async function queryHeads(backend, filter = {}, options = {}) {
3310
3372
  if (backend.queryHeads) {
3311
- const rows = (await backend.queryHeads(filter)).filter((row) => matchesFilter(row, filter));
3373
+ const rows = (await backend.queryHeads(filter, options.onSkip ? { onSkip: options.onSkip } : void 0)).filter((row) => matchesFilter(row, filter));
3312
3374
  rows.sort((a, b) => a.id.localeCompare(b.id));
3313
3375
  return rows;
3314
3376
  }
@@ -3337,11 +3399,11 @@ function toSelectorList(value) {
3337
3399
  if (value === void 0) return void 0;
3338
3400
  return Array.isArray(value) ? value : [value];
3339
3401
  }
3340
- async function queryEdges(backend, filter = {}) {
3402
+ async function queryEdges(backend, filter = {}, options = {}) {
3341
3403
  const fromSelectors = toSelectorList(filter.from)?.map(normalizeEdgeSelector);
3342
3404
  const toSelectors = toSelectorList(filter.to)?.map(normalizeEdgeSelector);
3343
3405
  const edges = [];
3344
- for (const doc of await query(backend)) {
3406
+ for (const doc of await query(backend, {}, options)) {
3345
3407
  for (const link of parseLinksFromDoc(doc)) {
3346
3408
  if (!matchesEdgeSelector(link.from, fromSelectors)) continue;
3347
3409
  if (!matchesEdgeSelector(link.to, toSelectors)) continue;
@@ -4761,8 +4823,8 @@ async function query2(bundle, filter = {}, options = {}) {
4761
4823
  async function queryHeads2(bundle, filter = {}, options = {}) {
4762
4824
  return queryHeads(backendFor(bundle), filter, options);
4763
4825
  }
4764
- async function queryEdges2(bundle, filter = {}) {
4765
- return queryEdges(backendFor(bundle), filter);
4826
+ async function queryEdges2(bundle, filter = {}, options = {}) {
4827
+ return queryEdges(backendFor(bundle), filter, options);
4766
4828
  }
4767
4829
 
4768
4830
  // ../core/src/bundle.ts
@@ -5477,6 +5539,29 @@ function parseRegistration(id, frontmatter2) {
5477
5539
  };
5478
5540
  }
5479
5541
 
5542
+ // ../core/src/query-order.ts
5543
+ function meaningfulChangeOrderKey(id, frontmatter2, okfVersion) {
5544
+ const value = meaningfulChangeTimeValue(frontmatter2);
5545
+ return {
5546
+ id,
5547
+ timestamp: typeof value === "string" ? value : "",
5548
+ timestampMs: parseTimestamp(value, okfVersion ?? void 0)
5549
+ };
5550
+ }
5551
+ function compareByMeaningfulChange(a, b) {
5552
+ if (a.timestampMs !== null && b.timestampMs !== null) {
5553
+ if (a.timestampMs !== b.timestampMs) return b.timestampMs - a.timestampMs;
5554
+ } else if (a.timestampMs !== null) {
5555
+ return -1;
5556
+ } else if (b.timestampMs !== null) {
5557
+ return 1;
5558
+ }
5559
+ return a.id < b.id ? -1 : a.id > b.id ? 1 : 0;
5560
+ }
5561
+ function sortByMeaningfulChange(rows, okfVersion) {
5562
+ return rows.map((row) => ({ row, key: meaningfulChangeOrderKey(row.id, row.frontmatter, okfVersion) })).sort((a, b) => compareByMeaningfulChange(a.key, b.key)).map(({ row }) => row);
5563
+ }
5564
+
5480
5565
  // ../view-runtime/src/bridge.ts
5481
5566
  var BRIDGE_PROTOCOL = "v0";
5482
5567
  var ACTION_BRIDGE_PROTOCOL = "v1";
@@ -5508,6 +5593,7 @@ var BRIDGE_HOST_CAPABILITIES = Object.freeze({
5508
5593
  queryFieldOr: "query.field-or",
5509
5594
  queryOpen: "query.open",
5510
5595
  queryCount: "query.count",
5596
+ queryNewest: "query.newest",
5511
5597
  edges: "edges",
5512
5598
  renderDocument: "render-document",
5513
5599
  openPage: "open-page",
@@ -5522,10 +5608,12 @@ var BRIDGE_SERVICE_CAPABILITIES = Object.freeze([
5522
5608
  BRIDGE_HOST_CAPABILITIES.queryFieldOr,
5523
5609
  BRIDGE_HOST_CAPABILITIES.queryOpen,
5524
5610
  BRIDGE_HOST_CAPABILITIES.queryCount,
5611
+ BRIDGE_HOST_CAPABILITIES.queryNewest,
5525
5612
  BRIDGE_HOST_CAPABILITIES.edges,
5526
5613
  BRIDGE_HOST_CAPABILITIES.graph,
5527
5614
  BRIDGE_HOST_CAPABILITIES.renderDocument
5528
5615
  ]);
5616
+ var BRIDGE_QUERY_ORDERS = Object.freeze(["id", "newest"]);
5529
5617
  var V0_REQUEST_TYPES = /* @__PURE__ */ new Set(["hello", "query", "read", "render-document", "edges", "graph", "subscribe", "host", "open-page"]);
5530
5618
  function isPlainRecord(value) {
5531
5619
  if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
@@ -5554,7 +5642,7 @@ function invalidV0RequestId(value) {
5554
5642
  }
5555
5643
  function normalizeQueryParams(raw) {
5556
5644
  if (!isPlainRecord(raw)) return null;
5557
- const allowed = /* @__PURE__ */ new Set(["type", "prefix", "field", "open", "limit"]);
5645
+ const allowed = /* @__PURE__ */ new Set(["type", "prefix", "field", "open", "limit", "order"]);
5558
5646
  if (Object.keys(raw).some((key) => !allowed.has(key))) return null;
5559
5647
  const out = {};
5560
5648
  if (raw.type !== void 0) {
@@ -5582,6 +5670,10 @@ function normalizeQueryParams(raw) {
5582
5670
  }
5583
5671
  out.limit = raw.limit;
5584
5672
  }
5673
+ if (raw.order !== void 0) {
5674
+ if (!BRIDGE_QUERY_ORDERS.includes(raw.order)) return null;
5675
+ out.order = raw.order;
5676
+ }
5585
5677
  return out;
5586
5678
  }
5587
5679
  function selector(value) {
@@ -5709,9 +5801,19 @@ function fail(id, bridge, code, message) {
5709
5801
  function replyWithinLimit(reply) {
5710
5802
  return Buffer.byteLength(JSON.stringify(reply), "utf8") <= MAX_REPLY_BYTES;
5711
5803
  }
5804
+ function skippedReply(skipped) {
5805
+ if (skipped.length === 0) return {};
5806
+ return { skipped: [...skipped].sort((a, b) => a.id.localeCompare(b.id)).slice(0, MAX_QUERY_ROWS) };
5807
+ }
5712
5808
  function boundedRows(rows, params, kinds) {
5713
5809
  const requested = params.limit === 0 || params.limit === void 0 ? MAX_QUERY_ROWS : Math.min(params.limit, MAX_QUERY_ROWS);
5714
- return applyQuerySelectionFilters(rows, { ...params, limit: requested }, kinds);
5810
+ const { order, ...selection } = params;
5811
+ if (order !== "newest") return applyQuerySelectionFilters(rows, { ...selection, limit: requested }, kinds);
5812
+ const matched = applyQuerySelectionFilters(rows, { ...selection, limit: void 0 }, kinds);
5813
+ return {
5814
+ rows: sortByMeaningfulChange(matched.rows, selection.okfVersion).slice(0, requested),
5815
+ count: matched.count
5816
+ };
5715
5817
  }
5716
5818
  var BridgeService = class {
5717
5819
  constructor(options) {
@@ -5839,7 +5941,8 @@ var BridgeService = class {
5839
5941
  return { status: "reload-required", message };
5840
5942
  }
5841
5943
  async subscriptionSnapshot() {
5842
- const rows = await queryHeads2(this.options.bundle, {});
5944
+ const rows = await queryHeads2(this.options.bundle, {}, { onSkip: () => {
5945
+ } });
5843
5946
  if (rows.length > MAX_SUBSCRIPTION_HEADS) {
5844
5947
  throw new Error("the bundle is too large for the experimental View polling snapshot");
5845
5948
  }
@@ -5856,7 +5959,11 @@ var BridgeService = class {
5856
5959
  * capability before adding them.
5857
5960
  */
5858
5961
  async graph(launch, request) {
5859
- const heads = await queryHeads2(this.options.bundle, {});
5962
+ const skipped = [];
5963
+ const onSkip = (skip) => {
5964
+ if (!skipped.some((row) => row.id === skip.id)) skipped.push({ id: skip.id, reason: skip.reason });
5965
+ };
5966
+ const heads = await queryHeads2(this.options.bundle, {}, { onSkip });
5860
5967
  if (heads.length > GRAPH_MAX_DOCUMENTS) {
5861
5968
  return {
5862
5969
  reply: fail(request.id, request.bridge, "TOO_LARGE", `the graph exceeded ${GRAPH_MAX_DOCUMENTS} documents`)
@@ -5865,7 +5972,7 @@ var BridgeService = class {
5865
5972
  const [registry, declaredOkfVersion, edges] = await Promise.all([
5866
5973
  loadKinds(this.options.bundle),
5867
5974
  readBundleOkfVersion2(this.options.bundle),
5868
- queryEdges2(this.options.bundle, {})
5975
+ queryEdges2(this.options.bundle, {}, { onSkip })
5869
5976
  ]);
5870
5977
  if (edges.length > GRAPH_MAX_RELATIONSHIPS) {
5871
5978
  return {
@@ -5899,7 +6006,8 @@ var BridgeService = class {
5899
6006
  okfVersion,
5900
6007
  documents,
5901
6008
  relationships,
5902
- counts: { documents: documents.length, relationships: relationships.length }
6009
+ counts: { documents: documents.length, relationships: relationships.length },
6010
+ ...skippedReply(skipped)
5903
6011
  })
5904
6012
  };
5905
6013
  }
@@ -5957,10 +6065,14 @@ var BridgeService = class {
5957
6065
  return { reply: ok(request.id, request.bridge, request.type, { capability: request.capability, output: outcome.output }) };
5958
6066
  }
5959
6067
  if (request.type === "query") {
6068
+ if (request.params.order !== void 0 && !this.options.host.capabilities.includes(BRIDGE_HOST_CAPABILITIES.queryNewest)) {
6069
+ return { reply: fail(request.id, request.bridge, "USAGE", "invalid or unsupported bridge request") };
6070
+ }
6071
+ const skipped = [];
5960
6072
  const rows = await queryHeads2(this.options.bundle, {
5961
6073
  ...request.params.type ? { type: request.params.type } : {},
5962
6074
  ...request.params.prefix ? { prefix: request.params.prefix } : {}
5963
- });
6075
+ }, { onSkip: (skip) => skipped.push({ id: skip.id, reason: skip.reason }) });
5964
6076
  const [registry, okfVersion] = await Promise.all([
5965
6077
  loadKinds(this.options.bundle),
5966
6078
  readBundleOkfVersion2(this.options.bundle)
@@ -5974,7 +6086,7 @@ var BridgeService = class {
5974
6086
  const kind = registry.kinds.get(String(row.frontmatter.type ?? ""));
5975
6087
  return kind ? { ...row, frontmatter: projectLogicalKindFields(okfVersion, kind, row.frontmatter) } : row;
5976
6088
  });
5977
- return { reply: ok(request.id, request.bridge, request.type, result) };
6089
+ return { reply: ok(request.id, request.bridge, request.type, { ...result, ...skippedReply(skipped) }) };
5978
6090
  }
5979
6091
  if (request.type === "read" || request.type === "read-versioned") {
5980
6092
  const [result, registry, okfVersion] = await Promise.all([
@@ -6026,12 +6138,21 @@ var BridgeService = class {
6026
6138
  };
6027
6139
  }
6028
6140
  if (request.type === "edges") {
6029
- const edges = await queryEdges2(this.options.bundle, request.params);
6141
+ const skipped = [];
6142
+ const edges = await queryEdges2(this.options.bundle, request.params, {
6143
+ onSkip: (skip) => skipped.push({ id: skip.id, reason: skip.reason })
6144
+ });
6030
6145
  if (edges.length > MAX_EDGE_ROWS) {
6031
6146
  return { reply: fail(request.id, request.bridge, "TOO_LARGE", `the edge query exceeded ${MAX_EDGE_ROWS} rows`) };
6032
6147
  }
6033
6148
  const projected = edges.map(({ from, to, text }) => ({ from, to, text }));
6034
- return { reply: ok(request.id, request.bridge, request.type, { edges: projected, count: projected.length }) };
6149
+ return {
6150
+ reply: ok(request.id, request.bridge, request.type, {
6151
+ edges: projected,
6152
+ count: projected.length,
6153
+ ...skippedReply(skipped)
6154
+ })
6155
+ };
6035
6156
  }
6036
6157
  if (request.type === "graph") {
6037
6158
  return this.graph(launch, request);
@@ -30951,6 +30951,16 @@ function stringifyDoc(frontmatter2, body) {
30951
30951
  return stringifyWithData(frontmatter2, body);
30952
30952
  }
30953
30953
 
30954
+ // ../core/src/meaningful-change-time.ts
30955
+ function meaningfulChangeTimeField(frontmatter2) {
30956
+ const generated = frontmatter2.generated;
30957
+ if (generated !== null && typeof generated === "object" && !Array.isArray(generated) && generated.at !== void 0) return "generated.at";
30958
+ return "timestamp";
30959
+ }
30960
+ function meaningfulChangeTimeValue(frontmatter2) {
30961
+ return meaningfulChangeTimeField(frontmatter2) === "generated.at" ? frontmatter2.generated.at : frontmatter2.timestamp;
30962
+ }
30963
+
30954
30964
  // ../core/src/errors.ts
30955
30965
  var InvalidInputError = class extends Error {
30956
30966
  constructor(message) {
@@ -30982,6 +30992,58 @@ var ConcurrentReplacementError = class extends Error {
30982
30992
  }
30983
30993
  };
30984
30994
 
30995
+ // ../core/src/verification.ts
30996
+ var TRUST_TIERS = Object.freeze([
30997
+ "unverified",
30998
+ "machine-confirmed",
30999
+ "human-reviewed"
31000
+ ]);
31001
+ var ISO_INSTANT = /^(\d{4})-(\d{2})-(\d{2})[Tt](\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{1,9}))?)?(?:([Zz])|([+-])(\d{2})(?::?(\d{2}))?)$/;
31002
+ function parseIsoInstant(raw, rounding = "truncate") {
31003
+ const match = ISO_INSTANT.exec(raw);
31004
+ if (!match) return null;
31005
+ const [, y, mo, d, h, mi, sec = "0", frac = "0", zulu, sign, oh, om = "0"] = match;
31006
+ const year = Number(y);
31007
+ const month = Number(mo);
31008
+ const day = Number(d);
31009
+ const hour = Number(h);
31010
+ const minute = Number(mi);
31011
+ const second = Number(sec);
31012
+ if (month < 1 || month > 12) return null;
31013
+ const probe = /* @__PURE__ */ new Date(0);
31014
+ probe.setUTCFullYear(year, month, 0);
31015
+ if (day < 1 || day > probe.getUTCDate()) return null;
31016
+ if (hour > 23 || minute > 59 || second > 59) return null;
31017
+ let offsetMinutes = 0;
31018
+ if (!zulu) {
31019
+ const offsetHours = Number(oh);
31020
+ const offsetMins = Number(om);
31021
+ if (offsetHours > 23 || offsetMins > 59) return null;
31022
+ offsetMinutes = (sign === "-" ? -1 : 1) * (offsetHours * 60 + offsetMins);
31023
+ }
31024
+ const millis = Number(frac.padEnd(3, "0").slice(0, 3));
31025
+ const instant = /* @__PURE__ */ new Date(0);
31026
+ instant.setUTCFullYear(year, month - 1, day);
31027
+ instant.setUTCHours(hour, minute, second, millis);
31028
+ const remainder = rounding === "ceil" && /[1-9]/.test(frac.slice(3)) ? 1 : 0;
31029
+ return instant.getTime() - offsetMinutes * 6e4 + remainder;
31030
+ }
31031
+
31032
+ // ../core/src/freshness.ts
31033
+ function parseTimestamp(ts, okfVersion) {
31034
+ if (okfVersion === "0.2") return typeof ts === "string" ? parseIsoInstant(ts) : null;
31035
+ if (ts instanceof Date) {
31036
+ const ms2 = ts.getTime();
31037
+ return Number.isNaN(ms2) ? null : ms2;
31038
+ }
31039
+ if (typeof ts === "number") {
31040
+ return Number.isFinite(ts) ? ts : null;
31041
+ }
31042
+ if (typeof ts !== "string" || ts.trim() === "") return null;
31043
+ const ms = Date.parse(ts);
31044
+ return Number.isNaN(ms) ? null : ms;
31045
+ }
31046
+
30985
31047
  // ../core/src/okf-lifecycle.ts
30986
31048
  var OKF_LIFECYCLE_STATUSES = Object.freeze(["draft", "stable", "deprecated"]);
30987
31049
  function isOkfLifecycleStatus(value) {
@@ -31262,7 +31324,7 @@ async function query(backend, filter = {}, options = {}) {
31262
31324
  }
31263
31325
  async function queryHeads(backend, filter = {}, options = {}) {
31264
31326
  if (backend.queryHeads) {
31265
- const rows = (await backend.queryHeads(filter)).filter((row) => matchesFilter(row, filter));
31327
+ const rows = (await backend.queryHeads(filter, options.onSkip ? { onSkip: options.onSkip } : void 0)).filter((row) => matchesFilter(row, filter));
31266
31328
  rows.sort((a, b) => a.id.localeCompare(b.id));
31267
31329
  return rows;
31268
31330
  }
@@ -31291,11 +31353,11 @@ function toSelectorList(value) {
31291
31353
  if (value === void 0) return void 0;
31292
31354
  return Array.isArray(value) ? value : [value];
31293
31355
  }
31294
- async function queryEdges(backend, filter = {}) {
31356
+ async function queryEdges(backend, filter = {}, options = {}) {
31295
31357
  const fromSelectors = toSelectorList(filter.from)?.map(normalizeEdgeSelector);
31296
31358
  const toSelectors = toSelectorList(filter.to)?.map(normalizeEdgeSelector);
31297
31359
  const edges = [];
31298
- for (const doc of await query(backend)) {
31360
+ for (const doc of await query(backend, {}, options)) {
31299
31361
  for (const link of parseLinksFromDoc(doc)) {
31300
31362
  if (!matchesEdgeSelector(link.from, fromSelectors)) continue;
31301
31363
  if (!matchesEdgeSelector(link.to, toSelectors)) continue;
@@ -32752,8 +32814,8 @@ async function query2(bundle, filter = {}, options = {}) {
32752
32814
  async function queryHeads2(bundle, filter = {}, options = {}) {
32753
32815
  return queryHeads(backendFor(bundle), filter, options);
32754
32816
  }
32755
- async function queryEdges2(bundle, filter = {}) {
32756
- return queryEdges(backendFor(bundle), filter);
32817
+ async function queryEdges2(bundle, filter = {}, options = {}) {
32818
+ return queryEdges(backendFor(bundle), filter, options);
32757
32819
  }
32758
32820
 
32759
32821
  // ../core/src/bundle.ts
@@ -42677,6 +42739,29 @@ function admitActiveView(bytes, contentType) {
42677
42739
  return { bytes: bytes.slice(), contentType: ACTIVE_VIEW_CONTENT_TYPE };
42678
42740
  }
42679
42741
 
42742
+ // ../core/src/query-order.ts
42743
+ function meaningfulChangeOrderKey(id, frontmatter2, okfVersion) {
42744
+ const value = meaningfulChangeTimeValue(frontmatter2);
42745
+ return {
42746
+ id,
42747
+ timestamp: typeof value === "string" ? value : "",
42748
+ timestampMs: parseTimestamp(value, okfVersion ?? void 0)
42749
+ };
42750
+ }
42751
+ function compareByMeaningfulChange(a, b) {
42752
+ if (a.timestampMs !== null && b.timestampMs !== null) {
42753
+ if (a.timestampMs !== b.timestampMs) return b.timestampMs - a.timestampMs;
42754
+ } else if (a.timestampMs !== null) {
42755
+ return -1;
42756
+ } else if (b.timestampMs !== null) {
42757
+ return 1;
42758
+ }
42759
+ return a.id < b.id ? -1 : a.id > b.id ? 1 : 0;
42760
+ }
42761
+ function sortByMeaningfulChange(rows, okfVersion) {
42762
+ return rows.map((row) => ({ row, key: meaningfulChangeOrderKey(row.id, row.frontmatter, okfVersion) })).sort((a, b) => compareByMeaningfulChange(a.key, b.key)).map(({ row }) => row);
42763
+ }
42764
+
42680
42765
  // ../view-runtime/src/bridge.ts
42681
42766
  var BRIDGE_PROTOCOL = "v0";
42682
42767
  var ACTION_BRIDGE_PROTOCOL = "v1";
@@ -42708,6 +42793,7 @@ var BRIDGE_HOST_CAPABILITIES = Object.freeze({
42708
42793
  queryFieldOr: "query.field-or",
42709
42794
  queryOpen: "query.open",
42710
42795
  queryCount: "query.count",
42796
+ queryNewest: "query.newest",
42711
42797
  edges: "edges",
42712
42798
  renderDocument: "render-document",
42713
42799
  openPage: "open-page",
@@ -42722,10 +42808,12 @@ var BRIDGE_SERVICE_CAPABILITIES = Object.freeze([
42722
42808
  BRIDGE_HOST_CAPABILITIES.queryFieldOr,
42723
42809
  BRIDGE_HOST_CAPABILITIES.queryOpen,
42724
42810
  BRIDGE_HOST_CAPABILITIES.queryCount,
42811
+ BRIDGE_HOST_CAPABILITIES.queryNewest,
42725
42812
  BRIDGE_HOST_CAPABILITIES.edges,
42726
42813
  BRIDGE_HOST_CAPABILITIES.graph,
42727
42814
  BRIDGE_HOST_CAPABILITIES.renderDocument
42728
42815
  ]);
42816
+ var BRIDGE_QUERY_ORDERS = Object.freeze(["id", "newest"]);
42729
42817
  var V0_REQUEST_TYPES = /* @__PURE__ */ new Set(["hello", "query", "read", "render-document", "edges", "graph", "subscribe", "host", "open-page"]);
42730
42818
  function isPlainRecord(value) {
42731
42819
  if (typeof value !== "object" || value === null || Array.isArray(value)) return false;
@@ -42754,7 +42842,7 @@ function invalidV0RequestId(value) {
42754
42842
  }
42755
42843
  function normalizeQueryParams(raw) {
42756
42844
  if (!isPlainRecord(raw)) return null;
42757
- const allowed = /* @__PURE__ */ new Set(["type", "prefix", "field", "open", "limit"]);
42845
+ const allowed = /* @__PURE__ */ new Set(["type", "prefix", "field", "open", "limit", "order"]);
42758
42846
  if (Object.keys(raw).some((key) => !allowed.has(key))) return null;
42759
42847
  const out = {};
42760
42848
  if (raw.type !== void 0) {
@@ -42782,6 +42870,10 @@ function normalizeQueryParams(raw) {
42782
42870
  }
42783
42871
  out.limit = raw.limit;
42784
42872
  }
42873
+ if (raw.order !== void 0) {
42874
+ if (!BRIDGE_QUERY_ORDERS.includes(raw.order)) return null;
42875
+ out.order = raw.order;
42876
+ }
42785
42877
  return out;
42786
42878
  }
42787
42879
  function selector(value) {
@@ -42909,9 +43001,19 @@ function fail(id, bridge, code2, message) {
42909
43001
  function replyWithinLimit(reply) {
42910
43002
  return Buffer.byteLength(JSON.stringify(reply), "utf8") <= MAX_REPLY_BYTES;
42911
43003
  }
43004
+ function skippedReply(skipped) {
43005
+ if (skipped.length === 0) return {};
43006
+ return { skipped: [...skipped].sort((a, b) => a.id.localeCompare(b.id)).slice(0, MAX_QUERY_ROWS) };
43007
+ }
42912
43008
  function boundedRows(rows, params, kinds) {
42913
43009
  const requested = params.limit === 0 || params.limit === void 0 ? MAX_QUERY_ROWS : Math.min(params.limit, MAX_QUERY_ROWS);
42914
- return applyQuerySelectionFilters(rows, { ...params, limit: requested }, kinds);
43010
+ const { order, ...selection } = params;
43011
+ if (order !== "newest") return applyQuerySelectionFilters(rows, { ...selection, limit: requested }, kinds);
43012
+ const matched = applyQuerySelectionFilters(rows, { ...selection, limit: void 0 }, kinds);
43013
+ return {
43014
+ rows: sortByMeaningfulChange(matched.rows, selection.okfVersion).slice(0, requested),
43015
+ count: matched.count
43016
+ };
42915
43017
  }
42916
43018
  var BridgeService = class {
42917
43019
  constructor(options) {
@@ -43039,7 +43141,8 @@ var BridgeService = class {
43039
43141
  return { status: "reload-required", message };
43040
43142
  }
43041
43143
  async subscriptionSnapshot() {
43042
- const rows = await queryHeads2(this.options.bundle, {});
43144
+ const rows = await queryHeads2(this.options.bundle, {}, { onSkip: () => {
43145
+ } });
43043
43146
  if (rows.length > MAX_SUBSCRIPTION_HEADS) {
43044
43147
  throw new Error("the bundle is too large for the experimental View polling snapshot");
43045
43148
  }
@@ -43056,7 +43159,11 @@ var BridgeService = class {
43056
43159
  * capability before adding them.
43057
43160
  */
43058
43161
  async graph(launch, request) {
43059
- const heads = await queryHeads2(this.options.bundle, {});
43162
+ const skipped = [];
43163
+ const onSkip = (skip) => {
43164
+ if (!skipped.some((row) => row.id === skip.id)) skipped.push({ id: skip.id, reason: skip.reason });
43165
+ };
43166
+ const heads = await queryHeads2(this.options.bundle, {}, { onSkip });
43060
43167
  if (heads.length > GRAPH_MAX_DOCUMENTS) {
43061
43168
  return {
43062
43169
  reply: fail(request.id, request.bridge, "TOO_LARGE", `the graph exceeded ${GRAPH_MAX_DOCUMENTS} documents`)
@@ -43065,7 +43172,7 @@ var BridgeService = class {
43065
43172
  const [registry, declaredOkfVersion, edges] = await Promise.all([
43066
43173
  loadKinds(this.options.bundle),
43067
43174
  readBundleOkfVersion2(this.options.bundle),
43068
- queryEdges2(this.options.bundle, {})
43175
+ queryEdges2(this.options.bundle, {}, { onSkip })
43069
43176
  ]);
43070
43177
  if (edges.length > GRAPH_MAX_RELATIONSHIPS) {
43071
43178
  return {
@@ -43099,7 +43206,8 @@ var BridgeService = class {
43099
43206
  okfVersion,
43100
43207
  documents,
43101
43208
  relationships,
43102
- counts: { documents: documents.length, relationships: relationships.length }
43209
+ counts: { documents: documents.length, relationships: relationships.length },
43210
+ ...skippedReply(skipped)
43103
43211
  })
43104
43212
  };
43105
43213
  }
@@ -43157,10 +43265,14 @@ var BridgeService = class {
43157
43265
  return { reply: ok3(request.id, request.bridge, request.type, { capability: request.capability, output: outcome.output }) };
43158
43266
  }
43159
43267
  if (request.type === "query") {
43268
+ if (request.params.order !== void 0 && !this.options.host.capabilities.includes(BRIDGE_HOST_CAPABILITIES.queryNewest)) {
43269
+ return { reply: fail(request.id, request.bridge, "USAGE", "invalid or unsupported bridge request") };
43270
+ }
43271
+ const skipped = [];
43160
43272
  const rows = await queryHeads2(this.options.bundle, {
43161
43273
  ...request.params.type ? { type: request.params.type } : {},
43162
43274
  ...request.params.prefix ? { prefix: request.params.prefix } : {}
43163
- });
43275
+ }, { onSkip: (skip) => skipped.push({ id: skip.id, reason: skip.reason }) });
43164
43276
  const [registry, okfVersion] = await Promise.all([
43165
43277
  loadKinds(this.options.bundle),
43166
43278
  readBundleOkfVersion2(this.options.bundle)
@@ -43174,7 +43286,7 @@ var BridgeService = class {
43174
43286
  const kind = registry.kinds.get(String(row.frontmatter.type ?? ""));
43175
43287
  return kind ? { ...row, frontmatter: projectLogicalKindFields(okfVersion, kind, row.frontmatter) } : row;
43176
43288
  });
43177
- return { reply: ok3(request.id, request.bridge, request.type, result) };
43289
+ return { reply: ok3(request.id, request.bridge, request.type, { ...result, ...skippedReply(skipped) }) };
43178
43290
  }
43179
43291
  if (request.type === "read" || request.type === "read-versioned") {
43180
43292
  const [result, registry, okfVersion] = await Promise.all([
@@ -43226,12 +43338,21 @@ var BridgeService = class {
43226
43338
  };
43227
43339
  }
43228
43340
  if (request.type === "edges") {
43229
- const edges = await queryEdges2(this.options.bundle, request.params);
43341
+ const skipped = [];
43342
+ const edges = await queryEdges2(this.options.bundle, request.params, {
43343
+ onSkip: (skip) => skipped.push({ id: skip.id, reason: skip.reason })
43344
+ });
43230
43345
  if (edges.length > MAX_EDGE_ROWS) {
43231
43346
  return { reply: fail(request.id, request.bridge, "TOO_LARGE", `the edge query exceeded ${MAX_EDGE_ROWS} rows`) };
43232
43347
  }
43233
43348
  const projected = edges.map(({ from, to, text: text4 }) => ({ from, to, text: text4 }));
43234
- return { reply: ok3(request.id, request.bridge, request.type, { edges: projected, count: projected.length }) };
43349
+ return {
43350
+ reply: ok3(request.id, request.bridge, request.type, {
43351
+ edges: projected,
43352
+ count: projected.length,
43353
+ ...skippedReply(skipped)
43354
+ })
43355
+ };
43235
43356
  }
43236
43357
  if (request.type === "graph") {
43237
43358
  return this.graph(launch, request);