tabbit 0.1.4: a turn's prompt stays under Tabbit's character limit, so long transcripts stop coming back as 492 (#833)

Tabbit refuses a turn whose content is 20,500 characters or more. The answer is code 492 inside the SSE stream, after about 40s. The reporter measured it on 0.1.3: 20,490 characters pass and 20,500 fail. The limit counts characters, not bytes: 61 KB of Chinese at 20,410 characters passed. Concurrency of 2 and 4 didn't reproduce it. An OpenCode agent's system prompt plus the flattened history reaches that size quickly.

flatten now keeps a new room's prompt within 18,000 characters. It keeps, in order, the final user message (at most two thirds), the system prompt (up to 60% of what is left when there are earlier turns), and the earlier turns newest first. A turn that doesn't fit is clipped to its head and tail, or left out with an "[earlier conversation omitted]" note. A continued room's single message is clipped to the same limit.

Run: on a separate test Mac, `npm test` in packages/tabbit passed 88 of 88, and `bun scripts/check.mjs tabbit` was ok.

Broken by hand: with 0.1.3's index.mjs, the 4 new limit tests fail. A property test over system, turn-count and length sizes first caught two budget slips (18,029 and 18,016 characters), both fixed.

Not tried: a real Tabbit turn. I have no Tabbit account. The reporter offered to run their OpenCode case again.

Reported-by: Beyfish <Beyfish@users.noreply.github.com>
This commit is contained in:
yetone
2026-11-13 00:26:53 +08:00
parent a2e601dc21
commit af7b4cc765
4 changed files with 139 additions and 8 deletions
+6
View File
@@ -174,6 +174,12 @@ the room (a new session, an edited history, a room Tabbit reset) gets a fresh
room with the whole transcript flattened into one prompt; measured, the model
answers the last question with the earlier turns as context.
One turn's prompt is at most **18,000 characters**. Tabbit counts characters,
not bytes, and answers a longer one with code 492 after about 40 seconds
(measured at 20,500 characters, #833). A flattened transcript keeps the final
message, then the system prompt, then the newest earlier turns. A message
that is still too long keeps its head and tail.
`maxConcurrency` is **1**: Tabbit answers one turn at a time.
## Working on this package
+69 -7
View File
@@ -100,20 +100,80 @@ function historyOf(messages) {
return out
}
// PROMPT_LIMIT is how long one turn's `content` may be. Tabbit counts
// characters, not bytes: a reporter measured (#833) 20,490 characters go
// through and 20,500 come back as code 492 after ~40s, the same for Chinese
// (61 KB) and ASCII (20 KB) text. 18,000 keeps room under that edge.
const PROMPT_LIMIT = 18_000
// clip keeps a text's head and tail inside max characters, with a note of
// what was left out between them.
function clip(text, max) {
if (text.length <= max) return text
if (max <= 0) return ""
const note = (n) => `\n[… ${n} characters omitted …]\n`
let room = max - note(text.length).length
if (room <= 0) return text.slice(0, max)
const head = Math.ceil(room * 2 / 3)
const tail = room - head
return text.slice(0, head) + note(text.length - room) + (tail ? text.slice(text.length - tail) : "")
}
// flatten writes a transcript as one prompt. Tabbit's turn takes one
// `content` string, and a fresh room has no history of its own, so the
// transcript is what carries it. Measured: the model answers the last
// question with the earlier turns as context.
function flatten(history) {
//
// The prompt stays within limit characters. The final user message comes
// first, then the system prompt (up to 60% of what is left when there are
// earlier turns), then the earlier turns newest first; a turn that doesn't
// fit is clipped or, with the ones before it, left out with a note. A long
// final message is clipped to two thirds of the limit.
function flatten(history, limit = PROMPT_LIMIT) {
const systems = history.filter((m) => m.role === "system").map((m) => m.content)
const turns = history.filter((m) => m.role !== "system")
const lastUser = [...turns].reverse().find((m) => m.role === "user")
const before = lastUser ? turns.slice(0, turns.lastIndexOf(lastUser)) : turns
const lines = []
if (systems.length) lines.push(`[system]\n${systems.join("\n\n")}`)
for (const m of before) lines.push(`${m.role === "assistant" ? "assistant" : "user"}: ${m.content}`)
const header = lines.length ? "Below is the conversation so far. Answer the final user message, continuing it.\n\n" : ""
return header + lines.join("\n\n") + (lines.length ? "\n\n" : "") + `user: ${lastUser?.content ?? ""}`
const header = "Below is the conversation so far. Answer the final user message, continuing it.\n\n"
const omitted = "[earlier conversation omitted]"
const sep = "\n\n"
if (!systems.length && !before.length) return "user: " + clip(lastUser?.content ?? "", limit - 6)
// Room for the omitted note is set aside first: any earlier turn may need it.
const note = before.length ? omitted.length + sep.length : 0
let left = limit - header.length - sep.length - 6
// A long final message takes at most two thirds, so the system prompt
// still has room.
const last = "user: " + clip(lastUser?.content ?? "", Math.max(0, Math.floor((left - note) * 2 / 3)))
left -= last.length - 6 + note
let system = ""
if (systems.length) {
const tag = "[system]\n"
const share = before.length ? Math.floor(left * 0.6) : left
const body = clip(systems.join(sep), Math.max(0, share - tag.length - sep.length))
if (body) { system = tag + body; left -= system.length + sep.length }
}
const kept = []
let dropped = false
for (let i = before.length - 1; i >= 0; i--) {
const m = before[i]
const label = `${m.role === "assistant" ? "assistant" : "user"}: `
const need = label.length + m.content.length + sep.length
if (need <= left) { kept.unshift(label + m.content); left -= need; continue }
const space = left - label.length - sep.length
if (space >= 500) { kept.unshift(label + clip(m.content, space)); left -= label.length + space + sep.length }
dropped = true
break
}
const render = () => {
const lines = []
if (system) lines.push(system)
if (dropped) lines.push(omitted)
lines.push(...kept)
return header + lines.join(sep) + (lines.length ? sep : "") + last
}
let out = render()
while (out.length > limit && kept.length) { kept.shift(); dropped = true; out = render() }
return out
}
// planTurn decides what one request sends: a new room with the flattened
@@ -126,7 +186,7 @@ function planTurn(entry, messages, now = Date.now()) {
const added = history.slice(entry.sent.length)
const lastUser = [...added].reverse().find((m) => m.role === "user")
if (continues && lastUser && added.length <= 2) {
return { room: entry.room, parent: entry.parent, content: lastUser.content, history }
return { room: entry.room, parent: entry.parent, content: clip(lastUser.content, PROMPT_LIMIT), history }
}
}
return { room: null, parent: null, content: flatten(history), history }
@@ -540,6 +600,8 @@ export const _internal = {
planTurn,
historyOf,
flatten,
clip,
PROMPT_LIMIT,
stripModel,
contentOf,
usageWindow,
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-tabbit-auth",
"version": "0.1.3",
"version": "0.1.4",
"description": "Tabbit (web.tabbit.com and web.tabbit.ai) as a magpie / OpenCode provider: live model catalog, cookie sign-in, chat completions, cycle usage and the daily check-in",
"type": "module",
"main": "./opencode.mjs",
+63
View File
@@ -612,3 +612,66 @@ test("rooms are pruned by ttl and capped in size", () => {
assert.ok(_internal.rooms.size <= 256, "size " + _internal.rooms.size)
_internal.rooms.clear()
})
test("a long transcript is flattened under Tabbit's character limit (#833)", () => {
const { flatten, PROMPT_LIMIT } = _internal
assert.ok(PROMPT_LIMIT < 20_490)
const system = "系统提示。".repeat(3000) // 15,000 characters
const history = [{ role: "system", content: system }]
for (let i = 0; i < 40; i++) {
history.push({ role: "user", content: `问题 ${i} ` + "x".repeat(1500) })
history.push({ role: "assistant", content: `回答 ${i} ` + "y".repeat(1500) })
}
history.push({ role: "user", content: "用一句话介绍你自己" })
const out = flatten(history)
assert.ok(out.length <= PROMPT_LIMIT, `prompt is ${out.length} characters`)
assert.ok(out.endsWith("user: 用一句话介绍你自己"))
assert.match(out, /\[system\]\n系统提示。/)
assert.match(out, /characters omitted/)
assert.match(out, /\[earlier conversation omitted\]/)
assert.match(out, /回答 39 /, "the newest earlier turn is kept")
assert.doesNotMatch(out, /问题 0 /, "the oldest turn is the one left out")
})
test("a short transcript is flattened unchanged", () => {
const out = _internal.flatten([
{ role: "system", content: "be brief" },
{ role: "user", content: "我叫小明" },
{ role: "assistant", content: "记住了" },
{ role: "user", content: "我叫什么" },
])
assert.equal(out, "Below is the conversation so far. Answer the final user message, continuing it.\n\n[system]\nbe brief\n\nuser: 我叫小明\n\nassistant: 记住了\n\nuser: 我叫什么")
assert.equal(_internal.flatten([{ role: "user", content: "hi" }]), "user: hi")
})
test("one oversized message keeps its head and tail under the limit", () => {
const { flatten, planTurn, PROMPT_LIMIT } = _internal
const big = "开头" + "z".repeat(40_000) + "结尾"
const out = flatten([{ role: "user", content: big }])
assert.ok(out.length <= PROMPT_LIMIT)
assert.ok(out.startsWith("user: 开头"))
assert.ok(out.endsWith("结尾"))
const entry = { room: "room-1", parent: "a1", sent: [{ role: "user", content: "a" }, { role: "assistant", content: "b" }], at: 1000 }
const next = planTurn(entry, [{ role: "user", content: "a" }, { role: "assistant", content: "b" }, { role: "user", content: big }], 2000)
assert.equal(next.room, "room-1")
assert.ok(next.content.length <= PROMPT_LIMIT)
assert.ok(next.content.endsWith("结尾"))
})
test("every transcript size stays under the limit", () => {
const { flatten, PROMPT_LIMIT } = _internal
for (const sys of [0, 10, 9_000, 30_000]) for (const n of [0, 1, 3, 30]) for (const len of [1, 600, 5_000, 25_000]) {
const h = sys ? [{ role: "system", content: "s".repeat(sys) }] : []
for (let i = 0; i < n; i++) h.push({ role: i % 2 ? "assistant" : "user", content: "t".repeat(len) })
h.push({ role: "user", content: "q".repeat(len) })
const out = flatten(h)
assert.ok(out.length <= PROMPT_LIMIT, `sys=${sys} n=${n} len=${len}: ${out.length}`)
assert.ok(out.includes("q"), "the final question survives")
}
})
test("a long final message leaves room for the system prompt", () => {
const out = _internal.flatten([{ role: "system", content: "SYS-PROMPT " + "s".repeat(3000) }, { role: "user", content: "q".repeat(40_000) }])
assert.ok(out.length <= _internal.PROMPT_LIMIT)
assert.match(out, /\[system\]\nSYS-PROMPT s{3000}/)
})