feat: variable value history with LRU rotation #25

Closed
opened 2026-06-01 12:52:54 +00:00 by xiaoju · 0 comments
Owner

What

每个 variable 记录最近 MAX_HISTORY (默认 10) 个历史值,支持查询和回滚。

Design

Storage

新增 variable_history 表:

CREATE TABLE variable_history (
  variable_name TEXT NOT NULL,
  variable_schema TEXT NOT NULL,
  value TEXT NOT NULL,
  position INTEGER NOT NULL,  -- 0 = current
  set_at INTEGER NOT NULL,
  PRIMARY KEY (variable_name, variable_schema, position),
  FOREIGN KEY (variable_name, variable_schema) REFERENCES variables(name, schema) ON DELETE CASCADE
);

set() 逻辑

当 varStore.set(name, newValue) 时:

  1. 查 history 中是否已有 newValue(position 0..MAX_HISTORY-1)
  2. 已存在:将其从原位置移除,其他项重排,newValue 插入 position 0
  3. 不存在:所有 position +1,newValue 插入 position 0,删除 position >= MAX_HISTORY
  4. variables.value 始终等于 history position 0

API

  • varStore.history(name, schema): Hash[] — 返回值数组,[0] 是当前值
  • varStore.rollback(name, schema, position) — 将指定 position 的值轮换到 [0]

CLI

  • ocas var history <name> [--schema <hash-or-name>] — 显示历史
  • ocas var rollback <name> <position> [--schema <hash-or-name>] — 回滚到指定版本

常量

  • MAX_HISTORY = 10(exported,可配置空间留好)

Acceptance

  • bun test all pass
  • 新增 history/rollback 单元测试 + e2e 测试
  • bootstrap 场景:首次写入创建 history[0],重复 bootstrap 幂等不增加历史

小橘 🍊(NEKO Team)

## What 每个 variable 记录最近 MAX_HISTORY (默认 10) 个历史值,支持查询和回滚。 ## Design ### Storage 新增 `variable_history` 表: ```sql CREATE TABLE variable_history ( variable_name TEXT NOT NULL, variable_schema TEXT NOT NULL, value TEXT NOT NULL, position INTEGER NOT NULL, -- 0 = current set_at INTEGER NOT NULL, PRIMARY KEY (variable_name, variable_schema, position), FOREIGN KEY (variable_name, variable_schema) REFERENCES variables(name, schema) ON DELETE CASCADE ); ``` ### set() 逻辑 当 varStore.set(name, newValue) 时: 1. 查 history 中是否已有 newValue(position 0..MAX_HISTORY-1) 2. **已存在**:将其从原位置移除,其他项重排,newValue 插入 position 0 3. **不存在**:所有 position +1,newValue 插入 position 0,删除 position >= MAX_HISTORY 4. `variables.value` 始终等于 history position 0 ### API - `varStore.history(name, schema): Hash[]` — 返回值数组,[0] 是当前值 - `varStore.rollback(name, schema, position)` — 将指定 position 的值轮换到 [0] ### CLI - `ocas var history <name> [--schema <hash-or-name>]` — 显示历史 - `ocas var rollback <name> <position> [--schema <hash-or-name>]` — 回滚到指定版本 ### 常量 - `MAX_HISTORY = 10`(exported,可配置空间留好) ## Acceptance - `bun test` all pass - 新增 history/rollback 单元测试 + e2e 测试 - bootstrap 场景:首次写入创建 history[0],重复 bootstrap 幂等不增加历史 小橘 🍊(NEKO Team)
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: shazhou/ocas#25