Skip to content

feat(session): add a delete API for native session journals - #3613

Closed
ningmao-hlyz wants to merge 5 commits into
agentscope-ai:mainfrom
ningmao-hlyz:feat/delete-session-api
Closed

ningmao-hlyz wants to merge 5 commits into
agentscope-ai:mainfrom
ningmao-hlyz:feat/delete-session-api

Conversation

@ningmao-hlyz

@ningmao-hlyz ningmao-hlyz commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

resolves #3612

AgentScope-Java Version

2.1.0-SNAPSHOT (main)

Description

Addresses the deletion half of #3567 (the issue owner is splitting it into separate deliverables;
the migration side is planned for 2.1 by the team, this one targets the 2.0.5 line where the
journal storage lands via the #3441 backport and the gap blocks retention and privacy-compliance
use cases).

Background

After the JEV switch, native session history lives in a protected namespace under
.agentscope-runtime/ (local files or SQLite). There is no official way to delete one: the
contract layer has no delete primitive (SessionLogStore only opens and lists;
AtomicSessionStorage only reads, lists and compare-and-sets), and users cannot reach the files
themselves. clearContext is not a substitute — it resets the model-visible context and appends a
context_cleared checkpoint while the journal stays on disk.

Changes

  • SessionLogStore.delete(SessionKey, RuntimeContext) and
    AtomicSessionStorage.deletePaths(String prefix), both optional (UnsupportedOperation by
    default, like list).
  • LocalSessionStorage deletes the prefix subtree on POSIX, deepest first, refusing symlinked
    subtrees wholesale instead of deleting around them; SqliteSessionStorage deletes the prefix
    row range in one statement (the DB file is not created by a delete); InMemorySessionLogStore
    drops the per-session store.
  • HarnessAgent.deleteSession(RuntimeContext) and deleteSession(String userId, String sessionId),
    symmetric with clearContext. It acquires the writer lease first, so an active execution rejects
    the deletion, then removes the whole SessionKey namespace — events, checkpoints, the inbox and
    writer seals — and drops the local caches. The lease record lives inside the deleted namespace
    and is deliberately not released.
  • Deletion granularity is per session: the journal is a hash-chained append-only log, so single
    event removal would break chain semantics, while the SessionKey namespace is self-contained.
    A tombstone + physical-reclaim two-phase mode can be discussed for audit-sensitive deployments
    and is not part of this change.
  • Retention is intentionally not included; explicit deletion is the primitive it would be built on.

How to test

mvn -pl agentscope-harness -am test -Dtest='ReActSessionDeleteTest,LocalSessionStorageDeleteTest'

Covers: namespace removal with sibling sessions untouched, restart-from-fresh on the deleted
session, rejection while an external writer holds the lease (and deletion succeeding after
release), the legacy-mode guard, SQLite row-range and file-subtree deletion, missing-prefix no-op,
and symlink refusal (the file-subtree deletion is two-phase: the whole subtree is scanned for
symlinks before anything is removed, so a refused deletion deletes nothing). Both new suites and
the full agentscope-core + agentscope-harness suites pass locally.

Two notes on scope: the remote store-backed storage does not implement deletePaths yet, so
deleteSession throws on distributed deployments until that backend adds support — the local
file and SQLite backends, which the 2.0.5 line targets, are covered. The user-facing documentation
(docs/v2/en/docs/building-blocks/context.md) gains a "Deleting a session" section next to the
clearContext one, stating the difference and the writer-lease behavior.

Checklist

Please check the following items before code is ready to be reviewed.

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn test)

@ningmao-hlyz
ningmao-hlyz force-pushed the feat/delete-session-api branch 2 times, most recently from c7437cf to dfc709e Compare October 16, 2026 13:13
agentscope-ai#3567 records two leftovers of the session-history backend switch. This
implements the deletion side, which is the urgent one for the 2.0.5 line:
the journal storage lands there via the agentscope-ai#3441 backport and the gap blocks
retention and privacy-compliance use cases.

- SessionLogStore.delete(SessionKey, RuntimeContext) and
  AtomicSessionStorage.deletePaths(prefix), both optional like list.
- LocalSessionStorage deletes the prefix subtree on POSIX, deepest first
  and refusing symlinked subtrees wholesale; SqliteSessionStorage deletes
  the prefix row range in one statement; InMemorySessionLogStore drops
  the session store.
- HarnessAgent.deleteSession(rc) / (userId, sessionId), symmetric with
  clearContext: acquires the writer lease, so an active execution rejects
  the deletion, then removes the whole SessionKey namespace including the
  inbox and writer seals and drops the local caches. The lease record
  lives inside the deleted namespace and is deliberately not released.
- Retention is intentionally not part of this change; explicit deletion
  is the primitive it would be built on.

clearContext stays as-is and both are documented as distinct: clearing
resets the model-visible context and appends a context_cleared checkpoint
while the journal itself remains.

Addresses agentscope-ai#3567
@ningmao-hlyz
ningmao-hlyz force-pushed the feat/delete-session-api branch from dfc709e to c08bf6f Compare October 16, 2026 13:25
@ningmao-hlyz
ningmao-hlyz force-pushed the feat/delete-session-api branch from 8125f5d to d7ba95d Compare October 16, 2026 13:50
SqliteSessionStorage opened a fresh JDBC connection (plus a CREATE TABLE
round-trip) for every read, list, compareAndSet and delete. Replaying a
hundred-event journal therefore paid hundreds of connection setups, which
dominates session-list traversal on Windows (measured: 17 sessions x 100
events went from ~2.8s to ~0.5s when each session opened its own storage
instance, and to ~0.35s with one shared storage).

The storage now keeps one long-lived connection per instance and holds the
instance monitor for every operation, so in-process writers serialize exactly
as SQLite file locks serialize them across processes. A failed operation
closes and rebuilds the connection. LocalSessionStorage and the per-root
cache in LocalFilesystem forward close so the database file is no longer held
open once the caller is done; the read-early-return semantics are unchanged
(reading an unwritten namespace still creates no database file).
@ningmao-hlyz
ningmao-hlyz marked this pull request as draft October 31, 2026 23:49
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.

[Bug] 会话历史存储换代后缺少删除原生会话(journal)的官方 API

1 participant