Skip to content
37 changes: 16 additions & 21 deletions src/aelfrice/classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@
BELIEF_REQUIREMENT,
BELIEF_TYPES,
INGEST_SOURCE_FILESYSTEM,
LOCK_NONE,
ONBOARD_STATE_PENDING,
ORIGIN_AGENT_INFERRED,
Belief,
OnboardSession,
)

Expand Down Expand Up @@ -485,6 +482,10 @@ def accept_classifications(
skipped_existing = 0
skipped_unclassified = 0

# Lazy import: derivation imports classify_sentence from this module;
# importing at module-load would form a cycle.
from aelfrice.derivation import DerivationInput, derive # noqa: PLC0415

for sd in sentences_data:
idx = int(sd["index"])
text = str(sd["text"])
Expand All @@ -496,11 +497,20 @@ def accept_classifications(
if not c.persist:
skipped_non_persisting += 1
continue
bid = _derive_belief_id(text, source)
out = derive(DerivationInput(
raw_text=text,
source_kind=INGEST_SOURCE_FILESYSTEM,
source_path=source,
ts=timestamp,
override_belief_type=c.belief_type,
))
# override_belief_type always produces a belief (persist=True
# is the caller's responsibility; checked above).
assert out.belief is not None
bid = out.belief.id
if store.get_belief(bid) is not None:
skipped_existing += 1
continue
alpha, beta = get_source_adjusted_prior(c.belief_type, source)
# v2.0 #205 parallel-write: log the host-classified text.
store.record_ingest(
source_kind=INGEST_SOURCE_FILESYSTEM,
Expand All @@ -509,22 +519,7 @@ def accept_classifications(
derived_belief_ids=[bid],
ts=timestamp,
)
store.insert_belief(
Belief(
id=bid,
content=text,
content_hash=_content_hash(text),
alpha=alpha,
beta=beta,
type=c.belief_type,
lock_level=LOCK_NONE,
locked_at=None,
demotion_pressure=0,
created_at=timestamp,
last_retrieved_at=None,
origin=ORIGIN_AGENT_INFERRED,
)
)
store.insert_belief(out.belief)
inserted += 1

store.complete_onboard_session(session_id, timestamp)
Expand Down
39 changes: 11 additions & 28 deletions src/aelfrice/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
from __future__ import annotations

import argparse
import hashlib
import json
import os
import subprocess
Expand All @@ -59,14 +58,12 @@
regime_description,
)
from aelfrice.models import (
BELIEF_FACTUAL,
INGEST_SOURCE_CLI_REMEMBER,
LOCK_NONE,
LOCK_USER,
ORIGIN_AGENT_INFERRED,
ORIGIN_USER_STATED,
ORIGIN_USER_VALIDATED,
Belief,
)
from aelfrice import __version__ as _AELFRICE_VERSION
from aelfrice.benchmark import run_benchmark, seed_corpus
Expand All @@ -75,6 +72,7 @@
accept_classifications,
start_onboard_session,
)
from aelfrice.derivation import DerivationInput, derive
from aelfrice.doctor import (
classify_orphans as _classify_orphans,
diagnose,
Expand Down Expand Up @@ -160,7 +158,6 @@
DEFAULT_HOOK_COMMAND: Final[str] = "aelf-hook"
DEFAULT_PRE_COMPACT_HOOK_COMMAND: Final[str] = "aelf-pre-compact-hook"
_FEEDBACK_VALENCES: Final[dict[str, float]] = {"used": 1.0, "harmful": -1.0}
_LOCK_ID_LEN: Final[int] = 16
_VALID_SCOPES: Final[tuple[SettingsScope, ...]] = ("user", "project")


Expand Down Expand Up @@ -262,14 +259,6 @@ def _utc_now_iso() -> str:
return datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")


def _lock_id_for(content: str) -> str:
return hashlib.sha256(f"lock\x00{content}".encode("utf-8")).hexdigest()[:_LOCK_ID_LEN]


def _content_hash(content: str) -> str:
return hashlib.sha256(content.encode("utf-8")).hexdigest()


def _resolve_corpus_min() -> int:
"""Read AELFRICE_CORPUS_MIN env var, fall back to auditor default.

Expand Down Expand Up @@ -797,9 +786,16 @@ def _cmd_rebuild(args: argparse.Namespace, out: object) -> int:
def _cmd_lock(args: argparse.Namespace, out: object) -> int:
store = _open_store()
try:
bid = _lock_id_for(args.statement)
existing = store.get_belief(bid)
now = _utc_now_iso()
derived = derive(DerivationInput(
raw_text=args.statement,
source_kind=INGEST_SOURCE_CLI_REMEMBER,
ts=now,
))
# cli_remember always produces a belief.
assert derived.belief is not None
bid = derived.belief.id
existing = store.get_belief(bid)
if existing is None:
# v2.0 #205 parallel-write.
store.record_ingest(
Expand All @@ -808,20 +804,7 @@ def _cmd_lock(args: argparse.Namespace, out: object) -> int:
derived_belief_ids=[bid],
ts=now,
)
store.insert_belief(Belief(
id=bid,
content=args.statement,
content_hash=_content_hash(args.statement),
alpha=9.0,
beta=0.5,
type=BELIEF_FACTUAL,
lock_level=LOCK_USER,
locked_at=now,
demotion_pressure=0,
created_at=now,
last_retrieved_at=None,
origin=ORIGIN_USER_STATED,
))
store.insert_belief(derived.belief)
print(f"locked: {bid}", file=out) # type: ignore[arg-type]
else:
existing.lock_level = LOCK_USER
Expand Down
Loading
Loading