Skip to content

fix(harness): match index prefixes literally, not as LIKE patterns - #3875

Merged
jujn merged 2 commits into
agentscope-ai:mainfrom
WMK666666:fix-workspace-index-like-escape
Nov 1, 2026
Merged

jujn merged 2 commits into
agentscope-ai:mainfrom
WMK666666:fix-workspace-index-like-escape

Conversation

@WMK666666

Copy link
Copy Markdown
Contributor

Problem

WorkspaceIndex answers prefix lookups by interpolating the prefix straight into a SQL LIKE
pattern:

ps.setString(1, prefix + "%");

_ and % are LIKE metacharacters, and agent / session / file names reach this class verbatim
from configuration and API callers, so the pattern is not a literal prefix match:

index.listByPrefix("agents/bot_1/sessions/s_1/");
// -> [agents/bot_1/sessions/s_1/report.md, agents/bot21/sessions/s21/report.md]

hasPrefix has the same false positives, which is the more damaging half: it decides whether
RemoteFilesystem may take the index fast path at all, so a look-alike directory reports "I have
entries" and the authoritative store scan is skipped.

ls then trusts the rows it gets back — it does not re-check them the way glob does with
matchesPathPrefix — and returns each matched path as an entry of the directory being listed:

infos.add(FileInfo.ofFile(p, 0, ""));   // p is the full path of a foreign file

So listing agents/bot_1/sessions/s_1/ can return a file that belongs to another agent's session
directory, and the relative names it derives for sub-directories are shifted for the same reason.
% behaves identically: a URL-derived session id such as sess%2F1 also matches sess2F1.

Change

  • Escape %, _ and the escape character itself in the prefix, and declare ESCAPE '\' on both
    prefix statements. The prefix is now matched literally, as its callers assume.
  • hasPrefix(null) returns false instead of building the pattern "null%".

No behaviour change for ordinary prefixes; the lookups are still a single indexed query.

Verification

  • New WorkspaceIndexPrefixMatchTest (4 cases): underscore in both the agent and the session
    segment, hasPrefix false positive, % literal, plus an ordinary-prefix regression case.
  • Reverse check: reverting WorkspaceIndex makes 3 of the 4 fail, e.g.
    expected: <[agents/bot_1/sessions/s_1/report.md]> but was: <[agents/bot_1/sessions/s_1/report.md, agents/bot21/sessions/s21/report.md]>,
    while the ordinary-prefix case keeps passing — the tests pin the defect, not the implementation.
  • mvn -pl agentscope-harness test: 1570 tests, 0 failures, 0 errors, 34 skipped, BUILD
    SUCCESS
    (1566 on the same commit without this change).
  • mvn spotless:check passes.

Related but different root cause: #3197 / #3201 cover the fast path returning no rows and never
falling back to the store. This change covers the case where the fast path returns the wrong
rows.

listByPrefix/hasPrefix interpolated the caller's prefix straight into a SQL LIKE
pattern. Agent, session and file names arrive verbatim, so '_' and '%' inside them
acted as wildcards: a lookup under agents/bot_1/sessions/s_1/ also returned
agents/bot21/sessions/s21/. hasPrefix reporting a false positive is the worse half
because it decides whether RemoteFilesystem may use the index fast path, so the
authoritative store scan was skipped; ls does not re-check the rows the way glob
does, so a foreign file could be listed as a child of the queried directory.

Escape the metacharacters and declare ESCAPE on both statements. hasPrefix(null)
now returns false instead of building the pattern "null%".

Tests: new WorkspaceIndexPrefixMatchTest with four cases, three of which fail when
the production change is reverted. Full harness module: 1570 tests, 0 failures,
34 skipped.
@CLAassistant

CLAassistant commented Oct 31, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@WMK666666

Copy link
Copy Markdown
Contributor Author

@jujn gentle ping on this one. CI is green on all four checks (build (ubuntu-latest), build (windows-latest), Check License, Check Module Sync), the CLA check is signed, and the change is contained: +134/-4 across WorkspaceIndex.java plus one test file.

For context, what it fixes: the prefix was interpolated straight into a LIKE pattern, so _ and % inside an agent or session name acted as wildcards — listByPrefix("agents/bot_1/sessions/s_1/") also returned agents/bot21/sessions/s21/report.md. The more damaging half is hasPrefix, which decides whether RemoteFilesystem may take the index fast path at all, so a look-alike directory could report "I have entries" and suppress the authoritative scan.

Happy to rework the approach if escaping does not fit the surrounding code, or to split / close it if you would rather handle it another way.

@WMK666666

Copy link
Copy Markdown
Contributor Author

Thanks for the review and the merge, and for picking it up after my ping.

chickenlj pushed a commit that referenced this pull request Nov 13, 2026
…3875)

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 4399f75)
Backport-review-patch: 340acd49331636940faf7d898805165e70045f21
Constraint: No AgentScope Service or SDK migration
Confidence: high
Not-tested: Whole-repository verification pending after lane integration
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.

3 participants