AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
feat(backend/copilot): durable DreamPass record written from the sync and batch dream paths (#14961)
Nicholas Tindleb6608eebae82
diff --git a/autogpt_platform/backend/backend/api/features/admin/memory_admin_routes.py b/autogpt_platform/backend/backend/api/features/admin/memory_admin_routes.pyindex e31bb41940..0ef257c645 100644--- a/autogpt_platform/backend/backend/api/features/admin/memory_admin_routes.py+++ b/autogpt_platform/backend/backend/api/features/admin/memory_admin_routes.py@@ -32,11 +32,14 @@ from backend.copilot.dream.job_status import (write_initial_status,)from backend.copilot.dream.nightly_batch import NightlyBatchResult+from backend.copilot.dream.pass_record import dream_pass_result_from_rowfrom backend.copilot.dream.ratification import RatificationResultfrom backend.copilot.dream.schemas import DreamPassResult+from backend.copilot.dream.store import read_dream_passfrom backend.copilot.graphiti.falkordb_driver import open_driverfrom backend.copilot.graphiti.recall import live_fact_predicatefrom backend.copilot.graphiti.scope import MemoryScope+from backend.data.dream_pass_models import DreamPassRecordfrom backend.util.clients import get_scheduler_clientfrom backend.util.url_redaction import redact_url@@ -1008,6 +1011,14 @@ class CommunityRebuildJobStatus(JobStatus[RebuildResult]):"""JobStatus envelope for ``kind="rebuild"``."""+class DreamPassRecordResponse(BaseModel):+ """One dream pass's durable record, and the ``DreamPassResult`` read off+ it: what the eval driver scores, with usage for batch passes too."""++ record: DreamPassRecord+ result: DreamPassResult++@router.post("/{user_id}/dream",response_model=JobTriggerResponse,@@ -1093,6 +1104,39 @@ async def get_dream_pass_status(return DreamJobStatus.model_validate(status.model_dump())+ "/{user_id}/dream/{pass_id}/record",+ response_model=DreamPassRecordResponse,+)+async def get_dream_pass_record(+ request: Request,+ user_id: Annotated[str, Path(description="User id or 'me'")],+ pass_id: Annotated[str, Path(description="The dream pass id")],+ caller_id: Annotated[str, Depends(get_user_id)],+ jwt_payload: Annotated[dict, Security(get_jwt_payload)],+) -> DreamPassRecordResponse:+ """Read one dream pass's durable record, on either route.++ The job status above is a Redis row that lapses after six hours and, for+ a batch pass, carries no usage; the record outlives the pass and holds+ what its phases used. 404 for a pass that does not exist or belongs to+ another user, alike.+ """+ target = _resolve_user_id(user_id, caller_id)+ _audit_cross_user_access(+ request=request,+ caller_id=caller_id,+ target_id=target,+ jwt_payload=jwt_payload,+ )+ record = await read_dream_pass(pass_id, user_id=target)+ if record is None:+ raise HTTPException(status_code=404, detail="dream pass not found")+ return DreamPassRecordResponse(+ record=record, result=dream_pass_result_from_row(record)+ )++@router.post("/{user_id}/ratification", response_model=RatificationResult)async def trigger_ratification_pass(request: Request,diff --git a/autogpt_platform/backend/backend/api/features/admin/memory_admin_routes_test.py b/autogpt_platform/backend/backend/api/features/admin/memory_admin_routes_test.pyindex b640c5663a..594e3997e6 100644--- a/autogpt_platform/backend/backend/api/features/admin/memory_admin_routes_test.py+++ b/autogpt_platform/backend/backend/api/features/admin/memory_admin_routes_test.py@@ -1,5 +1,6 @@"""Tests for the admin memory inspector routes."""+from datetime import datetime, timedelta, timezonefrom unittest.mock import AsyncMock, MagicMock, patchfrom urllib.parse import quote@@ -7,11 +8,24 @@ import fastapiimport fastapi.testclientimport pytestfrom autogpt_libs.auth.jwt_utils import get_jwt_payload+from prisma.enums import (+ DreamPassPhase,+ DreamPassRoute,+ DreamPassStatus,+ DreamPassTrigger,+)from redis.exceptions import ResponseErrorfrom backend.api.features.experts.models import PROTECTED_SOUL_RULES, Expert+from backend.copilot.dream.schemas import DreamPassUsage, IngestionDrainStatusfrom backend.copilot.graphiti.client import derive_memory_group_idfrom backend.copilot.graphiti.recall import live_fact_predicate+from backend.data.dream_pass_models import (+ DreamPassApplied,+ DreamPassOperations,+ DreamPassRecord,+ DreamPhaseOutputs,+)from .memory_admin_routes import router as memory_admin_router@@ -858,6 +872,92 @@ def _fake_status_row(kind: str, user_id: str, job_id: str = "job-1"):)+def _dream_pass_row(user_id: str) -> DreamPassRecord:+ started = datetime(2026, 9, 26, 3, 0, tzinfo=timezone.utc)+ return DreamPassRecord(+ id="p1",+ user_id=user_id,+ expert_id=None,+ scope_key=user_id,+ route=DreamPassRoute.ANTHROPIC_BATCH,+ trigger=DreamPassTrigger.EVAL,+ phase=DreamPassPhase.DONE,+ status=DreamPassStatus.COMPLETE,+ skip_reason=None,+ cancel_generation=0,+ provider_batch_id="msgbatch_sanitize",+ lease_token="tok",+ lease_expires_at=started + timedelta(hours=24),+ input_bundle=None,+ phase_outputs=DreamPhaseOutputs(),+ operations=DreamPassOperations(+ applied=DreamPassApplied(+ consolidated_count=2,+ dream_session_id="s1",+ ingestion_drain_status=IngestionDrainStatus.skipped,+ )+ ),+ usage=DreamPassUsage(total_cost_usd=0.011, discount_applied=0.5),+ window_start=None,+ window_end=None,+ error=None,+ created_at=started,+ started_at=started,+ submitted_at=started,+ applied_at=started + timedelta(minutes=7),+ completed_at=started + timedelta(minutes=7),+ updated_at=started + timedelta(minutes=7),+ )+++class TestDreamPassRecord:+ """``GET /{user_id}/dream/{pass_id}/record`` reads the pass's durable row,+ and the ``DreamPassResult`` the eval driver scores, usage included."""++ def test_returns_the_row_and_the_result_read_off_it(self) -> None:+ read = AsyncMock(return_value=_dream_pass_row("abc"))+ with patch(f"{_MOCK_MODULE}.read_dream_pass", new=read):+ resp = client.get("/admin/memory/abc/dream/p1/record")++ assert resp.status_code == 200+ read.assert_awaited_once_with("p1", user_id="abc")+ body = resp.json()+ assert body["record"]["id"] == "p1"+ assert body["record"]["status"] == "COMPLETE"+ assert body["record"]["trigger"] == "EVAL"+ result = body["result"]+ assert result["execution_path"] == "anthropic_batch"+ assert result["usage"]["total_cost_usd"] == 0.011+ assert result["consolidated_count"] == 2+ assert result["elapsed_seconds"] == 420.0+ assert result["skipped"] is False++ def test_me_reads_the_callers_own_pass(self, mock_jwt_admin) -> None:+ read = AsyncMock(return_value=_dream_pass_row(mock_jwt_admin["user_id"]))+ with patch(f"{_MOCK_MODULE}.read_dream_pass", new=read):+ resp = client.get("/admin/memory/me/dream/p1/record")++ assert resp.status_code == 200+ read.assert_awaited_once_with("p1", user_id=mock_jwt_admin["user_id"])++ def test_a_missing_or_another_users_pass_is_404(self) -> None:+ # The read is owner-scoped: another user's pass comes back as None,+ # exactly like a pass that does not exist.+ with patch(f"{_MOCK_MODULE}.read_dream_pass", new=AsyncMock(return_value=None)):+ resp = client.get("/admin/memory/abc/dream/p1/record")++ assert resp.status_code == 404++ def test_non_admin_gets_403_before_the_read(self, mock_jwt_user) -> None:+ app.dependency_overrides[get_jwt_payload] = mock_jwt_user["get_jwt_payload"]+ read = AsyncMock()+ with patch(f"{_MOCK_MODULE}.read_dream_pass", new=read):+ resp = client.get("/admin/memory/abc/dream/p1/record")++ assert resp.status_code == 403+ read.assert_not_awaited()++class TestAdminGating:"""Non-admin callers must get 403 on every route."""diff --git a/autogpt_platform/backend/backend/copilot/dream/batch_callbacks.py b/autogpt_platform/backend/backend/copilot/dream/batch_callbacks.pyindex 99a72c61db..6ab45831d1 100644--- a/autogpt_platform/backend/backend/copilot/dream/batch_callbacks.py+++ b/autogpt_platform/backend/backend/copilot/dream/batch_callbacks.py@@ -16,21 +16,11 @@ result lands. The orchestrator kicks off phase 1; this handler chainsphase 2 from phase 1's result, phase 3 from phase 2's result, thenruns the apply step + cost log + JobStatus complete when phase 3 lands.-Per-pass state lives in two Redis keys:-- * ``dream:batch:input:{pass_id}`` — the serialized ``DreamInput``- (so we can rebuild each phase's prompt without re-fetching from- Postgres / FalkorDB) plus the dream lock's ownership token for the- compare-and-delete release- * ``dream:batch:state:{pass_id}`` — accumulated phase outputs +- per-phase token usage so the apply step has everything it needs- and the cost log can record all three rows at once--Both are TTL'd to 24h (Anthropic's batch SLA) so a forgotten pass-naturally falls off the radar. Two SETNX gates (7-day TTL) keep the-side effects at-most-once across batch re-dispatch:-``dream:applied:{pass_id}`` for the memory writes and-``dream:batch:costs_logged:{pass_id}`` for billing.+The pass's Redis state and its at-most-once gates are ``batch_state.py``;+how the pass ends (JobStatus, the durable ``DreamPass`` record, the lock)+is ``batch_outcome.py``; what its phases cost and used is ``batch_costs.py``. Each+callback also advances the record: the phase that landed and its output,+the next batch, the apply, and the end."""from __future__ import annotations@@ -38,37 +28,41 @@ from __future__ import annotationsimport jsonimport loggingfrom contextlib import ExitStack-from typing import TYPE_CHECKING, Any, Literal+from typing import TYPE_CHECKING, Any-from pydantic import ValidationError+from pydantic import BaseModel, ValidationErrorfrom backend.copilot.graphiti.scope import MemoryScope-from backend.copilot.inference.context import (- InferenceContext,- InferenceError,- InferenceScope,- InferenceUsage,-)-from backend.copilot.inference.routing import anthropic_batch_route+from backend.copilot.inference.context import InferenceErrorfrom backend.copilot.trial_cost_context import TrialCostContext, restore_cost_contextfrom backend.copilot.usage_activation import UsageActivationUnavailable-from .batch_submit import (- PHASE_RESPONSE_MODELS,- delete_input_bundle,- read_input_bundle,- read_lock_token,- submit_phase,+from .batch_costs import landed_usage, log_all_phase_costs, recorded_usage+from .batch_outcome import (+ ApplyStats,+ BatchPass,+ fail_pass,+ finalize_stuck_duplicate,+ mark_job_errored,+ record_completion,+ release_lock,+)+from .batch_state import (+ best_effort_cleanup,+ claim_apply_gate,+ content_for,+ read_state,+ write_phase_to_state,)-from .billing import record_phase_cost+from .batch_submit import PHASE_RESPONSE_MODELS, read_input_bundle, submit_phase+from .clamp import clamp_operationsfrom .llm import parse_json_with_prose_fallback-from .locks import release_dream_lock-from .phase_jobs import PHASE_TIERS, phase_job-from .schemas import (- DreamOperations,- DreamOperationsSnapshot,- DreamPhase,- IngestionDrainStatus,+from .schemas import DreamOperations, DreamPhase, IngestionDrainStatus+from .store import (+ record_applying,+ record_batch_failed,+ record_next_batch,+ record_phase_output,)if TYPE_CHECKING:@@ -81,6 +75,8 @@ logger = logging.getLogger(__name__)NAMESPACE = "dream_pass"+# A pass whose batch carries no usage attribution it can be charged under.+UNATTRIBUTED_ERROR = "batch usage attribution unavailable"NEXT_PHASE: dict[DreamPhase, DreamPhase | None] = {"consolidate": "recombine",@@ -89,209 +85,6 @@ NEXT_PHASE: dict[DreamPhase, DreamPhase | None] = {}-# ----------------------------------------------------------------------------# State persistence-# ------------------------------------------------------------------------------def _state_key(pass_id: str) -> str:- return f"dream:batch:state:{pass_id}"---# 24h matches Anthropic's batch SLA; if no phase has landed in that-# window the BatchExecutor has already issued a timeout error via-# ``MAX_BATCH_LIFETIME_SECONDS``.-STATE_TTL_SECONDS = 24 * 60 * 60---async def _read_state(pass_id: str) -> dict[str, dict[str, Any]]:- """Per-pass accumulator: phase → {content, usage tokens, error}.-- Returns a ``dict[str, ...]`` rather than ``dict[DreamPhase, ...]``- because Redis hash keys are plain strings and we lose the- ``Literal`` narrowing the moment we read them back. Callers- re-narrow at the boundary (e.g. via ``NEXT_PHASE`` lookups) when- they need the phase ordering.- """- from backend.data.redis_client import get_redis_async-- redis = await get_redis_async()- raw = await redis.hgetall(_state_key(pass_id)) # type: ignore[misc]- out: dict[str, dict[str, Any]] = {}- for phase, body in (raw or {}).items():- if isinstance(phase, bytes):- phase = phase.decode("utf-8")- if isinstance(body, bytes):- body = body.decode("utf-8")- try:- out[phase] = json.loads(body)- except Exception:- logger.warning("Corrupted state row for pass=%s phase=%s", pass_id, phase)- return out---async def _write_phase_to_state(- *, pass_id: str, phase: DreamPhase, row: BatchResultRow-) -> None:- from backend.data.redis_client import get_redis_async-- redis = await get_redis_async()- body = json.dumps(- {- "custom_id": row.custom_id,- "content": row.content,- "input_tokens": row.input_tokens,- "output_tokens": row.output_tokens,- "cache_read_tokens": row.cache_read_tokens,- "cache_creation_tokens": row.cache_creation_tokens,- "error": row.error,- }- )- await redis.hset(_state_key(pass_id), phase, body) # type: ignore[misc]- await redis.expire(_state_key(pass_id), STATE_TTL_SECONDS)---async def _delete_state(pass_id: str) -> None:- from backend.data.redis_client import get_redis_async-- redis = await get_redis_async()- await redis.delete(_state_key(pass_id))---def _phase_models_from_payload(payload: dict[str, Any]) -> dict[str, str]:- """Per-phase model map persisted by ``submit_phase`` — used to chain- the next phase and to price each phase with the model it actually- used. Empty when absent (current code never omits it)."""- raw = payload.get("phase_models")- if not isinstance(raw, dict):- return {}- return {str(k): str(v) for k, v in raw.items()}---async def _mark_job_errored_best_effort(job_id: str, error: str) -> None:- """Close the admin job row on a dead-end the normal fail path can't- reach (malformed payload). Best-effort: status write failures are- logged, never raised."""- if not job_id:- return- try:- from .job_status import mark_errored-- await mark_errored(kind="dream_pass", job_id=job_id, error=error)- except Exception:- logger.exception("Failed to mark dead-end job %s errored", job_id[:12])---async def _finalize_stuck_duplicate(- *, user_id: str, pass_id: str, job_id: str, ops: DreamOperations-) -> None:- """On a duplicate delivery, finalize the job row iff the first- delivery crashed between apply and ``mark_complete`` and left it- non-terminal. An already-terminal row is left untouched so the- first delivery's real apply stats are never overwritten.-- Best-effort: a status read/write failure here must not crash the- duplicate tail (lock release + cleanup still need to run)."""- if not job_id:- return- try:- from .job_status import mark_complete, read_status- from .schemas import DreamPassResult-- existing = await read_status(kind="dream_pass", job_id=job_id)- if existing is None or existing.state in ("complete", "errored"):- return- logger.warning(- "Duplicate dispatch found job %s stuck in state=%s — "- "finalizing with the clamped op counts",- job_id[:12],- existing.state,- )- # The first delivery's per-edge outcomes (and dream session id)- # died with it: the writes and proposals it queued are counted- # pending, none written, and the demotion counts are the clamped- # *attempted* ops — annotate the summary so the admin UI doesn't- # present them as confirmed apply results.- note = (- "[finalized after duplicate delivery — writes and proposals were "- "queued by the original delivery, counted pending; demotion counts "- "reflect attempted operations] "- )- await mark_complete(- kind="dream_pass",- job_id=job_id,- result=DreamPassResult(- user_id=user_id,- pass_id=pass_id,- execution_path="anthropic_batch",- pending_writes=len(ops.writes) + len(ops.proposals),- demotion_count=len(ops.demotions),- entity_invalidation_count=len(ops.entity_invalidations),- # Batch apply never drains in-line by design; the first- # delivery's writes (if any) landed fire-and-forget. ``skipped``- # marks this as a healthy by-design skip, NOT a drain failure.- ingestion_drain_status=IngestionDrainStatus.skipped,- summary_for_user=note + (ops.summary_for_user or ""),- ),- )- except Exception:- logger.exception("Failed to finalize stuck duplicate for job %s", job_id[:12])---async def _best_effort_cleanup(pass_id: str) -> None:- """Delete the per-pass state + input bundle without letting a Redis- blip propagate. These deletes run AFTER ``mark_complete`` on the- success/duplicate tails — an exception here would route through the- crash guard to ``_fail_pass`` and rewrite a completed job to errored.- Both keys carry 24h TTLs, so a failed delete self-heals."""- try:- await _delete_state(pass_id)- await delete_input_bundle(pass_id)- except Exception:- logger.exception(- "Per-pass cleanup failed for pass=%s — keys will expire via TTL",- pass_id,- )---async def _release_lock(user_id: str, pass_id: str, expert_id: str | None) -> None:- """Release the disowned dream lock with the ownership token persisted- alongside the input bundle. Must run before ``delete_input_bundle`` —- the token rides on that key. A missing token (bundle TTL'd out,- malformed payload) leaves the lock for its TTL to clear rather than- blind-deleting what may be a newer pass's lock.-- Best-effort like ``release_dream_lock`` itself: a Redis blip on the- token read must not propagate — on the success tail it would fire- AFTER ``mark_complete`` and the crash guard would rewrite a completed- job to errored. Falls back to a token-less release (lock TTL)."""- token: str | None = None- if pass_id:- try:- token = await read_lock_token(pass_id)- except Exception:- logger.exception(- "Failed to read dream lock token for pass=%s — "- "leaving the lock to its TTL",- pass_id,- )- try:- scope = MemoryScope.build(user_id, expert_id)- except ValueError:- logger.warning(- "Invalid memory scope for disowned dream lock of user %s — "- "leaving it for the TTL to clear",- user_id[:12],- )- return- await release_dream_lock(scope, token)---# ----------------------------------------------------------------------------# Handler entry point-# -----------------------------------------------------------------------------async def handle_dream_batch_result(entry: PendingEntry, rows: list[BatchResultRow]) -> None:@@ -318,20 +111,27 @@ async def handle_dream_batch_result(async def _fail_unattributed_pass(payload: dict[str, Any]) -> None:- """Close a dead-end batch without charging against a new usage context."""- pass_id = str(payload["pass_id"])- await _mark_job_errored_best_effort(- str(payload.get("job_id") or ""), "batch usage attribution unavailable"- )+ """Close a pass whose batch result carries no usage attribution it can+ be charged under (#15208): errored like any failed pass (its admin job,+ its record with what the landed phases used, the disowned lock, its+ state), but nothing is charged against another usage context. The lock+ is the pass's own scope's, read off its input bundle as the phase+ handlers read it; without the bundle there is no token to release it+ with, and it is left to its TTL."""+ bp = BatchPass.from_payload(payload)try:- input_bundle = await read_input_bundle(pass_id)- if input_bundle is not None:- await _release_lock(input_bundle.user_id, pass_id, input_bundle.expert_id)+ input_bundle = await read_input_bundle(bp.pass_id)except Exception:- logger.exception(- "Unattributed dream batch lock cleanup failed for pass=%s", pass_id+ logger.exception("Unattributed dream batch pass=%s: no bundle", bp.pass_id)+ input_bundle = None+ if input_bundle is not None:+ bp = bp.model_copy(+ update={+ "user_id": input_bundle.user_id,+ "expert_id": input_bundle.expert_id,+ })- await _best_effort_cleanup(pass_id)+ await fail_pass(bp, UNATTRIBUTED_ERROR, charge=False)async def _handle_dream_batch_result(@@ -350,164 +150,127 @@ async def _handle_dream_batch_result(three phases, mark JobStatus complete, clean up"""payload = entry.payload or {}- user_id = str(payload.get("user_id") or "")- expert_id_raw = payload.get("expert_id")- expert_id = str(expert_id_raw) if expert_id_raw is not None else None- pass_id = str(payload.get("pass_id") or "")- job_id = str(payload.get("job_id") or "")- phase_models = _phase_models_from_payload(payload)+ bp = BatchPass.from_payload(payload)phase = payload.get("phase")-- if not user_id or not pass_id or not phase:+ if not bp.user_id or not bp.pass_id or not phase:logger.warning("Dream batch handler missing user_id/pass_id/phase — payload=%s",payload,)- # Dead-end payload: close the admin job row (it would otherwise sit- # queued/submitted until its TTL) and release the disowned lock so- # the user isn't locked out until the 24h TTL.- await _mark_job_errored_best_effort(- job_id, "batch payload missing user_id/pass_id/phase"- )- if user_id:- await _release_lock(user_id, pass_id, expert_id)+ await _dead_end(bp, "batch payload missing user_id/pass_id/phase")return-if phase not in NEXT_PHASE:logger.warning("Dream batch handler unknown phase=%r", phase)- await _mark_job_errored_best_effort(job_id, f"unknown batch phase {phase!r}")- await _release_lock(user_id, pass_id, expert_id)+ await _dead_end(bp, f"unknown batch phase {phase!r}")return+ await _handle_guarded(bp, phase, rows)- authoritative_user_id = user_id- authoritative_expert_id = expert_id++async def _dead_end(bp: BatchPass, error: str) -> None:+ """A payload no phase handler can take. Close the admin job row (it+ would otherwise sit queued/submitted until its TTL) and, when the pass+ is known, its record with what its landed phases used; then release+ the disowned lock so the user isn't locked out until the 24h TTL."""+ await mark_job_errored(bp.job_id, error, dead_end=True)+ if bp.pass_id:+ usage = await recorded_usage(bp.pass_id, bp.phase_models)+ await record_batch_failed(bp.pass_id, error, usage)+ if bp.user_id:+ await release_lock(bp)+++async def _handle_guarded(+ bp: BatchPass, phase: DreamPhase, rows: list[BatchResultRow]+) -> None:+ """Check the payload's scope against the pass's own, then handle the+ phase, all inside one crash guard.++ The batch path disowned the per-user dream lock to this callback, and+ BatchExecutor._dispatch swallows handler exceptions — so without this+ guard an unexpected error here would strand the user behind the+ disowned lock until its extended TTL expired and leave the admin+ JobStatus row stuck. The crash goes through ``fail_pass`` (releases the+ lock + marks the job errored); if even that fails, the lock is released+ directly so the user is never blocked on a leaked lock.+ """+ scoped = bptry:- input_bundle = await read_input_bundle(pass_id)+ input_bundle = await read_input_bundle(bp.pass_id)if input_bundle is None:logger.error("Dream batch input missing; refusing payload-only scope for pass=%s",- pass_id,- )- await _fail_pass(- user_id=user_id,- expert_id=expert_id,- pass_id=pass_id,- job_id=job_id,- phase_models=phase_models,- error="batch DreamInput missing; memory scope unavailable",+ bp.pass_id,)+ await fail_pass(bp, "batch DreamInput missing; memory scope unavailable")return-- authoritative_user_id = input_bundle.user_id- authoritative_expert_id = input_bundle.expert_id- if user_id != authoritative_user_id or expert_id != authoritative_expert_id:+ scoped = bp.model_copy(+ update={+ "user_id": input_bundle.user_id,+ "expert_id": input_bundle.expert_id,+ }+ )+ if scoped != bp:logger.error(- "Dream batch payload memory scope mismatch for pass=%s",- pass_id,- )- await _fail_pass(- user_id=authoritative_user_id,- expert_id=authoritative_expert_id,- pass_id=pass_id,- job_id=job_id,- phase_models=phase_models,- error="batch payload memory scope mismatch",+ "Dream batch payload memory scope mismatch for pass=%s", bp.pass_id)+ await fail_pass(scoped, "batch payload memory scope mismatch")return-- await _handle_phase_result(- rows=rows,- user_id=user_id,- expert_id=expert_id,- input_bundle=input_bundle,- pass_id=pass_id,- job_id=job_id,- phase_models=phase_models,- phase=phase,- )+ await _handle_phase_result(bp, input_bundle, phase, rows)except Exception:- # The batch path disowned the per-user dream lock to this callback,- # and BatchExecutor._dispatch swallows handler exceptions — so without- # this guard an unexpected error here would strand the user behind the- # disowned lock until its extended TTL expired and leave the admin- # JobStatus row stuck. Route the crash through _fail_pass (releases the- # lock + marks the job errored); if even that fails, release the lock- # directly so the user is never blocked on a leaked lock.logger.exception(- "Dream batch handler crashed for pass=%s phase=%s", pass_id, phase+ "Dream batch handler crashed for pass=%s phase=%s", bp.pass_id, phase)+ await _fail_after_crash(scoped, f"{phase}: handler crashed")+++async def _fail_after_crash(bp: BatchPass, error: str) -> None:+ try:+ await fail_pass(bp, error)+ except Exception:+ logger.exception("Dream batch _fail_pass also failed for pass=%s", bp.pass_id)try:- await _fail_pass(- user_id=authoritative_user_id,- expert_id=authoritative_expert_id,- pass_id=pass_id,- job_id=job_id,- phase_models=phase_models,- error=f"{phase}: handler crashed",- )+ await release_lock(bp)except Exception:- logger.exception("Dream batch _fail_pass also failed for pass=%s", pass_id)- try:- await _release_lock(- authoritative_user_id,- pass_id,- authoritative_expert_id,- )- except Exception:- logger.exception(- "Dream batch lock release failed for user=%s",- authoritative_user_id[:12],- )+ logger.exception(+ "Dream batch lock release failed for user=%s", bp.user_id[:12]+ )async def _handle_phase_result(- *,- rows: list[BatchResultRow],- user_id: str,- expert_id: str | None,+ bp: BatchPass,input_bundle: DreamInput,- pass_id: str,- job_id: str,- phase_models: dict[str, str],phase: DreamPhase,+ rows: list[BatchResultRow],) -> None:"""Validate one finished phase batch, then chain to the next phase or- finalize.-- Split out from ``handle_dream_batch_result`` so the latter can wrap- this in a single crash guard that always releases the disowned dream- lock — every early-return below already finalizes via ``_fail_pass``,- but an *unexpected* raise (Redis blip, apply bug) must not leak the- lock either.- """+ finalize. Every early return below finalizes via ``fail_pass``; an+ unexpected raise (Redis blip, apply bug) is the crash guard's."""if not rows:- logger.warning("Dream batch handler got empty rows for pass=%s", pass_id)- await _fail_pass(- user_id=user_id,- expert_id=expert_id,- pass_id=pass_id,- job_id=job_id,- phase_models=phase_models,- error=f"{phase}: provider returned no rows",- )+ logger.warning("Dream batch handler got empty rows for pass=%s", bp.pass_id)+ await fail_pass(bp, f"{phase}: provider returned no rows")return-# Single-request-per-batch today; the first (and only) row is the# phase result. When we group batches in the future the BatchExecutor# will already split by custom_id before calling us.- row = rows[0]- if row.error:- await _write_phase_to_state(pass_id=pass_id, phase=phase, row=row)- await _fail_pass(- user_id=user_id,- expert_id=expert_id,- pass_id=pass_id,- job_id=job_id,- phase_models=phase_models,- error=f"{phase}: {row.error}",- )+ if await _landed_output(bp, phase, rows[0]) is None:+ return+ next_phase = NEXT_PHASE[phase]+ if next_phase is not None:+ await _chain_next_phase(bp, input_bundle, next_phase)return+ # Terminal phase landed — apply + finalize.+ await _finalize_complete(bp, input_bundle)++async def _landed_output(+ bp: BatchPass, phase: DreamPhase, row: BatchResultRow+) -> BaseModel | None:+ """The phase's validated output, kept in the pass's Redis state and its+ record; ``None`` once an errored or malformed row has failed the pass."""+ if row.error:+ await write_phase_to_state(pass_id=bp.pass_id, phase=phase, row=row)+ await fail_pass(bp, f"{phase}: {row.error}")+ return None# Validate the row's content matches the phase's Pydantic schema# BEFORE persisting — corrupted content shouldn't pollute the# accumulator for the next phase to read back. Parsed the way the sync@@ -516,62 +279,21 @@ async def _handle_phase_result(# (and read back by the next phase) is the JSON alone.try:payload = parse_json_with_prose_fallback(row.content)- PHASE_RESPONSE_MODELS[phase].model_validate(payload)+ output = PHASE_RESPONSE_MODELS[phase].model_validate(payload)except (InferenceError, ValidationError) as exc:row.error = f"invalid output shape — {type(exc).__name__}"- await _write_phase_to_state(pass_id=pass_id, phase=phase, row=row)- await _fail_pass(- user_id=user_id,- expert_id=expert_id,- pass_id=pass_id,- job_id=job_id,- phase_models=phase_models,- error=f"{phase}: {row.error}",- )- return-- await _write_phase_to_state(- pass_id=pass_id, phase=phase, row=row.with_content(json.dumps(payload))- )-- next_phase = NEXT_PHASE[phase]- if next_phase is not None:- await _chain_next_phase(- user_id=user_id,- expert_id=expert_id,- input_bundle=input_bundle,- pass_id=pass_id,- job_id=job_id,- phase_models=phase_models,- next_phase=next_phase,- )- return-- # Terminal phase landed — apply + finalize.- await _finalize_complete(- user_id=user_id,- expert_id=expert_id,- input_bundle=input_bundle,- pass_id=pass_id,- job_id=job_id,- phase_models=phase_models,+ await write_phase_to_state(pass_id=bp.pass_id, phase=phase, row=row)+ await fail_pass(bp, f"{phase}: invalid output shape — {type(exc).__name__}")+ return None+ await write_phase_to_state(+ pass_id=bp.pass_id, phase=phase, row=row.with_content(json.dumps(payload)))---# ----------------------------------------------------------------------------# Phase chaining-# ---------------------------------------------------------------------------+ await record_phase_output(bp.pass_id, phase, output)+ return outputasync def _chain_next_phase(- *,- user_id: str,- expert_id: str | None,- input_bundle: DreamInput,- pass_id: str,- job_id: str,- phase_models: dict[str, str],- next_phase: DreamPhase,+ bp: BatchPass, input_bundle: DreamInput, next_phase: DreamPhase) -> None:"""Submit the next phase in the chain.@@ -580,133 +302,60 @@ async def _chain_next_phase(batch submission. On any failure to submit, marks the JobStatuserrored — silent submission failures are unrecoverable."""- state = await _read_state(pass_id)- consolidated_json = _content_for(state, "consolidate")- recombined_json = _content_for(state, "recombine")-+ state = await read_state(bp.pass_id)api_key = _anthropic_api_key()if api_key is None:- await _fail_pass(- user_id=user_id,- expert_id=expert_id,- pass_id=pass_id,- job_id=job_id,- phase_models=phase_models,- error=f"{next_phase}: no Anthropic API key configured",- )+ await fail_pass(bp, f"{next_phase}: no Anthropic API key configured")return-try:submission = await submit_phase(- user_id=user_id,- pass_id=pass_id,- job_id=job_id,+ user_id=bp.user_id,+ pass_id=bp.pass_id,+ job_id=bp.job_id,phase=next_phase,- phase_models=phase_models,+ phase_models=bp.phase_models,api_key=api_key,input_bundle=input_bundle,- consolidated_json=consolidated_json,- recombined_json=recombined_json,+ consolidated_json=content_for(state, "consolidate"),+ recombined_json=content_for(state, "recombine"),)except Exception as exc:logger.exception("Failed to submit %s phase for pass=%s — marking errored",next_phase,- pass_id,- )- await _fail_pass(- user_id=user_id,- expert_id=expert_id,- pass_id=pass_id,- job_id=job_id,- phase_models=phase_models,- error=f"{next_phase}: submit failed: {type(exc).__name__}: {exc}",+ bp.pass_id,)+ await fail_pass(bp, f"{next_phase}: submit failed: {type(exc).__name__}: {exc}")return+ await _advance_job(bp, next_phase, submission.provider_batch_id)+ await record_next_batch(bp.pass_id, next_phase, submission.provider_batch_id)- if job_id:- try:- from .job_status import update_status_phase-- await update_status_phase(- kind="dream_pass",- job_id=job_id,- state="submitted",- current_phase=next_phase,- batch_id=submission.provider_batch_id,- )- except Exception:- logger.exception(- "Failed to update status for next phase=%s pass=%s",- next_phase,- pass_id,- )---def _content_for(state: dict[str, dict[str, Any]], phase: str) -> str | None:- row = state.get(phase)- if row is None:- return None- content = row.get("content")- return content if isinstance(content, str) else None---# ----------------------------------------------------------------------------# Terminal handlers-# ------------------------------------------------------------------------------_APPLIED_GATE_PREFIX = "dream:applied"-# 7 days — same window as the costs_logged gate; no realistic-# BatchExecutor re-dispatch (poll backoff caps at 5 min, max lifetime-# 24h) can outlive it.-_APPLIED_GATE_TTL_SECONDS = 7 * 24 * 60 * 60---async def _claim_apply_gate(pass_id: str) -> Literal["claimed", "duplicate", "error"]:- """Atomically claim the per-pass apply gate.-- Returns ``"claimed"`` when this delivery is the first to run- ``apply_operations`` for the pass, ``"duplicate"`` on a re-dispatched- delivery whose writes already landed, and ``"error"`` when Redis is- unavailable and we cannot tell which of the two we are.-- Mirrors ``_claim_costs_logged_gate``: if the BatchExecutor crashes- between dispatch and ``remove_pending``, the next poll re-dispatches- the same finished batch — and ``apply_operations`` writes every- consolidated fact and proposal to the user's graph as fresh episodes,- so re-running it duplicates the user's memories. The three states must- stay distinct: treating a Redis brown-out as a duplicate would mark- the job complete with zero writes, silently dropping the dream.- """- from backend.data.redis_client import get_redis_async+async def _advance_job(+ bp: BatchPass, next_phase: DreamPhase, provider_batch_id: str+) -> None:+ """Point the admin job row at the next phase's batch; best-effort."""+ if not bp.job_id:+ returntry:- redis = await get_redis_async()- claimed = await redis.set(- f"{_APPLIED_GATE_PREFIX}:{pass_id}",- "1",- nx=True,- ex=_APPLIED_GATE_TTL_SECONDS,+ from .job_status import update_status_phase++ await update_status_phase(+ kind="dream_pass",+ job_id=bp.job_id,+ state="submitted",+ current_phase=next_phase,+ batch_id=provider_batch_id,)- return "claimed" if claimed else "duplicate"except Exception:logger.exception(- "Failed to claim apply gate for pass=%s — failing pass",- pass_id,+ "Failed to update status for next phase=%s pass=%s",+ next_phase,+ bp.pass_id,)- return "error"-async def _finalize_complete(- *,- user_id: str,- expert_id: str | None,- input_bundle: DreamInput,- pass_id: str,- job_id: str,- phase_models: dict[str, str],-) -> None:+async def _finalize_complete(bp: BatchPass, input_bundle: DreamInput) -> None:"""Sanitize phase has landed. Run apply + cost log + complete."""try:from .apply import (@@ -714,115 +363,23 @@ async def _finalize_complete(apply_operations,drain_status_from_stats,)- from .orchestrator import _clamp_operationsexcept Exception:- logger.exception("Failed to import dream apply for pass=%s", pass_id)- await _fail_pass(- user_id=user_id,- expert_id=expert_id,- pass_id=pass_id,- job_id=job_id,- phase_models=phase_models,- error="apply: import failed",- )- return-- state = await _read_state(pass_id)- sanitize_row = state.get("sanitize")- if sanitize_row is None or not sanitize_row.get("content"):- await _fail_pass(- user_id=user_id,- expert_id=expert_id,- pass_id=pass_id,- job_id=job_id,- phase_models=phase_models,- error="sanitize: missing terminal phase content",- )- return-- try:- ops = DreamOperations.model_validate(json.loads(sanitize_row["content"]))- except (json.JSONDecodeError, ValidationError) as exc:- await _fail_pass(- user_id=user_id,- expert_id=expert_id,- pass_id=pass_id,- job_id=job_id,- phase_models=phase_models,- error=f"sanitize: shape validation failed: {type(exc).__name__}",- )- return-- # Enforce the same per-pass operation caps the sync path applies- # before writing — the model can over-emit past the prompt's limits.- # The 5%-of-active-facts demotion ceiling needs the original fact count.- active_fact_count = len(input_bundle.facts)- # Pass the known-fact allowlist so hallucinated demotion uuids are- # filtered BEFORE the cap slice — otherwise they consume cap slots- # and displace valid demotions (cap can floor at 1 on small graphs).- ops = _clamp_operations(- ops,- active_fact_count,- known_fact_uuids=input_bundle.known_fact_uuids,- )-- # Batch results can re-dispatch (executor crash between dispatch and- # ``remove_pending``). Billing below is gated; the memory mutation must- # be too. A duplicate delivery skips apply AND mark_complete — the first- # delivery already wrote the real stats, and overwriting them with empty- # ones would zero the admin-visible counts. A gate error fails the pass:- # we cannot tell first-vs-duplicate apart, and "complete with no writes"- # would silently drop the dream.- gate = await _claim_apply_gate(pass_id)- if gate == "error":- await _fail_pass(- user_id=user_id,- expert_id=expert_id,- pass_id=pass_id,- job_id=job_id,- phase_models=phase_models,- error="apply: gate unavailable (redis) — cannot guarantee at-most-once",- )+ logger.exception("Failed to import dream apply for pass=%s", bp.pass_id)+ await fail_pass(bp, "apply: import failed")return- if gate == "duplicate":- logger.info(- "Duplicate dispatch for pass=%s — operations already applied; "- "preserving the first delivery's job result",- pass_id,- )- # Normally the first delivery wrote the terminal status. If it- # crashed between apply and mark_complete, the row is stuck in- # 'submitted' — finalize it here WITHOUT clobbering an existing- # terminal result (the apply counts are this pass's clamped ops;- # the writes themselves landed with the first delivery).- await _finalize_stuck_duplicate(- user_id=user_id, pass_id=pass_id, job_id=job_id, ops=ops- )- await _log_all_phase_costs(- user_id=user_id,- expert_id=expert_id,- pass_id=pass_id,- state=state,- phase_models=phase_models,- )- await _release_lock(user_id, pass_id, expert_id)- await _best_effort_cleanup(pass_id)+ state = await read_state(bp.pass_id)+ ops = await _terminal_ops(bp, state, input_bundle)+ if ops is None or not await _claim_apply(bp, state, ops):return-- apply_stats: dict[- str, int | str | IngestionDrainStatus | DreamOperationsSnapshot- ] = {}try:- # Thread the demotion allowlist from the already-validated bundle.- ## Skip the ingestion drain on this path: apply runs inside this# handler, which BatchExecutor.walk_once awaits serially in its single# poll loop — a 300s in-line drain would stall the poll/dispatch of# every other user's pending batch. See# ``BATCH_INGESTION_DRAIN_TIMEOUT_SECONDS``.apply_stats = await apply_operations(- MemoryScope.build(user_id, expert_id),- pass_id,+ MemoryScope.build(bp.user_id, bp.expert_id),+ bp.pass_id,ops,known_fact_uuids=input_bundle.known_fact_uuids,known_episode_uuids=input_bundle.known_episode_uuids,@@ -831,263 +388,128 @@ async def _finalize_complete(except Exception as exc:logger.exception("apply_operations crashed for batch pass=%s — marking errored",- pass_id,- )- await _fail_pass(- user_id=user_id,- expert_id=expert_id,- pass_id=pass_id,- job_id=job_id,- phase_models=phase_models,- error=f"apply: {type(exc).__name__}: {exc}",+ bp.pass_id,)+ await fail_pass(bp, f"apply: {type(exc).__name__}: {exc}")return-- # Per-phase usage log on the success path. Failure paths record the- # same usage via ``_fail_pass`` (we incurred those provider tokens- # regardless); the Redis dedup gate inside ``_log_all_phase_costs``- # keeps it at-most-once across both paths and any batch re-dispatch.- await _log_all_phase_costs(- user_id=user_id,- expert_id=expert_id,- pass_id=pass_id,- state=state,- phase_models=phase_models,+ await _finish_applied(+ bp, state, apply_stats, ops, drain_status_from_stats(apply_stats))- if job_id:- try:- from .job_status import mark_complete- from .schemas import DreamPassResult-- raw_snapshot = apply_stats.get("snapshot")- snapshot: DreamOperationsSnapshot | None = None- if isinstance(raw_snapshot, DreamOperationsSnapshot):- snapshot = raw_snapshot- elif isinstance(raw_snapshot, dict):- snapshot = DreamOperationsSnapshot.model_validate(raw_snapshot)-- # ``apply_stats`` values are typed as a union that includes- # ``DreamOperationsSnapshot``; narrow each count to a plain- # ``int`` (with a 0 default) before threading it into the- # Pydantic result so pyright doesn't flag the int() cast.- def _count(key: str) -> int:- value = apply_stats.get(key)- if isinstance(value, (int, str)):- try:- return int(value)- except (TypeError, ValueError):- return 0- return 0-- raw_session_id = apply_stats.get("session_id")- session_id = raw_session_id if isinstance(raw_session_id, str) else None-- # The batch path skips the drain by design, so apply reports- # ``skipped`` whenever the pass enqueued writes (``drained`` only- # for an empty pass). Read it via the shared, fail-closed helper- # rather than re-deriving the coercion here.- ingestion_drain_status = drain_status_from_stats(apply_stats)- pass_result = DreamPassResult(- user_id=user_id,- pass_id=pass_id,- execution_path="anthropic_batch",- consolidated_count=_count("consolidated_count"),- proposal_count=_count("proposal_count"),- demotion_count=_count("demotion_count"),- entity_invalidation_count=_count("entity_invalidation_count"),- dropped_forgotten=_count("dropped_forgotten"),- pending_writes=_count("pending_writes"),- failed_writes=_count("failed_writes"),- dream_session_id=session_id,- ingestion_drain_status=ingestion_drain_status,- operations=snapshot,- # Carry the user-facing narrative like the sync path does —- # without it the Memory Visualizer renders a blank summary for- # batch-completed dreams even though the session message exists.- summary_for_user=ops.summary_for_user,- )- await mark_complete(kind="dream_pass", job_id=job_id, result=pass_result)- except Exception:- logger.exception("Failed to mark dream pass job %s complete", job_id)+async def _claim_apply(+ bp: BatchPass, state: dict[str, dict[str, Any]], ops: DreamOperations+) -> bool:+ """Record APPLYING, then claim the apply gate; ``False`` once a duplicate+ delivery or an unreadable gate has ended this one.- # The batch path disowned the dream lock to this callback; release it now- # that the pass has terminated so the next dream for this user can run.- await _release_lock(user_id, pass_id, expert_id)- await _best_effort_cleanup(pass_id)---async def _fail_pass(- *,- user_id: str,- expert_id: str | None,- pass_id: str,- job_id: str,- phase_models: dict[str, str],- error: str,-) -> None:- """Mark JobStatus errored, record usage for any phases that already- landed, then clean up per-pass state.-- We incurred the provider tokens for completed phases regardless of- whether the whole pass landed, so they're recorded against the- user's usage — matching the sync path and the documented contract in- ``dream/billing.py``. The idempotency gate inside- ``_log_all_phase_costs`` keeps this at-most-once even if the batch- re-dispatches.+ The record write comes first, so a delivery that stalls or dies on it+ has not claimed the gate and a redelivery still applies. Once the gate+ is claimed, apply follows with nothing awaited in between."""- logger.warning("Dream batch pass=%s failed: %s", pass_id, error)- if job_id:- try:- from .job_status import mark_errored-- await mark_errored(kind="dream_pass", job_id=job_id, error=error)- except Exception:- logger.exception("Failed to mark dream pass job %s errored", job_id)- state = await _read_state(pass_id)- if state:- await _log_all_phase_costs(- user_id=user_id,- expert_id=expert_id,- pass_id=pass_id,- state=state,- phase_models=phase_models,+ await record_applying(bp.pass_id, ops)+ gate = await claim_apply_gate(bp.pass_id)+ if gate == "error":+ # We cannot tell first-vs-duplicate apart, and "complete with no+ # writes" would silently drop the dream.+ await fail_pass(+ bp, "apply: gate unavailable (redis) — cannot guarantee at-most-once")- # Release the dream lock the batch path disowned to this callback.- await _release_lock(user_id, pass_id, expert_id)- await _best_effort_cleanup(pass_id)---# ----------------------------------------------------------------------------# Cost logging-# ------------------------------------------------------------------------------_COSTS_LOGGED_PREFIX = "dream:batch:costs_logged"-# 7 days — long enough that no realistic BatchExecutor re-dispatch-# (poll backoff caps at 5 min, max lifetime 24h) can slip through and-# bill twice. Matches the spirit of the Stripe-reconcile gate's TTL.-_COSTS_LOGGED_TTL_SECONDS = 7 * 24 * 60 * 60---async def _claim_costs_logged_gate(pass_id: str) -> bool:- """Atomically claim the per-pass cost-charge gate. Returns True- when this caller won the race (first time costs_logged is set);- False when a prior caller already charged this pass.+ return False+ if gate == "duplicate":+ await _finish_duplicate(bp, state, ops)+ return False+ return True- Modelled on ``rate_limit._maybe_reconcile_stripe_tier`` — Redis- SETNX with a long TTL is the established convention for "do this- side-effect at most once per identifier" in this codebase. The- dedup lives at the dream-batch boundary, not inside- ``record_cost_usage`` itself (chat legitimately charges every turn).- """- from backend.data.redis_client import get_redis_async+async def _terminal_ops(+ bp: BatchPass, state: dict[str, dict[str, Any]], input_bundle: DreamInput+) -> DreamOperations | None:+ """The sanitize phase's operations, clamped; ``None`` once a missing or+ malformed result has failed the pass."""+ sanitize_row = state.get("sanitize")+ if sanitize_row is None or not sanitize_row.get("content"):+ await fail_pass(bp, "sanitize: missing terminal phase content")+ return Nonetry:- redis = await get_redis_async()- return bool(- await redis.set(- f"{_COSTS_LOGGED_PREFIX}:{pass_id}",- "1",- nx=True,- ex=_COSTS_LOGGED_TTL_SECONDS,- )- )- except Exception:- # Fail closed: if we can't claim the gate, do not charge.- # Better to under-bill on a Redis brown-out than risk- # double-billing under retry pressure.- logger.exception(- "Failed to claim costs_logged gate for pass=%s — skipping charge",- pass_id,- )- return False+ ops = DreamOperations.model_validate(json.loads(sanitize_row["content"]))+ except (json.JSONDecodeError, ValidationError) as exc:+ await fail_pass(bp, f"sanitize: shape validation failed: {type(exc).__name__}")+ return None+ # Enforce the same per-pass operation caps the sync path applies+ # before writing — the model can over-emit past the prompt's limits.+ # The 5%-of-active-facts demotion ceiling needs the original fact+ # count, and the known-fact allowlist filters hallucinated demotion+ # uuids BEFORE the cap slice (else they displace valid demotions).+ return clamp_operations(+ ops,+ len(input_bundle.facts),+ known_fact_uuids=input_bundle.known_fact_uuids,+ )-async def _log_all_phase_costs(- *,- user_id: str,- expert_id: str | None,- pass_id: str,- state: dict[str, dict[str, Any]],- phase_models: dict[str, str],+async def _finish_duplicate(+ bp: BatchPass, state: dict[str, dict[str, Any]], ops: DreamOperations) -> None:- """One PlatformCostLog row per phase, on the ``anthropic_batch`` route.-- Idempotent via a Redis SETNX gate keyed on ``pass_id``: if the- BatchExecutor crashes between charging and removing the pending- batch entry and the next poll re-dispatches the same batch, the- gate prevents the second charge. The gate is set BEFORE the loop- so a partial failure mid-loop still leaves the user charged for- whatever phases landed (matches the documented "partial pass- charges for completed phases" semantic in ``dream/billing.py``).-- Each phase is recorded through ``billing.record_phase_cost`` like a- sync phase, attributed to the pass's expert, and priced from its- model's catalog price card (``backend/copilot/price_card.py``):- Anthropic's additive cache buckets, less the batch path's half-price- discount.-- No-ops on per-phase failure — apply already wrote the user-facing- memory operations; a cost-log blip shouldn't take that down.+ """A repeated delivery: an earlier one claimed the apply gate, so skip+ apply and keep the first delivery's results.++ Normally the first delivery closed the admin JobStatus row. If it died+ between apply and ``mark_complete``, the row is stuck in 'submitted' and+ is finalized here without clobbering an existing terminal result. The+ pass's DreamPass record is left as it is: this delivery cannot tell+ whether the first one's apply finished, so closing it is a reaper's job."""- if not await _claim_costs_logged_gate(pass_id):- logger.info(- "Skipping batch cost log for pass=%s — already charged",- pass_id,- )- return-- for phase in PHASE_TIERS:- row = state.get(phase)- if row is None:- continue- try:- scope = InferenceScope(user_id=user_id, expert_id=expert_id)- await _log_phase_cost(scope, pass_id, phase, row, phase_models)- except Exception:- logger.exception(- "Failed to log batch cost for pass=%s phase=%s", pass_id, phase- )+ logger.info(+ "Duplicate dispatch for pass=%s — operations already applied; "+ "preserving the first delivery's job result",+ bp.pass_id,+ )+ await finalize_stuck_duplicate(bp, ops)+ await log_all_phase_costs(+ user_id=bp.user_id,+ expert_id=bp.expert_id,+ pass_id=bp.pass_id,+ state=state,+ phase_models=bp.phase_models,+ )+ await release_lock(bp)+ await best_effort_cleanup(bp.pass_id)-async def _log_phase_cost(- scope: InferenceScope,- pass_id: str,- phase: DreamPhase,- row: dict[str, Any],- phase_models: dict[str, str],+async def _finish_applied(+ bp: BatchPass,+ state: dict[str, dict[str, Any]],+ apply_stats: ApplyStats,+ ops: DreamOperations,+ ingestion_drain_status: IngestionDrainStatus,) -> None:- if row.get("error"):- return- phase_model = phase_models.get(phase)- if not phase_model:- logger.warning(- "No model recorded for pass=%s phase=%s — skipping cost log",- pass_id,- phase,- )- return- ctx = InferenceContext(- scope=scope,- job=phase_job(phase, pass_id, timeout_seconds=None, pinned_model=phase_model),- route=anthropic_batch_route(phase_model),+ """Charge the landed phases, close the job and the record, release."""+ # Per-phase usage log on the success path. Failure paths record the+ # same usage via ``fail_pass`` (we incurred those provider tokens+ # regardless); the Redis dedup gate inside ``log_all_phase_costs``+ # keeps it at-most-once across both paths and any repeated delivery.+ await log_all_phase_costs(+ user_id=bp.user_id,+ expert_id=bp.expert_id,+ pass_id=bp.pass_id,+ state=state,+ phase_models=bp.phase_models,)- usage = InferenceUsage(- model=phase_model,- input_tokens=int(row.get("input_tokens") or 0),- output_tokens=int(row.get("output_tokens") or 0),- cache_read_tokens=int(row.get("cache_read_tokens") or 0),- cache_creation_tokens=int(row.get("cache_creation_tokens") or 0),- payer=ctx.route.payer,+ # The batch path skips the drain by design, so apply reports+ # ``skipped`` whenever the pass enqueued writes (``drained`` only+ # for an empty pass), read via the shared, fail-closed helper.+ await record_completion(+ bp,+ apply_stats,+ ingestion_drain_status=ingestion_drain_status,+ summary_for_user=ops.summary_for_user,+ usage=landed_usage(state, bp.phase_models, bp.pass_id),)- await record_phase_cost(ctx, usage)---# ----------------------------------------------------------------------------# Helpers-# ---------------------------------------------------------------------------+ # The batch path disowned the dream lock to this callback; release it now+ # that the pass has terminated so the next dream for this user can run.+ await release_lock(bp)+ await best_effort_cleanup(bp.pass_id)def _anthropic_api_key() -> str | None:@@ -1115,11 +537,6 @@ def _anthropic_api_key() -> str | None:return None-# ----------------------------------------------------------------------------# Registration (runs at module import)-# -----------------------------------------------------------------------------def _register() -> None:try:from backend.executor.batch_executor import register_handlerdiff --git a/autogpt_platform/backend/backend/copilot/dream/batch_callbacks_test.py b/autogpt_platform/backend/backend/copilot/dream/batch_callbacks_test.pyindex 9e5d223f23..7ead9f1f77 100644--- a/autogpt_platform/backend/backend/copilot/dream/batch_callbacks_test.py+++ b/autogpt_platform/backend/backend/copilot/dream/batch_callbacks_test.py@@ -8,24 +8,42 @@ submit phase 3 → phase 3 result → apply + mark JobStatus complete.from __future__ import annotations+import asyncioimport jsonfrom datetime import datetime, timezonefrom unittest.mock import AsyncMock, MagicMock, patchimport pytest--from backend.copilot.dream.batch_callbacks import (- _write_phase_to_state,- handle_dream_batch_result,+from prisma.enums import (+ DreamPassPhase,+ DreamPassRoute,+ DreamPassStatus,+ DreamPassTrigger,)++from backend.copilot.dream import job_status+from backend.copilot.dream.batch_callbacks import handle_dream_batch_result+from backend.copilot.dream.batch_state import write_phase_to_statefrom backend.copilot.dream.batch_submit import input_bundle_key, persist_input_bundlefrom backend.copilot.dream.fetch import DreamInputfrom backend.copilot.dream.job_status import read_status, write_initial_status-from backend.copilot.dream.schemas import IngestionDrainStatus+from backend.copilot.dream.pass_record import dream_pass_result_from_row+from backend.copilot.dream.schemas import (+ DreamOperations,+ DreamOperationsSnapshot,+ IngestionDrainStatus,+)from backend.copilot.graphiti.scope import MemoryScopefrom backend.copilot.trial_cost_context import get_trial_cost_contextfrom backend.copilot.usage_activation import UsageActivationUnavailable-from backend.executor.batch_executor import PendingEntry, _dispatch+from backend.data.dream_pass_models import DreamPassDraft+from backend.executor.batch_executor import (+ PendingEntry,+ _dispatch,+ enqueue_pending,+ register_handler,+ walk_once,+)from backend.util.llm.providers import BatchResultRow@@ -174,7 +192,7 @@ class TestPhaseChaining:mark_errored,),patch(- "backend.copilot.dream.batch_callbacks.release_dream_lock",+ "backend.copilot.dream.batch_outcome.release_dream_lock",release_lock,),):@@ -225,7 +243,7 @@ class TestPhaseChaining:mark_errored,),patch(- "backend.copilot.dream.batch_callbacks.release_dream_lock",+ "backend.copilot.dream.batch_outcome.release_dream_lock",release_lock,),):@@ -281,7 +299,7 @@ class TestPhaseChaining:mark_errored,),patch(- "backend.copilot.dream.batch_callbacks.release_dream_lock",+ "backend.copilot.dream.batch_outcome.release_dream_lock",release_lock,),):@@ -371,9 +389,9 @@ class TestPhaseChaining:)# Pre-seed phase 1's output in the per-pass state- from backend.copilot.dream.batch_callbacks import _write_phase_to_state+ from backend.copilot.dream.batch_state import write_phase_to_state- await _write_phase_to_state(+ await write_phase_to_state(pass_id="p1",phase="consolidate",row=_row(custom_id="p1:consolidate", content=_CONSOLIDATE_CONTENT),@@ -407,7 +425,7 @@ class TestPhaseChaining:"""Phase 3 is terminal: apply runs, all three phases logged atanthropic_batch path (half the catalog list price), JobStatusflips to complete."""- from backend.copilot.dream.batch_callbacks import _write_phase_to_state+ from backend.copilot.dream.batch_state import write_phase_to_statefrom backend.copilot.dream.batch_submit import persist_input_bundlefrom backend.copilot.dream.fetch import DreamInput@@ -427,12 +445,12 @@ class TestPhaseChaining:known_episode_uuids={"episode-1"},),)- await _write_phase_to_state(+ await write_phase_to_state(pass_id="p1",phase="consolidate",row=_row(custom_id="p1:consolidate", content=_CONSOLIDATE_CONTENT),)- await _write_phase_to_state(+ await write_phase_to_state(pass_id="p1",phase="recombine",row=_row(custom_id="p1:recombine", content=_RECOMBINE_CONTENT),@@ -454,7 +472,7 @@ class TestPhaseChaining:patch("backend.copilot.dream.job_status.mark_complete", mark_complete),patch("backend.copilot.inference.record.persist_and_record_usage", persist),patch(- "backend.copilot.dream.batch_callbacks.release_dream_lock", release_lock+ "backend.copilot.dream.batch_outcome.release_dream_lock", release_lock),):await handle_dream_batch_result(@@ -522,7 +540,7 @@ class TestPhaseChaining:async def test_expert_terminal_result_applies_and_releases_in_expert_scope(self, fake_redis):- from backend.copilot.dream.batch_callbacks import _write_phase_to_state+ from backend.copilot.dream.batch_state import write_phase_to_statefrom backend.copilot.dream.batch_submit import persist_input_bundlefrom backend.copilot.dream.fetch import DreamInput@@ -538,12 +556,12 @@ class TestPhaseChaining:),lock_token="tok-expert",)- await _write_phase_to_state(+ await write_phase_to_state(pass_id="p-expert",phase="consolidate",row=_row(custom_id="p-expert:consolidate", content=_CONSOLIDATE_CONTENT),)- await _write_phase_to_state(+ await write_phase_to_state(pass_id="p-expert",phase="recombine",row=_row(custom_id="p-expert:recombine", content=_RECOMBINE_CONTENT),@@ -565,7 +583,7 @@ class TestPhaseChaining:new=persist,),patch(- "backend.copilot.dream.batch_callbacks.release_dream_lock",+ "backend.copilot.dream.batch_outcome.release_dream_lock",release_lock,),):@@ -591,7 +609,7 @@ class TestPhaseChaining:``remove_pending``, the next walk re-dispatches the same batch.The Redis dedup gates must prevent BOTH the second charge and asecond ``apply_operations`` run."""- from backend.copilot.dream.batch_callbacks import _write_phase_to_state+ from backend.copilot.dream.batch_state import write_phase_to_statefrom backend.copilot.dream.batch_submit import persist_input_bundlefrom backend.copilot.dream.fetch import DreamInput@@ -602,12 +620,12 @@ class TestPhaseChaining:user_id="u1", group_id="user_u1", window_start=now, window_end=now),)- await _write_phase_to_state(+ await write_phase_to_state(pass_id="p1",phase="consolidate",row=_row(custom_id="p1:consolidate", content=_CONSOLIDATE_CONTENT),)- await _write_phase_to_state(+ await write_phase_to_state(pass_id="p1",phase="recombine",row=_row(custom_id="p1:recombine", content=_RECOMBINE_CONTENT),@@ -618,9 +636,7 @@ class TestPhaseChaining:with (patch("backend.copilot.dream.apply.apply_operations", apply),patch("backend.copilot.dream.job_status.mark_complete", AsyncMock()),- patch(- "backend.copilot.dream.batch_callbacks.record_phase_cost", record_cost- ),+ patch("backend.copilot.dream.batch_costs.record_phase_cost", record_cost),):await handle_dream_batch_result(_entry(phase="sanitize"),@@ -650,7 +666,7 @@ class TestPhaseChaining:while the duplicate skips mark_complete (preserving the firstdelivery's job result) and still releases the lock + cleans upstate."""- from backend.copilot.dream.batch_callbacks import _write_phase_to_state+ from backend.copilot.dream.batch_state import write_phase_to_statefrom backend.copilot.dream.batch_submit import persist_input_bundlefrom backend.copilot.dream.fetch import DreamInput@@ -663,12 +679,12 @@ class TestPhaseChaining:user_id="u1", group_id="user_u1", window_start=now, window_end=now),)- await _write_phase_to_state(+ await write_phase_to_state(pass_id="p1",phase="consolidate",row=_row(custom_id="p1:consolidate", content=_CONSOLIDATE_CONTENT),)- await _write_phase_to_state(+ await write_phase_to_state(pass_id="p1",phase="recombine",row=_row(custom_id="p1:recombine", content=_RECOMBINE_CONTENT),@@ -682,16 +698,12 @@ class TestPhaseChaining:with (patch("backend.copilot.dream.apply.apply_operations", apply),patch("backend.copilot.dream.job_status.mark_complete", mark_complete),+ patch("backend.copilot.dream.batch_costs.record_phase_cost", AsyncMock()),patch(- "backend.copilot.dream.batch_callbacks.record_phase_cost", AsyncMock()- ),- patch(- "backend.copilot.dream.batch_callbacks.release_dream_lock", release_lock- ),- patch("backend.copilot.dream.batch_callbacks._delete_state", AsyncMock()),- patch(- "backend.copilot.dream.batch_callbacks.delete_input_bundle", AsyncMock()+ "backend.copilot.dream.batch_outcome.release_dream_lock", release_lock),+ patch("backend.copilot.dream.batch_state.delete_state", AsyncMock()),+ patch("backend.copilot.dream.batch_state.delete_input_bundle", AsyncMock()),):await handle_dream_batch_result(_entry(phase="sanitize"),@@ -726,15 +738,15 @@ class TestPhaseChaining:distinguish first delivery from duplicate. The pass must be markederrored — completing it would report success while no memory waswritten, silently dropping the dream."""- from backend.copilot.dream.batch_callbacks import _write_phase_to_state+ from backend.copilot.dream.batch_state import write_phase_to_stateawait _persist_autopilot_bundle()- await _write_phase_to_state(+ await write_phase_to_state(pass_id="p1",phase="consolidate",row=_row(custom_id="p1:consolidate", content=_CONSOLIDATE_CONTENT),)- await _write_phase_to_state(+ await write_phase_to_state(pass_id="p1",phase="recombine",row=_row(custom_id="p1:recombine", content=_RECOMBINE_CONTENT),@@ -748,10 +760,8 @@ class TestPhaseChaining:patch("backend.copilot.dream.apply.apply_operations", apply),patch("backend.copilot.dream.job_status.mark_complete", mark_complete),patch("backend.copilot.dream.job_status.mark_errored", mark_errored),- patch("backend.copilot.dream.batch_callbacks._claim_apply_gate", gate),- patch(- "backend.copilot.dream.batch_callbacks.record_phase_cost", AsyncMock()- ),+ patch("backend.copilot.dream.batch_callbacks.claim_apply_gate", gate),+ patch("backend.copilot.dream.batch_costs.record_phase_cost", AsyncMock()),):await handle_dream_batch_result(_entry(phase="sanitize"),@@ -777,7 +787,7 @@ class TestErrorPaths:entry.payload = {"user_id": "u1", "job_id": "j-dead"}with patch(- "backend.copilot.dream.batch_callbacks.release_dream_lock", AsyncMock()+ "backend.copilot.dream.batch_outcome.release_dream_lock", AsyncMock()):await handle_dream_batch_result(entry, [])@@ -800,7 +810,7 @@ class TestErrorPaths:}with patch(- "backend.copilot.dream.batch_callbacks.release_dream_lock", AsyncMock()+ "backend.copilot.dream.batch_outcome.release_dream_lock", AsyncMock()):await handle_dream_batch_result(entry, [])@@ -817,9 +827,7 @@ class TestErrorPaths:record_cost = AsyncMock()with (patch("backend.copilot.dream.job_status.mark_errored", mark_errored),- patch(- "backend.copilot.dream.batch_callbacks.record_phase_cost", record_cost- ),+ patch("backend.copilot.dream.batch_costs.record_phase_cost", record_cost),):await handle_dream_batch_result(_entry(phase="consolidate"),@@ -850,9 +858,7 @@ class TestErrorPaths:with (patch("backend.copilot.dream.batch_callbacks.submit_phase", submit_phase),patch("backend.copilot.dream.job_status.mark_errored", mark_errored),- patch(- "backend.copilot.dream.batch_callbacks.record_phase_cost", record_cost- ),+ patch("backend.copilot.dream.batch_costs.record_phase_cost", record_cost),):await handle_dream_batch_result(_entry(phase="consolidate"),@@ -886,7 +892,7 @@ class TestErrorPaths:self, fake_redis, content, error):await _persist_autopilot_bundle()- await _write_phase_to_state(+ await write_phase_to_state(pass_id="p1",phase="consolidate",row=_row(custom_id="p1:consolidate", content=_CONSOLIDATE_CONTENT),@@ -894,9 +900,7 @@ class TestErrorPaths:record_cost = AsyncMock()with (patch("backend.copilot.dream.job_status.mark_errored", AsyncMock()),- patch(- "backend.copilot.dream.batch_callbacks.record_phase_cost", record_cost- ),+ patch("backend.copilot.dream.batch_costs.record_phase_cost", record_cost),):await handle_dream_batch_result(_entry(phase="recombine"),@@ -946,7 +950,7 @@ class TestErrorPaths:"""If apply raises, the pass is errored — but the three LLM phasesalready ran and Anthropic billed us, so their usage is stillrecorded (matches the sync path + dream/billing.py)."""- from backend.copilot.dream.batch_callbacks import _write_phase_to_state+ from backend.copilot.dream.batch_state import write_phase_to_statefrom backend.copilot.dream.batch_submit import persist_input_bundlefrom backend.copilot.dream.fetch import DreamInput@@ -957,12 +961,12 @@ class TestErrorPaths:user_id="u1", group_id="user_u1", window_start=now, window_end=now),)- await _write_phase_to_state(+ await write_phase_to_state(pass_id="p1",phase="consolidate",row=_row(custom_id="p1:consolidate", content=_CONSOLIDATE_CONTENT),)- await _write_phase_to_state(+ await write_phase_to_state(pass_id="p1",phase="recombine",row=_row(custom_id="p1:recombine", content=_RECOMBINE_CONTENT),@@ -974,9 +978,7 @@ class TestErrorPaths:with (patch("backend.copilot.dream.apply.apply_operations", apply),patch("backend.copilot.dream.job_status.mark_errored", mark_errored),- patch(- "backend.copilot.dream.batch_callbacks.record_phase_cost", record_cost- ),+ patch("backend.copilot.dream.batch_costs.record_phase_cost", record_cost),):await handle_dream_batch_result(_entry(phase="sanitize"),@@ -990,7 +992,7 @@ class TestErrorPaths:@pytest.mark.asyncioasync def test_unexpected_crash_releases_disowned_lock(self, fake_redis):- """An unexpected raise OUTSIDE the handler's own _fail_pass guards+ """An unexpected raise OUTSIDE the handler's own fail_pass guards(here phase-chaining blows up) must still release the disowned dreamlock and mark the job errored. BatchExecutor._dispatch swallowshandler exceptions, so without the crash guard this would strand the@@ -1015,11 +1017,9 @@ class TestErrorPaths:with (patch("backend.copilot.dream.batch_callbacks._chain_next_phase", chain),patch("backend.copilot.dream.job_status.mark_errored", mark_errored),+ patch("backend.copilot.dream.batch_costs.record_phase_cost", record_cost),patch(- "backend.copilot.dream.batch_callbacks.record_phase_cost", record_cost- ),- patch(- "backend.copilot.dream.batch_callbacks.release_dream_lock", release_lock+ "backend.copilot.dream.batch_outcome.release_dream_lock", release_lock),):# Must not propagate — the crash guard finalizes and swallows.@@ -1045,7 +1045,7 @@ class TestMalformedPayload:release = AsyncMock()entry = _entry()entry.payload["pass_id"] = ""- with patch("backend.copilot.dream.batch_callbacks.release_dream_lock", release):+ with patch("backend.copilot.dream.batch_outcome.release_dream_lock", release):await handle_dream_batch_result(entry,[_row(custom_id="p1:consolidate", content=_CONSOLIDATE_CONTENT)],@@ -1075,7 +1075,7 @@ class TestMalformedPayload:release = AsyncMock()entry = _entry()entry.payload["phase"] = "some_fake_phase"- with patch("backend.copilot.dream.batch_callbacks.release_dream_lock", release):+ with patch("backend.copilot.dream.batch_outcome.release_dream_lock", release):await handle_dream_batch_result(entry, [_row(custom_id="x", content="y")])release.assert_awaited_once_with(MemoryScope.for_user("u1"), "tok-u1")@@ -1085,7 +1085,7 @@ class TestLockTokenWiring:fake redis implements the single-key compare-and-delete Lua."""async def _seed_terminal_pass(self) -> None:- from backend.copilot.dream.batch_callbacks import _write_phase_to_state+ from backend.copilot.dream.batch_state import write_phase_to_statefrom backend.copilot.dream.batch_submit import persist_input_bundlefrom backend.copilot.dream.fetch import DreamInput@@ -1096,12 +1096,12 @@ class TestLockTokenWiring:user_id="u1", group_id="user_u1", window_start=now, window_end=now),)- await _write_phase_to_state(+ await write_phase_to_state(pass_id="p1",phase="consolidate",row=_row(custom_id="p1:consolidate", content=_CONSOLIDATE_CONTENT),)- await _write_phase_to_state(+ await write_phase_to_state(pass_id="p1",phase="recombine",row=_row(custom_id="p1:recombine", content=_RECOMBINE_CONTENT),@@ -1114,9 +1114,7 @@ class TestLockTokenWiring:AsyncMock(return_value={"writes": 0}),),patch("backend.copilot.dream.job_status.mark_complete", AsyncMock()),- patch(- "backend.copilot.dream.batch_callbacks.record_phase_cost", AsyncMock()- ),+ patch("backend.copilot.dream.batch_costs.record_phase_cost", AsyncMock()),):await handle_dream_batch_result(_entry(phase="sanitize"),@@ -1140,7 +1138,7 @@ class TestLockTokenWiring:"""A Redis blip on the lock-token read in the terminal tail firesAFTER mark_complete already ran. The read must stay best-effort(token=None → lock TTL fallback) — letting it propagate would hit- the handler's crash guard, whose _fail_pass rewrites the+ the handler's crash guard, whose fail_pass rewrites thealready-completed job to errored."""_, _, string_store = fake_redisstring_store["dream:inflight:u1"] = "tok-u1"@@ -1157,12 +1155,10 @@ class TestLockTokenWiring:),patch("backend.copilot.dream.job_status.mark_complete", mark_complete),patch("backend.copilot.dream.job_status.mark_errored", mark_errored),+ patch("backend.copilot.dream.batch_costs.record_phase_cost", AsyncMock()),+ patch("backend.copilot.dream.batch_outcome.read_lock_token", read_token),patch(- "backend.copilot.dream.batch_callbacks.record_phase_cost", AsyncMock()- ),- patch("backend.copilot.dream.batch_callbacks.read_lock_token", read_token),- patch(- "backend.copilot.dream.batch_callbacks.release_dream_lock", release_lock+ "backend.copilot.dream.batch_outcome.release_dream_lock", release_lock),):await handle_dream_batch_result(@@ -1179,7 +1175,7 @@ class TestLockTokenWiring:@pytest.mark.asyncioasync def test_cleanup_failure_after_complete_keeps_job_completed(self, fake_redis):"""A Redis blip on the post-mark_complete state/bundle deletes must- not route through the crash guard to _fail_pass — both keys carry+ not route through the crash guard to fail_pass — both keys carry24h TTLs, so cleanup is best-effort and the completed job stayscompleted."""await self._seed_terminal_pass()@@ -1194,12 +1190,10 @@ class TestLockTokenWiring:),patch("backend.copilot.dream.job_status.mark_complete", mark_complete),patch("backend.copilot.dream.job_status.mark_errored", mark_errored),+ patch("backend.copilot.dream.batch_costs.record_phase_cost", AsyncMock()),+ patch("backend.copilot.dream.batch_state.delete_state", delete_state),patch(- "backend.copilot.dream.batch_callbacks.record_phase_cost", AsyncMock()- ),- patch("backend.copilot.dream.batch_callbacks._delete_state", delete_state),- patch(- "backend.copilot.dream.batch_callbacks.release_dream_lock", AsyncMock()+ "backend.copilot.dream.batch_outcome.release_dream_lock", AsyncMock()),):await handle_dream_batch_result(@@ -1235,11 +1229,9 @@ class TestLockTokenWiring:apply = AsyncMock()with (patch("backend.copilot.dream.apply.apply_operations", apply),+ patch("backend.copilot.dream.batch_costs.record_phase_cost", AsyncMock()),patch(- "backend.copilot.dream.batch_callbacks.record_phase_cost", AsyncMock()- ),- patch(- "backend.copilot.dream.batch_callbacks.release_dream_lock", AsyncMock()+ "backend.copilot.dream.batch_outcome.release_dream_lock", AsyncMock()),):await handle_dream_batch_result(@@ -1274,11 +1266,9 @@ class TestLockTokenWiring:patch("backend.copilot.dream.apply.apply_operations", AsyncMock()),patch("backend.copilot.dream.job_status.mark_complete", mark_complete),patch("backend.copilot.dream.job_status.read_status", read_existing),+ patch("backend.copilot.dream.batch_costs.record_phase_cost", AsyncMock()),patch(- "backend.copilot.dream.batch_callbacks.record_phase_cost", AsyncMock()- ),- patch(- "backend.copilot.dream.batch_callbacks.release_dream_lock", AsyncMock()+ "backend.copilot.dream.batch_outcome.release_dream_lock", AsyncMock()),):await handle_dream_batch_result(@@ -1308,6 +1298,425 @@ class TestLockTokenWiring:assert string_store["dream:inflight:u1"] == "tok-newer-pass"+def _seed_submitted_pass(fake_dream_db, pass_id: str = "p1") -> None:+ """The row as the orchestrator leaves it once consolidate is submitted."""+ fake_dream_db.seed(+ DreamPassDraft(+ id=pass_id,+ user_id="u1",+ scope_key="u1",+ route=DreamPassRoute.ANTHROPIC_BATCH,+ trigger=DreamPassTrigger.CRON,+ status=DreamPassStatus.SUBMITTED,+ phase=DreamPassPhase.CONSOLIDATE,+ started_at=datetime.now(timezone.utc),+ )+ )+++class TestDreamPassRecord:+ """Each callback advances the pass's durable row: the phase that landed+ and its output, the next batch, the apply, and the end with what the+ landed phases used."""++ @pytest.mark.asyncio+ async def test_the_batch_chain_records_every_step(self, fake_redis, fake_dream_db):+ _, _, string_store = fake_redis+ string_store["dream:inflight:u1"] = "tok-u1"+ await _persist_autopilot_bundle()+ _seed_submitted_pass(fake_dream_db)+ submit_phase = AsyncMock(+ side_effect=[+ MagicMock(provider_batch_id="msgbatch_recombine"),+ MagicMock(provider_batch_id="msgbatch_sanitize"),+ ]+ )+ apply = AsyncMock(+ return_value={+ "session_id": "s1",+ "consolidated_count": 2,+ "proposal_count": 1,+ "demotion_count": 0,+ "entity_invalidation_count": 0,+ "ingestion_drain_status": IngestionDrainStatus.skipped,+ "snapshot": DreamOperationsSnapshot(),+ }+ )+ with (+ patch("backend.copilot.dream.batch_callbacks.submit_phase", submit_phase),+ patch(+ "backend.copilot.dream.batch_callbacks._anthropic_api_key",+ return_value="sk-ant-test",+ ),+ patch("backend.copilot.dream.job_status.update_status_phase", AsyncMock()),+ patch("backend.copilot.dream.apply.apply_operations", apply),+ patch("backend.copilot.dream.job_status.mark_complete", AsyncMock()),+ patch(+ "backend.copilot.inference.record.persist_and_record_usage", AsyncMock()+ ),+ patch(+ "backend.copilot.dream.batch_outcome.release_dream_lock", AsyncMock()+ ),+ ):+ for phase, content in (+ ("consolidate", _CONSOLIDATE_CONTENT),+ ("recombine", _RECOMBINE_CONTENT),+ ("sanitize", _SANITIZE_CONTENT),+ ):+ await handle_dream_batch_result(+ _entry(phase=phase),+ [_row(custom_id=f"p1:{phase}", content=content)],+ )++ assert fake_dream_db.phases("p1") == [+ DreamPassPhase.RECOMBINE,+ DreamPassPhase.SANITIZE,+ DreamPassPhase.APPLY,+ DreamPassPhase.DONE,+ ]+ assert fake_dream_db.statuses("p1") == [+ DreamPassStatus.APPLYING,+ DreamPassStatus.COMPLETE,+ ]+ assert [+ update.provider_batch_id+ for _, update in fake_dream_db.writes+ if update.provider_batch_id+ ] == ["msgbatch_recombine", "msgbatch_sanitize"]+ row = fake_dream_db.rows["p1"]+ assert list(row["phase_outputs"]) == ["consolidate", "recombine", "sanitize"]+ assert row["operations"]["planned"] == DreamOperations(summary_for_user="ok")+ applied = row["operations"]["applied"]+ assert (applied.consolidated_count, applied.dream_session_id) == (2, "s1")+ # Each landed phase priced on its own model at the batch discount,+ # as its cost row is: 10 input + 20 output tokens per ``_row``.+ usage = row["usage"]+ assert [p.model for p in usage.phases] == [+ "claude-sonnet-5",+ "claude-opus-5-5",+ "claude-sonnet-5",+ ]+ sonnet_5_cost = (10 * 2.0 + 20 * 10.0) / 1_000_000 / 2+ opus_5_5_cost = (10 * 4.0 + 20 * 20.0) / 1_000_000 / 2+ assert usage.total_cost_usd == pytest.approx(2 * sonnet_5_cost + opus_5_5_cost)+ assert usage.discount_applied == 0.5+ # What the eval driver reads back: a batch pass's result with usage.+ result = dream_pass_result_from_row(fake_dream_db.record("p1"))+ assert result.execution_path == "anthropic_batch"+ assert result.usage == usage+ assert (result.consolidated_count, result.proposal_count) == (2, 1)+ assert result.ingestion_drain_status is IngestionDrainStatus.skipped++ @pytest.mark.asyncio+ async def test_a_failed_phase_closes_the_row_with_the_landed_usage(+ self, fake_redis, fake_dream_db+ ):+ await _persist_autopilot_bundle()+ _seed_submitted_pass(fake_dream_db)+ await write_phase_to_state(+ pass_id="p1",+ phase="consolidate",+ row=_row(custom_id="p1:consolidate", content=_CONSOLIDATE_CONTENT),+ )+ with (+ patch("backend.copilot.dream.batch_costs.record_phase_cost", AsyncMock()),+ patch(+ "backend.copilot.dream.batch_outcome.release_dream_lock", AsyncMock()+ ),+ ):+ await handle_dream_batch_result(+ _entry(phase="recombine"),+ [_row(custom_id="p1:recombine", content="", error="provider down")],+ )++ assert fake_dream_db.statuses("p1") == [DreamPassStatus.ERRORED]+ row = fake_dream_db.rows["p1"]+ assert row["error"] == "recombine: provider down"+ # The errored phase used nothing billable; consolidate did.+ assert [p.phase for p in row["usage"].phases] == ["consolidate"]+ assert row["completed_at"] is not None++ @pytest.mark.asyncio+ async def test_an_apply_crash_closes_the_row_errored_after_applying(+ self, fake_redis, fake_dream_db+ ):+ await _persist_autopilot_bundle()+ _seed_submitted_pass(fake_dream_db)+ with (+ patch(+ "backend.copilot.dream.apply.apply_operations",+ AsyncMock(side_effect=RuntimeError("FalkorDB unreachable")),+ ),+ patch("backend.copilot.dream.batch_costs.record_phase_cost", AsyncMock()),+ patch(+ "backend.copilot.dream.batch_outcome.release_dream_lock", AsyncMock()+ ),+ ):+ await handle_dream_batch_result(+ _entry(phase="sanitize"),+ [_row(custom_id="p1:sanitize", content=_SANITIZE_CONTENT)],+ )++ assert fake_dream_db.statuses("p1") == [+ DreamPassStatus.APPLYING,+ DreamPassStatus.ERRORED,+ ]+ row = fake_dream_db.rows["p1"]+ assert row["error"] == "apply: RuntimeError: FalkorDB unreachable"+ assert row["operations"]["planned"] == DreamOperations(summary_for_user="ok")++ @pytest.mark.asyncio+ @pytest.mark.parametrize(+ "payload_fix, error",+ [+ ({"phase": "daydream"}, "unknown batch phase 'daydream'"),+ ({"phase": None}, "batch payload missing user_id/pass_id/phase"),+ ],+ ids=["unknown_phase", "missing_phase"],+ )+ async def test_a_dead_end_payload_closes_the_row_with_landed_usage(+ self, fake_redis, fake_dream_db, payload_fix, error+ ):+ """The consolidate phase landed and was billed; a payload no phase+ handler can take then ends the pass. The record is closed with that+ phase's usage before the disowned lock is released."""+ _seed_submitted_pass(fake_dream_db)+ await write_phase_to_state(+ pass_id="p1",+ phase="consolidate",+ row=_row(custom_id="p1:consolidate", content=_CONSOLIDATE_CONTENT),+ )+ entry = _entry(phase="recombine")+ entry.payload.update(payload_fix)+ order: list[str] = []+ record = fake_dream_db.update_dream_pass++ async def recorded(pass_id, update):+ order.append(f"record {update.status}")+ return await record(pass_id, update)++ release_lock = AsyncMock(side_effect=lambda *_: order.append("lock released"))+ with (+ patch.object(fake_dream_db, "update_dream_pass", recorded),+ patch(+ "backend.copilot.dream.batch_outcome.release_dream_lock", release_lock+ ),+ ):+ await handle_dream_batch_result(entry, [])++ row = fake_dream_db.rows["p1"]+ assert row["status"] is DreamPassStatus.ERRORED+ assert row["error"] == error+ assert [p.phase for p in row["usage"].phases] == ["consolidate"]+ assert order == ["record ERRORED", "lock released"]++ @pytest.mark.asyncio+ async def test_an_unreadable_state_still_closes_the_row_errored(+ self, fake_dream_db, fake_dream_redis, monkeypatch+ ):+ """Only reading the pass's Redis state fails; the database and the+ rest of Redis are healthy. The crash it causes still closes the row+ ERRORED, its usage unknown, errors the admin job and releases the+ lock."""+ now = datetime.now(timezone.utc)+ await persist_input_bundle(+ "p1",+ DreamInput(+ user_id="u1", group_id="user_u1", window_start=now, window_end=now+ ),+ lock_token="tok-u1",+ )+ lock_key = MemoryScope.for_user("u1").redis_key("dream_lock")+ fake_dream_redis.store[lock_key] = "tok-u1"+ await job_status.write_initial_status(+ kind="dream_pass", job_id="j1", user_id="u1"+ )+ _seed_submitted_pass(fake_dream_db)+ hgetall = fake_dream_redis.hgetall++ async def state_unreadable(name):+ if name.startswith("dream:batch:state:"):+ raise ConnectionError("scripted Redis state outage")+ return await hgetall(name)++ monkeypatch.setattr(fake_dream_redis, "hgetall", state_unreadable)+ submit_phase = AsyncMock()++ with (+ patch("backend.copilot.dream.batch_callbacks.submit_phase", submit_phase),+ patch(+ "backend.copilot.dream.batch_callbacks._anthropic_api_key",+ return_value="sk-ant-test",+ ),+ ):+ await handle_dream_batch_result(+ _entry(phase="consolidate"),+ [_row(custom_id="p1:consolidate", content=_CONSOLIDATE_CONTENT)],+ )++ submit_phase.assert_not_awaited()+ row = fake_dream_db.rows["p1"]+ assert row["status"] is DreamPassStatus.ERRORED+ assert row["error"] == "consolidate: handler crashed"+ assert row.get("usage") is None+ status = await job_status.read_status(kind="dream_pass", job_id="j1")+ assert status is not None and status.state == "errored"+ assert lock_key not in fake_dream_redis.store++ @pytest.mark.asyncio+ async def test_applying_is_recorded_before_the_apply_gate_is_claimed(+ self, fake_dream_db, fake_dream_redis+ ):+ """A delivery that dies while its APPLYING write is in flight has not+ claimed the apply gate yet, so the next delivery still applies."""+ await _persist_autopilot_bundle()+ _seed_submitted_pass(fake_dream_db)+ for phase, content in (+ ("consolidate", _CONSOLIDATE_CONTENT),+ ("recombine", _RECOMBINE_CONTENT),+ ):+ await write_phase_to_state(+ pass_id="p1",+ phase=phase,+ row=_row(custom_id=f"p1:{phase}", content=content),+ )+ apply = AsyncMock(return_value={"consolidated_count": 0})+ entered, release = asyncio.Event(), asyncio.Event()+ record = fake_dream_db.update_dream_pass++ async def applying_held(pass_id, update):+ if update.status is DreamPassStatus.APPLYING:+ entered.set()+ await release.wait()+ return await record(pass_id, update)++ with (+ patch("backend.copilot.dream.apply.apply_operations", apply),+ patch("backend.copilot.dream.job_status.mark_complete", AsyncMock()),+ patch(+ "backend.copilot.inference.record.persist_and_record_usage", AsyncMock()+ ),+ patch(+ "backend.copilot.dream.batch_outcome.release_dream_lock", AsyncMock()+ ),+ ):+ with patch.object(fake_dream_db, "update_dream_pass", applying_held):+ first = asyncio.create_task(+ handle_dream_batch_result(+ _entry(phase="sanitize"),+ [_row(custom_id="p1:sanitize", content=_SANITIZE_CONTENT)],+ )+ )+ await asyncio.wait_for(entered.wait(), 5)+ assert "dream:applied:p1" not in fake_dream_redis.store+ first.cancel()+ with pytest.raises(asyncio.CancelledError):+ await first+ apply.assert_not_awaited()++ await handle_dream_batch_result(+ _entry(phase="sanitize"),+ [_row(custom_id="p1:sanitize", content=_SANITIZE_CONTENT)],+ )++ apply.assert_awaited_once()+ assert fake_dream_redis.store["dream:applied:p1"] == "1"+ assert fake_dream_db.rows["p1"]["status"] is DreamPassStatus.COMPLETE++ @pytest.mark.asyncio+ async def test_the_batch_walker_keeps_moving_past_a_stalled_store(+ self, stalled_dream_db+ ):+ """The executor walks its queue serially, awaiting each callback. With+ the DatabaseManager stalled, each callback still finishes (each record+ write abandoned at its deadline), so every entry is dispatched."""+ for pass_id in ("p1", "p2"):+ await _persist_autopilot_bundle(pass_id)+ await enqueue_pending(+ _entry(pass_id=pass_id, phase="consolidate").model_copy(+ update={"provider_batch_id": f"batch-{pass_id}"}+ )+ )+ register_handler("dream_pass", handle_dream_batch_result)+ submit_phase = AsyncMock(+ side_effect=[+ MagicMock(provider_batch_id="r1"),+ MagicMock(provider_batch_id="r2"),+ ]+ )++ async def results(provider, provider_batch_id, api_key):+ pass_id = provider_batch_id.removeprefix("batch-")+ return [+ _row(custom_id=f"{pass_id}:consolidate", content=_CONSOLIDATE_CONTENT)+ ]++ with (+ patch(+ "backend.executor.batch_executor.poll_batch",+ AsyncMock(return_value="ended"),+ ),+ patch(+ "backend.executor.batch_executor.download_batch_results",+ AsyncMock(side_effect=results),+ ),+ patch(+ "backend.executor.batch_executor._claim_dispatch",+ AsyncMock(return_value=True),+ ),+ patch("backend.copilot.dream.batch_callbacks.submit_phase", submit_phase),+ patch(+ "backend.copilot.dream.batch_callbacks._anthropic_api_key",+ return_value="sk-ant-test",+ ),+ patch("backend.copilot.dream.job_status.update_status_phase", AsyncMock()),+ ):+ await asyncio.wait_for(walk_once(api_key_for=lambda _: "sk-ant-test"), 10)++ assert submit_phase.await_count == 2+ assert {call.kwargs["pass_id"] for call in submit_phase.await_args_list} == {+ "p1",+ "p2",+ }+ # Each callback's two writes (its output, its next batch), each+ # abandoned at the deadline.+ assert (stalled_dream_db.started, stalled_dream_db.cancelled) == (4, 4)++ @pytest.mark.asyncio+ async def test_a_store_outage_never_fails_a_batch_pass(+ self, fake_redis, fake_dream_db+ ):+ await _persist_autopilot_bundle()+ _seed_submitted_pass(fake_dream_db)+ fake_dream_db.fail = True+ mark_complete = AsyncMock()+ mark_errored = AsyncMock()+ release_lock = AsyncMock()+ with (+ patch(+ "backend.copilot.dream.apply.apply_operations",+ AsyncMock(return_value={"consolidated_count": 0}),+ ),+ patch("backend.copilot.dream.job_status.mark_complete", mark_complete),+ patch("backend.copilot.dream.job_status.mark_errored", mark_errored),+ patch(+ "backend.copilot.inference.record.persist_and_record_usage", AsyncMock()+ ),+ patch(+ "backend.copilot.dream.batch_outcome.release_dream_lock", release_lock+ ),+ ):+ await handle_dream_batch_result(+ _entry(phase="sanitize"),+ [_row(custom_id="p1:sanitize", content=_SANITIZE_CONTENT)],+ )++ mark_complete.assert_awaited_once()+ mark_errored.assert_not_awaited()+ release_lock.assert_awaited_once()+ assert fake_dream_db.writes == []++@pytest.mark.asyncioasync def test_callback_restores_original_trial_generation_for_late_result():entry = _entry()@@ -1345,7 +1754,7 @@ async def test_callback_refuses_missing_or_foreign_attribution(fake_redis):@pytest.mark.parametrize("attribution", ["missing", "malformed", "foreign"])@pytest.mark.parametrize("newer_lock", [False, True])async def test_dispatch_invalid_attribution_closes_job_without_billing(- fake_redis, attribution, newer_lock+ fake_redis, fake_dream_db, attribution, newer_lock):_, state_store, string_store = fake_redisnow = datetime.now(timezone.utc)@@ -1361,6 +1770,7 @@ async def test_dispatch_invalid_attribution_closes_job_without_billing(lock_token="original-token",)await write_initial_status(kind="dream_pass", job_id="j1", user_id="owner")+ _seed_submitted_pass(fake_dream_db)lock_key = MemoryScope.for_expert("owner", "expert-original").redis_key("dream_lock")@@ -1378,7 +1788,7 @@ async def test_dispatch_invalid_attribution_closes_job_without_billing(entry.payload["cost_context"] = {"user_id": "foreign", "trial_id": None}with (- patch("backend.copilot.dream.batch_callbacks.record_phase_cost") as bill,+ patch("backend.copilot.dream.batch_costs.record_phase_cost") as bill,patch("backend.copilot.dream.batch_callbacks.submit_phase") as submit,patch("backend.copilot.dream.apply.apply_operations") as apply,patch("backend.copilot.trial_cost_context.capture_cost_context") as capture,@@ -1392,6 +1802,9 @@ async def test_dispatch_invalid_attribution_closes_job_without_billing(assert status.completed_at is not Noneassert string_store.get(lock_key) == ("newer-token" if newer_lock else None)assert string_store["dream:inflight:u1"] == "unrelated-token"+ row = fake_dream_db.rows["p1"]+ assert row["status"] is DreamPassStatus.ERRORED+ assert row["error"] == "batch usage attribution unavailable"assert input_bundle_key("p1") not in string_storeassert "dream:batch:state:p1" not in state_storebill.assert_not_called()diff --git a/autogpt_platform/backend/backend/copilot/dream/batch_costs.py b/autogpt_platform/backend/backend/copilot/dream/batch_costs.pynew file mode 100644index 0000000000..480fef570b--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/dream/batch_costs.py@@ -0,0 +1,126 @@+"""What a batch dream pass cost: one PlatformCostLog row per phase that+landed, charged at most once whichever way the pass ends, and the usage+of those phases for the pass's DreamPass record."""++from __future__ import annotations++import logging+from typing import Any++from backend.copilot.inference.context import InferenceContext, InferenceScope+from backend.copilot.inference.routing import anthropic_batch_route++from .batch_state import claim_costs_logged_gate, read_state_or_none+from .billing import record_phase_cost+from .phase_jobs import PHASE_TIERS, phase_job+from .schemas import DreamPassUsage, DreamPhase+from .usage import batch_pass_usage, landed_phase_usage++logger = logging.getLogger(__name__)+++async def log_all_phase_costs(+ *,+ user_id: str,+ expert_id: str | None,+ pass_id: str,+ state: dict[str, dict[str, Any]],+ phase_models: dict[str, str],+) -> None:+ """One PlatformCostLog row per phase, on the ``anthropic_batch`` route.++ Idempotent via a Redis SETNX gate keyed on ``pass_id``: a pass logs its+ costs from whichever terminal path it takes, success or failure, and a+ repeated delivery of a finished batch must not charge it twice. The gate+ is claimed once, before the loop, and never released, so each phase+ gets at most one accounting attempt while the gate is retained.+ Accounting is not transactional: the trial ledger and the weekly+ counter can succeed before the cost-log write raises, so a failed+ attempt may leave partial side effects, and it is not retried because+ no later delivery gets past the gate; the other landed phases are+ still charged. A partial failure under-accounts the pass rather than+ risk a second attempt for a phase.++ Each phase is recorded through ``billing.record_phase_cost`` like a+ sync phase, attributed to the pass's expert, and priced from its+ model's catalog price card (``backend/copilot/price_card.py``):+ Anthropic's additive cache buckets, less the batch path's half-price+ discount.++ No-ops on per-phase failure — apply already wrote the user-facing+ memory operations; a cost-log blip shouldn't take that down.+ """+ if not await claim_costs_logged_gate(pass_id):+ logger.info(+ "Skipping batch cost log for pass=%s — already charged",+ pass_id,+ )+ return++ for phase in PHASE_TIERS:+ row = state.get(phase)+ if row is None:+ continue+ try:+ scope = InferenceScope(user_id=user_id, expert_id=expert_id)+ await _log_phase_cost(scope, pass_id, phase, row, phase_models)+ except Exception:+ logger.exception(+ "Failed to log batch cost for pass=%s phase=%s", pass_id, phase+ )+++async def recorded_usage(+ pass_id: str, phase_models: dict[str, str]+) -> DreamPassUsage | None:+ """What the pass's landed phases used, read off its Redis state; ``None``+ when the state cannot be read."""+ return landed_usage(await read_state_or_none(pass_id), phase_models, pass_id)+++def landed_usage(+ state: dict[str, dict[str, Any]] | None,+ phase_models: dict[str, str],+ pass_id: str,+) -> DreamPassUsage | None:+ """What the pass's landed phases used, for its record; ``None``+ (unknown) when its state could not be read. A state row that will not+ price costs the record its usage, never the pass its outcome."""+ if state is None:+ return None+ try:+ return batch_pass_usage(state, phase_models)+ except Exception:+ logger.warning(+ f"Dream pass {pass_id}: could not price its landed phases for the record",+ exc_info=True,+ )+ return None+++async def _log_phase_cost(+ scope: InferenceScope,+ pass_id: str,+ phase: DreamPhase,+ row: dict[str, Any],+ phase_models: dict[str, str],+) -> None:+ if row.get("error"):+ return+ phase_model = phase_models.get(phase)+ if not phase_model:+ logger.warning(+ "No model recorded for pass=%s phase=%s — skipping cost log",+ pass_id,+ phase,+ )+ return+ usage = landed_phase_usage(row, phase_model)+ if usage is None:+ return+ ctx = InferenceContext(+ scope=scope,+ job=phase_job(phase, pass_id, timeout_seconds=None, pinned_model=phase_model),+ route=anthropic_batch_route(phase_model),+ )+ await record_phase_cost(ctx, usage)diff --git a/autogpt_platform/backend/backend/copilot/dream/batch_handoff.py b/autogpt_platform/backend/backend/copilot/dream/batch_handoff.pynew file mode 100644index 0000000000..7385f4d4b6--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/dream/batch_handoff.py@@ -0,0 +1,112 @@+"""Hands a gathered dream pass to Anthropic's Message Batches API.++The sync entry point runs this when routing picked ``anthropic_batch``: it+persists the input bundle, submits phase 1 (consolidate) and returns at once.+The BatchExecutor polls; ``batch_callbacks`` submits phases 2 (recombine) and+3 (sanitize) as each prior result lands, then applies and closes the pass.+The dream lock goes with the pass: extended to the batch window here and+released by the callbacks when the pass ends.++Any caller can take this route (it depends on the flag and the deployment's+key, not the caller). ``status_id`` ties the callbacks' updates to the+JobStatus row an admin trigger created; without one the callbacks skip the+JobStatus writes, and apply and cost logging still run.+"""++import logging++from backend.copilot.config import ChatConfig+from backend.executor.batch_executor import remove_pending++from .batch_submit import (+ delete_input_bundle,+ persist_input_bundle,+ phase_models_for_config,+ submit_phase,+)+from .fetch import DreamInput+from .locks import BATCH_LOCK_TTL_SECONDS, DreamLockHandle+from .pass_run import DreamPassRun+from .schemas import DreamPassResult+from .store import record_submitted++logger = logging.getLogger(__name__)+++async def submit_dream_pass_batch(+ run: DreamPassRun,+ *,+ config: ChatConfig,+ input_bundle: DreamInput,+ status_id: str | None,+ lock_handle: DreamLockHandle,+) -> DreamPassResult:+ """Submit phase 1 and hand the pass, and its lock, to the callbacks."""+ api_key = config.direct_anthropic_api_key+ if not api_key:+ # Shouldn't happen — routing.py only picks anthropic_batch when+ # the key is present. Guard for type-narrowing + future safety.+ return run.failure("anthropic_batch: no Anthropic API key (routing bug)")++ # Persist DreamInput so the per-phase callbacks can rebuild the+ # next phase's prompt without re-fetching from Postgres + FalkorDB.+ try:+ await persist_input_bundle(+ run.pass_id, input_bundle, lock_token=lock_handle.token+ )+ except Exception as exc:+ return run.failure(f"anthropic_batch: input persist failed: {exc}")++ # An empty job id tells the callbacks there is no JobStatus row to update.+ try:+ submission = await submit_phase(+ user_id=run.user_id,+ pass_id=run.pass_id,+ job_id=status_id or "",+ phase="consolidate",+ phase_models=phase_models_for_config(config),+ api_key=api_key,+ input_bundle=input_bundle,+ )+ except Exception as exc:+ return run.failure(f"anthropic_batch: phase 1 submit failed: {exc}")+ return await _hand_off(run, lock_handle, submission.provider_batch_id, input_bundle)+++async def _hand_off(+ run: DreamPassRun,+ lock_handle: DreamLockHandle,+ provider_batch_id: str,+ input_bundle: DreamInput,+) -> DreamPassResult:+ """Phase 1 is enqueued — hand the dream lock to the batch callback so it+ spans the full async lifetime (apply runs hours later). Extend the TTL to+ the batch window first; the callback releases it on terminal/failure.++ A failed extend means the lock expired before the handoff — a newer pass+ may already own the graph, so the just-submitted batch must never be+ applied: revoke the pending entry (the poller then never dispatches the+ callback chain) and drop the input bundle. The provider batch is orphaned;+ its results are discarded. The lock is NOT disowned, so the context+ manager's compare-and-delete release stays a safe no-op.+ """+ logger.info(+ "Dream pass %s submitted via Anthropic batch=%s (phase=consolidate)",+ run.pass_id,+ provider_batch_id,+ )+ if not await lock_handle.extend(BATCH_LOCK_TTL_SECONDS):+ await remove_pending(provider_batch_id)+ await delete_input_bundle(run.pass_id)+ return run.failure(+ "anthropic_batch: dream lock lost before handoff — batch revoked"+ )+ lock_handle.disown()+ await record_submitted(+ run.pass_id,+ input_bundle=input_bundle,+ provider_batch_id=provider_batch_id,+ lease_token=lock_handle.token,+ lease_ttl_seconds=BATCH_LOCK_TTL_SECONDS,+ )+ return run.handed_off()diff --git a/autogpt_platform/backend/backend/copilot/dream/batch_outcome.py b/autogpt_platform/backend/backend/copilot/dream/batch_outcome.pynew file mode 100644index 0000000000..c5cd0e84df--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/dream/batch_outcome.py@@ -0,0 +1,302 @@+"""How a batch dream pass ends: what it reports to the admin JobStatus row and+to its durable DreamPass record, and the lock it gives back.++A batch pass ends in a callback, in another process, long after it started:+``fail_pass`` for every failure, ``record_completion`` once apply has run.+Either way the record gets the usage of every phase that landed, read off the+pass's Redis state and priced in ``batch_costs.py``.+"""++from __future__ import annotations++import logging+from typing import Any++from pydantic import BaseModel, ConfigDict++from backend.copilot.graphiti.scope import MemoryScope++from .batch_costs import landed_usage, log_all_phase_costs+from .batch_state import best_effort_cleanup, read_state_or_none+from .batch_submit import read_lock_token+from .locks import release_dream_lock+from .schemas import (+ DreamOperations,+ DreamOperationsSnapshot,+ DreamPassResult,+ DreamPassUsage,+ IngestionDrainStatus,+)+from .store import record_batch_complete, record_batch_failed++logger = logging.getLogger(__name__)++# What ``apply.apply_operations`` reports.+ApplyStats = dict[str, int | str | IngestionDrainStatus | DreamOperationsSnapshot]+++class BatchPass(BaseModel):+ """One batch pass as a callback sees it: whose it is, the admin job it+ reports to (empty when there is none), and the model each phase ran on."""++ model_config = ConfigDict(frozen=True)++ user_id: str+ expert_id: str | None+ pass_id: str+ job_id: str+ phase_models: dict[str, str]++ @classmethod+ def from_payload(cls, payload: dict[str, Any]) -> "BatchPass":+ """The pass a pending entry's payload names. Missing fields read as+ empty; the handler turns those into a dead end."""+ expert_id = payload.get("expert_id")+ return cls(+ user_id=str(payload.get("user_id") or ""),+ expert_id=str(expert_id) if expert_id is not None else None,+ pass_id=str(payload.get("pass_id") or ""),+ job_id=str(payload.get("job_id") or ""),+ phase_models=_phase_models(payload.get("phase_models")),+ )+++async def fail_pass(bp: BatchPass, error: str, *, charge: bool = True) -> None:+ """Close the pass errored: its admin job and its record first, then+ charge the phases that landed, release the lock and clean up per-pass+ state. Each step is best-effort on its own, so one that fails never+ stops the ones after it. A pass that cannot be charged under its own+ usage attribution passes ``charge=False``: its record still shows what+ the landed phases used, and nothing is charged under another one.++ The record carries the usage of the phases that landed, read off the+ pass's state; a state that cannot be read leaves that usage unknown+ rather than the record open.++ We incurred the provider tokens for completed phases regardless of+ whether the whole pass landed, so they're recorded against the+ user's usage — matching the sync path and the documented contract in+ ``dream/billing.py``. The idempotency gate inside+ ``log_all_phase_costs`` keeps this at-most-once.+ """+ logger.warning("Dream batch pass=%s failed: %s", bp.pass_id, error)+ await mark_job_errored(bp.job_id, error)+ state = await read_state_or_none(bp.pass_id)+ await record_batch_failed(+ bp.pass_id, error, landed_usage(state, bp.phase_models, bp.pass_id)+ )+ if state and charge:+ await _charge_landed_phases(bp, state)+ # Release the dream lock the batch path disowned to this callback.+ await release_lock(bp)+ await best_effort_cleanup(bp.pass_id)+++async def record_completion(+ bp: BatchPass,+ apply_stats: ApplyStats,+ *,+ ingestion_drain_status: IngestionDrainStatus,+ summary_for_user: str,+ usage: DreamPassUsage | None,+) -> None:+ """Close the admin job and the pass's record with what apply reported.++ Neither may raise: apply's writes have landed, and the crash guard would+ route an exception here to ``fail_pass`` and report the pass errored.+ """+ try:+ pass_result = _applied_result(+ bp, apply_stats, ingestion_drain_status, summary_for_user+ )+ except Exception:+ logger.exception("Failed to build the result of dream pass %s", bp.pass_id)+ return+ if bp.job_id:+ try:+ from .job_status import mark_complete++ await mark_complete(kind="dream_pass", job_id=bp.job_id, result=pass_result)+ except Exception:+ logger.exception("Failed to mark dream pass job %s complete", bp.job_id)+ await record_batch_complete(pass_result, usage)+++async def finalize_stuck_duplicate(bp: BatchPass, ops: DreamOperations) -> None:+ """On a duplicate delivery, finalize the admin job row iff the first+ delivery crashed between apply and ``mark_complete`` and left it+ non-terminal. An already-terminal row is left untouched so the first+ delivery's real apply stats are never overwritten.++ Best-effort: a status read/write failure here must not crash the+ duplicate tail (lock release + cleanup still need to run)."""+ if not bp.job_id:+ return+ try:+ from .job_status import mark_complete, read_status++ existing = await read_status(kind="dream_pass", job_id=bp.job_id)+ if existing is None or existing.state in ("complete", "errored"):+ return+ logger.warning(+ "Duplicate dispatch found job %s stuck in state=%s — "+ "finalizing with the clamped op counts",+ bp.job_id[:12],+ existing.state,+ )+ await mark_complete(+ kind="dream_pass", job_id=bp.job_id, result=_attempted_result(bp, ops)+ )+ except Exception:+ logger.exception(+ "Failed to finalize stuck duplicate for job %s", bp.job_id[:12]+ )+++async def mark_job_errored(job_id: str, error: str, *, dead_end: bool = False) -> None:+ """Close the admin job row errored. Best-effort: status write failures+ are logged, never raised; a *dead_end* (a payload no phase handler can+ take) logs under its own message."""+ if not job_id:+ return+ try:+ from .job_status import mark_errored++ await mark_errored(kind="dream_pass", job_id=job_id, error=error)+ except Exception:+ if dead_end:+ logger.exception("Failed to mark dead-end job %s errored", job_id[:12])+ else:+ logger.exception("Failed to mark dream pass job %s errored", job_id)+++async def _charge_landed_phases(+ bp: BatchPass, state: dict[str, dict[str, Any]]+) -> None:+ """Charge the phases that landed; a failure is logged, never raised."""+ try:+ await log_all_phase_costs(+ user_id=bp.user_id,+ expert_id=bp.expert_id,+ pass_id=bp.pass_id,+ state=state,+ phase_models=bp.phase_models,+ )+ except Exception:+ logger.exception("Dream batch cost log failed for pass=%s", bp.pass_id)+++async def release_lock(bp: BatchPass) -> None:+ """Release the disowned dream lock with the ownership token persisted+ alongside the input bundle. Must run before ``delete_input_bundle`` —+ the token rides on that key. A missing token (bundle TTL'd out,+ malformed payload) leaves the lock for its TTL to clear rather than+ blind-deleting what may be a newer pass's lock.++ Best-effort like ``release_dream_lock`` itself: a Redis blip on the+ token read must not propagate — on the success tail it would fire+ AFTER ``mark_complete`` and the crash guard would rewrite a completed+ job to errored. Falls back to a token-less release (lock TTL)."""+ token: str | None = None+ if bp.pass_id:+ try:+ token = await read_lock_token(bp.pass_id)+ except Exception:+ logger.exception(+ "Failed to read dream lock token for pass=%s — "+ "leaving the lock to its TTL",+ bp.pass_id,+ )+ try:+ scope = MemoryScope.build(bp.user_id, bp.expert_id)+ except ValueError:+ logger.warning(+ "Invalid memory scope for disowned dream lock of user %s — "+ "leaving it for the TTL to clear",+ bp.user_id[:12],+ )+ return+ await release_dream_lock(scope, token)+++def _applied_result(+ bp: BatchPass,+ apply_stats: ApplyStats,+ ingestion_drain_status: IngestionDrainStatus,+ summary_for_user: str,+) -> DreamPassResult:+ """The ``DreamPassResult`` of a batch pass whose apply just returned."""+ raw_session_id = apply_stats.get("session_id")+ return DreamPassResult(+ user_id=bp.user_id,+ pass_id=bp.pass_id,+ execution_path="anthropic_batch",+ consolidated_count=_stat_count(apply_stats, "consolidated_count"),+ proposal_count=_stat_count(apply_stats, "proposal_count"),+ demotion_count=_stat_count(apply_stats, "demotion_count"),+ entity_invalidation_count=_stat_count(apply_stats, "entity_invalidation_count"),+ dropped_forgotten=_stat_count(apply_stats, "dropped_forgotten"),+ pending_writes=_stat_count(apply_stats, "pending_writes"),+ failed_writes=_stat_count(apply_stats, "failed_writes"),+ dream_session_id=raw_session_id if isinstance(raw_session_id, str) else None,+ ingestion_drain_status=ingestion_drain_status,+ operations=_stat_snapshot(apply_stats),+ # Carry the user-facing narrative like the sync path does —+ # without it the Memory Visualizer renders a blank summary for+ # batch-completed dreams even though the session message exists.+ summary_for_user=summary_for_user,+ )+++def _attempted_result(bp: BatchPass, ops: DreamOperations) -> DreamPassResult:+ """The first delivery's per-edge outcomes (and dream session id) died+ with it, so the counts here are the clamped *attempted* ops — annotated+ so the admin UI doesn't present them as confirmed apply results."""+ note = (+ "[finalized after duplicate delivery — writes and proposals were "+ "queued by the original delivery, counted pending; demotion counts "+ "reflect attempted operations] "+ )+ return DreamPassResult(+ user_id=bp.user_id,+ pass_id=bp.pass_id,+ execution_path="anthropic_batch",+ pending_writes=len(ops.writes) + len(ops.proposals),+ demotion_count=len(ops.demotions),+ entity_invalidation_count=len(ops.entity_invalidations),+ # Batch apply never drains in-line by design; the first+ # delivery's writes (if any) landed fire-and-forget. ``skipped``+ # marks this as a healthy by-design skip, NOT a drain failure.+ ingestion_drain_status=IngestionDrainStatus.skipped,+ summary_for_user=note + (ops.summary_for_user or ""),+ )+++def _stat_count(apply_stats: ApplyStats, key: str) -> int:+ """One apply count as a plain ``int``, 0 when missing or malformed: the+ stats values are a union that includes the snapshot."""+ value = apply_stats.get(key)+ if isinstance(value, (int, str)):+ try:+ return int(value)+ except (TypeError, ValueError):+ return 0+ return 0+++def _stat_snapshot(apply_stats: ApplyStats) -> DreamOperationsSnapshot | None:+ raw_snapshot = apply_stats.get("snapshot")+ if isinstance(raw_snapshot, DreamOperationsSnapshot):+ return raw_snapshot+ if isinstance(raw_snapshot, dict):+ return DreamOperationsSnapshot.model_validate(raw_snapshot)+ return None+++def _phase_models(raw: Any) -> dict[str, str]:+ """Per-phase model map persisted by ``submit_phase`` — used to chain+ the next phase and to price each phase with the model it actually+ used. Empty when absent (current code never omits it)."""+ if not isinstance(raw, dict):+ return {}+ return {str(k): str(v) for k, v in raw.items()}diff --git a/autogpt_platform/backend/backend/copilot/dream/batch_outcome_test.py b/autogpt_platform/backend/backend/copilot/dream/batch_outcome_test.pynew file mode 100644index 0000000000..0858df8bae--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/dream/batch_outcome_test.py@@ -0,0 +1,25 @@+from .batch_outcome import BatchPass, _applied_result+from .schemas import IngestionDrainStatus+++def test_applied_result_preserves_recall_write_outcomes():+ result = _applied_result(+ BatchPass(+ user_id="u1", expert_id=None, pass_id="p1", job_id="j1", phase_models={}+ ),+ {+ "consolidated_count": 1,+ "dropped_forgotten": 2,+ "pending_writes": 3,+ "failed_writes": 4,+ },+ IngestionDrainStatus.skipped,+ "done",+ )++ assert result.consolidated_count == 1+ assert (result.dropped_forgotten, result.pending_writes, result.failed_writes) == (+ 2,+ 3,+ 4,+ )diff --git a/autogpt_platform/backend/backend/copilot/dream/batch_state.py b/autogpt_platform/backend/backend/copilot/dream/batch_state.pynew file mode 100644index 0000000000..9910a8dd93--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/dream/batch_state.py@@ -0,0 +1,223 @@+"""The Redis state a batch dream pass keeps between its callbacks, and the+gates that keep its side effects at-most-once.++Per-pass state lives in two Redis keys:++ * ``dream:batch:input:{pass_id}`` — the serialized ``DreamInput`` (so each+ phase's prompt is rebuilt without re-fetching from Postgres / FalkorDB)+ plus the dream lock's ownership token for the compare-and-delete release;+ written and read by ``batch_submit.py``+ * ``dream:batch:state:{pass_id}`` — accumulated phase outputs + per-phase+ token usage, so the apply step has everything it needs and the cost log+ can record all three rows at once++Both are TTL'd to 24h (Anthropic's batch SLA) so a forgotten pass naturally+falls off the radar. Two SETNX gates (7-day TTL) keep the side effects+at-most-once should a finished batch be delivered twice:+``dream:applied:{pass_id}`` for the memory writes and+``dream:batch:costs_logged:{pass_id}`` for billing.+"""++from __future__ import annotations++import json+import logging+from collections.abc import Awaitable+from typing import TYPE_CHECKING, Any, Literal, cast++from .batch_submit import delete_input_bundle+from .schemas import DreamPhase++if TYPE_CHECKING:+ from backend.util.llm.providers import BatchResultRow++logger = logging.getLogger(__name__)++# 24h matches Anthropic's batch SLA; if no phase has landed in that+# window the BatchExecutor has already issued a timeout error via+# ``MAX_BATCH_LIFETIME_SECONDS``.+STATE_TTL_SECONDS = 24 * 60 * 60++_APPLIED_GATE_PREFIX = "dream:applied"+# 7 days — same window as the costs_logged gate; no realistic+# BatchExecutor re-dispatch (poll backoff caps at 5 min, max lifetime+# 24h) can outlive it.+_APPLIED_GATE_TTL_SECONDS = 7 * 24 * 60 * 60++_COSTS_LOGGED_PREFIX = "dream:batch:costs_logged"+# 7 days — long enough that no realistic BatchExecutor re-dispatch+# (poll backoff caps at 5 min, max lifetime 24h) can slip through and+# bill twice. Matches the spirit of the Stripe-reconcile gate's TTL.+_COSTS_LOGGED_TTL_SECONDS = 7 * 24 * 60 * 60+++def state_key(pass_id: str) -> str:+ return f"dream:batch:state:{pass_id}"+++async def read_state(pass_id: str) -> dict[str, dict[str, Any]]:+ """Per-pass accumulator: phase → {content, usage tokens, error}.++ Returns a ``dict[str, ...]`` rather than ``dict[DreamPhase, ...]``+ because Redis hash keys are plain strings and we lose the+ ``Literal`` narrowing the moment we read them back. Callers+ re-narrow at the boundary (e.g. via ``NEXT_PHASE`` lookups) when+ they need the phase ordering.+ """+ from backend.data.redis_client import get_redis_async++ redis = await get_redis_async()+ raw = await cast("Awaitable[dict[Any, Any]]", redis.hgetall(state_key(pass_id)))+ out: dict[str, dict[str, Any]] = {}+ for phase, body in (raw or {}).items():+ if isinstance(phase, bytes):+ phase = phase.decode("utf-8")+ if isinstance(body, bytes):+ body = body.decode("utf-8")+ try:+ out[phase] = json.loads(body)+ except Exception:+ logger.warning("Corrupted state row for pass=%s phase=%s", pass_id, phase)+ return out+++async def read_state_or_none(pass_id: str) -> dict[str, dict[str, Any]] | None:+ """The pass's state, or ``None`` when it cannot be read, for a caller+ that only reports on the pass (the usage its record carries) and must+ go on without it."""+ try:+ return await read_state(pass_id)+ except Exception:+ logger.warning(+ f"Dream pass {pass_id}: could not read its batch state for the record",+ exc_info=True,+ )+ return None+++async def write_phase_to_state(+ *, pass_id: str, phase: DreamPhase, row: BatchResultRow+) -> None:+ from backend.data.redis_client import get_redis_async++ redis = await get_redis_async()+ body = json.dumps(+ {+ "custom_id": row.custom_id,+ "content": row.content,+ "input_tokens": row.input_tokens,+ "output_tokens": row.output_tokens,+ "cache_read_tokens": row.cache_read_tokens,+ "cache_creation_tokens": row.cache_creation_tokens,+ "error": row.error,+ }+ )+ await cast("Awaitable[int]", redis.hset(state_key(pass_id), phase, body))+ await redis.expire(state_key(pass_id), STATE_TTL_SECONDS)+++async def delete_state(pass_id: str) -> None:+ from backend.data.redis_client import get_redis_async++ redis = await get_redis_async()+ await redis.delete(state_key(pass_id))+++def content_for(state: dict[str, dict[str, Any]], phase: str) -> str | None:+ row = state.get(phase)+ if row is None:+ return None+ content = row.get("content")+ return content if isinstance(content, str) else None+++async def best_effort_cleanup(pass_id: str) -> None:+ """Delete the per-pass state + input bundle without letting a Redis+ blip propagate. These deletes run AFTER ``mark_complete`` on the+ success/duplicate tails — an exception here would route through the+ crash guard to ``fail_pass`` and rewrite a completed job to errored.+ Both keys carry 24h TTLs, so a failed delete self-heals."""+ try:+ await delete_state(pass_id)+ await delete_input_bundle(pass_id)+ except Exception:+ logger.exception(+ "Per-pass cleanup failed for pass=%s — keys will expire via TTL",+ pass_id,+ )+++async def claim_apply_gate(pass_id: str) -> Literal["claimed", "duplicate", "error"]:+ """Atomically claim the per-pass apply gate.++ Returns ``"claimed"`` when this delivery is the first to run+ ``apply_operations`` for the pass, ``"duplicate"`` when an earlier+ delivery already claimed the gate, and ``"error"`` when Redis is+ unavailable and we cannot tell which of the two we are. A duplicate+ proves only that earlier claim, not that its writes landed: a delivery+ that died between its claim and the end of apply leaves the gate+ claimed and its writes partial.++ The BatchExecutor claims each finished batch atomically before it+ dispatches it (``BatchExecutor._claim_dispatch``), so the same result+ comes back only through a re-enqueued pending entry. The gate keeps the+ memory mutation at-most-once even then: ``apply_operations`` writes+ every consolidated fact and proposal to the user's graph as fresh+ episodes, so running it twice duplicates the user's memories. The three+ states must stay distinct: treating a Redis brown-out as a duplicate+ would mark the job complete with zero writes, silently dropping the+ dream.+ """+ from backend.data.redis_client import get_redis_async++ try:+ redis = await get_redis_async()+ claimed = await redis.set(+ f"{_APPLIED_GATE_PREFIX}:{pass_id}",+ "1",+ nx=True,+ ex=_APPLIED_GATE_TTL_SECONDS,+ )+ return "claimed" if claimed else "duplicate"+ except Exception:+ logger.exception(+ "Failed to claim apply gate for pass=%s — failing pass",+ pass_id,+ )+ return "error"+++async def claim_costs_logged_gate(pass_id: str) -> bool:+ """Atomically claim the per-pass cost-charge gate. Returns True+ when this caller won the race (first time costs_logged is set);+ False when an earlier caller already claimed it, which does not+ prove the pass was charged: that caller's accounting may have+ failed part-way, or Redis may have failed this claim.++ Modelled on ``rate_limit._maybe_reconcile_stripe_tier`` — Redis+ SETNX with a long TTL is the established convention for "do this+ side-effect at most once per identifier" in this codebase. The+ dedup lives at the dream-batch boundary, not inside+ ``record_cost_usage`` itself (chat legitimately charges every turn).+ """+ from backend.data.redis_client import get_redis_async++ try:+ redis = await get_redis_async()+ return bool(+ await redis.set(+ f"{_COSTS_LOGGED_PREFIX}:{pass_id}",+ "1",+ nx=True,+ ex=_COSTS_LOGGED_TTL_SECONDS,+ )+ )+ except Exception:+ # Fail closed: if we can't claim the gate, do not charge.+ # Better to under-bill on a Redis brown-out than risk+ # double-billing under retry pressure.+ logger.exception(+ "Failed to claim costs_logged gate for pass=%s — skipping charge",+ pass_id,+ )+ return Falsediff --git a/autogpt_platform/backend/backend/copilot/dream/batch_submit.py b/autogpt_platform/backend/backend/copilot/dream/batch_submit.pyindex d97ca8c879..9ec029af10 100644--- a/autogpt_platform/backend/backend/copilot/dream/batch_submit.py+++ b/autogpt_platform/backend/backend/copilot/dream/batch_submit.py@@ -20,7 +20,6 @@ from __future__ import annotationsimport jsonimport logging-from datetime import datetimefrom typing import TYPE_CHECKING, Literalfrom pydantic import BaseModel@@ -47,6 +46,7 @@ from backend.util.llm.tool_use import ()from .fetch import DreamInput+from .input_bundle import input_bundle_from_dict, input_bundle_to_dictfrom .locks import read_dream_lock_tokenfrom .prompts import (build_consolidate_prompt,@@ -371,7 +371,7 @@ async def persist_input_bundle(from backend.data.redis_client import get_redis_asyncredis = await get_redis_async()- payload = _input_bundle_to_dict(input_bundle)+ payload = input_bundle_to_dict(input_bundle)if lock_token is None:lock_token = await read_dream_lock_token(MemoryScope.build(input_bundle.user_id, input_bundle.expert_id)@@ -445,7 +445,7 @@ async def read_input_bundle(pass_id: str) -> DreamInput | None:if isinstance(raw, bytes):raw = raw.decode("utf-8")try:- return _dict_to_input_bundle(json.loads(raw))+ return input_bundle_from_dict(json.loads(raw))except Exception:logger.exception("Corrupted DreamInput in Redis for pass=%s — bailing", pass_id)return None@@ -456,78 +456,3 @@ async def delete_input_bundle(pass_id: str) -> None:redis = await get_redis_async()await redis.delete(input_bundle_key(pass_id))---def _input_bundle_to_dict(input_bundle: DreamInput) -> dict:- return {- "user_id": input_bundle.user_id,- "expert_id": input_bundle.expert_id,- "group_id": input_bundle.group_id,- "window_start": input_bundle.window_start.isoformat(),- "window_end": input_bundle.window_end.isoformat(),- "episodes": [- {- "uuid": e.uuid,- "name": e.name,- "content": e.content,- "source_description": e.source_description,- "valid_at": e.valid_at,- "created_at": e.created_at,- }- for e in input_bundle.episodes- ],- "facts": [- {- "uuid": f.uuid,- "source": f.source,- "target": f.target,- "name": f.name,- "fact": f.fact,- "scope": f.scope,- "confidence": f.confidence,- "status": f.status,- "created_at": f.created_at,- }- for f in input_bundle.facts- ],- "recent_sessions": [- {- "session_id": s.session_id,- "title": s.title,- "created_at": s.created_at.isoformat() if s.created_at else None,- "body": s.body,- }- for s in input_bundle.recent_sessions- ],- "known_fact_uuids": list(input_bundle.known_fact_uuids),- "known_episode_uuids": list(input_bundle.known_episode_uuids),- }---def _dict_to_input_bundle(data: dict) -> DreamInput:- from .fetch import EpisodeRow, FactRow, SessionRow-- return DreamInput(- user_id=data["user_id"],- expert_id=data.get("expert_id"),- group_id=data["group_id"],- window_start=datetime.fromisoformat(data["window_start"]),- window_end=datetime.fromisoformat(data["window_end"]),- episodes=[EpisodeRow(**e) for e in data.get("episodes") or []],- facts=[FactRow(**f) for f in data.get("facts") or []],- recent_sessions=[- SessionRow(- session_id=s["session_id"],- title=s.get("title"),- created_at=(- datetime.fromisoformat(s["created_at"])- if s.get("created_at")- else None- ),- body=s.get("body") or "",- )- for s in data.get("recent_sessions") or []- ],- known_fact_uuids=set(data.get("known_fact_uuids") or []),- known_episode_uuids=set(data.get("known_episode_uuids") or []),- )diff --git a/autogpt_platform/backend/backend/copilot/dream/billing.py b/autogpt_platform/backend/backend/copilot/dream/billing.pyindex 4dd3b6ca86..916d474c30 100644--- a/autogpt_platform/backend/backend/copilot/dream/billing.py+++ b/autogpt_platform/backend/backend/copilot/dream/billing.py@@ -28,6 +28,8 @@ for the phases that completed before the error. The failed phase isabsorbed on both sync and batch paths, including truncated or invalidstructured output. Its usage stays on the error for telemetry, but itsprovider spend is not written to a cost row.+A charge that itself fails raises :class:`PhaseChargeError` carrying+the completed phase's priced usage so the pass still reports it."""from __future__ import annotations@@ -37,7 +39,7 @@ from typing import Literalfrom backend.copilot.config import ChatConfigfrom backend.copilot.inference.context import InferenceContext, InferenceUsage-from backend.copilot.inference.record import record+from backend.copilot.inference.record import price, recordfrom backend.copilot.rate_limit import (RateLimitExceeded,RateLimitUnavailable,@@ -49,6 +51,16 @@ from backend.copilot.rate_limit import (logger = logging.getLogger(__name__)+class PhaseChargeError(Exception):+ """Charging a phase the provider already billed failed. Carries the+ phase's usage, priced, so the caller still reports what was billed; the+ message is the charge's own, and the charge's error is the cause."""++ def __init__(self, usage: InferenceUsage, cause: Exception) -> None:+ super().__init__(str(cause))+ self.usage = usage++DreamBudgetSkipReason = Literal["insufficient_credits","rate_limit_unavailable",@@ -134,13 +146,22 @@ async def record_phase_cost(Returns *usage* as priced. No row and no charge for a phase withneither tokens nor a cost; tokens without a cost (a model with nocatalog price) still log but don't charge the rate-limit counter.++ The price is read before the charge. Charging can fail after the+ provider has billed the call (the rate-limit counters, the trial-cost+ ledger, the cost log); that raises ``PhaseChargeError`` carrying the+ priced usage, never swallowed and never retried here."""- return await record(- ctx,- usage,- block_name=f"copilot:dream:{ctx.job.phase}",- metadata={- "dream_pass_id": ctx.job.correlation_id,- "dream_phase": ctx.job.phase,- },- )+ priced = price(usage, ctx.route)+ try:+ return await record(+ ctx,+ usage,+ block_name=f"copilot:dream:{ctx.job.phase}",+ metadata={+ "dream_pass_id": ctx.job.correlation_id,+ "dream_phase": ctx.job.phase,+ },+ )+ except Exception as exc:+ raise PhaseChargeError(priced, exc) from excdiff --git a/autogpt_platform/backend/backend/copilot/dream/billing_test.py b/autogpt_platform/backend/backend/copilot/dream/billing_test.pyindex a84920417c..cc56865282 100644--- a/autogpt_platform/backend/backend/copilot/dream/billing_test.py+++ b/autogpt_platform/backend/backend/copilot/dream/billing_test.py@@ -259,6 +259,27 @@ async def test_record_phase_cost_prices_an_unpriced_phase_from_the_catalog(routeassert priced.cost_source == "catalog"+async def test_a_charge_that_fails_raises_with_the_phases_usage_priced():+ """The provider billed the phase before charging it failed: the error+ carries the usage priced as its row would have been, with the charge's+ error as its cause, and the charge is not retried."""+ down = ConnectionError("trial cost database unavailable")+ persist = AsyncMock(side_effect=down)+ with patch(_PERSIST, new=persist), pytest.raises(+ billing_mod.PhaseChargeError+ ) as raised:+ await billing_mod.record_phase_cost(+ _ctx(route=_sync_route("anthropic")),+ _usage("claude-sonnet-5", **_SONNET_5_TOKENS),+ )+ assert str(raised.value) == "trial cost database unavailable"+ assert raised.value.__cause__ is down+ assert raised.value.usage.cost_usd == pytest.approx(_SONNET_5_LIST_COST)+ assert raised.value.usage.cost_source == "catalog"+ persist.assert_awaited_once()++@pytest.mark.asyncioasync def test_record_phase_cost_leaves_an_unpriced_model_unknown():"""No catalog price means an unknown cost, never a zero one: the rowdiff --git a/autogpt_platform/backend/backend/copilot/dream/clamp.py b/autogpt_platform/backend/backend/copilot/dream/clamp.pynew file mode 100644index 0000000000..488eb4e0d6--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/dream/clamp.py@@ -0,0 +1,192 @@+"""Hard limits on what one dream pass may write, whatever the sanitizer+emitted.++The phase 3 prompt asks for these caps, but the model can still over-emit, so+both routes clamp its ``DreamOperations`` in code before apply runs: the sync+orchestrator and the batch callbacks call ``clamp_operations``. On the way it+drops "transient intent" (questions captured as facts) and collapses+near-duplicate writes (``dedup.py``).+"""++from __future__ import annotations++import logging+import re+from collections.abc import Sequence+from typing import TypeVar++from .dedup import dedupe_near_duplicate_writes+from .prompts import MAX_DEMOTIONS_PER_PASS, MAX_PROPOSALS_PER_PASS, MAX_WRITES_PER_PASS+from .schemas import ConsolidatedFact, DreamOperations, ProposedFinding++logger = logging.getLogger(__name__)+++# Entity invalidation is the most destructive op the sanitizer can emit:+# each one single-hop demotes EVERY :RELATES_TO edge on the entity, so a+# hub entity multiplies the blast radius far past the demotion caps. We+# cap the *count* of invalidations per pass here; the per-entity edge+# blast radius is bounded by the ``DREAM_PASS_INVALIDATE_ENTITY`` LD flag+# (staged rollout, off by default) plus the single-hop-only guarantee in+# ``invalidate_entity_direct_neighbors`` (no multi-hop propagation).+# Deliberately NOT degree-aware — fetching entity degrees is async graph+# work that doesn't belong in this sync clamp.+MAX_ENTITY_INVALIDATIONS_PER_PASS = 2+++# High-precision filter for "transient intent" facts — content that+# records what the user is ASKING/wants to KNOW rather than a durable+# fact about them. The sanitize prompt is told to drop these, but+# prompt-only sanitization leaks them (#13388: "User is asking how+# Kubernetes works", "User is interested in knowing which PRs are open"),+# so this is a deterministic belt-and-suspenders gate.+#+# Deliberately NARROW to knowledge-seeking intent. We do NOT match goals+# like "user wants to create/build X" — those are legitimate durable+# memories. Generic world-knowledge pollution ("Kubernetes uses pods…")+# is left to the sanitize prompt: it needs LLM judgment (is the subject+# the user?) that a regex can't do without false-positives.+#+# Interrogative complements are the sharp edge — several verbs are durable+# aspirations on their own but transient questions once they take a+# question word:+# * ``asking`` — "asking FOR weekly reports" / "asking the agent TO+# monitor PRs" are durable requests (semantically like "wants X"), so+# only interrogative ``asking HOW/WHAT/…`` counts as transient.+# * ``learn``/``understand``/``find out`` — "wants to learn Spanish",+# "wants to understand distributed systems", "wants to find out about+# new markets" are durable skill/aspiration GOALS; they only read as+# transient with an interrogative ("wants to learn HOW X works").+# * ``curious`` — "curious ABOUT X" is transient, but "curious by nature"+# is a durable personality trait, so a ``curious about`` complement is+# required (mirroring ``confused about``/``unsure about``).+# ``know`` stays complement-free: "wants to know X" is transient curiosity+# regardless of phrasing (there is no durable "wants to know" aspiration —+# that role is served by ``learn``).+# Known limitation (nice-to-have, low frequency): a standing notification+# preference phrased "wants to know when X happens" is dropped; separating+# it from a one-off "wants to know when the deploy is" isn't reliably+# regex-able, so it's left to the sanitize prompt + human review.+#+# Subject scope: the gate deliberately anchors on the generic ``user``+# subject only. Name-phrased transient intent ("Nick is asking how the+# auth flow works") is intentionally NOT matched here — broadening the+# subject to arbitrary proper nouns would risk false-positives on+# non-user entities ("Kubernetes is asking for more nodes"), so+# name-first phrasing is left to the sanitize prompt's LLM judgment. The+# leading auxiliary allows perfect-progressive forms ("has been asking").+_TRANSIENT_INTENT_RE = re.compile(+ r"^(the\s+)?user\s+(?:(?:is|has|was)\s+(?:been\s+)?)?"+ r"(asking\s+(how|what|why|whether|if|when|where|who|which|about)\b"+ r"|wondering\b"+ r"|curious\s+about\b"+ r"|confused\s+about\b"+ r"|unsure\s+about\b"+ r"|trying\s+to\s+understand\b"+ r"|interested\s+in\s+(knowing|understanding)\b"+ r"|interested\s+in\s+learning\s+(how|what|why|whether|if|when|where)\b"+ r"|wants?\s+to\s+know\b"+ r"|wants?\s+to\s+(understand|find\s+out)\s+(how|what|why|whether|if|when|where)\b"+ r"|wants?\s+to\s+learn\s+(how|what|why|whether|if|when|where)\b"+ r"|asked\s+(how|what|why|whether|if|when|where|about)\b)",+ re.IGNORECASE,+)+++def _is_transient_intent(content: str) -> bool:+ """True when ``content`` reads as a question/knowledge-seeking intent+ rather than a durable fact about the user."""+ return bool(_TRANSIENT_INTENT_RE.match(content.strip()))+++_ContentItem = TypeVar("_ContentItem", ConsolidatedFact, ProposedFinding)+++def _drop_transient_intent(+ items: Sequence[_ContentItem],+) -> tuple[list[_ContentItem], int]:+ """Filter ConsolidatedFact / ProposedFinding items whose ``.content``+ is a transient intent. Returns (kept, dropped_count)."""+ kept = [it for it in items if not _is_transient_intent(it.content)]+ return kept, len(items) - len(kept)+++def clamp_operations(+ ops: DreamOperations,+ active_fact_count: int,+ known_fact_uuids: set[str] | None = None,+) -> DreamOperations:+ """Hard-trim oversized phase 3 outputs.++ Phase 3's prompt asks for these caps but the model can still+ over-emit. The orchestrator enforces them in code so apply.py+ never writes more than the policy allows.++ Demotions carry a second ceiling — 5% of the active fact set — so a+ single pass can never wipe a meaningful fraction of a user's memory+ even if the absolute ``MAX_DEMOTIONS_PER_PASS`` cap would allow it.+ The 5% ceiling floors at 1 when there is at least one active fact:+ small graphs (< 20 facts) would otherwise round to a cap of 0 and+ never get even a single contradicted fact demoted.+ ``active_fact_count < 0`` means the count is unknown (the batch path+ lost its persisted input bundle); fall back to the absolute cap only+ rather than silently dropping every demotion.++ When ``known_fact_uuids`` is provided, demotions targeting uuids+ outside it are dropped BEFORE the cap slice — otherwise a+ hallucinated uuid at the head of the model's list consumes a cap+ slot (the entire budget on a floor-of-1 small graph) and displaces+ a valid demotion that apply.py would have accepted. ``None`` skips+ the pre-filter; apply.py's idempotent known-uuid filter remains the+ security chokepoint either way.++ Entity invalidations are count-capped at+ ``MAX_ENTITY_INVALIDATIONS_PER_PASS``; see the constant's comment+ for why the per-entity edge blast radius is bounded elsewhere (LD+ flag + single-hop guarantee), not here.+ """+ demotions = ops.demotions+ if known_fact_uuids is not None:+ demotions = [d for d in demotions if d.edge_uuid in known_fact_uuids]+ dropped = len(ops.demotions) - len(demotions)+ if dropped:+ logger.warning(+ "Dream clamp: dropped %d demotion(s) targeting edge uuids "+ "outside known_fact_uuids before applying the demotion cap",+ dropped,+ )+ demotion_cap = MAX_DEMOTIONS_PER_PASS+ if active_fact_count == 0:+ demotion_cap = 0+ elif active_fact_count > 0:+ demotion_cap = min(MAX_DEMOTIONS_PER_PASS, max(1, active_fact_count * 5 // 100))++ # Drop transient-intent pollution ("user is asking…") before the cap+ # slice so a leaked question never displaces a real fact (#13388).+ writes, w_intent_dropped = _drop_transient_intent(ops.writes)+ proposals, p_intent_dropped = _drop_transient_intent(ops.proposals)+ if w_intent_dropped or p_intent_dropped:+ logger.info(+ "Dream clamp: dropped %d transient-intent write(s) and %d "+ "proposal(s) (questions captured as facts)",+ w_intent_dropped,+ p_intent_dropped,+ )+ # Collapse intra-pass near-duplicate writes before the cap slice so the+ # cap counts distinct facts, not paraphrases of one (#13387).+ writes, w_dup_dropped = dedupe_near_duplicate_writes(writes)+ if w_dup_dropped:+ logger.info(+ "Dream clamp: collapsed %d near-duplicate write(s) into their "+ "canonical (longest) phrasing",+ w_dup_dropped,+ )+ return DreamOperations(+ writes=writes[:MAX_WRITES_PER_PASS],+ proposals=proposals[:MAX_PROPOSALS_PER_PASS],+ demotions=demotions[:demotion_cap],+ entity_invalidations=ops.entity_invalidations[+ :MAX_ENTITY_INVALIDATIONS_PER_PASS+ ],+ summary_for_user=ops.summary_for_user,+ )diff --git a/autogpt_platform/backend/backend/copilot/dream/conftest.py b/autogpt_platform/backend/backend/copilot/dream/conftest.pyindex 4bfb5f3243..4c80a41677 100644--- a/autogpt_platform/backend/backend/copilot/dream/conftest.py+++ b/autogpt_platform/backend/backend/copilot/dream/conftest.py@@ -10,15 +10,32 @@ Without the server there is no Redis either, and ``get_redis_async`` retriesfor close to an hour before giving up, so every test in this directory gets anin-memory stand-in by default. Tests that need their own fake keep patching``backend.data.redis_client.get_redis_async`` as before; a later patch wins.++No Postgres either, and the DatabaseManager RPC client behind ``dream_db()``+would stall a write until the store's deadline, so the dream store writes+each pass's record to an in-memory ``FakeDreamDb`` that tests can read the+transitions back from."""from __future__ import annotations+import asyncio+from datetime import datetime, timezone+from enum import Enumfrom typing import Anyimport pytestimport pytest_asyncio+from backend.data.dream_pass_models import (+ OPEN_STATUSES,+ DreamPassDraft,+ DreamPassOperations,+ DreamPassRecord,+ DreamPassUpdate,+ DreamPhaseOutputs,+)+@pytest_asyncio.fixture(scope="session", loop_scope="session")async def server():@@ -164,6 +181,138 @@ class FakeAsyncRedis:return None+class FakeDreamDb:+ """In-memory stand-in for ``backend.data.dream_pass`` behind ``dream_db()``.++ Applies an update by the rules of ``backend/data/dream_pass_update.py``:+ a ``None`` field leaves its column, status and phase only move forward,+ a batch id lands only while the row has not moved past the update's+ phase, ``phase_outputs`` and ``operations`` merge one field at a time,+ and a terminal row is final. ``writes`` holds every draft and update+ that reached an open row, in order, as sent. ``fail`` makes every call+ raise, as an unreachable database would.+ """++ def __init__(self) -> None:+ self.rows: dict[str, dict[str, Any]] = {}+ self.writes: list[tuple[str, DreamPassDraft | DreamPassUpdate]] = []+ self.fail = False++ async def create_dream_pass(self, draft: DreamPassDraft) -> None:+ self._raise_if_down()+ if draft.id in self.rows:+ raise ValueError(f"duplicate dream pass {draft.id}")+ self.writes.append((draft.id, draft))+ self.rows[draft.id] = {**dict(draft), "phase_outputs": {}, "operations": {}}++ async def update_dream_pass(self, pass_id: str, update: DreamPassUpdate) -> bool:+ self._raise_if_down()+ row = self.rows.get(pass_id)+ if row is None or row["status"] not in OPEN_STATUSES:+ return False+ self.writes.append((pass_id, update))+ if update.provider_batch_id is not None and _order(row["phase"]) <= _order(+ update.phase+ ):+ row["provider_batch_id"] = update.provider_batch_id+ if update.status is not None and _order(update.status) > _order(row["status"]):+ row["status"] = update.status+ if update.phase is not None and _order(update.phase) > _order(row["phase"]):+ row["phase"] = update.phase+ for field, value in dict(update).items():+ if value is None or field in ("status", "phase", "provider_batch_id"):+ continue+ if field in ("phase_outputs", "operations"):+ row[field].update(+ {k: v for k, v in dict(value).items() if v is not None}+ )+ else:+ row[field] = value+ return True++ def seed(self, draft: DreamPassDraft) -> None:+ """A row as an earlier step (another process) would have left it."""+ self.rows[draft.id] = {**dict(draft), "phase_outputs": {}, "operations": {}}++ def statuses(self, pass_id: str) -> list[Any]:+ return [w.status for pid, w in self.writes if pid == pass_id and w.status]++ def phases(self, pass_id: str) -> list[Any]:+ """The steps the row went through, a step written twice in a row once."""+ steps = [w.phase for pid, w in self.writes if pid == pass_id and w.phase]+ return [s for i, s in enumerate(steps) if i == 0 or s != steps[i - 1]]++ def record(self, pass_id: str) -> DreamPassRecord:+ """The row as ``get_dream_pass`` would read it back."""+ row = self.rows[pass_id]+ stamp = row.get("started_at") or datetime.now(timezone.utc)+ return DreamPassRecord.model_validate(+ {+ **{name: row.get(name) for name in DreamPassRecord.model_fields},+ "cancel_generation": 0,+ "created_at": stamp,+ "updated_at": stamp,+ "phase_outputs": DreamPhaseOutputs(**row["phase_outputs"]),+ "operations": DreamPassOperations(**row["operations"]),+ }+ )++ def _raise_if_down(self) -> None:+ if self.fail:+ raise ConnectionError("dream pass database unreachable")+++def _order(value: Enum | None) -> int:+ """Where *value* sits in its enum's declared order, as Postgres compares+ enums."""+ assert value is not None+ return list(type(value)).index(value)++[email protected](autouse=True)+def fake_dream_db(monkeypatch: pytest.MonkeyPatch) -> FakeDreamDb:+ """Give every dream test an in-memory pass record behind the store."""+ fake = FakeDreamDb()+ monkeypatch.setattr("backend.copilot.dream.store.dream_db", lambda: fake)+ return fake+++class StalledDreamDb:+ """A DatabaseManager that never answers: every record write hangs until+ the store's deadline gives up on it and cancels it."""++ def __init__(self) -> None:+ self.started = 0+ self.cancelled = 0++ async def create_dream_pass(self, draft: DreamPassDraft) -> None:+ await self._hang()++ async def update_dream_pass(self, pass_id: str, update: DreamPassUpdate) -> bool:+ await self._hang()+ return True++ async def _hang(self) -> None:+ self.started += 1+ try:+ await asyncio.Event().wait()+ except asyncio.CancelledError:+ self.cancelled += 1+ raise+++def stalled_dream_db(monkeypatch: pytest.MonkeyPatch) -> StalledDreamDb:+ """Put a DatabaseManager that never answers behind the store, with a+ short write deadline so a test sees it expire."""+ stalled = StalledDreamDb()+ monkeypatch.setattr("backend.copilot.dream.store.dream_db", lambda: stalled)+ monkeypatch.setattr(+ "backend.copilot.dream.store.RECORD_WRITE_TIMEOUT_SECONDS", 0.05+ )+ return stalled++@pytest.fixture(autouse=True)def fake_dream_redis(monkeypatch: pytest.MonkeyPatch) -> FakeAsyncRedis:"""Give every dream test an in-memory Redis unless it patches its own."""diff --git a/autogpt_platform/backend/backend/copilot/dream/dedup.py b/autogpt_platform/backend/backend/copilot/dream/dedup.pynew file mode 100644index 0000000000..9a46d9cb27--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/dream/dedup.py@@ -0,0 +1,228 @@+"""Collapse near-duplicate writes within one dream pass (#13387)."""++from __future__ import annotations++import re++from .schemas import ConsolidatedFact++# Intra-pass near-duplicate write rejection (#13387). The consolidate+# prompt asks the model to merge near-duplicates, but a single pass still+# emits the same fact phrased 2-3 ways ("Nick uses Terminus on iPhone for+# CLI work" / "Nick wants Terminus on iPhone to show more ASCII"). This is+# a CONSERVATIVE, deterministic backstop: it only collapses writes that are+# near-identical by content-word overlap, keeping the longest (most+# specific) of each cluster. It deliberately does NOT do semantic+# (embedding) merging or compare against the user's EXISTING active facts —+# both need real-data threshold tuning + edge-merge design and are tracked+# as the follow-up P2 dedup pass. High threshold so distinct facts about+# the same entity ("prefers Python" vs "prefers Rust") are never merged.+_DEDUP_JACCARD_THRESHOLD = 0.7+# A short fact whose content words are (nearly) all contained in a longer+# fact is a duplicate the longer one subsumes — even when the longer fact's+# extra detail drags Jaccard below the threshold ("Nick uses Terminus on+# iPhone for CLI" ⊂ "…for CLI and wants more ASCII"). Set high (0.9): for+# facts under ~10 words it effectively requires the shorter to be a strict+# SUBSET of the longer, so a single distinguishing word keeps them apart+# ("deployed the AUTH service" vs "deployed the BILLING service" → 4/5=0.8,+# NOT merged). Only applied when the smaller fact has enough words to be+# specific, so 1-2 word fragments don't spuriously match.+_DEDUP_CONTAINMENT_THRESHOLD = 0.9+_DEDUP_CONTAINMENT_MIN_TOKENS = 3+# Facts with IDENTICAL content-word sets can still describe different+# events when word order differs ("Alice introduced Bob to Carol" vs+# "Alice introduced Carol to Bob"). Equal token sets therefore also+# require agreement on adjacent-word bigrams before merging.+_DEDUP_BIGRAM_THRESHOLD = 0.6+# A negation marker present in one fact but not the other flips the+# meaning ("uses vim" vs "never uses vim") — never merge across it.+# A contraction's "n't" is folded into its word before the split+# (``_CONTRACTED_NOT``: "can't" and "can’t" read "cant"), so the folded+# forms are the markers; split, "can't" would read "can" and "t", neither+# of them a marker, and "can't use vim" would merge into "can use vim".+# The bare stems ("doesn") cover a contraction spelled with a space. A+# false veto only ever KEEPS both facts, which is the conservative+# direction.+_CONTRACTED_NOT = re.compile(r"n['’]t\b")+_DEDUP_NEGATION_MARKERS = frozenset(+ {+ "no",+ "not",+ "never",+ "none",+ "nor",+ "cannot",+ "nt",+ "dont",+ "don",+ "doesnt",+ "doesn",+ "didnt",+ "didn",+ "isnt",+ "isn",+ "wasnt",+ "wasn",+ "werent",+ "weren",+ "wont",+ "cant",+ "aint",+ "shant",+ "mustnt",+ "neednt",+ "mightnt",+ "hasnt",+ "hasn",+ "havent",+ "haven",+ "hadnt",+ "hadn",+ "wouldnt",+ "wouldn",+ "shouldnt",+ "shouldn",+ "couldnt",+ "couldn",+ "arent",+ "aren",+ }+)+_DEDUP_STOPWORDS = frozenset(+ {+ "a",+ "an",+ "the",+ "to",+ "of",+ "and",+ "or",+ "is",+ "are",+ "was",+ "were",+ "for",+ "on",+ "in",+ "at",+ "by",+ "with",+ "that",+ "this",+ "it",+ "their",+ "they",+ "them",+ "his",+ "her",+ "its",+ "as",+ "be",+ "has",+ "have",+ "had",+ "s",+ "so",+ "more",+ "user",+ "users",+ }+)+++def _content_tokens(content: str) -> frozenset[str]:+ """Lowercased alphanumeric content words, minus a small stopword set.+ ``user`` is a stopword because nearly every fact begins with it."""+ return frozenset(t for t in _words(content) if t not in _DEDUP_STOPWORDS)+++def _word_bigrams(content: str) -> frozenset[tuple[str, str]]:+ """Adjacent word pairs over the full (unfiltered) word sequence — the+ order-sensitive signal used when two facts' token sets are equal."""+ words = _words(content)+ return frozenset(zip(words, words[1:]))+++def _words(content: str) -> list[str]:+ """The lowercased alphanumeric words of *content*, each contraction's+ "n't" folded into its word first."""+ return re.findall(r"[a-z0-9]+", _CONTRACTED_NOT.sub("nt", content.lower()))+++def _near_duplicate(+ a: frozenset[str],+ b: frozenset[str],+ a_bigrams: frozenset[tuple[str, str]],+ b_bigrams: frozenset[tuple[str, str]],+) -> bool:+ """Two content-word sets are near-duplicate when their Jaccard overlap+ is high, OR the smaller (sufficiently specific) set is nearly contained+ in the larger one. A negation marker on only one side always vetoes;+ identical sets additionally need word-order (bigram) agreement."""+ if not a or not b:+ return False+ if _DEDUP_NEGATION_MARKERS & (a ^ b):+ return False+ if a == b:+ bigram_union = len(a_bigrams | b_bigrams)+ if not bigram_union:+ return True+ return len(a_bigrams & b_bigrams) / bigram_union >= _DEDUP_BIGRAM_THRESHOLD+ inter = len(a & b)+ union = len(a | b)+ if union and inter / union >= _DEDUP_JACCARD_THRESHOLD:+ return True+ smaller = min(len(a), len(b))+ if smaller < _DEDUP_CONTAINMENT_MIN_TOKENS:+ return False+ return inter / smaller >= _DEDUP_CONTAINMENT_THRESHOLD+++def dedupe_near_duplicate_writes(+ writes: list[ConsolidatedFact],+) -> tuple[list[ConsolidatedFact], int]:+ """Collapse near-duplicate writes, keeping the longest of each cluster.++ Greedy by content length (desc) so the most specific phrasing survives;+ output preserves the writes' original order. Writes are only compared+ within the same ``scope`` (facts must never merge across scopes), and a+ dropped write's ``source_episode_uuids`` are unioned into its cluster's+ survivor so provenance is never lost. Returns (kept, dropped)."""+ by_len_desc = sorted(+ range(len(writes)), key=lambda i: len(writes[i].content), reverse=True+ )+ tokens = [_content_tokens(w.content) for w in writes]+ bigrams = [_word_bigrams(w.content) for w in writes]+ kept_idx: list[int] = []+ absorbed: dict[int, list[str]] = {}+ for i in by_len_desc:+ survivor = next(+ (+ k+ for k in kept_idx+ if writes[k].scope == writes[i].scope+ and _near_duplicate(tokens[i], tokens[k], bigrams[i], bigrams[k])+ ),+ None,+ )+ if survivor is None:+ kept_idx.append(i)+ absorbed[i] = []+ else:+ absorbed[survivor].extend(writes[i].source_episode_uuids)+ kept = [_absorb_provenance(writes[i], absorbed[i]) for i in sorted(kept_idx)]+ return kept, len(writes) - len(kept)+++def _absorb_provenance(+ write: ConsolidatedFact, absorbed_uuids: list[str]+) -> ConsolidatedFact:+ """Union dropped near-duplicates' episode uuids into the survivor."""+ extra = [+ u for u in dict.fromkeys(absorbed_uuids) if u not in write.source_episode_uuids+ ]+ if not extra:+ return write+ return write.model_copy(+ update={"source_episode_uuids": [*write.source_episode_uuids, *extra]}+ )diff --git a/autogpt_platform/backend/backend/copilot/dream/input_bundle.py b/autogpt_platform/backend/backend/copilot/dream/input_bundle.pynew file mode 100644index 0000000000..e3038f9a5e--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/dream/input_bundle.py@@ -0,0 +1,86 @@+"""A dream pass's ``DreamInput`` as plain JSON.++One format, two stores: the batch path persists it to Redis so each phase+callback can rebuild its prompt (``batch_submit.persist_input_bundle``), and+the pass's durable record keeps the same dict (``backend/data/dream_pass.py``).+It round-trips through ``json`` rather than pickling so the wire format stays+portable and debuggable.+"""++from datetime import datetime+from typing import Any++from .fetch import DreamInput, EpisodeRow, FactRow, SessionRow+++def input_bundle_to_dict(input_bundle: DreamInput) -> dict[str, Any]:+ return {+ "user_id": input_bundle.user_id,+ "expert_id": input_bundle.expert_id,+ "group_id": input_bundle.group_id,+ "window_start": input_bundle.window_start.isoformat(),+ "window_end": input_bundle.window_end.isoformat(),+ "episodes": [+ {+ "uuid": e.uuid,+ "name": e.name,+ "content": e.content,+ "source_description": e.source_description,+ "valid_at": e.valid_at,+ "created_at": e.created_at,+ }+ for e in input_bundle.episodes+ ],+ "facts": [+ {+ "uuid": f.uuid,+ "source": f.source,+ "target": f.target,+ "name": f.name,+ "fact": f.fact,+ "scope": f.scope,+ "confidence": f.confidence,+ "status": f.status,+ "created_at": f.created_at,+ }+ for f in input_bundle.facts+ ],+ "recent_sessions": [+ {+ "session_id": s.session_id,+ "title": s.title,+ "created_at": s.created_at.isoformat() if s.created_at else None,+ "body": s.body,+ }+ for s in input_bundle.recent_sessions+ ],+ "known_fact_uuids": list(input_bundle.known_fact_uuids),+ "known_episode_uuids": list(input_bundle.known_episode_uuids),+ }+++def input_bundle_from_dict(data: dict[str, Any]) -> DreamInput:+ return DreamInput(+ user_id=data["user_id"],+ expert_id=data.get("expert_id"),+ group_id=data["group_id"],+ window_start=datetime.fromisoformat(data["window_start"]),+ window_end=datetime.fromisoformat(data["window_end"]),+ episodes=[EpisodeRow(**e) for e in data.get("episodes") or []],+ facts=[FactRow(**f) for f in data.get("facts") or []],+ recent_sessions=[+ SessionRow(+ session_id=s["session_id"],+ title=s.get("title"),+ created_at=(+ datetime.fromisoformat(s["created_at"])+ if s.get("created_at")+ else None+ ),+ body=s.get("body") or "",+ )+ for s in data.get("recent_sessions") or []+ ],+ known_fact_uuids=set(data.get("known_fact_uuids") or []),+ known_episode_uuids=set(data.get("known_episode_uuids") or []),+ )diff --git a/autogpt_platform/backend/backend/copilot/dream/nightly_batch.py b/autogpt_platform/backend/backend/copilot/dream/nightly_batch.pyindex 14883f357d..d4ba655874 100644--- a/autogpt_platform/backend/backend/copilot/dream/nightly_batch.py+++ b/autogpt_platform/backend/backend/copilot/dream/nightly_batch.py@@ -33,9 +33,10 @@ read-only against the user's graph); only the apply / writeback stepof each submitter briefly acquires the per-user lock and queues userwrites for the seconds it holds.-One pre-flight billing check runs at the top so a single-``nightly_id`` correlates all per-submitter cost log rows for this-pass.+One pre-flight billing check runs at the top, so a paywalled or+over-budget user costs one check for the night rather than one per+submitter. ``nightly_id`` names the fan-out in its logs and result;+cost rows carry each submitter's own id (the dream pass's pass id)."""from __future__ import annotations@@ -52,6 +53,7 @@ from backend.copilot.learning.nightly import run_skill_learning_passfrom backend.util.feature_flag import Flag, is_feature_enabledfrom .billing import check_dream_budget+from .pass_record import DreamTriggerfrom .ratification import RatificationResult, run_ratification_passfrom .schemas import DreamPassResult@@ -70,9 +72,9 @@ class NightlyBatchResult(BaseModel):expert_id: str | None = Nonenightly_id: str = Field(description=(- "UUID correlating every per-submitter cost-log row for "- "this pass. The dream pass and ratification supersession "- "sweep all share this id."+ "UUID naming this fan-out in its logs and result. Cost "+ "rows carry each submitter's own id instead: the dream "+ "pass logs under dream.pass_id."))started_at: datetime@@ -103,14 +105,10 @@ class NightlyBatchResult(BaseModel):async def run_nightly_batch_submit(- user_id: str, *, expert_id: str | None = None+ user_id: str, *, expert_id: str | None = None, trigger: DreamTrigger = "cron") -> NightlyBatchResult:"""Fan out one memory scope's nightly batch-family submissions, in order.- ``expert_id`` picks the expert's scope instead of the account's; the- budget check stays on the owner either way, since an expert's dreams- are paid from the owner's allowance.-Today the order is dream-pass → ratification-supersession-sweep.Future additions land as new sequential calls between these two(e.g. P2 dedup goes before dream so the dream pass operates on a@@ -124,30 +122,44 @@ async def run_nightly_batch_submit(Never raises — top-level failures are captured in``NightlyBatchResult.error`` so the scheduler wrapper can logwithout retry-storming the cron.++ ``trigger`` is recorded on the dream pass: ``cron`` from the nightly+ job, ``admin`` when an admin ran the fan-out on demand."""nightly_id = str(uuidlib.uuid4())started_at = datetime.now(timezone.utc)- # One pre-flight billing check for the whole pass. Per-submitter- # cost logs (written by each submitter's own `record_phase_cost`- # calls) all reference the same ``nightly_id`` via the- # ``dream_pass_id`` field on PlatformCostLog rows. This means a- # paywalled / over-budget user costs us one LD lookup + one- # Redis read for the night, not one per submitter.+ # One pre-flight billing check for the whole fan-out: a paywalled /+ # over-budget user costs us one LD lookup + one Redis read for the+ # night, not one per submitter.budget_ok, budget_skip = await check_dream_budget(user_id)if not budget_ok:- completed_at = datetime.now(timezone.utc)- elapsed = (completed_at - started_at).total_seconds()- if budget_skip == "rate_limit_unavailable":- return NightlyBatchResult(- user_id=user_id,- expert_id=expert_id,- nightly_id=nightly_id,- started_at=started_at,- completed_at=completed_at,- elapsed_seconds=elapsed,- error=f"billing: {budget_skip}",- )+ return _budget_stopped(user_id, expert_id, nightly_id, started_at, budget_skip)++ result = NightlyBatchResult(+ user_id=user_id,+ expert_id=expert_id,+ nightly_id=nightly_id,+ started_at=started_at,+ )+ await _submit_dream(result, trigger)+ await _submit_ratification(result)+ await _submit_learning(result)+ return _finished(result)+++def _budget_stopped(+ user_id: str,+ expert_id: str | None,+ nightly_id: str,+ started_at: datetime,+ budget_skip: str | None,+) -> NightlyBatchResult:+ """The fan-out the pre-flight billing check stopped: errored when the+ budget could not be read, else skipped."""+ completed_at = datetime.now(timezone.utc)+ elapsed = (completed_at - started_at).total_seconds()+ if budget_skip == "rate_limit_unavailable":return NightlyBatchResult(user_id=user_id,expert_id=expert_id,@@ -155,29 +167,36 @@ async def run_nightly_batch_submit(started_at=started_at,completed_at=completed_at,elapsed_seconds=elapsed,- skipped=True,- skip_reason=budget_skip or "insufficient_credits",+ error=f"billing: {budget_skip}",)-- result = NightlyBatchResult(+ return NightlyBatchResult(user_id=user_id,expert_id=expert_id,nightly_id=nightly_id,started_at=started_at,+ completed_at=completed_at,+ elapsed_seconds=elapsed,+ skipped=True,+ skip_reason=budget_skip or "insufficient_credits",)- # Dream pass submitter. Per-submitter failure stays isolated —- # a crashed dream pass doesn't block the ratification sweep- # (ratification operates on already-written tentatives from- # previous passes, not tonight's failed one).++async def _submit_dream(result: NightlyBatchResult, trigger: DreamTrigger) -> None:+ """Dream pass submitter. Per-submitter failure stays isolated — a+ crashed dream pass doesn't block the ratification sweep (ratification+ operates on already-written tentatives from previous passes, not+ tonight's failed one)."""+ user_id = result.user_idtry:from .orchestrator import execute_dream_pass- result.dream = await execute_dream_pass(user_id, expert_id=expert_id)+ result.dream = await execute_dream_pass(+ user_id, expert_id=result.expert_id, trigger=trigger+ )if result.dream.error:logger.warning("Nightly batch %s: dream submitter errored for user %s: %s",- nightly_id,+ result.nightly_id,user_id[:12],result.dream.error,)@@ -188,31 +207,35 @@ async def run_nightly_batch_submit(except Exception as exc:logger.exception("Nightly batch %s: dream submitter crashed for user %s",- nightly_id,+ result.nightly_id,user_id[:12],)# Capture but continue to the next submitter — sweep is# independent.result.error = f"dream: {exc}"- # Ratification supersession sweep. With the sync hit-hook landed- # (see ``ratification_hits.try_ratify_on_hit``), the nightly- # sweep primarily handles supersession of unratified tentatives- # past their grace period — promotions happen inline at- # retrieval-hit time.++async def _submit_ratification(result: NightlyBatchResult) -> None:+ """Ratification supersession sweep. With the sync hit-hook landed+ (see ``ratification_hits.try_ratify_on_hit``), the nightly sweep+ primarily handles supersession of unratified tentatives past their+ grace period — promotions happen inline at retrieval-hit time."""+ user_id = result.user_idtry:- result.ratification = await run_ratification_pass(user_id, expert_id=expert_id)+ result.ratification = await run_ratification_pass(+ user_id, expert_id=result.expert_id+ )if result.ratification.error:logger.warning("Nightly batch %s: ratification sweep errored for user %s: %s",- nightly_id,+ result.nightly_id,user_id[:12],result.ratification.error,)except Exception as exc:logger.exception("Nightly batch %s: ratification sweep crashed for user %s",- nightly_id,+ result.nightly_id,user_id[:12],)# Append rather than overwrite so a dream failure plus a@@ -220,10 +243,13 @@ async def run_nightly_batch_submit(prev = result.error or ""result.error = (prev + " | " if prev else "") + f"ratification: {exc}"- # Skill learning runs once per account: its pass reviews the account's- # skills and its experts', so an expert's nightly batch leaves it out.++async def _submit_learning(result: NightlyBatchResult) -> None:+ """Skill learning, when its own flag is on for the user: the dream pass+ never implies it. A failure stays isolated, as the other submitters' do."""+ user_id = result.user_idtry:- if expert_id is None and await is_feature_enabled(+ if result.expert_id is None and await is_feature_enabled(Flag.DREAM_SKILL_LEARNING_ENABLED, user_id):result.learning = await run_skill_learning_pass(user_id, trigger="nightly")@@ -233,13 +259,15 @@ async def run_nightly_batch_submit(part for part in (result.error, f"learning: {exc}") if part)++def _finished(result: NightlyBatchResult) -> NightlyBatchResult:completed_at = datetime.now(timezone.utc)result.completed_at = completed_at- result.elapsed_seconds = (completed_at - started_at).total_seconds()+ result.elapsed_seconds = (completed_at - result.started_at).total_seconds()logger.info("Nightly batch %s done for user %s in %.1fs: dream=%s ratification=%s",- nightly_id,- user_id[:12],+ result.nightly_id,+ result.user_id[:12],result.elapsed_seconds,_summary(result.dream),_summary(result.ratification),diff --git a/autogpt_platform/backend/backend/copilot/dream/nightly_batch_test.py b/autogpt_platform/backend/backend/copilot/dream/nightly_batch_test.pyindex 249570e8bd..9ae1e67c4f 100644--- a/autogpt_platform/backend/backend/copilot/dream/nightly_batch_test.py+++ b/autogpt_platform/backend/backend/copilot/dream/nightly_batch_test.py@@ -311,7 +311,8 @@ async def test_skipped_or_errored_batch_path_dream_is_not_in_flight():@pytest.mark.asyncio-async def test_expert_scope_reaches_both_submitters_and_the_owner_pays():[email protected]("trigger", ["cron", "admin"])+async def test_expert_scope_reaches_both_submitters_and_the_owner_pays(trigger):"""An expert's nightly cron runs the dream and the sweep on the expert'sgraph, while the budget check stays on the owner, whose allowance paysfor it."""@@ -321,10 +322,12 @@ async def test_expert_scope_reaches_both_submitters_and_the_owner_pays():with patch.object(nightly_batch, "check_dream_budget", new=budget), patch("backend.copilot.dream.orchestrator.execute_dream_pass", new=dream_spy), patch.object(nightly_batch, "run_ratification_pass", new=rat_spy):- result = await run_nightly_batch_submit("u", expert_id="expert-1")+ result = await run_nightly_batch_submit(+ "u", expert_id="expert-1", trigger=trigger+ )budget.assert_awaited_once_with("u")- dream_spy.assert_awaited_once_with("u", expert_id="expert-1")+ dream_spy.assert_awaited_once_with("u", expert_id="expert-1", trigger=trigger)rat_spy.assert_awaited_once_with("u", expert_id="expert-1")assert result.expert_id == "expert-1"@@ -344,7 +347,7 @@ async def test_account_scope_runs_the_account_graph():):result = await run_nightly_batch_submit("u")- dream_spy.assert_awaited_once_with("u", expert_id=None)+ dream_spy.assert_awaited_once_with("u", expert_id=None, trigger="cron")rat_spy.assert_awaited_once_with("u", expert_id=None)assert result.expert_id is Nonediff --git a/autogpt_platform/backend/backend/copilot/dream/orchestrator.py b/autogpt_platform/backend/backend/copilot/dream/orchestrator.pyindex 56a32c1c59..0927139d75 100644--- a/autogpt_platform/backend/backend/copilot/dream/orchestrator.py+++ b/autogpt_platform/backend/backend/copilot/dream/orchestrator.py@@ -2,28 +2,32 @@Walks a user's recent memory window through the consolidate → recombine→ sanitize pipeline, then applies the sanitizer's ``DreamOperations``-to Graphiti + Postgres.+(clamped by ``clamp.py``) to Graphiti + Postgres.Each phase here is one ``inference.complete.structured_complete`` callon the chat transport's provider, traced and recorded through theinference package. When ``routing.resolve_dream_execution_path``-picks ``anthropic_batch``, ``_submit_dream_pass_batch`` submits the-first phase to Anthropic's Message Batches API instead and+picks ``anthropic_batch``, ``batch_handoff.submit_dream_pass_batch``+submits the first phase to Anthropic's Message Batches API instead and``batch_callbacks`` runs the later phases and the apply step as theresults land.+Every pass, on either route, gets a durable ``DreamPass`` row+(``store.py``): inserted at the start, advanced after each step, and+closed with how the pass ended, a write attempted before the pass releases+its lock. Like every record write it is best-effort: one that fails or+times out leaves the row open behind a free lock until a reaper closes it.+The orchestrator never raises out — every failure becomes a-``DreamPassResult`` with ``error`` set, so the admin trigger always-gets a structured response back.+``DreamPassResult`` with ``error`` set and, on the sync route, the usage+of every phase billed before it (a phase whose charge failed included),+so the admin trigger always gets a structured response back."""from __future__ import annotations-import asyncioimport logging-import re-import uuid as uuidlib-from collections.abc import Sequence+from collections.abc import Awaitablefrom datetime import datetime, timezonefrom typing import TypeVar@@ -36,7 +40,6 @@ from backend.copilot.inference.context import (InferenceContext,InferenceError,InferenceScope,- InferenceUsage,)from backend.copilot.inference.routing import resolve_routefrom backend.copilot.inference.trace import trace@@ -44,7 +47,9 @@ from backend.copilot.trial_cost_context import attributed_usagefrom backend.util.feature_flag import Flag, is_feature_enabledfrom .apply import apply_operations, drain_status_from_stats-from .billing import check_dream_budget, record_phase_cost+from .batch_handoff import submit_dream_pass_batch+from .billing import PhaseChargeError, check_dream_budget, record_phase_cost+from .clamp import clamp_operationsfrom .fetch import (DreamInput,EpisodeRow,@@ -52,35 +57,34 @@ from .fetch import (is_dream_authored_episode,parse_episode_timestamp,)-from .locks import (- BATCH_LOCK_TTL_SECONDS,- DEFAULT_LOCK_TTL_SECONDS,- DreamLockHandle,- DreamLockHeld,- dream_lock,-)+from .locks import DEFAULT_LOCK_TTL_SECONDS, DreamLockHandle, DreamLockHeld, dream_lock+from .pass_record import DreamTrigger+from .pass_run import DreamPassRunfrom .phase_jobs import phase_jobfrom .prompts import (- MAX_DEMOTIONS_PER_PASS,- MAX_PROPOSALS_PER_PASS,- MAX_WRITES_PER_PASS,build_consolidate_prompt,build_recombine_prompt,build_sanitize_prompt,)-from .routing import ExecutionPath, batch_discount, resolve_dream_execution_path+from .routing import ExecutionPath, resolve_dream_execution_pathfrom .schemas import (- ConsolidatedFact,ConsolidationOutput,DreamOperations,DreamOperationsSnapshot,DreamPassResult,- DreamPassUsage,DreamPhase,+ IngestionDrainStatus,PhaseUsage,- ProposedFinding,RecombinationOutput,)+from .store import (+ record_applying,+ record_gathered,+ record_phase_output,+ record_sync_outcome,+ start_pass,+)+from .usage import phase_usagelogger = logging.getLogger(__name__)@@ -125,7 +129,8 @@ SANITIZE_MAX_TOKENS = 16384# consolidate 240s + recombine 600s + sanitize 480s = 1320s LLM ceiling# + apply.INGESTION_DRAIN_TIMEOUT_SECONDS 300s drain cap# + DREAM_NON_LLM_HEADROOM_SECONDS 120s (budget check + fetch + enqueue-# + demotions + summary + cost logging, EXCLUDING the drain)+# + demotions + summary + cost logging + the pass's record writes,+# EXCLUDING the drain)# = 1740s <= 1800s SCHEDULER_DREAM_OPERATION_TIMEOUT_SECONDS (60s slack)## The drain no longer counts against the lock TTL: apply renews the dream@@ -147,21 +152,11 @@ RECOMBINE_TIMEOUT_SECONDS = 600SANITIZE_TIMEOUT_SECONDS = 480# Reserved for the non-LLM segments of the pass OTHER than the ingestion# drain (budget check, fetch, enqueue, demotions, summary write, cost-# logging). The drain is budgeted separately as+# logging, and the DreamPass record writes, each capped at+# store.RECORD_WRITE_TIMEOUT_SECONDS). The drain is budgeted separately as# apply.INGESTION_DRAIN_TIMEOUT_SECONDS and covered by a lock renewal.DREAM_NON_LLM_HEADROOM_SECONDS = 120-# Entity invalidation is the most destructive op the sanitizer can emit:-# each one single-hop demotes EVERY :RELATES_TO edge on the entity, so a-# hub entity multiplies the blast radius far past the demotion caps. We-# cap the *count* of invalidations per pass here; the per-entity edge-# blast radius is bounded by the ``DREAM_PASS_INVALIDATE_ENTITY`` LD flag-# (staged rollout, off by default) plus the single-hop-only guarantee in-# ``invalidate_entity_direct_neighbors`` (no multi-hop propagation).-# Deliberately NOT degree-aware — fetching entity degrees is async graph-# work that doesn't belong in this sync clamp.-MAX_ENTITY_INVALIDATIONS_PER_PASS = 2-# Per-user marker stamped after a successful (non-skipped) sync apply so# the next nightly pass can skip all three LLM phases when no new episode# landed since. Single key — prod Redis runs in cluster mode (see@@ -173,374 +168,6 @@ MAX_ENTITY_INVALIDATIONS_PER_PASS = 2LAST_COMPLETED_TTL_SECONDS = 35 * 24 * 60 * 60-# High-precision filter for "transient intent" facts — content that-# records what the user is ASKING/wants to KNOW rather than a durable-# fact about them. The sanitize prompt is told to drop these, but-# prompt-only sanitization leaks them (#13388: "User is asking how-# Kubernetes works", "User is interested in knowing which PRs are open"),-# so this is a deterministic belt-and-suspenders gate.-#-# Deliberately NARROW to knowledge-seeking intent. We do NOT match goals-# like "user wants to create/build X" — those are legitimate durable-# memories. Generic world-knowledge pollution ("Kubernetes uses pods…")-# is left to the sanitize prompt: it needs LLM judgment (is the subject-# the user?) that a regex can't do without false-positives.-#-# Interrogative complements are the sharp edge — several verbs are durable-# aspirations on their own but transient questions once they take a-# question word:-# * ``asking`` — "asking FOR weekly reports" / "asking the agent TO-# monitor PRs" are durable requests (semantically like "wants X"), so-# only interrogative ``asking HOW/WHAT/…`` counts as transient.-# * ``learn``/``understand``/``find out`` — "wants to learn Spanish",-# "wants to understand distributed systems", "wants to find out about-# new markets" are durable skill/aspiration GOALS; they only read as-# transient with an interrogative ("wants to learn HOW X works").-# * ``curious`` — "curious ABOUT X" is transient, but "curious by nature"-# is a durable personality trait, so a ``curious about`` complement is-# required (mirroring ``confused about``/``unsure about``).-# ``know`` stays complement-free: "wants to know X" is transient curiosity-# regardless of phrasing (there is no durable "wants to know" aspiration —-# that role is served by ``learn``).-# Known limitation (nice-to-have, low frequency): a standing notification-# preference phrased "wants to know when X happens" is dropped; separating-# it from a one-off "wants to know when the deploy is" isn't reliably-# regex-able, so it's left to the sanitize prompt + human review.-#-# Subject scope: the gate deliberately anchors on the generic ``user``-# subject only. Name-phrased transient intent ("Nick is asking how the-# auth flow works") is intentionally NOT matched here — broadening the-# subject to arbitrary proper nouns would risk false-positives on-# non-user entities ("Kubernetes is asking for more nodes"), so-# name-first phrasing is left to the sanitize prompt's LLM judgment. The-# leading auxiliary allows perfect-progressive forms ("has been asking").-_TRANSIENT_INTENT_RE = re.compile(- r"^(the\s+)?user\s+(?:(?:is|has|was)\s+(?:been\s+)?)?"- r"(asking\s+(how|what|why|whether|if|when|where|who|which|about)\b"- r"|wondering\b"- r"|curious\s+about\b"- r"|confused\s+about\b"- r"|unsure\s+about\b"- r"|trying\s+to\s+understand\b"- r"|interested\s+in\s+(knowing|understanding)\b"- r"|interested\s+in\s+learning\s+(how|what|why|whether|if|when|where)\b"- r"|wants?\s+to\s+know\b"- r"|wants?\s+to\s+(understand|find\s+out)\s+(how|what|why|whether|if|when|where)\b"- r"|wants?\s+to\s+learn\s+(how|what|why|whether|if|when|where)\b"- r"|asked\s+(how|what|why|whether|if|when|where|about)\b)",- re.IGNORECASE,-)---def _is_transient_intent(content: str) -> bool:- """True when ``content`` reads as a question/knowledge-seeking intent- rather than a durable fact about the user."""- return bool(_TRANSIENT_INTENT_RE.match(content.strip()))---_ContentItem = TypeVar("_ContentItem", ConsolidatedFact, ProposedFinding)---def _drop_transient_intent(- items: Sequence[_ContentItem],-) -> tuple[list[_ContentItem], int]:- """Filter ConsolidatedFact / ProposedFinding items whose ``.content``- is a transient intent. Returns (kept, dropped_count)."""- kept = [it for it in items if not _is_transient_intent(it.content)]- return kept, len(items) - len(kept)---# Intra-pass near-duplicate write rejection (#13387). The consolidate-# prompt asks the model to merge near-duplicates, but a single pass still-# emits the same fact phrased 2-3 ways ("Nick uses Terminus on iPhone for-# CLI work" / "Nick wants Terminus on iPhone to show more ASCII"). This is-# a CONSERVATIVE, deterministic backstop: it only collapses writes that are-# near-identical by content-word overlap, keeping the longest (most-# specific) of each cluster. It deliberately does NOT do semantic-# (embedding) merging or compare against the user's EXISTING active facts —-# both need real-data threshold tuning + edge-merge design and are tracked-# as the follow-up P2 dedup pass. High threshold so distinct facts about-# the same entity ("prefers Python" vs "prefers Rust") are never merged.-_DEDUP_JACCARD_THRESHOLD = 0.7-# A short fact whose content words are (nearly) all contained in a longer-# fact is a duplicate the longer one subsumes — even when the longer fact's-# extra detail drags Jaccard below the threshold ("Nick uses Terminus on-# iPhone for CLI" ⊂ "…for CLI and wants more ASCII"). Set high (0.9): for-# facts under ~10 words it effectively requires the shorter to be a strict-# SUBSET of the longer, so a single distinguishing word keeps them apart-# ("deployed the AUTH service" vs "deployed the BILLING service" → 4/5=0.8,-# NOT merged). Only applied when the smaller fact has enough words to be-# specific, so 1-2 word fragments don't spuriously match.-_DEDUP_CONTAINMENT_THRESHOLD = 0.9-_DEDUP_CONTAINMENT_MIN_TOKENS = 3-# Facts with IDENTICAL content-word sets can still describe different-# events when word order differs ("Alice introduced Bob to Carol" vs-# "Alice introduced Carol to Bob"). Equal token sets therefore also-# require agreement on adjacent-word bigrams before merging.-_DEDUP_BIGRAM_THRESHOLD = 0.6-# A negation marker present in one fact but not the other flips the-# meaning ("uses vim" vs "never uses vim") — never merge across it.-# The tokenizer splits contractions ("doesn't" → "doesn", "t"), so the-# bare stems cover the apostrophe forms; a false veto only ever KEEPS-# both facts, which is the conservative direction.-_DEDUP_NEGATION_MARKERS = frozenset(- {- "no",- "not",- "never",- "none",- "nor",- "cannot",- "nt",- "dont",- "don",- "doesnt",- "doesn",- "didnt",- "didn",- "isnt",- "isn",- "wasnt",- "wasn",- "werent",- "weren",- "wont",- "cant",- "hasnt",- "hasn",- "havent",- "haven",- "hadnt",- "hadn",- "wouldnt",- "wouldn",- "shouldnt",- "shouldn",- "couldnt",- "couldn",- "arent",- "aren",- }-)-_DEDUP_STOPWORDS = frozenset(- {- "a",- "an",- "the",- "to",- "of",- "and",- "or",- "is",- "are",- "was",- "were",- "for",- "on",- "in",- "at",- "by",- "with",- "that",- "this",- "it",- "their",- "they",- "them",- "his",- "her",- "its",- "as",- "be",- "has",- "have",- "had",- "s",- "so",- "more",- "user",- "users",- }-)---def _content_tokens(content: str) -> frozenset[str]:- """Lowercased alphanumeric content words, minus a small stopword set.- ``user`` is a stopword because nearly every fact begins with it."""- return frozenset(- t- for t in re.findall(r"[a-z0-9]+", content.lower())- if t not in _DEDUP_STOPWORDS- )---def _word_bigrams(content: str) -> frozenset[tuple[str, str]]:- """Adjacent word pairs over the full (unfiltered) word sequence — the- order-sensitive signal used when two facts' token sets are equal."""- words = re.findall(r"[a-z0-9]+", content.lower())- return frozenset(zip(words, words[1:]))---def _near_duplicate(- a: frozenset[str],- b: frozenset[str],- a_bigrams: frozenset[tuple[str, str]],- b_bigrams: frozenset[tuple[str, str]],-) -> bool:- """Two content-word sets are near-duplicate when their Jaccard overlap- is high, OR the smaller (sufficiently specific) set is nearly contained- in the larger one. A negation marker on only one side always vetoes;- identical sets additionally need word-order (bigram) agreement."""- if not a or not b:- return False- if _DEDUP_NEGATION_MARKERS & (a ^ b):- return False- if a == b:- bigram_union = len(a_bigrams | b_bigrams)- if not bigram_union:- return True- return len(a_bigrams & b_bigrams) / bigram_union >= _DEDUP_BIGRAM_THRESHOLD- inter = len(a & b)- union = len(a | b)- if union and inter / union >= _DEDUP_JACCARD_THRESHOLD:- return True- smaller = min(len(a), len(b))- if smaller < _DEDUP_CONTAINMENT_MIN_TOKENS:- return False- return inter / smaller >= _DEDUP_CONTAINMENT_THRESHOLD---def _dedupe_near_duplicate_writes(- writes: list[ConsolidatedFact],-) -> tuple[list[ConsolidatedFact], int]:- """Collapse near-duplicate writes, keeping the longest of each cluster.-- Greedy by content length (desc) so the most specific phrasing survives;- output preserves the writes' original order. Writes are only compared- within the same ``scope`` (facts must never merge across scopes), and a- dropped write's ``source_episode_uuids`` are unioned into its cluster's- survivor so provenance is never lost. Returns (kept, dropped)."""- by_len_desc = sorted(- range(len(writes)), key=lambda i: len(writes[i].content), reverse=True- )- tokens = [_content_tokens(w.content) for w in writes]- bigrams = [_word_bigrams(w.content) for w in writes]- kept_idx: list[int] = []- absorbed: dict[int, list[str]] = {}- for i in by_len_desc:- survivor = next(- (- k- for k in kept_idx- if writes[k].scope == writes[i].scope- and _near_duplicate(tokens[i], tokens[k], bigrams[i], bigrams[k])- ),- None,- )- if survivor is None:- kept_idx.append(i)- absorbed[i] = []- else:- absorbed[survivor].extend(writes[i].source_episode_uuids)- kept = [_absorb_provenance(writes[i], absorbed[i]) for i in sorted(kept_idx)]- return kept, len(writes) - len(kept)---def _absorb_provenance(- write: ConsolidatedFact, absorbed_uuids: list[str]-) -> ConsolidatedFact:- """Union dropped near-duplicates' episode uuids into the survivor."""- extra = [- u for u in dict.fromkeys(absorbed_uuids) if u not in write.source_episode_uuids- ]- if not extra:- return write- return write.model_copy(- update={"source_episode_uuids": [*write.source_episode_uuids, *extra]}- )---def _clamp_operations(- ops: DreamOperations,- active_fact_count: int,- known_fact_uuids: set[str] | None = None,-) -> DreamOperations:- """Hard-trim oversized phase 3 outputs.-- Phase 3's prompt asks for these caps but the model can still- over-emit. The orchestrator enforces them in code so apply.py- never writes more than the policy allows.-- Demotions carry a second ceiling — 5% of the active fact set — so a- single pass can never wipe a meaningful fraction of a user's memory- even if the absolute ``MAX_DEMOTIONS_PER_PASS`` cap would allow it.- The 5% ceiling floors at 1 when there is at least one active fact:- small graphs (< 20 facts) would otherwise round to a cap of 0 and- never get even a single contradicted fact demoted.- ``active_fact_count < 0`` means the count is unknown (the batch path- lost its persisted input bundle); fall back to the absolute cap only- rather than silently dropping every demotion.-- When ``known_fact_uuids`` is provided, demotions targeting uuids- outside it are dropped BEFORE the cap slice — otherwise a- hallucinated uuid at the head of the model's list consumes a cap- slot (the entire budget on a floor-of-1 small graph) and displaces- a valid demotion that apply.py would have accepted. ``None`` skips- the pre-filter; apply.py's idempotent known-uuid filter remains the- security chokepoint either way.-- Entity invalidations are count-capped at- ``MAX_ENTITY_INVALIDATIONS_PER_PASS``; see the constant's comment- for why the per-entity edge blast radius is bounded elsewhere (LD- flag + single-hop guarantee), not here.- """- demotions = ops.demotions- if known_fact_uuids is not None:- demotions = [d for d in demotions if d.edge_uuid in known_fact_uuids]- dropped = len(ops.demotions) - len(demotions)- if dropped:- logger.warning(- "Dream clamp: dropped %d demotion(s) targeting edge uuids "- "outside known_fact_uuids before applying the demotion cap",- dropped,- )- demotion_cap = MAX_DEMOTIONS_PER_PASS- if active_fact_count == 0:- demotion_cap = 0- elif active_fact_count > 0:- demotion_cap = min(MAX_DEMOTIONS_PER_PASS, max(1, active_fact_count * 5 // 100))-- # Drop transient-intent pollution ("user is asking…") before the cap- # slice so a leaked question never displaces a real fact (#13388).- writes, w_intent_dropped = _drop_transient_intent(ops.writes)- proposals, p_intent_dropped = _drop_transient_intent(ops.proposals)- if w_intent_dropped or p_intent_dropped:- logger.info(- "Dream clamp: dropped %d transient-intent write(s) and %d "- "proposal(s) (questions captured as facts)",- w_intent_dropped,- p_intent_dropped,- )- # Collapse intra-pass near-duplicate writes before the cap slice so the- # cap counts distinct facts, not paraphrases of one (#13387).- writes, w_dup_dropped = _dedupe_near_duplicate_writes(writes)- if w_dup_dropped:- logger.info(- "Dream clamp: collapsed %d near-duplicate write(s) into their "- "canonical (longest) phrasing",- w_dup_dropped,- )- return DreamOperations(- writes=writes[:MAX_WRITES_PER_PASS],- proposals=proposals[:MAX_PROPOSALS_PER_PASS],- demotions=demotions[:demotion_cap],- entity_invalidations=ops.entity_invalidations[- :MAX_ENTITY_INVALIDATIONS_PER_PASS- ],- summary_for_user=ops.summary_for_user,- )--_Output = TypeVar("_Output", bound=BaseModel)@@ -642,44 +269,7 @@ async def _run_phase()usage = await record_phase_cost(call.ctx, completion.usage)call.usage = usage- return completion.value, _phase_usage(phase, usage)---def _phase_usage(phase: DreamPhase, usage: InferenceUsage) -> PhaseUsage:- return PhaseUsage(- phase=phase,- model=usage.model,- input_tokens=usage.input_tokens,- output_tokens=usage.output_tokens,- cache_read_tokens=usage.cache_read_tokens,- cache_creation_tokens=usage.cache_creation_tokens,- cost_usd=usage.cost_usd,- )---def _aggregate_usage(- phases: list[PhaseUsage], execution_path: ExecutionPath-) -> DreamPassUsage:- """Roll up per-phase usage into a ``DreamPassUsage``.-- ``total_cost_usd`` is None when any single phase had unknown cost- so we never silently bill at a partial figure.- """- total_cost: float | None = 0.0- for p in phases:- if p.cost_usd is None:- total_cost = None- break- total_cost += p.cost_usd- return DreamPassUsage(- phases=phases,- total_input_tokens=sum(p.input_tokens for p in phases),- total_output_tokens=sum(p.output_tokens for p in phases),- total_cache_read_tokens=sum(p.cache_read_tokens for p in phases),- total_cache_creation_tokens=sum(p.cache_creation_tokens for p in phases),- total_cost_usd=total_cost,- discount_applied=batch_discount(execution_path),- )+ return completion.value, phase_usage(phase, usage)async def _read_last_completed_marker(scope: MemoryScope) -> datetime | None:@@ -770,6 +360,32 @@ def _has_new_episodes_since(episodes: list[EpisodeRow], marker: datetime) -> booreturn False+async def execute_dream_pass(+ user_id: str,+ *,+ status_id: str | None = None,+ expert_id: str | None = None,+ trigger: DreamTrigger = "cron",+) -> DreamPassResult:+ """Public async entry point used by the scheduler + admin trigger.++ ``status_id`` is the JobStatus row id when the caller is the polling+ admin trigger: it lets the trigger re-run a pass the last-completed+ marker would skip, and on the batch route the callbacks advance that+ row as each phase lands. Which route a pass takes depends on the batch+ flag and the deployment's Anthropic key, not on the caller, so a pass+ without a ``status_id`` can take the batch route too; its callbacks+ then skip the JobStatus writes.++ ``trigger`` is what started the pass, as its ``DreamPass`` row records+ it: ``cron`` for the nightly job, ``admin`` for the admin triggers,+ ``eval`` for an eval run.+ """+ return await _execute_dream_pass_async(+ user_id, status_id=status_id, expert_id=expert_id, trigger=trigger+ )++@attributed_usageasync def _execute_dream_pass_async(user_id: str,@@ -777,446 +393,301 @@ async def _execute_dream_pass_async(expert_id: str | None,config: ChatConfig | None = None,status_id: str | None = None,+ trigger: DreamTrigger = "cron",) -> DreamPassResult:config = config or ChatConfig()- pass_id = str(uuidlib.uuid4())- started_at = datetime.now(timezone.utc)- monotonic_start = asyncio.get_event_loop().time()-- has_anthropic_key = bool(config.direct_anthropic_api_key)- # Step 5: the async Anthropic batch path is gated by the- # ``DREAM_PASS_BATCH_ENABLED`` LD flag AND a direct Anthropic key (the- # native Batch API can't be reached via OpenRouter/subscription, so the- # key is a hard requirement). When the flag is off, dreams run on the- # synchronous baseline regardless of key presence — the flag lets the- # batch path ship dark and roll out per-cohort. When on, phase 1 submits- # via call_provider(execution_mode="batch"); the BatchExecutor polls and- # dream's batch_callbacks chain phases 2 → 3 + apply when results land- # (half the list price, see routing.batch_discount).- #- # ``transport_name`` short-circuits to sync_baseline for transports that- # can't honour a batch path (local backends have no batch API;- # subscription mode shouldn't dual-bill the user's Anthropic key when the- # chat layer is on Claude Code OAuth).- batch_enabled = await is_feature_enabled(Flag.DREAM_PASS_BATCH_ENABLED, user_id)- execution_path: ExecutionPath = resolve_dream_execution_path(- has_anthropic_key=has_anthropic_key,- batch_processing_enabled=batch_enabled,- transport_name=config.transport.name,- )-- ttl = DEFAULT_LOCK_TTL_SECONDS+ run = DreamPassRun.begin(user_id, await _route_for(user_id, config))try:scope = MemoryScope.build(user_id, expert_id)- lock_context = dream_lock(scope, ttl_seconds=ttl)- async with lock_context as dream_lock_handle:- # Pre-flight billing check. Runs inside the lock so a- # paywalled user doesn't burn the slot for an eligible- # concurrent pass on a shared FalkorDB.- budget_ok, budget_skip = await check_dream_budget(user_id, config=config)- if not budget_ok:- if budget_skip == "rate_limit_unavailable":- return _failure_result(- user_id,- pass_id,- started_at,- monotonic_start,- execution_path,- f"billing: {budget_skip}",- )- return DreamPassResult(- user_id=user_id,- pass_id=pass_id,- started_at=started_at,- completed_at=datetime.now(timezone.utc),- elapsed_seconds=(asyncio.get_event_loop().time() - monotonic_start),- execution_path=execution_path,- skipped=True,- skip_reason=budget_skip or "insufficient_credits",- )-- input_bundle = await gather_dream_input(scope)-- if not input_bundle.episodes and not input_bundle.facts:- # Nothing to consolidate — early-return as skipped so the- # admin UI can render "nothing to dream about yet".- return DreamPassResult(- user_id=user_id,- pass_id=pass_id,- started_at=started_at,- completed_at=datetime.now(timezone.utc),- elapsed_seconds=(asyncio.get_event_loop().time() - monotonic_start),- execution_path=execution_path,- skipped=True,- skip_reason="no_input",- )-- # No NEW activity since the last completed pass — every episode- # in the bundle predates the marker, so re-running all three- # LLM phases would only re-chew already-consolidated material- # (and, before the empty-pass guard in apply.py, manufacture an- # empty dream chat). Marker read is best-effort: missing,- # unparseable, or Redis-down all mean "run the pass".- #- # Manual admin triggers (the only callers that set status_id)- # bypass the marker: "dream now" is the memory-debugging tool- # for re-running a pass after prompt/flag/model changes, and a- # silent no_new_activity skip would neuter it for up to the- # marker's 35-day TTL.- last_completed = (- await _read_last_completed_marker(scope) if status_id is None else None- )- if last_completed is not None and not _has_new_episodes_since(- input_bundle.episodes, last_completed- ):- logger.info(- "Dream pass %s skipped for user %s — no episodes newer "- "than last completed pass at %s",- pass_id,- user_id[:12],- last_completed.isoformat(),- )- return DreamPassResult(- user_id=user_id,- pass_id=pass_id,- started_at=started_at,- completed_at=datetime.now(timezone.utc),- elapsed_seconds=(asyncio.get_event_loop().time() - monotonic_start),- execution_path=execution_path,- skipped=True,- skip_reason="no_new_activity",- )-- # ---- Anthropic batch path ------------------------------------ #- # When routing picks ``anthropic_batch`` we persist the- # input bundle, submit phase 1 to Anthropic's Messages- # Batches API with forced tool_use structured output, and- # return ``status="submitted"`` immediately. The BatchExecutor- # polls; dream's batch_callbacks chain phases 2 → 3 + apply- # when each phase result lands. Total latency is provider-- # driven (typically <30min, hard cap 24h via- # BatchExecutor.MAX_BATCH_LIFETIME_SECONDS); the user sees- # JobStatus.current_phase advance through consolidate →- # recombine → sanitize → complete.- if execution_path == "anthropic_batch":- return await _submit_dream_pass_batch(- user_id=user_id,- pass_id=pass_id,- started_at=started_at,- monotonic_start=monotonic_start,- execution_path=execution_path,- config=config,- input_bundle=input_bundle,- status_id=status_id,- dream_lock_handle=dream_lock_handle,- )-- run = _PassInference(- scope=InferenceScope(user_id=user_id, expert_id=expert_id),- pass_id=pass_id,- config=config,- )- step_usages: list[PhaseUsage] = []-- try:- consolidated, usage = await _run_consolidate(run, input_bundle)- except InferenceError as exc:- return _failure_result(- user_id,- pass_id,- started_at,- monotonic_start,- execution_path,- f"consolidate: {exc}",- usage=_aggregate_usage(step_usages, execution_path),- )- step_usages.append(usage)-- try:- recombined, usage = await _run_recombine(- run, input_bundle, consolidated- )- except InferenceError as exc:- return _failure_result(- user_id,- pass_id,- started_at,- monotonic_start,- execution_path,- f"recombine: {exc}",- usage=_aggregate_usage(step_usages, execution_path),- )- step_usages.append(usage)-- try:- sanitized, usage = await _run_sanitize(- run, input_bundle, consolidated, recombined- )- except InferenceError as exc:- return _failure_result(- user_id,- pass_id,- started_at,- monotonic_start,- execution_path,- f"sanitize: {exc}",- usage=_aggregate_usage(step_usages, execution_path),- )- step_usages.append(usage)-- ops = _clamp_operations(- sanitized,- len(input_bundle.facts),- known_fact_uuids=input_bundle.known_fact_uuids,- )- apply_stats = await apply_operations(- scope,- pass_id,- ops,- known_fact_uuids=input_bundle.known_fact_uuids,- known_episode_uuids=input_bundle.known_episode_uuids,- lock_handle=dream_lock_handle,- )- # Apply succeeded (even as a no-op) — stamp the marker so the- # next nightly pass can skip when nothing new has landed.- # Stamped with the gather-window end so episodes that arrived- # mid-pass still count as new next time. Sync path only: batch- # apply runs hours later in batch_callbacks, which doesn't- # stamp yet.- await _stamp_last_completed_marker(scope, input_bundle.window_end)-- completed_at = datetime.now(timezone.utc)- snapshot = apply_stats.get("snapshot")- raw_session_id = apply_stats.get("session_id")-- def _as_int(key: str) -> int:- v = apply_stats.get(key, 0)- return int(v) if isinstance(v, (int, str)) and v else 0-- # Fail-closed: a missing/malformed drain flag reads as- # ``timed_out`` (writes at risk), never a confirmed drain.- ingestion_drain_status = drain_status_from_stats(apply_stats)-- return DreamPassResult(- user_id=user_id,- pass_id=pass_id,- started_at=started_at,- completed_at=completed_at,- elapsed_seconds=(asyncio.get_event_loop().time() - monotonic_start),- execution_path=execution_path,- consolidated_count=_as_int("consolidated_count"),- proposal_count=_as_int("proposal_count"),- demotion_count=_as_int("demotion_count"),- entity_invalidation_count=_as_int("entity_invalidation_count"),- dropped_forgotten=_as_int("dropped_forgotten"),- pending_writes=_as_int("pending_writes"),- failed_writes=_as_int("failed_writes"),- summary_for_user=ops.summary_for_user,- ingestion_drain_status=ingestion_drain_status,- # ``None`` (key absent) on an empty pass — apply skipped the- # dream session entirely, so there is no id to surface.- dream_session_id=(- raw_session_id if isinstance(raw_session_id, str) else None- ),- operations=(- snapshot if isinstance(snapshot, DreamOperationsSnapshot) else None- ),- usage=_aggregate_usage(step_usages, execution_path),+ await start_pass(+ run.pass_id,+ scope,+ route=run.execution_path,+ trigger=trigger,+ started_at=run.started_at,+ )+ async with dream_lock(scope, ttl_seconds=DEFAULT_LOCK_TTL_SECONDS) as handle:+ return await _run_locked(+ run, scope, handle, config=config, status_id=status_id)-except DreamLockHeld:- return DreamPassResult(- user_id=user_id,- pass_id=pass_id,- started_at=started_at,- completed_at=datetime.now(timezone.utc),- elapsed_seconds=(asyncio.get_event_loop().time() - monotonic_start),- execution_path=execution_path,- skipped=True,- skip_reason="lock_held",- )+ result = run.skipped("lock_held")except Exception as exc: # pragma: no cover — last-resort guardlogger.exception("Dream pass crashed for user %s: %s", user_id[:12], exc)- return _failure_result(- user_id,- pass_id,- started_at,- monotonic_start,- execution_path,- str(exc),- )+ result = run.failure(str(exc))+ await record_sync_outcome(result)+ return result+++async def _route_for(user_id: str, config: ChatConfig) -> ExecutionPath:+ """The route a pass takes.++ The async Anthropic batch path is gated by the+ ``DREAM_PASS_BATCH_ENABLED`` LD flag AND a direct Anthropic key (the+ native Batch API can't be reached via OpenRouter/subscription, so the+ key is a hard requirement). When the flag is off, dreams run on the+ synchronous baseline regardless of key presence — the flag lets the+ batch path ship dark and roll out per-cohort. When on, phase 1 submits+ via call_provider(execution_mode="batch"); the BatchExecutor polls and+ dream's batch_callbacks chain phases 2 → 3 + apply when results land+ (half the list price, see routing.batch_discount).++ ``transport_name`` short-circuits to sync_baseline for transports that+ can't honour a batch path (local backends have no batch API;+ subscription mode shouldn't dual-bill the user's Anthropic key when the+ chat layer is on Claude Code OAuth).+ """+ batch_enabled = await is_feature_enabled(Flag.DREAM_PASS_BATCH_ENABLED, user_id)+ return resolve_dream_execution_path(+ has_anthropic_key=bool(config.direct_anthropic_api_key),+ batch_processing_enabled=batch_enabled,+ transport_name=config.transport.name,+ )-def _failure_result(- user_id: str,- pass_id: str,- started_at: datetime,- monotonic_start: float,- execution_path: ExecutionPath,- error: str,- usage: DreamPassUsage | None = None,+async def _run_locked(+ run: DreamPassRun,+ scope: MemoryScope,+ lock_handle: DreamLockHandle,+ *,+ config: ChatConfig,+ status_id: str | None,) -> DreamPassResult:- """Build a failure ``DreamPassResult`` that still carries usage for- the phases that completed before the error, and for a failed phase whose- answer came back — billing has to charge for tokens we already paid- for, even on partial failure."""- return DreamPassResult(- user_id=user_id,- pass_id=pass_id,- started_at=started_at,- completed_at=datetime.now(timezone.utc),- elapsed_seconds=asyncio.get_event_loop().time() - monotonic_start,- execution_path=execution_path,- error=error,- usage=usage,- )+ """The pass while it holds its scope's lock. Never raises: an unexpected+ error becomes a failure carrying the usage the pass has run up.++ The outcome write is attempted here, inside the lock, so the next pass+ to take the lock normally finds this one's row closed. It is best-effort+ like every record write: one that fails or times out is dropped and the+ lock is released anyway, so a free lock can still have an open row+ (APPLYING, say) behind it until a reaper closes it."""+ try:+ result = await _dream(+ run, scope, lock_handle, config=config, status_id=status_id+ )+ except Exception as exc:+ logger.exception("Dream pass crashed for user %s: %s", run.user_id[:12], exc)+ result = run.failure(str(exc))+ await record_sync_outcome(result)+ return result-async def execute_dream_pass(- user_id: str,+async def _dream(+ run: DreamPassRun,+ scope: MemoryScope,+ lock_handle: DreamLockHandle,*,- status_id: str | None = None,- expert_id: str | None = None,+ config: ChatConfig,+ status_id: str | None,) -> DreamPassResult:- """Public async entry point used by the scheduler + admin trigger.-- ``status_id`` is the optional JobStatus row id when the caller is- the polling admin trigger (Step 2). The orchestrator threads it- into the batch path so the BatchExecutor callbacks can update the- user-visible status row as each phase lands. AgentProbe + other- sync callers pass ``None`` and never hit the batch path.- """- return await _execute_dream_pass_async(- user_id, status_id=status_id, expert_id=expert_id- )+ """Gather, then hand the pass to the batch route or run its three phases+ and apply them. A pass that ends early (a skip, a billing or phase+ failure) comes back as the result it ended with."""+ try:+ input_bundle = await _gather(run, scope, config=config, status_id=status_id)+ if run.execution_path == "anthropic_batch":+ # Phase 1 goes to Anthropic's Messages Batches API with the+ # output tool, and the pass returns at once. The BatchExecutor+ # polls; dream's batch_callbacks chain phases 2 → 3 + apply as+ # each result lands. Total latency is provider-driven (typically+ # <30min, hard cap 24h per phase batch via+ # BatchExecutor.MAX_BATCH_LIFETIME_SECONDS).+ return await submit_dream_pass_batch(+ run,+ config=config,+ input_bundle=input_bundle,+ status_id=status_id,+ lock_handle=lock_handle,+ )+ inference = _PassInference(+ scope=InferenceScope(user_id=run.user_id, expert_id=scope.expert_id),+ pass_id=run.pass_id,+ config=config,+ )+ sanitized = await _run_phases(run, inference, input_bundle)+ except _PassEnded as ended:+ return ended.result+ return await _apply(run, scope, lock_handle, sanitized, input_bundle)-async def _submit_dream_pass_batch(+async def _gather(+ run: DreamPassRun,+ scope: MemoryScope,*,- user_id: str,- pass_id: str,- started_at: datetime,- monotonic_start: float,- execution_path: ExecutionPath,config: ChatConfig,+ status_id: str | None,+) -> DreamInput:+ """Check the budget, gather the input, and end a pass with nothing to do.++ The billing check runs inside the lock so a paywalled user doesn't burn+ the slot for an eligible concurrent pass on a shared FalkorDB. Raises+ ``_PassEnded`` with the skip or failure that ends the pass early.+ """+ budget_ok, budget_skip = await check_dream_budget(run.user_id, config=config)+ if not budget_ok:+ if budget_skip == "rate_limit_unavailable":+ raise _PassEnded(run.failure(f"billing: {budget_skip}"))+ raise _PassEnded(run.skipped(budget_skip or "insufficient_credits"))+ input_bundle = await gather_dream_input(scope)+ if not input_bundle.episodes and not input_bundle.facts:+ # Nothing to consolidate — skipped so the admin UI can render+ # "nothing to dream about yet".+ raise _PassEnded(run.skipped("no_input"))+ if await _nothing_new_since_last_pass(run, scope, input_bundle, status_id):+ raise _PassEnded(run.skipped("no_new_activity"))+ await record_gathered(run.pass_id, input_bundle)+ return input_bundle+++async def _nothing_new_since_last_pass(+ run: DreamPassRun,+ scope: MemoryScope,input_bundle: DreamInput,status_id: str | None,- dream_lock_handle: DreamLockHandle,-) -> DreamPassResult:- """Submit phase 1 of the dream pass via Anthropic batch + return.-- The orchestrator hands control to the BatchExecutor here: phase 1- (consolidate) is enqueued; phases 2 (recombine) and 3 (sanitize)- fire from dream's batch_callbacks as each prior phase's result- lands. Apply + cost log + JobStatus complete run when sanitize- lands. This function only does the kickoff.-- The job_id link to JobStatus comes from the scheduler's- ``execute_dream_pass_with_status`` wrapper (Step 2). When the- orchestrator is invoked outside that path (e.g. AgentProbe eval),- ``job_id`` is empty and the callback skips the JobStatus updates —- apply still runs, cost still logs.+) -> bool:+ """No NEW activity since the last completed pass: every episode in the+ bundle predates the marker, so re-running all three LLM phases would+ only re-chew already-consolidated material (and, before the empty-pass+ guard in apply.py, manufacture an empty dream chat). Marker read is+ best-effort: missing, unparseable, or Redis-down all mean "run the pass".++ Manual admin triggers (the only callers that set status_id) bypass the+ marker: "dream now" is the memory-debugging tool for re-running a pass+ after prompt/flag/model changes, and a silent no_new_activity skip+ would neuter it for up to the marker's 35-day TTL."""- from .batch_submit import (- persist_input_bundle,- phase_models_for_config,- submit_phase,+ last_completed = (+ await _read_last_completed_marker(scope) if status_id is None else None)+ if last_completed is None or _has_new_episodes_since(+ input_bundle.episodes, last_completed+ ):+ return False+ logger.info(+ "Dream pass %s skipped for user %s — no episodes newer "+ "than last completed pass at %s",+ run.pass_id,+ run.user_id[:12],+ last_completed.isoformat(),+ )+ return True- api_key = config.direct_anthropic_api_key- if not api_key:- # Shouldn't happen — routing.py only picks anthropic_batch when- # the key is present. Guard for type-narrowing + future safety.- return _failure_result(- user_id,- pass_id,- started_at,- monotonic_start,- execution_path,- "anthropic_batch: no Anthropic API key (routing bug)",- )- # Persist DreamInput so the per-phase callbacks can rebuild the- # next phase's prompt without re-fetching from Postgres + FalkorDB.- try:- await persist_input_bundle(- pass_id, input_bundle, lock_token=dream_lock_handle.token- )- except Exception as exc:- return _failure_result(- user_id,- pass_id,- started_at,- monotonic_start,- execution_path,- f"anthropic_batch: input persist failed: {exc}",- )+async def _run_phases(+ run: DreamPassRun, inference: _PassInference, input_bundle: DreamInput+) -> DreamOperations:+ """Consolidate, recombine and sanitize in turn, recording each phase's+ usage on the run and its output on the record as it lands."""+ consolidated = await _phase(+ run, "consolidate", _run_consolidate(inference, input_bundle)+ )+ recombined = await _phase(+ run, "recombine", _run_recombine(inference, input_bundle, consolidated)+ )+ return await _phase(+ run,+ "sanitize",+ _run_sanitize(inference, input_bundle, consolidated, recombined),+ )- # ``status_id`` ties the per-phase callback updates back to the- # JobStatus row the admin trigger created. Empty string when no- # caller wired it (AgentProbe eval, ad-hoc invocation) — the- # callback skips status writes in that case but apply still runs.- try:- submission = await submit_phase(- user_id=user_id,- pass_id=pass_id,- job_id=status_id or "",- phase="consolidate",- phase_models=phase_models_for_config(config),- api_key=api_key,- input_bundle=input_bundle,- )- except Exception as exc:- return _failure_result(- user_id,- pass_id,- started_at,- monotonic_start,- execution_path,- f"anthropic_batch: phase 1 submit failed: {exc}",- )- completed_at = datetime.now(timezone.utc)- logger.info(- "Dream pass %s submitted via Anthropic batch=%s (phase=consolidate)",- pass_id,- submission.provider_batch_id,+async def _phase(+ run: DreamPassRun,+ phase: DreamPhase,+ call: Awaitable[tuple[_Output, PhaseUsage]],+) -> _Output:+ """One phase's output. A phase with no usable answer ends the pass+ (``_PassEnded``) with the completed phases' usage. Failed inference+ attempts are absorbed. A completed phase whose charge failed+ (``PhaseChargeError``) joins that usage too, then its error ends the+ pass as a crash does."""+ try:+ output, usage = await call+ except InferenceError as exc:+ raise _PassEnded(run.failure(f"{phase}: {exc}")) from exc+ except PhaseChargeError as exc:+ run.phases.append(phase_usage(phase, exc.usage))+ raise+ run.phases.append(usage)+ await record_phase_output(run.pass_id, phase, output)+ return output+++async def _apply(+ run: DreamPassRun,+ scope: MemoryScope,+ lock_handle: DreamLockHandle,+ sanitized: DreamOperations,+ input_bundle: DreamInput,+) -> DreamPassResult:+ """Clamp the sanitized operations, apply them and stamp the marker."""+ ops = clamp_operations(+ sanitized,+ len(input_bundle.facts),+ known_fact_uuids=input_bundle.known_fact_uuids,)- # Phase 1 is enqueued — hand the dream lock to the batch callback so it- # spans the full async lifetime (apply runs hours later). Extend the TTL- # to the batch window first; the callback releases it on terminal/failure.- # A failed extend means the lock expired before the handoff — a newer- # pass may already own the graph, so the just-submitted batch must never- # be applied: revoke the pending entry (the poller then never dispatches- # the callback chain) and drop the input bundle. The provider batch is- # orphaned; its results are discarded. The lock is NOT disowned, so the- # context manager's compare-and-delete release stays a safe no-op.- if not await dream_lock_handle.extend(BATCH_LOCK_TTL_SECONDS):- from backend.executor.batch_executor import remove_pending-- from .batch_submit import delete_input_bundle-- await remove_pending(submission.provider_batch_id)- await delete_input_bundle(pass_id)- return _failure_result(- user_id,- pass_id,- started_at,- monotonic_start,- execution_path,- "anthropic_batch: dream lock lost before handoff — batch revoked",- )- dream_lock_handle.disown()+ await record_applying(run.pass_id, ops)+ apply_stats = await apply_operations(+ scope,+ run.pass_id,+ ops,+ known_fact_uuids=input_bundle.known_fact_uuids,+ known_episode_uuids=input_bundle.known_episode_uuids,+ lock_handle=lock_handle,+ )+ # Apply succeeded (even as a no-op) — stamp the marker so the+ # next nightly pass can skip when nothing new has landed.+ # Stamped with the gather-window end so episodes that arrived+ # mid-pass still count as new next time. Sync path only: batch+ # apply runs hours later in batch_callbacks, which doesn't+ # stamp yet.+ await _stamp_last_completed_marker(scope, input_bundle.window_end)+ return _applied_result(run, apply_stats, ops)+++def _applied_result(+ run: DreamPassRun,+ apply_stats: dict[str, int | str | IngestionDrainStatus | DreamOperationsSnapshot],+ ops: DreamOperations,+) -> DreamPassResult:+ snapshot = apply_stats.get("snapshot")+ raw_session_id = apply_stats.get("session_id")++ def _as_int(key: str) -> int:+ v = apply_stats.get(key, 0)+ return int(v) if isinstance(v, (int, str)) and v else 0+return DreamPassResult(- user_id=user_id,- pass_id=pass_id,- started_at=started_at,- completed_at=completed_at,- elapsed_seconds=asyncio.get_event_loop().time() - monotonic_start,- execution_path=execution_path,- skipped=False,- # The pass is async-pending — no usage / ops / session_id yet.- # The BatchExecutor + dream callbacks deliver those when phase- # 3 lands and apply runs.+ user_id=run.user_id,+ pass_id=run.pass_id,+ started_at=run.started_at,+ completed_at=datetime.now(timezone.utc),+ elapsed_seconds=run.elapsed_seconds(),+ execution_path=run.execution_path,+ dropped_forgotten=_as_int("dropped_forgotten"),+ pending_writes=_as_int("pending_writes"),+ failed_writes=_as_int("failed_writes"),+ consolidated_count=_as_int("consolidated_count"),+ proposal_count=_as_int("proposal_count"),+ demotion_count=_as_int("demotion_count"),+ entity_invalidation_count=_as_int("entity_invalidation_count"),+ summary_for_user=ops.summary_for_user,+ # Fail-closed: a missing/malformed drain flag reads as+ # ``timed_out`` (writes at risk), never a confirmed drain.+ ingestion_drain_status=drain_status_from_stats(apply_stats),+ # ``None`` (key absent) on an empty pass — apply skipped the+ # dream session entirely, so there is no id to surface.+ dream_session_id=(raw_session_id if isinstance(raw_session_id, str) else None),+ operations=(+ snapshot if isinstance(snapshot, DreamOperationsSnapshot) else None+ ),+ usage=run.usage(),)+++class _PassEnded(Exception):+ """A pass that ends before apply, carrying the result it ends with."""++ def __init__(self, result: DreamPassResult) -> None:+ super().__init__(result.error or result.skip_reason)+ self.result = resultdiff --git a/autogpt_platform/backend/backend/copilot/dream/orchestrator_test.py b/autogpt_platform/backend/backend/copilot/dream/orchestrator_test.pyindex a13787fd17..6cc7d77c0b 100644--- a/autogpt_platform/backend/backend/copilot/dream/orchestrator_test.py+++ b/autogpt_platform/backend/backend/copilot/dream/orchestrator_test.py@@ -8,13 +8,23 @@ the unit-level safety net for the control-flow.from __future__ import annotations+import asyncio+import loggingfrom contextlib import asynccontextmanager-from datetime import datetime, timezone+from datetime import datetime, timedelta, timezonefrom unittest.mock import AsyncMock, MagicMockimport pytest-from prisma.enums import SubscriptionTier+from prisma.enums import (+ DreamPassPhase,+ DreamPassRoute,+ DreamPassStatus,+ DreamPassTrigger,+ SubscriptionTier,+)+from backend.copilot import rate_limit as rate_limit_mod+from backend.copilot import token_tracking as token_tracking_modfrom backend.copilot import usage_activation_fixturesfrom backend.copilot.graphiti.scope import MemoryScopefrom backend.copilot.inference.complete import StructuredCompletion@@ -25,15 +35,32 @@ from backend.copilot.inference.context import ()from backend.copilot.inference.trace import TracedCallfrom backend.copilot.trial_cost_context import get_trial_cost_context+from backend.data.dream_pass_models import DreamPassDraftfrom backend.data.pro_activation import UsageActivationState+from backend.executor.batch_executor import PendingEntryfrom backend.executor.scheduler import SCHEDULER_DREAM_OPERATION_TIMEOUT_SECONDS+from backend.util.llm.providers import BatchResultRow+from . import batch_handoff as batch_handoff_modfrom . import batch_submitfrom . import billing as billing_mod+from . import clamp as clamp_mod+from . import dedup as dedup_mod+from . import nightly_batch as nightly_batch_modfrom . import orchestrator as orchestrator_modfrom .apply import INGESTION_DRAIN_TIMEOUT_SECONDS, LOCK_DRAIN_RENEWAL_SECONDS+from .batch_callbacks import handle_dream_batch_result+from .batch_submit import persist_input_bundlefrom .fetch import DreamInput, EpisodeRow, FactRow-from .locks import DEFAULT_LOCK_TTL_SECONDS+from .locks import (+ BATCH_LOCK_TTL_SECONDS,+ DEFAULT_LOCK_TTL_SECONDS,+ DreamLockHeld,+ DreamLockLostError,+)+from .locks import dream_lock as real_dream_lock+from .pass_run import DreamPassRun+from .ratification import RatificationResultfrom .schemas import (ConsolidatedFact,ConsolidationOutput,@@ -730,17 +757,13 @@ def test_clamp_operations_demotion_cap_rules():demotions=[DreamDemotion(edge_uuid=f"e{i}", reason="r") for i in range(50)],)# 5% of 100 = 5 (below the absolute cap)- assert len(orchestrator_mod._clamp_operations(ops, 100).demotions) == 5+ assert len(clamp_mod.clamp_operations(ops, 100).demotions) == 5# 5% of 1000 = 50, so the absolute cap bindsassert (- len(orchestrator_mod._clamp_operations(ops, 1000).demotions)- == MAX_DEMOTIONS_PER_PASS+ len(clamp_mod.clamp_operations(ops, 1000).demotions) == MAX_DEMOTIONS_PER_PASS)# Unknown active-fact count -> absolute cap only, never zero- assert (- len(orchestrator_mod._clamp_operations(ops, -1).demotions)- == MAX_DEMOTIONS_PER_PASS- )+ assert len(clamp_mod.clamp_operations(ops, -1).demotions) == MAX_DEMOTIONS_PER_PASSdef test_clamp_operations_small_graph_demotion_cap_floors_at_one():@@ -751,14 +774,14 @@ def test_clamp_operations_small_graph_demotion_cap_floors_at_one():ops = DreamOperations(demotions=[DreamDemotion(edge_uuid=f"e{i}", reason="r") for i in range(50)],)- assert len(orchestrator_mod._clamp_operations(ops, 10).demotions) == 1- assert len(orchestrator_mod._clamp_operations(ops, 1).demotions) == 1- assert len(orchestrator_mod._clamp_operations(ops, 19).demotions) == 1+ assert len(clamp_mod.clamp_operations(ops, 10).demotions) == 1+ assert len(clamp_mod.clamp_operations(ops, 1).demotions) == 1+ assert len(clamp_mod.clamp_operations(ops, 19).demotions) == 1# 20 facts crosses the 5% threshold back to the proportional cap- assert len(orchestrator_mod._clamp_operations(ops, 20).demotions) == 1- assert len(orchestrator_mod._clamp_operations(ops, 40).demotions) == 2+ assert len(clamp_mod.clamp_operations(ops, 20).demotions) == 1+ assert len(clamp_mod.clamp_operations(ops, 40).demotions) == 2# No active facts at all -> no demotion budget- assert len(orchestrator_mod._clamp_operations(ops, 0).demotions) == 0+ assert len(clamp_mod.clamp_operations(ops, 0).demotions) == 0def test_hallucinated_uuid_does_not_consume_cap_slot():@@ -772,12 +795,12 @@ def test_hallucinated_uuid_does_not_consume_cap_slot():DreamDemotion(edge_uuid="f0", reason="r"),],)- clamped = orchestrator_mod._clamp_operations(ops, 10, known_fact_uuids={"f0"})+ clamped = clamp_mod.clamp_operations(ops, 10, known_fact_uuids={"f0"})assert [d.edge_uuid for d in clamped.demotions] == ["f0"]# Without the allowlist the clamp can't pre-filter — the cap slices# the raw list and apply.py's filter remains the only defense.- unfiltered = orchestrator_mod._clamp_operations(ops, 10)+ unfiltered = clamp_mod.clamp_operations(ops, 10)assert [d.edge_uuid for d in unfiltered.demotions] == ["hallucinated"]@@ -849,10 +872,9 @@ def test_clamp_operations_caps_entity_invalidations():EntityInvalidation(entity_uuid=f"ent{i}", reason="r") for i in range(25)],)- clamped = orchestrator_mod._clamp_operations(ops, 100)+ clamped = clamp_mod.clamp_operations(ops, 100)assert (- len(clamped.entity_invalidations)- == orchestrator_mod.MAX_ENTITY_INVALIDATIONS_PER_PASS+ len(clamped.entity_invalidations) == clamp_mod.MAX_ENTITY_INVALIDATIONS_PER_PASS)# The first N proposed invalidations survive, in orderassert [e.entity_uuid for e in clamped.entity_invalidations] == [@@ -1775,7 +1797,7 @@ class TestTransientIntentFilter:],)def test_flags_transient_intent(self, content):- assert orchestrator_mod._is_transient_intent(content) is True+ assert clamp_mod._is_transient_intent(content) is True@pytest.mark.parametrize("content",@@ -1821,7 +1843,7 @@ class TestTransientIntentFilter:],)def test_keeps_durable_facts_goals_and_generic_knowledge(self, content):- assert orchestrator_mod._is_transient_intent(content) is False+ assert clamp_mod._is_transient_intent(content) is Falsedef test_clamp_drops_transient_writes_and_proposals(self):ops = DreamOperations(@@ -1847,7 +1869,7 @@ class TestTransientIntentFilter:],summary_for_user="ok",)- clamped = orchestrator_mod._clamp_operations(ops, active_fact_count=50)+ clamped = clamp_mod.clamp_operations(ops, active_fact_count=50)assert [w.content for w in clamped.writes] == ["Nick prefers Python"]assert [p.content for p in clamped.proposals] == ["Nick and Sarah both work on auth"@@ -1864,7 +1886,7 @@ class TestTransientIntentFilter:),ConsolidatedFact(content="Nick deploys to us-east1", confidence=0.8),]- kept, dropped_count = orchestrator_mod._drop_transient_intent(items)+ kept, dropped_count = clamp_mod._drop_transient_intent(items)assert [k.content for k in kept] == ["Nick prefers Python","Nick deploys to us-east1",@@ -1876,7 +1898,7 @@ class TestTransientIntentFilter:items = [ConsolidatedFact(content="Nick prefers Python", confidence=0.9),]- kept, dropped_count = orchestrator_mod._drop_transient_intent(items)+ kept, dropped_count = clamp_mod._drop_transient_intent(items)assert [k.content for k in kept] == ["Nick prefers Python"]assert dropped_count == 0@@ -1900,7 +1922,7 @@ class TestNearDuplicateWriteDedup:confidence=0.7,),]- kept, dropped = orchestrator_mod._dedupe_near_duplicate_writes(writes)+ kept, dropped = dedup_mod.dedupe_near_duplicate_writes(writes)assert dropped == 1assert len(kept) == 1# The longer, more specific phrasing survives.@@ -1913,7 +1935,7 @@ class TestNearDuplicateWriteDedup:content="Nick prefers Rust for systems work", confidence=0.8),]- kept, dropped = orchestrator_mod._dedupe_near_duplicate_writes(writes)+ kept, dropped = dedup_mod.dedupe_near_duplicate_writes(writes)assert dropped == 0assert len(kept) == 2@@ -1928,7 +1950,7 @@ class TestNearDuplicateWriteDedup:content="Nick deployed the billing service to prod", confidence=0.7),]- kept, dropped = orchestrator_mod._dedupe_near_duplicate_writes(writes)+ kept, dropped = dedup_mod.dedupe_near_duplicate_writes(writes)assert dropped == 0assert len(kept) == 2@@ -1938,7 +1960,7 @@ class TestNearDuplicateWriteDedup:ConsolidatedFact(content="Beta fact about billing", confidence=0.5),ConsolidatedFact(content="Gamma fact about deploys", confidence=0.5),]- kept, dropped = orchestrator_mod._dedupe_near_duplicate_writes(writes)+ kept, dropped = dedup_mod.dedupe_near_duplicate_writes(writes)assert dropped == 0assert [w.content for w in kept] == ["Alpha fact about onboarding",@@ -1959,7 +1981,7 @@ class TestNearDuplicateWriteDedup:confidence=0.7,),]- kept, dropped = orchestrator_mod._dedupe_near_duplicate_writes(writes)+ kept, dropped = dedup_mod.dedupe_near_duplicate_writes(writes)assert dropped == 0assert len(kept) == 2@@ -1979,7 +2001,7 @@ class TestNearDuplicateWriteDedup:source_episode_uuids=["ep-2", "ep-3"],),]- kept, dropped = orchestrator_mod._dedupe_near_duplicate_writes(writes)+ kept, dropped = dedup_mod.dedupe_near_duplicate_writes(writes)assert dropped == 1assert len(kept) == 1# Survivor keeps its own uuids first, then the absorbed extras.@@ -1990,7 +2012,7 @@ class TestNearDuplicateWriteDedup:ConsolidatedFact(content="Alice introduced Bob to Carol", confidence=0.7),ConsolidatedFact(content="Alice introduced Carol to Bob", confidence=0.7),]- kept, dropped = orchestrator_mod._dedupe_near_duplicate_writes(writes)+ kept, dropped = dedup_mod.dedupe_near_duplicate_writes(writes)assert dropped == 0assert len(kept) == 2@@ -1999,10 +2021,26 @@ class TestNearDuplicateWriteDedup:ConsolidatedFact(content="Nick uses vim", confidence=0.7),ConsolidatedFact(content="Nick never uses vim", confidence=0.7),]- kept, dropped = orchestrator_mod._dedupe_near_duplicate_writes(writes)+ kept, dropped = dedup_mod.dedupe_near_duplicate_writes(writes)assert dropped == 0assert len(kept) == 2+ @pytest.mark.parametrize(+ "negated",+ ["User can't use vim", "User can’t use vim"],+ ids=["apostrophe", "typographic_apostrophe"],+ )+ def test_a_contracted_negation_is_not_merged(self, negated):+ """Split at its apostrophe, "can't" left only "t" between the two+ facts, no marker, and their 3/4 overlap merged them."""+ writes = [+ ConsolidatedFact(content="User can use vim", confidence=0.7),+ ConsolidatedFact(content=negated, confidence=0.7),+ ]+ kept, dropped = dedup_mod.dedupe_near_duplicate_writes(writes)+ assert dropped == 0+ assert [w.content for w in kept] == ["User can use vim", negated]+def test_clamp_collapses_duplicate_writes(self):ops = DreamOperations(writes=[@@ -2020,7 +2058,7 @@ class TestNearDuplicateWriteDedup:proposals=[],summary_for_user="ok",)- clamped = orchestrator_mod._clamp_operations(ops, active_fact_count=50)+ clamped = clamp_mod.clamp_operations(ops, active_fact_count=50)contents = [w.content for w in clamped.writes]# The two churn paraphrases collapse to one; revenue fact untouched.assert len(contents) == 2@@ -2035,20 +2073,22 @@ async def test_batch_handoff_revokes_batch_when_lock_extend_fails(mocker):removed (poller never dispatches callbacks), the input bundle isdropped, the lock is NOT disowned, and the pass reports failure."""submission = mocker.MagicMock(provider_batch_id="batch-xyz")- mocker.patch("backend.copilot.dream.batch_submit.persist_input_bundle", AsyncMock())mocker.patch(- "backend.copilot.dream.batch_submit.phase_models_for_config",+ "backend.copilot.dream.batch_handoff.persist_input_bundle", AsyncMock()+ )+ mocker.patch(+ "backend.copilot.dream.batch_handoff.phase_models_for_config",return_value=mocker.MagicMock(),)mocker.patch(- "backend.copilot.dream.batch_submit.submit_phase",+ "backend.copilot.dream.batch_handoff.submit_phase",AsyncMock(return_value=submission),)remove_pending = mocker.patch(- "backend.executor.batch_executor.remove_pending", AsyncMock()+ "backend.copilot.dream.batch_handoff.remove_pending", AsyncMock())delete_bundle = mocker.patch(- "backend.copilot.dream.batch_submit.delete_input_bundle", AsyncMock()+ "backend.copilot.dream.batch_handoff.delete_input_bundle", AsyncMock())handle = mocker.MagicMock()handle.extend = AsyncMock(return_value=False)@@ -2057,24 +2097,685 @@ async def test_batch_handoff_revokes_batch_when_lock_extend_fails(mocker):config = mocker.MagicMock()config.direct_anthropic_api_key = "key"- result = await orchestrator_mod._submit_dream_pass_batch(- user_id="u-lock-lost",- pass_id="p-lock-lost",- started_at=datetime.now(timezone.utc),- monotonic_start=0.0,- execution_path="anthropic_batch",+ run = DreamPassRun.begin("u-lock-lost", "anthropic_batch")++ result = await batch_handoff_mod.submit_dream_pass_batch(+ run,config=config,input_bundle=_build_input(),status_id=None,- dream_lock_handle=handle,+ lock_handle=handle,)remove_pending.assert_awaited_once_with("batch-xyz")- delete_bundle.assert_awaited_once_with("p-lock-lost")+ delete_bundle.assert_awaited_once_with(run.pass_id)handle.disown.assert_not_called()assert result.error and "lock lost" in result.error+# ---------------------------------------------------------------------------+# The pass's durable DreamPass row, as execute_dream_pass writes it+# ---------------------------------------------------------------------------+++async def test_a_sync_pass_records_each_step_on_its_row(mocker, fake_dream_db):+ input_bundle = _build_input()+ mocker.patch.object(+ orchestrator_mod, "gather_dream_input", AsyncMock(return_value=input_bundle)+ )+ _stub_three_phases_and_apply(mocker)++ result = await orchestrator_mod.execute_dream_pass("u")++ assert result.error is None and result.skipped is False+ assert fake_dream_db.writes[0] == (+ result.pass_id,+ DreamPassDraft(+ id=result.pass_id,+ user_id="u",+ scope_key=MemoryScope.for_user("u").scope_key,+ route=DreamPassRoute.SYNC,+ trigger=DreamPassTrigger.CRON,+ started_at=result.started_at,+ ),+ )+ assert fake_dream_db.statuses(result.pass_id) == [+ DreamPassStatus.RUNNING,+ DreamPassStatus.APPLYING,+ DreamPassStatus.COMPLETE,+ ]+ assert fake_dream_db.phases(result.pass_id) == [+ DreamPassPhase.GATHER,+ DreamPassPhase.CONSOLIDATE,+ DreamPassPhase.RECOMBINE,+ DreamPassPhase.SANITIZE,+ DreamPassPhase.APPLY,+ DreamPassPhase.DONE,+ ]+ row = fake_dream_db.rows[result.pass_id]+ assert (row["window_start"], row["window_end"]) == (+ input_bundle.window_start,+ input_bundle.window_end,+ )+ assert row["phase_outputs"] == {+ "consolidate": ConsolidationOutput(facts=[]),+ "recombine": RecombinationOutput(proposals=[]),+ "sanitize": DreamOperations(summary_for_user="ok"),+ }+ assert row["operations"]["planned"] == DreamOperations(summary_for_user="ok")+ assert row["operations"]["applied"].dream_session_id == "s"+ assert row["usage"] == result.usage+ assert row["completed_at"] == result.completed_at+ # The sync route keeps no input bundle: nothing resumes it from the row.+ assert "input_bundle" not in row+++ "trigger, recorded",+ [("admin", DreamPassTrigger.ADMIN), ("eval", DreamPassTrigger.EVAL)],+)+async def test_the_callers_trigger_is_recorded(+ mocker, fake_dream_db, trigger, recorded+):+ mocker.patch.object(+ orchestrator_mod,+ "gather_dream_input",+ AsyncMock(return_value=_build_input(episodes=0, facts=0)),+ )++ result = await orchestrator_mod.execute_dream_pass("u", trigger=trigger)++ pass_id, draft = fake_dream_db.writes[0]+ assert pass_id == result.pass_id+ assert isinstance(draft, DreamPassDraft) and draft.trigger is recorded+++async def test_a_skipped_pass_closes_its_row_as_skipped(mocker, fake_dream_db):+ mocker.patch.object(+ orchestrator_mod,+ "gather_dream_input",+ AsyncMock(return_value=_build_input(episodes=0, facts=0)),+ )++ result = await orchestrator_mod.execute_dream_pass("u")++ assert fake_dream_db.statuses(result.pass_id) == [+ DreamPassStatus.RUNNING,+ DreamPassStatus.SKIPPED,+ ]+ assert fake_dream_db.phases(result.pass_id) == [DreamPassPhase.GATHER]+ row = fake_dream_db.rows[result.pass_id]+ assert row["skip_reason"] == "no_input"+ assert row["completed_at"] == result.completed_at+++async def test_a_pass_that_finds_the_lock_held_is_recorded_skipped(+ mocker, fake_dream_db+):+ @asynccontextmanager+ async def busy_lock(*args, **kwargs):+ raise DreamLockHeld("u")+ yield # pragma: no cover++ mocker.patch.object(orchestrator_mod, "dream_lock", busy_lock)++ result = await orchestrator_mod.execute_dream_pass("u")++ assert fake_dream_db.statuses(result.pass_id) == [+ DreamPassStatus.RUNNING,+ DreamPassStatus.SKIPPED,+ ]+ assert fake_dream_db.rows[result.pass_id]["skip_reason"] == "lock_held"+++async def test_a_phase_failure_closes_the_row_errored_at_that_phase(+ mocker, fake_dream_db+):+ mocker.patch.object(+ orchestrator_mod, "gather_dream_input", AsyncMock(return_value=_build_input())+ )+ mocker.patch.object(+ orchestrator_mod,+ "structured_complete",+ AsyncMock(+ side_effect=[+ _wrap(ConsolidationOutput(facts=[])),+ InferenceError("provider down"),+ ]+ ),+ )+ apply_mock = mocker.patch.object(orchestrator_mod, "apply_operations", AsyncMock())++ result = await orchestrator_mod.execute_dream_pass("u")++ assert result.error == "recombine: provider down"+ apply_mock.assert_not_awaited()+ assert fake_dream_db.statuses(result.pass_id) == [+ DreamPassStatus.RUNNING,+ DreamPassStatus.ERRORED,+ ]+ assert fake_dream_db.phases(result.pass_id) == [+ DreamPassPhase.GATHER,+ DreamPassPhase.CONSOLIDATE,+ DreamPassPhase.RECOMBINE,+ ]+ row = fake_dream_db.rows[result.pass_id]+ assert row["error"] == result.error+ assert list(row["phase_outputs"]) == ["consolidate"]+ assert row["usage"] == result.usage+ assert [p.phase for p in row["usage"].phases] == ["consolidate"]+++async def test_a_store_outage_never_fails_the_pass(mocker, fake_dream_db, caplog):+ fake_dream_db.fail = True+ mocker.patch.object(+ orchestrator_mod, "gather_dream_input", AsyncMock(return_value=_build_input())+ )+ apply_mock = _stub_three_phases_and_apply(mocker)++ with caplog.at_level(logging.WARNING):+ result = await orchestrator_mod.execute_dream_pass("u")++ assert result.error is None and result.skipped is False+ assert result.dream_session_id == "s"+ apply_mock.assert_awaited_once()+ assert fake_dream_db.writes == []+ assert "could not insert its record" in caplog.text+ assert "could not record the outcome" in caplog.text+++def _batch_route(mocker, *, lock_extends: bool) -> MagicMock:+ """Route the pass to the Anthropic batch path with its submit mocked;+ returns the lock handle the pass holds."""+ mocker.patch.object(+ orchestrator_mod,+ "resolve_dream_execution_path",+ return_value="anthropic_batch",+ )+ config = MagicMock()+ config.direct_anthropic_api_key = "key"+ mocker.patch.object(orchestrator_mod, "ChatConfig", return_value=config)+ handle = MagicMock()+ handle.token = "tok"+ handle.extend = AsyncMock(return_value=lock_extends)++ @asynccontextmanager+ async def held_lock(*args, **kwargs):+ yield handle++ mocker.patch.object(orchestrator_mod, "dream_lock", held_lock)+ mocker.patch(+ "backend.copilot.dream.batch_handoff.persist_input_bundle", AsyncMock()+ )+ mocker.patch(+ "backend.copilot.dream.batch_handoff.phase_models_for_config", return_value={}+ )+ mocker.patch(+ "backend.copilot.dream.batch_handoff.submit_phase",+ AsyncMock(return_value=MagicMock(provider_batch_id="batch-xyz")),+ )+ return handle+++async def test_a_batch_submit_records_the_batch_and_leaves_the_end_to_callbacks(+ mocker, fake_dream_db+):+ _batch_route(mocker, lock_extends=True)+ input_bundle = _build_input()+ mocker.patch.object(+ orchestrator_mod, "gather_dream_input", AsyncMock(return_value=input_bundle)+ )+ before = datetime.now(timezone.utc)++ result = await orchestrator_mod.execute_dream_pass("u")++ assert result.execution_path == "anthropic_batch" and result.error is None+ assert fake_dream_db.statuses(result.pass_id) == [+ DreamPassStatus.RUNNING,+ DreamPassStatus.SUBMITTED,+ ]+ assert fake_dream_db.phases(result.pass_id) == [+ DreamPassPhase.GATHER,+ DreamPassPhase.CONSOLIDATE,+ ]+ row = fake_dream_db.rows[result.pass_id]+ assert row["route"] is DreamPassRoute.ANTHROPIC_BATCH+ assert row["provider_batch_id"] == "batch-xyz"+ assert row["input_bundle"] == input_bundle+ assert row["lease_token"] == "tok"+ assert row["submitted_at"] >= before+ assert row["lease_expires_at"] - row["submitted_at"] == timedelta(+ seconds=BATCH_LOCK_TTL_SECONDS+ )+ assert "completed_at" not in row+++async def test_a_batch_handoff_that_lost_the_lock_is_recorded_errored(+ mocker, fake_dream_db+):+ _batch_route(mocker, lock_extends=False)+ mocker.patch("backend.copilot.dream.batch_handoff.remove_pending", AsyncMock())+ mocker.patch("backend.copilot.dream.batch_handoff.delete_input_bundle", AsyncMock())+ mocker.patch.object(+ orchestrator_mod, "gather_dream_input", AsyncMock(return_value=_build_input())+ )++ result = await orchestrator_mod.execute_dream_pass("u")++ assert result.error and "lock lost" in result.error+ assert fake_dream_db.statuses(result.pass_id) == [+ DreamPassStatus.RUNNING,+ DreamPassStatus.ERRORED,+ ]+ assert "provider_batch_id" not in fake_dream_db.rows[result.pass_id]+ # The revoked batch still runs and bills at the provider, unread: what+ # the pass used is unknown, not zero.+ assert result.usage is None+ assert "usage" not in fake_dream_db.rows[result.pass_id]+++# What the provider billed for each phase answer these tests script.+_BILLED = InferenceUsage(+ model="claude-sonnet-5",+ input_tokens=100,+ output_tokens=20,+ cost_usd=0.001,+ cost_source="provider",+ payer="platform_allowance",+)+++def _billed(value) -> StructuredCompletion:+ """A phase answer the provider billed 100 input + 20 output tokens and+ $0.001 for."""+ return StructuredCompletion(value=value, usage=_BILLED)+++def _three_billed_phases(mocker) -> None:+ mocker.patch.object(+ orchestrator_mod,+ "structured_complete",+ AsyncMock(+ side_effect=[+ _billed(ConsolidationOutput(facts=[])),+ _billed(RecombinationOutput(proposals=[])),+ _billed(DreamOperations(summary_for_user="ok")),+ ]+ ),+ )+++ "failure",+ [RuntimeError("apply exploded"), DreamLockLostError("u")],+ ids=["apply_crash", "lock_lost"],+)+async def test_a_failure_after_the_phases_keeps_all_three_phases_usage(+ mocker, fake_dream_db, failure+):+ mocker.patch.object(+ orchestrator_mod, "gather_dream_input", AsyncMock(return_value=_build_input())+ )+ _three_billed_phases(mocker)+ mocker.patch.object(+ orchestrator_mod, "apply_operations", AsyncMock(side_effect=failure)+ )++ result = await orchestrator_mod.execute_dream_pass("u")++ assert result.error == str(failure)+ assert result.usage is not None+ assert [p.phase for p in result.usage.phases] == [+ "consolidate",+ "recombine",+ "sanitize",+ ]+ assert (result.usage.total_input_tokens, result.usage.total_output_tokens) == (+ 300,+ 60,+ )+ assert result.usage.total_cost_usd == pytest.approx(0.003)+ row = fake_dream_db.rows[result.pass_id]+ assert row["status"] is DreamPassStatus.ERRORED+ assert row["usage"] == result.usage+++_PHASES = ("consolidate", "recombine", "sanitize")+++def _answers_until(phase: str, answer: str) -> list[StructuredCompletion | Exception]:+ """The provider's billed answers up to and including *phase*'s, which+ parses or, for ``failed_parse``, comes back billed but unparseable."""+ outputs = [+ ConsolidationOutput(facts=[]),+ RecombinationOutput(proposals=[]),+ DreamOperations(summary_for_user="ok"),+ ]+ answers: list[StructuredCompletion | Exception] = [+ _billed(output) for output in outputs[: _PHASES.index(phase) + 1]+ ]+ if answer == "failed_parse":+ answers[-1] = InferenceError("answer did not parse", usage=_BILLED)+ return answers+++def _fail_the_charge(mocker, phase: str, failure: str) -> AsyncMock:+ """Charge through the real accounting chain, failing *phase*'s charge:+ at the trial-cost ledger (``trial_cost``) or at the cost log, after the+ spend counters were charged (``cost_log``). Returns the step every+ phase's charge reaches, to count the charges."""+ mocker.patch.object(+ orchestrator_mod, "record_phase_cost", billing_mod.record_phase_cost+ )+ mocker.patch.object(token_tracking_mod, "get_current_envelope", return_value=None)+ if failure == "cost_log":++ def cost_log(entry) -> None:+ if entry.block_name == f"copilot:dream:{phase}":+ raise RuntimeError("cost log scheduling failed after the charge")++ mocker.patch.object(token_tracking_mod, "_schedule_cost_log", cost_log)+ return mocker.patch.object(token_tracking_mod, "record_cost_usage", AsyncMock())+ ledger = AsyncMock(+ side_effect=[None] * _PHASES.index(phase)+ + [ConnectionError("trial cost database unavailable")]+ )+ mocker.patch.object(token_tracking_mod, "_schedule_cost_log")+ mocker.patch(+ "backend.copilot.trial_cost_context.get_ready_usage_state",+ AsyncMock(+ side_effect=lambda user_id: UsageActivationState(+ user_id=user_id,+ tier=SubscriptionTier.TRIAL,+ ready=True,+ trial_id="trial-1",+ )+ ),+ )+ mocker.patch.object(+ rate_limit_mod,+ "credit_db",+ return_value=MagicMock(record_subscription_trial_cost=ledger),+ )+ mocker.patch.object(+ rate_limit_mod, "get_redis_async", AsyncMock(return_value=MagicMock())+ )+ mocker.patch.object(rate_limit_mod, "_incr_counter_atomic", AsyncMock())+ return ledger++[email protected]("phase", _PHASES)+ "answer, failure, error",+ [+ ("parsed", "trial_cost", "trial cost database unavailable"),+ ("parsed", "cost_log", "cost log scheduling failed after the charge"),+ ],+ ids=["trial_cost", "cost_log"],+)+async def test_a_charge_that_fails_keeps_every_billed_phases_usage(+ mocker, fake_dream_db, phase, answer, failure, error+):+ """The provider answered, and billed, every phase up to *phase*; then+ charging *phase* fails in the accounting chain. The pass fails on that+ error without charging anything twice, and its result and its record+ carry the usage of every billed phase, *phase*'s included."""+ mocker.patch.object(+ orchestrator_mod, "gather_dream_input", AsyncMock(return_value=_build_input())+ )+ mocker.patch.object(+ orchestrator_mod,+ "structured_complete",+ AsyncMock(side_effect=_answers_until(phase, answer)),+ )+ charges = _fail_the_charge(mocker, phase, failure)+ apply_mock = mocker.patch.object(orchestrator_mod, "apply_operations", AsyncMock())++ result = await orchestrator_mod.execute_dream_pass("u")++ billed = _PHASES.index(phase) + 1+ assert result.error == error+ assert charges.await_count == billed+ apply_mock.assert_not_awaited()+ assert result.usage is not None+ assert [p.phase for p in result.usage.phases] == list(_PHASES[:billed])+ assert (result.usage.total_input_tokens, result.usage.total_output_tokens) == (+ 100 * billed,+ 20 * billed,+ )+ assert result.usage.total_cost_usd == pytest.approx(0.001 * billed)+ row = fake_dream_db.rows[result.pass_id]+ assert row["status"] is DreamPassStatus.ERRORED+ assert row["usage"] == result.usage++[email protected]("phase", _PHASES)+async def test_failed_inference_is_absorbed_in_ledger_and_durable_pass(+ mocker, fake_dream_db, phase+):+ mocker.patch.object(+ orchestrator_mod, "gather_dream_input", AsyncMock(return_value=_build_input())+ )+ mocker.patch.object(+ orchestrator_mod,+ "structured_complete",+ AsyncMock(side_effect=_answers_until(phase, "failed_parse")),+ )+ charges = _fail_the_charge(mocker, phase, "trial_cost")+ apply_mock = mocker.patch.object(orchestrator_mod, "apply_operations", AsyncMock())++ result = await orchestrator_mod.execute_dream_pass("u")++ completed = _PHASES.index(phase)+ assert result.error == f"{phase}: answer did not parse"+ assert charges.await_count == completed+ apply_mock.assert_not_awaited()+ assert result.usage is not None+ assert [p.phase for p in result.usage.phases] == list(_PHASES[:completed])+ assert result.usage.total_input_tokens == 100 * completed+ assert result.usage.total_cost_usd == pytest.approx(0.001 * completed)+ assert fake_dream_db.rows[result.pass_id]["usage"] == result.usage+++def _ends(mocker, ending: str) -> None:+ """Stub the pass to end the way *ending* names."""+ bundle = _build_input(episodes=0, facts=0) if ending == "no_input" else None+ mocker.patch.object(+ orchestrator_mod,+ "gather_dream_input",+ AsyncMock(return_value=bundle or _build_input()),+ )+ if ending == "phase_failure":+ mocker.patch.object(+ orchestrator_mod,+ "structured_complete",+ AsyncMock(side_effect=InferenceError("provider down")),+ )+ return+ apply_mock = _stub_three_phases_and_apply(mocker)+ if ending == "apply_crash":+ apply_mock.side_effect = RuntimeError("apply exploded")+++ "ending, status",+ [+ ("complete", DreamPassStatus.COMPLETE),+ ("phase_failure", DreamPassStatus.ERRORED),+ ("no_input", DreamPassStatus.SKIPPED),+ ("apply_crash", DreamPassStatus.ERRORED),+ ],+)+async def test_the_outcome_is_recorded_before_the_lock_is_released(+ mocker, fake_dream_db, fake_dream_redis, ending, status+):+ """With a healthy store the outcome is persisted before the lock is+ released. A dropped outcome write can still leave an open row behind a+ free lock (see ``_run_locked``); that case is the reaper's."""+ mocker.patch(+ "backend.data.redis_client.get_redis_async",+ AsyncMock(return_value=fake_dream_redis),+ )+ mocker.patch.object(orchestrator_mod, "dream_lock", real_dream_lock)+ _ends(mocker, ending)+ lock_key = MemoryScope.for_user("u").redis_key("dream_lock")+ held_at_outcome: list[bool] = []+ record = fake_dream_db.update_dream_pass++ async def watched(pass_id, update):+ if update.status is status:+ held_at_outcome.append(lock_key in fake_dream_redis.store)+ return await record(pass_id, update)++ mocker.patch.object(fake_dream_db, "update_dream_pass", watched)++ result = await orchestrator_mod.execute_dream_pass("u")++ assert fake_dream_db.rows[result.pass_id]["status"] is status+ assert held_at_outcome == [True]+ assert lock_key not in fake_dream_redis.store+++def _consolidate_delivery(pass_id: str) -> tuple[PendingEntry, list[BatchResultRow]]:+ now = datetime.now(timezone.utc)+ entry = PendingEntry(+ provider="anthropic",+ provider_batch_id="batch-xyz",+ callback_namespace="dream_pass",+ submitted_at=now,+ next_poll_at=now,+ payload={+ "user_id": "u",+ "cost_context": {"user_id": "u", "trial_id": None, "generation": None},+ "pass_id": pass_id,+ "job_id": "",+ "phase": "consolidate",+ "phase_models": {+ "consolidate": "claude-sonnet-5",+ "recombine": "claude-opus-5-5",+ "sanitize": "claude-sonnet-5",+ },+ },+ )+ row = BatchResultRow(+ custom_id=f"{pass_id}_consolidate",+ content='{"facts": []}',+ input_tokens=10,+ output_tokens=20,+ )+ return entry, [row]+++async def test_a_late_submit_write_keeps_the_first_callbacks_progress(+ mocker, fake_dream_db, fake_dream_redis+):+ """The submit's record write lands only after the first callback has+ moved the row on (it is written once the batch is already pollable).+ It must not pull the row back to consolidate or its first batch."""+ mocker.patch(+ "backend.data.redis_client.get_redis_async",+ AsyncMock(return_value=fake_dream_redis),+ )+ _batch_route(mocker, lock_extends=True)+ mocker.patch(+ "backend.copilot.dream.batch_handoff.persist_input_bundle",+ persist_input_bundle,+ )+ mocker.patch.object(+ orchestrator_mod, "gather_dream_input", AsyncMock(return_value=_build_input())+ )+ mocker.patch(+ "backend.copilot.dream.batch_callbacks.submit_phase",+ AsyncMock(return_value=MagicMock(provider_batch_id="batch-recombine")),+ )+ mocker.patch(+ "backend.copilot.dream.batch_callbacks._anthropic_api_key",+ return_value="sk-ant-test",+ )+ entered, release = asyncio.Event(), asyncio.Event()+ record = fake_dream_db.update_dream_pass++ async def submit_held(pass_id, update):+ if update.status is DreamPassStatus.SUBMITTED:+ entered.set()+ await release.wait()+ return await record(pass_id, update)++ mocker.patch.object(fake_dream_db, "update_dream_pass", submit_held)++ task = asyncio.create_task(orchestrator_mod.execute_dream_pass("u"))+ await asyncio.wait_for(entered.wait(), 5)+ pass_id = next(iter(fake_dream_db.rows))+ await handle_dream_batch_result(*_consolidate_delivery(pass_id))+ assert fake_dream_db.rows[pass_id]["phase"] is DreamPassPhase.RECOMBINE+ release.set()+ await task++ row = fake_dream_db.rows[pass_id]+ assert row["status"] is DreamPassStatus.SUBMITTED+ assert row["phase"] is DreamPassPhase.RECOMBINE+ assert row["provider_batch_id"] == "batch-recombine"+ assert list(row["phase_outputs"]) == ["consolidate"]+ assert row["lease_token"] == "tok"+++async def test_a_stalled_record_store_costs_each_write_only_its_deadline(+ mocker, stalled_dream_db+):+ mocker.patch.object(+ orchestrator_mod, "gather_dream_input", AsyncMock(return_value=_build_input())+ )+ apply_mock = _stub_three_phases_and_apply(mocker)++ result = await asyncio.wait_for(orchestrator_mod.execute_dream_pass("u"), 10)++ assert result.error is None and result.dream_session_id == "s"+ apply_mock.assert_awaited_once()+ # The insert and six updates, each abandoned at the deadline.+ assert (stalled_dream_db.started, stalled_dream_db.cancelled) == (7, 7)+++async def test_the_nightly_fan_out_reaches_ratification_past_a_stalled_store(+ mocker, stalled_dream_db+):+ mocker.patch.object(+ orchestrator_mod, "gather_dream_input", AsyncMock(return_value=_build_input())+ )+ _stub_three_phases_and_apply(mocker)+ mocker.patch.object(+ nightly_batch_mod, "check_dream_budget", AsyncMock(return_value=(True, None))+ )+ ratification = mocker.patch.object(+ nightly_batch_mod,+ "run_ratification_pass",+ AsyncMock(+ return_value=RatificationResult(+ user_id="u", started_at=datetime.now(timezone.utc)+ )+ ),+ )++ result = await asyncio.wait_for(nightly_batch_mod.run_nightly_batch_submit("u"), 10)++ ratification.assert_awaited_once_with("u", expert_id=None)+ assert result.dream is not None and result.dream.error is None++@pytest.mark.asyncioasync def test_each_phase_cost_is_charged_under_the_passs_trial_attribution(mocker, usage_snapshotdiff --git a/autogpt_platform/backend/backend/copilot/dream/pass_record.py b/autogpt_platform/backend/backend/copilot/dream/pass_record.pynew file mode 100644index 0000000000..cabb82a1fc--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/dream/pass_record.py@@ -0,0 +1,233 @@+"""What each step of a dream pass writes to its ``DreamPass`` row, and what+the row reads back as.++Pure: how a route, a trigger and a step are named on the row, the insert or+update each transition makes (``store.py`` writes them, bounded), and the+``DreamPassResult`` a row describes, for the admin API and the eval driver.+"""++from datetime import datetime, timedelta, timezone+from typing import Literal++from prisma.enums import (+ DreamPassPhase,+ DreamPassRoute,+ DreamPassStatus,+ DreamPassTrigger,+)+from pydantic import BaseModel++from backend.copilot.graphiti.scope import MemoryScope+from backend.data.dream_pass_models import (+ DreamPassApplied,+ DreamPassDraft,+ DreamPassOperations,+ DreamPassRecord,+ DreamPassUpdate,+ DreamPhaseOutputs,+)++from .fetch import DreamInput+from .routing import ExecutionPath+from .schemas import DreamOperations, DreamPassResult, DreamPassUsage, DreamPhase++# What started a pass: the nightly job, the admin trigger, or an eval run.+DreamTrigger = Literal["cron", "admin", "eval"]++# Errors are capped as the admin job row caps them (job_status.mark_errored).+MAX_ERROR_CHARS = 2000++_ROUTES: dict[ExecutionPath, DreamPassRoute] = {+ "sync_baseline": DreamPassRoute.SYNC,+ "anthropic_batch": DreamPassRoute.ANTHROPIC_BATCH,+}+_EXECUTION_PATHS: dict[DreamPassRoute, ExecutionPath] = {+ route: path for path, route in _ROUTES.items()+}+_TRIGGERS: dict[DreamTrigger, DreamPassTrigger] = {+ "cron": DreamPassTrigger.CRON,+ "admin": DreamPassTrigger.ADMIN,+ "eval": DreamPassTrigger.EVAL,+}+# The step a pass is at while one of its phases runs, or waits on its batch.+_PHASE_STEPS: dict[DreamPhase, DreamPassPhase] = {+ "consolidate": DreamPassPhase.CONSOLIDATE,+ "recombine": DreamPassPhase.RECOMBINE,+ "sanitize": DreamPassPhase.SANITIZE,+}+# The step a pass is at once a phase's output is in.+_STEP_AFTER: dict[DreamPhase, DreamPassPhase] = {+ "consolidate": DreamPassPhase.RECOMBINE,+ "recombine": DreamPassPhase.SANITIZE,+ "sanitize": DreamPassPhase.APPLY,+}+++def new_pass(+ pass_id: str,+ scope: MemoryScope,+ *,+ route: ExecutionPath,+ trigger: DreamTrigger,+ started_at: datetime,+) -> DreamPassDraft:+ """A pass's first row: running, gathering its input."""+ return DreamPassDraft(+ id=pass_id,+ user_id=scope.owner_user_id,+ expert_id=scope.expert_id,+ scope_key=scope.scope_key,+ route=_ROUTES[route],+ trigger=_TRIGGERS[trigger],+ started_at=started_at,+ )+++def gathered(input_bundle: DreamInput) -> DreamPassUpdate:+ """The input is in: the window it covers; consolidate is next."""+ return DreamPassUpdate(+ phase=DreamPassPhase.CONSOLIDATE,+ window_start=input_bundle.window_start,+ window_end=input_bundle.window_end,+ )+++def phase_output(phase: DreamPhase, output: BaseModel) -> DreamPassUpdate:+ """A phase's validated output, on either route; the pass moves on."""+ return DreamPassUpdate(+ phase=_STEP_AFTER[phase],+ phase_outputs=DreamPhaseOutputs.model_validate({phase: output}),+ )+++def applying(ops: DreamOperations) -> DreamPassUpdate:+ """Apply is about to run on *ops*, the clamped operations."""+ return DreamPassUpdate(+ status=DreamPassStatus.APPLYING,+ phase=DreamPassPhase.APPLY,+ operations=DreamPassOperations(planned=ops),+ )+++def submitted(+ *,+ input_bundle: DreamInput,+ provider_batch_id: str,+ lease_token: str,+ lease_ttl_seconds: int,+) -> DreamPassUpdate:+ """The batch route took the pass: consolidate's batch is in flight, and the+ dream lock is held for the callbacks until it lapses."""+ now = datetime.now(timezone.utc)+ return DreamPassUpdate(+ status=DreamPassStatus.SUBMITTED,+ phase=_PHASE_STEPS["consolidate"],+ provider_batch_id=provider_batch_id,+ input_bundle=input_bundle,+ lease_token=lease_token,+ lease_expires_at=now + timedelta(seconds=lease_ttl_seconds),+ submitted_at=now,+ )+++def next_batch(phase: DreamPhase, provider_batch_id: str) -> DreamPassUpdate:+ """*phase*'s batch is in flight."""+ return DreamPassUpdate(+ phase=_PHASE_STEPS[phase], provider_batch_id=provider_batch_id+ )+++def handed_to_batch(result: DreamPassResult) -> bool:+ """The sync entry point handed the pass to the batch route: its end is the+ batch callbacks' to write."""+ return (+ result.execution_path == "anthropic_batch"+ and not result.skipped+ and result.error is None+ )+++def outcome(result: DreamPassResult, usage: DreamPassUsage | None) -> DreamPassUpdate:+ """How a pass ended: skipped, failed, or applied."""+ finished = result.completed_at or datetime.now(timezone.utc)+ if result.skipped:+ return DreamPassUpdate(+ status=DreamPassStatus.SKIPPED,+ skip_reason=result.skip_reason,+ completed_at=finished,+ )+ if result.error is not None:+ return failed(result.error, usage, finished)+ return DreamPassUpdate(+ status=DreamPassStatus.COMPLETE,+ phase=DreamPassPhase.DONE,+ operations=DreamPassOperations(applied=_applied(result)),+ usage=usage,+ applied_at=finished,+ completed_at=finished,+ )+++def failed(+ error: str, usage: DreamPassUsage | None, finished: datetime+) -> DreamPassUpdate:+ return DreamPassUpdate(+ status=DreamPassStatus.ERRORED,+ error=error[:MAX_ERROR_CHARS],+ usage=usage,+ completed_at=finished,+ )+++def dream_pass_result_from_row(row: DreamPassRecord) -> DreamPassResult:+ """The ``DreamPassResult`` a pass's row describes, on either route.++ A pass still in flight reads as neither skipped nor errored, with no+ completion time and no usage yet.+ """+ applied = row.operations.applied or DreamPassApplied()+ return DreamPassResult(+ user_id=row.user_id,+ pass_id=row.id,+ started_at=row.started_at,+ completed_at=row.completed_at,+ elapsed_seconds=_elapsed_seconds(row),+ execution_path=_EXECUTION_PATHS[row.route],+ consolidated_count=applied.consolidated_count,+ proposal_count=applied.proposal_count,+ demotion_count=applied.demotion_count,+ entity_invalidation_count=applied.entity_invalidation_count,+ dropped_forgotten=applied.dropped_forgotten,+ pending_writes=applied.pending_writes,+ failed_writes=applied.failed_writes,+ summary_for_user=applied.summary_for_user,+ dream_session_id=applied.dream_session_id,+ ingestion_drain_status=applied.ingestion_drain_status,+ operations=applied.snapshot,+ usage=row.usage,+ error=row.error,+ skipped=row.status == DreamPassStatus.SKIPPED,+ skip_reason=row.skip_reason,+ )+++def _applied(result: DreamPassResult) -> DreamPassApplied:+ return DreamPassApplied(+ consolidated_count=result.consolidated_count,+ proposal_count=result.proposal_count,+ demotion_count=result.demotion_count,+ entity_invalidation_count=result.entity_invalidation_count,+ dropped_forgotten=result.dropped_forgotten,+ pending_writes=result.pending_writes,+ failed_writes=result.failed_writes,+ summary_for_user=result.summary_for_user,+ dream_session_id=result.dream_session_id,+ ingestion_drain_status=result.ingestion_drain_status,+ snapshot=result.operations,+ )+++def _elapsed_seconds(row: DreamPassRecord) -> float | None:+ if row.started_at is None or row.completed_at is None:+ return None+ return (row.completed_at - row.started_at).total_seconds()diff --git a/autogpt_platform/backend/backend/copilot/dream/pass_run.py b/autogpt_platform/backend/backend/copilot/dream/pass_run.pynew file mode 100644index 0000000000..430983704c--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/dream/pass_run.py@@ -0,0 +1,83 @@+"""One dream pass while the sync entry point runs it: who and what it is,+when it started, and what its phases have used so far.++Built once when a pass starts and handed down, so every result the pass can+end with (a skip, a failure at any step, an unexpected error, the hand-off to+the batch route) carries the same identity and timing, and every failure+carries the usage of every phase billed before it.+"""++import asyncio+import uuid+from datetime import datetime, timezone++from pydantic import BaseModel, Field++from .routing import ExecutionPath+from .schemas import DreamPassResult, DreamPassUsage, PhaseUsage+from .usage import aggregate_usage+++class DreamPassRun(BaseModel):+ user_id: str+ pass_id: str+ started_at: datetime+ monotonic_start: float+ execution_path: ExecutionPath+ phases: list[PhaseUsage] = Field(default_factory=list)++ @classmethod+ def begin(cls, user_id: str, execution_path: ExecutionPath) -> "DreamPassRun":+ return cls(+ user_id=user_id,+ pass_id=str(uuid.uuid4()),+ started_at=datetime.now(timezone.utc),+ monotonic_start=asyncio.get_event_loop().time(),+ execution_path=execution_path,+ )++ def usage(self) -> DreamPassUsage:+ """What the phases billed so far used, per phase and in total."""+ return aggregate_usage(self.phases, self.execution_path)++ def elapsed_seconds(self) -> float:+ return asyncio.get_event_loop().time() - self.monotonic_start++ def failure(self, error: str) -> DreamPassResult:+ """The pass failed. On the sync route it still carries the usage of+ the phases completed before the error. The batch route's phases bill at the provider and+ reach the callbacks, never this side (a batch submitted before the+ failure still runs), so its usage here is unknown."""+ if self.execution_path == "anthropic_batch":+ return self._ended(error=error)+ return self._ended(error=error, usage=self.usage())++ def skipped(self, reason: str) -> DreamPassResult:+ return self._ended(skipped=True, skip_reason=reason)++ def handed_off(self) -> DreamPassResult:+ """Phase 1 is queued on the batch route: no usage, operations or+ session yet. The batch callbacks deliver those when phase 3 lands and+ apply runs."""+ return self._ended()++ def _ended(+ self,+ *,+ error: str | None = None,+ skipped: bool = False,+ skip_reason: str | None = None,+ usage: DreamPassUsage | None = None,+ ) -> DreamPassResult:+ return DreamPassResult(+ user_id=self.user_id,+ pass_id=self.pass_id,+ started_at=self.started_at,+ completed_at=datetime.now(timezone.utc),+ elapsed_seconds=self.elapsed_seconds(),+ execution_path=self.execution_path,+ error=error,+ skipped=skipped,+ skip_reason=skip_reason,+ usage=usage,+ )diff --git a/autogpt_platform/backend/backend/copilot/dream/store.py b/autogpt_platform/backend/backend/copilot/dream/store.pynew file mode 100644index 0000000000..821e99b76b--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/dream/store.py@@ -0,0 +1,181 @@+"""Writes the durable record of every dream pass as the pass moves.++Both routes write here, through ``db_accessors.dream_db()``: the Prisma module+where it is connected, the DatabaseManager RPC in the scheduler and the batch+executor. The sync orchestrator records the start, the gathered window, each+phase's output, the operations it is about to apply and how the pass ended.+The batch path records the submit, each phase that lands, each next batch, the+apply and the end, with what the landed phases used. What each step writes is+``pass_record.py``; how a write lands on the row (forward only, JSON merged in+the database) is ``backend/data/dream_pass_update.py``.++A record write is best-effort and bounded. Each one gets+``RECORD_WRITE_TIMEOUT_SECONDS``, whatever the client does inside it+(connecting, retrying): a write that fails or runs out of time is logged at+warning and dropped, and the pass goes on. So a stalled DatabaseManager costs+a pass at most that deadline per write, and the nightly fan-out and the batch+walker keep moving.++The row is additive to the Redis state: the lock, the batch state and the+admin job status keep their keys, TTLs, scripts and formats. What changed is+timing: the pass now waits on these bounded writes before it locks, inside the+lock, before it chains a batch or applies, and before it cleans up.++``read_dream_pass`` is the read side, for the admin API and the eval driver.+"""++import asyncio+import logging+from collections.abc import Awaitable, Callable+from datetime import datetime, timezone+from typing import TypeVar++from pydantic import BaseModel++from backend.copilot.graphiti.scope import MemoryScope+from backend.data.db_accessors import dream_db+from backend.data.dream_pass_models import DreamPassRecord, DreamPassUpdate++from .fetch import DreamInput+from .pass_record import (+ DreamTrigger,+ applying,+ failed,+ gathered,+ handed_to_batch,+ new_pass,+ next_batch,+ outcome,+ phase_output,+ submitted,+)+from .routing import ExecutionPath+from .schemas import DreamOperations, DreamPassResult, DreamPassUsage, DreamPhase++logger = logging.getLogger(__name__)++_T = TypeVar("_T")++# How long one record write may take, retries included, before the pass+# moves on without it.+RECORD_WRITE_TIMEOUT_SECONDS = 10.0+++async def start_pass(+ pass_id: str,+ scope: MemoryScope,+ *,+ route: ExecutionPath,+ trigger: DreamTrigger,+ started_at: datetime,+) -> None:+ """Insert the pass's row: running, gathering its input."""+ try:+ draft = new_pass(+ pass_id, scope, route=route, trigger=trigger, started_at=started_at+ )+ await _bounded(dream_db().create_dream_pass(draft))+ except Exception:+ logger.warning(+ f"Dream pass {pass_id}: could not insert its record; the pass goes on",+ exc_info=True,+ )+++async def record_gathered(pass_id: str, input_bundle: DreamInput) -> None:+ await _write(pass_id, "the gathered window", lambda: gathered(input_bundle))+++async def record_phase_output(+ pass_id: str, phase: DreamPhase, output: BaseModel+) -> None:+ await _write(pass_id, f"the {phase} output", lambda: phase_output(phase, output))+++async def record_applying(pass_id: str, ops: DreamOperations) -> None:+ await _write(pass_id, "the start of apply", lambda: applying(ops))+++async def record_submitted(+ pass_id: str,+ *,+ input_bundle: DreamInput,+ provider_batch_id: str,+ lease_token: str,+ lease_ttl_seconds: int,+) -> None:+ await _write(+ pass_id,+ "the batch submit",+ lambda: submitted(+ input_bundle=input_bundle,+ provider_batch_id=provider_batch_id,+ lease_token=lease_token,+ lease_ttl_seconds=lease_ttl_seconds,+ ),+ )+++async def record_next_batch(+ pass_id: str, phase: DreamPhase, provider_batch_id: str+) -> None:+ await _write(+ pass_id, "the next batch", lambda: next_batch(phase, provider_batch_id)+ )+++async def record_sync_outcome(result: DreamPassResult) -> None:+ """How a pass the orchestrator ran ended. A pass it handed to the batch+ route stays open: the batch callbacks write its end."""+ if handed_to_batch(result):+ return+ await _write(result.pass_id, "the outcome", lambda: outcome(result, result.usage))+++async def record_batch_complete(+ result: DreamPassResult, usage: DreamPassUsage | None+) -> None:+ """A batch pass applied: *result* holds what apply reported, *usage* what+ its phases used."""+ await _write(result.pass_id, "the outcome", lambda: outcome(result, usage))+++async def record_batch_failed(+ pass_id: str, error: str, usage: DreamPassUsage | None+) -> None:+ """A batch pass failed; *usage* is what its landed phases used."""+ await _write(+ pass_id,+ "the failure",+ lambda: failed(error, usage, datetime.now(timezone.utc)),+ )+++async def read_dream_pass(pass_id: str, *, user_id: str) -> DreamPassRecord | None:+ """The pass's row when *user_id* owns it, else ``None``. Unlike the+ writes, a failed read raises: its caller is answering a request."""+ return await dream_db().get_dream_pass_for_user(pass_id, user_id)+++async def _write(pass_id: str, step: str, build: Callable[[], DreamPassUpdate]) -> None:+ """Write one transition, bounded, building it inside the guard so no part+ of it can fail the pass."""+ try:+ written = await _bounded(dream_db().update_dream_pass(pass_id, build()))+ except Exception:+ logger.warning(+ f"Dream pass {pass_id}: could not record {step}; the pass goes on",+ exc_info=True,+ )+ return+ if not written:+ # Expected, not a fault: the row is closed (a late or repeated+ # delivery, which the transition rules refuse by design) or was+ # never inserted (that failure was logged when it happened).+ logger.debug("Dream pass %s: no open record to write %s to", pass_id, step)+++async def _bounded(call: Awaitable[_T]) -> _T:+ """*call*, or ``TimeoutError`` once it has taken the write deadline; the+ abandoned call is cancelled, not left running."""+ return await asyncio.wait_for(call, timeout=RECORD_WRITE_TIMEOUT_SECONDS)diff --git a/autogpt_platform/backend/backend/copilot/dream/store_deadline_test.py b/autogpt_platform/backend/backend/copilot/dream/store_deadline_test.pynew file mode 100644index 0000000000..e8bfb222bc--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/dream/store_deadline_test.py@@ -0,0 +1,138 @@+"""A record write is bounded: a DatabaseManager that stalls or refuses costs a+pass the write deadline, retries included, and the abandoned request is+cancelled rather than left running.++Each test drives the real DatabaseManager RPC client; only the HTTP transport+under it is replaced."""++import asyncio+import logging+from collections.abc import AsyncIterator, Callable+from datetime import datetime, timezone++import httpx+import pytest++from backend.copilot.graphiti.scope import MemoryScope+from backend.data.db_manager import DatabaseManagerAsyncClient+from backend.util.service import get_service_client++from . import store+from .schemas import DreamOperations++_DEADLINE = 0.2+# Generous against a slow CI box, and far below one retry backoff (~1 s) or+# the client's own 300 s request timeout.+_BOUND = 2.0+++class _Hanging(httpx.AsyncBaseTransport):+ """Accepts every request and never answers."""++ def __init__(self) -> None:+ self.requests = 0+ self.cancelled = 0++ async def handle_async_request(self, request: httpx.Request) -> httpx.Response:+ self.requests += 1+ try:+ await asyncio.Event().wait()+ except asyncio.CancelledError:+ self.cancelled += 1+ raise+ raise AssertionError("a hanging transport never answers")+++class _Refusing(httpx.AsyncBaseTransport):+ """Refuses every connection, as a DatabaseManager that is down would."""++ def __init__(self) -> None:+ self.requests = 0++ async def handle_async_request(self, request: httpx.Request) -> httpx.Response:+ self.requests += 1+ raise httpx.ConnectError("connection refused", request=request)+++def deadline(monkeypatch: pytest.MonkeyPatch) -> float:+ monkeypatch.setattr(store, "RECORD_WRITE_TIMEOUT_SECONDS", _DEADLINE)+ return _DEADLINE+++async def rpc_over(+ monkeypatch: pytest.MonkeyPatch,+) -> AsyncIterator[Callable[[httpx.AsyncBaseTransport, bool], None]]:+ """Point the store at a real DatabaseManager client whose requests go to+ a given transport, retrying or not."""+ clients: list[DatabaseManagerAsyncClient] = []++ def use(transport: httpx.AsyncBaseTransport, retry: bool) -> None:+ client = get_service_client(DatabaseManagerAsyncClient, request_retry=retry)++ def over_transport() -> httpx.AsyncClient:+ return httpx.AsyncClient(transport=transport, base_url=client.base_url)++ # A client the RPC layer rebuilds after connection failures must+ # still go to the fake transport, never to a real address.+ monkeypatch.setattr(client, "_create_async_client", over_transport)+ clients.append(client)+ monkeypatch.setattr(store, "dream_db", lambda: client)++ yield use+ for client in clients:+ await client.aclose()++[email protected]("retry", [True, False], ids=["retrying", "no-retry"])+async def test_a_stalled_update_costs_the_deadline_and_is_cancelled(+ deadline, rpc_over, caplog, retry+):+ hanging = _Hanging()+ rpc_over(hanging, retry)+ loop = asyncio.get_running_loop()++ started = loop.time()+ with caplog.at_level(logging.WARNING, logger=store.logger.name):+ await store.record_next_batch("p1", "recombine", "batch-1")++ assert loop.time() - started < _BOUND+ assert (hanging.requests, hanging.cancelled) == (1, 1)+ assert "could not record the next batch" in caplog.text+++async def test_a_stalled_insert_costs_the_deadline(deadline, rpc_over, caplog):+ hanging = _Hanging()+ rpc_over(hanging, True)+ loop = asyncio.get_running_loop()++ started = loop.time()+ with caplog.at_level(logging.WARNING, logger=store.logger.name):+ await store.start_pass(+ "p1",+ MemoryScope.for_user("u1"),+ route="sync_baseline",+ trigger="cron",+ started_at=datetime.now(timezone.utc),+ )++ assert loop.time() - started < _BOUND+ assert hanging.cancelled == 1+ assert "could not insert its record" in caplog.text+++async def test_the_deadline_covers_the_clients_retries(deadline, rpc_over, caplog):+ """A retrying client backs off for seconds between attempts, up to a+ hundred of them; the write deadline cuts all of that short."""+ refusing = _Refusing()+ rpc_over(refusing, True)+ loop = asyncio.get_running_loop()++ started = loop.time()+ with caplog.at_level(logging.WARNING, logger=store.logger.name):+ await store.record_applying("p1", DreamOperations())++ assert loop.time() - started < _BOUND+ assert refusing.requests >= 1+ assert "could not record the start of apply" in caplog.textdiff --git a/autogpt_platform/backend/backend/copilot/dream/store_test.py b/autogpt_platform/backend/backend/copilot/dream/store_test.pynew file mode 100644index 0000000000..8704d1de01--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/dream/store_test.py@@ -0,0 +1,552 @@+"""The dream store: each transition the two routes write, the read side, and+that a failed write never reaches the pass."""++import logging+from datetime import datetime, timedelta, timezone+from unittest.mock import AsyncMock, MagicMock++import pytest+from prisma.enums import (+ DreamPassPhase,+ DreamPassRoute,+ DreamPassStatus,+ DreamPassTrigger,+)++from backend.copilot.graphiti.scope import MemoryScope+from backend.data.dream_pass_models import (+ DreamPassApplied,+ DreamPassDraft,+ DreamPassOperations,+ DreamPassRecord,+ DreamPassUpdate,+ DreamPhaseOutputs,+)++from . import store+from .fetch import DreamInput+from .pass_record import dream_pass_result_from_row+from .schemas import (+ ConsolidationOutput,+ DreamOperations,+ DreamOperationsSnapshot,+ DreamPassResult,+ DreamPassUsage,+ IngestionDrainStatus,+ PhaseUsage,+ RecombinationOutput,+ WriteSummary,+)++_STARTED = datetime(2026, 9, 26, 3, 0, tzinfo=timezone.utc)+_FINISHED = _STARTED + timedelta(minutes=7)+++def db(mocker) -> MagicMock:+ """The accessor's target, with every write succeeding."""+ database = MagicMock()+ database.create_dream_pass = AsyncMock()+ database.update_dream_pass = AsyncMock(return_value=True)+ database.get_dream_pass_for_user = AsyncMock()+ mocker.patch.object(store, "dream_db", return_value=database)+ return database+++def _update(db: MagicMock) -> tuple[str, DreamPassUpdate]:+ db.update_dream_pass.assert_awaited_once()+ pass_id, update = db.update_dream_pass.await_args.args+ return pass_id, update+++def _usage(cost: float | None = 0.03) -> DreamPassUsage:+ return DreamPassUsage(+ phases=[+ PhaseUsage(+ phase="consolidate",+ model="claude-sonnet-5",+ input_tokens=100,+ output_tokens=40,+ cost_usd=cost,+ )+ ],+ total_input_tokens=100,+ total_output_tokens=40,+ total_cost_usd=cost,+ discount_applied=0.5,+ )+++def _bundle() -> DreamInput:+ return DreamInput(+ user_id="u1",+ group_id="user_u1",+ window_start=_STARTED - timedelta(days=14),+ window_end=_STARTED,+ )+++def _complete_result(**overrides) -> DreamPassResult:+ fields = {+ "user_id": "u1",+ "pass_id": "p1",+ "started_at": _STARTED,+ "completed_at": _FINISHED,+ "elapsed_seconds": 420.0,+ "consolidated_count": 3,+ "proposal_count": 1,+ "demotion_count": 2,+ "entity_invalidation_count": 0,+ "summary_for_user": "Tidied what you told me this week.",+ "dream_session_id": "session-1",+ "ingestion_drain_status": IngestionDrainStatus.drained,+ "operations": DreamOperationsSnapshot(+ writes=[WriteSummary(content="Nick ships on Fridays")]+ ),+ "usage": _usage(),+ }+ return DreamPassResult(**{**fields, **overrides})+++class TestWrites:+ @pytest.mark.parametrize(+ "route, trigger, expected_route, expected_trigger",+ [+ ("sync_baseline", "cron", DreamPassRoute.SYNC, DreamPassTrigger.CRON),+ (+ "anthropic_batch",+ "admin",+ DreamPassRoute.ANTHROPIC_BATCH,+ DreamPassTrigger.ADMIN,+ ),+ ("sync_baseline", "eval", DreamPassRoute.SYNC, DreamPassTrigger.EVAL),+ ],+ )+ async def test_start_inserts_a_running_row_gathering(+ self, db, route, trigger, expected_route, expected_trigger+ ):+ scope = MemoryScope.for_expert("u1", "expert-1")++ await store.start_pass(+ "p1", scope, route=route, trigger=trigger, started_at=_STARTED+ )++ db.create_dream_pass.assert_awaited_once_with(+ DreamPassDraft(+ id="p1",+ user_id="u1",+ expert_id="expert-1",+ scope_key=scope.scope_key,+ route=expected_route,+ trigger=expected_trigger,+ status=DreamPassStatus.RUNNING,+ phase=DreamPassPhase.GATHER,+ started_at=_STARTED,+ )+ )++ async def test_gathered_records_the_window_and_moves_to_consolidate(self, db):+ bundle = _bundle()++ await store.record_gathered("p1", bundle)++ assert _update(db) == (+ "p1",+ DreamPassUpdate(+ phase=DreamPassPhase.CONSOLIDATE,+ window_start=bundle.window_start,+ window_end=bundle.window_end,+ ),+ )++ @pytest.mark.parametrize(+ "phase, output, next_step",+ [+ ("consolidate", ConsolidationOutput(), DreamPassPhase.RECOMBINE),+ ("recombine", RecombinationOutput(), DreamPassPhase.SANITIZE),+ ("sanitize", DreamOperations(summary_for_user="ok"), DreamPassPhase.APPLY),+ ],+ )+ async def test_phase_output_is_kept_and_the_pass_moves_on(+ self, db, phase, output, next_step+ ):+ await store.record_phase_output("p1", phase, output)++ _, update = _update(db)+ assert update.phase is next_step+ assert update.status is None+ assert update.phase_outputs == DreamPhaseOutputs.model_validate({phase: output})++ async def test_applying_keeps_the_clamped_operations(self, db):+ ops = DreamOperations(summary_for_user="clamped")++ await store.record_applying("p1", ops)++ assert _update(db) == (+ "p1",+ DreamPassUpdate(+ status=DreamPassStatus.APPLYING,+ phase=DreamPassPhase.APPLY,+ operations=DreamPassOperations(planned=ops),+ ),+ )++ async def test_submitted_records_the_batch_the_bundle_and_the_lease(self, db):+ bundle = _bundle()+ before = datetime.now(timezone.utc)++ await store.record_submitted(+ "p1",+ input_bundle=bundle,+ provider_batch_id="msgbatch_1",+ lease_token="tok",+ lease_ttl_seconds=3600,+ )++ _, update = _update(db)+ assert update.status is DreamPassStatus.SUBMITTED+ assert update.phase is DreamPassPhase.CONSOLIDATE+ assert update.provider_batch_id == "msgbatch_1"+ assert update.input_bundle == bundle+ assert update.lease_token == "tok"+ assert update.submitted_at is not None and update.submitted_at >= before+ assert update.lease_expires_at == update.submitted_at + timedelta(hours=1)++ async def test_next_batch_names_the_phase_it_runs(self, db):+ await store.record_next_batch("p1", "recombine", "msgbatch_2")++ assert _update(db) == (+ "p1",+ DreamPassUpdate(+ phase=DreamPassPhase.RECOMBINE, provider_batch_id="msgbatch_2"+ ),+ )++ async def test_a_completed_sync_pass_records_what_apply_reported(self, db):+ result = _complete_result()++ await store.record_sync_outcome(result)++ _, update = _update(db)+ assert update.status is DreamPassStatus.COMPLETE+ assert update.phase is DreamPassPhase.DONE+ assert update.usage == result.usage+ assert update.applied_at == update.completed_at == _FINISHED+ assert update.operations == DreamPassOperations(+ applied=DreamPassApplied(+ consolidated_count=3,+ proposal_count=1,+ demotion_count=2,+ summary_for_user="Tidied what you told me this week.",+ dream_session_id="session-1",+ ingestion_drain_status=IngestionDrainStatus.drained,+ snapshot=result.operations,+ )+ )++ async def test_a_skipped_pass_records_its_reason(self, db):+ result = DreamPassResult(+ user_id="u1",+ pass_id="p1",+ completed_at=_FINISHED,+ skipped=True,+ skip_reason="no_new_activity",+ )++ await store.record_sync_outcome(result)++ assert _update(db) == (+ "p1",+ DreamPassUpdate(+ status=DreamPassStatus.SKIPPED,+ skip_reason="no_new_activity",+ completed_at=_FINISHED,+ ),+ )++ async def test_a_failed_pass_keeps_its_partial_usage_and_a_capped_error(self, db):+ result = DreamPassResult(+ user_id="u1",+ pass_id="p1",+ completed_at=_FINISHED,+ error="recombine: " + "x" * 5000,+ usage=_usage(),+ )++ await store.record_sync_outcome(result)++ _, update = _update(db)+ assert update.status is DreamPassStatus.ERRORED+ assert update.error == result.error[:2000]+ assert update.usage == result.usage+ assert update.completed_at == _FINISHED+ assert update.phase is None++ async def test_a_pass_handed_to_the_batch_route_stays_open(self, db):+ await store.record_sync_outcome(+ DreamPassResult(+ user_id="u1", pass_id="p1", execution_path="anthropic_batch"+ )+ )++ db.update_dream_pass.assert_not_awaited()++ async def test_a_batch_route_pass_that_failed_before_submit_is_closed(self, db):+ await store.record_sync_outcome(+ DreamPassResult(+ user_id="u1",+ pass_id="p1",+ execution_path="anthropic_batch",+ error="anthropic_batch: phase 1 submit failed: boom",+ )+ )++ _, update = _update(db)+ assert update.status is DreamPassStatus.ERRORED++ async def test_a_completed_batch_pass_records_the_usage_it_is_given(self, db):+ usage = _usage(cost=0.011)+ result = _complete_result(execution_path="anthropic_batch", usage=None)++ await store.record_batch_complete(result, usage)++ _, update = _update(db)+ assert update.status is DreamPassStatus.COMPLETE+ assert update.usage == usage++ async def test_a_failed_batch_pass_records_the_error_and_usage(self, db):+ usage = _usage()++ await store.record_batch_failed("p1", "recombine: provider down", usage)++ _, update = _update(db)+ assert update.status is DreamPassStatus.ERRORED+ assert update.error == "recombine: provider down"+ assert update.usage == usage+ assert update.completed_at is not None+++class TestAFailedWriteNeverFailsThePass:+ @pytest.mark.parametrize(+ "write",+ [+ lambda: store.record_gathered("p1", _bundle()),+ lambda: store.record_phase_output(+ "p1", "consolidate", ConsolidationOutput()+ ),+ lambda: store.record_applying("p1", DreamOperations()),+ lambda: store.record_submitted(+ "p1",+ input_bundle=_bundle(),+ provider_batch_id="b",+ lease_token="t",+ lease_ttl_seconds=60,+ ),+ lambda: store.record_next_batch("p1", "sanitize", "b"),+ lambda: store.record_sync_outcome(_complete_result()),+ lambda: store.record_batch_complete(_complete_result(), None),+ lambda: store.record_batch_failed("p1", "boom", None),+ ],+ )+ async def test_an_update_that_raises_is_logged_at_warning(self, db, caplog, write):+ db.update_dream_pass.side_effect = ConnectionError("db down")++ with caplog.at_level(logging.WARNING, logger=store.logger.name):+ await write()++ assert any(+ r.levelno == logging.WARNING and "p1" in r.getMessage()+ for r in caplog.records+ )++ async def test_an_insert_that_raises_is_logged_at_warning(self, db, caplog):+ db.create_dream_pass.side_effect = ConnectionError("db down")++ with caplog.at_level(logging.WARNING, logger=store.logger.name):+ await store.start_pass(+ "p1",+ MemoryScope.for_user("u1"),+ route="sync_baseline",+ trigger="cron",+ started_at=_STARTED,+ )++ assert "could not insert" in caplog.text++ async def test_an_unreachable_accessor_is_logged_at_warning(self, mocker, caplog):+ mocker.patch.object(store, "dream_db", side_effect=RuntimeError("no rpc"))++ with caplog.at_level(logging.WARNING, logger=store.logger.name):+ await store.record_next_batch("p1", "sanitize", "b")++ assert "could not record the next batch" in caplog.text++ async def test_an_output_that_is_not_the_phase_is_dropped_not_raised(+ self, db, caplog+ ):+ with caplog.at_level(logging.WARNING, logger=store.logger.name):+ await store.record_phase_output("p1", "consolidate", RecombinationOutput())++ db.update_dream_pass.assert_not_awaited()+ assert "could not record the consolidate output" in caplog.text++ async def test_a_row_that_is_missing_or_closed_is_logged_at_debug(self, db, caplog):+ """A closed row refusing a late or repeated write is expected, so a+ normal duplicate delivery raises no warning."""+ db.update_dream_pass.return_value = False++ with caplog.at_level(logging.DEBUG, logger=store.logger.name):+ await store.record_batch_failed("p1", "boom", None)++ [record] = [r for r in caplog.records if "no open record" in r.getMessage()]+ assert record.levelno == logging.DEBUG+++class TestReadSide:+ async def test_read_is_owner_scoped(self, db):+ row = _row()+ db.get_dream_pass_for_user.return_value = row++ assert await store.read_dream_pass("p1", user_id="u1") is row+ db.get_dream_pass_for_user.assert_awaited_once_with("p1", "u1")++ async def test_a_failed_read_raises(self, db):+ db.get_dream_pass_for_user.side_effect = ConnectionError("db down")++ with pytest.raises(ConnectionError):+ await store.read_dream_pass("p1", user_id="u1")++ def test_a_completed_batch_row_reads_back_with_usage_and_timings(self):+ usage = _usage(cost=0.011)+ snapshot = DreamOperationsSnapshot(writes=[WriteSummary(content="fact")])+ row = _row(+ route=DreamPassRoute.ANTHROPIC_BATCH,+ status=DreamPassStatus.COMPLETE,+ phase=DreamPassPhase.DONE,+ completed_at=_FINISHED,+ usage=usage,+ operations=DreamPassOperations(+ applied=DreamPassApplied(+ consolidated_count=1,+ proposal_count=2,+ summary_for_user="ok",+ dream_session_id="s1",+ ingestion_drain_status=IngestionDrainStatus.skipped,+ snapshot=snapshot,+ )+ ),+ )++ result = dream_pass_result_from_row(row)++ assert result == DreamPassResult(+ user_id="u1",+ pass_id="p1",+ started_at=_STARTED,+ completed_at=_FINISHED,+ elapsed_seconds=420.0,+ execution_path="anthropic_batch",+ consolidated_count=1,+ proposal_count=2,+ summary_for_user="ok",+ dream_session_id="s1",+ ingestion_drain_status=IngestionDrainStatus.skipped,+ operations=snapshot,+ usage=usage,+ )++ def test_a_skipped_row_reads_as_skipped(self):+ row = _row(+ status=DreamPassStatus.SKIPPED,+ skip_reason="lock_held",+ completed_at=_FINISHED,+ )++ result = dream_pass_result_from_row(row)++ assert result.skipped is True+ assert result.skip_reason == "lock_held"+ assert result.operations is None+ assert result.usage is None++ def test_an_errored_row_reads_with_its_error_and_usage(self):+ usage = _usage()+ row = _row(+ status=DreamPassStatus.ERRORED,+ error="sanitize: bad json",+ usage=usage,+ completed_at=_FINISHED,+ )++ result = dream_pass_result_from_row(row)++ assert (result.error, result.usage, result.skipped) == (+ "sanitize: bad json",+ usage,+ False,+ )++ def test_a_pass_in_flight_has_no_end_yet(self):+ row = _row(+ route=DreamPassRoute.ANTHROPIC_BATCH,+ status=DreamPassStatus.SUBMITTED,+ phase=DreamPassPhase.RECOMBINE,+ )++ result = dream_pass_result_from_row(row)++ assert result.completed_at is None+ assert result.elapsed_seconds is None+ assert result.usage is None+ assert result.error is None and result.skipped is False++ async def test_a_sync_outcome_reads_back_as_the_result_it_recorded(self, db):+ """What the orchestrator returned is what the eval driver reads."""+ result = _complete_result(+ dropped_forgotten=2, pending_writes=3, failed_writes=4+ )+ await store.record_sync_outcome(result)+ _, update = _update(db)+ assert update.operations is not None++ row = _row(+ status=update.status,+ phase=update.phase,+ operations=update.operations,+ usage=update.usage,+ completed_at=update.completed_at,+ )++ assert dream_pass_result_from_row(row) == result+++def _row(**overrides) -> DreamPassRecord:+ fields = {+ "id": "p1",+ "user_id": "u1",+ "expert_id": None,+ "scope_key": "u1",+ "route": DreamPassRoute.SYNC,+ "trigger": DreamPassTrigger.CRON,+ "phase": DreamPassPhase.GATHER,+ "status": DreamPassStatus.RUNNING,+ "skip_reason": None,+ "cancel_generation": 0,+ "provider_batch_id": None,+ "lease_token": None,+ "lease_expires_at": None,+ "input_bundle": None,+ "phase_outputs": DreamPhaseOutputs(),+ "operations": DreamPassOperations(),+ "usage": None,+ "window_start": None,+ "window_end": None,+ "error": None,+ "created_at": _STARTED,+ "started_at": _STARTED,+ "submitted_at": None,+ "applied_at": None,+ "completed_at": None,+ "updated_at": _STARTED,+ }+ return DreamPassRecord(**{**fields, **overrides})diff --git a/autogpt_platform/backend/backend/copilot/dream/usage.py b/autogpt_platform/backend/backend/copilot/dream/usage.pynew file mode 100644index 0000000000..71b8d00891--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/dream/usage.py@@ -0,0 +1,96 @@+"""What a dream pass used, per phase and in total, on either route.++The sync orchestrator gets each phase's usage back priced from the call it+made. A batch pass's phases are priced here from the rows its callbacks kept+in Redis, the way ``inference.record.record`` prices their cost rows: on the+Anthropic batch route, at the batch discount, and a phase that reported no+tokens keeps an unknown cost rather than a known zero.+"""++from typing import Any++from backend.copilot.inference.context import InferenceUsage+from backend.copilot.inference.record import price+from backend.copilot.inference.routing import anthropic_batch_route++from .phase_jobs import PHASE_TIERS+from .routing import ExecutionPath, batch_discount+from .schemas import DreamPassUsage, DreamPhase, PhaseUsage+++def batch_pass_usage(+ state: dict[str, dict[str, Any]], phase_models: dict[str, str]+) -> DreamPassUsage | None:+ """The completed batch phases, priced on their models; ``None`` when none did."""+ phases = [+ phase_usage(phase, _priced(usage))+ for phase in PHASE_TIERS+ if (usage := landed_phase_usage(state.get(phase), phase_models.get(phase)))+ is not None+ ]+ return aggregate_usage(phases, "anthropic_batch") if phases else None+++def landed_phase_usage(+ row: dict[str, Any] | None, model: str | None+) -> InferenceUsage | None:+ """The tokens a batch phase's state row records, on *model*. ``None`` for a+ phase that has not landed, failed, or has no model recorded.+ """+ if row is None or not model or row.get("error"):+ return None+ return InferenceUsage(+ model=model,+ input_tokens=int(row.get("input_tokens") or 0),+ output_tokens=int(row.get("output_tokens") or 0),+ cache_read_tokens=int(row.get("cache_read_tokens") or 0),+ cache_creation_tokens=int(row.get("cache_creation_tokens") or 0),+ payer=anthropic_batch_route(model).payer,+ )+++def phase_usage(phase: DreamPhase, usage: InferenceUsage) -> PhaseUsage:+ return PhaseUsage(+ phase=phase,+ model=usage.model,+ input_tokens=usage.input_tokens,+ output_tokens=usage.output_tokens,+ cache_read_tokens=usage.cache_read_tokens,+ cache_creation_tokens=usage.cache_creation_tokens,+ cost_usd=usage.cost_usd,+ )+++def aggregate_usage(+ phases: list[PhaseUsage], execution_path: ExecutionPath+) -> DreamPassUsage:+ """Roll up per-phase usage into a ``DreamPassUsage``.++ ``total_cost_usd`` is None when any single phase had unknown cost+ so we never silently bill at a partial figure.+ """+ total_cost: float | None = 0.0+ for p in phases:+ if p.cost_usd is None:+ total_cost = None+ break+ total_cost += p.cost_usd+ return DreamPassUsage(+ phases=phases,+ total_input_tokens=sum(p.input_tokens for p in phases),+ total_output_tokens=sum(p.output_tokens for p in phases),+ total_cache_read_tokens=sum(p.cache_read_tokens for p in phases),+ total_cache_creation_tokens=sum(p.cache_creation_tokens for p in phases),+ total_cost_usd=total_cost,+ discount_applied=batch_discount(execution_path),+ )+++def _priced(usage: InferenceUsage) -> InferenceUsage:+ reported = (+ usage.input_tokens+ or usage.output_tokens+ or usage.cache_read_tokens+ or usage.cache_creation_tokens+ )+ return price(usage, anthropic_batch_route(usage.model)) if reported else usagediff --git a/autogpt_platform/backend/backend/copilot/dream/usage_test.py b/autogpt_platform/backend/backend/copilot/dream/usage_test.pynew file mode 100644index 0000000000..496b789892--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/dream/usage_test.py@@ -0,0 +1,71 @@+"""A batch pass's usage, read off the phase rows its callbacks kept."""++import pytest++from .usage import batch_pass_usage++_MODELS = {+ "consolidate": "claude-sonnet-5",+ "recombine": "claude-opus-5-5",+ "sanitize": "claude-sonnet-5",+}+# Catalog list prices per million tokens, halved on the batch route.+_SONNET_5 = (10 * 2.0 + 20 * 10.0) / 1_000_000 / 2+_OPUS_5_5 = (10 * 4.0 + 20 * 20.0) / 1_000_000 / 2+++def _landed(**overrides) -> dict:+ return {"input_tokens": 10, "output_tokens": 20, "error": None, **overrides}+++def test_each_landed_phase_is_priced_on_its_own_model_at_the_batch_discount():+ usage = batch_pass_usage(+ {"consolidate": _landed(), "recombine": _landed(), "sanitize": _landed()},+ _MODELS,+ )++ assert usage is not None+ assert [(p.phase, p.model) for p in usage.phases] == list(_MODELS.items())+ assert [p.cost_usd for p in usage.phases] == pytest.approx(+ [_SONNET_5, _OPUS_5_5, _SONNET_5]+ )+ assert usage.total_cost_usd == pytest.approx(2 * _SONNET_5 + _OPUS_5_5)+ assert (usage.total_input_tokens, usage.total_output_tokens) == (30, 60)+ assert usage.discount_applied == 0.5+++ "error", ["provider down", "max_tokens", "invalid output shape"]+)+def test_errored_phases_and_phases_without_a_model_are_left_out(error):+ usage = batch_pass_usage(+ {+ "consolidate": _landed(),+ "recombine": _landed(error=error),+ "sanitize": _landed(),+ },+ {"consolidate": "claude-sonnet-5", "recombine": "claude-opus-5-5"},+ )++ assert usage is not None+ assert [p.phase for p in usage.phases] == ["consolidate"]+++def test_a_phase_that_reported_no_tokens_keeps_an_unknown_cost():+ """As ``inference.record.record`` leaves it: unknown, not a known $0."""+ usage = batch_pass_usage(+ {+ "consolidate": _landed(),+ "recombine": _landed(input_tokens=0, output_tokens=0),+ },+ _MODELS,+ )++ assert usage is not None+ assert [p.cost_usd for p in usage.phases] == [pytest.approx(_SONNET_5), None]+ assert usage.total_cost_usd is None+++def test_nothing_landed_is_no_usage():+ assert batch_pass_usage({}, _MODELS) is None+ assert batch_pass_usage({"consolidate": _landed(error="boom")}, _MODELS) is Nonediff --git a/autogpt_platform/backend/backend/data/db_accessors.py b/autogpt_platform/backend/backend/data/db_accessors.pyindex 1937426364..8db4734bf2 100644--- a/autogpt_platform/backend/backend/data/db_accessors.py+++ b/autogpt_platform/backend/backend/data/db_accessors.py@@ -400,6 +400,22 @@ def bot_installs_db():return bot_installs_db+def dream_db():+ if db.is_connected():+ from backend.data import dream_pass as _dream_db++ dream_db = _dream_db+ else:+ from backend.util.clients import get_database_manager_async_client++ # The dream record is written under a short deadline that already+ # covers retrying (copilot/dream/store.py); a retrying client would+ # only keep going on a request the deadline has given up on.+ dream_db = get_database_manager_async_client(should_retry=False)++ return dream_db++def pro_activation_db():if db.is_connected():from backend.data import pro_activationdiff --git a/autogpt_platform/backend/backend/data/db_accessors_test.py b/autogpt_platform/backend/backend/data/db_accessors_test.pyindex 906b7d9bb8..17c3002c0a 100644--- a/autogpt_platform/backend/backend/data/db_accessors_test.py+++ b/autogpt_platform/backend/backend/data/db_accessors_test.py@@ -5,6 +5,7 @@ from unittest.mock import MagicMock, patchfrom backend.api.features.orgs import db as orgs_modulefrom backend.data import bot_installs as bot_installs_modulefrom backend.data import db_accessors+from backend.data import dream_pass as dream_pass_moduledef test_orgs_db_uses_direct_module_when_connected():@@ -43,3 +44,25 @@ def test_bot_installs_db_falls_back_to_database_manager_client():),):assert db_accessors.bot_installs_db() is client+++def test_dream_db_uses_direct_module_when_connected():+ with patch("backend.data.db_accessors.db.is_connected", return_value=True):+ assert db_accessors.dream_db() is dream_pass_module+++def test_dream_db_falls_back_to_a_non_retrying_database_manager_client():+ # The scheduler (sync passes) and the batch executor (batch callbacks)+ # keep no Prisma connection, so the dream store's writes cross the RPC.+ # The store bounds each write itself; client retries would only keep+ # going on a request the store has already given up on.+ client = MagicMock()+ with (+ patch("backend.data.db_accessors.db.is_connected", return_value=False),+ patch(+ "backend.util.clients.get_database_manager_async_client",+ return_value=client,+ ) as get_client,+ ):+ assert db_accessors.dream_db() is client+ get_client.assert_called_once_with(should_retry=False)diff --git a/autogpt_platform/backend/backend/data/db_manager.py b/autogpt_platform/backend/backend/data/db_manager.pyindex b33ff6620c..e17fe095f5 100644--- a/autogpt_platform/backend/backend/data/db_manager.py+++ b/autogpt_platform/backend/backend/data/db_manager.py@@ -66,6 +66,7 @@ from backend.copilot.sharing.db import link_new_execution_to_chat_sharefrom backend.data import bot_analytics as bot_analytics_dbfrom backend.data import bot_installs as bot_installs_dbfrom backend.data import db+from backend.data import dream_pass as dream_pass_dbfrom backend.data import memory_schedule as memory_schedule_dbfrom backend.data import skill_deletion as skill_deletion_datafrom backend.data import skill_learning as skill_learning_data@@ -779,6 +780,17 @@ class DatabaseManager(AppService):get_chat_session_status = _(chat_db.get_chat_session_status)get_latest_user_message_in_session = _(chat_db.get_latest_user_message_in_session)+ # ============ Dream Passes ============ #+ # The scheduler (sync passes) and the batch executor (batch callbacks)+ # keep no Prisma connection; the dream store writes each pass's record+ # through these via db_accessors.dream_db().+ create_dream_pass = _(dream_pass_db.create_dream_pass)+ update_dream_pass = _(dream_pass_db.update_dream_pass)+ get_dream_pass = _(dream_pass_db.get_dream_pass)+ get_dream_pass_for_user = _(dream_pass_db.get_dream_pass_for_user)+ list_open_dream_passes = _(dream_pass_db.list_open_dream_passes)+ list_dream_passes = _(dream_pass_db.list_dream_passes)+# ============ Morning Briefing ============ ## Exposed so the Prisma-less scheduler process can compose, store and# post a briefing via db_accessors / the DatabaseManager RPC.@@ -1285,3 +1297,11 @@ class DatabaseManagerAsyncClient(AppServiceClient):get_chat_session_status = d.get_chat_session_statusget_latest_user_message_in_session = d.get_latest_user_message_in_sessionadd_chat_message = d.add_chat_message++ # ============ Dream Passes ============ #+ create_dream_pass = d.create_dream_pass+ update_dream_pass = d.update_dream_pass+ get_dream_pass = d.get_dream_pass+ get_dream_pass_for_user = d.get_dream_pass_for_user+ list_open_dream_passes = d.list_open_dream_passes+ list_dream_passes = d.list_dream_passesdiff --git a/autogpt_platform/backend/backend/data/db_manager_test.py b/autogpt_platform/backend/backend/data/db_manager_test.pyindex a4c065b697..354cd63b63 100644--- a/autogpt_platform/backend/backend/data/db_manager_test.py+++ b/autogpt_platform/backend/backend/data/db_manager_test.py@@ -1,4 +1,32 @@+import inspect+from datetime import datetime, timezone++from prisma.enums import (+ DreamPassPhase,+ DreamPassRoute,+ DreamPassStatus,+ DreamPassTrigger,+)+from pydantic import TypeAdapter++from backend.copilot.dream.fetch import DreamInput+from backend.copilot.dream.schemas import (+ ConsolidatedFact,+ ConsolidationOutput,+ DreamOperations,+ DreamPassUsage,+ PhaseUsage,+)+from backend.util.json import to_dict+from .db_manager import DatabaseManager, DatabaseManagerAsyncClient+from .dream_pass_models import (+ DreamPassApplied,+ DreamPassOperations,+ DreamPassRecord,+ DreamPassUpdate,+ DreamPhaseOutputs,+)def test_async_client_exposes_chat_methods() -> None:@@ -24,29 +52,92 @@ def test_add_store_agent_rpc_request_schema_is_constructible() -> None:manager._create_fastapi_endpoint(manager.add_store_agent_to_library)-def test_memory_schedule_methods_registered() -> None:- """The scheduler and copilot-executor have no Prisma client; the- schedule registry reaches its rows through these."""- for method in (- "get_scope_schedule",- "claim_scope_schedule",- "record_scope_jobs",- "set_scope_state",- "forget_scope_job",- "record_scope_run",- "list_user_scope_schedules",- "list_scope_schedules",- "existing_user_ids",- ):+_DREAM_PASS_METHODS = (+ "create_dream_pass",+ "update_dream_pass",+ "get_dream_pass",+ "get_dream_pass_for_user",+ "list_open_dream_passes",+ "list_dream_passes",+)+++def test_dream_pass_methods_registered() -> None:+ for method in _DREAM_PASS_METHODS:assert hasattr(DatabaseManager, method)assert hasattr(DatabaseManagerAsyncClient, method)-def test_memory_schedule_rpc_request_schemas_are_constructible() -> None:+def test_dream_pass_models_survive_the_rpc_round_trip() -> None:+ """The dream store in the scheduler and the batch executor reaches the+ table over this RPC: an update must arrive, and a record come back, as+ they left (JSON encoded one way, validated from the signature the other)."""+ now = datetime(2026, 9, 26, 3, 0, tzinfo=timezone.utc)+ bundle = DreamInput(+ user_id="u1",+ group_id="user_u1",+ window_start=now,+ window_end=now,+ known_fact_uuids={"f1", "f2"},+ )+ usage = DreamPassUsage(+ phases=[PhaseUsage(phase="consolidate", model="claude-sonnet-5")],+ discount_applied=0.5,+ )+ outputs = DreamPhaseOutputs(+ consolidate=ConsolidationOutput(+ facts=[ConsolidatedFact(content="Nick ships on Fridays", confidence=0.8)]+ )+ )+ operations = DreamPassOperations(+ planned=DreamOperations(summary_for_user="ok"),+ applied=DreamPassApplied(consolidated_count=1),+ )+ update = DreamPassUpdate(+ status=DreamPassStatus.SUBMITTED,+ phase=DreamPassPhase.CONSOLIDATE,+ input_bundle=bundle,+ phase_outputs=outputs,+ operations=operations,+ usage=usage,+ lease_expires_at=now,+ )manager = DatabaseManager()- manager._create_fastapi_endpoint(manager.claim_scope_schedule)- manager._create_fastapi_endpoint(manager.record_scope_run)- manager._create_fastapi_endpoint(manager.existing_user_ids)+ endpoint = manager._create_fastapi_endpoint(manager.update_dream_pass)+ body = inspect.signature(endpoint).parameters["body"].annotation+ sent = body.model_validate(to_dict({"pass_id": "p1", "update": update}))+ assert sent.update == update++ record = DreamPassRecord(+ id="p1",+ user_id="u1",+ expert_id=None,+ scope_key="u1",+ route=DreamPassRoute.ANTHROPIC_BATCH,+ trigger=DreamPassTrigger.EVAL,+ phase=DreamPassPhase.CONSOLIDATE,+ status=DreamPassStatus.SUBMITTED,+ skip_reason=None,+ cancel_generation=0,+ provider_batch_id="msgbatch_1",+ lease_token="tok",+ lease_expires_at=now,+ input_bundle=bundle,+ phase_outputs=outputs,+ operations=operations,+ usage=usage,+ window_start=now,+ window_end=now,+ error=None,+ created_at=now,+ started_at=now,+ submitted_at=now,+ applied_at=None,+ completed_at=None,+ updated_at=now,+ )+ returned = TypeAdapter(DreamPassRecord | None).validate_python(to_dict(record))+ assert returned == recorddef test_the_audience_consumer_can_read_the_opt_out() -> None:@@ -77,3 +168,28 @@ def test_the_scheduler_can_post_a_note_into_a_chat() -> None:assert hasattr(DatabaseManagerAsyncClient, "append_session_message")manager = DatabaseManager()manager._create_fastapi_endpoint(manager.append_session_message)+++def test_memory_schedule_methods_registered() -> None:+ """The scheduler and copilot-executor have no Prisma client; the+ schedule registry reaches its rows through these."""+ for method in (+ "get_scope_schedule",+ "claim_scope_schedule",+ "record_scope_jobs",+ "set_scope_state",+ "forget_scope_job",+ "record_scope_run",+ "list_user_scope_schedules",+ "list_scope_schedules",+ "existing_user_ids",+ ):+ assert hasattr(DatabaseManager, method)+ assert hasattr(DatabaseManagerAsyncClient, method)+++def test_memory_schedule_rpc_request_schemas_are_constructible() -> None:+ manager = DatabaseManager()+ manager._create_fastapi_endpoint(manager.claim_scope_schedule)+ manager._create_fastapi_endpoint(manager.record_scope_run)+ manager._create_fastapi_endpoint(manager.existing_user_ids)diff --git a/autogpt_platform/backend/backend/data/dream_pass.py b/autogpt_platform/backend/backend/data/dream_pass.pynew file mode 100644index 0000000000..f2fe752192--- /dev/null+++ b/autogpt_platform/backend/backend/data/dream_pass.py@@ -0,0 +1,86 @@+"""The durable record of a dream pass: one ``DreamPass`` row per pass.++``backend/copilot/dream/store.py`` writes the row as a pass moves, on both+routes, through ``db_accessors.dream_db()``: the scheduler and the batch+executor keep no Prisma connection, so their writes cross the DatabaseManager+RPC. The row's shapes are ``dream_pass_models.py``. Every transition goes+through one statement (``dream_pass_update.py``) that only ever moves the row+forward and merges its JSON columns in the database.++A pass that has reached a terminal status (complete, errored, cancelled,+expired, skipped) is final: no update writes it, the way+``job_status.mark_complete`` never rewrites a finished job.+"""++import prisma.models++from backend.data.db import execute_raw_with_schema+from backend.data.dream_pass_models import (+ OPEN_STATUSES,+ DreamPassDraft,+ DreamPassRecord,+ DreamPassUpdate,+)+from backend.data.dream_pass_update import TRANSITION_SQL, transition_args+++async def create_dream_pass(draft: DreamPassDraft) -> DreamPassRecord:+ row = await prisma.models.DreamPass.prisma().create(+ data={+ "id": draft.id,+ "userId": draft.user_id,+ "expertId": draft.expert_id,+ "scopeKey": draft.scope_key,+ "route": draft.route,+ "trigger": draft.trigger,+ "status": draft.status,+ "phase": draft.phase,+ "startedAt": draft.started_at,+ }+ )+ return DreamPassRecord.from_db(row)+++async def update_dream_pass(pass_id: str, update: DreamPassUpdate) -> bool:+ """Apply one transition to the pass's row, in a single statement.++ ``False`` when there is no such row or it has reached a terminal status.+ ``True`` means the row was open, not that every field moved: a status or+ phase behind the row's, or a batch for a phase the row has left, is+ dropped in the statement.+ """+ written = await execute_raw_with_schema(+ TRANSITION_SQL, *transition_args(pass_id, update)+ )+ return written > 0+++async def get_dream_pass(pass_id: str) -> DreamPassRecord | None:+ row = await prisma.models.DreamPass.prisma().find_unique(where={"id": pass_id})+ return DreamPassRecord.from_db(row) if row else None+++async def get_dream_pass_for_user(pass_id: str, user_id: str) -> DreamPassRecord | None:+ """The pass's row when *user_id* owns it. ``None`` for another user's pass,+ the same as for a missing one, so a caller cannot probe for pass ids."""+ row = await prisma.models.DreamPass.prisma().find_first(+ where={"id": pass_id, "userId": user_id}+ )+ return DreamPassRecord.from_db(row) if row else None+++async def list_open_dream_passes(scope_key: str) -> list[DreamPassRecord]:+ """The scope's passes that have not reached a terminal status, oldest first."""+ rows = await prisma.models.DreamPass.prisma().find_many(+ where={"scopeKey": scope_key, "status": {"in": list(OPEN_STATUSES)}},+ order={"createdAt": "asc"},+ )+ return [DreamPassRecord.from_db(row) for row in rows]+++async def list_dream_passes(user_id: str, limit: int = 20) -> list[DreamPassRecord]:+ """The user's passes, across every scope, newest first."""+ rows = await prisma.models.DreamPass.prisma().find_many(+ where={"userId": user_id}, order={"createdAt": "desc"}, take=limit+ )+ return [DreamPassRecord.from_db(row) for row in rows]diff --git a/autogpt_platform/backend/backend/data/dream_pass_models.py b/autogpt_platform/backend/backend/data/dream_pass_models.pynew file mode 100644index 0000000000..4bb2c42c26--- /dev/null+++ b/autogpt_platform/backend/backend/data/dream_pass_models.py@@ -0,0 +1,189 @@+"""The shapes of a ``DreamPass`` row: what a new pass inserts, what one+transition writes, and the row as it reads back.++The JSON columns hold the dream's own models; the input bundle keeps the+format the batch path persists to Redis (``copilot/dream/input_bundle.py``).+How an update lands on a row, only ever moving it forward, is+``dream_pass_update.py``.+"""++from datetime import datetime++import prisma.models+from prisma.enums import (+ DreamPassPhase,+ DreamPassRoute,+ DreamPassStatus,+ DreamPassTrigger,+)+from pydantic import BaseModel, Field, model_validator++from backend.copilot.dream.fetch import DreamInput+from backend.copilot.dream.input_bundle import input_bundle_from_dict+from backend.copilot.dream.schemas import (+ ConsolidationOutput,+ DreamOperations,+ DreamOperationsSnapshot,+ DreamPassUsage,+ IngestionDrainStatus,+ RecombinationOutput,+)++# The statuses a row can still move on from. Any other status is terminal: the+# row is final and no update writes it.+OPEN_STATUSES: tuple[DreamPassStatus, ...] = (+ DreamPassStatus.QUEUED,+ DreamPassStatus.RUNNING,+ DreamPassStatus.SUBMITTED,+ DreamPassStatus.APPLYING,+)+++class DreamPhaseOutputs(BaseModel):+ """Each phase's validated output, as the next phase and apply read it."""++ consolidate: ConsolidationOutput | None = None+ recombine: RecombinationOutput | None = None+ sanitize: DreamOperations | None = None+++class DreamPassApplied(BaseModel):+ """What apply reported: the counts and narrative a ``DreamPassResult``+ carries, and the per-operation snapshot."""++ consolidated_count: int = 0+ proposal_count: int = 0+ demotion_count: int = 0+ entity_invalidation_count: int = 0+ dropped_forgotten: int = 0+ pending_writes: int = 0+ failed_writes: int = 0+ summary_for_user: str = ""+ dream_session_id: str | None = None+ ingestion_drain_status: IngestionDrainStatus = IngestionDrainStatus.drained+ snapshot: DreamOperationsSnapshot | None = None+++class DreamPassOperations(BaseModel):+ """The clamped operations apply was handed, then what it reported."""++ planned: DreamOperations | None = None+ applied: DreamPassApplied | None = None+++class DreamPassDraft(BaseModel):+ """A new row: whose pass it is, how it runs, what started it."""++ id: str = Field(min_length=1)+ user_id: str = Field(min_length=1)+ expert_id: str | None = None+ scope_key: str = Field(min_length=1)+ route: DreamPassRoute+ trigger: DreamPassTrigger+ status: DreamPassStatus = DreamPassStatus.RUNNING+ phase: DreamPassPhase = DreamPassPhase.GATHER+ started_at: datetime | None = None+++class DreamPassUpdate(BaseModel):+ """What one transition writes. A ``None`` field leaves its column as it is.++ ``status`` and ``phase`` only ever move forward. ``provider_batch_id`` is+ the batch of the update's ``phase``, which it therefore needs.+ ``phase_outputs`` and ``operations`` merge into what the row holds, one+ top-level field at a time, so a transition sends only the phase or the+ part it produced.+ """++ status: DreamPassStatus | None = None+ phase: DreamPassPhase | None = None+ skip_reason: str | None = None+ provider_batch_id: str | None = None+ lease_token: str | None = None+ lease_expires_at: datetime | None = None+ input_bundle: DreamInput | None = None+ phase_outputs: DreamPhaseOutputs | None = None+ operations: DreamPassOperations | None = None+ usage: DreamPassUsage | None = None+ window_start: datetime | None = None+ window_end: datetime | None = None+ error: str | None = None+ submitted_at: datetime | None = None+ applied_at: datetime | None = None+ completed_at: datetime | None = None++ @model_validator(mode="after")+ def _batch_names_its_phase(self) -> "DreamPassUpdate":+ if self.provider_batch_id is not None and self.phase is None:+ raise ValueError("a provider batch id needs the phase it runs")+ return self+++class DreamPassRecord(BaseModel):+ """One ``DreamPass`` row."""++ id: str+ user_id: str+ expert_id: str | None+ scope_key: str+ route: DreamPassRoute+ trigger: DreamPassTrigger+ phase: DreamPassPhase+ status: DreamPassStatus+ skip_reason: str | None+ cancel_generation: int+ provider_batch_id: str | None+ lease_token: str | None+ lease_expires_at: datetime | None+ input_bundle: DreamInput | None+ phase_outputs: DreamPhaseOutputs+ operations: DreamPassOperations+ usage: DreamPassUsage | None+ window_start: datetime | None+ window_end: datetime | None+ error: str | None+ created_at: datetime+ started_at: datetime | None+ submitted_at: datetime | None+ applied_at: datetime | None+ completed_at: datetime | None+ updated_at: datetime++ @classmethod+ def from_db(cls, row: prisma.models.DreamPass) -> "DreamPassRecord":+ return cls(+ id=row.id,+ user_id=row.userId,+ expert_id=row.expertId,+ scope_key=row.scopeKey,+ route=row.route,+ trigger=row.trigger,+ phase=row.phase,+ status=row.status,+ skip_reason=row.skipReason,+ cancel_generation=row.cancelGeneration,+ provider_batch_id=row.providerBatchId,+ lease_token=row.leaseToken,+ lease_expires_at=row.leaseExpiresAt,+ input_bundle=(+ input_bundle_from_dict(dict(row.inputBundle))+ if row.inputBundle is not None+ else None+ ),+ phase_outputs=DreamPhaseOutputs.model_validate(row.phaseOutputs or {}),+ operations=DreamPassOperations.model_validate(row.operations or {}),+ usage=(+ DreamPassUsage.model_validate(row.usage)+ if row.usage is not None+ else None+ ),+ window_start=row.windowStart,+ window_end=row.windowEnd,+ error=row.error,+ created_at=row.createdAt,+ started_at=row.startedAt,+ submitted_at=row.submittedAt,+ applied_at=row.appliedAt,+ completed_at=row.completedAt,+ updated_at=row.updatedAt,+ )diff --git a/autogpt_platform/backend/backend/data/dream_pass_test.py b/autogpt_platform/backend/backend/data/dream_pass_test.pynew file mode 100644index 0000000000..236aa5e8bf--- /dev/null+++ b/autogpt_platform/backend/backend/data/dream_pass_test.py@@ -0,0 +1,339 @@+"""The DreamPass table against a real database: the column mapping, the JSON+merges, the owner check, and that a finished pass's row is final."""++import uuid+from collections.abc import AsyncIterator, Awaitable, Callable+from datetime import datetime, timedelta, timezone++import pytest+from prisma.enums import (+ DreamPassPhase,+ DreamPassRoute,+ DreamPassStatus,+ DreamPassTrigger,+)+from prisma.models import DreamPass as PrismaDreamPass+from prisma.models import Expert, User++from backend.copilot.dream.fetch import DreamInput, EpisodeRow, FactRow, SessionRow+from backend.copilot.dream.input_bundle import input_bundle_to_dict+from backend.copilot.dream.schemas import (+ ConsolidatedFact,+ ConsolidationOutput,+ DreamOperations,+ DreamPassUsage,+ PhaseUsage,+ RecombinationOutput,+)+from backend.util.json import SafeJson++from .dream_pass import (+ create_dream_pass,+ get_dream_pass,+ get_dream_pass_for_user,+ list_dream_passes,+ list_open_dream_passes,+ update_dream_pass,+)+from .dream_pass_models import (+ DreamPassApplied,+ DreamPassDraft,+ DreamPassOperations,+ DreamPassUpdate,+ DreamPhaseOutputs,+)++pytestmark = pytest.mark.asyncio(loop_scope="session")++# Postgres keeps TIMESTAMP(3): whole milliseconds read back exactly.+_NOW = datetime.now(timezone.utc).replace(microsecond=0)+++async def make_user() -> AsyncIterator[Callable[[], Awaitable[str]]]:+ """Create throwaway users; deleting them afterwards cascades to their+ passes."""+ created: list[str] = []++ async def make() -> str:+ user_id = str(uuid.uuid4())+ await User.prisma().create(+ data={+ "id": user_id,+ "email": f"dream-pass-{user_id}@example.com",+ "topUpConfig": SafeJson({}),+ "timezone": "UTC",+ }+ )+ created.append(user_id)+ return user_id++ yield make+ await User.prisma().delete_many(where={"id": {"in": created}})+++def _draft(user_id: str, **overrides) -> DreamPassDraft:+ fields = {+ "id": str(uuid.uuid4()),+ "user_id": user_id,+ "scope_key": user_id,+ "route": DreamPassRoute.SYNC,+ "trigger": DreamPassTrigger.CRON,+ "started_at": _NOW,+ }+ return DreamPassDraft(**{**fields, **overrides})+++async def test_a_new_row_reads_back_running_and_gathering(make_user):+ owner = await make_user()+ draft = _draft(owner)++ created = await create_dream_pass(draft)++ fetched = await get_dream_pass(draft.id)+ assert fetched == created+ assert fetched is not None+ assert (fetched.status, fetched.phase, fetched.route, fetched.trigger) == (+ DreamPassStatus.RUNNING,+ DreamPassPhase.GATHER,+ DreamPassRoute.SYNC,+ DreamPassTrigger.CRON,+ )+ assert fetched.started_at == _NOW+ assert fetched.cancel_generation == 0+ assert fetched.phase_outputs == DreamPhaseOutputs()+ assert fetched.operations == DreamPassOperations()+ assert fetched.input_bundle is None and fetched.usage is None+++async def test_an_update_writes_only_the_columns_it_sets(make_user):+ owner = await make_user()+ draft = _draft(owner)+ await create_dream_pass(draft)++ assert await update_dream_pass(+ draft.id,+ DreamPassUpdate(+ phase=DreamPassPhase.CONSOLIDATE,+ window_start=_NOW - timedelta(days=14),+ window_end=_NOW,+ ),+ )+ assert await update_dream_pass(+ draft.id,+ DreamPassUpdate(phase=DreamPassPhase.CONSOLIDATE, provider_batch_id="b1"),+ )++ row = await get_dream_pass(draft.id)+ assert row is not None+ assert row.phase is DreamPassPhase.CONSOLIDATE+ assert (row.window_start, row.window_end) == (_NOW - timedelta(days=14), _NOW)+ assert row.provider_batch_id == "b1"+ assert row.status is DreamPassStatus.RUNNING+++async def test_phase_outputs_and_operations_merge_one_field_at_a_time(make_user):+ owner = await make_user()+ draft = _draft(owner)+ await create_dream_pass(draft)+ consolidated = ConsolidationOutput(+ facts=[ConsolidatedFact(content="Nick ships on Fridays", confidence=0.8)]+ )+ planned = DreamOperations(summary_for_user="clamped")+ applied = DreamPassApplied(consolidated_count=1, dream_session_id="s1")++ for update in (+ DreamPassUpdate(phase_outputs=DreamPhaseOutputs(consolidate=consolidated)),+ DreamPassUpdate(+ phase_outputs=DreamPhaseOutputs(recombine=RecombinationOutput())+ ),+ DreamPassUpdate(operations=DreamPassOperations(planned=planned)),+ DreamPassUpdate(operations=DreamPassOperations(applied=applied)),+ ):+ assert await update_dream_pass(draft.id, update)++ row = await get_dream_pass(draft.id)+ assert row is not None+ assert row.phase_outputs == DreamPhaseOutputs(+ consolidate=consolidated, recombine=RecombinationOutput()+ )+ assert row.operations == DreamPassOperations(planned=planned, applied=applied)+++async def test_the_input_bundle_keeps_the_batch_paths_format(make_user):+ owner = await make_user()+ draft = _draft(owner, route=DreamPassRoute.ANTHROPIC_BATCH)+ await create_dream_pass(draft)+ bundle = DreamInput(+ user_id=owner,+ group_id=f"user_{owner}",+ window_start=_NOW - timedelta(days=14),+ window_end=_NOW,+ episodes=[+ EpisodeRow(+ uuid="e1",+ name="chat",+ content="I ship on Fridays",+ source_description=None,+ valid_at="2026-09-25T10:00:00Z",+ created_at=None,+ )+ ],+ facts=[+ FactRow(+ uuid="f1",+ source="Nick",+ target="Fridays",+ name="ships_on",+ fact="Nick ships on Fridays",+ scope="real:global",+ confidence=0.7,+ status="active",+ created_at=None,+ )+ ],+ recent_sessions=[+ SessionRow(session_id="s1", title="Release", created_at=_NOW, body="hi")+ ],+ known_fact_uuids={"f1"},+ known_episode_uuids={"e1"},+ )+ usage = DreamPassUsage(+ phases=[PhaseUsage(phase="consolidate", model="claude-sonnet-5")],+ total_cost_usd=0.01,+ discount_applied=0.5,+ )++ assert await update_dream_pass(+ draft.id, DreamPassUpdate(input_bundle=bundle, usage=usage)+ )++ raw = await PrismaDreamPass.prisma().find_unique(where={"id": draft.id})+ assert raw is not None+ assert raw.inputBundle == input_bundle_to_dict(bundle)+ row = await get_dream_pass(draft.id)+ assert row is not None+ assert row.input_bundle == bundle+ assert row.usage == usage+++async def test_a_finished_pass_is_final(make_user):+ owner = await make_user()+ draft = _draft(owner)+ await create_dream_pass(draft)+ assert await update_dream_pass(+ draft.id,+ DreamPassUpdate(+ status=DreamPassStatus.COMPLETE,+ phase=DreamPassPhase.DONE,+ completed_at=_NOW,+ ),+ )++ assert not await update_dream_pass(+ draft.id,+ DreamPassUpdate(status=DreamPassStatus.ERRORED, error="late crash"),+ )+ assert not await update_dream_pass(+ draft.id,+ DreamPassUpdate(+ phase_outputs=DreamPhaseOutputs(consolidate=ConsolidationOutput())+ ),+ )+ row = await get_dream_pass(draft.id)+ assert row is not None+ assert (row.status, row.error) == (DreamPassStatus.COMPLETE, None)+ assert row.phase_outputs == DreamPhaseOutputs()+++async def test_a_missing_pass_is_not_written():+ assert not await update_dream_pass(+ "no-such-pass", DreamPassUpdate(phase=DreamPassPhase.CONSOLIDATE)+ )+ assert not await update_dream_pass(+ "no-such-pass",+ DreamPassUpdate(+ phase_outputs=DreamPhaseOutputs(recombine=RecombinationOutput())+ ),+ )+++async def test_only_the_owner_reads_a_pass(make_user):+ owner, stranger = await make_user(), await make_user()+ draft = _draft(owner)+ await create_dream_pass(draft)++ owned = await get_dream_pass_for_user(draft.id, owner)++ assert owned is not None and owned.id == draft.id+ assert await get_dream_pass_for_user(draft.id, stranger) is None+ assert await get_dream_pass_for_user("no-such-pass", owner) is None+++async def test_open_passes_are_listed_per_scope(make_user):+ owner = await make_user()+ scope, other_scope = f"{owner}:a", f"{owner}:b"+ running = _draft(owner, scope_key=scope)+ submitted = _draft(owner, scope_key=scope, status=DreamPassStatus.SUBMITTED)+ finished = _draft(owner, scope_key=scope)+ elsewhere = _draft(owner, scope_key=other_scope)+ for draft in (running, submitted, finished, elsewhere):+ await create_dream_pass(draft)+ await update_dream_pass(+ finished.id, DreamPassUpdate(status=DreamPassStatus.SKIPPED)+ )++ open_passes = await list_open_dream_passes(scope)++ assert {row.id for row in open_passes} == {running.id, submitted.id}+++async def test_a_users_passes_list_newest_first(make_user):+ owner = await make_user()+ drafts = [_draft(owner, scope_key=f"{owner}:{i}") for i in range(3)]+ for draft in drafts:+ await create_dream_pass(draft)++ rows = await list_dream_passes(owner)++ assert {row.id for row in rows} == {draft.id for draft in drafts}+ created = [row.created_at for row in rows]+ assert created == sorted(created, reverse=True)+ assert len(await list_dream_passes(owner, limit=1)) == 1+++async def test_deleting_the_expert_or_the_user_removes_their_passes(make_user):+ owner = await make_user()+ expert = await Expert.prisma().create(+ data={+ "ownerUserId": owner,+ "name": "Scout",+ "role": "Research",+ "identity": "Finds things out.",+ }+ )+ experts_pass = _draft(owner, expert_id=expert.id, scope_key=f"{owner}:expert")+ accounts_pass = _draft(owner)+ await create_dream_pass(experts_pass)+ await create_dream_pass(accounts_pass)++ await Expert.prisma().delete(where={"id": expert.id})+ assert await get_dream_pass(experts_pass.id) is None+ assert await get_dream_pass(accounts_pass.id) is not None++ await User.prisma().delete(where={"id": owner})+ assert await get_dream_pass(accounts_pass.id) is None+++async def test_text_loses_the_control_characters_postgres_rejects(make_user):+ owner = await make_user()+ draft = _draft(owner)+ await create_dream_pass(draft)++ assert await update_dream_pass(+ draft.id,+ DreamPassUpdate(status=DreamPassStatus.ERRORED, error="bad\x00byte"),+ )++ row = await get_dream_pass(draft.id)+ assert row is not None and row.error == "badbyte"diff --git a/autogpt_platform/backend/backend/data/dream_pass_update.py b/autogpt_platform/backend/backend/data/dream_pass_update.pynew file mode 100644index 0000000000..d7f7005723--- /dev/null+++ b/autogpt_platform/backend/backend/data/dream_pass_update.py@@ -0,0 +1,106 @@+"""One ``DreamPass`` transition as a single conditional ``UPDATE``.++A pass's writes can land out of order: the batch path's submit write can reach+the database after the first result callback's, and a write the store gave up+on (``copilot/dream/store.py`` bounds each one) can still be executed by the+DatabaseManager later. So each column only moves forward, decided inside the+statement against the row as it stands when the statement runs:++ * ``status`` and ``phase`` take the new value only when it comes later in+ their enum's declared order, which is the order a pass moves through them;+ * ``providerBatchId`` is the batch of the update's ``phase``: it is written+ only while the row has not moved past that phase, so a late write for an+ earlier phase never replaces a later phase's batch;+ * ``phaseOutputs`` and ``operations`` merge key by key with jsonb ``||``, so+ two callbacks writing different phases both land;+ * every other column takes the new value when the update gives one.++Nothing is written to a row that has reached a terminal status. The statement+text is constant; every value is a bound parameter.+"""++from typing import Any++from pydantic import BaseModel++from backend.copilot.dream.input_bundle import input_bundle_to_dict+from backend.data.dream_pass_models import OPEN_STATUSES, DreamPassUpdate+from backend.util.json import dumps, sanitize_json, sanitize_string++# ``{schema_prefix}`` is filled in by ``db.execute_raw_with_schema``; the+# doubled braces are a literal empty JSON object once it has.+TRANSITION_SQL = """+UPDATE {schema_prefix}"DreamPass" SET+ "status" = CASE WHEN $2::{schema_prefix}"DreamPassStatus" > "status"+ THEN $2::{schema_prefix}"DreamPassStatus" ELSE "status" END,+ "phase" = CASE WHEN $3::{schema_prefix}"DreamPassPhase" > "phase"+ THEN $3::{schema_prefix}"DreamPassPhase" ELSE "phase" END,+ "providerBatchId" = CASE WHEN $4::text IS NOT NULL+ AND "phase" <= $3::{schema_prefix}"DreamPassPhase"+ THEN $4::text ELSE "providerBatchId" END,+ "skipReason" = COALESCE($5::text, "skipReason"),+ "error" = COALESCE($6::text, "error"),+ "leaseToken" = COALESCE($7::text, "leaseToken"),+ "leaseExpiresAt" = COALESCE(+ $8::timestamptz AT TIME ZONE 'UTC', "leaseExpiresAt"),+ "windowStart" = COALESCE($9::timestamptz AT TIME ZONE 'UTC', "windowStart"),+ "windowEnd" = COALESCE($10::timestamptz AT TIME ZONE 'UTC', "windowEnd"),+ "submittedAt" = COALESCE($11::timestamptz AT TIME ZONE 'UTC', "submittedAt"),+ "appliedAt" = COALESCE($12::timestamptz AT TIME ZONE 'UTC', "appliedAt"),+ "completedAt" = COALESCE($13::timestamptz AT TIME ZONE 'UTC', "completedAt"),+ "inputBundle" = COALESCE($14::jsonb, "inputBundle"),+ "usage" = COALESCE($15::jsonb, "usage"),+ "phaseOutputs" = CASE WHEN $16::jsonb IS NULL THEN "phaseOutputs"+ ELSE COALESCE("phaseOutputs", '{{}}'::jsonb) || $16::jsonb END,+ "operations" = CASE WHEN $17::jsonb IS NULL THEN "operations"+ ELSE COALESCE("operations", '{{}}'::jsonb) || $17::jsonb END,+ "updatedAt" = CURRENT_TIMESTAMP AT TIME ZONE 'UTC'+WHERE "id" = $1 AND "status"::text = ANY($18::text[])+"""+++def transition_args(pass_id: str, update: DreamPassUpdate) -> list[Any]:+ """``TRANSITION_SQL``'s parameters in order, ``None`` for each column the+ update leaves alone. Free text and JSON lose the control characters+ Postgres rejects."""+ return [+ pass_id,+ update.status.value if update.status is not None else None,+ update.phase.value if update.phase is not None else None,+ update.provider_batch_id,+ _text(update.skip_reason),+ _text(update.error),+ update.lease_token,+ update.lease_expires_at,+ update.window_start,+ update.window_end,+ update.submitted_at,+ update.applied_at,+ update.completed_at,+ (+ _json(input_bundle_to_dict(update.input_bundle))+ if update.input_bundle is not None+ else None+ ),+ _json(update.usage.model_dump(mode="json")) if update.usage else None,+ _merge_part(update.phase_outputs),+ _merge_part(update.operations),+ [status.value for status in OPEN_STATUSES],+ ]+++def _merge_part(part: BaseModel | None) -> str | None:+ """The top-level fields *part* sets, as the JSON object merged into its+ column; ``None`` when the update leaves the column alone."""+ if part is None:+ return None+ fields = part.model_dump(mode="json")+ return _json({key: value for key, value in fields.items() if value is not None})+++def _text(value: str | None) -> str | None:+ return sanitize_string(value) if value is not None else None+++def _json(value: Any) -> str:+ return dumps(sanitize_json(value))diff --git a/autogpt_platform/backend/backend/data/dream_pass_update_test.py b/autogpt_platform/backend/backend/data/dream_pass_update_test.pynew file mode 100644index 0000000000..607550c5bc--- /dev/null+++ b/autogpt_platform/backend/backend/data/dream_pass_update_test.py@@ -0,0 +1,240 @@+"""A DreamPass transition against a real database: each column only moves+forward, the JSON columns merge inside the statement, and writes that land+out of order or at the same time end in the row they should."""++import asyncio+import uuid+from collections.abc import AsyncIterator+from datetime import datetime, timedelta, timezone++import pytest+from prisma.enums import (+ DreamPassPhase,+ DreamPassRoute,+ DreamPassStatus,+ DreamPassTrigger,+)+from prisma.models import User++from backend.copilot.dream.fetch import DreamInput+from backend.copilot.dream.schemas import (+ ConsolidationOutput,+ DreamOperations,+ RecombinationOutput,+)+from backend.data.db import query_raw_with_schema+from backend.util.json import SafeJson++from .dream_pass import create_dream_pass, get_dream_pass, update_dream_pass+from .dream_pass_models import (+ DreamPassApplied,+ DreamPassDraft,+ DreamPassOperations,+ DreamPassRecord,+ DreamPassUpdate,+ DreamPhaseOutputs,+)++pytestmark = pytest.mark.asyncio(loop_scope="session")++_NOW = datetime.now(timezone.utc).replace(microsecond=0)+++async def owner() -> AsyncIterator[str]:+ """A throwaway user; deleting it afterwards cascades to its passes."""+ user_id = str(uuid.uuid4())+ await User.prisma().create(+ data={+ "id": user_id,+ "email": f"dream-pass-update-{user_id}@example.com",+ "topUpConfig": SafeJson({}),+ "timezone": "UTC",+ }+ )+ yield user_id+ await User.prisma().delete(where={"id": user_id})+++async def _new_pass(owner: str) -> str:+ pass_id = str(uuid.uuid4())+ await create_dream_pass(+ DreamPassDraft(+ id=pass_id,+ user_id=owner,+ scope_key=owner,+ route=DreamPassRoute.ANTHROPIC_BATCH,+ trigger=DreamPassTrigger.CRON,+ started_at=_NOW,+ )+ )+ return pass_id+++async def _row(pass_id: str) -> DreamPassRecord:+ row = await get_dream_pass(pass_id)+ assert row is not None+ return row+++async def test_status_and_phase_only_move_forward(owner):+ pass_id = await _new_pass(owner)+ await update_dream_pass(+ pass_id,+ DreamPassUpdate(+ status=DreamPassStatus.SUBMITTED, phase=DreamPassPhase.SANITIZE+ ),+ )++ behind = DreamPassUpdate(+ status=DreamPassStatus.RUNNING, phase=DreamPassPhase.CONSOLIDATE+ )+ assert await update_dream_pass(pass_id, behind)+ row = await _row(pass_id)+ assert (row.status, row.phase) == (+ DreamPassStatus.SUBMITTED,+ DreamPassPhase.SANITIZE,+ )++ ahead = DreamPassUpdate(status=DreamPassStatus.APPLYING, phase=DreamPassPhase.APPLY)+ assert await update_dream_pass(pass_id, ahead)+ row = await _row(pass_id)+ assert (row.status, row.phase) == (DreamPassStatus.APPLYING, DreamPassPhase.APPLY)+++async def test_a_late_submit_keeps_the_first_callbacks_phase_and_batch(owner):+ """The batch submit's write reaching the database after the consolidate+ callback's must leave the row at recombine, on the recombine batch."""+ pass_id = await _new_pass(owner)+ bundle = DreamInput(+ user_id=owner, group_id=f"user_{owner}", window_start=_NOW, window_end=_NOW+ )+ consolidated = ConsolidationOutput()+ await update_dream_pass(+ pass_id,+ DreamPassUpdate(+ phase=DreamPassPhase.RECOMBINE,+ phase_outputs=DreamPhaseOutputs(consolidate=consolidated),+ ),+ )+ await update_dream_pass(+ pass_id,+ DreamPassUpdate(+ phase=DreamPassPhase.RECOMBINE, provider_batch_id="batch-recombine"+ ),+ )++ await update_dream_pass(+ pass_id,+ DreamPassUpdate(+ status=DreamPassStatus.SUBMITTED,+ phase=DreamPassPhase.CONSOLIDATE,+ provider_batch_id="batch-consolidate",+ input_bundle=bundle,+ lease_token="tok",+ lease_expires_at=_NOW + timedelta(hours=24),+ submitted_at=_NOW,+ ),+ )++ row = await _row(pass_id)+ assert row.status is DreamPassStatus.SUBMITTED+ assert row.phase is DreamPassPhase.RECOMBINE+ assert row.provider_batch_id == "batch-recombine"+ assert row.phase_outputs == DreamPhaseOutputs(consolidate=consolidated)+ assert (row.input_bundle, row.lease_token) == (bundle, "tok")+ assert row.lease_expires_at == _NOW + timedelta(hours=24)+++async def test_a_batch_for_an_earlier_phase_never_replaces_a_later_one(owner):+ pass_id = await _new_pass(owner)+ await update_dream_pass(+ pass_id,+ DreamPassUpdate(phase=DreamPassPhase.SANITIZE, provider_batch_id="batch-s"),+ )++ await update_dream_pass(+ pass_id,+ DreamPassUpdate(phase=DreamPassPhase.RECOMBINE, provider_batch_id="batch-r"),+ )++ row = await _row(pass_id)+ assert (row.phase, row.provider_batch_id) == (DreamPassPhase.SANITIZE, "batch-s")+++async def test_writers_racing_on_the_json_columns_all_land(owner):+ """Each writer merges its own key in the statement, so none of them can+ overwrite another's with a stale copy of the column."""+ parts = DreamPassUpdate(+ phase_outputs=DreamPhaseOutputs(+ consolidate=ConsolidationOutput(),+ recombine=RecombinationOutput(),+ sanitize=DreamOperations(summary_for_user="sanitized"),+ ),+ operations=DreamPassOperations(+ planned=DreamOperations(summary_for_user="planned"),+ applied=DreamPassApplied(consolidated_count=2),+ ),+ )+ writers = [+ DreamPassUpdate(+ phase_outputs=DreamPhaseOutputs(consolidate=ConsolidationOutput())+ ),+ DreamPassUpdate(+ phase_outputs=DreamPhaseOutputs(recombine=RecombinationOutput())+ ),+ DreamPassUpdate(+ phase_outputs=DreamPhaseOutputs(+ sanitize=DreamOperations(summary_for_user="sanitized")+ )+ ),+ DreamPassUpdate(+ operations=DreamPassOperations(+ planned=DreamOperations(summary_for_user="planned")+ )+ ),+ DreamPassUpdate(+ operations=DreamPassOperations(+ applied=DreamPassApplied(consolidated_count=2)+ )+ ),+ ]+ for _ in range(5):+ pass_id = await _new_pass(owner)++ written = await asyncio.gather(+ *(update_dream_pass(pass_id, writer) for writer in writers)+ )++ assert written == [True] * len(writers)+ row = await _row(pass_id)+ assert (row.phase_outputs, row.operations) == (+ parts.phase_outputs,+ parts.operations,+ )+++async def test_timestamps_land_as_the_same_instant(owner):+ """A timestamp in any zone is stored as its UTC wall clock, the way the+ Prisma client writes the table's other timestamps."""+ pass_id = await _new_pass(owner)+ brisbane = datetime(2026, 9, 26, 13, 0, tzinfo=timezone(timedelta(hours=10)))+ before = await _row(pass_id)++ await update_dream_pass(+ pass_id,+ DreamPassUpdate(lease_expires_at=brisbane, window_start=_NOW),+ )++ row = await _row(pass_id)+ assert row.lease_expires_at == datetime(2026, 9, 26, 3, 0, tzinfo=timezone.utc)+ assert row.window_start == _NOW+ assert row.updated_at >= before.updated_at+++async def test_deleting_an_expert_finds_its_passes_by_index():+ indexes = await query_raw_with_schema(+ "SELECT indexname FROM pg_indexes WHERE tablename = 'DreamPass'"+ )++ assert "DreamPass_expertId_idx" in {row["indexname"] for row in indexes}diff --git a/autogpt_platform/backend/backend/executor/scheduler.py b/autogpt_platform/backend/backend/executor/scheduler.pyindex 8944c7d8f9..814b08ad93 100644--- a/autogpt_platform/backend/backend/executor/scheduler.py+++ b/autogpt_platform/backend/backend/executor/scheduler.py@@ -1708,7 +1708,10 @@ def execute_nightly_batch_sync(def _submit_nightly_batch(- user_id: str, expert_id: str | None = None+ user_id: str,+ expert_id: str | None = None,+ *,+ trigger: Literal["cron", "admin"] = "cron",) -> "NightlyBatchResult | None":"""Flag-gated nightly batch-family fan-out of one scope, outcome logged.@@ -1729,7 +1732,8 @@ def _submit_nightly_batch(Returns the typed ``NightlyBatchResult`` so the admin``*_with_status`` wrapper can persist it on the JobStatus row.Returns ``None`` only when the runtime flag gate short-circuits- before the submitter runs.+ before the submitter runs. ``trigger`` is what the dream pass's+ record says started it: the cron, or the admin wrapper."""from backend.copilot.dream.nightly_batch import (NightlyBatchResult,@@ -1746,7 +1750,7 @@ def _submit_nightly_batch(return Noneresult: NightlyBatchResult = run_async(- run_nightly_batch_submit(user_id, expert_id=expert_id),+ run_nightly_batch_submit(user_id, expert_id=expert_id, trigger=trigger),timeout=SCHEDULER_DREAM_OPERATION_TIMEOUT_SECONDS,)log_nightly_outcome(scope, result)@@ -1861,7 +1865,7 @@ def execute_nightly_batch_with_status(user_id: str, job_id: str):try:# The admin trigger bypasses the registry gate of the cron body:# it runs the account's pass whatever state its scope row is in.- result = _submit_nightly_batch(user_id)+ result = _submit_nightly_batch(user_id, trigger="admin")except Exception as exc:logger.exception("Admin-triggered nightly batch crashed for user %s job %s",@@ -1968,7 +1972,7 @@ def execute_dream_pass_with_status(user_id: str, job_id: str):try:result = run_async(- execute_dream_pass(user_id, status_id=job_id),+ execute_dream_pass(user_id, status_id=job_id, trigger="admin"),timeout=SCHEDULER_DREAM_OPERATION_TIMEOUT_SECONDS,)except Exception as exc:diff --git a/autogpt_platform/backend/backend/executor/scheduler_test.py b/autogpt_platform/backend/backend/executor/scheduler_test.pyindex 7eec794e36..d7cac20d8f 100644--- a/autogpt_platform/backend/backend/executor/scheduler_test.py+++ b/autogpt_platform/backend/backend/executor/scheduler_test.py@@ -1216,12 +1216,13 @@ def _run_nightly_wrapper(result):patch("backend.executor.scheduler._submit_nightly_batch",return_value=result,- ),+ ) as submit_mock,patch("backend.copilot.dream.job_status.mark_complete") as complete_mock,patch("backend.copilot.dream.job_status.mark_errored") as errored_mock,patch("backend.copilot.dream.job_status.update_status_phase") as phase_mock,):execute_nightly_batch_with_status("abc", "job-1")+ submit_mock.assert_called_once_with("abc", trigger="admin")return complete_mock, errored_mock, phase_mockdiff --git a/autogpt_platform/backend/migrations/20260926180000_add_dream_pass/migration.sql b/autogpt_platform/backend/migrations/20260926180000_add_dream_pass/migration.sqlnew file mode 100644index 0000000000..7823284213--- /dev/null+++ b/autogpt_platform/backend/migrations/20260926180000_add_dream_pass/migration.sql@@ -0,0 +1,62 @@+-- The durable record of each dream pass: one row per pass, written by the+-- sync orchestrator as each step runs and by the batch path at submit and in+-- each result callback. A new table, so nothing existing is rewritten.++-- CreateEnum+CREATE TYPE "DreamPassRoute" AS ENUM ('SYNC', 'ANTHROPIC_BATCH');++-- CreateEnum+CREATE TYPE "DreamPassTrigger" AS ENUM ('CRON', 'ADMIN', 'EVAL');++-- CreateEnum+CREATE TYPE "DreamPassPhase" AS ENUM ('GATHER', 'CONSOLIDATE', 'RECOMBINE', 'SANITIZE', 'APPLY', 'DONE');++-- CreateEnum+CREATE TYPE "DreamPassStatus" AS ENUM ('QUEUED', 'RUNNING', 'SUBMITTED', 'APPLYING', 'COMPLETE', 'ERRORED', 'CANCELLED', 'EXPIRED', 'SKIPPED');++-- CreateTable+CREATE TABLE "DreamPass" (+ "id" TEXT NOT NULL,+ "userId" TEXT NOT NULL,+ "expertId" TEXT,+ "scopeKey" TEXT NOT NULL,+ "route" "DreamPassRoute" NOT NULL,+ "trigger" "DreamPassTrigger" NOT NULL,+ "phase" "DreamPassPhase" NOT NULL DEFAULT 'GATHER',+ "status" "DreamPassStatus" NOT NULL DEFAULT 'QUEUED',+ "skipReason" TEXT,+ "cancelGeneration" INTEGER NOT NULL DEFAULT 0,+ "providerBatchId" TEXT,+ "leaseToken" TEXT,+ "leaseExpiresAt" TIMESTAMP(3),+ "inputBundle" JSONB,+ "phaseOutputs" JSONB,+ "operations" JSONB,+ "usage" JSONB,+ "windowStart" TIMESTAMP(3),+ "windowEnd" TIMESTAMP(3),+ "error" TEXT,+ "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,+ "startedAt" TIMESTAMP(3),+ "submittedAt" TIMESTAMP(3),+ "appliedAt" TIMESTAMP(3),+ "completedAt" TIMESTAMP(3),+ "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,++ CONSTRAINT "DreamPass_pkey" PRIMARY KEY ("id")+);++-- CreateIndex+CREATE INDEX "DreamPass_scopeKey_status_idx" ON "DreamPass"("scopeKey", "status");++-- CreateIndex+CREATE INDEX "DreamPass_userId_createdAt_idx" ON "DreamPass"("userId", "createdAt");++-- CreateIndex+CREATE INDEX "DreamPass_expertId_idx" ON "DreamPass"("expertId");++-- AddForeignKey+ALTER TABLE "DreamPass" ADD CONSTRAINT "DreamPass_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;++-- AddForeignKey+ALTER TABLE "DreamPass" ADD CONSTRAINT "DreamPass_expertId_fkey" FOREIGN KEY ("expertId") REFERENCES "Expert"("id") ON DELETE CASCADE ON UPDATE CASCADE;diff --git a/autogpt_platform/backend/schema.prisma b/autogpt_platform/backend/schema.prismaindex f9f7cd23c8..8d916cca4d 100644--- a/autogpt_platform/backend/schema.prisma+++ b/autogpt_platform/backend/schema.prisma@@ -180,6 +180,7 @@ model User {ExpertRoutines ExpertRoutine[]+ DreamPasses DreamPass[]ScheduledChatStreaks ScheduledChatStreak[]MemoryScopeSchedules MemoryScopeSchedule[]@@ -537,6 +538,105 @@ model ActivityEvent {@@index([expertId, createdAt])}+// The route a dream pass took: the chat transport's sync path, or Anthropic's+// Message Batches API.+enum DreamPassRoute {+ SYNC+ ANTHROPIC_BATCH+}++enum DreamPassTrigger {+ CRON+ ADMIN+ EVAL+}++// The step a dream pass is at: gathering its input, running (or waiting on+// the batch of) one of its three phases, applying what the last one returned,+// or done. Declared in that order, and the order is load-bearing: an update+// only moves a row's phase to a later value (data/dream_pass_update.py).+enum DreamPassPhase {+ GATHER+ CONSOLIDATE+ RECOMBINE+ SANITIZE+ APPLY+ DONE+}++// Open statuses first, in the order a pass moves through them, then the+// terminal ones. An update only moves a row's status to a later value.+enum DreamPassStatus {+ QUEUED+ RUNNING+ SUBMITTED+ APPLYING+ COMPLETE+ ERRORED+ CANCELLED+ EXPIRED+ SKIPPED+}++// One dream pass over one memory scope (the account's or an expert's), from+// gather to apply, on either route. The sync orchestrator writes it as each+// step runs; the batch path at submit and in each result callback. Redis still+// holds the lock, the batch state and the admin job status: this row is the+// durable record beside them, and a failed write never fails the pass.+model DreamPass {+ // The pass id the orchestrator mints, which every phase's cost row carries.+ id String @id++ userId String+ User User @relation(fields: [userId], references: [id], onDelete: Cascade)+ expertId String?+ Expert Expert? @relation(fields: [expertId], references: [id], onDelete: Cascade)+ // MemoryScope.scope_key: what the scope's dream lock and markers key on.+ scopeKey String++ route DreamPassRoute+ trigger DreamPassTrigger+ phase DreamPassPhase @default(GATHER)+ status DreamPassStatus @default(QUEUED)+ skipReason String?+ // Bumped by a cancel, so a step that started under an older generation+ // knows to stop. Nothing cancels yet.+ cancelGeneration Int @default(0)++ // The provider batch of the phase in flight (batch route only).+ providerBatchId String?+ // The dream lock's ownership token, and when the lock lapses unless renewed.+ leaseToken String?+ leaseExpiresAt DateTime?++ // The pass's DreamInput, in the format the batch path already persists to+ // Redis (batch route only).+ inputBundle Json?+ // Each phase's validated output, keyed by phase.+ phaseOutputs Json?+ // The clamped operations apply was handed, and what apply reported.+ operations Json?+ // DreamPassUsage: tokens and cost, per phase and in total.+ usage Json?++ // The episode window the pass gathered.+ windowStart DateTime?+ windowEnd DateTime?+ error String?++ createdAt DateTime @default(now())+ startedAt DateTime?+ submittedAt DateTime?+ appliedAt DateTime?+ completedAt DateTime?+ updatedAt DateTime @default(now()) @updatedAt++ @@index([scopeKey, status])+ @@index([userId, createdAt])+ // Deleting an expert cascades here; without it that scans every pass.+ @@index([expertId])+}+//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// USER WORKSPACE TABLES /////////////////@@ -1294,6 +1394,7 @@ model Expert {PauseEvents ExpertPauseEvent[]BundledSkills ExpertSkillListing[]Routines ExpertRoutine[]+ DreamPasses DreamPass[]MemorySchedule MemoryScopeSchedule?// Owner-scoped grouping into named "pods" (distinct from org-tenancy Team).diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.jsonindex 5c01fbf8eb..b9721c1b80 100644--- a/autogpt_platform/frontend/src/app/api/openapi.json+++ b/autogpt_platform/frontend/src/app/api/openapi.json@@ -1569,6 +1569,62 @@}}},+ "/api/admin/memory/{user_id}/dream/{pass_id}/record": {+ "get": {+ "tags": ["v2", "admin", "admin", "memory"],+ "summary": "Get Dream Pass Record",+ "description": "Read one dream pass's durable record, on either route.\n\nThe job status above is a Redis row that lapses after six hours and, for\na batch pass, carries no usage; the record outlives the pass and holds\nwhat its phases used. 404 for a pass that does not exist or belongs to\nanother user, alike.",+ "operationId": "getV2GetDreamPassRecord",+ "security": [{ "HTTPBearerJWT": [] }],+ "parameters": [+ {+ "name": "user_id",+ "in": "path",+ "required": true,+ "schema": {+ "type": "string",+ "description": "User id or 'me'",+ "title": "User Id"+ },+ "description": "User id or 'me'"+ },+ {+ "name": "pass_id",+ "in": "path",+ "required": true,+ "schema": {+ "type": "string",+ "description": "The dream pass id",+ "title": "Pass Id"+ },+ "description": "The dream pass id"+ }+ ],+ "responses": {+ "200": {+ "description": "Successful Response",+ "content": {+ "application/json": {+ "schema": {+ "$ref": "#/components/schemas/DreamPassRecordResponse"+ }+ }+ }+ },+ "401": {+ "$ref": "#/components/responses/HTTP401NotAuthenticatedError"+ },+ "422": {+ "description": "Validation Error",+ "content": {+ "application/json": {+ "schema": { "$ref": "#/components/schemas/HTTPValidationError" }+ }+ }+ }+ }+ }+ },"/api/admin/memory/{user_id}/entities": {"get": {"tags": ["v2", "admin", "admin", "memory"],@@ -21632,6 +21688,49 @@"title": "ConnectionTier","description": "One quality level on a connection.\n\n``display_model`` is the model this tier resolves to, run through the\nsame router the turn will use — LaunchDarkly cell, then registry, then\nconfig — so it cannot drift from what actually answers.\n\nOn a ChatGPT connection it is the model the catalog pins for that cell,\nread straight from the registry -- no lease, no call to the account.\nWhat a lease would add is confirmation that this particular account still\nadvertises it; the router checks that when the turn runs and falls back\nif not, so the name here is the routed model rather than a guarantee.\n``None`` only when the catalog pins nothing for the cell."},+ "ConsolidatedFact": {+ "properties": {+ "content": {+ "type": "string",+ "title": "Content",+ "description": "Canonical statement of the consolidated fact."+ },+ "scope": {+ "type": "string",+ "title": "Scope",+ "description": "Memory scope namespace, e.g. 'real:global' or 'project:foo'.",+ "default": "real:global"+ },+ "confidence": {+ "type": "number",+ "maximum": 1.0,+ "minimum": 0.0,+ "title": "Confidence",+ "description": "Phase 1 model's confidence in the consolidated statement."+ },+ "source_episode_uuids": {+ "items": { "type": "string" },+ "type": "array",+ "title": "Source Episode Uuids",+ "description": "UUIDs of the :Episodic nodes the fact was consolidated from."+ }+ },+ "type": "object",+ "required": ["content", "confidence"],+ "title": "ConsolidatedFact",+ "description": "A cluster of related facts merged into a single canonical statement.\n\nPhase 1 output. Provenance always points back to the source episodes\nso apply.py can record where the consolidation came from."+ },+ "ConsolidationOutput": {+ "properties": {+ "facts": {+ "items": { "$ref": "#/components/schemas/ConsolidatedFact" },+ "type": "array",+ "title": "Facts"+ }+ },+ "type": "object",+ "title": "ConsolidationOutput"+ },"ContentType": {"type": "string","enum": [@@ -23087,6 +23186,81 @@"required": ["url", "relevance_score"],"title": "Document"},+ "DreamDemotion": {+ "properties": {+ "edge_uuid": {+ "type": "string",+ "title": "Edge Uuid",+ "description": "UUID of the :RELATES_TO edge to demote."+ },+ "reason": {+ "type": "string",+ "title": "Reason",+ "description": "Short reason recorded on the edge. Examples: 'stale_fact', 'contradicted_by:{uuid}', 'entity_invalidated:{uuid}', 'user_signal', 'unratified', 'web_contradicted:{url}'."+ },+ "new_status": {+ "type": "string",+ "enum": ["superseded", "contradicted"],+ "title": "New Status",+ "default": "superseded"+ }+ },+ "type": "object",+ "required": ["edge_uuid", "reason"],+ "title": "DreamDemotion",+ "description": "Demote an existing :RELATES_TO edge.\n\nSets ``expired_at`` and ``status``; ``invalid_at`` is reserved for\ncontradiction-detector world-changes per Snodgrass bi-temporal\nsemantics (see ``dream/p0-spec.md`` §4 and the audit at §6.13)."+ },+ "DreamInput": {+ "properties": {+ "user_id": { "type": "string", "title": "User Id" },+ "expert_id": {+ "anyOf": [{ "type": "string" }, { "type": "null" }],+ "title": "Expert Id"+ },+ "group_id": { "type": "string", "title": "Group Id" },+ "window_start": {+ "type": "string",+ "format": "date-time",+ "title": "Window Start"+ },+ "window_end": {+ "type": "string",+ "format": "date-time",+ "title": "Window End"+ },+ "episodes": {+ "items": { "$ref": "#/components/schemas/EpisodeRow" },+ "type": "array",+ "title": "Episodes"+ },+ "facts": {+ "items": { "$ref": "#/components/schemas/FactRow" },+ "type": "array",+ "title": "Facts"+ },+ "recent_sessions": {+ "items": { "$ref": "#/components/schemas/SessionRow" },+ "type": "array",+ "title": "Recent Sessions"+ },+ "known_fact_uuids": {+ "items": { "type": "string" },+ "type": "array",+ "uniqueItems": true,+ "title": "Known Fact Uuids"+ },+ "known_episode_uuids": {+ "items": { "type": "string" },+ "type": "array",+ "uniqueItems": true,+ "title": "Known Episode Uuids"+ }+ },+ "type": "object",+ "required": ["user_id", "group_id", "window_start", "window_end"],+ "title": "DreamInput",+ "description": "The whole gather-step bundle handed to the phase 1 prompt."+ },"DreamJobStatus": {"properties": {"job_id": { "type": "string", "title": "Job Id" },@@ -23150,6 +23324,41 @@"title": "DreamJobStatus","description": "JobStatus envelope for ``kind=\"dream_pass\"``."},+ "DreamOperations": {+ "properties": {+ "writes": {+ "items": { "$ref": "#/components/schemas/ConsolidatedFact" },+ "type": "array",+ "title": "Writes",+ "description": "Phase 1 consolidated facts that survived the sanitizer."+ },+ "proposals": {+ "items": { "$ref": "#/components/schemas/ProposedFinding" },+ "type": "array",+ "title": "Proposals",+ "description": "Phase 2 proposals that survived the sanitizer."+ },+ "demotions": {+ "items": { "$ref": "#/components/schemas/DreamDemotion" },+ "type": "array",+ "title": "Demotions"+ },+ "entity_invalidations": {+ "items": { "$ref": "#/components/schemas/EntityInvalidation" },+ "type": "array",+ "title": "Entity Invalidations"+ },+ "summary_for_user": {+ "type": "string",+ "title": "Summary For User",+ "description": "Short narrative for the dream-kind ChatSession body.",+ "default": ""+ }+ },+ "type": "object",+ "title": "DreamOperations",+ "description": "Phase 3 output — what apply.py writes back to the world.\n\nGuardrails enforced by phase 3's prompt and double-checked in\napply.py:\n * ≤ ``max_demotions_per_pass`` demotions per pass (runaway-demotion\n mitigation per spec §3 / TODO P0.3b).\n * Scope match enforced — proposals cannot cross scopes.\n * Empty ``writes`` and ``proposals`` is fine; a pass can be no-op."+ },"DreamOperationsSnapshot": {"properties": {"writes": {@@ -23179,6 +23388,247 @@"title": "DreamOperationsSnapshot","description": "Detailed per-operation rollup of a single dream pass.\n\nShape used by three downstream consumers:\n 1. The admin memory-visualizer UI (renders the per-edge detail).\n 2. AgentProbe eval scorers (read via ``rawExchangeKey``).\n 3. The future chat-stream ``dream.operations`` SSE event (P6 + P9\n daydreaming, see ``dream/TODO.md``).\n\nKept additive on ``DreamPassResult`` so adding fields here doesn't\nbump the count-only top-level columns existing clients rely on."},+ "DreamPassApplied": {+ "properties": {+ "consolidated_count": {+ "type": "integer",+ "title": "Consolidated Count",+ "default": 0+ },+ "proposal_count": {+ "type": "integer",+ "title": "Proposal Count",+ "default": 0+ },+ "demotion_count": {+ "type": "integer",+ "title": "Demotion Count",+ "default": 0+ },+ "entity_invalidation_count": {+ "type": "integer",+ "title": "Entity Invalidation Count",+ "default": 0+ },+ "dropped_forgotten": {+ "type": "integer",+ "title": "Dropped Forgotten",+ "default": 0+ },+ "pending_writes": {+ "type": "integer",+ "title": "Pending Writes",+ "default": 0+ },+ "failed_writes": {+ "type": "integer",+ "title": "Failed Writes",+ "default": 0+ },+ "summary_for_user": {+ "type": "string",+ "title": "Summary For User",+ "default": ""+ },+ "dream_session_id": {+ "anyOf": [{ "type": "string" }, { "type": "null" }],+ "title": "Dream Session Id"+ },+ "ingestion_drain_status": {+ "$ref": "#/components/schemas/IngestionDrainStatus",+ "default": "drained"+ },+ "snapshot": {+ "anyOf": [+ { "$ref": "#/components/schemas/DreamOperationsSnapshot" },+ { "type": "null" }+ ]+ }+ },+ "type": "object",+ "title": "DreamPassApplied",+ "description": "What apply reported: the counts and narrative a ``DreamPassResult``\ncarries, and the per-operation snapshot."+ },+ "DreamPassOperations": {+ "properties": {+ "planned": {+ "anyOf": [+ { "$ref": "#/components/schemas/DreamOperations" },+ { "type": "null" }+ ]+ },+ "applied": {+ "anyOf": [+ { "$ref": "#/components/schemas/DreamPassApplied" },+ { "type": "null" }+ ]+ }+ },+ "type": "object",+ "title": "DreamPassOperations",+ "description": "The clamped operations apply was handed, then what it reported."+ },+ "DreamPassPhase": {+ "type": "string",+ "enum": [+ "GATHER",+ "CONSOLIDATE",+ "RECOMBINE",+ "SANITIZE",+ "APPLY",+ "DONE"+ ],+ "title": "DreamPassPhase"+ },+ "DreamPassRecord": {+ "properties": {+ "id": { "type": "string", "title": "Id" },+ "user_id": { "type": "string", "title": "User Id" },+ "expert_id": {+ "anyOf": [{ "type": "string" }, { "type": "null" }],+ "title": "Expert Id"+ },+ "scope_key": { "type": "string", "title": "Scope Key" },+ "route": { "$ref": "#/components/schemas/DreamPassRoute" },+ "trigger": { "$ref": "#/components/schemas/DreamPassTrigger" },+ "phase": { "$ref": "#/components/schemas/DreamPassPhase" },+ "status": { "$ref": "#/components/schemas/DreamPassStatus" },+ "skip_reason": {+ "anyOf": [{ "type": "string" }, { "type": "null" }],+ "title": "Skip Reason"+ },+ "cancel_generation": {+ "type": "integer",+ "title": "Cancel Generation"+ },+ "provider_batch_id": {+ "anyOf": [{ "type": "string" }, { "type": "null" }],+ "title": "Provider Batch Id"+ },+ "lease_token": {+ "anyOf": [{ "type": "string" }, { "type": "null" }],+ "title": "Lease Token"+ },+ "lease_expires_at": {+ "anyOf": [+ { "type": "string", "format": "date-time" },+ { "type": "null" }+ ],+ "title": "Lease Expires At"+ },+ "input_bundle": {+ "anyOf": [+ { "$ref": "#/components/schemas/DreamInput" },+ { "type": "null" }+ ]+ },+ "phase_outputs": { "$ref": "#/components/schemas/DreamPhaseOutputs" },+ "operations": { "$ref": "#/components/schemas/DreamPassOperations" },+ "usage": {+ "anyOf": [+ { "$ref": "#/components/schemas/DreamPassUsage" },+ { "type": "null" }+ ]+ },+ "window_start": {+ "anyOf": [+ { "type": "string", "format": "date-time" },+ { "type": "null" }+ ],+ "title": "Window Start"+ },+ "window_end": {+ "anyOf": [+ { "type": "string", "format": "date-time" },+ { "type": "null" }+ ],+ "title": "Window End"+ },+ "error": {+ "anyOf": [{ "type": "string" }, { "type": "null" }],+ "title": "Error"+ },+ "created_at": {+ "type": "string",+ "format": "date-time",+ "title": "Created At"+ },+ "started_at": {+ "anyOf": [+ { "type": "string", "format": "date-time" },+ { "type": "null" }+ ],+ "title": "Started At"+ },+ "submitted_at": {+ "anyOf": [+ { "type": "string", "format": "date-time" },+ { "type": "null" }+ ],+ "title": "Submitted At"+ },+ "applied_at": {+ "anyOf": [+ { "type": "string", "format": "date-time" },+ { "type": "null" }+ ],+ "title": "Applied At"+ },+ "completed_at": {+ "anyOf": [+ { "type": "string", "format": "date-time" },+ { "type": "null" }+ ],+ "title": "Completed At"+ },+ "updated_at": {+ "type": "string",+ "format": "date-time",+ "title": "Updated At"+ }+ },+ "type": "object",+ "required": [+ "id",+ "user_id",+ "expert_id",+ "scope_key",+ "route",+ "trigger",+ "phase",+ "status",+ "skip_reason",+ "cancel_generation",+ "provider_batch_id",+ "lease_token",+ "lease_expires_at",+ "input_bundle",+ "phase_outputs",+ "operations",+ "usage",+ "window_start",+ "window_end",+ "error",+ "created_at",+ "started_at",+ "submitted_at",+ "applied_at",+ "completed_at",+ "updated_at"+ ],+ "title": "DreamPassRecord",+ "description": "One ``DreamPass`` row."+ },+ "DreamPassRecordResponse": {+ "properties": {+ "record": { "$ref": "#/components/schemas/DreamPassRecord" },+ "result": { "$ref": "#/components/schemas/DreamPassResult" }+ },+ "type": "object",+ "required": ["record", "result"],+ "title": "DreamPassRecordResponse",+ "description": "One dream pass's durable record, and the ``DreamPassResult`` read off\nit: what the eval driver scores, with usage for batch passes too."+ },"DreamPassResult": {"properties": {"user_id": { "type": "string", "title": "User Id" },@@ -23286,6 +23736,31 @@"title": "DreamPassResult","description": "Return value of ``execute_dream_pass`` and the admin API.\n\nMirrors ``RebuildResponse`` from the community-rebuild endpoint so\nthe admin frontend can render both with the same toast logic."},+ "DreamPassRoute": {+ "type": "string",+ "enum": ["SYNC", "ANTHROPIC_BATCH"],+ "title": "DreamPassRoute"+ },+ "DreamPassStatus": {+ "type": "string",+ "enum": [+ "QUEUED",+ "RUNNING",+ "SUBMITTED",+ "APPLYING",+ "COMPLETE",+ "ERRORED",+ "CANCELLED",+ "EXPIRED",+ "SKIPPED"+ ],+ "title": "DreamPassStatus"+ },+ "DreamPassTrigger": {+ "type": "string",+ "enum": ["CRON", "ADMIN", "EVAL"],+ "title": "DreamPassTrigger"+ },"DreamPassUsage": {"properties": {"phases": {@@ -23327,6 +23802,31 @@"title": "DreamPassUsage","description": "Aggregate usage across all phases of one dream pass.\n\nSurfaced on ``DreamPassResult.usage`` so eval / admin UI / billing\ncan read tokens + cost without recomputing. Written to\n``PlatformCostLog`` from apply.py with ``provider='dream_pass'``;\ncharged against the user's rate-limit window via\n``persist_and_record_usage`` so dream-pass spend rolls into the\nsame daily/weekly subscription tier budget as chat-execution\noverage."},+ "DreamPhaseOutputs": {+ "properties": {+ "consolidate": {+ "anyOf": [+ { "$ref": "#/components/schemas/ConsolidationOutput" },+ { "type": "null" }+ ]+ },+ "recombine": {+ "anyOf": [+ { "$ref": "#/components/schemas/RecombinationOutput" },+ { "type": "null" }+ ]+ },+ "sanitize": {+ "anyOf": [+ { "$ref": "#/components/schemas/DreamOperations" },+ { "type": "null" }+ ]+ }+ },+ "type": "object",+ "title": "DreamPhaseOutputs",+ "description": "Each phase's validated output, as the next phase and apply read it."+ },"DumpStatusResponse": {"properties": {"status": {@@ -23415,6 +23915,16 @@"title": "EnableShareRequest","description": "Whether to auto-link every ``run_agent`` execution in this chat.\n\nWhen true, the backend creates ``ChatLinkedShare`` rows for every\nexisting agent run AND auto-links any new runs that land while the\nshare is active. When false, only the chat messages are shared\n(cards in the viewer render in their \"execution not shared\" state)."},+ "EntityInvalidation": {+ "properties": {+ "entity_uuid": { "type": "string", "title": "Entity Uuid" },+ "reason": { "type": "string", "title": "Reason" }+ },+ "type": "object",+ "required": ["entity_uuid", "reason"],+ "title": "EntityInvalidation",+ "description": "Demote every :RELATES_TO edge directly attached to an entity.\n\nSingle-hop only — apply.py calls ``invalidate_entity_direct_neighbors``\nwhich clamps to ``[r:RELATES_TO]-(other)``; never expands to\nneighbors-of-neighbors."+ },"EntityInvalidationSummary": {"properties": {"entity_uuid": { "type": "string", "title": "Entity Uuid" },@@ -23456,6 +23966,41 @@"required": ["uuid", "name"],"title": "EntitySummary"},+ "EpisodeRow": {+ "properties": {+ "uuid": { "type": "string", "title": "Uuid" },+ "name": {+ "anyOf": [{ "type": "string" }, { "type": "null" }],+ "title": "Name"+ },+ "content": {+ "anyOf": [{ "type": "string" }, { "type": "null" }],+ "title": "Content"+ },+ "source_description": {+ "anyOf": [{ "type": "string" }, { "type": "null" }],+ "title": "Source Description"+ },+ "valid_at": {+ "anyOf": [{ "type": "string" }, { "type": "null" }],+ "title": "Valid At"+ },+ "created_at": {+ "anyOf": [{ "type": "string" }, { "type": "null" }],+ "title": "Created At"+ }+ },+ "type": "object",+ "required": [+ "uuid",+ "name",+ "content",+ "source_description",+ "valid_at",+ "created_at"+ ],+ "title": "EpisodeRow"+ },"EraseMemoryResponse": {"properties": {"expert_id": {@@ -24922,6 +25467,56 @@"required": ["user_id", "items"],"title": "FactListResponse"},+ "FactRow": {+ "properties": {+ "uuid": { "type": "string", "title": "Uuid" },+ "source": {+ "anyOf": [{ "type": "string" }, { "type": "null" }],+ "title": "Source"+ },+ "target": {+ "anyOf": [{ "type": "string" }, { "type": "null" }],+ "title": "Target"+ },+ "name": {+ "anyOf": [{ "type": "string" }, { "type": "null" }],+ "title": "Name"+ },+ "fact": {+ "anyOf": [{ "type": "string" }, { "type": "null" }],+ "title": "Fact"+ },+ "scope": {+ "anyOf": [{ "type": "string" }, { "type": "null" }],+ "title": "Scope"+ },+ "confidence": {+ "anyOf": [{ "type": "number" }, { "type": "null" }],+ "title": "Confidence"+ },+ "status": {+ "anyOf": [{ "type": "string" }, { "type": "null" }],+ "title": "Status"+ },+ "created_at": {+ "anyOf": [{ "type": "string" }, { "type": "null" }],+ "title": "Created At"+ }+ },+ "type": "object",+ "required": [+ "uuid",+ "source",+ "target",+ "name",+ "fact",+ "scope",+ "confidence",+ "status",+ "created_at"+ ],+ "title": "FactRow"+ },"FactSummary": {"properties": {"uuid": { "type": "string", "title": "Uuid" },@@ -28481,6 +29076,19 @@"title": "MemoryForgetFailureCode","description": "Stable, machine-switchable reason a forget delete failed.\n\nThe frontend/model can branch on this code (retry vs. give up) without\nparsing the free-text ``reason``. New codes may be added over time, so\nconsumers must tolerate unknown values."},+ "MemoryKind": {+ "type": "string",+ "enum": [+ "fact",+ "preference",+ "rule",+ "finding",+ "plan",+ "event",+ "procedure"+ ],+ "title": "MemoryKind"+ },"MemoryOverview": {"properties": {"user_id": { "type": "string", "title": "User Id" },@@ -28728,7 +29336,7 @@"nightly_id": {"type": "string","title": "Nightly Id",- "description": "UUID correlating every per-submitter cost-log row for this pass. The dream pass and ratification supersession sweep all share this id."+ "description": "UUID naming this fan-out in its logs and result. Cost rows carry each submitter's own id instead: the dream pass logs under dream.pass_id."},"started_at": {"type": "string",@@ -30512,6 +31120,52 @@"title": "ProfileDetails","description": "Marketplace user profile (including read-only fields)"},+ "ProposedFinding": {+ "properties": {+ "content": {+ "type": "string",+ "title": "Content",+ "description": "The proposed finding."+ },+ "scope": {+ "type": "string",+ "title": "Scope",+ "description": "Memory scope.",+ "default": "real:global"+ },+ "memory_kind": {+ "$ref": "#/components/schemas/MemoryKind",+ "description": "Envelope kind — finding | rule | preference | plan.",+ "default": "finding"+ },+ "confidence": {+ "type": "number",+ "maximum": 1.0,+ "minimum": 0.0,+ "title": "Confidence",+ "description": "Phase 2 model's self-rated confidence."+ },+ "rationale": {+ "type": "string",+ "title": "Rationale",+ "description": "Why the dream pass proposes this — recorded for audit."+ },+ "source_episode_uuids": {+ "items": { "type": "string" },+ "type": "array",+ "title": "Source Episode Uuids"+ },+ "source_fact_uuids": {+ "items": { "type": "string" },+ "type": "array",+ "title": "Source Fact Uuids"+ }+ },+ "type": "object",+ "required": ["content", "confidence", "rationale"],+ "title": "ProposedFinding",+ "description": "A novel connection or weak-link discovery from phase 2.\n\nAll proposals land as ``status=tentative`` in apply.py and ride the\nP-0.4 ratification loop. The ``rationale`` is recorded as part of\nthe episode body so a reviewer (or future audit) can see *why* the\ndream pass proposed the finding."+ },"Provider": {"properties": {"name": {@@ -31078,6 +31732,17 @@"title": "RecentExecution","description": "Summary of a recent execution for quality assessment.\n\nUsed by the LLM to understand the agent's recent performance with specific examples\nrather than just aggregate statistics."},+ "RecombinationOutput": {+ "properties": {+ "proposals": {+ "items": { "$ref": "#/components/schemas/ProposedFinding" },+ "type": "array",+ "title": "Proposals"+ }+ },+ "type": "object",+ "title": "RecombinationOutput"+ },"RecommendedExpert": {"properties": {"template_id": { "type": "string", "title": "Template Id" },@@ -32200,6 +32865,26 @@"title": "SessionDetailResponse","description": "Response model providing complete details for a chat session, including messages."},+ "SessionRow": {+ "properties": {+ "session_id": { "type": "string", "title": "Session Id" },+ "title": {+ "anyOf": [{ "type": "string" }, { "type": "null" }],+ "title": "Title"+ },+ "created_at": {+ "anyOf": [+ { "type": "string", "format": "date-time" },+ { "type": "null" }+ ],+ "title": "Created At"+ },+ "body": { "type": "string", "title": "Body" }+ },+ "type": "object",+ "required": ["session_id", "title", "created_at", "body"],+ "title": "SessionRow"+ },"SessionSummaryResponse": {"properties": {"id": { "type": "string", "title": "Id" },