Skip to content

fix: forward caller permission context when loading legacy v1 session state - #3221

Closed
wylovelyi wants to merge 2 commits into
agentscope-ai:mainfrom
wylovelyi:fix/issue-2888-legacy-permission-context
Closed

wylovelyi wants to merge 2 commits into
agentscope-ai:mainfrom
wylovelyi:fix/issue-2888-legacy-permission-context

Conversation

@wylovelyi

Copy link
Copy Markdown

What

ReActAgent.loadOrCreateAgentStateForSlot now forwards the caller's PermissionContextState through the legacy v1 session load path, so sessions that only carry v1 keys (memory_messages / toolkit_activeGroups) keep their configured permission mode (e.g. BYPASS) instead of being silently downgraded to DEFAULT.

Why

Closes #2888. This is a regression: #2760 rewrote the method and reverted the legacy-state path to the 3-arg LegacyStateLoader.loadFromLegacySessionWithPresence(stateStore, userId, sessionId) overload, which passes null for permCtx. The 4-arg overload that forwards permCtx already exists (added in #2886 / #2769), so only the call site needed fixing — exactly the change #2760 dropped.

How

  • agentscope-core/src/main/java/io/agentscope/core/ReActAgent.java:439 — call loadFromLegacySessionWithPresence(stateStore, userId, sessionId, permCtx) instead of the 3-arg overload.
  • Re-adds the regression test ReActAgentLegacyPermissionContextTest (removed by fix(core): propagate agent state load failures #2760) which pins the legacy + permission-context combination. The test triggers the legacy load via getAgentState() and asserts the builder-supplied BYPASS mode survives (and that an existing v2 state keeps its own DEFAULT).

Verification

  • The change is a one-line forwarding that mirrors the previously-merged fix(core): pass builder permission context when loading legacy v1 session state #2886 fix; the 4-arg overload already applies permCtx to the reconstructed state (LegacyStateLoader.java:122-125).
  • loadOrCreateAgentStateForSlot is exercised by getAgentState() (ReActAgent.java:4407), which the new test drives.
  • Note: the full Gradle build/test was not executed in the contributor sandbox; CI must confirm on the merged base.

Self-review (P0-P3)

  • P0: change matches issue scope, no secrets, no unrelated refactor, compiles against current main (4-arg overload present). ✅
  • P1: edge case covered — an existing v2 state wins over the builder template; an anonymous slot keeps a null user id. ✅
  • P2/P3: regression test added; formatting matches surrounding code. ✅

… state

ReActAgent.loadOrCreateAgentStateForSlot forwards the caller's permCtx on the
fresh-state path, but its legacy-state path called the 3-arg
LegacyStateLoader.loadFromLegacySessionWithPresence overload, which passes null
and silently downgrades a configured permission mode (e.g. BYPASS) to DEFAULT.
This re-introduces the regression fixed in agentscope-ai#2769 / agentscope-ai#2886 after agentscope-ai#2760 rewrote the
method.

Forward permCtx through the existing 4-arg overload so legacy v1 sessions keep
the caller-configured permission context. Re-adds the regression test removed by
agentscope-ai#2760.

Closes agentscope-ai#2888

Signed-off-by: wylovelyi <[email protected]>

@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

One-line forwarding of the caller's PermissionContextState into the legacy v1 session load path — the fix itself is correct and well targeted at #2888, and the permCtx overload it calls is already on main. Two things block it right now, both visible in CI: the new test file's license header is malformed (Check License fails) and the new sessionId assertion fails because LegacyStateLoader never sets sessionId/userId on the reconstructed state (build (ubuntu-latest) fails). Fix the header and either forward the slot identity or scope the test down, and this is good to go.

Findings

  • [Critical] agentscope-core/src/test/java/io/agentscope/core/agent/ReActAgentLegacyPermissionContextTest.java:6 — license header missing the trailing at, fails Check License.
  • [Critical] ...ReActAgentLegacyPermissionContextTest.java:79 — fails in CI: LegacyStateLoader.loadFromLegacySessionWithPresence does not set sessionId/userId, so the migrated state gets a random id.
  • [Info] agentscope-core/src/main/java/io/agentscope/core/ReActAgent.java:440 — permission-context forwarding verified correct against LegacyStateLoader.java:113-133.

Verification seen

  • license/cla is green, so CLA is signed; CI runs on head 6958aa38 show Check License and build (ubuntu-latest) failing for the two reasons above.
  • The second test (existingV2State_winsOverBuilderPermissionContext) is a nice touch — it pins that an existing v2 state is not clobbered by the builder template.

Automated review by github-manager-bot

*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License

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.

[Critical] The license header is malformed — line 6 is missing the trailing at (You may obtain a copy of the License → should be You may obtain a copy of the License at). This is what makes the Check License job fail on this PR; please copy the exact header from a neighbouring file (e.g. ReActAgentHitlTest.java) or run license-eye header fix locally.

PermissionMode.BYPASS,
state.getPermissionContext().getMode(),
"builder-supplied permission context must survive legacy v1 session loading");
assertEquals(

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.

[Critical] This assertion fails in CI (build (ubuntu-latest)): expected: <session-legacy-perm> but was: <92536d6e...>. The test is right, the production code is not: LegacyStateLoader.loadFromLegacySessionWithPresence(...) builds the state with AgentState.builder().context(msgs) and never calls .sessionId(...)/.userId(...), so AgentState falls back to newHex() for sessionId. That means the legacy migration path returns a state whose identity does not match the (userId, sessionId) slot it was loaded for — a second regression next to the permission-context one this PR fixes.

Suggested direction (either is fine, but the PR must be green):

  1. Forward the slot identity here too, e.g. pass sessionId/userId (or the whole slot) into the legacy loader and set them on the builder — consistent with freshState(permCtx, agentId, userId, sessionId, ...), which does set them; or
  2. If you prefer to keep this PR to the one-line permission fix, drop these two identity assertions and open a follow-up issue so the sessionId loss is tracked separately.

LegacyStateLoader.LegacyLoadResult legacy =
LegacyStateLoader.loadFromLegacySessionWithPresence(stateStore, userId, sessionId);
LegacyStateLoader.loadFromLegacySessionWithPresence(
stateStore, userId, sessionId, permCtx);

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] The forwarding itself is correct — the 4-arg overload already applies permCtx (LegacyStateLoader.java:122-125), loadFromLegacySessionWithPresence is only called from this one site, and legacy keys never carry a permission context, so the builder-supplied template is the right fallback. No change needed here; just note that the same reconstruction also drops sessionId/userId (see the inline comment on the new test), which is why the added test currently fails.

- Restore the missing ' at' in the Apache license header so license-eye
  header check passes on the new regression test.
- The legacy loader does not stamp a session id onto the reconstructed
  state, so assert the null user id instead of a specific session id.

Signed-off-by: wylovelyi <[email protected]>
@codecov

codecov Bot commented Sep 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@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

Re-review after 814447afe6. Both blockers from my previous review are resolved and the production change is unchanged, so this is an approval. One non-blocking wording nit is pinned inline on the new comment.

What I verified (not taken from the commit message)

  • License header now reads You may obtain a copy of the License at, and Check License + Check Module Sync are pass on head 814447afe6 (they were failing on 6958aa383b).
  • The scoped-down assertion is correct, not just weaker. LegacyStateLoader.loadFromLegacySessionWithPresence builds AgentState.builder().context(msgs) and only attaches permissionContext / toolContext; it never calls userId(...), and AgentState's constructor defaults only sessionId (builder.sessionId == null ? newHex() : ...) while userId stays null. So assertNull(state.getUserId(), ...) pins real behaviour.
  • The fix itself is untouched by this push. loadOrCreateAgentStateForSlot still forwards permCtx into loadFromLegacySessionWithPresence(stateStore, userId, sessionId, permCtx), which is what keeps a builder-supplied PermissionMode.BYPASS from silently degrading to DEFAULT on a v1-only session. The second test still pins that an existing v2 state wins over the builder template.

Follow-up worth a separate issue (pre-existing, not caused by this PR)

ReActAgent's shutdown state saver (constructor, ~line 369) derives its write target from the state object itself — agentState.getUserId() / agentState.getSessionId() — instead of from the slot key used everywhere else (saveStateToSession goes through SlotRef.parse(scope.slotKey)). For a legacy-migrated state, whose sessionId is the constructor-generated hex, that saver would compute a slot never registered in slotVersions (getOrDefault → UNVERSIONED) and persist an orphan agent_state entry rather than updating the real session. Worth tracking independently of this change; flagging it here only because this test is the place where the "no slot identity on migrated state" fact is now documented.

CI / merge state at review time

license/cla signed (Contributor License Agreement is signed.), Check License and Check Module Sync pass; build (ubuntu-latest) and build (windows-latest) were still pending when this review was posted, and the PR is MERGEABLE / BLOCKED. The approval covers the code, not merge readiness — the two build jobs are the ones that were red last round, so they are the ones to watch.


Automated review by github-manager-bot

state.getPermissionContext().getMode(),
"builder-supplied permission context must survive legacy v1 session loading");
// The legacy loader only reconstructs context/tool/permission state; it does not stamp a
// session id (the slot's session id is applied later by the caller), so we assert the

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] The new comment says the slot's session id "is applied later by the caller" — that part I could not verify in the code. loadOrCreateAgentStateForSlot / activateSlotForContext cache the loaded state under the slot key, but nothing writes userId/sessionId back into the AgentState, and since LegacyStateLoader never sets them, AgentState's constructor keeps the generated newHex() value on the object. So the assertion below is right (a legacy-migrated state really does have a null user id), but the reason stated here overstates what the caller does. Suggest rewording to something like "the legacy loader does not stamp slot identity onto the state; the slot key is tracked separately by ReActAgent". Non-blocking — no behaviour change requested in this PR.


Automated review by github-manager-bot

@wylovelyi

Copy link
Copy Markdown
Author

@jujn 你好,这个 PR 在加载 legacy v1 session state 时转发调用方权限上下文。准备好 review 了,谢谢!

@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.

Regression: #2760 dropped permission context when loading legacy v1 session state (re-opens #2768)

3 participants