Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
with:
path: |
${{ github.workspace }}/.cache/huggingface
key: ${{ runner.os }}-sentence-transformers-${{ hashFiles('pyproject.toml', 'app/startup/settings.py') }}
key: ${{ runner.os }}-onnx-${{ hashFiles('pyproject.toml', 'app/infrastructure/embeddings/local_provider.py') }}

- name: Install dependencies
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/real-embedding-smoke.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
with:
path: |
${{ github.workspace }}/.cache/huggingface
key: ${{ runner.os }}-sentence-transformers-${{ hashFiles('pyproject.toml', 'app/startup/settings.py') }}
key: ${{ runner.os }}-onnx-${{ hashFiles('pyproject.toml', 'app/infrastructure/embeddings/local_provider.py') }}

- name: Install dependencies
run: |
Expand Down
10 changes: 10 additions & 0 deletions app/core/ports/db/concept_repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ def get_concept_bundle(
) -> dict[str, Any] | None:
"""This method returns one concept plus directly related graph records."""

@abstractmethod
def get_concept_bundles(
self,
*,
repo_id: str,
concept_ids: Sequence[str],
include_lifecycle_events: bool = False,
) -> dict[str, dict[str, Any]]:
"""Return bundles keyed by concept ID; omit missing concepts."""

@abstractmethod
def find_concepts_for_memory_ids(
self, *, repo_id: str, memory_ids: Sequence[str]
Expand Down
29 changes: 15 additions & 14 deletions app/core/use_cases/admin/initialize_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,20 +230,21 @@ def run_initialize_runtime(
schema_changed = ports.apply_schema_migrations(machine_config)
mutated_machine = mutated_machine or schema_changed

if not maintenance_only:
machine_config = ports.update_bootstrap_state(
machine_config,
bootstrap_state=ports.bootstrap_state_provisioning,
current_step="embeddings",
last_error=None,
)
ports.save_machine_config(machine_config)
embedding_changed, machine_config = ports.prewarm_embeddings(
machine_config,
skip_model_download=skip_model_download,
)
ports.save_machine_config(machine_config)
mutated_machine = mutated_machine or embedding_changed
machine_config = ports.update_bootstrap_state(
machine_config,
bootstrap_state=ports.bootstrap_state_provisioning,
current_step="embeddings",
last_error=None,
)
ports.save_machine_config(machine_config)
embedding_changed, machine_config = ports.prewarm_embeddings(
machine_config,
skip_model_download=skip_model_download,
)
ports.save_machine_config(machine_config)
mutated_machine = mutated_machine or embedding_changed
if machine_config.embeddings.readiness_state == "failed":
raise InitDependencyError(machine_config.embeddings.last_error)

machine_config = ports.update_bootstrap_state(
machine_config,
Expand Down
56 changes: 41 additions & 15 deletions app/core/use_cases/retrieval/deterministic_graph_recall.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,19 @@ def build_deterministic_graph_pack(
uow=uow,
thresholds=thresholds,
)
concept_bundles = uow.concepts.get_concept_bundles(
repo_id=request.repo_id,
concept_ids=list(concept_candidates),
)
selected_concepts, concept_trace = _select_concepts(
request=request,
concept_candidates=concept_candidates,
uow=uow,
concept_bundles=concept_bundles,
)
traversal = _traverse_selected_concepts(
request=request,
selected_concepts=selected_concepts,
concept_bundles=concept_bundles,
memory_candidates=memory_candidates,
uow=uow,
)
Expand Down Expand Up @@ -484,8 +489,14 @@ def _expand_structural_memory_relations(
predicates=predicates,
)
for row in rows:
key = (row["subject_memory_id"], row["predicate"], row["object_memory_id"])
memory_relations[key] = row | {"validated_at": _iso(row["validated_at"])}
key = (
row["subject_memory_id"],
row["predicate"],
row["object_memory_id"],
)
memory_relations[key] = row | {
"validated_at": _iso(row["validated_at"])
}
del memory_relations[key]["visible_memory_ids"]
for neighbor in select_structural_memory_relation_neighbors(
rows, anchor_memory_id=anchor_memory_id
Expand Down Expand Up @@ -605,14 +616,14 @@ def _select_concepts(
*,
request: MemoryReadRequest,
concept_candidates: dict[str, dict[str, Any]],
uow: IUnitOfWork,
concept_bundles: dict[str, dict[str, Any]],
) -> tuple[list[dict[str, Any]], dict[str, Any]]:
selected: list[tuple[float, str, dict[str, Any]]] = []
rejected_count = 0
query_terms = _tokenize(request.query)
identifiers = _extract_identifiers(request.query)
for concept_id, candidate in concept_candidates.items():
bundle = uow.concepts.get_concept_bundle(
repo_id=request.repo_id, concept_ref=concept_id
)
bundle = concept_bundles.get(concept_id)
if bundle is None:
rejected_count += 1
continue
Expand All @@ -621,8 +632,8 @@ def _select_concepts(
continue
score = float(candidate["score"]) + _bundle_signal_score(
bundle=bundle,
query_terms=_tokenize(request.query),
identifiers=_extract_identifiers(request.query),
query_terms=query_terms,
identifiers=identifiers,
)
if score <= 0:
rejected_count += 1
Expand Down Expand Up @@ -653,6 +664,7 @@ def _traverse_selected_concepts(
*,
request: MemoryReadRequest,
selected_concepts: list[dict[str, Any]],
concept_bundles: dict[str, dict[str, Any]],
memory_candidates: dict[str, dict[str, Any]],
uow: IUnitOfWork,
) -> dict[str, Any]:
Expand Down Expand Up @@ -711,12 +723,22 @@ def _traverse_selected_concepts(
]
neighbors = []
selected_ids = {entry["bundle"]["concept"].id for entry in selected_concepts}
missing_ids = [
key
for key in unique_neighbor_ids
if key not in concept_bundles and key not in selected_ids
]
if missing_ids:
concept_bundles.update(
uow.concepts.get_concept_bundles(
repo_id=request.repo_id,
concept_ids=missing_ids,
)
)
for neighbor_id in unique_neighbor_ids:
if neighbor_id in selected_ids:
continue
bundle = uow.concepts.get_concept_bundle(
repo_id=request.repo_id, concept_ref=neighbor_id
)
bundle = concept_bundles.get(neighbor_id)
if bundle is None or not _active_concept(bundle["concept"]):
continue
neighbors.append(
Expand Down Expand Up @@ -1214,9 +1236,13 @@ def _brief_case_texts(
for target in memories:
linked = relations_by_target[target["id"]]
if not linked:
cases.extend(_brief_memory_texts(
[target], kinds={"solution"}, link_roles={"solution_for", "example_of"}
))
cases.extend(
_brief_memory_texts(
[target],
kinds={"solution"},
link_roles={"solution_for", "example_of"},
)
)
for relation in linked:
subject = memories_by_id[relation["subject_memory_id"]]
qualifiers = [relation["status"]]
Expand Down
15 changes: 2 additions & 13 deletions app/infrastructure/db/admin/provisioning/external_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from __future__ import annotations

import importlib.metadata
import secrets

import psycopg
Expand Down Expand Up @@ -43,7 +42,6 @@ def build_fresh_machine_config(
raise RuntimeError("runtime.embeddings must be configured")
app_dsn = _provision_external_app_role(admin_dsn=admin_dsn)
instance_id = dsn_fingerprint(admin_dsn)
backend_version = _sentence_transformers_version()
return MachineConfig(
config_version=CONFIG_VERSION,
bootstrap_version=BOOTSTRAP_VERSION,
Expand All @@ -56,10 +54,10 @@ def build_fresh_machine_config(
managed=None,
backups=BackupState(root=str(get_machine_backups_dir()), mirror_root=None),
embeddings=EmbeddingRuntimeState(
provider=str(embeddings.get("provider") or "sentence_transformers"),
provider=str(embeddings.get("provider") or "onnxruntime"),
model=str(embeddings.get("model") or "all-MiniLM-L6-v2"),
model_revision=None,
backend_version=backend_version,
backend_version=None,
cache_path=str(get_machine_models_dir()),
readiness_state="pending",
last_error=None,
Expand Down Expand Up @@ -205,12 +203,3 @@ def _app_dsn_from_admin_dsn(*, admin_dsn: str, app_user: str, app_password: str)

url = make_url(admin_dsn)
return str(url.set(username=app_user, password=app_password))


def _sentence_transformers_version() -> str | None:
"""Return the installed sentence-transformers version when present."""

try:
return importlib.metadata.version("sentence-transformers")
except importlib.metadata.PackageNotFoundError:
return None
4 changes: 2 additions & 2 deletions app/infrastructure/db/admin/provisioning/managed_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def build_fresh_machine_config(*, embeddings: dict[str, object]) -> MachineConfi
),
backups=BackupState(root=str(get_machine_backups_dir()), mirror_root=None),
embeddings=EmbeddingRuntimeState(
provider=str(embeddings.get("provider") or "sentence_transformers"),
provider=str(embeddings.get("provider") or "onnxruntime"),
model=str(embeddings.get("model") or "all-MiniLM-L6-v2"),
model_revision=None,
backend_version=None,
Expand Down Expand Up @@ -288,7 +288,7 @@ def recover_machine_config_from_docker(
),
backups=BackupState(root=str(get_machine_backups_dir()), mirror_root=None),
embeddings=EmbeddingRuntimeState(
provider="sentence_transformers",
provider="onnxruntime",
model=str(embeddings.get("model") or "all-MiniLM-L6-v2"),
model_revision=None,
backend_version=None,
Expand Down
Loading
Loading