多个任务之间有依赖关系怎么办?
Java实现代码
public class TaskSystem {
// --- 配置 ---
private static final Path WORKDIR = Paths.get(System.getProperty("user.dir"));
private static final Path TASKS_DIR = WORKDIR.resolve(".tasks"); // 任务存储目录
private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
// --- 工具枚举---
public enum ToolType {
BASH("bash", "Run a shell command."),
READ_FILE("read_file", "Read file contents."),
WRITE_FILE("write_file", "Write content to file."),
EDIT_FILE("edit_file", "Replace exact text in file."),
TASK_CREATE("task_create", "Create a new task."), // 新:创建任务
TASK_GET("task_get", "Get full details of a task by ID."), // 新增:获取任务细节
TASK_UPDATE("task_update", "Update a task's status or dependencies."), // 新增:更新任务
TASK_LIST("task_list", "List all tasks with status summary."); // 新:列出任务
public final String name;
public final String description;
ToolType(String name, String description) { this.name = name; this.description = description; }
}
// ... 省略相同的 ToolExecutor 接口
// --- 任务管理器 ---
static class TaskManager {
private final Path tasksDir;
private int nextId = 1;
public TaskManager(Path tasksDir) throws IOException {
this.tasksDir = tasksDir;
Files.createDirectories(tasksDir);
this.nextId = getMaxId() + 1; // 下一个ID自动计算
}
private int getMaxId() {
// 扫描现有任务文件,找到最大ID
try {
return Files.list(tasksDir)
.filter(p -> p.getFileName().toString().startsWith("task_"))
.map(p -> {
try {
String name = p.getFileName().toString();
return Integer.parseInt(name.substring(5, name.length() - 5)); // task_xxx.json
} catch (NumberFormatException e) {
return 0;
}
})
.max(Integer::compare)
.orElse(0);
} catch (IOException e) {
return 0;
}
}
private Map<String, Object> loadTask(int taskId) throws IOException {
Path path = tasksDir.resolve("task_" + taskId + ".json");
if (!Files.exists(path)) {
throw new IllegalArgumentException("Task " + taskId + " not found");
}
String content = Files.readString(path);
Type type = new TypeToken<Map<String, Object>>(){}.getType();
return gson.fromJson(content, type);
}
private void saveTask(Map<String, Object> task) throws IOException {
int id = ((Double) task.get("id")).intValue();
Path path = tasksDir.resolve("task_" + id + ".json");
Files.writeString(path, gson.toJson(task)); // JSON格式存储
}
public String createTask(String subject, String description) throws IOException {
Map<String, Object> task = new LinkedHashMap<>();
task.put("id", nextId);
task.put("subject", subject);
task.put("description", description != null ? description : "");
task.put("status", "pending"); // 默认状态
task.put("blockedBy", new ArrayList<Integer>()); // 被什么任务阻塞
task.put("blocks", new ArrayList<Integer>()); // 阻塞哪些任务
task.put("owner", ""); // 任务负责人
saveTask(task);
nextId++;
return gson.toJson(task);
}
public String getTask(int taskId) throws IOException {
return gson.toJson(loadTask(taskId));
}
public String updateTask(int taskId, String status,
List<Integer> addBlockedBy, List<Integer> addBlocks) throws IOException {
Map<String, Object> task = loadTask(taskId);
if (status != null) {
if (!Arrays.asList("pending", "in_progress", "completed").contains(status)) {
throw new IllegalArgumentException("Invalid status: " + status);
}
task.put("status", status);
// 在完成任务时,从其他任务开始 blockedBy 中移除
if ("completed".equals(status)) {
clearDependency(taskId);
}
}
if (addBlockedBy != null && != null && !addBlockedBy.isEmpty()) {
@SuppressWarnings("unchecked")
List<Integer> currentBlockedBy = (List<Integer>) task.get("blockedBy");
List<Integer> newList = new ArrayList<>(currentBlockedBy);
newList.addAll(addBlockedBy);
task.put("blockedBy", newList.stream().distinct().collect(Collectors.toList()));
}
if (addBlocks != null && !addBlocks.isEmpty()) {
@SuppressWarnings("unchecked")
List<Integer> currentBlocks = (List<Integer>) task.get("blocks");
List<Integer> newList = new ArrayList<>(currentBlocks);
newList.addAll(addBlocks);
List<Integer> distinctBlocks = newList.stream().distinct().collect(Collectors.toList());
task.put("blocks", distinctBlocks);
// 双向更新:更新堵塞任务 blockedBy 列表
for (int blockedId : distinctBlocks) {
try {
Map<String, Object> blockedTask = loadTask(blockedId);
@SuppressWarnings("unchecked")
List<Integer> blockedByList = (List<Integer>) blockedTask.get("blockedBy");
if (!blockedByList.contains(taskId)) {
blockedByList.add(taskId);
saveTask(blockedTask);
}
} catch (Exception e) {
// 忽略不存在的任务
}
}
}
saveTask(task);
return gson.toJson(task);
}
private void clearDependency(int completedId) throws IOException {
Files.list(tasksDir)
.filter(p -> p.getFileName().toString().endsWith(".json"))
.forEach(p -> {
try {
String content = Files.readString(p);
Type type = new TypeToken<Map<String, Object>>(){}.getType();
Map<String, Object> task = gson.fromJson(content, type);
@SuppressWarnings("unchecked")
List<Integer> blockedBy = (List<Integer>) task.get("blockedBy");
if (blockedBy != null && blockedBy.contains(completedId)) {
blockedBy.remove(Integer.valueOf(completedId));
Files.writeString(p, gson.toJson(task));
}
} catch (IOException e) {
// 忽略阅读错误
}
});
}
public String listAllTasks() throws IOException {
List<Map<String, Object>> tasks = new ArrayList<>();
Files.list(tasksDir)
.filter(p -> p.getFileName().toString().endsWith(".json"))
.sorted()
.forEach(p -> {
try {
String content = Files.readString(p);
Type type = new TypeToken<Map<String, Object>>(){}.getType();
tasks.add(gson.fromJson(content, type));
} catch (IOException e) {
// 忽略错误
}
});
if (tasks.isEmpty()) {
return "No tasks.";
}
StringBuilder sb = new StringBuilder();
for (Map<String, Object> task : tasks) {
String status = (String) task.get("status");
String marker = switch(status) {
case "pending" -> "[ ]";
case "in_progress" -> "[>]";
case "completed" -> "[x]";
default -> "[?]";
};
int id = ((Double) task.get("id")).intValue();
String subject = (String) task.get("subject");
@SuppressWarnings("unchecked")
List<Integer> blockedBy = (List<Integer>) task.get("blockedBy");
String blockedStr = (blockedBy != null && != null && !blockedBy.isEmpty())
? " (blocked by: " + blockedBy + ")"
: "";
sb.append(String.format("%s #%d: %s%s\n", marker, id, subject, blockedStr));
}
return sb.toString().trim();
}
}
// --- 工具处理器映射 ---
private static final Map<String, ToolExecutor> TOOL_HANDLERS = new HashMap<>();
static {
// 初始化任务管理器
TaskManager taskManager;
try {
taskManager = new TaskManager(TASKS_DIR);
} catch (IOException e) {
throw new RuntimeException("Failed to initialize task manager", e);
}
// ... 省略基础工具注册
// 注册 Task Create 工具
TOOL_HANDLERS.put(ToolType.TASK_CREATE.name, args -> {
String subject = (String) args.get("subject");
String description = (String) args.get("description");
return taskManager.createTask(subject, description);
});
// 注册 Task Get 工具
TOOL_HANDLERS.put(ToolType.TASK_GET.name, args -> {
int taskId = ((Number) args.get("task_id")).intValue();
return taskManager.getTask(taskId);
});
// 注册 Task Update 工具
TOOL_HANDLERS.put(ToolType.TASK_UPDATE.name, args -> {
int taskId = ((Number) args.get("task_id")).intValue();
String status = (String) args.get("status");
@SuppressWarnings("unchecked")
List<Integer> addBlockedBy = (List<Integer>) args.get("addBlockedBy");
@SuppressWarnings("unchecked")
List<Integer> addBlocks = (List<Integer>) args.get("addBlocks");
return taskManager.updateTask(taskId, status, addBlockedBy, addBlocks);
});
// 注册 Task List 工具
TOOL_HANDLERS.put(ToolType.TASK_LIST.name, args -> {
return taskManager.listAllTasks();
});
}
// ... 省略同一工具的实现和主循环
}
核心思想:使用基于文件的任务图,Agent 开始理解任务间的顺序和并行逻辑,成为真正的项目协调者。
企业级任务管理系统架构架构核心理念:Todo管理器从简单的内存升级为持久、结构化的企业任务管理系统,支持复杂的依赖关系、多任务协调和持久的状态,适用于真实的项目管理和合作场景。
// 任务管理器 - 持久的存储架构
static class TaskManager {
private final Path tasksDir; // 任务存储目录
private int nextId = 1; // 自增ID
public TaskManager(Path tasksDir) throws IOException {
this.tasksDir = tasksDir;
Files.createDirectories(tasksDir);
this.nextId = getMaxId() + 1; // 启动时自动计算下一个ID
}
// 文件系统存储:每个任务存储为独立的JSON文件
// 自动ID管理:启动时扫描现有文件,避免ID冲突
// 持久性:重启后不丢失任务状态
}
- 企业级存储:Todo从内存升级为文件系统的持久存储
- 原子操作:各任务独立文件,避免并发问题
- 增量ID:支持大规模任务ID的自动管理
- 灾备恢复:文件存储支持手动备份和恢复
// 创建任务时,初始化完整的数据结构
public String createTask(String subject, String description) throws IOException {
Map<String, Object> task = new LinkedHashMap<>();
task.put("id", nextId);
task.put("subject", subject); // 任务主题
task.put("description", description != null ? description : ""); // 详细描述
task.put("status", "pending"); // 状态:pending/in_progress/completed
task.put("blockedBy", new ArrayList<Integer>()); // 被什么任务阻塞
task.put("blocks", new ArrayList<Integer>()); // 阻塞哪些任务
task.put("owner", ""); // 任务负责人,支持分派
// 结构化任务:包括完整的元数据和关系
// 依赖管理:blockedby和blocks双向记录依赖关系
// 权限控制:owner字段支持任务分配
saveTask(task);
nextId++;
return gson.toJson(task);
}
- 结构化元数据:任务包含丰富的信息字段
- 依赖管理:支持任务之间的阻塞/阻塞关系
- 扩展:保留owner字段支持团队合作
- JSON格式:人类可读,便于调试和手动修改
// 更新任务时自动同步依赖关系
public String updateTask(int taskId, String status,
List<Integer> addBlockedBy, List<Integer> addBlocks) throws IOException {
Map<String, Object> task = loadTask(taskId);
if (status != null) {
task.put("status", status);
// 在完成任务时,从其他任务开始 blockedBy 中移除
if ("completed".equals(status)) {
clearDependency(taskId);
}
}
if (addBlocks != null && !addBlocks.isEmpty()) {
// 双向更新:更新堵塞任务 blockedBy 列表
for (int blockedId : distinctBlocks) {
try {
Map<String, Object> blockedTask = loadTask(blockedId);
@SuppressWarnings("unchecked")
List<Integer> blockedByList = (List<Integer>) blockedTask.get("blockedBy");
if (!blockedByList.contains(taskId)) {
blockedByList.add(taskId);
saveTask(blockedTask);
}
} catch (Exception e) {
// 忽略不存在的任务
}
}
}
// 依赖自动化:更新任务时,自动更新相关任务的依赖关系
// 完成清理:任务完成后,自动清理依赖于其阻塞
// 容错处理:忽略不存在的任务ID
}
- 关系自动维护:在更新任务依赖时,自动更新相关任务
- 完成时清理:任务完成后自动清理堵塞关系
- 容错设计:忽略没有任务的引用
- 数据一致性:确保依赖关系的双向一致性
// 列出所有任务的摘要信息
public String listAllTasks() throws IOException {
List<Map<String, Object>> tasks = new ArrayList<>();
Files.list(tasksDir)
.filter(p -> p.getFileName().toString().endsWith(".json"))
.sorted() // 按文件名排序,通常是ID顺序
.forEach(p -> {
// 逐一加载任务文件
});
StringBuilder sb = new StringBuilder();
for (Map<String, Object> task : tasks) {
String status = (String) task.get("status");
String marker = switch(status) {
case "pending" -> "[ ]";
case "in_progress" -> "[>]";
case "completed" -> "[x]";
default -> "[?]";
};
int id = ((Double) task.get("id")).intValue();
String subject = (String) task.get("subject");
@SuppressWarnings("unchecked")
List<Integer> blockedBy = (List<Integer>) task.get("blockedBy");
String blockedStr = (blockedBy != null && !blockedBy.isEmpty())
? " (blocked by: " + blockedBy + ")"
: "";
// 状态可视化:[ ]待办 [>]进行中 [x]已完成
// 依赖提示:显示哪些任务阻止了当前任务?
// 简单摘要:只显示关键信息
sb.append(String.format("%s #%d: %s%s\n", marker, id, subject, blockedStr));
}
return sb.toString().trim();
}
- 状态可视化:用图标清晰显示任务状态
- 依赖性提示:明确显示阻塞关系
- 批量加载:高效加载所有任务
- 人性化格式:易于人类阅读和理解
// 完整的任务工具集定义
public enum ToolType {
TASK_CREATE("task_create", "Create a new task."), // CRUD: Create
TASK_GET("task_get", "Get full details of a task by ID."), // CRUD: Read
TASK_UPDATE("task_update", "Update a task's status or dependencies."), // CRUD: Update
TASK_LIST("task_list", "List all tasks with status summary."); // CRUD: List
// 完整的CRUD:创建、读取、更新、删除(完成更新状态)
// 语义清晰:每个工具单一职责:
// 与基本工具分离:任务管理工具独立于文件操作工具
}
- 完整的CRUD:提供完整的任务管理操作
- 单一职责:每个工具都有明确的功能
- 语义接口:名称清晰,便于LLM理解
- 分离注意:任务工具与基本文件工具分离
// 任务存储格式示例
private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
private void saveTask(Map<String, Object> task) throws IOException {
int id = ((Double) task.get("id")).intValue();
Path path = tasksDir.resolve("task_" + id + ".json");
Files.writeString(path, gson.toJson(task)); // 美化JSON格式
}
// 标准化格式:每个任务存储格式化的JSON文件
// 命名规范:task_<id>.json
// 人类可读性:美化JSON便于手动查看和编辑
// 可互操作:标准JSON格式支持外部工具处理
- 标准化存储:JSON是一种常见的数据交换格式
- 可读性:美化格式便于调试
- 可扩展:可随时添加新字段
- 可操作性:其他工具可读取任务文件
从 AgentWithTodo 到 TaskSystem 的升级:
维度 AgentWithTodo TaskSystem 存储方式 内存存储 持久的文件系统 依赖管理 无依赖关系 双向依赖管理 数据持久性 重启丢失 永久保存 任务复杂性 简单待办 复杂的项目管理 协同能力 单人使用 支持团队合作 可扩展性 有限 强大