honcho/tests/deriver
Aakash Kattelu ccdb8ba113
fix(deriver): fix create_documents deadlock (#1033)
* fix(deriver): eliminate create_documents deadlock and stop silently burning batches on transient errors

Two concurrent work units writing the same (workspace, observer, observed)
collection deadlocked on times_derived reinforcement UPDATEs issued in
batch order (DEV-1975, 682 events in 90 days). The deadlock was swallowed
per-document, the loop cascaded PendingRollbackErrors against the dead
session, the whole batch was lost, and the queue item was marked processed.

- serialize writers per collection with a transaction-scoped advisory lock
  (pg_advisory_xact_lock + SET LOCAL lock_timeout), skipped for insert-only
  batches; covers all three row-lock sites in one move
- hoist external-vector-store dup-candidate resolution ahead of the first
  DB statement so the lock's critical section contains no network calls
- abort the batch on SQLAlchemyError instead of continuing through an
  aborted transaction; per-document skip semantics kept for non-DB errors
- classify transient errors (new src/utils/retryable_errors.py) and retry
  them via a bounded in-process counter instead of marking items errored

* fix(deriver): replace create_documents advisory lock with id-ordered row locks

Advisory locks are database-scoped and would serialize every writer to a
collection, including across Groudon tenants that share names. Collect
reinforcement and replace ops during the loop, lock target rows with
SELECT ... ORDER BY id FOR UPDATE, then apply. populate_existing reloads
times_derived so a prefetched identity-map row cannot lose a concurrent
increment.

* fix(deriver): harden create_documents candidate hoist and test isolation

Skip empty embeddings on the external-store path, isolate per-document
resolve failures, and keep replacement times_derived in the in-batch
ledger. Patch get_external_vector_store in the hoist test and cover
in-loop SQLAlchemyError abort.

* fix(deriver): address CodeRabbit findings on create_documents deadlock fix

- Distinguish external resolve failure ([] skip) from pgvector fallback (None)
  so _semantic_dup_decision never re-enters external I/O under an open session
- Bound external candidate hoist concurrency with a semaphore
- Map in-loop IntegrityError to ValidationException for a uniform contract
- Persist transient retry attempts on the oldest unprocessed queue item so
  every deriver instance shares one MAX_RETRYABLE_ATTEMPTS budget
- Cover resolve-failure skip and multi-manager reclaim of the retry budget

* fix(deriver): harden retry metadata cleanup and stale reinforce fallback

- Strip _retry_attempts from payloads in the same transaction as
  mark_queue_items_as_processed / mark_queue_item_as_errored
- Clear shared retry metadata only after a successful terminal mark
- On reinforce, if the locked target is gone or soft-deleted, insert the
  incoming document instead of dropping it
- Skip pgvector semantic lookup when embedding is empty so query_documents
  cannot embed under an open session

* fix(deriver): address review on deadlock retry and row-lock apply

Strip _retry_attempts before payload validation so non-representation
tasks are not burned as extra_forbidden. Re-raise retryable observer
save errors after telemetry so the queue actually retries. Skip
same-batch reinforce fallbacks after a replace. Revert unordered
FOR UPDATE on mark processed/errored and drop post-commit retry
cleanup from the success path.

* fix: add test and simplify queue query

---------

Co-authored-by: Vineeth Voruganti <13438633+VVoruganti@users.noreply.github.com>
2026-09-02 11:07:50 -04:00
..
README.md create Representation class and use it to unify all formatting (#214) 2025-10-07 15:28:44 -04:00
__init__.py Vineeth/dev 1027 (#177) 2025-08-06 16:20:22 -04:00
conftest.py feat: honcho 3.0, sdks 2.0, excise stainless, update v3 docs, changelogs (#331) 2026-01-22 15:16:28 -05:00
test_deriver_processing.py fix(deriver): fix create_documents deadlock (#1033) 2026-09-02 11:07:50 -04:00
test_embed_now.py add prometheus metric for in_flight 2026-07-09 12:01:09 -04:00
test_enqueue_dream.py fix(dreamer): threshold and time-guard semantics (#573) 2026-04-30 11:40:51 -04:00
test_prompts.py feat: deriver custom instructions (#609) 2026-05-11 18:05:42 -04:00
test_queue_operations.py feat: webhooks (#168) 2025-08-06 17:52:35 -04:00
test_queue_processing.py fix(deriver): fix create_documents deadlock (#1033) 2026-09-02 11:07:50 -04:00
test_representation_crud.py feat: agentic dreamer and agentic dialectic (#309) 2026-01-12 15:12:17 -05:00
test_scope_backfill.py fix(deriver): reduce scope backfill memory usage (#1104) 2026-09-01 09:17:34 -04:00
test_vector_reconciliation.py fix(embedding): truncate in batch embed and return results breakdown (#1019) 2026-08-20 11:42:38 -04:00

README.md

Deriver Testing

This directory contains tests for the deriver system, which handles background processing of messages to extract insights and update working representations.

Structure

  • conftest.py - Shared fixtures for deriver testing
  • test_queue_operations.py - Tests for basic queue operations
  • test_deriver_processing.py - Tests for deriver processing logic
  • test_queue_processing.py - Tests for queue manager and work unit processing

Key Fixtures

Database Fixtures

  • sample_session_with_peers - Creates a session with multiple peers having different observation configurations
  • sample_messages - Creates sample messages for testing
  • sample_queue_items - Creates queue items with various payload types (representation, summary)

Queue Fixtures

  • create_queue_payload - Helper to create queue payloads for testing
  • add_queue_items - Helper to add queue items to the database
  • create_active_queue_session - Helper to create active queue sessions for work unit tracking

Mocking Fixtures

  • mock_critical_analysis_call - Mocks the critical analysis LLM call
  • mock_queue_manager - Mocks the queue manager for testing
  • mock_representation_manager - Mocks the representation manager operations

Testing Patterns

Creating Queue Items

# Create representation payloads
payload = create_queue_payload(
    message=message,
    task_type="representation",
    observer=observer_peer.name,
    observed=message.peer_name
)

# Add to queue
queue_items = await add_queue_items([payload], session.id)

Testing Work Units

# Create a work unit
work_unit = WorkUnit(
    session_id=session.id,
    task_type="representation",
    observer=observer,
    observed=observed
)

# Test string representation
assert str(work_unit) == f"({session.id}, {observed.name}, {observer.name}, representation)"