kbforge-okfquery 0.2.2__tar.gz → 0.4.0__tar.gz

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.
Files changed (45) hide show
  1. kbforge_okfquery-0.2.2/README.md → kbforge_okfquery-0.4.0/PKG-INFO +153 -61
  2. kbforge_okfquery-0.2.2/PKG-INFO → kbforge_okfquery-0.4.0/README.md +124 -74
  3. {kbforge_okfquery-0.2.2 → kbforge_okfquery-0.4.0}/pyproject.toml +22 -2
  4. {kbforge_okfquery-0.2.2 → kbforge_okfquery-0.4.0}/src/okfquery/__init__.py +1 -1
  5. {kbforge_okfquery-0.2.2 → kbforge_okfquery-0.4.0}/src/okfquery/cli.py +85 -26
  6. {kbforge_okfquery-0.2.2 → kbforge_okfquery-0.4.0}/src/okfquery/index.py +79 -36
  7. kbforge_okfquery-0.4.0/src/okfquery/links.py +77 -0
  8. kbforge_okfquery-0.4.0/src/okfquery/load.py +231 -0
  9. kbforge_okfquery-0.4.0/src/okfquery/parse.py +345 -0
  10. kbforge_okfquery-0.4.0/src/okfquery/related.py +89 -0
  11. {kbforge_okfquery-0.2.2 → kbforge_okfquery-0.4.0}/src/okfquery/schema.py +14 -2
  12. {kbforge_okfquery-0.2.2 → kbforge_okfquery-0.4.0}/tests/fixtures/messy/concepts/incomplete/overview.md +3 -0
  13. kbforge_okfquery-0.4.0/tests/fixtures/v03/.drafts/hidden.md +4 -0
  14. kbforge_okfquery-0.4.0/tests/fixtures/v03/README.md +3 -0
  15. kbforge_okfquery-0.4.0/tests/fixtures/v03/attesters/check.py +2 -0
  16. kbforge_okfquery-0.4.0/tests/fixtures/v03/index.md +10 -0
  17. kbforge_okfquery-0.4.0/tests/fixtures/v03/log.md +4 -0
  18. kbforge_okfquery-0.4.0/tests/fixtures/v03/metrics/gross-margin-legacy.md +8 -0
  19. kbforge_okfquery-0.4.0/tests/fixtures/v03/metrics/gross-margin.md +28 -0
  20. kbforge_okfquery-0.4.0/tests/fixtures/v03/metrics/index.md +3 -0
  21. kbforge_okfquery-0.4.0/tests/fixtures/v03/notes/scratch.md +5 -0
  22. kbforge_okfquery-0.4.0/tests/fixtures/v03/policies/standard.md +3 -0
  23. kbforge_okfquery-0.4.0/tests/fixtures/v03/tables/orders.md +8 -0
  24. {kbforge_okfquery-0.2.2 → kbforge_okfquery-0.4.0}/tests/test_index.py +88 -11
  25. {kbforge_okfquery-0.2.2 → kbforge_okfquery-0.4.0}/tests/test_load.py +59 -48
  26. {kbforge_okfquery-0.2.2 → kbforge_okfquery-0.4.0}/tests/test_okfquery_cli.py +21 -5
  27. kbforge_okfquery-0.4.0/tests/test_okfquery_links.py +88 -0
  28. kbforge_okfquery-0.4.0/tests/test_okfquery_v03.py +204 -0
  29. kbforge_okfquery-0.4.0/tests/test_parse.py +231 -0
  30. kbforge_okfquery-0.4.0/tests/test_related.py +166 -0
  31. {kbforge_okfquery-0.2.2 → kbforge_okfquery-0.4.0}/tests/test_roundtrip.py +72 -3
  32. kbforge_okfquery-0.2.2/src/okfquery/load.py +0 -134
  33. kbforge_okfquery-0.2.2/src/okfquery/parse.py +0 -252
  34. kbforge_okfquery-0.2.2/tests/test_parse.py +0 -158
  35. {kbforge_okfquery-0.2.2 → kbforge_okfquery-0.4.0}/.gitignore +0 -0
  36. {kbforge_okfquery-0.2.2 → kbforge_okfquery-0.4.0}/tests/fixtures/clean/concepts/api/overview.md +0 -0
  37. {kbforge_okfquery-0.2.2 → kbforge_okfquery-0.4.0}/tests/fixtures/messy/concepts/badyaml/overview.md +0 -0
  38. {kbforge_okfquery-0.2.2 → kbforge_okfquery-0.4.0}/tests/fixtures/messy/concepts/claims/index.md +0 -0
  39. {kbforge_okfquery-0.2.2 → kbforge_okfquery-0.4.0}/tests/fixtures/messy/concepts/index.md +0 -0
  40. {kbforge_okfquery-0.2.2 → kbforge_okfquery-0.4.0}/tests/fixtures/messy/concepts/log.md +0 -0
  41. {kbforge_okfquery-0.2.2 → kbforge_okfquery-0.4.0}/tests/fixtures/messy/concepts/nofence/overview.md +0 -0
  42. {kbforge_okfquery-0.2.2 → kbforge_okfquery-0.4.0}/tests/fixtures/messy/concepts/notmapping/overview.md +0 -0
  43. {kbforge_okfquery-0.2.2 → kbforge_okfquery-0.4.0}/tests/fixtures/messy/concepts/ok/overview.md +0 -0
  44. {kbforge_okfquery-0.2.2 → kbforge_okfquery-0.4.0}/tests/fixtures/messy/concepts/unterminated/overview.md +0 -0
  45. {kbforge_okfquery-0.2.2 → kbforge_okfquery-0.4.0}/tests/test_okfquery_live.py +0 -0
@@ -1,11 +1,40 @@
1
+ Metadata-Version: 2.5
2
+ Name: kbforge-okfquery
3
+ Version: 0.4.0
4
+ Summary: SQL over any OKF bundle, via DuckDB
5
+ Project-URL: Homepage, https://github.com/flyersworder/kbforge/tree/main/packages/okfquery
6
+ Project-URL: Repository, https://github.com/flyersworder/kbforge
7
+ Project-URL: Issues, https://github.com/flyersworder/kbforge/issues
8
+ Project-URL: Changelog, https://github.com/flyersworder/kbforge/blob/main/CHANGELOG.md
9
+ Author-email: Qing <qingye779@gmail.com>
10
+ License: MIT
11
+ Keywords: duckdb,knowledge-base,okf,open-knowledge-format,sql
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Environment :: Console
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: License :: OSI Approved :: MIT License
16
+ Classifier: Operating System :: OS Independent
17
+ Classifier: Programming Language :: Python :: 3
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Programming Language :: Python :: 3.13
20
+ Classifier: Programming Language :: Python :: 3.14
21
+ Classifier: Topic :: Database
22
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
23
+ Requires-Python: >=3.12
24
+ Requires-Dist: duckdb>=1.1
25
+ Requires-Dist: markdown-it-py>=3
26
+ Requires-Dist: pytz>=2024.1
27
+ Requires-Dist: pyyaml>=6
28
+ Description-Content-Type: text/markdown
29
+
1
30
  # okfquery
2
31
 
3
- SQL over an [OKF v0.2](https://github.com/GoogleCloudPlatform/knowledge-catalog/blob/main/okf/SPEC.md)
32
+ SQL over an [OKF v0.3](https://github.com/open-knowledge-format/open-knowledge-format/blob/main/SPEC.md)
4
33
  bundle, via DuckDB. `okfquery` loads a published bundle into an in-memory DuckDB
5
34
  connection and hands the connection back — no daemon, no cache, and no
6
35
  committed artifact except the navigation `index.md` you opt into with
7
- `okfquery index`. It depends on no kbforge code at runtime, so it reads any OKF v0.2
8
- bundle, kbforge-built or not.
36
+ `okfquery index`. It depends on no kbforge code at runtime, so it reads any OKF
37
+ bundle, kbforge-built or not: it is tested against OKF's own example bundles.
9
38
 
10
39
  ## Install and use
11
40
 
@@ -17,7 +46,29 @@ okfquery shell --bundle path/to/bundle
17
46
 
18
47
  `query` runs one SQL statement and prints the result (`--format table|json|csv`).
19
48
  `shell` writes a temporary `.duckdb` file, opens the `duckdb` CLI on it, and
20
- deletes the file when the shell exits.
49
+ deletes the file when the shell exits. `check` lists every problem and exits 1
50
+ on OKF non-conformance it can see (§11.1): frontmatter that is not a YAML
51
+ mapping, and frontmatter in a reserved file where §8/§9 forbid it (`log.md`, an
52
+ `index.md` below the root). It does not parse `log.md` date headings or
53
+ `index.md` sections. Warnings print but pass; a path holding no concept at all
54
+ exits 2.
55
+
56
+ **What it reads as a concept** is OKF §3.2's rule, anywhere under `--bundle`: a
57
+ `.md` file whose frontmatter has a non-empty `type`. A `README.md` without
58
+ frontmatter is silently not one; a fenced file without `type` is not one either,
59
+ with a warning. `index.md` and `log.md` never are (§3.1), and names starting with
60
+ `.` are not part of the bundle. Only `type` is required (§4.1): kbforge's own
61
+ laws ask more of what kbforge emits, but a reader holding every bundle to them
62
+ would reject everyone else's.
63
+
64
+ **Links** are OKF §6.1 body links (`[text](target)` and reference-style, parsed as
65
+ CommonMark, so a link in a code block is not one), kbforge's `links` key, and
66
+ the path fields (§6.2) that resolve to a concept: `resource`, `sources[].resource`,
67
+ `superseded_by`, `computation`, `executor.resource`, `attester.resource`. Both
68
+ in-bundle forms resolve (§6): `/x.md` from the bundle root, anything else from
69
+ the linking file's directory. A bundle that declares `okf_version` 0.2 or
70
+ earlier, or none, also gets §12.1's fallback: a bare path field that does not
71
+ resolve from its file is tried from the root.
21
72
 
22
73
  ## `index`: a front door for agents
23
74
 
@@ -37,8 +88,12 @@ listing `concepts/*/overview.md` and opening files to learn what they are.
37
88
  hand-written `index.md` is refused unless you pass `--force`.
38
89
  - **Deterministic.** No timestamps, total ordering: rerunning on an unchanged
39
90
  bundle rewrites nothing, so CI commits only when a listing changed.
40
- - **No frontmatter**, not even the optional root `okf_version`: a reserved name
41
- that opens a fence is a concept, and the index would count itself.
91
+ - **The bundle's frontmatter is kept.** A root `index.md` may carry bundle
92
+ metadata (`okf_version`, `title`, `description`, `license`; OKF v0.3 §8.1),
93
+ and regenerating keeps that block byte for byte, `--force` included. okfquery
94
+ writes none of its own: it did not write the bundle, so it does not know its
95
+ version. The index never counts itself either way: `index.md` is never a
96
+ concept.
42
97
 
43
98
  **Run it after merge, not per kbforge run.** Each kbforge system publishes on its
44
99
  own sync branch; a whole-bundle file on several branches would collide. Generate
@@ -49,8 +104,9 @@ Both jobs share three properties, each learned from running them on a real
49
104
  forge:
50
105
 
51
106
  - **They wait for the first concept.** A bundle repo is created before anything
52
- merges, and `okfquery index` rejects a bundle with no `concepts/` on purpose
53
- (that is how a wrong `--bundle` shows). The jobs skip until one exists.
107
+ merges, and `okfquery index` rejects a bundle with no concepts on purpose
108
+ (that is how a wrong `--bundle` shows). The jobs skip until kbforge's
109
+ `concepts/` exists.
54
110
  - **They index the branch as it is now.** A cold start merges chunks in quick
55
111
  succession; a job that indexed its own commit and pushed would be rejected
56
112
  when a later merge had already moved the branch. They fetch the tip, index
@@ -100,7 +156,7 @@ index:
100
156
  rules:
101
157
  # Default branch only, and only once a concept has merged: before the first
102
158
  # merge there is nothing to index, and `okfquery index` rejects a bundle
103
- # with no concepts/ directory on purpose (it is how a wrong --bundle shows).
159
+ # with no concepts on purpose (it is how a wrong --bundle shows).
104
160
  - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
105
161
  exists: ['concepts/**/*.md']
106
162
  resource_group: okfquery-index # one index job at a time
@@ -135,6 +191,31 @@ every request that adds or retitles a concept would fail it, and fixing that on
135
191
  the sync branches makes several systems edit one whole-bundle file, the
136
192
  collision this job exists to avoid.
137
193
 
194
+ ## `related`: what one concept connects to
195
+
196
+ ```bash
197
+ okfquery related concepts/gaps/redundant-supply/overview.md --bundle .
198
+ okfquery related concepts/reports/q3/overview.md --by tags,scenario --limit 5
199
+ ```
200
+
201
+ Three sections, each line in `index`'s `* [Title](path) - description` format:
202
+
203
+ - **Links to**: what its links and path fields resolve to inside the bundle. A
204
+ target that is not a concept (a broken link) is listed by its path; a URL is
205
+ left out.
206
+ - **Linked from**: concepts whose links or path fields resolve to it, so a
207
+ deprecated concept's `superseded_by` shows on its replacement. No single file
208
+ shows these.
209
+ - **Shares**: concepts sharing at least one value of the `--by` facets (default
210
+ `tags`), most shared values first, then by path, each naming what it shares
211
+ (`(shares tags: 800v, sic)`). Concepts already listed above are left out, and
212
+ `--limit` (default 10) caps this section only.
213
+
214
+ An empty section prints `(none)`, so "nothing" reads differently from "not
215
+ checked". These relations are derived when asked, not stored: OKF §3.1 leaves
216
+ tag views to the consumer, and a derived view cannot go stale. The argument is a
217
+ path from the bundle root; a leading `/` or `./` is accepted.
218
+
138
219
  ## Schema
139
220
 
140
221
  ```
@@ -170,15 +251,27 @@ CREATE TABLE sources (
170
251
  content_hash VARCHAR
171
252
  );
172
253
 
254
+ -- One row per body link (OKF §6.1), per entry of kbforge's `links` key, and per
255
+ -- path field (§6.2) that resolves to a concept. `target` is the value as the
256
+ -- file wrote it; `resolved` is the bundle path it names (§6: `/` from the root,
257
+ -- otherwise from the file's directory), NULL when it points outside the bundle.
258
+ -- A graph edge (§6.4) is a row whose `resolved` is in concepts.path.
173
259
  CREATE TABLE links (
174
260
  path VARCHAR NOT NULL,
175
- target VARCHAR NOT NULL
261
+ target VARCHAR NOT NULL,
262
+ resolved VARCHAR,
263
+ -- 'body', 'links', or the path field: 'resource', 'sources',
264
+ -- 'superseded_by', 'computation', 'executor', 'attester'.
265
+ via VARCHAR NOT NULL
176
266
  );
177
267
 
268
+ -- `error`: the bundle is not conformant (§11.1). `warning`: a consumer keeps
269
+ -- going (§11.2), ignoring an entry or keeping a value it cannot use.
178
270
  CREATE TABLE problems (
179
271
  path VARCHAR NOT NULL,
180
272
  kind VARCHAR NOT NULL,
181
- detail VARCHAR NOT NULL
273
+ detail VARCHAR NOT NULL,
274
+ severity VARCHAR NOT NULL
182
275
  );
183
276
  ```
184
277
 
@@ -187,17 +280,21 @@ CREATE TABLE problems (
187
280
  convention `synthesize.assemble` writes) — on a foreign bundle it is merely
188
281
  source order, with no error to tell you that. `facets` is one JSON column, open
189
282
  by construction, so `facets->>'owner'` reaches whatever a connector attached.
190
- `problems` is additive: a file that failed to parse still gets a `concepts` row
191
- with NULLs for what could not be read, plus one `problems` row per distinct
192
- failure — nothing a broken concept says gets silently dropped from an audit.
283
+ `problems` is what keeps the audit honest: a file whose frontmatter cannot be
284
+ read is not a concept (§3.2), so it has no `concepts` row, but it is never
285
+ silently dropped. It gets an `error` row, and every file that is a concept
286
+ carries a `warning` row for each value a consumer had to ignore. Every fenced
287
+ `.md` file is a concept or has a problem. A graph edge (§6.4) is a `links` row
288
+ whose `resolved` is a concept path.
193
289
 
194
290
  ## Queries, run against the messy test fixture
195
291
 
196
292
  All four queries below were run as shown against
197
- `packages/okfquery/tests/fixtures/messy` — 9 `.md` files under `concepts/`, 7 of
198
- them concepts (`concepts/index.md` and `concepts/log.md` are reserved and
199
- fenceless, so they are skipped; `concepts/claims/index.md` is reserved-*named*
200
- but carries frontmatter, so it counts).
293
+ `packages/okfquery/tests/fixtures/messy` — 9 `.md` files under `concepts/`, 2 of
294
+ them concepts. `concepts/index.md` and `concepts/log.md` are reserved;
295
+ `concepts/nofence/overview.md` has no frontmatter; four more have frontmatter
296
+ that cannot be read, or are a reserved file carrying some, and show up in
297
+ `problems` as errors.
201
298
 
202
299
  **What is built on a given upstream document:**
203
300
 
@@ -231,50 +328,46 @@ GROUP BY 1 ORDER BY oldest;
231
328
  │ system │ concepts │ oldest │
232
329
  │ varchar │ int64 │ timestamp with time zone │
233
330
  ├─────────┼──────────┼──────────────────────────┤
234
- │ wiki │ 3 │ 2026-08-23 00:00:00+00 │
331
+ │ wiki │ 1 │ 2026-08-23 00:00:00+00 │
235
332
  └─────────┴──────────┴──────────────────────────┘
236
333
  ```
237
334
 
238
- Only one system shows up on this fixture: most of the messy bundle's files
239
- exist to exercise a `problems` kind and never got far enough to have a
240
- `sources` entry at all, so they don't join into this query — which is correct,
241
- not a bug in the query.
335
+ Only one concept shows up on this fixture: most of the messy bundle's files
336
+ exist to exercise a `problems` kind and are not concepts, and the one other
337
+ concept's ordinal-0 source has no `resource`, so it was ignored (§11.2) — which
338
+ is correct, not a bug in the query.
242
339
 
243
- **Orphans — nothing links to them:**
340
+ **Orphans — nothing else links to them** (`resolved` is the §6 reading of every
341
+ target, so `/x.md`, `./x.md` and `../x.md` all compare equal):
244
342
 
245
343
  ```bash
246
344
  $ okfquery query "
247
345
  SELECT c.path FROM concepts c
248
- WHERE NOT EXISTS (SELECT 1 FROM links l WHERE l.target = c.path);
346
+ WHERE NOT EXISTS (SELECT 1 FROM links l WHERE l.resolved = c.path AND l.path <> c.path);
249
347
  " --bundle packages/okfquery/tests/fixtures/messy
250
- ┌───────────────────────────────────┐
251
- │ path │
252
- │ varchar │
253
- ├───────────────────────────────────┤
254
- │ concepts/badyaml/overview.md │
255
- │ concepts/claims/index.md │
256
- │ concepts/incomplete/overview.md │
257
- │ concepts/nofence/overview.md │
258
- │ concepts/notmapping/overview.md │
259
- │ concepts/unterminated/overview.md │
260
- └───────────────────────────────────┘
348
+ ┌─────────────────────────────────┐
349
+ │ path │
350
+ │ varchar │
351
+ ├─────────────────────────────────┤
352
+ │ concepts/incomplete/overview.md │
353
+ │ concepts/ok/overview.md │
354
+ └─────────────────────────────────┘
261
355
  ```
262
356
 
263
- **Everything that failed to parse** (`--format csv` here, since the default
264
- table renderer truncates long `detail` strings):
357
+ **Every problem, errors first** (`--format csv` here, since the default table
358
+ renderer truncates long `detail` strings):
265
359
 
266
360
  ```bash
267
- $ okfquery query "SELECT path, kind, detail FROM problems ORDER BY path, kind;" \
361
+ $ okfquery query "SELECT path, severity, kind, detail FROM problems ORDER BY severity, path, kind;" \
268
362
  --bundle packages/okfquery/tests/fixtures/messy --format csv
269
- path,kind,detail
270
- concepts/badyaml/overview.md,invalid-yaml,frontmatter is not valid YAML: ParserError
271
- concepts/incomplete/overview.md,bad-links,'links' entry 3 is not a string
272
- concepts/incomplete/overview.md,bad-sources,'sources' entry 0 has no 'resource' (§5.1 requires one)
273
- concepts/incomplete/overview.md,bad-timestamp,'generated.at' 'not-a-date' is not an ISO-8601 datetime
274
- concepts/incomplete/overview.md,missing-required,"missing required OKF keys: title, description"
275
- concepts/nofence/overview.md,no-frontmatter,file does not open a '---' frontmatter fence
276
- concepts/notmapping/overview.md,frontmatter-not-mapping,"frontmatter parses to list, not a mapping"
277
- concepts/unterminated/overview.md,unterminated-frontmatter,frontmatter fence is opened but never closed by a closing '---'
363
+ path,severity,kind,detail
364
+ concepts/badyaml/overview.md,error,invalid-yaml,frontmatter is not valid YAML: ParserError
365
+ concepts/claims/index.md,error,reserved-frontmatter,§8: an index.md below the root has no frontmatter
366
+ concepts/notmapping/overview.md,error,frontmatter-not-mapping,"frontmatter parses to list, not a mapping"
367
+ concepts/unterminated/overview.md,error,unterminated-frontmatter,frontmatter fence is opened but never closed by a closing '---'
368
+ concepts/incomplete/overview.md,warning,bad-links,'links' entry 3 is not a string
369
+ concepts/incomplete/overview.md,warning,bad-sources,'sources' entry 0 has no 'resource' (§5.1 requires one); ignored
370
+ concepts/incomplete/overview.md,warning,bad-timestamp,'generated.at' 'not-a-date' is not an RFC 3339 timestamp
278
371
  ```
279
372
 
280
373
  ## The mirror, and why both obvious joins are wrong
@@ -306,10 +399,10 @@ every source with no mirror row at all, which is exactly the "never
306
399
  published" case the query needs to report. A match means current; a mismatch
307
400
  means an update is sitting in review; a source with no mirror row was never
308
401
  published. Run for real against the messy fixture (`concepts/ok/overview.md`'s
309
- two sources — an owning `wiki:ok` and a grounding `notes:ok` — plus two other
310
- concepts whose sources have no mirror row at all) with a synthetic
311
- two-document mirror covering only `wiki:ok` and `notes:ok`, one hash left
312
- matching and one changed to simulate an update stuck in review:
402
+ two sources — an owning `wiki:ok` and a grounding `notes:ok` — plus one other
403
+ concept's source with no mirror row at all) with a synthetic two-document
404
+ mirror covering only `wiki:ok` and `notes:ok`, one hash left matching and one
405
+ changed to simulate an update stuck in review:
313
406
 
314
407
  ```bash
315
408
  $ okfquery query "
@@ -323,15 +416,14 @@ SELECT s.path,
323
416
  FROM sources s LEFT JOIN mirror m ON s.id = m.doc_id
324
417
  ORDER BY s.path, s.id;
325
418
  " --bundle packages/okfquery/tests/fixtures/messy --mirror /path/to/mirror
326
- ┌─────────────────────────────────┬─────────────────┬─────────────┬─────────────┬──────────────────┐
327
- │ path │ id │ bundle_hash │ mirror_hash │ status │
328
- │ varchar │ varchar │ varchar │ varchar │ varchar │
329
- ├─────────────────────────────────┼─────────────────┼─────────────┼─────────────┼──────────────────┤
330
- │ concepts/claims/index.md │ wiki:claims │ h-claims │ NULL │ never published │
331
- │ concepts/incomplete/overview.md │ wiki:incomplete │ NULL │ NULL │ never published │
332
- │ concepts/ok/overview.md │ notes:ok │ h-ground │ h-ground-v2 │ update in review │
333
- │ concepts/ok/overview.md │ wiki:ok │ h-own │ h-own │ current │
334
- └─────────────────────────────────┴─────────────────┴─────────────┴─────────────┴──────────────────┘
419
+ ┌─────────────────────────────────┬─────────────────┬──────────────┬─────────────┬──────────────────┐
420
+ │ path │ id │ bundle_hash │ mirror_hash │ status │
421
+ │ varchar │ varchar │ varchar │ varchar │ varchar │
422
+ ├─────────────────────────────────┼─────────────────┼──────────────┼─────────────┼──────────────────┤
423
+ │ concepts/incomplete/overview.md │ wiki:incomplete │ h-incomplete │ NULL │ never published │
424
+ │ concepts/ok/overview.md │ notes:ok │ h-ground │ h-ground-v2 │ update in review │
425
+ │ concepts/ok/overview.md │ wiki:ok │ h-own │ h-own │ current │
426
+ └─────────────────────────────────┴─────────────────┴──────────────┴─────────────┴──────────────────┘
335
427
  ```
336
428
 
337
429
  That does not fix the `id` gap above: a connector whose anchor disagrees with
@@ -1,24 +1,11 @@
1
- Metadata-Version: 2.5
2
- Name: kbforge-okfquery
3
- Version: 0.2.2
4
- Summary: SQL over an OKF v0.2 bundle, via DuckDB
5
- Author-email: Qing <qingye779@gmail.com>
6
- License: MIT
7
- Keywords: duckdb,knowledge-base,okf,open-knowledge-format,sql
8
- Requires-Python: >=3.12
9
- Requires-Dist: duckdb>=1.1
10
- Requires-Dist: pytz>=2024.1
11
- Requires-Dist: pyyaml>=6
12
- Description-Content-Type: text/markdown
13
-
14
1
  # okfquery
15
2
 
16
- SQL over an [OKF v0.2](https://github.com/GoogleCloudPlatform/knowledge-catalog/blob/main/okf/SPEC.md)
3
+ SQL over an [OKF v0.3](https://github.com/open-knowledge-format/open-knowledge-format/blob/main/SPEC.md)
17
4
  bundle, via DuckDB. `okfquery` loads a published bundle into an in-memory DuckDB
18
5
  connection and hands the connection back — no daemon, no cache, and no
19
6
  committed artifact except the navigation `index.md` you opt into with
20
- `okfquery index`. It depends on no kbforge code at runtime, so it reads any OKF v0.2
21
- bundle, kbforge-built or not.
7
+ `okfquery index`. It depends on no kbforge code at runtime, so it reads any OKF
8
+ bundle, kbforge-built or not: it is tested against OKF's own example bundles.
22
9
 
23
10
  ## Install and use
24
11
 
@@ -30,7 +17,29 @@ okfquery shell --bundle path/to/bundle
30
17
 
31
18
  `query` runs one SQL statement and prints the result (`--format table|json|csv`).
32
19
  `shell` writes a temporary `.duckdb` file, opens the `duckdb` CLI on it, and
33
- deletes the file when the shell exits.
20
+ deletes the file when the shell exits. `check` lists every problem and exits 1
21
+ on OKF non-conformance it can see (§11.1): frontmatter that is not a YAML
22
+ mapping, and frontmatter in a reserved file where §8/§9 forbid it (`log.md`, an
23
+ `index.md` below the root). It does not parse `log.md` date headings or
24
+ `index.md` sections. Warnings print but pass; a path holding no concept at all
25
+ exits 2.
26
+
27
+ **What it reads as a concept** is OKF §3.2's rule, anywhere under `--bundle`: a
28
+ `.md` file whose frontmatter has a non-empty `type`. A `README.md` without
29
+ frontmatter is silently not one; a fenced file without `type` is not one either,
30
+ with a warning. `index.md` and `log.md` never are (§3.1), and names starting with
31
+ `.` are not part of the bundle. Only `type` is required (§4.1): kbforge's own
32
+ laws ask more of what kbforge emits, but a reader holding every bundle to them
33
+ would reject everyone else's.
34
+
35
+ **Links** are OKF §6.1 body links (`[text](target)` and reference-style, parsed as
36
+ CommonMark, so a link in a code block is not one), kbforge's `links` key, and
37
+ the path fields (§6.2) that resolve to a concept: `resource`, `sources[].resource`,
38
+ `superseded_by`, `computation`, `executor.resource`, `attester.resource`. Both
39
+ in-bundle forms resolve (§6): `/x.md` from the bundle root, anything else from
40
+ the linking file's directory. A bundle that declares `okf_version` 0.2 or
41
+ earlier, or none, also gets §12.1's fallback: a bare path field that does not
42
+ resolve from its file is tried from the root.
34
43
 
35
44
  ## `index`: a front door for agents
36
45
 
@@ -50,8 +59,12 @@ listing `concepts/*/overview.md` and opening files to learn what they are.
50
59
  hand-written `index.md` is refused unless you pass `--force`.
51
60
  - **Deterministic.** No timestamps, total ordering: rerunning on an unchanged
52
61
  bundle rewrites nothing, so CI commits only when a listing changed.
53
- - **No frontmatter**, not even the optional root `okf_version`: a reserved name
54
- that opens a fence is a concept, and the index would count itself.
62
+ - **The bundle's frontmatter is kept.** A root `index.md` may carry bundle
63
+ metadata (`okf_version`, `title`, `description`, `license`; OKF v0.3 §8.1),
64
+ and regenerating keeps that block byte for byte, `--force` included. okfquery
65
+ writes none of its own: it did not write the bundle, so it does not know its
66
+ version. The index never counts itself either way: `index.md` is never a
67
+ concept.
55
68
 
56
69
  **Run it after merge, not per kbforge run.** Each kbforge system publishes on its
57
70
  own sync branch; a whole-bundle file on several branches would collide. Generate
@@ -62,8 +75,9 @@ Both jobs share three properties, each learned from running them on a real
62
75
  forge:
63
76
 
64
77
  - **They wait for the first concept.** A bundle repo is created before anything
65
- merges, and `okfquery index` rejects a bundle with no `concepts/` on purpose
66
- (that is how a wrong `--bundle` shows). The jobs skip until one exists.
78
+ merges, and `okfquery index` rejects a bundle with no concepts on purpose
79
+ (that is how a wrong `--bundle` shows). The jobs skip until kbforge's
80
+ `concepts/` exists.
67
81
  - **They index the branch as it is now.** A cold start merges chunks in quick
68
82
  succession; a job that indexed its own commit and pushed would be rejected
69
83
  when a later merge had already moved the branch. They fetch the tip, index
@@ -113,7 +127,7 @@ index:
113
127
  rules:
114
128
  # Default branch only, and only once a concept has merged: before the first
115
129
  # merge there is nothing to index, and `okfquery index` rejects a bundle
116
- # with no concepts/ directory on purpose (it is how a wrong --bundle shows).
130
+ # with no concepts on purpose (it is how a wrong --bundle shows).
117
131
  - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
118
132
  exists: ['concepts/**/*.md']
119
133
  resource_group: okfquery-index # one index job at a time
@@ -148,6 +162,31 @@ every request that adds or retitles a concept would fail it, and fixing that on
148
162
  the sync branches makes several systems edit one whole-bundle file, the
149
163
  collision this job exists to avoid.
150
164
 
165
+ ## `related`: what one concept connects to
166
+
167
+ ```bash
168
+ okfquery related concepts/gaps/redundant-supply/overview.md --bundle .
169
+ okfquery related concepts/reports/q3/overview.md --by tags,scenario --limit 5
170
+ ```
171
+
172
+ Three sections, each line in `index`'s `* [Title](path) - description` format:
173
+
174
+ - **Links to**: what its links and path fields resolve to inside the bundle. A
175
+ target that is not a concept (a broken link) is listed by its path; a URL is
176
+ left out.
177
+ - **Linked from**: concepts whose links or path fields resolve to it, so a
178
+ deprecated concept's `superseded_by` shows on its replacement. No single file
179
+ shows these.
180
+ - **Shares**: concepts sharing at least one value of the `--by` facets (default
181
+ `tags`), most shared values first, then by path, each naming what it shares
182
+ (`(shares tags: 800v, sic)`). Concepts already listed above are left out, and
183
+ `--limit` (default 10) caps this section only.
184
+
185
+ An empty section prints `(none)`, so "nothing" reads differently from "not
186
+ checked". These relations are derived when asked, not stored: OKF §3.1 leaves
187
+ tag views to the consumer, and a derived view cannot go stale. The argument is a
188
+ path from the bundle root; a leading `/` or `./` is accepted.
189
+
151
190
  ## Schema
152
191
 
153
192
  ```
@@ -183,15 +222,27 @@ CREATE TABLE sources (
183
222
  content_hash VARCHAR
184
223
  );
185
224
 
225
+ -- One row per body link (OKF §6.1), per entry of kbforge's `links` key, and per
226
+ -- path field (§6.2) that resolves to a concept. `target` is the value as the
227
+ -- file wrote it; `resolved` is the bundle path it names (§6: `/` from the root,
228
+ -- otherwise from the file's directory), NULL when it points outside the bundle.
229
+ -- A graph edge (§6.4) is a row whose `resolved` is in concepts.path.
186
230
  CREATE TABLE links (
187
231
  path VARCHAR NOT NULL,
188
- target VARCHAR NOT NULL
232
+ target VARCHAR NOT NULL,
233
+ resolved VARCHAR,
234
+ -- 'body', 'links', or the path field: 'resource', 'sources',
235
+ -- 'superseded_by', 'computation', 'executor', 'attester'.
236
+ via VARCHAR NOT NULL
189
237
  );
190
238
 
239
+ -- `error`: the bundle is not conformant (§11.1). `warning`: a consumer keeps
240
+ -- going (§11.2), ignoring an entry or keeping a value it cannot use.
191
241
  CREATE TABLE problems (
192
242
  path VARCHAR NOT NULL,
193
243
  kind VARCHAR NOT NULL,
194
- detail VARCHAR NOT NULL
244
+ detail VARCHAR NOT NULL,
245
+ severity VARCHAR NOT NULL
195
246
  );
196
247
  ```
197
248
 
@@ -200,17 +251,21 @@ CREATE TABLE problems (
200
251
  convention `synthesize.assemble` writes) — on a foreign bundle it is merely
201
252
  source order, with no error to tell you that. `facets` is one JSON column, open
202
253
  by construction, so `facets->>'owner'` reaches whatever a connector attached.
203
- `problems` is additive: a file that failed to parse still gets a `concepts` row
204
- with NULLs for what could not be read, plus one `problems` row per distinct
205
- failure — nothing a broken concept says gets silently dropped from an audit.
254
+ `problems` is what keeps the audit honest: a file whose frontmatter cannot be
255
+ read is not a concept (§3.2), so it has no `concepts` row, but it is never
256
+ silently dropped. It gets an `error` row, and every file that is a concept
257
+ carries a `warning` row for each value a consumer had to ignore. Every fenced
258
+ `.md` file is a concept or has a problem. A graph edge (§6.4) is a `links` row
259
+ whose `resolved` is a concept path.
206
260
 
207
261
  ## Queries, run against the messy test fixture
208
262
 
209
263
  All four queries below were run as shown against
210
- `packages/okfquery/tests/fixtures/messy` — 9 `.md` files under `concepts/`, 7 of
211
- them concepts (`concepts/index.md` and `concepts/log.md` are reserved and
212
- fenceless, so they are skipped; `concepts/claims/index.md` is reserved-*named*
213
- but carries frontmatter, so it counts).
264
+ `packages/okfquery/tests/fixtures/messy` — 9 `.md` files under `concepts/`, 2 of
265
+ them concepts. `concepts/index.md` and `concepts/log.md` are reserved;
266
+ `concepts/nofence/overview.md` has no frontmatter; four more have frontmatter
267
+ that cannot be read, or are a reserved file carrying some, and show up in
268
+ `problems` as errors.
214
269
 
215
270
  **What is built on a given upstream document:**
216
271
 
@@ -244,50 +299,46 @@ GROUP BY 1 ORDER BY oldest;
244
299
  │ system │ concepts │ oldest │
245
300
  │ varchar │ int64 │ timestamp with time zone │
246
301
  ├─────────┼──────────┼──────────────────────────┤
247
- │ wiki │ 3 │ 2026-08-23 00:00:00+00 │
302
+ │ wiki │ 1 │ 2026-08-23 00:00:00+00 │
248
303
  └─────────┴──────────┴──────────────────────────┘
249
304
  ```
250
305
 
251
- Only one system shows up on this fixture: most of the messy bundle's files
252
- exist to exercise a `problems` kind and never got far enough to have a
253
- `sources` entry at all, so they don't join into this query — which is correct,
254
- not a bug in the query.
306
+ Only one concept shows up on this fixture: most of the messy bundle's files
307
+ exist to exercise a `problems` kind and are not concepts, and the one other
308
+ concept's ordinal-0 source has no `resource`, so it was ignored (§11.2) — which
309
+ is correct, not a bug in the query.
255
310
 
256
- **Orphans — nothing links to them:**
311
+ **Orphans — nothing else links to them** (`resolved` is the §6 reading of every
312
+ target, so `/x.md`, `./x.md` and `../x.md` all compare equal):
257
313
 
258
314
  ```bash
259
315
  $ okfquery query "
260
316
  SELECT c.path FROM concepts c
261
- WHERE NOT EXISTS (SELECT 1 FROM links l WHERE l.target = c.path);
317
+ WHERE NOT EXISTS (SELECT 1 FROM links l WHERE l.resolved = c.path AND l.path <> c.path);
262
318
  " --bundle packages/okfquery/tests/fixtures/messy
263
- ┌───────────────────────────────────┐
264
- │ path │
265
- │ varchar │
266
- ├───────────────────────────────────┤
267
- │ concepts/badyaml/overview.md │
268
- │ concepts/claims/index.md │
269
- │ concepts/incomplete/overview.md │
270
- │ concepts/nofence/overview.md │
271
- │ concepts/notmapping/overview.md │
272
- │ concepts/unterminated/overview.md │
273
- └───────────────────────────────────┘
319
+ ┌─────────────────────────────────┐
320
+ │ path │
321
+ │ varchar │
322
+ ├─────────────────────────────────┤
323
+ │ concepts/incomplete/overview.md │
324
+ │ concepts/ok/overview.md │
325
+ └─────────────────────────────────┘
274
326
  ```
275
327
 
276
- **Everything that failed to parse** (`--format csv` here, since the default
277
- table renderer truncates long `detail` strings):
328
+ **Every problem, errors first** (`--format csv` here, since the default table
329
+ renderer truncates long `detail` strings):
278
330
 
279
331
  ```bash
280
- $ okfquery query "SELECT path, kind, detail FROM problems ORDER BY path, kind;" \
332
+ $ okfquery query "SELECT path, severity, kind, detail FROM problems ORDER BY severity, path, kind;" \
281
333
  --bundle packages/okfquery/tests/fixtures/messy --format csv
282
- path,kind,detail
283
- concepts/badyaml/overview.md,invalid-yaml,frontmatter is not valid YAML: ParserError
284
- concepts/incomplete/overview.md,bad-links,'links' entry 3 is not a string
285
- concepts/incomplete/overview.md,bad-sources,'sources' entry 0 has no 'resource' (§5.1 requires one)
286
- concepts/incomplete/overview.md,bad-timestamp,'generated.at' 'not-a-date' is not an ISO-8601 datetime
287
- concepts/incomplete/overview.md,missing-required,"missing required OKF keys: title, description"
288
- concepts/nofence/overview.md,no-frontmatter,file does not open a '---' frontmatter fence
289
- concepts/notmapping/overview.md,frontmatter-not-mapping,"frontmatter parses to list, not a mapping"
290
- concepts/unterminated/overview.md,unterminated-frontmatter,frontmatter fence is opened but never closed by a closing '---'
334
+ path,severity,kind,detail
335
+ concepts/badyaml/overview.md,error,invalid-yaml,frontmatter is not valid YAML: ParserError
336
+ concepts/claims/index.md,error,reserved-frontmatter,§8: an index.md below the root has no frontmatter
337
+ concepts/notmapping/overview.md,error,frontmatter-not-mapping,"frontmatter parses to list, not a mapping"
338
+ concepts/unterminated/overview.md,error,unterminated-frontmatter,frontmatter fence is opened but never closed by a closing '---'
339
+ concepts/incomplete/overview.md,warning,bad-links,'links' entry 3 is not a string
340
+ concepts/incomplete/overview.md,warning,bad-sources,'sources' entry 0 has no 'resource' (§5.1 requires one); ignored
341
+ concepts/incomplete/overview.md,warning,bad-timestamp,'generated.at' 'not-a-date' is not an RFC 3339 timestamp
291
342
  ```
292
343
 
293
344
  ## The mirror, and why both obvious joins are wrong
@@ -319,10 +370,10 @@ every source with no mirror row at all, which is exactly the "never
319
370
  published" case the query needs to report. A match means current; a mismatch
320
371
  means an update is sitting in review; a source with no mirror row was never
321
372
  published. Run for real against the messy fixture (`concepts/ok/overview.md`'s
322
- two sources — an owning `wiki:ok` and a grounding `notes:ok` — plus two other
323
- concepts whose sources have no mirror row at all) with a synthetic
324
- two-document mirror covering only `wiki:ok` and `notes:ok`, one hash left
325
- matching and one changed to simulate an update stuck in review:
373
+ two sources — an owning `wiki:ok` and a grounding `notes:ok` — plus one other
374
+ concept's source with no mirror row at all) with a synthetic two-document
375
+ mirror covering only `wiki:ok` and `notes:ok`, one hash left matching and one
376
+ changed to simulate an update stuck in review:
326
377
 
327
378
  ```bash
328
379
  $ okfquery query "
@@ -336,15 +387,14 @@ SELECT s.path,
336
387
  FROM sources s LEFT JOIN mirror m ON s.id = m.doc_id
337
388
  ORDER BY s.path, s.id;
338
389
  " --bundle packages/okfquery/tests/fixtures/messy --mirror /path/to/mirror
339
- ┌─────────────────────────────────┬─────────────────┬─────────────┬─────────────┬──────────────────┐
340
- │ path │ id │ bundle_hash │ mirror_hash │ status │
341
- │ varchar │ varchar │ varchar │ varchar │ varchar │
342
- ├─────────────────────────────────┼─────────────────┼─────────────┼─────────────┼──────────────────┤
343
- │ concepts/claims/index.md │ wiki:claims │ h-claims │ NULL │ never published │
344
- │ concepts/incomplete/overview.md │ wiki:incomplete │ NULL │ NULL │ never published │
345
- │ concepts/ok/overview.md │ notes:ok │ h-ground │ h-ground-v2 │ update in review │
346
- │ concepts/ok/overview.md │ wiki:ok │ h-own │ h-own │ current │
347
- └─────────────────────────────────┴─────────────────┴─────────────┴─────────────┴──────────────────┘
390
+ ┌─────────────────────────────────┬─────────────────┬──────────────┬─────────────┬──────────────────┐
391
+ │ path │ id │ bundle_hash │ mirror_hash │ status │
392
+ │ varchar │ varchar │ varchar │ varchar │ varchar │
393
+ ├─────────────────────────────────┼─────────────────┼──────────────┼─────────────┼──────────────────┤
394
+ │ concepts/incomplete/overview.md │ wiki:incomplete │ h-incomplete │ NULL │ never published │
395
+ │ concepts/ok/overview.md │ notes:ok │ h-ground │ h-ground-v2 │ update in review │
396
+ │ concepts/ok/overview.md │ wiki:ok │ h-own │ h-own │ current │
397
+ └─────────────────────────────────┴─────────────────┴──────────────┴─────────────┴──────────────────┘
348
398
  ```
349
399
 
350
400
  That does not fix the `id` gap above: a connector whose anchor disagrees with