Skip to content

fix(core): pass builder permission context when loading legacy v1 session state - #2886

Closed
chang6666 wants to merge 4 commits into
agentscope-ai:mainfrom
chang6666:fix/legacy-session-permission-context
Closed

chang6666 wants to merge 4 commits into
agentscope-ai:mainfrom
chang6666:fix/legacy-session-permission-context

Conversation

@chang6666

@chang6666 chang6666 commented Aug 28, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2768 / re-lands #2769

Commit 6059782 (#2760) rewrote loadOrCreateAgentStateForSlot and dropped the permCtx argument, calling the 3-arg LegacyStateLoader.loadFromLegacySessionWithPresence overload. Legacy v1 keys (memory_messages / toolkit_activeGroups) never carry a permission context, so the first turn after a 1.x -> 2.0 migration silently downgraded the caller-configured permission mode to DEFAULT, re-introducing the bug fixed by #2769.

Changes

  • loadOrCreateAgentStateForSlot passes the builder-time permission context to the 4-arg LegacyStateLoader overload (original fix(core): preserve permission context when loading legacy v1 session state #2769 re-land).
  • LegacyStateLoader now also preserves the slot identity on the reconstructed state: sessionId and userId were previously left unset, so AgentState auto-generated a random sessionId that got persisted into the migrated agent_state.
  • The 3-arg overloads on LegacyStateLoader (no permission context) are deprecated so future callers cannot silently fall back to DEFAULT again.
  • End-to-end regression test (ReActAgentLegacyPermissionContextTest) covering: builder BYPASS surviving legacy load, migrated session id stability, and an existing v2 state staying authoritative over the builder template.

Verification

  • mvn -pl agentscope-core test -Dtest=ReActAgentLegacyPermissionContextTest,LegacyStateLoaderTest (JDK 17): 8/8 pass
  • spotless:check and git diff --check clean

…sion state

Commit 6059782 (agentscope-ai#2760) rewrote loadOrCreateAgentStateForSlot and
dropped the permCtx argument, calling the 3-arg
LegacyStateLoader.loadFromLegacySessionWithPresence overload. Legacy v1
keys (memory_messages / toolkit_activeGroups) never carry a permission
context, so the first turn after a 1.x -> 2.0 migration silently
downgraded the caller-configured permission mode to DEFAULT,
re-introducing the bug fixed by agentscope-ai#2769.

Pass the builder-time permission context to the 4-arg overload and add
an end-to-end regression test (ReActAgentLegacyPermissionContextTest)
that fails without the fix: expected BYPASS but was DEFAULT.

Re-lands agentscope-ai#2769 (fixes agentscope-ai#2768).
@codecov

codecov Bot commented Aug 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@guslegend0510 guslegend0510 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@chang6666 chang6666 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@chang6666 chang6666 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@oss-maintainer oss-maintainer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

Correct, well-scoped fix for the v1 → v2 legacy migration path, and the blast radius is bigger than the diff suggests. Verified against main:

  • ReActAgent.loadOrCreateAgentStateForSlot called the permission-context-less loadFromLegacySessionWithPresence(stateStore, userId, sessionId) overload, so a builder-configured permissionContext (e.g. BYPASS) was silently replaced by AgentState's default on any session that still lived under v1 keys — a permission-engine behaviour difference between "fresh session", "v2 session" and "migrated v1 session". Forwarding permCtx closes that.
  • Setting sessionId / userId on the reconstructed state matters for persistence, not just logging: AgentState falls back to newHex() when the builder has no session id (AgentState.java:82), and the shutdown state saver derives uid/sid from the state object (ReActAgent.java:365-388), so a migrated session could be flushed to a brand-new slot. The added assertEquals(SESSION, state.getSessionId()) is the right regression guard.
  • Deprecating the two context-less overloads instead of deleting them keeps this source-compatible for agentscope-harness / -distribution / extensions, and both E2E tests (builder context survives legacy load; pre-existing v2 state wins over the builder template) pin the precedence rule in both directions.

Two non-blocking notes below: the same path still drops initialActiveToolGroups (pre-existing, not introduced here), and the new test could also assert the persisted slot.

CI green (License, Module Sync, ubuntu + windows builds), CLA signed, mergeable.


Automated review by github-manager-bot

stateStore.get(userId, sessionId, "toolkit_activeGroups", ToolkitState.class);

AgentState.Builder builder = AgentState.builder().context(msgs);
AgentState.Builder builder = AgentState.builder().sessionId(sessionId).context(msgs);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Warning] Forwarding permCtx closes one half of the builder-vs-legacy asymmetry, but the same path still drops initialActiveToolGroups. ReActAgent.freshState() seeds ToolContextState from the builder's initial groups (and from agentId as the session fallback); here toolContext is only set when the legacy toolkit_activeGroups key exists. So for a v1 session that has memory_messages but no toolkit_activeGroups, found() is true, the migrated state wins, and the builder's initial groups are silently lost; when the key exists they are discarded rather than merged.

Not introduced by this PR — but since this change is exactly about "the builder's intent must survive legacy loading", it is worth doing the tool-group dimension in the same sweep, or opening a follow-up issue so the two dimensions do not drift apart again. Either way the precedence rule (what wins on migration: persisted v1 data or builder template) should be written down once and applied uniformly to permission context, tool groups and session identity.


AgentState.Builder builder = AgentState.builder().context(msgs);
AgentState.Builder builder = AgentState.builder().sessionId(sessionId).context(msgs);
if (userId != null) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Info] Minor asymmetry in the null handling: sessionId is applied unconditionally (a null still lands on AgentState's newHex() fallback, AgentState.java:82) while userId is guarded. The javadoc does say the session id is required, and ReActAgent always resolves one, so this is not a live bug — but a sessionId != null ? sessionId : <something explicit> guard (or Objects.requireNonNull) here would stop a future caller from silently reproducing the very bug this PR fixes, since the failure only surfaces on the shutdown-saver path that reads the id back off the state object (ReActAgent.java:365-388).

"builder-supplied permission context must survive legacy v1 session loading");
assertEquals(
SESSION,
state.getSessionId(),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Info] Good assertion — this is the one that would catch a regression, because per-call persistence keys off CallExecution.slotKey, whereas the shutdown saver re-derives (userId, sessionId) from the AgentState itself and would flush a migrated session into a random new slot. Worth strengthening with a read-back from the store after agent.call(...):

AgentState persisted = stateStore.get(null, SESSION, "agent_state", AgentState.class).orElse(null);
assertNotNull(persisted, "migrated session must be written back to its own slot");
assertEquals(SESSION, persisted.getSessionId());

That pins the slot identity where it actually matters rather than only in the in-memory object.

@jujn

jujn commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator

superseded #3441

@jujn jujn closed this Oct 8, 2026
juruoO-o pushed a commit that referenced this pull request Dec 1, 2026
…ader on 2.0.x (#2888) (#4452)

Fixes #2888 on `2.0.x`, where the call still exists. `main` no longer
has it.

## Problem

A session that only has v1 keys (`memory_messages` /
`toolkit_activeGroups`) is reconstructed by `LegacyStateLoader` on first
load. Legacy keys carry no permission context, and
`ReActAgent.loadOrCreateAgentStateForSlot` calls the three-argument
overload, which passes `null`. The reconstructed state therefore gets
the default context although the builder configured one, mode and rules
alike. The fresh-state branch of the same method does use the builder's
context.

On `2.0.x` this shows through `getAgentState(userId, sessionId)` and
`getAgentState(ctx)`, which return the state as loaded. A `call()` is
not affected: `activateSlotForContext` applies the builder's context the
first time it activates a slot.

## Fix

`permCtx` is forwarded to the four-argument overload, the shape of #2886
and #3221. Nothing else changes: a state that already exists is still
loaded as it was persisted.

## Tests

`ReActAgentLegacyPermissionContextTest` (new, 4 cases).
`LegacyStateLoaderTest` already covers the loader's four-argument
overload; nothing covered the caller.

| Case | Unmodified `2.0.x` | With the fix |
| --- | --- | --- |
| the legacy session is reconstructed with the builder's context (mode
and deny rule), keeping the conversation and the tool groups | fails:
`expected ... mode=BYPASS, denyRules={shell=[...]}` but was
`mode=DEFAULT` with no rules | passes |
| without a builder context the default is kept | passes | passes |
| an existing state keeps its own context | passes | passes |
| a `call()` applies the builder's context on first activation | passes
| passes |

The last two pass before and after on purpose: they bound the change. A
variant of the fix that also overwrites the context of an existing state
fails the third case (`expected: <EXPLORE> but was: <BYPASS>`).

## Verification

The full `agentscope-core` suite was run before and after the change on
Windows and in a Linux container. The failing lists are identical; the
only difference is the 4 added tests.

| Environment | Before | After | Failing before and after |
| --- | --- | --- | --- |
| Windows | 3237 run, 0 F + 3 E | 3241 run, 0 F + 3 E | 2 ×
`DangerousPathBypassTest.symlink*` (no symlink privilege),
`SkillBoxTest$UploadContainment.preservesLegacyNamesWithRelativeNonNormalizedWorkDirectory`
|
| Linux container (`maven:3.9-eclipse-temurin-17`) | 3242 run, 0 F + 0 E
| 3246 run, 0 F + 0 E | none |

**CI.** The first run of this PR was red for reasons that lay in the
base branch: since the backports of 2026-11-13, every PR against `2.0.x`
failed the Windows job, at first in `agentscope-core`, and had its
Ubuntu job cancelled. Those causes are fixed by #4454 and #4461, merged
into `2.0.x` on 2026-11-27; the harness part went in with #4461. The
current run of this PR, on top of them, is green on both systems. The
local before/after runs recorded above were made before the fixes
existed, which is why the "before" column lists failures that the branch
no longer has.
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.

ReActAgent legacy-state load silently drops permissionContext (downgrades to DEFAULT)

4 participants