Skip to content

refactor: extract pure derive() function (#261) - #273

Merged
robotrocketscience merged 8 commits into
mainfrom
feat/issue-261-derivation-fn
Apr 29, 2026
Merged

refactor: extract pure derive() function (#261)#273
robotrocketscience merged 8 commits into
mainfrom
feat/issue-261-derivation-fn

Conversation

@robotrocketscience

Copy link
Copy Markdown
Owner

Summary

  • Adds src/aelfrice/derivation.py exposing DerivationInput, DerivationOutput, and derive() — a pure function with no MemoryStore dependency that maps raw text + source metadata to a Belief (or skip).
  • Routes all six ingest entry points (ingest.py, scanner.py, classification.py, triple_extractor.py, mcp_server.py, cli.py) through derive() instead of constructing Belief inline. Store I/O (record_ingest, insert_belief) is unchanged at each call site.
  • Removes duplicate hash helpers from call sites (_lock_id_for, _content_hash, _belief_id_for_phrase) now that the logic lives in derivation.py.
  • Adds tests/test_derivation.py with 26 unit tests covering every source_kind in INGEST_SOURCE_KINDS, the override_belief_type path, determinism, and id-scheme cross-checks.

No behavior change. Full pytest suite: 1790 passed, 8 skipped.

Closes #261

Touched files

  • src/aelfrice/derivation.py (new)
  • src/aelfrice/ingest.py
  • src/aelfrice/scanner.py
  • src/aelfrice/classification.py
  • src/aelfrice/triple_extractor.py
  • src/aelfrice/mcp_server.py
  • src/aelfrice/cli.py
  • tests/test_derivation.py (new)

Review

Verify that derive() has zero MemoryStore imports; check that id-scheme assertions in test_derivation.py match the sha256 formulas previously in each call site; confirm no behavior change by running the full suite.

@robotrocketscience robotrocketscience added the author-Setr PR coordination mutex label Apr 29, 2026
@robotrocketscience
robotrocketscience force-pushed the feat/issue-261-derivation-fn branch from 91225ac to db07251 Compare April 29, 2026 00:27
@robotrocketscience

Copy link
Copy Markdown
Owner Author

Rebased onto current main (was branched from a stale base, would have reverted #253's classify-orphans pass on merge). Conflict in cli.py imports resolved as union (both doctor's classify_orphans helpers and the new derivation imports). Full suite green: 1804 passed, 8 skipped. Review: ready.

@robotrocketscience robotrocketscience added the review-Kulili PR coordination mutex label Apr 29, 2026
…ve()

Pure function that maps (raw_text, source_kind, source_path, …) to a
Belief + edges with no MemoryStore dependency. Covers all four
source-kind families: lock/remember (mcp_remember, cli_remember),
triple-extraction (git), and classifier paths (filesystem, python_ast,
etc.). Id schemes are byte-for-byte identical to the inline Belief
constructions they replace so replay equality is id-stable.

Closes half of #261 acceptance criterion 1-2.
Replace inline classify_sentence + Belief construction in
_ingest_turn_ids with a derive() call. Store I/O (record_ingest,
insert_belief) unchanged; only the belief derivation is delegated.
Remove now-unused hashlib import.
Replace _route_from_regex + inline Belief construction with a
derive() call for the default regex path. The LLM-router path retains
inline Belief construction since the router already encapsulates
classification; it still removes the _route_from_regex helper. Remove
unused classify_sentence and ORIGIN_AGENT_INFERRED imports.
…ve()

Replace get_source_adjusted_prior + inline Belief construction in
accept_classifications with a derive() call using override_belief_type.
Add override_belief_type field to DerivationInput for host-LLM-classified
paths; add get_source_adjusted_prior to derivation.py imports. Use lazy
import inside accept_classifications to break the derive->classify_sentence
import cycle.
Replace _resolve_or_create_belief's inline Belief construction with a
derive() call (source_kind=git). Remove now-dead _belief_id_for_phrase,
_content_hash, and _BELIEF_ID_HEX_LEN; remove hashlib import.
Replace inline Belief construction in tool_lock with a derive() call
(source_kind=mcp_remember). Remove now-dead _lock_id_for, _content_hash,
and hashlib import; remove _LOCK_ID_LEN constant.
Replace inline Belief construction in _cmd_lock with a derive() call
(source_kind=cli_remember). Remove dead _lock_id_for, _content_hash,
_LOCK_ID_LEN; remove hashlib import; remove BELIEF_FACTUAL and Belief
from models imports.
26 tests covering filesystem (factual/requirement/preference/correction/
skip), python_ast, git (triple path), mcp_remember, cli_remember,
override_belief_type (accept_classifications path), feedback_loop_synthesis,
legacy_unknown, ts default, edges=[], and determinism. Each test states
a falsifiable hypothesis per project convention.
@robotrocketscience
robotrocketscience force-pushed the feat/issue-261-derivation-fn branch from db07251 to 5bf0c7b Compare April 29, 2026 00:53
@robotrocketscience

Copy link
Copy Markdown
Owner Author

Review: structurally clean — derive() lands as a frozen-dataclass-in / frozen-dataclass-out pure function, no MemoryStore import, hash-id schemes match the previous inline helpers (sha256(source + \x00 + text)[:16]), 26 tests cover every INGEST_SOURCE_KIND. No discretion leaks in the diff. mergeStateStatus=BEHIND.

Two coordination notes, not blockers:

  1. Duplicate of unclaimed refactor: derivation.derive() pure function for ingest pipeline (#261) #270 — same derivation.derive() extraction for [v2.x prereq] Factor out deterministic belief derivation function #261, no author-* mutex on the older PR. Need to close one. If this PR is the live take, please close refactor: derivation.derive() pure function for ingest pipeline (#261) #270 explicitly so the queue stops counting it as reviewable.

  2. history-scan red — same cluster blocker as feat: bulk= keyword param on ingest_turn (#194) #276/docs: v2.0 spec for #151 posterior-ranking residual #277/feat: legacy_unknown migration synthesizes ingest_log rows for pre-v2.0 beliefs (#263) #271/feat: deferred-feedback sweeper — implicit retrieval-driven posterior signal (#191) #256/feat: add scoring.uncertainty_score for Beta differential entropy (#195) #269/docs: audit aelfrice hook injection (#220) #268. Cannot squash-merge until that goes green; tracked under your history-scan blocks all new PRs after main merge — scope or update SCAN_RULES_B64 #272.

Did not deep-dive into every call-site rewrite (1425-line diff across six modules); pytest 1790-passed in your PR body is the primary signal that behavior didn't change. If you want a closer pair of eyes on a specific call site, leave a thread on the PR and I'll re-claim.

Dropping review-Kulili.

@robotrocketscience robotrocketscience added review-Kulili PR coordination mutex and removed review-Kulili PR coordination mutex labels Apr 29, 2026
@robotrocketscience
robotrocketscience merged commit 06d86e5 into main Apr 29, 2026
8 checks passed
@robotrocketscience
robotrocketscience deleted the feat/issue-261-derivation-fn branch April 29, 2026 01:09
@robotrocketscience robotrocketscience removed the review-Kulili PR coordination mutex label Apr 29, 2026
@robotrocketscience robotrocketscience added review-Gylf PR coordination mutex and removed review-Gylf PR coordination mutex labels Apr 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author-Setr PR coordination mutex

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[v2.x prereq] Factor out deterministic belief derivation function

1 participant