dsh-todo-freshness 0.1.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -6
- package/client.js +28 -3
- package/index.js +99 -19
- package/lib/fold.js +97 -48
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,16 +6,17 @@ DSH 的任务卡片("任务 1 进行中 · 5 待处理")只是**最后一次
|
|
|
6
6
|
|
|
7
7
|
**Host(`index.js`)**
|
|
8
8
|
|
|
9
|
-
- 注册一个会话投影 `todoFreshness
|
|
10
|
-
- 在 `tools/post-execute`
|
|
9
|
+
- 注册一个会话投影 `todoFreshness`,从既有事件里折叠出:上次写清单是什么时候、之后又跑了多少次工具调用、其中有多少次真的改了文件、清单里还剩几项没有结论、这一轮是不是已经结束。
|
|
10
|
+
- 在 `tools/post-execute` 上挂一个建议:当清单里还有未完成项、且从上次回写起已经连续跑了默认 6 次(以及 14 次)工具调用时,往下一步注入一条提醒,要求模型用真实状态整表重写。次数可配置,见下文。
|
|
11
11
|
- 不写 `todo/write`,不把 `pending` 改成 `completed`。没干完就是没干完。
|
|
12
12
|
|
|
13
13
|
**Client(`client.js`)**
|
|
14
14
|
|
|
15
15
|
- 在 `conversation.input.dock` 的 order 1 注册一条提示条,只在两种情况下出现:
|
|
16
|
-
- `stale
|
|
16
|
+
- `stale`:清单里还有未完成项,但已经连续超过阈值次工具调用没有回写;
|
|
17
17
|
- `closedWithUnfinished`:这一轮已经结束,清单却还留着未完成项。
|
|
18
|
-
-
|
|
18
|
+
- 提示条只报测得的事实——"清单 12 分钟前写下,之后有 8 次文件改动"——不说"这些活已经做完了"。哪一项做完了插件无从得知,能从事件里数出来的只有"清单没动、但代码动了",所以它就说这一句。
|
|
19
|
+
- 正常更新清单时它不出现,不会和内置的任务卡片抢位置。鼠标悬停可以看到当前生效的阈值。
|
|
19
20
|
|
|
20
21
|
## 为什么这么设计
|
|
21
22
|
|
|
@@ -26,7 +27,7 @@ DSH 的任务卡片("任务 1 进行中 · 5 待处理")只是**最后一次
|
|
|
26
27
|
| 轮次结束时把遗留项一律标成 `completed` | 面板永远干净 | 没做完的活也显示完成,等于伪造结果 |
|
|
27
28
|
| 如实标注 + 催模型回写 | 面板可能仍然显示未完成 | 但它显示的确实是真话 |
|
|
28
29
|
|
|
29
|
-
这个插件走第二条。清单"对不上"
|
|
30
|
+
这个插件走第二条。清单"对不上"时你看到的是可核对的差距(几分钟前写的、之后改了几次文件),而不是一个看起来还在进行的假进度,也不是一句替你下结论的"还没跟上"。
|
|
30
31
|
|
|
31
32
|
## 配置
|
|
32
33
|
|
|
@@ -37,17 +38,23 @@ DSH 的任务卡片("任务 1 进行中 · 5 待处理")只是**最后一次
|
|
|
37
38
|
- id: todo-freshness
|
|
38
39
|
name: dsh-todo-freshness
|
|
39
40
|
config:
|
|
41
|
+
staleCalls: 10
|
|
40
42
|
nudge: false
|
|
41
43
|
```
|
|
42
44
|
|
|
43
45
|
| 字段 | 默认 | 含义 |
|
|
44
46
|
|---|---|---|
|
|
47
|
+
| `staleCalls` | `6` | 连续多少次工具调用没有 `todo_write` 就算过期。做长任务嫌提示太频繁就调大,比如 `15` |
|
|
48
|
+
| `nudgeAt` | 跟随 `staleCalls` | 在第几次调用时注入提醒,可写多个。默认是 `[staleCalls, staleCalls * 2 + 2]`,即阈值 6 时是 `[6, 14]` |
|
|
45
49
|
| `nudge` | `true` | 是否注入中途提醒。关闭后只保留 Client 侧的标注,不产生任何模型请求 |
|
|
46
50
|
|
|
51
|
+
`staleCalls` 与 `nudgeAt` 的每一项都必须是 ≥ 1 的整数(写成 `"10"` 这样的字符串也认)。取值不合法时会退回默认值,并在插件日志里写一条 warning 说明忽略了哪个字段——不会静默失效。
|
|
52
|
+
|
|
47
53
|
## 限制
|
|
48
54
|
|
|
49
55
|
- 提醒只是建议。模型可以不理会它,这时面板依旧停在旧状态——插件不会替它决定。
|
|
50
|
-
-
|
|
56
|
+
- 阈值是经验值。默认 6 次是按"一次工具调用大概推进一个子任务"估的,实际任务粒度不同的话用 `staleCalls` 调。
|
|
57
|
+
- 触发看的是工具调用总数,提示条里引用的却是文件改动次数。读文件、跑 shell 也算调用但不会改代码,所以在连续跑命令的阶段,计数会先于真实进度到达阈值——把 `staleCalls` 调大就是给这类阶段留余量。
|
|
51
58
|
- 投影 key `todoFreshness` 是本插件私有的;卸载后它从会话状态里消失,不影响内置的 `todos`。
|
|
52
59
|
|
|
53
60
|
## 安装
|
package/client.js
CHANGED
|
@@ -74,6 +74,11 @@ window.__ModuleLoader__.load({
|
|
|
74
74
|
* The staleness note. Renders nothing while the list still matches the work,
|
|
75
75
|
* and one line when the built-in card describes a moment that has passed.
|
|
76
76
|
* `todoFreshness` is this bundle's own projection; `todos` is the built-in one.
|
|
77
|
+
*
|
|
78
|
+
* The line reports what was measured — how long ago the list was written and
|
|
79
|
+
* how many edits landed since — rather than asserting that work "is done".
|
|
80
|
+
* The plugin cannot know which item finished; it can only know that the tree
|
|
81
|
+
* moved while the list stood still, and that is what it says.
|
|
77
82
|
*/
|
|
78
83
|
function FreshnessNote({ useProjection, t }) {
|
|
79
84
|
const freshness = useProjection('todoFreshness')
|
|
@@ -83,27 +88,47 @@ window.__ModuleLoader__.load({
|
|
|
83
88
|
if (unfinished === 0) return null
|
|
84
89
|
const closed = (freshness.closedWithUnfinished ?? 0) > 0
|
|
85
90
|
if (!freshness.stale && !closed) return null
|
|
91
|
+
const edits = freshness.mutationsSinceWrite ?? 0
|
|
92
|
+
const written = freshness.writeTime ?? 0
|
|
93
|
+
const minutes = written > 0 ? Math.max(1, Math.round((Date.now() - written) / 60000)) : 0
|
|
94
|
+
// A quiet list is not evidence of anything, so the note only claims what
|
|
95
|
+
// the counters support; with no edits to point at it falls back to the age.
|
|
96
|
+
const text = closed
|
|
97
|
+
? t('note.closed')
|
|
98
|
+
: edits > 0 && minutes > 0
|
|
99
|
+
? t('note.stale', { edits, minutes })
|
|
100
|
+
: minutes > 0 ? t('note.age', { minutes }) : t('note.plain')
|
|
101
|
+
const threshold = freshness.staleCalls
|
|
86
102
|
return h('section', {
|
|
87
103
|
style: styles.root,
|
|
88
104
|
'data-todo-freshness': closed ? 'closed' : 'stale',
|
|
105
|
+
// The threshold is configurable per profile, so it is worth naming on
|
|
106
|
+
// hover: the note otherwise looks arbitrary to anyone who did not pick it.
|
|
107
|
+
title: Number.isInteger(threshold) ? t('note.hint', { threshold }) : undefined,
|
|
89
108
|
role: 'status',
|
|
90
109
|
},
|
|
91
110
|
h('div', { style: styles.body },
|
|
92
111
|
h('span', { style: styles.glyph }, h(Glyph)),
|
|
93
|
-
h('span', { style: styles.text },
|
|
112
|
+
h('span', { style: styles.text }, text),
|
|
94
113
|
h('span', { style: styles.count }, t('note.count', { unfinished }))))
|
|
95
114
|
}
|
|
96
115
|
|
|
97
116
|
const dictionaries = {
|
|
98
117
|
zh: {
|
|
99
|
-
'note.stale': '
|
|
118
|
+
'note.stale': '清单 {minutes} 分钟前写下,之后有 {edits} 次文件改动',
|
|
119
|
+
'note.age': '清单已经 {minutes} 分钟没有回写',
|
|
120
|
+
'note.plain': '这份清单可能已经落后于进度',
|
|
100
121
|
'note.closed': '这一轮已经结束,清单停在旧状态',
|
|
101
122
|
'note.count': '还有 {unfinished} 项没有结论',
|
|
123
|
+
'note.hint': '连续 {threshold} 次文件改动没有回写时提示',
|
|
102
124
|
},
|
|
103
125
|
en: {
|
|
104
|
-
'note.stale': '
|
|
126
|
+
'note.stale': 'List written {minutes}m ago, {edits} file edits since',
|
|
127
|
+
'note.age': 'List not rewritten for {minutes} minutes',
|
|
128
|
+
'note.plain': 'This list may be behind the work',
|
|
105
129
|
'note.closed': 'This turn ended and the list still shows old statuses',
|
|
106
130
|
'note.count': '{unfinished} without a verdict',
|
|
131
|
+
'note.hint': 'shown after {threshold} file edits without a rewrite',
|
|
107
132
|
},
|
|
108
133
|
}
|
|
109
134
|
|
package/index.js
CHANGED
|
@@ -14,24 +14,39 @@
|
|
|
14
14
|
*/
|
|
15
15
|
|
|
16
16
|
import { createUserMessage } from '@deepseek-ai/dsh-llm'
|
|
17
|
-
import {
|
|
17
|
+
import {
|
|
18
|
+
anySchema,
|
|
19
|
+
createFold,
|
|
20
|
+
defaultNudgeAt,
|
|
21
|
+
emptyState,
|
|
22
|
+
STALE_CALLS,
|
|
23
|
+
unfinished,
|
|
24
|
+
} from './lib/fold.js'
|
|
18
25
|
|
|
19
26
|
/**
|
|
20
|
-
* The reminder both thresholds send
|
|
21
|
-
*
|
|
27
|
+
* The reminder both thresholds send. It states the measured fact — how many file
|
|
28
|
+
* edits landed since the list was written — instead of claiming the list is
|
|
29
|
+
* wrong. A claim the model can check is likelier to be acted on than an
|
|
30
|
+
* assertion it may disagree with.
|
|
31
|
+
* @param calls - tool calls since the list was last written.
|
|
32
|
+
* @param edits - file edits among those calls.
|
|
22
33
|
*/
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
34
|
+
function nudgeText(calls, edits) {
|
|
35
|
+
return `The todo list has not been rewritten in ${calls} tool calls (${edits} of them edited files). `
|
|
36
|
+
+ 'If the work has moved on, call `todo_write` with the COMPLETE list and the true status of every item. '
|
|
37
|
+
+ 'Do not mark unfinished work `completed` just to clear the panel.'
|
|
38
|
+
}
|
|
26
39
|
|
|
27
40
|
/**
|
|
28
41
|
* The advisory nudge, stamped with a producer source so history shows it as
|
|
29
42
|
* injected context rather than as something the user said.
|
|
43
|
+
* @param calls - tool calls since the list was last written.
|
|
44
|
+
* @param edits - file edits among those calls.
|
|
30
45
|
* @returns a frozen user message carrying one notice.
|
|
31
46
|
*/
|
|
32
|
-
function nudgeMessage() {
|
|
47
|
+
function nudgeMessage(calls, edits) {
|
|
33
48
|
return createUserMessage({
|
|
34
|
-
content: [{ type: 'text', text:
|
|
49
|
+
content: [{ type: 'text', text: nudgeText(calls, edits ?? 0) }],
|
|
35
50
|
source: { kind: 'todo-freshness', form: 'notice', summary: 'todo list out of date' },
|
|
36
51
|
})
|
|
37
52
|
}
|
|
@@ -47,28 +62,93 @@ export const name = 'todo-freshness'
|
|
|
47
62
|
*/
|
|
48
63
|
export const inject = ['sessionProjections']
|
|
49
64
|
|
|
65
|
+
/**
|
|
66
|
+
* A whole number of tool calls, or `undefined` when the value is unusable. A
|
|
67
|
+
* profile config is hand-written YAML, so a string, a fraction, or a negative
|
|
68
|
+
* number is a typo rather than an instruction; falling back to the default
|
|
69
|
+
* keeps a bad value from turning the reminder into something that fires on
|
|
70
|
+
* every single call.
|
|
71
|
+
* @param value - candidate value from the deployment config.
|
|
72
|
+
* @param min - smallest accepted value.
|
|
73
|
+
* @returns the normalized integer, or `undefined`.
|
|
74
|
+
*/
|
|
75
|
+
function wholeNumber(value, min) {
|
|
76
|
+
const number = typeof value === 'string' ? Number(value) : value
|
|
77
|
+
if (typeof number !== 'number' || !Number.isFinite(number)) return undefined
|
|
78
|
+
const whole = Math.floor(number)
|
|
79
|
+
return whole >= min ? whole : undefined
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Read the deployment config. Every key is optional so the defaults described in
|
|
84
|
+
* the README hold when a profile says nothing.
|
|
85
|
+
* @param config - raw Cordis row config.
|
|
86
|
+
* @returns normalized settings.
|
|
87
|
+
*/
|
|
88
|
+
export function settings(config) {
|
|
89
|
+
const staleCalls = wholeNumber(config?.staleCalls, 1) ?? STALE_CALLS
|
|
90
|
+
|
|
91
|
+
// An explicit list wins; otherwise the schedule follows the threshold, so
|
|
92
|
+
// widening `staleCalls` does not silently leave the reminders at the old,
|
|
93
|
+
// now-unreachable counts.
|
|
94
|
+
const declared = Array.isArray(config?.nudgeAt) ? config.nudgeAt : undefined
|
|
95
|
+
const parsed = declared === undefined
|
|
96
|
+
? undefined
|
|
97
|
+
: declared.map(value => wholeNumber(value, 1)).filter(value => value !== undefined)
|
|
98
|
+
const nudgeAt = parsed !== undefined && parsed.length > 0
|
|
99
|
+
? [...new Set(parsed)].sort((left, right) => left - right)
|
|
100
|
+
: declared === undefined ? defaultNudgeAt(staleCalls) : []
|
|
101
|
+
|
|
102
|
+
return {
|
|
103
|
+
staleCalls,
|
|
104
|
+
nudgeAt,
|
|
105
|
+
nudge: config?.nudge !== false,
|
|
106
|
+
// Names of the config keys that were present but unusable. Reported once at
|
|
107
|
+
// activation so a typo is visible instead of silently ignored.
|
|
108
|
+
ignored: [
|
|
109
|
+
...(config?.staleCalls !== undefined && wholeNumber(config.staleCalls, 1) === undefined ? ['staleCalls'] : []),
|
|
110
|
+
...(declared !== undefined && nudgeAt.length === 0 ? ['nudgeAt'] : []),
|
|
111
|
+
],
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
50
115
|
/**
|
|
51
116
|
* Register the freshness projection and the mid-turn nudge.
|
|
52
117
|
* @param ctx - registrant context carrying the projection and event registries.
|
|
53
|
-
* @param config - deployment config
|
|
54
|
-
* drops the reminder.
|
|
118
|
+
* @param config - deployment config: `staleCalls`, `nudgeAt`, `nudge`.
|
|
55
119
|
* @returns when the projection is installed.
|
|
56
120
|
*/
|
|
57
121
|
export async function apply(ctx, config) {
|
|
122
|
+
const chosen = settings(config)
|
|
123
|
+
if (chosen.ignored.length > 0) {
|
|
124
|
+
ctx.logger?.warn?.(`${name}: ignoring ${chosen.ignored.join(', ')} (expected a whole number >= 1); `
|
|
125
|
+
+ `using staleCalls=${chosen.staleCalls}${chosen.nudgeAt.length > 0 ? `, nudgeAt=${chosen.nudgeAt.join(',')}` : ''}`)
|
|
126
|
+
}
|
|
127
|
+
|
|
58
128
|
const handle = await ctx.sessionProjections.register({
|
|
59
129
|
key: 'todoFreshness',
|
|
60
130
|
stateSchema: anySchema,
|
|
61
131
|
init: () => emptyState(),
|
|
62
|
-
apply:
|
|
63
|
-
|
|
64
|
-
|
|
132
|
+
apply: createFold(chosen.staleCalls),
|
|
133
|
+
// The threshold is a deployment choice, so it rides the published view
|
|
134
|
+
// rather than the stored state: every session uses the same value, and a
|
|
135
|
+
// profile change should not require rewriting checkpoints.
|
|
136
|
+
wire: {
|
|
137
|
+
viewSchema: anySchema,
|
|
138
|
+
view: state => ({ ...state, staleCalls: chosen.staleCalls }),
|
|
139
|
+
summary: false,
|
|
140
|
+
},
|
|
141
|
+
// v2 counts edits instead of calls and carries the list's write time. An
|
|
142
|
+
// older checkpoint has neither field, and arithmetic on a missing counter
|
|
143
|
+
// yields NaN, which would silently disable the whole projection.
|
|
144
|
+
stateVersion: 2,
|
|
65
145
|
})
|
|
66
146
|
|
|
67
|
-
if (
|
|
147
|
+
if (!chosen.nudge || chosen.nudgeAt.length === 0) return
|
|
68
148
|
|
|
69
|
-
// Process-local dedup: the
|
|
70
|
-
//
|
|
71
|
-
//
|
|
149
|
+
// Process-local dedup: the edit count already reminded at, per live Agent. The
|
|
150
|
+
// projection is a pure fold and must not record that a nudge went out, or the
|
|
151
|
+
// note would change shape depending on who read it.
|
|
72
152
|
const nudged = new WeakMap()
|
|
73
153
|
|
|
74
154
|
// The reminder rides the model-facing surface as injected context, which lands
|
|
@@ -82,11 +162,11 @@ export async function apply(ctx, config) {
|
|
|
82
162
|
const state = handle.stateOf(agent.session)
|
|
83
163
|
if (!state.list || unfinished(state) === 0) return decision
|
|
84
164
|
const at = state.callsSinceWrite
|
|
85
|
-
if (!
|
|
165
|
+
if (!chosen.nudgeAt.includes(at) || nudged.get(agent) === at) return decision
|
|
86
166
|
nudged.set(agent, at)
|
|
87
167
|
return {
|
|
88
168
|
...decision,
|
|
89
|
-
additionalContexts: [...(decision.additionalContexts ?? []), nudgeMessage()],
|
|
169
|
+
additionalContexts: [...(decision.additionalContexts ?? []), nudgeMessage(at, state.mutationsSinceWrite)],
|
|
90
170
|
}
|
|
91
171
|
})
|
|
92
172
|
}
|
package/lib/fold.js
CHANGED
|
@@ -6,12 +6,32 @@
|
|
|
6
6
|
* @module dsh-todo-freshness/fold
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
/**
|
|
9
|
+
/** Default tool calls without a `todo/write` after which the list counts as stale. */
|
|
10
10
|
export const STALE_CALLS = 6
|
|
11
11
|
|
|
12
|
-
/**
|
|
12
|
+
/** Default tool-call counts, measured from the last write, that each earn one nudge. */
|
|
13
13
|
export const NUDGE_AT = [6, 14]
|
|
14
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Tool calls that edit files. Counted for the note's evidence only — never for
|
|
17
|
+
* the staleness trigger — so that a list can be described as "written 5 minutes
|
|
18
|
+
* and 8 edits ago" rather than with a call count nobody can verify. Shell calls
|
|
19
|
+
* are deliberately absent: `rm` and `sed` are indistinguishable from `ls` at
|
|
20
|
+
* this layer, so counting them would overstate what actually changed.
|
|
21
|
+
*/
|
|
22
|
+
export const MUTATING_TOOLS = new Set(['edit', 'write', 'apply_patch', 'multiedit', 'multi_edit'])
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The nudge schedule that follows a given staleness threshold: the flip point
|
|
26
|
+
* itself, then roughly twice as far out for a list that stalled again after
|
|
27
|
+
* being rewritten. `defaultNudgeAt(6)` returns {@link NUDGE_AT}.
|
|
28
|
+
* @param staleCalls - the configured staleness threshold.
|
|
29
|
+
* @returns ascending tool-call counts, each earning one reminder.
|
|
30
|
+
*/
|
|
31
|
+
export function defaultNudgeAt(staleCalls) {
|
|
32
|
+
return [staleCalls, staleCalls * 2 + 2]
|
|
33
|
+
}
|
|
34
|
+
|
|
15
35
|
/**
|
|
16
36
|
* State before the session's first list write.
|
|
17
37
|
* @param turn - the turn being opened, or 0 before any.
|
|
@@ -27,8 +47,10 @@ export function emptyState(turn = 0) {
|
|
|
27
47
|
active: 0,
|
|
28
48
|
pending: 0,
|
|
29
49
|
writeSeq: -1,
|
|
50
|
+
writeTime: 0,
|
|
30
51
|
callsSinceWrite: 0,
|
|
31
52
|
stepsSinceWrite: 0,
|
|
53
|
+
mutationsSinceWrite: 0,
|
|
32
54
|
closedWithUnfinished: 0,
|
|
33
55
|
stale: false,
|
|
34
56
|
}
|
|
@@ -42,67 +64,94 @@ export function unfinished(state) {
|
|
|
42
64
|
/**
|
|
43
65
|
* Recompute the derived `stale` flag. Returns a new object only when the flag
|
|
44
66
|
* actually moves, so an unchanged list does not republish.
|
|
67
|
+
*
|
|
68
|
+
* The trigger stays on total tool calls, which is what `staleCalls` has meant
|
|
69
|
+
* since 0.1.0 — redefining it as edits would silently tighten every profile that
|
|
70
|
+
* already set a value. Edits are counted alongside it, but only as evidence the
|
|
71
|
+
* note can quote: "8 edits since this list was written" is something the reader
|
|
72
|
+
* can check in the transcript, while "20 calls" is not.
|
|
45
73
|
* @param state - state carrying the latest counters.
|
|
74
|
+
* @param staleCalls - tool calls without a write that make the list stale.
|
|
46
75
|
* @returns the state with a correct `stale` flag.
|
|
47
76
|
*/
|
|
48
|
-
export function settled(state) {
|
|
77
|
+
export function settled(state, staleCalls = STALE_CALLS) {
|
|
49
78
|
const stale = state.list && unfinished(state) > 0 && state.turnOpen
|
|
50
|
-
&& state.callsSinceWrite >=
|
|
79
|
+
&& state.callsSinceWrite >= staleCalls
|
|
51
80
|
if (stale === state.stale) return state
|
|
52
81
|
return { ...state, stale }
|
|
53
82
|
}
|
|
54
83
|
|
|
55
84
|
/**
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
* @param
|
|
60
|
-
* @returns the
|
|
85
|
+
* Build a fold bound to one staleness threshold. The threshold is a deployment
|
|
86
|
+
* choice, not session data, so it stays in this closure instead of the published
|
|
87
|
+
* state: the wire shape is the same whichever value a profile configures.
|
|
88
|
+
* @param staleCalls - mutating calls without a write that make the list stale.
|
|
89
|
+
* @returns the fold for that threshold.
|
|
61
90
|
*/
|
|
62
|
-
export function
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
91
|
+
export function createFold(staleCalls = STALE_CALLS) {
|
|
92
|
+
/**
|
|
93
|
+
* One session's freshness fold. An event it does not care about returns the
|
|
94
|
+
* same reference, so an idle session publishes nothing and stores nothing.
|
|
95
|
+
* @param state - previous projection state.
|
|
96
|
+
* @param event - the next committed session event.
|
|
97
|
+
* @returns the next state.
|
|
98
|
+
*/
|
|
99
|
+
return function fold(state, event) {
|
|
100
|
+
switch (event.type) {
|
|
101
|
+
case 'turn/start':
|
|
102
|
+
return emptyState(event.data.turn)
|
|
103
|
+
case 'turn/end': {
|
|
104
|
+
if (!state.turnOpen && state.turn === event.data.turn) return state
|
|
105
|
+
return {
|
|
106
|
+
...state,
|
|
107
|
+
turnOpen: false,
|
|
108
|
+
stale: false,
|
|
109
|
+
closedWithUnfinished: state.list ? unfinished(state) : 0,
|
|
110
|
+
}
|
|
73
111
|
}
|
|
112
|
+
case 'step/start':
|
|
113
|
+
if (!state.list) return state
|
|
114
|
+
return settled({
|
|
115
|
+
...state,
|
|
116
|
+
step: event.data.step,
|
|
117
|
+
stepsSinceWrite: state.stepsSinceWrite + 1,
|
|
118
|
+
}, staleCalls)
|
|
119
|
+
case 'tool/call': {
|
|
120
|
+
if (!state.list) return state
|
|
121
|
+
const mutates = MUTATING_TOOLS.has(event.data?.name)
|
|
122
|
+
return settled({
|
|
123
|
+
...state,
|
|
124
|
+
callsSinceWrite: state.callsSinceWrite + 1,
|
|
125
|
+
mutationsSinceWrite: state.mutationsSinceWrite + (mutates ? 1 : 0),
|
|
126
|
+
}, staleCalls)
|
|
127
|
+
}
|
|
128
|
+
case 'todo/write': {
|
|
129
|
+
const todos = Array.isArray(event.data?.todos) ? event.data.todos : []
|
|
130
|
+
const count = status => todos.filter(todo => todo.status === status).length
|
|
131
|
+
return settled({
|
|
132
|
+
...state,
|
|
133
|
+
list: todos.length > 0,
|
|
134
|
+
done: count('completed'),
|
|
135
|
+
active: count('in_progress'),
|
|
136
|
+
pending: count('pending'),
|
|
137
|
+
writeSeq: event.seq ?? -1,
|
|
138
|
+
writeTime: event.time ?? 0,
|
|
139
|
+
callsSinceWrite: 0,
|
|
140
|
+
stepsSinceWrite: 0,
|
|
141
|
+
mutationsSinceWrite: 0,
|
|
142
|
+
closedWithUnfinished: 0,
|
|
143
|
+
stale: false,
|
|
144
|
+
}, staleCalls)
|
|
145
|
+
}
|
|
146
|
+
default:
|
|
147
|
+
return state
|
|
74
148
|
}
|
|
75
|
-
case 'step/start':
|
|
76
|
-
if (!state.list) return state
|
|
77
|
-
return settled({
|
|
78
|
-
...state,
|
|
79
|
-
step: event.data.step,
|
|
80
|
-
stepsSinceWrite: state.stepsSinceWrite + 1,
|
|
81
|
-
})
|
|
82
|
-
case 'tool/call':
|
|
83
|
-
if (!state.list) return state
|
|
84
|
-
return settled({ ...state, callsSinceWrite: state.callsSinceWrite + 1 })
|
|
85
|
-
case 'todo/write': {
|
|
86
|
-
const todos = Array.isArray(event.data?.todos) ? event.data.todos : []
|
|
87
|
-
const count = status => todos.filter(todo => todo.status === status).length
|
|
88
|
-
return settled({
|
|
89
|
-
...state,
|
|
90
|
-
list: todos.length > 0,
|
|
91
|
-
done: count('completed'),
|
|
92
|
-
active: count('in_progress'),
|
|
93
|
-
pending: count('pending'),
|
|
94
|
-
writeSeq: event.seq ?? -1,
|
|
95
|
-
callsSinceWrite: 0,
|
|
96
|
-
stepsSinceWrite: 0,
|
|
97
|
-
closedWithUnfinished: 0,
|
|
98
|
-
stale: false,
|
|
99
|
-
})
|
|
100
|
-
}
|
|
101
|
-
default:
|
|
102
|
-
return state
|
|
103
149
|
}
|
|
104
150
|
}
|
|
105
151
|
|
|
152
|
+
/** The fold at the default threshold, for callers that configure nothing. */
|
|
153
|
+
export const fold = createFold()
|
|
154
|
+
|
|
106
155
|
/**
|
|
107
156
|
* Accepts any persisted payload. The state is plain data whose shape only ever
|
|
108
157
|
* grows, so a stale checkpoint row cannot mislead the fold; `stateVersion` is
|
package/package.json
CHANGED