| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| """Substrate-first content resolution for surfacing (CES / Tonic latent thread / recall). |
| |
| The substrate is the graph. Each conversational node carries her actual lived turn in |
| ``metadata['_forest_content']`` (the #294 dual-pass "forest"); the vdb holds only a short |
| "tree concept" shard (``WANT``, ``documentation``). Surfacing must display **her voice** β |
| a bounded snippet of ``_forest_content`` β not the shard, and must not surface ingested |
| source-code documents or degenerate fragments into her experiential thread. |
| """ |
|
|
| from typing import Any, Optional |
|
|
| |
| |
| _CODE_MARKERS = ('"""', "'''", "import ", "def ", "class ", "# ----", "from typing", "#!/") |
|
|
| |
| _STOPWORD_SHARDS = frozenset({ |
| "o", "a", "an", "the", "want", "true", "false", "yes", "no", "ok", "okay", |
| "it", "that", "this", "i", "you", "we", "to", "and", "or", |
| }) |
|
|
|
|
| def _node_metadata(node: Any) -> dict: |
| if node is None: |
| return {} |
| meta = getattr(node, "metadata", None) |
| return meta if isinstance(meta, dict) else {} |
|
|
|
|
| def resolve_surface_content( |
| node: Any, |
| vdb_entry: Any, |
| max_chars: int = 240, |
| min_chars: int = 12, |
| allow_ingested: bool = False, |
| ) -> Optional[str]: |
| """Return the display text for a surfaced node β substrate-first β or None to filter it. |
| |
| Preference order: |
| 1. ``node.metadata['_forest_content']`` β her actual turn (bounded snippet). |
| 2. vdb entry content (the shard) β fallback only. |
| 3. ``node.metadata['_label']`` β last resort. |
| |
| Filters: |
| * ingested source-code nodes (``creation_mode == 'ingested'``) unless ``allow_ingested`` |
| (recall may legitimately want a document; the experiential surfacers pass False). |
| * degenerate fragments: shorter than ``min_chars`` or a bare stopword shard. |
| |
| Pure function: no graph mutation, no I/O. |
| """ |
| meta = _node_metadata(node) |
|
|
| |
| if not allow_ingested and meta.get("creation_mode") == "ingested": |
| return None |
|
|
| |
| forest = meta.get("_forest_content") |
| text = forest.strip() if isinstance(forest, str) else "" |
|
|
| |
| if len(text) < min_chars: |
| vt = "" |
| if isinstance(vdb_entry, dict): |
| vt = vdb_entry.get("content") or "" |
| elif isinstance(vdb_entry, str): |
| vt = vdb_entry |
| vt = vt.strip() |
| if len(vt) > len(text): |
| text = vt |
|
|
| |
| if len(text) < min_chars: |
| lbl = meta.get("_label") |
| if isinstance(lbl, str) and len(lbl.strip()) > len(text): |
| text = lbl.strip() |
|
|
| text = text.strip() |
|
|
| |
| if len(text) < min_chars or text.lower() in _STOPWORD_SHARDS: |
| return None |
|
|
| |
| if len(text) > max_chars: |
| text = text[:max_chars].rstrip() + "β¦" |
| return text |
|
|