Skip to content

Sessions: internal annotations and session config - #3061

Merged
mattzcarey merged 2 commits into
mainfrom
slim/sessions-annotations
Nov 24, 2026
Merged

mattzcarey merged 2 commits into
mainfrom
slim/sessions-annotations

Conversation

@mattzcarey

@mattzcarey mattzcarey commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Lands together with, or immediately under, context's #2773, which is the caller of these methods. It is held as a draft until then.

Sessions can now keep server-side data about a session's messages and the session itself, for Think's context records (#2773). It is internal only. Session gets five @internal methods: internal_annotations, internal_setAnnotation, internal_latestAnnotation, internal_config and internal_setConfig. Nothing is exported and there is no user docs page.

Context needs one small record per turn, such as the system prompt a turn was sent with. That record must stay out of the message JSON so a client cannot read or forge it, must follow the transcript tree, and must be found without walking the stored path:

  • annotations sit in cf_agents_session_annotations, one row per message and kind, created with the other Sessions tables
  • deleteMessages drops the deleted messages' annotations, and clearMessages drops all of them
  • storing an overlay (compact() or addCompaction()) copies, for each kind, the newest annotation it hides onto the overlay's message id, including one set directly on an overlay it replaces. The read runs before the overlay's write transaction and the copy inside it, so whoever reads the visible path never looks below an overlay
  • internal_latestAnnotation(kind, leafId) returns the newest annotation among the messages getHistory({ leafId }) returns, overlay messages included. It walks newest-first in batches, checks only the ids in each batch, and stops at the first hit: a record one row from the leaf costs a few rows read at 10,000 stored rows (a test pins under 50), and no id set is held
  • config uses the existing cf_agents_session_config table; clearMessages deletes keys starting with transcript:, and every other key outlives a clear

Cost, from perf's harness, base #3056 / head. Turns and connects are unchanged. Every start costs one more statement, the CREATE TABLE IF NOT EXISTS, which reads 0 rows. The other extra rows read come from five existing startup probes that scan all of sqlite_master (perf's finding 6), each now seeing one more schema row: think/startup-500 rows read 5,008 / 5,015, chat/startup-500 4,860 / 4,868, still about 1,000 below the release. A fresh agent writes 2 more rows, once, when it creates the table. A session that never annotates pays nothing on reads, and a compaction in such a session writes only its overlay row; tests pin both.

Once a session has any annotation, addCompaction and compact() also look for annotations to carry onto the new overlay. That walk goes row by row from the overlay's end down to where the history continues, through rows already under older overlays, and reads about 4 more rows per row it passes. Measured by the gate on a deployment, the second compaction of a 100,000-row session read 600,158 rows with an annotation present, against 200,211 without. With #2773, every Think session with a context block has an annotation. A follow-up PR makes the walk jump over an older overlay the way the read path does, taking the value that overlay already carried.

latestAnnotation reads 102 to 113 rows at 10,000 stored rows (rv-5, deployed). A cold start reads 6 more rows (the gate).

An annotation an overlay carried stays on the overlay after the message it came from is deleted. The overlay's summary was written with that message in it, so the record still describes what the summary covers. The follow-up states this in the doc comment.

Tests ran: src/tests/sessions/annotations.test.ts (new), the sessions suite, Think session and hydration, and AIChat persistence and clear.

@changeset-bot

changeset-bot Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: a34472b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
agents Patch
@cloudflare/agent-think Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

agents

npm i https://pkg.pr.new/agents@3061

@cloudflare/ai-chat

npm i https://pkg.pr.new/@cloudflare/ai-chat@3061

@cloudflare/codemode

npm i https://pkg.pr.new/@cloudflare/codemode@3061

hono-agents

npm i https://pkg.pr.new/hono-agents@3061

@cloudflare/shell

npm i https://pkg.pr.new/@cloudflare/shell@3061

@cloudflare/think

npm i https://pkg.pr.new/@cloudflare/think@3061

@cloudflare/voice

npm i https://pkg.pr.new/@cloudflare/voice@3061

@cloudflare/worker-bundler

npm i https://pkg.pr.new/@cloudflare/worker-bundler@3061

commit: a34472b

@agent-think

agent-think Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

🟡 agents import sizes: 1 entry point grew

Entry point Exports Largest gzip change Size now
🟡 agents/sessions 1 resized +918 B (+6.18%) 15.4 KiB
Changed exports (1)
Import Gzip change Size now
🟡 agents/sessions#Sessions +918 B (+6.18%) 15.4 KiB
How this works

Each runtime export is bundled on its own, minified, and gzipped. Changes smaller than 100 B, or smaller than 1% and 1 KiB, are ignored. Growth over 10% or 5 KiB is marked 🔴. This report is informational and does not fail CI. The workflow artifact contains every measurement.

Compared f2795529 → a34472be · workflow run · reported by agent-think[bot]

@mattzcarey
mattzcarey force-pushed the slim/sessions-annotations branch 3 times, most recently from 3290786 to a5bfe06 Compare November 23, 2026 19:46
Base automatically changed from slim/sessions-fix to main November 23, 2026 20:47
@mattzcarey
mattzcarey force-pushed the slim/sessions-annotations branch from a5bfe06 to f6c6de6 Compare November 23, 2026 20:58
@mattzcarey
mattzcarey marked this pull request as ready for review November 23, 2026 21:00
devin-ai-integration[bot]

This comment was marked as resolved.

@mattzcarey
mattzcarey force-pushed the slim/sessions-annotations branch from f6c6de6 to aa768d3 Compare November 23, 2026 22:27
@mattzcarey
mattzcarey changed the base branch from main to slim/sessions-read-model November 23, 2026 22:29
@mattzcarey
mattzcarey marked this pull request as draft November 23, 2026 22:30
devin-ai-integration[bot]

This comment was marked as resolved.

@mattzcarey
mattzcarey force-pushed the slim/sessions-read-model branch from 06e569f to 865f313 Compare November 23, 2026 22:42
@mattzcarey
mattzcarey force-pushed the slim/sessions-annotations branch from aa768d3 to 5da3de1 Compare November 23, 2026 22:46
@mattzcarey
mattzcarey changed the base branch from slim/sessions-read-model to slim/sessions-tail November 23, 2026 22:46
@mattzcarey
mattzcarey force-pushed the slim/sessions-annotations branch from 635dfb9 to fdc4d39 Compare November 23, 2026 23:47
@mattzcarey
mattzcarey force-pushed the slim/sessions-annotations branch from fdc4d39 to fca6adb Compare November 24, 2026 00:06
@mattzcarey
mattzcarey force-pushed the slim/sessions-annotations branch from fca6adb to 1000b2b Compare November 24, 2026 00:39
@mattzcarey
mattzcarey force-pushed the slim/sessions-annotations branch from 1000b2b to 926d6f6 Compare November 24, 2026 02:26
@mattzcarey
mattzcarey force-pushed the slim/sessions-annotations branch from 926d6f6 to d2abdcd Compare November 24, 2026 02:50
@mattzcarey
mattzcarey force-pushed the slim/sessions-annotations branch from d2abdcd to 43b8357 Compare November 24, 2026 03:47
@mattzcarey
mattzcarey force-pushed the slim/sessions-annotations branch from 43b8357 to 63cd4e6 Compare November 24, 2026 03:56
@mattzcarey
mattzcarey force-pushed the slim/sessions-annotations branch from 63cd4e6 to 8aa64c5 Compare November 24, 2026 04:13
@mattzcarey
mattzcarey force-pushed the slim/sessions-annotations branch from 8aa64c5 to 3a8e1c0 Compare November 24, 2026 05:23
@mattzcarey
mattzcarey force-pushed the slim/sessions-annotations branch from 3a8e1c0 to f41c819 Compare November 24, 2026 05:52
@mattzcarey
mattzcarey force-pushed the slim/sessions-annotations branch from f41c819 to 95e1b7f Compare November 24, 2026 06:06
@mattzcarey
mattzcarey force-pushed the slim/sessions-annotations branch from 95e1b7f to 92a8972 Compare November 24, 2026 06:20
@mattzcarey
mattzcarey force-pushed the slim/sessions-annotations branch from 92a8972 to aab6f4b Compare November 24, 2026 06:34
Base automatically changed from slim/sessions-tail to main November 24, 2026 06:59
@mattzcarey
mattzcarey force-pushed the slim/sessions-annotations branch 2 times, most recently from b5944be to ce27cd5 Compare November 24, 2026 10:23
@mattzcarey
mattzcarey marked this pull request as ready for review November 24, 2026 10:23
devin-ai-integration[bot]

This comment was marked as resolved.

@mattzcarey
mattzcarey force-pushed the slim/sessions-annotations branch from ce27cd5 to d4ed309 Compare November 24, 2026 10:46
@mattzcarey
mattzcarey force-pushed the slim/sessions-annotations branch from d4ed309 to a34472b Compare November 24, 2026 10:56
@mattzcarey
mattzcarey merged commit 4978fb1 into main Nov 24, 2026
26 checks passed
@mattzcarey
mattzcarey deleted the slim/sessions-annotations branch November 24, 2026 11:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant