Closes#2909.
Omit the fewest eligible historical tool-argument payloads so oversized completed exchanges remain replayable, without touching signed/native provider rounds or durable history.
Long mastery sessions froze the event loop: every existence-only probe
(history_groups, context-builder guards) ran normalize_model_turn, which
deep-copies the whole request record via a JSON round-trip. A 121-message
session re-serialized megabytes per probe, holding the GIL long enough for
health checks to time out and the worker to be declared lost.
Extract the structural acceptance checks into _is_complete_model_turn and
add has_model_turn for existence-only sites. normalize_model_turn delegates
to the same predicate, then cleans and copies as before, so replay keeps its
full isolation. Stored records are normalize's own JSON-safe outputs, so the
predicate accepts exactly what normalize accepts on stored rows; a parity
corpus pins the shared rejections.
Fixes#2788
Co-authored-by: ZQR1101 <278034003+ZQR1101@users.noreply.github.com>
* fix: keep sent runtime snapshots in mastery replay
Mastery replay dropped every runtime snapshot from history and the loop
re-appended them after it, so each follow-up request diverged from the
previous one at message 1 and only tools + system hit the prompt cache.
- project_mastery_replay(keep_snapshots=True) for the tutor request and
its token accounting; memory material still drops snapshots
- replay_group no longer blanks a tagged mastery_status_snapshot while
removing the legacy fused suffix
- status snapshot anchors are numbered from the start (occurrence), so a
later snapshot never changes how an earlier turn is projected
Refs #2922
* fix: keep runtime snapshots out of the mastery history budget
Replayed snapshots stay in the request so follow-up turns extend the cached prefix, but counting them made a large mastery_status snapshot push the history over its 20k budget after two turns; the next turn then paid a memory rewrite and a full prefix miss. The budget now measures the learning history only, as before. Refs #2922
* fix: bound the runtime snapshots kept in mastery history
Keeping sent snapshots out of the history budget entirely let many short
exchanges accumulate them without limit (80 quiz-style turns: 5.4k counted,
62k sent). Dialogue still keeps to the plan; dialogue plus snapshots keep to
twice the plan, and only as far as the model's window allows. Refs #2922
* fix: keep only this turn's state in the mastery status snapshot
Every mastery turn sends a fresh mastery_status snapshot and, since the
replay change, keeps earlier ones in history, so whatever a snapshot
carries is paid for once per turn and then kept. A full status read
repeats fixed guidance (open question, question group, working brief,
intake, teaching guidance and focus) and every default value of every
knowledge point.
The snapshot now drops empty and default values and the fixed guidance.
That guidance lives in named constants and is stated once in a new
mastery_status_guide system block, which also explains the omitted
defaults. Snapshots start with a one-line header; history sharing reads
both the header and the longer notice older sessions stored. An explicit
mastery_status call still returns the full payload.
Measured over 54 recorded snapshots: working briefs 1,454 -> 645 tokens
(median), full outline snapshots 6,352 -> 2,954. The guide adds 861
tokens (en) to the cached system prompt.
Refs #2922
* fix: size the exact tail the way the plan does, and share full reads with compact snapshots
Two gaps left by keeping snapshots in mastery replay (review on #2992):
- Recent-history selection counted kept snapshots in full while the plan
discounted them, so a session of short exchanges with large snapshots
archived most of them as soon as selection ran. ContextBuilder now sizes
the candidate tail through _recent_history_tokens(), and the mastery
builder answers with the same _plan_tokens() measure.
- Snapshots carry the compact form of map, learner_profile, learning_stages
and visual_requirements, so a later full mastery_status result never matched
them exactly. Sharing now also compares a full field's compact form and
marks such references with "defaults_omitted": true, which the state
reference notice explains.
Also: rename _next to _compact_next_step, and say in the status guide that
the omitted-default rules cover only what the snapshot includes.
Refs #2922
The summary was prepended inside the replayed history, so every
compaction rewrite changed bytes at the head of the request and threw
away the whole cached prefix behind it. Measured on a six-turn session
on a custom OpenAI-compatible route: the turn that rewrote the summary
dropped the shared prefix to one message and ran 21,629 prompt tokens
for 5,888 cached (27.2%).
Deliver it where the runtime-snapshot machinery already puts per-turn
context: as a trailing role=user message tagged _context_snapshot. A
rewrite is then a pure append, and an unchanged revision is a no-op.
replay_history no longer injects the summary, which is what kept it
mid-request whatever position it was given.
Two smaller fixes ride along, both in the same summary path:
* The summariser's scratchpad leaked into compressed_summary when the
route emits reasoning inline. The stored summary was 6,933 characters,
half of them the model's drafting monologue under an unclosed think
tag, and the real summary was truncated by it. clean_thinking_tags
cannot be used here - it deletes through end of text for an unclosed
block - so the tag is honoured only when it is closed, and an unclosed
one is cut at the first heading.
* _model_tokens counts the summary the way the request carries it.
The two regression tests that pinned the summary inside model_history
now assert the new delivery: the history starts at the oldest retained
turn and the summary still reaches the model.
The rolling summary is replayed as a system row on every later turn, so the
language it happens to be written in keeps steering the answer long after the
turns it condensed have scrolled out. The summarizer meta-prompt never stated
a language, so a session held in a language other than the prompt's own came
back to en/zh at the first compaction — #1511's "drifts after a few rounds".
Both the English and Chinese meta-prompt now say which language the summary
body itself must use, via the same language_label() the reply directive uses.
Ship folder-based workspaces and data migration, daily practice, redesigned
provider and model settings, and streaming conversation improvements.
Synchronize README translations and document the archive/permanent-delete
upgrade behavior in assets/releases/ver1-6-9.md.
Reduce initial JavaScript by deduplicating English resources and loading
animation features after hydration. Keep the existing route size budgets.
Update regression fixtures and browser audits for scoped URLs and the new
settings and learning interfaces.
Resolved ask_user exchanges happened before the assistant produced the
row's answer, so they must precede the row's content in both
_build_history() and format_messages_as_transcript(). The previous order
(assistant content first, clarification second) made later turns see the
answer before the user supplied the missing context, confusing the model
and polluting rolling summaries.
Update the test that had locked in the wrong order.
This PR fixes two related issues:
1. Qwen vLLM 'System message must be at the beginning' error:
- deeptutor/agents/chat/agentic_pipeline.py: Merge system_prompt and memory_context into single system message, filter out system messages from conversation_history
- deeptutor/services/session/context_builder.py: Filter out system messages from DB since summary is already added as system
- deeptutor/agents/chat/chat_agent.py: Merge system prompt and RAG context into single system message
2. History reference (@reference) empty context issue:
- deeptutor/services/session/turn_runtime.py: Add fallback to raw record content when NotebookAnalysisAgent.analyze() returns empty string
Benefits:
- Fixes Qwen vLLM compatibility issue completely
- Enables history reference feature to work correctly
- Reduces token usage by eliminating duplicate system messages
- Does not break any functionality - system context from DB is already in compressed_summary
- Cleaner message structure with exactly one system message at position 0
Testing:
- Verified with Qwen model via vLLM - no more template errors
- Chat with memory files (PROFILE.md/SUMMARY.md) works correctly
- Chat with conversation history works correctly
- History reference (@reference) now works correctly
- Conversation continuation works without errors
- No regression with other models (GPT-4, etc.)
Closes#294