Skip to content

symfonic.memory.backends.postgres_candidate_predicates

postgres_candidate_predicates

Row-local eligibility and rank fragments of native candidate selection.

The ranked statement and the CORE-199 eligible-candidate index are built from these same strings, so the index can only hold the rows the statement admits. Every fragment references bare memory_nodes columns and no parameter.

identity

identity(category, subject=None)

Both compatibility spellings must be absent or the same complete pair.

Source code in src/symfonic/memory/backends/postgres_candidate_predicates.py
def identity(category, subject=None):
    """Both compatibility spellings must be absent or the same complete pair."""
    parts = ("properties", "properties->'record_metadata'")
    checks, present, subjects = [], [], []
    for part in parts:
        cat, sub = f"({part})->'memory_category'", f"({part})->'subject'"
        missing = f"({cat} IS NULL AND {sub} IS NULL)"
        subject_check = f"btrim(({part})->>'subject') <> ''"
        if subject is not None:
            subject_check = f"btrim(({part})->>'subject') = '{subject}'"
        valid = (f"(jsonb_typeof({cat})='string' AND "
                 f"btrim(({part})->>'memory_category')='{category}' AND "
                 f"jsonb_typeof({sub})='string' AND {subject_check})")
        checks.append(f"({missing} OR {valid})")
        present.append(valid)
        subjects.append(f"btrim(({part})->>'subject')")
    agreement = f"({subjects[0]} IS NULL OR {subjects[1]} IS NULL OR {subjects[0]}={subjects[1]})"
    return f"({' AND '.join(checks)} AND ({' OR '.join(present)}) AND {agreement})"

lifecycle_flags

lifecycle_flags()

Pending, retracted and unreviewed procedural rows are never candidates.

Source code in src/symfonic/memory/backends/postgres_candidate_predicates.py
def lifecycle_flags():
    """Pending, retracted and unreviewed procedural rows are never candidates."""
    flags = []
    for key in ("__symfonic_pending__", "__symfonic_retracted__"):
        flags.append(f"COALESCE(properties->'{key}','null'::jsonb) "
                     "IN ('null'::jsonb,'false'::jsonb,'0'::jsonb,"
                     "'\"\"'::jsonb,'[]'::jsonb,'{}'::jsonb)")
    # Top-level lifecycle keys take precedence even when malformed/null.
    status = ("CASE WHEN properties ? 'status' THEN properties->>'status' "
              "ELSE properties->'record_metadata'->>'status' END")
    active = ("CASE WHEN properties ? 'active' THEN properties->'active' "
              "ELSE properties->'record_metadata'->'active' END")
    flags.append(f"(layer <> 'procedural' OR ({status} = 'approved' "
                 f"AND {active} = 'true'::jsonb))")
    return flags

profile

profile()

The user-profile slot: a typed profile identity or a legacy SOUL row.

Source code in src/symfonic/memory/backends/postgres_candidate_predicates.py
def profile():
    """The user-profile slot: a typed profile identity or a legacy SOUL row."""
    typed = identity("profile", "user")
    absent = " AND ".join(
        f"({part})->'{key}' IS NULL"
        for part in ("properties", "properties->'record_metadata'")
        for key in ("memory_category", "subject")
    )
    marker = ("upper(btrim(COALESCE(properties->'record_metadata'->>'label_prefix',"
              "properties->>'label_prefix','')))='SOUL'")
    return (f"(layer='semantic' AND ({typed} OR "
            f"({absent} AND (ltrim(label) ~* '^SOUL([ :]|$)' OR {marker}))))")

session

session()

The row's conversation as JSON; a missing id is the empty string.

Source code in src/symfonic/memory/backends/postgres_candidate_predicates.py
def session():
    """The row's conversation as JSON; a missing id is the empty string."""
    metadata = ("CASE WHEN jsonb_typeof(properties->'record_metadata')='object' "
                "THEN properties->'record_metadata' ELSE properties END")
    return f"COALESCE(({metadata})->'session_id','\"\"'::jsonb)"

shared

shared()

Rows every session may read; NULL (a malformed event) is not shared.

Source code in src/symfonic/memory/backends/postgres_candidate_predicates.py
def shared():
    """Rows every session may read; NULL (a malformed event) is not shared."""
    return (f"(layer NOT IN ('working','episodic') "
            f"OR (layer='episodic' AND {identity('past_event')}))")