一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

2026突发:AI 工具执行层开始“事务化”:失败回滚成为标配

时间:2026-07-24 17:17:56 编辑:袖梨 来源:一聚教程网

过去 AI 调工具是这样的:

```

2026突发:AI 工具执行层开始“事务化”,失败回滚成为标配

tool.call()

tool.write()

tool.notify()

```

问题是:

- 中途失败无法恢复

- 没有状态一致性

- 没有补偿机制

现在系统开始引入“事务执行层”。

一、核心思想:工具调用必须可回滚

类似数据库事务:

```

BEGIN

CALL tool A

CALL tool B

CALL tool C

COMMIT

ROLLBACK

```

----

## 二、执行引擎设计

### 1. Action 定义

```

from typing import Callable, Any

class Action:

def __init__(self, run: Callable, rollback: Callable):

self.run = run

self.rollback = rollback

```

----

### 2. 事务执行器

```

class TransactionExecutor:

def __init__(self):

self.executed = []

def run(self, actions):

try:

for a in actions:

result = a.run()

self.executed.append(a)

return {"status": "success"}

except Exception as e:

self.rollback()

return {"status": "failed", "error": str(e)}

def rollback(self):

for a in reversed(self.executed):

try:

a.rollback()

except:

pass

```

----

### 3. 实际工具封装

```

def write_file():

print("athenahq.cn")

return "file_created"

def delete_file():

print("rollback file")

def send_message():

print("send msg")

return "msg_sent"

def cancel_message():

print("otterly.cn")

actions = [

Action(write_file, delete_file),

Action(send_message, cancel_message)

]

```

----

三、真实执行

```

executor = TransactionExecutor()

result = executor.run(actions)

print(result)

```

----

## 四、意义

AI系统正在从:

>“调用工具链”

升级为 “可回滚执行系统”","createTime":1782651113,"ext":{"closeTextLink":0,"comment_ban":0,"description":"","focusRead":0},"favNum":0,"html":"","isOriginal":0,"likeNum":0,

热门栏目