Skip to content

fix(jdbc): widen agentscope_store.item_key on PostgreSQL and H2 - #3464

Merged
xzxiaoshan merged 2 commits into
agentscope-ai:mainfrom
wahllllll:fix/jdbc-store-item-key-length
Nov 9, 2026
Merged

xzxiaoshan merged 2 commits into
agentscope-ai:mainfrom
wahllllll:fix/jdbc-store-item-key-length

Conversation

@wahllllll

Copy link
Copy Markdown
Contributor

Symptom

On AgentScope Service 2.1.0-BETA1 (Docker Compose, PostgreSQL) the first Turn of every freshly created Managed Agent fails before the model is invoked:

IllegalStateException: JdbcStore putIfVersion (insert) failed
  Caused by: value too long for type character varying(255)

Reported in #3461, reproduced there on two independently created agents, so it is systemic — it blocks the documented quickstart on a clean deployment.

Root cause

agentscope_store.item_key is VARCHAR(255) in every dialect's DDL (PostgresDialect.java:61, H2Dialect.java:47, MysqlDialect.java:79; SQLite uses TEXT). But the keys written through JdbcStore are not short ids — they are workspace file paths (RemoteFilesystem.write(…, filePath, …) → store.putIfVersion(ns, filePath, …)) and session journal paths (StoreSessionStorage.compareAndSet(path, …)), i.e. agents/<name>/users/<user>/sessions/<id>.log.jsonl with generated ids in them. Those regularly exceed 255 characters.

namespace_path next to it is already VARCHAR(2048) on PostgreSQL/H2, so the asymmetry was never a deliberate size budget on those vendors.

Fix

Widen item_key to VARCHAR(2048) — matching namespace_path — on PostgreSQL and H2 only.

MySQL deliberately stays at VARCHAR(255). Its dialect records why:

// Keep composite PK under InnoDB utf8mb4 3072-byte limit:
// (512 + 255) * 4 = 3068 bytes. idx_namespace (512 * 4 = 2048 bytes) also fits.

Widening it there would break table creation (#1765 was that failure). MySQL is not the Service dataplane's database (application.yml is PostgreSQL-only), so the reported deployment is covered without touching the MySQL budget. The existing DialectSqlTests assertion on the MySQL DDL stays green untouched.

Existing deployments

Startup validation (TableSchemaValidator) compares column names only, never types or lengths, and the framework never alters an existing table. So a PostgreSQL table created before this change keeps its 255-character column silently. The docs page for the module already has a "the framework never alters existing tables — run this once by hand" convention; I added the matching note under §2 JdbcStore in both en and zh:

ALTER TABLE agentscope_store ALTER COLUMN item_key TYPE VARCHAR(2048);

(For the Service compose stack that is dp.agentscope_store; currentSchema=dp on the JDBC URL resolves the unqualified name.)

Tests

New JdbcStoreLongKeyH2Test (H2 enforces VARCHAR length, so no Docker needed): a 300-character key through putIfVersion and a 600-character key through put, both built as a realistic session journal path and read back by key.

Red — on current main both fail with exactly the reported shape:

JdbcStoreLongKeyH2Test.putIfVersionAcceptsLongKey
  java.lang.IllegalStateException: JdbcStore putIfVersion (insert) failed
  Caused by: org.h2.jdbc.JdbcSQLDataException: Value too long for column "ITEM_KEY CHARACTER VARYING(255)"
Tests run: 2, Failures: 0, Errors: 2

Green — with the fix, the new class and every non-Docker dialect / store / state / skill test in the module pass: 177 tests across 16 classes, 0 failures. spotless:apply applied.

No database migration code and no configuration change; the manual ALTER for pre-existing PostgreSQL tables is documented rather than automated, consistent with the module's stated policy.

Fixes #3461

The keys written through JdbcStore are workspace file paths and session
journal paths -- agents/<name>/users/<user>/sessions/<id>.log.jsonl and the
like -- whose ids regularly push them past 255 characters. The store DDL
declared item_key as VARCHAR(255) on every vendor, so the very first write of
a fresh session was rejected with "value too long for type character
varying(255)", surfacing as "JdbcStore putIfVersion (insert) failed" before
the model was ever called. On AgentScope Service 2.1.0-BETA1 this fails the
first Turn of every freshly created Managed Agent on a clean deployment.

Widen the column to VARCHAR(2048), matching namespace_path, on PostgreSQL and
H2. SQLite already uses TEXT. MySQL deliberately keeps VARCHAR(255): next to
namespace_path VARCHAR(512) its utf8mb4 composite primary key already sits at
InnoDB's 3072-byte limit, as the dialect's own comment records.

Startup validation compares column names only, so a PostgreSQL table created
before this change keeps its 255-character column silently. Document the
one-off ALTER for existing deployments next to the component.

Fixes agentscope-ai#3461
@codecov

codecov Bot commented Oct 9, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@jacksonw111

Copy link
Copy Markdown

Independent verification on a clean deployment — the patch is correct, and I suggest merging it.

What I ran

  1. Applied this patch to main @ abf3c35.
  2. Built agentscope-service/service-dataplane.
  3. Started the 2.1.0-BETA1 Compose bundle against a fresh PostgreSQL 17 volume (no reused schema).
  4. Created a Local environment, a managed Agent, a Session, and submitted the first Turn.

Results

Check Before the patch With the patch
dp.agentscope_store.item_key on a fresh database character varying(255) character varying(2048)
First Turn of a new managed Session failed, IllegalStateException: JdbcStore putIfVersion (insert) failed, PostgreSQL log: value too long for type character varying(255) completed, no error
Longest key written in the run n/a (write rejected) 256 characters

The failing key confirms the shape described in the PR body:

agents/s_<base64>/sessions/s_<base64>/s_<base64>/inbox/commits/<sha256>.json

256 characters is 1 over the old limit, and it was written by the very first Turn of a session created seconds earlier — so this hits the documented quickstart on a clean install, not only long-lived deployments.

Two notes for the reviewer

  1. Existing deployments keep VARCHAR(255) even after this patch, because CREATE TABLE IF NOT EXISTS cannot widen a column. The manual ALTER TABLE in the docs is correct. I opened fix(jdbc): warn when an existing store column is narrower than the DDL #3920 to add one start-up warning for that case; it is complementary and either PR can land first.
  2. Scope check: agentscope-extensions-postgresql and agentscope-extensions-mysql are marked deprecated and replaced by agentscope-extensions-jdbc in docs/v2/en/integration/distributed/jdbc.md, so leaving their DDL at 255 is right. Keeping MySQL at 255 is also right for the InnoDB 3072-byte primary-key limit.

@lianYeah

lianYeah commented Nov 3, 2026

Copy link
Copy Markdown

Independent reproduction on 2.1.0-BETA1 (official Compose bundle, PostgreSQL 17), with two data points that may help this PR land.

The overflow is exactly one character

The failing key is 256 characters against item_key VARCHAR(255) — measured directly from the database, not inferred:

agents/s_<base64url(agentId)>/sessions/s_<base64url(userId)>/<base64url(sessionId)>/inbox/commits/<64-hex>.json
└───────────── 172 ─────────────┘└ 6 ┘└── 9 ──┘└──── 64 ────┘└ 5 ┘  = 256
Segment Cumulative
storagePath() (SessionKey.java:31) 172
+ /inbox (JournalSessionLog.inbox()) 178
+ /commits/ 187
+ <64-char SHA-256 hex> (JournalSessionLog:412) 251
+ .json 256

So the failure boundary is storagePath() <= 171. Two other shapes are worth guarding:

  • root-level writer-seals/<64hex>.json = 255 — passes with zero margin
  • inbox-level writer-seals/<64hex>.json = 261 — also overflows, and this one is reachable from SessionNativeLogService.java:125 (log.sealWriter(admission.runId))

How it was measured

PostgreSQL log with log_parameter_max_length_on_error = -1:

ERROR:  value too long for type character varying(255)
DETAIL:  Parameters: $1 = '...', $2 = 'agents/s_.../sessions/s_.../s_.../inbox/commits/321cf6ad....json', ...
-- SQLSTATE 22001, measured $2 length = 256

Falsification, not just correlation

ALTER TABLE dp.agentscope_store ALTER COLUMN item_key TYPE VARCHAR(1024); makes the 22001 disappear and the Turn proceeds to the next real problem (NullPointerException: Cannot invoke "io.agentscope.core.model.Model.getModelName()" because "this.this$0.model" is null, i.e. the model is unconfigured). That confirms key length was the sole blocker at that point. Separately, two genuine 23505 duplicate-key errors on the same path were correctly swallowed, confirming isDuplicateKey handles 23* and only 22001 falls through.

Supporting the PostgreSQL/H2-only scope

Agreed on excluding MySQL: MysqlDialect uses namespace_path VARCHAR(512) + item_key VARCHAR(255), and DialectSqlTests.mysqlStorePkFitsInnoDbUtf8mb4Limit pins (512 + 255) * 4 = 3068 against the InnoDB 3072-byte limit. Widening item_key there would need a shorter namespace_path or a prefix index.

Two things worth adding to this PR

  1. The writer-seals/ variants overflow too (261 chars). A regression test bounding the maximum journal key length — covering inbox(), commits/ and writer-seals/ — would catch both, not just the commits/ path.
  2. CREATE TABLE IF NOT EXISTS cannot widen an existing column, so deployments created before this PR keep VARCHAR(255) even after upgrading, and the failure still surfaces as an information-free IllegalStateException at first write. A migration note in the docs would close that operator-facing gap. We filed [Bug]: putIfVersion discards the underlying SQLState and message, so JDBC store failures are undiagnosable #3948 for the message itself.

Reproduced independently of #3461 — +1 for merging.

…y column

The widening covered the inbox/commits/ key that failed on the quickstart, but the
same session path also writes writer-seals/ at the root and below inbox/. The
inbox-level one is 261 characters, six over the old limit and reachable from
SessionNativeLogService.sealWriter, and nothing pinned either of them.

The new test builds the keys from the real SessionKey, writes all three shapes
through JdbcStore on H2, and asserts that the measured failure case stays over the
old 255-character width while fitting the widened one, together with the
maximum-length id case. Narrowing the column back to 255 makes it fail.
@mintlify

mintlify Bot commented Nov 3, 2026

Copy link
Copy Markdown

Preview approval required

This pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit 613bfa2.

Approve preview

@wahllllll

Copy link
Copy Markdown
Contributor Author

Pushed a commit that covers the first suggestion — the other key shapes are now bounded too.

JdbcStoreJournalKeyLengthTest builds the keys from the real SessionKey and writes all three shapes through JdbcStore on H2:

  • writer-seals/<64hex>.json at the root (255 — zero margin under the old width)
  • inbox/commits/<64hex>.json (256)
  • inbox/writer-seals/<64hex>.json (261)

It also asserts that the measured failure case stays over the old 255-character width and fits the widened one, and that ids at their maximum length still fit. Narrowing the column back to 255 makes it fail, so the guard is not vacuous.

Good catch on the writer-seals/ path: the column widening already covers 261, but nothing pinned it, and SessionNativeLogService.sealWriter reaches it on the inbox log, so it would have been the next report of the same defect with a different shape.

On the second point, the migration note is in docs/v2/{en,zh}/integration/distributed/jdbc.md (the manual ALTER TABLE, since CREATE TABLE IF NOT EXISTS cannot widen an existing column). The start-up warning you would have added is a separate improvement and #3948 covers the diagnostics side of the same failure, so I left both out of this PR rather than growing it.

Thanks for measuring the boundary at both ends — "under 255 by zero" and "over by six" is a much sharper statement than the single failing key, and it is what the test is built around.

@wahllllll

Copy link
Copy Markdown
Contributor Author

@jujn — #3464 is 29 days old and has two independent reproductions that both recommend merge, which is unusual enough that I would rather you see them than take my word:

  • @jacksonw111 reproduced the failure from a clean environment, confirmed the before/after, and withdrew his own competing fix(jdbc): warn when an existing store column is narrower than the DDL #3920 with "it is the right place for that change";
  • @lianYeah reproduced it on PostgreSQL 17, falsified the diagnosis by applying the ALTER TABLE by hand, and mapped the exact boundaries: 256 characters for inbox/commits/, 261 for inbox/writer-seals/, and 255 with zero margin for the root seals.

One thing I want your call on rather than mine: @zachMelody's #3583 answers the same issue #3461 by projecting turns into the session log instead of widening the column. If that is the direction you prefer, I would rather withdraw #3464 than have two conflicting answers land. If widening is useful regardless — it also protects databases that already hold short keys from the next long one — it is one line per dialect plus a migration note in the docs.

#3559 is the same class of defect in a different table, if you would rather look at them together.

@jujn

jujn commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator

cc @xzxiaoshan

@xzxiaoshan
xzxiaoshan merged commit 7d84cff into agentscope-ai:main Nov 9, 2026
7 checks passed
chickenlj pushed a commit that referenced this pull request Nov 13, 2026
Backport the verified fix to the 2.0.x implementation and retain its
existing public contracts where the upstream kernel differs.

(cherry picked from commit 7d84cff)
Constraint: No AgentScope Service or SDK migration
Confidence: high
Not-tested: Whole-repository verification pending after lane integration
linlihuiyang added a commit to linlihuiyang/agentscope-java that referenced this pull request Nov 26, 2026
…tscope-ai#3464)

New PostgreSQL and H2 tables created by the unified
io.agentscope.extensions.jdbc.store.JdbcStore now use
item_key VARCHAR(2048), matching namespace_path. Workspace and session
journal paths regularly exceed 255 characters, which made the first
write of a fresh managed session fail with
"value too long for type character varying(255)".

MySQL keeps 255 characters so the composite primary key stays within
the InnoDB utf8mb4 index limit; the legacy
io.agentscope.extensions.mysql.store.JdbcStore is unchanged.

Existing tables are never altered automatically; the migration steps are
documented in the EN and ZH session storage pages.

(cherry picked from commit 440ef85)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P1 紧急不重要

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: dp.agentscope_store.item_key varchar(255) too short — every Managed Agent's first Turn fails before model invocation

5 participants