Skip to content

[Bug]: Builder permission rules are snapshotted into persisted sessions; rule changes never reach existing sessions #3291

Closed

Description

@xuchaoguo1983

AgentScope-Java Version

2.0.3

Description

The builder-time permissionContext(...) is copied into AgentState only when a session slot is first created (loadOrCreateAgentStateForSlot → freshState(initialPermissionContext, ...)). After that, each call rebuilds the PermissionEngine from the persisted AgentState.permissionContext, so the rules declared in code never reach existing sessions again.

With a persistent AgentStateStore (Redis, JSON file), a code change to the permission rules therefore applies only to sessions created after the deployment:

  • Tightening fails open. A tool moved from ALLOW to ASK keeps running without confirmation in every existing session. The same applies to a newly added DENY rule.
  • New ALLOW rules do not apply. A tool added in a later release, with an ALLOW rule, still asks for confirmation in existing sessions (DEFAULT mode).

Reproduction (verified on 2.0.3, Redis state store)

  1. Build agent v1 with PermissionMode.DEFAULT and an ALLOW rule for tool T. Call it on (userId=u, sessionId=s), and have the model call T. T runs.
  2. Build agent v2 on the same store, with an ASK rule for T instead ("redeploy"). Call it on the same (u, s), and have the model call T.
    • Expected: PERMISSION_ASKING.
    • Actual: T runs without confirmation (MODEL_STOP).
  3. Call v2 on a new session (u, s2). It returns PERMISSION_ASKING, as expected.

Discussion

replacePermissionContext can push new rules into a slot, but it has to be called per session by the host. It also currently has the stale-cache write issue addressed in #2731. There seems to be no supported way to say "the rules declared in code are authoritative for every call", while keeping per-session runtime changes (such as setPermissionMode, and rules accepted through ConfirmResult) where they are wanted.

Possible directions:

  • An opt-in builder flag. On each call, the engine is built from the builder rules merged with the session's runtime additions, rather than from the persisted snapshot alone.
  • Or persist only the runtime additions (mode changes, user-accepted rules) in AgentState, and always compose them with the current builder rules.

Activity

  1. oss-maintainer commented on Sep 25, 2026

    @oss-maintainer
    Collaborator

    Issue Evaluation

    Category: bug | Status: Confirmed (verified against main, 2.0.4-SNAPSHOT)

    The report is accurate. Builder-declared permission rules are copied into AgentState.permissionContext only when a session slot is first created, and every later call rebuilds the PermissionEngine from the persisted snapshot, so in-code rule changes never reach pre-existing sessions.

    Root cause (code paths)

    • ReActAgent.loadOrCreateAgentStateForSlot returns the stored agent_state when one exists; freshState(initialPermissionContext, ...) (which writes the builder context) only runs for a new slot — agentscope-core/src/main/java/io/agentscope/core/ReActAgent.java:421-468.
    • ReActAgent.activateSlotForContext builds the engine from the loaded state: new PermissionEngine(loaded.getPermissionContext()) (ReActAgent.java:659-707, esp. 697-705), and saveStateToSession re-persists that same snapshot (ReActAgent.java:475-496).
    • PermissionEngine snapshots rules at construction (state/PermissionEngine.java:59-106); PermissionContextState.withMode preserves the stale rule tables (state/PermissionContextState.java:148-159).
    • setPermissionMode / replacePermissionContext operate per session on the persisted context (ReActAgent.java:4621-4656); there is no option that makes builder rules authoritative per call.

    Impact

    • Any deployment with a persistent AgentStateStore (Redis / JSON / JDBC), for ReActAgent and HarnessAgent (which delegates).
    • Confirmed by execution on current main: ALLOW→ASK on an existing session runs the tool uncconfirmed (MODEL_STOP instead of PERMISSION_ASKING); a newly added ALLOW rule still prompts on old sessions; a newly added DENY/ASK does not apply to old sessions.
    • Additional finding: with a store configured, rules accepted through a HITL ConfirmResult are applied only to the in-memory engine and are lost at the next call boundary, so "always allow for this session" does not survive across calls (ReActAgent.java:2113-2119).
    • Not affected: sessions with a trivial persisted context stay on the lightweight permission path (ReActAgent.java:3349), and store-less in-process agents keep the memoized engine — worth covering in the fix's test matrix.

    Severity: high — a security control silently fails open for the whole pre-existing session population after a rule change; the only current mitigation is calling replacePermissionContext per session (or flushing sessions) on every deploy.

    Next step: a fix proposal (spec) will be posted here for review — reply /approve to proceed with PR generation. Suggested direction: compose the per-call engine from the current builder rules as the base and layer only the session's runtime deltas on top (mode override + user-accepted rules), persisting deltas instead of the full rule snapshot, with precedence so a stale persisted ALLOW can never relax a newer builder ASK/DENY. Please coordinate with #2731, which touches the same setPermissionMode / replacePermissionContext paths and is still open.


    Automated evaluation by github-manager-bot

  2. added
    area/core/agent
    bug
    on Sep 25, 2026
  3. xuchaoguo1983 commented on Sep 25, 2026

    @xuchaoguo1983
    Author

    Cross-reference: the maintainer evaluation on #3135 independently lists the same defect ("Builder permissionContext(...) is silently ignored once a persisted state exists. Precedence should either reconcile the two or be documented explicitly."). Also worth noting for prioritisation: persisted sessions have no expiry by default, so after a release that tightens a rule, every existing session keeps the old rule for as long as it lives.

  4. oss-maintainer commented on Sep 28, 2026

    @oss-maintainer
    Collaborator

    Status: 72 h feedback window expired — no automated PR will be generated, this proposal window here is closed

    The evaluation above (2026-09-25 11:12:06Z) reached its 72 hour limit at 2026-09-28 11:12:06Z without any of the three feedback commands arriving, so the spec-and-PR step is not being taken: we are not opening a PR for #3291 and no automated PR will be generated from this thread. No action is needed from anyone.

    What landed under the window:

    • @xuchaoguo1983 (2026-09-25 14:50:22Z) cross-referenced [Bug]:ASKING 状态会被持久化到状态存储中 #3135, whose evaluation independently records the same defect, and added that persisted sessions have no expiry by default — so a stale rule keeps serving an existing session for as long as that session lives. Useful prioritisation input; it is not one of the feedback commands, so the gate stayed unsatisfied.

    The finding itself still stands as written: builder-declared permission rules are copied into AgentState.permissionContext only when a session slot is first created, and later calls rebuild the PermissionEngine from the persisted snapshot, so rule changes never reach pre-existing sessions. This stand-down is about our own proposal window, not about the defect.

    Two coordination notes for whoever picks this up: #3135 reports the same defect from the persisted-ASKING side, and #2731 is still open against the adjacent setPermissionMode / replacePermissionContext paths. If an automated proposal is still wanted here, say so on this issue and we will re-evaluate.

    Automated by github-manager-bot

  5. kyy-logs commented on Sep 29, 2026

    @kyy-logs
    Contributor

    I'd like to take this one.

    Direction I plan to take: an opt-in builder flag rather than changing what gets persisted. With it enabled, each call composes the engine from the current builder rules as the base and layers the session's runtime deltas on top — a mode override, and the rules accepted through a HITL ConfirmResult. With it disabled nothing changes, including the persisted format.

    Why that rather than composing by default:

    The seam is the engine construction in activateSlotForContext (ReActAgent.java:697-707), which currently reads loaded.getPermissionContext() in both branches.

    Test matrix I intend to cover, including the two cases the evaluation flagged as unaffected: a persistent store with an existing slot (the defect), a fresh slot, a store-less agent (memoized engine), and a session whose persisted context is trivial so it stays on the lightweight path.

    Happy to be told the composition direction is wanted instead — that one changes persisted semantics and I would rather not start there without a maintainer saying so.

  6. kyy-logs commented on Sep 29, 2026

    @kyy-logs
    Contributor

    Correction to one claim above: I said this option fixes the evaluation's second finding — that rules accepted through a ConfirmResult are lost at the call boundary. It does not.

    Those rules are written to the live engine only (ReActAgent.java:2179) and are never persisted, so rebuilding the engine per call drops them however the context is composed. Carrying them across calls means persisting them, which changes what gets stored — the thing this option deliberately leaves alone. I have kept it out of the change and will raise it separately.

  7. added a commit that references this issue on Nov 4, 2026
    d0ce778
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/core/agentbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions