Skip to content

fix(core): 保留取消快照中已完成的工具结果 - #4046

Merged
juruoO-o merged 1 commit into
agentscope-ai:mainfrom
xihongshichaojidan8:codex/fix-4045-native-tool-cancel-results
Nov 11, 2026
Merged

juruoO-o merged 1 commit into
agentscope-ai:mainfrom
xihongshichaojidan8:codex/fix-4045-native-tool-cancel-results

Conversation

@xihongshichaojidan8

Copy link
Copy Markdown
Contributor

Fixes #4045

AgentScope-Java Version

2.1.0-SNAPSHOT;本地验证提交:000386c816507cf3fbbc39fc95e0a7accadc4da0(基于 85fa9a60257ceb1570e6a195d0588ceba1e8a8c2)。

Description

并行工具批次中,已完成工具的 action/end 可以先于批次结果写入 canonical context。若工具更新了会话状态,此时硬取消产生的新 checkpoint 会遮蔽已记录的完成结果;显式核对真正不确定的兄弟工具并恢复后,已成功工具可能再次执行。完整复现及四组对照见 #4045。

本 PR 将改动限定在取消收尾:

  • 在 SessionRecorder 中保留本轮完成工具结果的冻结副本。
  • 仅在 finish(..., "cancelled") 时,向恢复快照补入当前仍有未配对本地调用的完成结果。
  • 不修改 live context,不覆盖 canonical 已有结果,不补 suspended 或已移除的调用。
  • 不改变正常批次、middleware/PostActing 的执行顺序;未知工具仍要求显式 reconciliation。

生产代码只改 SessionRecorder.java(35 行新增、1 行删除),另加两个测试文件;无公共 API、事件格式、依赖或配置变化。不修改 SessionProjection 或恢复扫描锚点。

验证

在 macOS / Microsoft OpenJDK 21.0.7(源码 release 17)上,对本 PR 实际提交树完成干净构建:

mvn -B -T1C -pl agentscope-core -am clean test spotless:check

本地使用 Maven Central settings 和离线缓存;结果:

Tests run: 3031, Failures: 0, Errors: 0, Skipped: 9
BUILD SUCCESS
  • 新增 15 项回归全部通过:call/streamEvents × 状态变化/不变;已改写结果优先且不重复;移除调用/状态回退不复活旧结果;未知结果仍不确定;suspended、ERROR/DENIED 和旧结果编码兼容;live state 不被修改。
  • 9 项为仓库现有条件跳过:7 项 stream ordering 占位测试、1 项需显式开启的结构化输出竞争测试、1 项 Windows-only 测试。
  • Spotless 与 git diff --check 通过。
  • 未执行全仓 verify;以上是本地验证,不代表上游 CI 已通过。

范围边界

本修复确保成功写出的取消 checkpoint 保留本轮已确认结果,不承诺任意进程崩溃下的 exactly-once,也不迁移或修复已经产生的受损日志。hard cancellation 不保证协作中断回复/保存路径的现有契约不变。

Checklist

  • 已格式化并通过 Spotless 检查。
  • 完整 agentscope-core 测试通过,条件跳过与未执行的全仓验证已在上面明确列出。
  • 无新增公共 API,现有 Javadoc 契约不变;新增内部逻辑有范围说明。
  • 已核对文档影响:本次修正取消恢复缺陷,不新增 API/配置或更改现有中断契约,无文档变更。
  • 代码已完成本地 review,可供维护者审阅。

@jujn jujn added the P0 重要紧急 label Nov 10, 2026

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

取消 checkpoint 补回已确认工具结果能够避免恢复后重复执行。已核对补充逻辑只作用于恢复快照,不修改 live context,不覆盖已有 canonical 结果,也不将未知或 suspended 结果转为完成。修改限定在 SessionRecorder,未改变公共 API 或事件格式。代码复核未发现阻塞问题;本轮未重跑 core 全量测试。与 #3888 新增的同名测试文件需要在后合入的一侧整合。

@juruoO-o
juruoO-o merged commit 2e7b59c into agentscope-ai:main Nov 11, 2026
5 checks passed
juruoO-o added a commit that referenced this pull request Nov 11, 2026
…ancelled (#3888)

## Problem

`SessionRecorder.flush()` schedules storage work like this:

```java
public Mono<Void> flush() {
    return Mono.<Void>fromRunnable(this::flushNow).subscribeOn(Schedulers.boundedElastic());
}
```

`flushNow()` calls `log.renew(writer, LEASE)` and then
`log.commit(...)`. Reactor's `boundedElastic` runs the task on a
`ScheduledThreadPoolExecutor`, and disposing the subscription **cancels
the task with interruption**. A caller that cancels a flush — a request
whose subscriber goes away, a `Mono.using` cleanup, a timeout —
therefore interrupts the thread while it is inside `renew` or `commit`.

That is worse than a lost write: the durable outcome becomes *unknown*.
A commit that was interrupted halfway may or may not have been applied,
and automatically retrying a partially applied commit is not safe.

`finish()` had the same shape for `close()` (`.then(...)` plus the
`onErrorResume` path), so a cancellation could also interrupt
`log.release(writer)`.

## Change

Storage work is now submitted so that **only the signal can be
cancelled**: the runnable runs to completion on the elastic pool, and
the returned `Mono` still reports completion, failure or cancellation to
its subscriber as before.

```java
private static Mono<Void> uninterruptible(Runnable work) {
    return Mono.<Void>create(
            sink -> Schedulers.boundedElastic().schedule(() -> {
                try {
                    work.run();
                    sink.success();
                } catch (Throwable error) {
                    sink.error(error);
                }
            }));
}
```

`flush()` and both `close()` call sites in `finish()` use it. No
signature, sequencing or error-propagation change beyond "cancelling no
longer aborts an in-flight write".

I deliberately did **not** touch the second problem in the report (a
cancelled initialization can leave the recorder's writer lease held).
That is a lifecycle question about when `close()` is guaranteed to run,
and it deserves its own change rather than being folded in here.

## Regression


`SessionRecorderFlushCancellationTest.disposingFlushDoesNotInterruptJournalIo`
wraps the journal in a spy whose `renew` blocks on a latch, queues one
event, subscribes to `flush()`, disposes the subscription while the
journal call is in flight, and then asserts:

- the thread executing journal I/O was **not** interrupted;
- the queued event still reached the journal.

Before the change it fails with:

```
disposing flush interrupted the thread executing journal I/O ==> expected: <false> but was: <true>
```

## Verification

- `mvn -B -pl agentscope-core -Dtest='io.agentscope.core.session.*Test'
test`: 23 tests, 0 failures.
- `mvn -B -pl agentscope-core test`: 2959 tests, 0 failures, 0 errors
other than the two pre-existing `DangerousPathBypassTest` Windows
symlink errors, which also fail on unmodified `main`.
- `mvn -B -pl agentscope-core spotless:check`: BUILD SUCCESS.

Addresses the first problem in #3815.

### Maintainer integration validation

Merged `main` at `9d4f86426470f06a5b15e0788efd4bad5eab6253` into this
original PR branch. Resolved the add/add test conflict with #4046 by
retaining its `SessionRecorderCancellationTest` and naming this PR's
original regression `SessionRecorderFlushCancellationTest`; no
production behavior was added beyond the original cancellation-safe
storage change.

On integration commit `b82ed745f75be301a732853dc09df15c56e08d6f`, `mvn
-B -T1C -pl agentscope-core -am test spotless:check` passed locally on
JDK 21 (source release 17): 3250 tests, 0 failures/errors, 11 skipped.
Skips are 3 Windows-only tests, 1 opt-in structured-output race test,
and 7 pre-existing disabled stream-contract tests. Both cancellation
suites ran (11 checkpoint cases and 1 flush-cancellation case), as did
the 4 native tool cancellation cases. Full repository verification and
Windows execution are left to the new CI run.

Co-authored-by: juruoO-o <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P0 重要紧急

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: native session 取消 checkpoint 丢失已完成工具结果,恢复时重复执行

3 participants