f2f010a350
* refactor(gateway): create module for launching async/bg agents * refactor(memory): refactor worker launch around source context & output deltas * refactor(gateway): generalize async/bg module * refactor(memory): revamp worker launching * feat(memory): add observation linking * test(memory): remove redundant test branches * fix(memory): make 'supersedes' relation directional * fix(memory): don't create empty project observation dirs * fix(memory): schedule direct observations for linking * fix(cli): wait for observation linker before shutdown * fix(memory): block arbitrary writes to /memories * fix(linker): remove `linked_by` attribute from frontmatter * refactor(linker): rename base relationship to `comlpements` * fix(cli): bump worker wait to 2m * feat(tools): catch malformed tool calls & retry * feat(status): add linking result to statusbar * fix(linker): don't launch linker when observations are disabled * fix(memory): use posix paths * fix(watcher): call abort hook on error status * fix(watcher): delete thread on failed run creation * fix(watcher): preserve url * fix(observation): record session_id, drop unused fields * fix(memory): reject unsupported worker source types * refactor(backends): shared memory backend builder * fix(scheduler): resolve linker inputs outside lock * fix(memory): dont launch workers / record observations without thread_id * feat(memory): include related observations in tool results * fix(memory): skip malformed observation frontmatter * revert(tools): drop tool error handling changes from this PR * fix(memory): serialize observation link writes * fix(memory): queue observations written by aborted workers * fix(memory): track observation linker launch handoff * fix(memory): resolve cross-project related observations * fix(status): avoid recounting reason-only link updates * fix(memory): avoid rereading file for content * fix(linker): use neutral prose for bidirectional reasons * test(memory): coverage for aborted/failed launches * test(memory): cleanup & helpers * feat(linker): add observations index hint
218 lines
6.5 KiB
Python
218 lines
6.5 KiB
Python
"""Prompt-facing observation memory indexes."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import Iterable, Sequence
|
|
from pathlib import Path
|
|
|
|
from ..types import MemoryScope, MemoryType, ObservationSearchDocument
|
|
from .store import list_observation_documents
|
|
|
|
DEFAULT_MAX_INLINE_OBSERVATION_INDEX_CHARS = 12_000
|
|
|
|
|
|
def build_observation_index_context(
|
|
*,
|
|
memory_dir: str | Path,
|
|
project_id: str,
|
|
max_inline_chars: int = DEFAULT_MAX_INLINE_OBSERVATION_INDEX_CHARS,
|
|
) -> str:
|
|
"""Build a compact observation-memory index for prompts."""
|
|
return _format_observation_index_context(
|
|
_observation_documents(memory_dir=memory_dir, project_id=project_id),
|
|
include_counts=True,
|
|
include_paths=True,
|
|
include_search_hints=True,
|
|
empty_context=True,
|
|
intro="Indexed observations:",
|
|
max_inline_chars=max_inline_chars,
|
|
)
|
|
|
|
|
|
def build_observation_linker_index_context(
|
|
*,
|
|
memory_dir: str | Path,
|
|
project_id: str,
|
|
exclude_ids: Iterable[str],
|
|
max_inline_chars: int = DEFAULT_MAX_INLINE_OBSERVATION_INDEX_CHARS,
|
|
) -> str:
|
|
"""Build the existing-observation index included in linker launches."""
|
|
return _format_observation_index_context(
|
|
_observation_documents(
|
|
memory_dir=memory_dir,
|
|
project_id=project_id,
|
|
exclude_ids=exclude_ids,
|
|
),
|
|
include_counts=False,
|
|
include_paths=False,
|
|
include_search_hints=False,
|
|
empty_context=False,
|
|
intro=(
|
|
"Stored observation snapshot excluding the current batch "
|
|
"(id [type/scope]: summary). Read before linking when needed."
|
|
),
|
|
max_inline_chars=max_inline_chars,
|
|
)
|
|
|
|
|
|
def _observation_documents(
|
|
*,
|
|
memory_dir: str | Path,
|
|
project_id: str,
|
|
exclude_ids: Iterable[str] = (),
|
|
) -> list[ObservationSearchDocument]:
|
|
excluded = set(exclude_ids)
|
|
return sorted(
|
|
(
|
|
document
|
|
for document in list_observation_documents(
|
|
memory_dir=memory_dir,
|
|
project_id=project_id,
|
|
)
|
|
if document.observation_id not in excluded
|
|
),
|
|
key=lambda document: document.observation_id,
|
|
)
|
|
|
|
|
|
def _format_observation_index_context(
|
|
documents: Sequence[ObservationSearchDocument],
|
|
*,
|
|
include_counts: bool = True,
|
|
include_paths: bool = True,
|
|
include_search_hints: bool = True,
|
|
empty_context: bool = True,
|
|
intro: str = "Indexed observations:",
|
|
max_inline_chars: int = DEFAULT_MAX_INLINE_OBSERVATION_INDEX_CHARS,
|
|
) -> str:
|
|
"""Format parsed observation documents as a prompt index."""
|
|
if not documents and not empty_context:
|
|
return ""
|
|
|
|
header = ["<observation_memory>"]
|
|
if include_counts:
|
|
header.append(_observation_index_count_line(documents))
|
|
|
|
footer = [_observation_search_hints()] if include_search_hints else []
|
|
if not documents:
|
|
return "\n".join([*header, *footer, "</observation_memory>"])
|
|
|
|
lines = [
|
|
_observation_index_line(document, include_paths=include_paths)
|
|
for document in documents
|
|
]
|
|
full = "\n".join(
|
|
[
|
|
*header,
|
|
intro,
|
|
*lines,
|
|
*footer,
|
|
"</observation_memory>",
|
|
]
|
|
)
|
|
if len(full) <= max_inline_chars:
|
|
return full
|
|
|
|
return _truncated_observation_index_context(
|
|
header=header,
|
|
intro=intro,
|
|
lines=lines,
|
|
footer=footer,
|
|
max_inline_chars=max_inline_chars,
|
|
)
|
|
|
|
|
|
def _truncated_observation_index_context(
|
|
*,
|
|
header: Sequence[str],
|
|
intro: str,
|
|
lines: Sequence[str],
|
|
footer: Sequence[str],
|
|
max_inline_chars: int,
|
|
) -> str:
|
|
prefix = [
|
|
*header,
|
|
"Observation index truncated to entries that fit.",
|
|
intro,
|
|
]
|
|
suffix = [*footer, "</observation_memory>"]
|
|
selected: list[str] = []
|
|
for line in lines:
|
|
candidate = "\n".join([*prefix, *selected, line, *suffix])
|
|
if len(candidate) <= max_inline_chars:
|
|
selected.append(line)
|
|
if selected:
|
|
return "\n".join([*prefix, *selected, *suffix])
|
|
|
|
return "\n".join(
|
|
[
|
|
*header,
|
|
"Observation summaries are too large to inline; search on demand.",
|
|
*footer,
|
|
"</observation_memory>",
|
|
]
|
|
)
|
|
|
|
|
|
def _observation_index_line(
|
|
document: ObservationSearchDocument,
|
|
*,
|
|
include_paths: bool,
|
|
) -> str:
|
|
typed_scope = f"[{document.memory_type.value}/{document.scope.value}]"
|
|
if include_paths:
|
|
return (
|
|
f"- {document.observation_id} {typed_scope} "
|
|
f"{document.path}: {document.summary}"
|
|
)
|
|
return f"- {document.observation_id} {typed_scope}: {document.summary}"
|
|
|
|
|
|
def _observation_index_count_line(
|
|
documents: Sequence[ObservationSearchDocument],
|
|
) -> str:
|
|
"""Return compact observation counts by scope and memory type."""
|
|
scope_counts = dict.fromkeys(MemoryScope, 0)
|
|
type_counts = dict.fromkeys(MemoryType, 0)
|
|
for document in documents:
|
|
scope_counts[document.scope] += 1
|
|
type_counts[document.memory_type] += 1
|
|
return (
|
|
f"Counts: total={len(documents)}; "
|
|
f"scope global={scope_counts[MemoryScope.GLOBAL]}, "
|
|
f"project={scope_counts[MemoryScope.PROJECT]}; "
|
|
f"type semantic={type_counts[MemoryType.SEMANTIC]}, "
|
|
f"procedural={type_counts[MemoryType.PROCEDURAL]}, "
|
|
f"episodic={type_counts[MemoryType.EPISODIC]}."
|
|
)
|
|
|
|
|
|
def _observation_search_hints() -> str:
|
|
"""Return stable search hints for observation memory."""
|
|
return "\n".join(
|
|
[
|
|
"Search hints:",
|
|
"- Each line gives id, type/scope, path, and summary.",
|
|
(
|
|
"- Use `search_observations` for ranked keyword search "
|
|
"and `read_memory` for known observation IDs."
|
|
),
|
|
"- Use `mode=regex` only when exact grep-like matching is required.",
|
|
"- Search by id when you already know it from the index.",
|
|
(
|
|
"- Filter by type when appropriate: "
|
|
"`memory_type: procedural`, `memory_type: semantic`, or "
|
|
"`memory_type: episodic`."
|
|
),
|
|
(
|
|
"- Filter by scope when appropriate: "
|
|
"`scope: project` or `scope: global`."
|
|
),
|
|
(
|
|
"- Search with a few distinctive words or phrases from "
|
|
"the current work that describe the issue, constraint, "
|
|
"procedure, or prior result to find."
|
|
),
|
|
]
|
|
)
|