Skip to content

Commit d698f73

Browse files
authored
Pin the capped own-chat read's window and its count (#2892)
Fixes #2891 `read_own_chat` with no words answers the newest 12 messages of a Bot's chat with the user (`OWN_CHAT_READ`) and says how many it left out, with `more` coming from the chat index's count (`core/src/host/chat-index.js` `latestOwnChat`). No test asserted that heading, so a change that lost the count still left the 12 lines in place and the chat read as a short one. The new row seeds a 20-message chat and pins three facts together: 12 lines shown, the `Your own chat (newest 12 of 20, oldest first)` heading, and the last line the newest message. ### Mutation (the red) The row pins present, correct behaviour, so the red is a mutation: with `latestOwnChat` returning `more: 0`, the heading degrades to `Your own chat (oldest first):` and the row fails. Restored, it passes twice. ### Not already covered `grep -rn 'Your own chat' core/test/*.test.js` finds only the own-chat *brief* sentences in `context-sharing.test.js`; no test asserts the capped read's heading or its `newest N of M` count. ### What I ran - The new file alone, twice, green, and once with the mutation above, red (`Your own chat (oldest first):`). - `TZ=UTC npm run verify` — green (`EXIT=0`). - `node scripts/ds-bot-patches.mjs generate` then `check` — only the new mirror `patches/ds-bot/files/test/own-chat-read-window.test.js` appears; `core/ equals ds-bot/ + patches/ds-bot/`. - `node core/scripts/check-arch-docs.mjs` with `ARCH_DOCS_BASE=7bf75332` — `arch-docs: OK (3 changed file(s))`. - `node .github/scripts/changelog.mjs check` — `22 fragment(s) in changelog.d/ are well formed`. Not run: `smoke` — it cannot start in this checkout (`EROFS` symlinking the pnpm store into `.dsh-test-smoke`), so it is skipped rather than counted green. No `core/client.js`, no existing test and no `*.patch` file is touched: the only generated entry is the new test mirror, which the `core/ equals ds-bot/ + patches/` check requires. Owner: Dev-1 Reviewer: Birfy Written-by: Dev-1 Bot: Dev-1 Claim-Id: fdad58c6acac4449b4c9655f2a5c9f06 Risk tier: T1 Base: `7bf753325a46fa7a5a2777f1f4a560f40927f638` (live `main` at push time) Verified commit: `ae2e501ee0ef10fcc160bd059033d2a74ed8534a` (tree `fad46a48351c5210fd8d70812fba267196e6dc9d`) ## Checkpoint <!-- flybot:checkpoint --> ```json { "pr": 2892, "claim": "Dev-1", "issue": 2891, "summary": "Pins the capped own-chat read: 12 lines, the `Your own chat (newest 12 of 20, oldest first)` heading, and the newest message last. Returning `more: 0` degrades the heading to `Your own chat (oldest first):` and the row fails, so the count cannot be lost silently. Test-only change plus its generated mirror and one changelog fragment.", "files": [ "core/test/own-chat-read-window.test.js", "changelog.d/2891-own-chat-window.changed.md", "patches/ds-bot/files/test/own-chat-read-window.test.js" ], "checks": { "ran": "the new file alone twice, once with the mutation red; TZ=UTC npm run verify; ds-bot-patches generate+check; arch-docs; changelog check", "result": "green", "at": "007f0ed6e0e57d07661c03976c55bd4a0b47e87a" }, "next": "none — ready for review" } ``` <!-- /flybot:checkpoint -->
1 parent a7afa9d commit d698f73

3 files changed

Lines changed: 145 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
A read of a Bot's own chat now has a test for its capped window: the heading says `newest 12 of 20` and the 12 lines end on the newest message.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// `read_own_chat` with no words answers the newest messages of the Bot's chat with the user, capped at
2+
// `OWN_CHAT_READ` (12) per read, and says how many it left out: `Your own chat (newest N of M, oldest
3+
// first)` with `more` coming from the chat index's count over every part (#1906). Nothing pinned that
4+
// heading, so a change that lost the count — or counted a part twice — would still leave the lines in
5+
// place and read as a short chat. This pins the cap, the closing count and the newest line together.
6+
import assert from 'node:assert/strict'
7+
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
8+
import { tmpdir } from 'node:os'
9+
import { join } from 'node:path'
10+
import { test } from 'node:test'
11+
import { createRuntime, provideFlyBot } from '../index.js'
12+
import { fakeContext } from './fake-ctx.js'
13+
14+
const T0 = 1_760_000_000_000
15+
const line = (seq, text, who = 'user') => (who === 'you'
16+
? { type: 'assistant/message', seq, time: T0 + seq * 1000, data: { message: { content: [{ type: 'text', text }] } } }
17+
: { type: 'user/message', seq, time: T0 + seq * 1000, data: { content: [{ type: 'text', text }], source: { kind: 'user' } } })
18+
const HISTORY = { s1: Array.from({ length: 20 }, (_, i) => line(i, `message ${i + 1}`, i % 2 === 0 ? 'user' : 'you')) }
19+
20+
const seed = () => ({
21+
version: 1,
22+
mainBotIds: ['a'],
23+
bots: {
24+
a: { id: 'a', sessionId: 's1', name: 'Solo', role: 'Dev', model: 'deepseek/v4', instructions: '', createdAt: 1 },
25+
},
26+
rooms: {},
27+
exchanges: [],
28+
exchangeTurns: {},
29+
questions: {},
30+
prefs: {},
31+
revision: 1,
32+
})
33+
34+
async function start(t) {
35+
const home = mkdtempSync(join(tmpdir(), 'ds-bot-own-window-'))
36+
writeFileSync(join(home, 'state.json'), JSON.stringify(seed()), { mode: 0o600 })
37+
const ctx = fakeContext()
38+
Object.assign(ctx, {
39+
llm: { listProviders: () => [], listModels: async () => [], resolveModelInfo: async () => ({}) },
40+
sessionController: {
41+
create: async () => ({ sessionId: 'fresh' }),
42+
resolveAgent: async sessionId => ({ agent: { session: { id: sessionId, seq: (HISTORY[sessionId]?.at(-1)?.seq ?? -1) + 1 }, followup() {} } }),
43+
selectModel: async () => {},
44+
page: async ({ address, beforeSeq, maxMessages } = {}) => {
45+
const all = (HISTORY[address?.sessionId] ?? []).filter(event => beforeSeq === undefined || event.seq < beforeSeq)
46+
const records = maxMessages === undefined ? all : all.slice(-maxMessages)
47+
return { records: records.map(event => ({ event })), hasMore: records.length < all.length }
48+
},
49+
},
50+
workspaceRegistry: { get: () => undefined, resolveByPath: async () => undefined, create: async path => ({ id: 'w1', path }), archiveSession: async () => {} },
51+
sessionTitle: { rename: async () => {} },
52+
sessions: { flush: async () => {}, messageProjections: [] },
53+
})
54+
const rt = createRuntime(ctx, { home })
55+
provideFlyBot(ctx, rt.flybot)
56+
t.after(async () => {
57+
await ctx.dispose()
58+
rmSync(home, { recursive: true, force: true, maxRetries: 5, retryDelay: 20 })
59+
})
60+
const tool = (name, args, sessionId) => ctx.registeredTools.get(name).execute(args, { agent: { id: sessionId } })
61+
return { home, ctx, rt, tool }
62+
}
63+
64+
test('a capped own-chat read says how many messages it left out', async (t) => {
65+
const host = await start(t)
66+
const out = await host.tool('read_own_chat', {}, 's1')
67+
const shown = out.split('\n').filter(entry => entry.startsWith('[part 1 #'))
68+
assert.equal(shown.length, 12, `the read shows the 12 the cap allows: ${out.split('\n')[0]}`)
69+
assert.match(out, /^Your own chat \(newest 12 of 20, oldest first\):$/m, `the heading carries the count the cap left out: ${out.split('\n')[0]}`)
70+
assert.match(shown[0], /message 9\b/, 'the window starts at the ninth message, not the first')
71+
assert.match(shown.at(-1), /message 20\b/, 'and ends on the newest one')
72+
})
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// `read_own_chat` with no words answers the newest messages of the Bot's chat with the user, capped at
2+
// `OWN_CHAT_READ` (12) per read, and says how many it left out: `Your own chat (newest N of M, oldest
3+
// first)` with `more` coming from the chat index's count over every part (#1906). Nothing pinned that
4+
// heading, so a change that lost the count — or counted a part twice — would still leave the lines in
5+
// place and read as a short chat. This pins the cap, the closing count and the newest line together.
6+
import assert from 'node:assert/strict'
7+
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
8+
import { tmpdir } from 'node:os'
9+
import { join } from 'node:path'
10+
import { test } from 'node:test'
11+
import { createRuntime, provideFlyBot } from '../index.js'
12+
import { fakeContext } from './fake-ctx.js'
13+
14+
const T0 = 1_760_000_000_000
15+
const line = (seq, text, who = 'user') => (who === 'you'
16+
? { type: 'assistant/message', seq, time: T0 + seq * 1000, data: { message: { content: [{ type: 'text', text }] } } }
17+
: { type: 'user/message', seq, time: T0 + seq * 1000, data: { content: [{ type: 'text', text }], source: { kind: 'user' } } })
18+
const HISTORY = { s1: Array.from({ length: 20 }, (_, i) => line(i, `message ${i + 1}`, i % 2 === 0 ? 'user' : 'you')) }
19+
20+
const seed = () => ({
21+
version: 1,
22+
mainBotIds: ['a'],
23+
bots: {
24+
a: { id: 'a', sessionId: 's1', name: 'Solo', role: 'Dev', model: 'deepseek/v4', instructions: '', createdAt: 1 },
25+
},
26+
rooms: {},
27+
exchanges: [],
28+
exchangeTurns: {},
29+
questions: {},
30+
prefs: {},
31+
revision: 1,
32+
})
33+
34+
async function start(t) {
35+
const home = mkdtempSync(join(tmpdir(), 'ds-bot-own-window-'))
36+
writeFileSync(join(home, 'state.json'), JSON.stringify(seed()), { mode: 0o600 })
37+
const ctx = fakeContext()
38+
Object.assign(ctx, {
39+
llm: { listProviders: () => [], listModels: async () => [], resolveModelInfo: async () => ({}) },
40+
sessionController: {
41+
create: async () => ({ sessionId: 'fresh' }),
42+
resolveAgent: async sessionId => ({ agent: { session: { id: sessionId, seq: (HISTORY[sessionId]?.at(-1)?.seq ?? -1) + 1 }, followup() {} } }),
43+
selectModel: async () => {},
44+
page: async ({ address, beforeSeq, maxMessages } = {}) => {
45+
const all = (HISTORY[address?.sessionId] ?? []).filter(event => beforeSeq === undefined || event.seq < beforeSeq)
46+
const records = maxMessages === undefined ? all : all.slice(-maxMessages)
47+
return { records: records.map(event => ({ event })), hasMore: records.length < all.length }
48+
},
49+
},
50+
workspaceRegistry: { get: () => undefined, resolveByPath: async () => undefined, create: async path => ({ id: 'w1', path }), archiveSession: async () => {} },
51+
sessionTitle: { rename: async () => {} },
52+
sessions: { flush: async () => {}, messageProjections: [] },
53+
})
54+
const rt = createRuntime(ctx, { home })
55+
provideFlyBot(ctx, rt.flybot)
56+
t.after(async () => {
57+
await ctx.dispose()
58+
rmSync(home, { recursive: true, force: true, maxRetries: 5, retryDelay: 20 })
59+
})
60+
const tool = (name, args, sessionId) => ctx.registeredTools.get(name).execute(args, { agent: { id: sessionId } })
61+
return { home, ctx, rt, tool }
62+
}
63+
64+
test('a capped own-chat read says how many messages it left out', async (t) => {
65+
const host = await start(t)
66+
const out = await host.tool('read_own_chat', {}, 's1')
67+
const shown = out.split('\n').filter(entry => entry.startsWith('[part 1 #'))
68+
assert.equal(shown.length, 12, `the read shows the 12 the cap allows: ${out.split('\n')[0]}`)
69+
assert.match(out, /^Your own chat \(newest 12 of 20, oldest first\):$/m, `the heading carries the count the cap left out: ${out.split('\n')[0]}`)
70+
assert.match(shown[0], /message 9\b/, 'the window starts at the ninth message, not the first')
71+
assert.match(shown.at(-1), /message 20\b/, 'and ends on the newest one')
72+
})

0 commit comments

Comments
 (0)