Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions tests/xlog/test_sefaria_mcp_fetch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import importlib.util
from pathlib import Path


MODULE_PATH = Path(__file__).resolve().parents[2] / "xlog" / "tools" / "sefaria_mcp_fetch.py"
SPEC = importlib.util.spec_from_file_location("sefaria_mcp_fetch", MODULE_PATH)
sefaria_mcp_fetch = importlib.util.module_from_spec(SPEC)
assert SPEC.loader is not None
SPEC.loader.exec_module(sefaria_mcp_fetch)


def test_build_linked_source_selection_records_selected_and_skipped_links():
links = [
{
"category": "Commentary",
"ref": "Turei Zahav on Shulchan Arukh, Yoreh De'ah 87:1",
"type": "commentary",
},
{
"category": "Halakhah",
"ref": "Mishneh Torah, Forbidden Foods 9:1",
"type": "ein mishpat / ner mitsvah",
},
{
"category": "Commentary",
"ref": "Yad Avraham on Shulchan Arukh, Yoreh Deah 87:3",
"type": "commentary",
},
]

selection = sefaria_mcp_fetch.build_linked_source_selection(
slice_id="smicha86_u01_s87_seif_1",
links=links,
selected_refs={
"Turei Zahav on Shulchan Arukh, Yoreh De'ah 87:1": (
"qualifies the first-pass cooking/eating/benefit split"
),
"Mishneh Torah, Forbidden Foods 9:1": (
"upstream Rambam codification for Torah-level meat-milk prohibitions"
),
},
source_links_artifact=(
"xlog/evidence/smicha86/u01/yd_87/seif_1/sources/seif-links.json"
),
evidence_dir=Path("xlog/evidence/smicha86/u01/yd_87/seif_1"),
generated_at="2026-05-31T00:00:00+00:00",
)

rows = {row["ref"]: row for row in selection["links"]}

taz = rows["Turei Zahav on Shulchan Arukh, Yoreh De'ah 87:1"]
assert taz["selected"] is True
assert taz["family"] == "taz_turei_zahav"
assert taz["selection_reason"].startswith("qualifies")
assert taz["skip_reason"] == ""
assert taz["fetched_text_artifact"].endswith(
"sources/linked/turei_zahav_on_shulchan_arukh_yoreh_deah_87_1.json"
)

rambam = rows["Mishneh Torah, Forbidden Foods 9:1"]
assert rambam["family"] == "mishneh_torah_rambam"
assert rambam["selected"] is True

skipped = rows["Yad Avraham on Shulchan Arukh, Yoreh Deah 87:3"]
assert skipped["selected"] is False
assert skipped["fetched_text_artifact"] is None
assert skipped["skip_reason"]
assert selection["gate"]["status"] == "source_chain_expansion_required"
215 changes: 215 additions & 0 deletions xlog/GOAL_BUNDLE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
# GDSD Goal Bundle: Mistaber On XLOG

## Mission

Re-implement Mistaber from scratch as a corpus of `.xlog` programs that encode
Jewish legal sources with executable validation, ontology coherence, source
provenance, and non-blocking human semantic review.

This bundle adapts Goal-Driven Software Development (GDSD) and Goal-Question-
Metric (GQM) into an agent-operated encoding system. It is designed for long
running continuous work: each source slice becomes a small, validated XLOG
change with evidence and a human validation gate, while later slices continue in
parallel.

## Strategic Outcome

Mistaber becomes an XLOG-native legal reasoning system where:

- the corpus is encoded as audited `.xlog` programs;
- rules are executable and explainable;
- source references come from Sefaria MCP;
- commentary and upstream sources are fetched before human review and drive a
mandatory second-pass re-encoding for every seif;
- ontology reuse is enforced automatically when confidence is high;
- predicate drift is detected before it pollutes the core model;
- humans validate semantic correctness before merge;
- agents can keep encoding without waiting for each human response.

## Scope

In scope:

- XLOG program architecture for codex, ontology, scenarios, and provenance.
- Continuous agent workflow for source ingestion, encoding, validation, review,
and merge.
- Ontology grounding and predicate reuse controls.
- GQM metrics and evidence requirements.
- PR/discussion templates for human validation.

Out of scope for this bundle:

- Porting the old ASP implementation directly.
- Treating old HLL syntax as the target language.
- Claiming a law is accepted without human semantic validation.
- Maintaining ungrounded predicate aliases for convenience.

## Roles

### Encoding Agent

Selects the next source slice, retrieves sources through Sefaria MCP, proposes
semantic decomposition, grounds all atoms, writes `.xlog`, runs validation, and
opens the review artifact.

### Ontology Steward

Maintains the canonical predicate catalog, synonym map, drift policy, and merge
rules for new public concepts. This role may be an agent, but public ontology
changes require human visibility in the review artifact.

### Validation Agent

Runs static, runtime, explanation, scenario, and regression gates. It writes
evidence manifests and marks machine validation status.

### Human Reviewer

Validates that the encoded law faithfully represents the cited source material.
This role approves, requests changes, rejects, or marks a slice as needing
rabbinic/domain escalation.

### Merge Automation

Polls GitHub PRs/discussions without reminders, detects human verdicts, reruns
gates, verifies scope isolation, and merges only approved slices.

## Goal Tree

### G0: Continuous Corpus Encoding System

Build a sustainable loop that converts source slices into validated XLOG
programs without blocking later slices on pending human review.

Acceptance:

- every slice has a durable branch, PR or discussion, evidence directory, and
ledger entry;
- pending human validation does not stop future slice selection;
- merge is impossible before human approval and green validation.

### G1: Source Comprehension

Acquire and understand source material from authoritative corpus APIs.

Acceptance:

- all source text and metadata come through Sefaria MCP;
- source fetch logs include reference, version metadata when available, language,
retrieval timestamp, and MCP tool trace;
- semantic claims are linked to source spans or exact references.

### G2: Ontology Grounding

Prevent concept drift and predicate pollution while preserving coverage.

Acceptance:

- each public predicate is grounded to a canonical ontology entry;
- top-k atom and subgraph search is recorded before new predicate creation;
- high-confidence matches automatically reuse canonical predicates;
- uncertain cases are blocked from public ontology expansion until reviewed.

### G3: XLOG Encoding

Encode the source slice as executable XLOG modules.

Acceptance:

- encoded rules are idiomatic XLOG;
- private helper predicates stay local unless promoted through ontology review;
- provenance facts connect rules to source references;
- no old Mistaber DSL is introduced as a compatibility layer.

### G4: Mechanical Validation

Prove that the encoding runs, explains, and handles scenarios.

Acceptance:

- static syntax and style gates pass;
- `xlog run` outputs expected values for all scenarios;
- `xlog explain` JSON or equivalent provenance evidence is captured;
- regression checks prove no unrelated law slice changed.

### G5: Human Semantic Validation

Require human approval for every encoded law without blocking the continuous
encoding pipeline.

Acceptance:

- a PR or discussion is created with source links, citations, formulas, rules,
scenarios, evidence links, and an explicit verdict section;
- automation polls for verdicts on a schedule;
- approved slices are merged only after validation reruns;
- requested changes create a new encoding iteration and evidence refresh.

### G6: Integration And Closure

Keep the system coherent as the corpus grows.

Acceptance:

- ontology catalog, aliases, scenario index, and evidence manifests are updated;
- merged slices are traceable from source to rule to scenario to human verdict;
- metrics show drift, reuse, validation, and review health.

## Goal Lifecycle

```text
candidate
-> sourced
-> source_chain_expanded
-> comprehended
-> commentary_comprehended
-> grounded
-> encoded
-> machine_validated
-> awaiting_human
-> human_approved
-> merge_validated
-> merged
```

Allowed non-terminal states:

- `human_changes_requested`
- `ontology_review_required`
- `source_chain_expansion_required`
- `source_blocked`
- `machine_validation_failed`
- `domain_escalation_required`

## Non-Blocking Principle

Human approval blocks merge, not encoding.

The agent must continue with the next eligible candidate slice after creating a
complete review artifact for the current slice. It must not wait idly for a
human reply. The review monitor is responsible for checking existing PRs and
discussions on every cycle and on a scheduled cadence.

## Merge Gate

A branch may merge only when:

- source evidence exists;
- commentary and upstream source expansion is complete;
- second-pass re-encoding from the expanded source chain is complete;
- ontology grounding is complete;
- machine validation is green;
- human verdict is approved;
- branch scope matches exactly one law slice or one explicitly approved bundle;
- final validation rerun passes after the human-approved revision.

## Stop Conditions

Stop the current slice and continue elsewhere when:

- Sefaria MCP cannot retrieve required source text;
- ontology grounding has no confident candidate and no reviewable proposal;
- XLOG execution exposes a runtime blocker outside the slice scope;
- the source requires domain escalation before a faithful encoding can be made.

Do not stop the whole pipeline for pending human validation.
Loading
Loading