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(plugin): 将派发前拒绝结算为确定失败

Dashdd90bed2c6fb
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/pluginTaskService.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/pluginTaskService.test.ts
index f011a55820..d17135aef6 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/pluginTaskService.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/pluginTaskService.test.ts
@@ -702,7 +702,7 @@ describe('current plugin dispatch authority', () => {
if(point==='route') f.deps.resolveRoute=async()=>{enable();return f.route;};
if(point==='insert') {const insert=f.deps.store.insert;f.deps.store.insert=async row=>{await insert(row);enable();};}
const send=f.service.send('p',{taskId:task.taskId,expectedRevision:task.revision,requestKey:'plan',text:'input'});
- if(point==='insert') await expect(send).resolves.toMatchObject({status:'reconciling'});
+ if(point==='insert') await expect(send).resolves.toMatchObject({status:'failed'});
else await expect(send).rejects.toMatchObject({code:'PERMISSION_DENIED'});
expect(f.deps.dispatch).not.toHaveBeenCalled();
});
@@ -734,7 +734,7 @@ describe('current plugin dispatch authority', () => {
f.deps.store.insert = async row => { await insert(row); mode = 'plan'; };
}
const attempt = f.service.send('p', {taskId:task.taskId,expectedRevision:task.revision,requestKey:'new',text:'input'});
- if (point === 'insert') await expect(attempt).resolves.toMatchObject({status:'reconciling'});
+ if (point === 'insert') await expect(attempt).resolves.toMatchObject({status:'failed'});
else await expect(attempt).rejects.toMatchObject({code:'PERMISSION_DENIED'});
expect(f.deps.dispatch).not.toHaveBeenCalled();
});
@@ -754,4 +754,88 @@ describe('current plugin dispatch authority', () => {
await f.send(); expect(f.deps.dispatch).toHaveBeenCalledTimes(calls);
await expect(f.service.cancel('p',run.runId)).resolves.toMatchObject({status:'cancelled'});
});
+
+ it.each(['plan', 'permission', 'archive', 'read failure'] as const)(
+ 'persists a known pre-dispatch failure after %s without reopening the request key', async change => {
+ const f = fixture(), task = await f.create();
+ f.tasks.get(task.taskId)!.permissionMode = 'auto';
+ const insert = f.deps.store.insert;
+ const readSession = f.deps.readSession;
+ f.deps.store.insert = async row => {
+ await insert(row);
+ if (change === 'plan') f.tasks.get(task.taskId)!.planModeEnabled = true;
+ if (change === 'permission') f.deps.readPermissionMode = () => 'plan';
+ if (change === 'archive') f.tasks.get(task.taskId)!.status = 'archived';
+ if (change === 'read failure') f.deps.readSession = vi.fn()
+ .mockRejectedValueOnce(new Error('Read unavailable')).mockImplementation(readSession);
+ };
+ const request = { taskId: task.taskId, expectedRevision: task.revision, requestKey: 'known', text: 'input' };
+ const run = await f.service.send('p', request);
+ expect(run).toMatchObject({ status: 'failed', error: 'Input was not accepted by the host.' });
+ expect(await f.service.getRun('p', run.runId)).toEqual(run);
+ expect((await f.service.listRuns('p', task.taskId)).items).toEqual([run]);
+ expect(f.deps.inspect).not.toHaveBeenCalled();
+ const restarted = createPluginTaskService(f.deps);
+ expect(await restarted.send('p', request)).toEqual(run);
+ expect(f.deps.dispatch).not.toHaveBeenCalled();
+ // Restored authority permits an explicit new request, never replay of the old input.
+ f.deps.store.insert = insert;
+ Object.assign(f.tasks.get(task.taskId)!, { planModeEnabled: false, status: 'active' });
+ f.deps.readPermissionMode = () => 'auto';
+ expect(await restarted.send('p', request)).toEqual(run);
+ expect(await restarted.send('p', { ...request, requestKey: 'new' })).toMatchObject({ status: 'queued' });
+ expect(f.deps.dispatch).toHaveBeenCalledTimes(1);
+ },
+ );
+
+ it.each(['approval', 'ownership', 'deleted', 'account', 'save'] as const)(
+ 'keeps the request receipt and rejects when %s prevents failure settlement', async change => {
+ const f = fixture(), task = await f.create();
+ const insert = f.deps.store.insert;
+ f.deps.store.insert = async row => {
+ await insert(row);
+ f.tasks.get(task.taskId)!.planModeEnabled = true;
+ if (change === 'approval') f.deps.assertAuthorized = () => { throw new Error('Approval revoked'); };
+ if (change === 'ownership') await f.deps.store.revokePlugin('p');
+ if (change === 'deleted') f.tasks.get(task.taskId)!.status = 'deleted';
+ if (change === 'account') f.switchOwner();
+ if (change === 'save') f.deps.store.save = async () => { throw new Error('Write unavailable'); };
+ };
+ await expect(f.service.send('p', {
+ taskId: task.taskId, expectedRevision: task.revision, requestKey: 'blocked', text: 'input',
+ })).rejects.toThrow();
+ expect(f.deps.dispatch).not.toHaveBeenCalled();
+ const receipts = [...f.rows.values()].filter(row => row.operation === 'send');
+ expect(receipts).toHaveLength(1);
+ expect(JSON.parse(receipts[0].payload).status).toBe('queued');
+ expect(f.deps.store.discardUncreated).not.toHaveBeenCalled();
+ },
+ );
+
+ it.each(['reject', 'throw'] as const)('preserves native progress over a late dispatch %s', async response => {
+ for (const status of ['running', 'stopping', 'completed', 'cancelled'] as const) {
+ const f = fixture();
+ f.deps.dispatch = vi.fn(async (_plugin, taskId, clientId) => {
+ await f.service.accept(taskId, { clientId }, f.execution);
+ if (status === 'stopping' || status === 'cancelled') {
+ f.deps.cancel = async () => status;
+ await f.service.cancel('p', [...f.rows.values()].find(row => row.operation === 'send')!.id);
+ }
+ if (status === 'completed') await f.service.settle(taskId, f.execution, 'completed', 'answer');
+ if (response === 'throw') throw new Error('Late response lost');
+ return { ok: false };
+ });
+ expect(await f.send()).toMatchObject({ status });
+ }
+ });
+
+ it('settles an explicit host rejection as failed', async () => {
+ const f = fixture();
+ f.deps.dispatch = vi.fn(async () => ({ ok: false }));
+ const run = await f.send();
+ expect(run.status).toBe('failed');
+ expect(await f.service.getRun('p', run.runId)).toEqual(run);
+ expect(await f.send()).toEqual(run);
+ expect(f.deps.dispatch).toHaveBeenCalledTimes(1);
+ });
});
diff --git a/apps/desktop/src/main/maker-ipc/pluginTaskService.ts b/apps/desktop/src/main/maker-ipc/pluginTaskService.ts
index 8d19ec6204..1a6ff30480 100644
--- a/apps/desktop/src/main/maker-ipc/pluginTaskService.ts
+++ b/apps/desktop/src/main/maker-ipc/pluginTaskService.ts
@@ -352,10 +352,12 @@ export function createPluginTaskService(deps: PluginTaskServiceDeps) {
});
if (!prepared.dispatch) return prepared.run;
let failure: 'failed' | 'reconciling' | undefined;
+ let dispatchStarted = false;
// Never hold the receipt lock while entering Session dispatch/control. The
// coordinator awaits accept() before vendor dispatch under its own lock.
try {
await assertDispatch(pluginId, prepared.run.taskId);
+ dispatchStarted = true;
const outcome = await deps.dispatch(
pluginId,
prepared.run.taskId,
@@ -364,7 +366,8 @@ export function createPluginTaskService(deps: PluginTaskServiceDeps) {
);
if (!outcome.ok) failure = 'failed';
} catch {
- failure = 'reconciling';
+ // Before entering dispatch, this input is known not to have reached the host.
+ failure = dispatchStarted ? 'reconciling' : 'failed';
}
return exclusive(async () => {
const row = await ownRun(pluginId, prepared.run.runId);
diff --git a/docs/dev-rules/plugin-task-api-implementation.md b/docs/dev-rules/plugin-task-api-implementation.md
index 9d30845afe..b5432703ee 100644
--- a/docs/dev-rules/plugin-task-api-implementation.md
+++ b/docs/dev-rules/plugin-task-api-implementation.md
@@ -13,6 +13,7 @@
- 本次创建在 Session INSERT 开始前明确失败(如配置、目录或工作区准备失败)时,沿现有回执队列撤回仍为原版本、且原始 Session 行不存在的新回执;修正原因后可用原 requestKey 显式重试,不自动重派。回执存储固定原账号数据库,撤销或版本变化的回执不删除。
- 新请求的幂等摘要忽略对象键顺序,保留数组顺序;计划及已受理配置共用相同比较。早期开发版创建回执保留原序列化输入的精确重试兼容;这类旧摘要未保存原始键序,不能承诺首次以不同键序重放成功。
- 回执独立落库,区分任务、输入执行及原生执行代次;原生终态按 instanceId + generation 结算,不以 idle 或最后一条 assistant 消息猜测完成。
+- send 回执写入后,进入宿主派发前的检查拒绝可确定未发送,结算为 failed;进入派发后的异常仍为 reconciling,明确拒绝仍为 failed,已有原生进展优先。同 requestKey 只读取旧回执,权限恢复后需新 key 显式提交。若账号、归属或存储条件不再允许结算,调用仍拒绝并保留回执,不绕过权限门补写终态。
- 读取限定于本插件创建的任务;返回消息 clientId。list/listRuns 按 (createdAt, id) 排序,现有回执 ID 游标在同插件与目标内解析,不再只按随机 UUID 排序。列表是分页查询,不是增量订阅;刷新应从首页重扫,不能把游标当永久同步水位(同毫秒追加、系统时钟回退或既有回执终态更新均需刷新)。
- 插件持有 create 归属的任务及其 Orca Worker 使用既有 restricted `cindy_helper` 能力面,不提供账号级历史、待发消息或跨任务控制;发现与实际调用共用实时 Host 判据,猜工具名也不放行。另一个原始历史入口 `cindy_memory.session_search` 复用同一判据,在查询前拒绝,不伪装为空结果;读取期间账号变化也不返回结果。按创建回执和 Worker→Team→Lead 关系判断,不按 `source=plugin` 一刀切,也不因团队结束而放开。旧 errand/workspace 无回执时保持原行为;明确 `ownershipRevoked=true` 后,调用方已无正在执行的输入或已接受新的 Host 真人输入时,保留的用户任务恢复普通 helper 与历史检索能力;仍执行旧插件输入时继续受限。发现、调用和 memory 检索复用 Orca 的只读来源判据,首个异步查询前固定当前来源、返回前复核;真人重试保留 Host 文本及稳定来源,插件重试不变真人。Lead 自动回报可沿当前可见输入追溯,但 Worker 的新 Lead 指令阻断对更早真人的回溯。损坏回执、未知来源或查询失败保持拒绝;这不恢复插件归属、旧 requestKey 或插件控制权,也不承诺停止已接受执行。独立 Orca 工具不受影响。这只收住宿主 helper 与原始历史检索入口,不是通用执行或文件系统隔离。
- 取消只撤回自己的排队输入或其原生重试别名;停止前在 Session 锁内再次核对执行身份与插件权限。