refactor: unified thread storage + resume completed threads #45

Merged
xiaomo merged 3 commits from refactor/39-unified-thread-storage into main 2026-06-04 07:25:57 +00:00
Owner

What

统一 thread 存储 + 支持 resume completed thread(衔尾蛇:end → start)。

Why

  • Active/history 分两个 prefix 是设计债——完成要搬数据,resume 又要搬回来
  • 已完成的 thread 无法 resume,用户丢失上下文

Changes

Phase 1: 统一存储(3 commits)

protocol 包:

  • ThreadIndexEntrystatus + completedAt 字段
  • markThreadCompleted() 新函数
  • createThreadIndexEntry / updateThreadHead / markThreadSuspended 适配新字段

cli 包 store.ts:

  • 所有 thread 统一 @uwf/thread/*,用 tag 表达 status
  • 删除 HISTORY_VAR_PREFIXThreadHistoryLinedeleteThread
  • 新增 loadActiveThreadsloadHistoryThreadscompleteThread
  • 迁移 @uwf/history/*@uwf/thread/*

cli 包 thread.ts:

  • deleteThread + addHistoryEntrycompleteThread()
  • list/show 适配新查询方式
  • shared.ts 去掉 findHistoryEntry fallback

Phase 2: 衔尾蛇 resume

  • uwf thread resume 支持 completed 状态
  • $START 重新进入 workflow graph
  • 清除 completed 状态,保留完整 CAS chain 上下文
  • --supplement 传递新信息
  • cancelled/idle 仍然拒绝 resume

测试

425 tests pass (protocol + cli)

Tag 模型

Tag 说明
status idle | suspended | completed | cancelled 线程状态
suspendedRole role name suspend 时
suspendMessage prompt suspend 时
completedAt timestamp 完成时

Closes #39, #40, #41, #42, #43

## What 统一 thread 存储 + 支持 resume completed thread(衔尾蛇:end → start)。 ## Why - Active/history 分两个 prefix 是设计债——完成要搬数据,resume 又要搬回来 - 已完成的 thread 无法 resume,用户丢失上下文 ## Changes ### Phase 1: 统一存储(3 commits) **protocol 包:** - `ThreadIndexEntry` 加 `status` + `completedAt` 字段 - `markThreadCompleted()` 新函数 - `createThreadIndexEntry` / `updateThreadHead` / `markThreadSuspended` 适配新字段 **cli 包 store.ts:** - 所有 thread 统一 `@uwf/thread/*`,用 tag 表达 status - 删除 `HISTORY_VAR_PREFIX`、`ThreadHistoryLine`、`deleteThread` - 新增 `loadActiveThreads`、`loadHistoryThreads`、`completeThread` - 迁移 `@uwf/history/*` → `@uwf/thread/*` **cli 包 thread.ts:** - `deleteThread + addHistoryEntry` → `completeThread()` - list/show 适配新查询方式 - shared.ts 去掉 `findHistoryEntry` fallback ### Phase 2: 衔尾蛇 resume - `uwf thread resume` 支持 completed 状态 - 从 `$START` 重新进入 workflow graph - 清除 completed 状态,保留完整 CAS chain 上下文 - `--supplement` 传递新信息 - cancelled/idle 仍然拒绝 resume ## 测试 425 tests pass (protocol + cli) ## Tag 模型 | Tag | 值 | 说明 | |-----|------|------| | `status` | `idle \| suspended \| completed \| cancelled` | 线程状态 | | `suspendedRole` | role name | suspend 时 | | `suspendMessage` | prompt | suspend 时 | | `completedAt` | timestamp | 完成时 | Closes #39, #40, #41, #42, #43
xiaoju added 3 commits 2026-06-04 07:14:21 +00:00
- ThreadIndexEntry gains status and completedAt fields
- createThreadIndexEntry defaults to idle/null
- normalizeThreadIndexEntry backward-compat defaults
- updateThreadHead resets to idle (衔尾蛇 resume prep)
- markThreadSuspended sets status=suspended
- New markThreadCompleted(entry, status, now) function
- serializeThreadIndexEntry includes new fields

Part of #39, closes #40
- store.ts: all threads in @uwf/thread/* with status tag
- Remove HISTORY_VAR_PREFIX, ThreadHistoryLine, deleteThread
- Add loadActiveThreads, loadHistoryThreads, completeThread
- Add migrateHistoryVarsToThreadVars migration
- thread.ts: replace deleteThread+addHistoryEntry with completeThread
- shared.ts: remove findHistoryEntry fallback
- Update all tests for unified storage model

422 tests pass.

Part of #39, closes #41, closes #42
uwf thread resume now supports completed threads:
- Evaluates workflow graph from $START to find first role
- Clears completed state (status → idle, completedAt → null)
- Builds resume prompt with supplement context
- Full CAS chain preserved for rich context

Suspended resume behavior unchanged.
Cancelled/idle threads still rejected.

425 tests pass.

Part of #39, closes #43
xiaomo approved these changes 2026-06-04 07:25:55 +00:00
xiaomo left a comment
Owner

LGTM

统一存储 + 衔尾蛇 resume 实现很干净。迁移逻辑幂等安全,测试覆盖全面。

一个小问题不 block:collectCompletedThreadsworkflow: "" — 完成的 thread 在 list 里会显示空 workflow,旧代码从 history entry 带了 workflow hash。可以后续用 resolveWorkflowFromHead 补上。

LGTM ✅ 统一存储 + 衔尾蛇 resume 实现很干净。迁移逻辑幂等安全,测试覆盖全面。 一个小问题不 block:`collectCompletedThreads` 里 `workflow: ""` — 完成的 thread 在 list 里会显示空 workflow,旧代码从 history entry 带了 workflow hash。可以后续用 `resolveWorkflowFromHead` 补上。
xiaomo merged commit bbea89c067 into main 2026-06-04 07:25:57 +00:00
Sign in to join this conversation.
No Reviewers
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: shazhou/united-workforce#45