Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(fork): 保留重建前历史片段的运行设备

Dasheac3e9c34c86
diff --git a/apps/desktop/src/main/__tests__/fork.test.ts b/apps/desktop/src/main/__tests__/fork.test.ts
index b6a28a7b60..b803bb5754 100644
--- a/apps/desktop/src/main/__tests__/fork.test.ts
+++ b/apps/desktop/src/main/__tests__/fork.test.ts
@@ -1339,7 +1339,14 @@ describe('forkSessionAtMessage', () => {
expect(createMessageMock).toHaveBeenCalled();
});
- it('uses the rebuild boundary engine when history crosses a later engine switch', async () => {
+ // Review #6563: native-session routing tests did not cover rebuild-before-switch.
+ // Public fork entry must persist the historical device even without a reusable SDK session (unit).
+ it.each([
+ ['device-b', 'device-c', 'device-b'],
+ [null, 'device-c', null],
+ ['device-b', null, 'device-b'],
+ [undefined, 'device-c', 'device-c'],
+ ])('uses the rebuild boundary engine and historical device %s after moving to %s', async (historicalDevice, currentDevice, expectedDevice) => {
const target = makeMessageRow({
id: 'historical-claude-assistant',
clientId: 'historical-claude-assistant-client',
@@ -1359,6 +1366,7 @@ describe('forkSessionAtMessage', () => {
selectQueue.push([
makeSourceRow({
agentKind: 'codex',
+ agentDeviceId: currentDevice,
model: 'gpt-5.6',
sdkSessionId: 'current-codex-thread',
}),
@@ -1391,6 +1399,7 @@ describe('forkSessionAtMessage', () => {
.mockResolvedValueOnce({
content: JSON.stringify({
fromAgentKind: 'cc',
+ ...(historicalDevice !== undefined ? { fromAgentDeviceId: historicalDevice } : {}),
toAgentKind: 'codex',
fromModel: 'claude-sonnet-4-6',
fromSdkSessionId: null,
@@ -1406,6 +1415,9 @@ describe('forkSessionAtMessage', () => {
};
expect(txArgs.newSession.agentKind).toBe('cc');
expect(txArgs.newSession.model).toBe('claude-sonnet-4-6');
+ expect(txArgs.newSession.agentDeviceId).toBe(expectedDevice);
+ expect(txArgs.newSession.sdkSessionId).toBeNull();
+ expect(remoteInvokeMock).not.toHaveBeenCalled();
expect(forkSdkSessionMock).not.toHaveBeenCalled();
});
it('rejects a historical engine boundary without a parked native session id', async () => {
diff --git a/apps/desktop/src/main/maker-orchestration/fork.ts b/apps/desktop/src/main/maker-orchestration/fork.ts
index af4533e7c6..ae09392011 100644
--- a/apps/desktop/src/main/maker-orchestration/fork.ts
+++ b/apps/desktop/src/main/maker-orchestration/fork.ts
@@ -426,6 +426,12 @@ async function resolveForkNativeSource(
LIMIT 1`,
positionParams,
);
+ const parsedNextSwitch = nextSwitch ? parseAgentSwitchBoundary(nextSwitch.content) : null;
+ // Device ownership belongs to the historical segment, even when a rebuild
+ // invalidated its native session. Explicit null means local; legacy records
+ // without the field retain the current task's device.
+ const agentDeviceId = parsedNextSwitch && Object.hasOwn(parsedNextSwitch, 'fromAgentDeviceId')
+ ? parsedNextSwitch.fromAgentDeviceId ?? null : source.agentDeviceId ?? null;
if (
nextSwitch &&
(!invalidated ||
@@ -434,7 +440,7 @@ async function resolveForkNativeSource(
{ createdAt: invalidated.created_at, rowid: invalidated.rowid },
))
) {
- const parsed = parseAgentSwitchBoundary(nextSwitch.content);
+ const parsed = parsedNextSwitch;
if (!parsed?.fromSdkSessionId) {
throw forkError('UNSUPPORTED_HISTORY', '目标消息对应的历史引擎会话不可用');
}
@@ -471,8 +477,7 @@ async function resolveForkNativeSource(
}
return {
agentKind: parsed.fromAgentKind,
- agentDeviceId: Object.hasOwn(parsed, 'fromAgentDeviceId')
- ? parsed.fromAgentDeviceId ?? null : source.agentDeviceId ?? null,
+ agentDeviceId,
sdkSessionId: parsed.fromSdkSessionId,
model,
providerId,
@@ -484,7 +489,7 @@ async function resolveForkNativeSource(
if (rebuildBoundary?.sourceAgentKind) {
return {
agentKind: rebuildBoundary.sourceAgentKind,
- agentDeviceId: source.agentDeviceId ?? null,
+ agentDeviceId,
sdkSessionId: null,
model: rebuildBoundary.sourceModel ?? source.model,
providerId: Object.hasOwn(rebuildBoundary, 'sourceProviderId')
@@ -503,7 +508,7 @@ async function resolveForkNativeSource(
}
return {
agentKind: normalizeDbAgentKind(source.agentKind),
- agentDeviceId: source.agentDeviceId ?? null,
+ agentDeviceId,
sdkSessionId: null,
model: source.model,
providerId: source.providerId,
@@ -517,7 +522,7 @@ async function resolveForkNativeSource(
}
return {
agentKind: normalizeDbAgentKind(source.agentKind),
- agentDeviceId: source.agentDeviceId ?? null,
+ agentDeviceId,
sdkSessionId: source.sdkSessionId,
model: source.model,
providerId: source.providerId,
diff --git a/docs/dev-rules/protocol-compatibility.md b/docs/dev-rules/protocol-compatibility.md
index 5b120f1350..27238f185a 100644
--- a/docs/dev-rules/protocol-compatibility.md
+++ b/docs/dev-rules/protocol-compatibility.md
@@ -1154,6 +1154,8 @@ handler 无 sender 依赖;不加入共享任务访客白名单,不进入自
任务和消息仍在任务所属电脑落库,新任务保留历史片段的 `agentDeviceId`、供应商、模型和原工作目录。
曾经换过运行电脑时,按 `agent_switch.fromAgentDeviceId` 向保存该片段的电脑分叉,
子任务也继承这一运行位置(显式 `null` 表示本机);旧边界缺少该字段时沿用任务的运行位置。
+历史片段的原生会话因 `context_rebuild` 失效时,既有交接分叉仍沿用这一设备判据;
+重建只决定原生会话能否复用,不把子任务改送到当前设备。
Desktop 与 Mobile 沿用 `maker:fork`;共享任务访客与 SSH 工作区的现有边界不变。
运行端按已认证调用方复核供应商权限;受邀者及转分享还必须拥有来源原生会话。