[NOTICKET] refactor(knowledge-extraction): BriefContext -> DomainContext
Browse filesThe workstream diagram has always called this box "Domain Knowledge", while
the model and its fields described a short orientation card. Rename the entity
to match the intent before anything is persisted: once an expert has approved
rows keyed on these ids, changing the `b_` prefix is an id migration that loses
the approvals - the exact failure the deterministic-id scheme exists to prevent.
BriefContext -> DomainContext
brief_id -> domain_id (prefix b_ -> d_)
ExtractionResult.brief -> .domain
brief_context.json -> domain_context.json
Deliberately unchanged:
- `Branch = "summary"` and GUARDED_FIELDS["summary"]. The branch literal is
the measurement key in CallUsage.branch / RejectedField.branch; renaming it
would break comparability with the committed eval baseline.
- prompts/summary.txt. Editing prompt text needs the eval path, and the
rewrite belongs with the v3 schema change, not with a rename.
No field, prompt or guarding change - the shape is still v2's. Also gitignores
out/knowledge/ (CLI output: data, not code).
Verified: ruff clean on src/knowledge_extraction + src/knowledge_parsing,
`import main` OK, suite 541 passed / 7 skipped. No test references the renamed
symbols. One residual check: branches.py passes the structured-output schema
NAME on the wire, so "BriefContext" -> "DomainContext" alters the LLM request;
almost certainly inert, not yet confirmed by an eval run.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
- .gitignore +1 -0
- docs/knowledge/KNOWLEDGE_OUTPUT_CONTRACT.md +6 -6
- src/knowledge_extraction/__init__.py +2 -2
- src/knowledge_extraction/cli.py +2 -2
- src/knowledge_extraction/extract/branches.py +6 -6
- src/knowledge_extraction/extract/schemas.py +1 -1
- src/knowledge_extraction/ids.py +10 -3
- src/knowledge_extraction/models.py +11 -3
- src/knowledge_extraction/service.py +2 -2
- src/knowledge_parsing/README.md +1 -1
|
@@ -63,3 +63,4 @@ docs/_archive/
|
|
| 63 |
data/knowledge_cache/
|
| 64 |
data/knowledge_runs/
|
| 65 |
data/knowledge_docs/
|
|
|
|
|
|
| 63 |
data/knowledge_cache/
|
| 64 |
data/knowledge_runs/
|
| 65 |
data/knowledge_docs/
|
| 66 |
+
out/knowledge/
|
|
@@ -24,7 +24,7 @@ nothing consumes.
|
|
| 24 |
|
| 25 |
| Change | Detail |
|
| 26 |
|---|---|
|
| 27 |
-
| **Ids on every entity** | `term_id`, `formula_id`, `
|
| 28 |
| **Glossary no longer restates formulas** | `formula_latex` removed β `defining_formula_id` reference |
|
| 29 |
| **Rules link to formulas and terms** | `formula_ids[]`, `term_ids[]` |
|
| 30 |
| **`rule_type` added** | `interpretation` \| `calculation` β see Β§2 for why |
|
|
@@ -52,7 +52,7 @@ None is model-generated; none is positional.
|
|
| 52 |
term_id = "t_" + sha256(doc_id + "|" + canonical)[:10]
|
| 53 |
formula_id = "f_" + sha256(doc_id + "|" + name_or_latex_or_chunk_id)[:10]
|
| 54 |
rule_id = "r_" + sha256(doc_id + "|" + chunk_id + "|" + char_start)[:10]
|
| 55 |
-
|
| 56 |
```
|
| 57 |
|
| 58 |
Implemented in `src/knowledge_extraction/ids.py`. `term_id` is keyed on the **cluster canonical**,
|
|
@@ -80,7 +80,7 @@ registry, which belongs with persistence (DEV_PLAN Β§0.8 D2).
|
|
| 80 |
| `glossary.json` | `GlossaryEntry[]` | Terms and their definitions |
|
| 81 |
| `interpretation_pack.json` | `RuleEntry[]` | Rules of thumb β how to interpret data or behaviour |
|
| 82 |
| `formulas.json` | `FormulaEntry[]` | Formulas, transcribed from the document |
|
| 83 |
-
| `
|
| 84 |
|
| 85 |
Plus three derived/audit payloads: `review_queue.json`, `rejected.json`, `usage.json`.
|
| 86 |
|
|
@@ -340,11 +340,11 @@ consequence can.
|
|
| 340 |
|
| 341 |
---
|
| 342 |
|
| 343 |
-
## 8 Β· `
|
| 344 |
|
| 345 |
```json
|
| 346 |
{
|
| 347 |
-
"
|
| 348 |
"title": "Standard Parameter Produksi & ECA",
|
| 349 |
"purpose": "Standard Parameter ini merupakan pedoman dalam perhitungan parameter produksi yang meliputi Qty, PA, UA, PTY, serta Equipment Capacity Analysis (ECA)...",
|
| 350 |
"key_parameters": ["Qty", "PA", "UA", "PTY", "ECA", "Grouping (Composite)"],
|
|
@@ -360,7 +360,7 @@ consequence can.
|
|
| 360 |
|
| 361 |
| Field | Type | Req |
|
| 362 |
|---|---|---|
|
| 363 |
-
| `
|
| 364 |
| `title` | string \| null | β |
|
| 365 |
| `purpose` | string \| null | β |
|
| 366 |
| `key_parameters` | string[] | β
Default `[]` |
|
|
|
|
| 24 |
|
| 25 |
| Change | Detail |
|
| 26 |
|---|---|
|
| 27 |
+
| **Ids on every entity** | `term_id`, `formula_id`, `domain_id`; `rule_id` re-specified. All deterministic and content-derived |
|
| 28 |
| **Glossary no longer restates formulas** | `formula_latex` removed β `defining_formula_id` reference |
|
| 29 |
| **Rules link to formulas and terms** | `formula_ids[]`, `term_ids[]` |
|
| 30 |
| **`rule_type` added** | `interpretation` \| `calculation` β see Β§2 for why |
|
|
|
|
| 52 |
term_id = "t_" + sha256(doc_id + "|" + canonical)[:10]
|
| 53 |
formula_id = "f_" + sha256(doc_id + "|" + name_or_latex_or_chunk_id)[:10]
|
| 54 |
rule_id = "r_" + sha256(doc_id + "|" + chunk_id + "|" + char_start)[:10]
|
| 55 |
+
domain_id = "d_" + sha256(doc_id)[:10]
|
| 56 |
```
|
| 57 |
|
| 58 |
Implemented in `src/knowledge_extraction/ids.py`. `term_id` is keyed on the **cluster canonical**,
|
|
|
|
| 80 |
| `glossary.json` | `GlossaryEntry[]` | Terms and their definitions |
|
| 81 |
| `interpretation_pack.json` | `RuleEntry[]` | Rules of thumb β how to interpret data or behaviour |
|
| 82 |
| `formulas.json` | `FormulaEntry[]` | Formulas, transcribed from the document |
|
| 83 |
+
| `domain_context.json` | `DomainContext` | One domain-context entry per document (renamed from `brief_context.json` / `BriefContext`, 2026-09-01) |
|
| 84 |
|
| 85 |
Plus three derived/audit payloads: `review_queue.json`, `rejected.json`, `usage.json`.
|
| 86 |
|
|
|
|
| 340 |
|
| 341 |
---
|
| 342 |
|
| 343 |
+
## 8 Β· `domain_context.json` β `DomainContext` β 5 fields *(single object)*
|
| 344 |
|
| 345 |
```json
|
| 346 |
{
|
| 347 |
+
"domain_id": "d_2ef60a8c19",
|
| 348 |
"title": "Standard Parameter Produksi & ECA",
|
| 349 |
"purpose": "Standard Parameter ini merupakan pedoman dalam perhitungan parameter produksi yang meliputi Qty, PA, UA, PTY, serta Equipment Capacity Analysis (ECA)...",
|
| 350 |
"key_parameters": ["Qty", "PA", "UA", "PTY", "ECA", "Grouping (Composite)"],
|
|
|
|
| 360 |
|
| 361 |
| Field | Type | Req |
|
| 362 |
|---|---|---|
|
| 363 |
+
| `domain_id` | string | β
**New in v2** |
|
| 364 |
| `title` | string \| null | β |
|
| 365 |
| `purpose` | string \| null | β |
|
| 366 |
| `key_parameters` | string[] | β
Default `[]` |
|
|
@@ -22,10 +22,10 @@ Calibrated constants and why: KNOWLEDGE_PIPELINE_CALIBRATION.md
|
|
| 22 |
from .adapter import parsed_doc_from_artifact
|
| 23 |
from .models import (
|
| 24 |
AbbrevPair,
|
| 25 |
-
BriefContext,
|
| 26 |
CallUsage,
|
| 27 |
Chunk,
|
| 28 |
ClusterResult,
|
|
|
|
| 29 |
FilterResult,
|
| 30 |
FormulaEntry,
|
| 31 |
GlossaryEntry,
|
|
@@ -40,7 +40,7 @@ from .service import ExtractionResult, build_clusters, estimate_cost, extract_al
|
|
| 40 |
|
| 41 |
__all__ = [
|
| 42 |
"AbbrevPair",
|
| 43 |
-
"
|
| 44 |
"CallUsage",
|
| 45 |
"Chunk",
|
| 46 |
"ClusterResult",
|
|
|
|
| 22 |
from .adapter import parsed_doc_from_artifact
|
| 23 |
from .models import (
|
| 24 |
AbbrevPair,
|
|
|
|
| 25 |
CallUsage,
|
| 26 |
Chunk,
|
| 27 |
ClusterResult,
|
| 28 |
+
DomainContext,
|
| 29 |
FilterResult,
|
| 30 |
FormulaEntry,
|
| 31 |
GlossaryEntry,
|
|
|
|
| 40 |
|
| 41 |
__all__ = [
|
| 42 |
"AbbrevPair",
|
| 43 |
+
"DomainContext",
|
| 44 |
"CallUsage",
|
| 45 |
"Chunk",
|
| 46 |
"ClusterResult",
|
|
@@ -221,8 +221,8 @@ def main(argv: list[str] | None = None) -> int:
|
|
| 221 |
_dump(args.out_dir / "formulas.json", result.formulas)
|
| 222 |
_dump(args.out_dir / "review_queue.json", result.review_queue)
|
| 223 |
_dump(args.out_dir / "rejected.json", [r.model_dump(mode="json") for r in result.rejected])
|
| 224 |
-
if result.
|
| 225 |
-
_dump(args.out_dir / "
|
| 226 |
_dump(args.out_dir / "usage.json", [u.model_dump(mode="json") for u in result.usages])
|
| 227 |
print(f"[write ] {args.out_dir}")
|
| 228 |
return 0
|
|
|
|
| 221 |
_dump(args.out_dir / "formulas.json", result.formulas)
|
| 222 |
_dump(args.out_dir / "review_queue.json", result.review_queue)
|
| 223 |
_dump(args.out_dir / "rejected.json", [r.model_dump(mode="json") for r in result.rejected])
|
| 224 |
+
if result.domain:
|
| 225 |
+
_dump(args.out_dir / "domain_context.json", result.domain)
|
| 226 |
_dump(args.out_dir / "usage.json", [u.model_dump(mode="json") for u in result.usages])
|
| 227 |
print(f"[write ] {args.out_dir}")
|
| 228 |
return 0
|
|
@@ -13,11 +13,11 @@ response must not abort a corpus-scale run that has already paid for parsing.
|
|
| 13 |
from __future__ import annotations
|
| 14 |
|
| 15 |
from ...middlewares.logging import get_logger
|
| 16 |
-
from ..ids import
|
| 17 |
from ..models import (
|
| 18 |
-
BriefContext,
|
| 19 |
CallUsage,
|
| 20 |
Chunk,
|
|
|
|
| 21 |
FormulaEntry,
|
| 22 |
FormulaVariable,
|
| 23 |
GlossaryEntry,
|
|
@@ -207,7 +207,7 @@ def extract_formula(
|
|
| 207 |
|
| 208 |
def extract_summary(
|
| 209 |
chunks: list[Chunk], extractor, doc_id: str
|
| 210 |
-
) -> tuple[
|
| 211 |
"""Whole-document summary β the quiet cost risk. Few calls, but a large
|
| 212 |
share of all input tokens, because summarisation cannot be filtered: it
|
| 213 |
needs the whole document.
|
|
@@ -218,7 +218,7 @@ def extract_summary(
|
|
| 218 |
"""
|
| 219 |
user = evidence_block(chunks)
|
| 220 |
result = extractor.complete(
|
| 221 |
-
"summary", load_prompt("summary"), user, schema_for("summary"), "
|
| 222 |
)
|
| 223 |
try:
|
| 224 |
draft = SummaryDraft.model_validate(result.data)
|
|
@@ -227,8 +227,8 @@ def extract_summary(
|
|
| 227 |
return None, result.usage
|
| 228 |
|
| 229 |
return (
|
| 230 |
-
|
| 231 |
-
|
| 232 |
title=draft.title,
|
| 233 |
purpose=draft.purpose,
|
| 234 |
key_parameters=draft.key_parameters,
|
|
|
|
| 13 |
from __future__ import annotations
|
| 14 |
|
| 15 |
from ...middlewares.logging import get_logger
|
| 16 |
+
from ..ids import domain_id, formula_id, rule_id, term_id
|
| 17 |
from ..models import (
|
|
|
|
| 18 |
CallUsage,
|
| 19 |
Chunk,
|
| 20 |
+
DomainContext,
|
| 21 |
FormulaEntry,
|
| 22 |
FormulaVariable,
|
| 23 |
GlossaryEntry,
|
|
|
|
| 207 |
|
| 208 |
def extract_summary(
|
| 209 |
chunks: list[Chunk], extractor, doc_id: str
|
| 210 |
+
) -> tuple[DomainContext | None, CallUsage]:
|
| 211 |
"""Whole-document summary β the quiet cost risk. Few calls, but a large
|
| 212 |
share of all input tokens, because summarisation cannot be filtered: it
|
| 213 |
needs the whole document.
|
|
|
|
| 218 |
"""
|
| 219 |
user = evidence_block(chunks)
|
| 220 |
result = extractor.complete(
|
| 221 |
+
"summary", load_prompt("summary"), user, schema_for("summary"), "DomainContext"
|
| 222 |
)
|
| 223 |
try:
|
| 224 |
draft = SummaryDraft.model_validate(result.data)
|
|
|
|
| 227 |
return None, result.usage
|
| 228 |
|
| 229 |
return (
|
| 230 |
+
DomainContext(
|
| 231 |
+
domain_id=domain_id(doc_id),
|
| 232 |
title=draft.title,
|
| 233 |
purpose=draft.purpose,
|
| 234 |
key_parameters=draft.key_parameters,
|
|
@@ -7,7 +7,7 @@ invitation to fabricate**, so the request schema is narrower than the stored one
|
|
| 7 |
|
| 8 |
v2 narrowed it further, and every removal has a reason of that kind:
|
| 9 |
|
| 10 |
-
- **ids and links** (`term_id`, `formula_id`, `rule_id`, `
|
| 11 |
`defining_formula_id`, `formula_ids`, `term_ids`, `variables[].term_id`) are
|
| 12 |
minted deterministically. A model cannot produce a stable identifier β it has
|
| 13 |
no memory of what it emitted last run β and `rule_id` being model-written was
|
|
|
|
| 7 |
|
| 8 |
v2 narrowed it further, and every removal has a reason of that kind:
|
| 9 |
|
| 10 |
+
- **ids and links** (`term_id`, `formula_id`, `rule_id`, `domain_id`,
|
| 11 |
`defining_formula_id`, `formula_ids`, `term_ids`, `variables[].term_id`) are
|
| 12 |
minted deterministically. A model cannot produce a stable identifier β it has
|
| 13 |
no memory of what it emitted last run β and `rule_id` being model-written was
|
|
@@ -71,6 +71,13 @@ def rule_id(doc_id: str, chunk_id: str, char_start: int) -> str:
|
|
| 71 |
return f"r_{_digest(doc_id, chunk_id, str(char_start))}"
|
| 72 |
|
| 73 |
|
| 74 |
-
def
|
| 75 |
-
"""One
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
return f"r_{_digest(doc_id, chunk_id, str(char_start))}"
|
| 72 |
|
| 73 |
|
| 74 |
+
def domain_id(doc_id: str) -> str:
|
| 75 |
+
"""One domain-context entry per document, so the document identifies it.
|
| 76 |
+
|
| 77 |
+
Prefix moved `b_` β `d_` with the `BriefContext` β `DomainContext` rename
|
| 78 |
+
(2026-09-01). Free to do then because nothing was persisted yet; once an
|
| 79 |
+
expert has approved rows keyed on these, changing a prefix is an id
|
| 80 |
+
migration that loses the approvals β the same failure the whole scheme
|
| 81 |
+
exists to prevent.
|
| 82 |
+
"""
|
| 83 |
+
return f"d_{_digest(doc_id)}"
|
|
@@ -308,17 +308,25 @@ class FormulaEntry(BaseModel):
|
|
| 308 |
latex_verification: LatexVerification | None = None
|
| 309 |
|
| 310 |
|
| 311 |
-
class
|
| 312 |
-
"""Whole-document
|
| 313 |
span-checked β a plausible summary is indistinguishable from a correct one,
|
| 314 |
which is why it belongs on the larger model tier when one is available.
|
| 315 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 316 |
`summary_md` and `scope` were dropped in v2 for that same reason: the
|
| 317 |
longest unverifiable prose on the least verifiable branch bought the least,
|
| 318 |
and `scope` overlapped `purpose`. What remains is what a reviewer needs to
|
| 319 |
orient before working the queue."""
|
| 320 |
|
| 321 |
-
|
| 322 |
title: str | None = None
|
| 323 |
purpose: str | None = None
|
| 324 |
key_parameters: list[str] = Field(default_factory=list)
|
|
|
|
| 308 |
latex_verification: LatexVerification | None = None
|
| 309 |
|
| 310 |
|
| 311 |
+
class DomainContext(BaseModel):
|
| 312 |
+
"""Whole-document domain context. The only branch that cannot be
|
| 313 |
span-checked β a plausible summary is indistinguishable from a correct one,
|
| 314 |
which is why it belongs on the larger model tier when one is available.
|
| 315 |
|
| 316 |
+
Renamed from `BriefContext` 2026-09-01. The name was the smaller half of the
|
| 317 |
+
problem: the workstream diagram has always called this box *Domain
|
| 318 |
+
Knowledge*, while the fields describe a short orientation card. The shape
|
| 319 |
+
below is still v2's β the v3 proposal that closes the gap (deriving the
|
| 320 |
+
document outline and subdomain coverage, and turning `purpose` from a
|
| 321 |
+
generated summary into a span-guarded verbatim quote) is
|
| 322 |
+
`docs/knowledge/KNOWLEDGE_DOMAIN_CONTEXT_V3.md`, and is NOT implemented.
|
| 323 |
+
|
| 324 |
`summary_md` and `scope` were dropped in v2 for that same reason: the
|
| 325 |
longest unverifiable prose on the least verifiable branch bought the least,
|
| 326 |
and `scope` overlapped `purpose`. What remains is what a reviewer needs to
|
| 327 |
orient before working the queue."""
|
| 328 |
|
| 329 |
+
domain_id: str
|
| 330 |
title: str | None = None
|
| 331 |
purpose: str | None = None
|
| 332 |
key_parameters: list[str] = Field(default_factory=list)
|
|
@@ -53,7 +53,7 @@ class ExtractionResult:
|
|
| 53 |
self.glossary: list[dict] = []
|
| 54 |
self.rules: list[dict] = []
|
| 55 |
self.formulas: list[dict] = []
|
| 56 |
-
self.
|
| 57 |
self.review_queue: list[dict] = []
|
| 58 |
self.rejected: list[RejectedField] = []
|
| 59 |
self.usages: list[CallUsage] = []
|
|
@@ -248,4 +248,4 @@ def _run_summary(doc, extractor, out) -> None:
|
|
| 248 |
return
|
| 249 |
# Summary prose is NOT span-checked β it cannot be. Only its provenance is
|
| 250 |
# carried, and the branch belongs on a larger tier for exactly that reason.
|
| 251 |
-
out.
|
|
|
|
| 53 |
self.glossary: list[dict] = []
|
| 54 |
self.rules: list[dict] = []
|
| 55 |
self.formulas: list[dict] = []
|
| 56 |
+
self.domain: dict | None = None
|
| 57 |
self.review_queue: list[dict] = []
|
| 58 |
self.rejected: list[RejectedField] = []
|
| 59 |
self.usages: list[CallUsage] = []
|
|
|
|
| 248 |
return
|
| 249 |
# Summary prose is NOT span-checked β it cannot be. Only its provenance is
|
| 250 |
# carried, and the branch belongs on a larger tier for exactly that reason.
|
| 251 |
+
out.domain = entry.model_dump(mode="json")
|
|
@@ -147,7 +147,7 @@ All kept off the nearly-full `C:` drive:
|
|
| 147 |
|
| 148 |
**The file name is currently a primary key, not a label.** `doc_id` is the file
|
| 149 |
name stem; `chunk_id` is built from `doc_id`; and `term_id`, `formula_id`,
|
| 150 |
-
`rule_id` and `
|
| 151 |
Renaming a file after ingestion therefore changes every identifier derived from
|
| 152 |
it, and an expert's approve/reject decisions are keyed to those identifiers.
|
| 153 |
|
|
|
|
| 147 |
|
| 148 |
**The file name is currently a primary key, not a label.** `doc_id` is the file
|
| 149 |
name stem; `chunk_id` is built from `doc_id`; and `term_id`, `formula_id`,
|
| 150 |
+
`rule_id` and `domain_id` are all digests of `doc_id` (`knowledge_extraction/ids.py`).
|
| 151 |
Renaming a file after ingestion therefore changes every identifier derived from
|
| 152 |
it, and an expert's approve/reject decisions are keyed to those identifiers.
|
| 153 |
|