Skip to content

[P0] Route free-form financial plans to AI and preserve recent chat context #121

Closed

Description

@alex-mextner

What

Prevent the deterministic expense parser from consuming multi-line free-form requests merely because later lines look like expenses. Only canonical expense-entry messages should enter the direct parser; mixed/natural-language requests must go to the AI agent. Persist recent relevant Telegram messages into shared chat history regardless of whether they were handled deterministically, so follow-ups such as 'Все суммы в евро' resolve against the immediately preceding plan.

Why (motivation)

Production on 2026-09-24 recorded 9 budget-plan lines as real expenses and then the agent lacked the preceding request when handling the follow-up.

User impact

Users can safely discuss or plan budgets in natural language without accidental writes, and follow-up messages keep conversational meaning.

Cost of inaction

The bot can corrupt expense data and make contradictory AI changes because it loses the immediate conversation context.

Acceptance criteria

  • A message beginning with natural-language instructions followed by amount/category lines is not saved as expenses by the deterministic parser and is routed to AI intact. — forced: Backend routing/history acceptance is non-visual; verified by full pre-push suite (157/157 files, 3652/3652 tests), exact 2026-09-24 and 2026-09-30 budget regressions in message.handler.test.ts, and causal/concurrent history regressions in ask.history.test.ts.
  • Recent relevant user messages handled outside the AI path are available to the next AI turn without duplicate history entries. — forced: Backend routing/history acceptance is non-visual; verified by full pre-push suite (157/157 files, 3652/3652 tests), exact 2026-09-24 and 2026-09-30 budget regressions in message.handler.test.ts, and causal/concurrent history regressions in ask.history.test.ts.
  • Regression tests cover the exact 2026-09-24 budget-plan conversation and contextual follow-up. — forced: Backend routing/history acceptance is non-visual; verified by full pre-push suite (157/157 files, 3652/3652 tests), exact 2026-09-24 and 2026-09-30 budget regressions in message.handler.test.ts, and causal/concurrent history regressions in ask.history.test.ts.

Screenshots

(none)

Deploy

Deployed: no

Links

Skipped gates

  • links: Root cause and exact production evidence will be linked in implementation PR.
  • msgref-quote: Original request is captured in production log and this working session; no stable external message URL is available.
  • screenshots: Routing/history behavior is covered by deterministic tests and production logs; no visual proof is needed for this backend P0.

Activity

  1. alex-mextner commented on Sep 30, 2026

    @alex-mextner
    OwnerAuthor

    2026-09-30 production recurrence confirmed. User message 4239: 'Сформируй бюджет на грядущий октябрь' followed by €450 — квартира, €600 — еда, etc. Current production f0619bb ignored prose lines and created pending_expenses 1209–1218; each parsed category as literal em dash '—' and the intended category as comment. No final expenses were committed. Follow-up 'Бюджет надо а не траты / См сообщение выше' reached AI without the original request in history. Added exact regression for this message to PR #125 and rebased onto current main where old dependency-review vulnerabilities are already fixed.

  2. alex-mextner commented on Sep 30, 2026

    @alex-mextner
    OwnerAuthor

    Review findings addressed before merge: (1) bounded chat history now prunes immediately on every stored user turn; (2) direct-AI current turn is carried by exact DB row id and causal history is queried with id < current, eliminating same-second/concurrency duplication and later-message leakage; (3) ordinary messages in forum groups without an active /topic binding are not added to AI history, preventing cross-topic disclosure. Added regression tests for unconfigured forums, immediate pruning/id handoff, deterministic id ordering, and causal history.

  3. alex-mextner commented on Sep 30, 2026

    @alex-mextner
    OwnerAuthor

    Second review edge addressed: forum history remains stateless until /topic is bound; changing or clearing active_topic_id deletes prior group-scoped chat history, so context cannot cross old/new topic boundaries. Re-running /topic in the same thread preserves history. Added /topic regression tests.

  4. 2 remaining items

  5. alex-mextner commented on Oct 1, 2026

    @alex-mextner
    OwnerAuthor

    Final queue review fix: optional maybeSmartAdvice now runs after the ordered primary conversation queue has been released, so a slow post-answer advice stream cannot block the next user turn. Added a regression that holds the first advice stream open and verifies the second primary AI turn still starts.

  6. alex-mextner commented on Oct 1, 2026

    @alex-mextner
    OwnerAuthor

    Shipped: merged via #125.
    Merge commit: 5645969
    This does NOT close the ticket — acceptance still needs proof. Run task read #121 for the outstanding criteria, then task check/task done.

  7. alex-mextner commented on Oct 1, 2026

    @alex-mextner
    OwnerAuthor

    ✅ Deployed to production as merge commit 5645969 (PR #125) on 2026-10-01. Deploy workflow 36859415031 completed successfully: production pulled main, rebuilt the Mini App, reloaded PM2, and both expensesyncbot and bank-sync came online. The exact Sep/Oct budget-planning regressions, causal recent-history ordering, forum topic scoping, retention bounds, and conversation-queue regression are covered by tests; final pre-push suite passed 157/157 test files and 3652/3652 tests.

  8. alex-mextner commented on Nov 6, 2026

    @alex-mextner
    OwnerAuthor

    Shipped: merged via #241.
    Merge commit: f4d3530
    The ticket is already Done — recorded here for reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions