Skip to content

[Bug]: rules accepted through a ConfirmResult are lost at the next call boundary #3369

Closed

Description

@kyy-logs

AgentScope-Java Version

2.0.4-SNAPSHOT (main)

Description

When a ConfirmResult accepts a call together with rules to remember, those rules are added to the live PermissionEngine (ReActAgent.java:2180) and never reach the session's persisted context. With a state store configured the next call rebuilds the engine from that context, so the accepted rule is gone: "always allow for this session" does not outlive the call it was granted in.

Found while working on #3291 and recorded there as out of scope. The option added in #3361 does not change it — it composes the session's persisted context with the declared rules, and an accepted rule was never persisted to begin with.

Reproduction

  1. Configure a persistent AgentStateStore (Redis, JDBC, a JSON file).
  2. Declare a tool that asks — an ASK rule, or the default when no rule matches.
  3. Accept a call with ConfirmResult(true, toolCall, rules) carrying a rule to remember.
  4. Call again on the same (userId, sessionId) and trigger the same tool.

Expected: the remembered rule applies and the call runs without asking.

Actual: it asks again, because the rule went to the engine that the next call replaces.

Discussion

Carrying the rules across calls means persisting them. They fit in the existing rule tables of AgentState.permissionContext without a format change, so this may be smaller than it looks — but writing runtime-accepted rules into the stored context does change what is persisted, which is why it was kept out of #3291.

Activity

  1. oss-maintainer commented on Sep 30, 2026

    @oss-maintainer
    Collaborator

    Issue Evaluation

    Category: bug | Status: Confirmed (verified against main @ a5be76105, revision = 2.0.4-SNAPSHOT)

    All three claims in the report hold on the current code.

    Confirmed

    1. Rules carried by a ConfirmResult reach only the in-memory engine. ReActAgent.applyConfirmResults (ReActAgent.java:2101-2144) calls permissionEngine.addRule(rule) (:2116) on the call-scoped CallExecution.permissionEngine (field declared at :1725). PermissionEngine.addRule (PermissionEngine.java:94-106) mutates only the engine's own allowRules/denyRules/askRules maps, and the class javadoc (:43-45) states the engine snapshots the rules on construction and "the original context is never mutated" — so AgentState.permissionContext never sees the accepted rule. (The ReActAgent.java:2180 cited in the report is the same method; the line has drifted slightly.)
    2. The next call really does rebuild the engine from the persisted context. activateSlotForContext (ReActAgent.java:659-707), invoked from beforeAgentExecution (:750) at the start of every call(), unconditionally does new PermissionEngine(loaded.getPermissionContext()) when an AgentStateStore is configured (:699) and replaces the cached instance (:700). End-of-call persistence saveStateToSession → persistAgentStateCas (:475-496) writes scope.state as-is, and the only in-flow write to state.setPermissionContext(...) is installPermissionContext (:4709-4718), reachable only from the external setPermissionMode / replacePermissionContext APIs — the ConfirmResult path never routes through it.
    3. No format change is needed. PermissionContextState already serializes allow_rules / deny_rules / ask_rules and its builder exposes addAllowRule/addDenyRule/addAskRule plus a withMode copy pattern (PermissionContextState.java:36,59-61,148,223-233).

    One clarifying note on the failure mode

    Without a store the engine is cached via computeIfAbsent (ReActAgent.java:298-304,702-704), so runtime rules do survive across calls in a single JVM. That is why this is easy to miss: the in-memory path masks the defect, and it only surfaces once a state store is configured, the process restarts, or another instance serves the same session. The documented contract contradicts the implementation — ConfirmResult.java:27-29,72-80 promises "new rules that the PermissionEngine should remember for future calls", and docs/v2/en/docs/building-blocks/permission-system.md:13,202-214 promises that accepted suggested rules mean "identical future calls are auto-handled without prompting" (the documented rule source enum even includes "session", doc line 169).

    Impact: agentscope-core permission/HITL flow for every deployment with a persistent AgentStateStore — which is the supported path for distributed and multi-tenant sessions, so stateless horizontal scaling and session recovery are affected. The AGUI adapter currently passes null rules (AguiMessageConverter.java:477), so the stock UI path is not yet hit; the impact falls on API consumers using the documented suggested-rules flow.

    Severity: medium — it fails safe (re-ASK rather than over-ALLOW), so this is a functional/UX contract violation, not a security bypass. The annoying part is that "always allow for this session" silently gives up exactly when a session outlives one JVM.

    Fix direction: in applyConfirmResults, merge each accepted rule into scope.state's PermissionContextState (routed by behavior the same way PermissionEngine.addRule routes it) so the existing end-of-call saveStateToSession persists it; a withAllowRule/withDenyRule/withAskRule or toBuilder() helper on PermissionContextState, mirroring withMode, keeps the merge small, with dedupe so repeated approvals don't grow the tables unboundedly. Regression test: with an in-memory store, call 1 raises ASK, resume with ConfirmResult(true, toolCall, List.of(rule)), then assert call 2 does not re-ASK and the persisted context contains the rule. No existing test constructs a ConfirmResult with non-null rules (ReActAgentHitlTest.java:255 always passes null), so the area is completely uncovered.

    An automated fix proposal will be generated. Reply /approve to proceed with PR generation.


    Automated evaluation by github-manager-bot

  2. added
    bug
    area/core/agent
    on Sep 30, 2026
  3. oss-maintainer commented on Sep 30, 2026

    @oss-maintainer
    Collaborator

    Proposed Fix (spec v1) — for the confirmed rule-persistence gap above

    I generated a fix proposal for this. Summary below; replying /approve starts PR generation against main, /revise <feedback> regenerates the spec.

    Root cause — ReActAgent.applyConfirmResults (ReActAgent.java:2111-2119) feeds accepted rules only into the call-scoped PermissionEngine, which by design never writes back to the PermissionContextState it was snapshotted from (PermissionEngine.java:43-45). The one state object that crosses the call boundary — AgentState.permissionContext — stays untouched, so activateSlotForContext (:699) rebuilds an engine without the rule on the next call.

    Additional finding worth flagging: the defect is worse than "lost at the next call boundary" when a session starts with a trivial permission context. The gate at ReActAgent.java:3399 (useEngine = !state.getPermissionContext().isTrivial()) means the lightweight path (:3441-3456) never consults the engine's rule tables at all — so an accepted rule is ineffective even in-JVM, not only after a reload. Writing the rule back into permissionContext fixes both halves at once, because the context stops being trivial.

    Design (single change site)

    1. PermissionContextState.withRule(PermissionRule) — new copy-on-write helper next to withMode (PermissionContextState.java:148-159): routes by rule.behavior() like PermissionEngine.addRule, returns this for PASSTHROUGH, dedupes on (toolName, ruleContent, behavior) and deliberately ignores source (provenance, not match semantics). Keeps the context deeply immutable, which the engine snapshot / CAS persistence / conflict-merge paths rely on.
    2. applyConfirmResults — alongside the existing permissionEngine.addRule(rule), fold each accepted rule into scope.state's context and set it once per result; the existing end-of-call saveStateToSession → persistAgentStateCas (:475-496) then persists it. No new persistence code.
    3. PermissionEngine.addRule — skip semantically equal rules too, so the no-store cached engine doesn't grow on repeated approvals.
    4. Not routed through installPermissionContext (:4709-4718): it also swaps the engine cache and persists immediately, which mid-call would double-write and fight the CAS loadedVersion bookkeeping.

    Compatibility — serialized format unchanged (rules go into the existing allow_rules/deny_rules/ask_rules keys); old persisted states read fine and withRule is additive. Calls on one (userId, sessionId) slot are serialized (:727-737), and a lost CAS race drops the delta per the existing ConflictPolicy, degrading to a re-ASK. Security-wise the only widening is exactly the documented "always allow for this session" promise: only rules a user explicitly accepted at an ASK prompt are persisted, there is no denial→allow path, and every failure mode re-ASKs rather than auto-allowing.

    Tests — new ReActAgentConfirmRulePersistenceTest with an in-memory store: accepted ALLOW survives into call 2 without re-ASK and is present in the persisted context; DENY auto-denies; ASK re-prompts; N repeated approvals persist one rule; no-store behavior unchanged; rule survives a rebuilt agent instance on the same (userId, sessionId) (restart / another node). Plus withRule units and an addRule dedupe test. This area is currently uncovered — no test anywhere builds a ConfirmResult with non-null rules.

    Out of scope — the AGUI adapter passes null rules (AguiMessageConverter.java:477), so the stock UI path is untouched; surfacing suggested rules in the UI is a separate feature. No change to PermissionEngine snapshot semantics, ConflictPolicy, or replacePermissionContext.

    Trade-off to review — this does write runtime-accepted rules into the stored context, which is the boundary #3291 intentionally kept. The arguments for crossing it here: the API and docs already promise cross-call memory, the change is one additive helper plus one call site, and it fails safe. If maintainers would rather keep AgentState.permissionContext init-only, the alternative is a separate persisted section for session-scoped rules — a format change, which is why it wasn't chosen.


    Automated proposal by github-manager-bot

  4. oss-maintainer commented on Oct 3, 2026

    @oss-maintainer
    Collaborator

    Proposal window closed — no automated PR will be generated, this proposal window here is closed

    The 72 h feedback window opened by the specification above (2026-09-30T05:14:56Z, comment 5904573164) reached its bound at 2026-10-03T05:14:56Z with no approve, revise or reject directive from a maintainer or the reporter. Per the policy stated in that comment, automated PR generation for this issue is therefore skipped: we are not opening a PR for #3369 and no automated PR will be generated from this thread.

    The implementation path is already owned by the community. PR #3377 (fix(core): persist ConfirmResult rules into session permission context, authored by @chaucerj, cross-referenced here on 2026-09-30T17:48:46Z) targets the same seam the specification describes — folding rules accepted through a ConfirmResult into the persisted permission context so they survive the next call boundary. As of this read #3377 is OPEN, not a draft, unmerged, head f1faf083945331244fdff009dac5330cc8f5f3f2 (560 additions / 6 deletions across 7 files), last updated 2026-10-01T03:41:42Z, mergeable reported MERGEABLE. Filing a second PR against the same issue is exactly the collision this window policy exists to prevent, which is why the proposal is stood down rather than escalated.

    Nothing in this notice disputes the evaluation. The reported defect — rules accepted through a ConfirmResult reaching the live engine but not the stored context, so the next call rebuilds from a snapshot without them — was verified against the code at main@915507134 and stands. The productive thread now is review of #3377: merge it, request changes, or close it.

    If #3377 is closed unmerged and an automated proposal is still wanted here, reply on this issue and the fix will be re-evaluated against whatever main looks like at that point. No action is needed from anyone else.

    Automated feedback-window maintenance by github-manager-bot

  5. juruoO-o commented on Nov 12, 2026

    @juruoO-o
    Collaborator

    Closing as resolved by #3377 (merged) — its description states "Fixes #3369": rules accepted through a ConfirmResult are now persisted into the session permission context (the auto-close did not trigger due to body formatting). Reopen if rules are still lost at the next call boundary.

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