diff --git a/changelog.d/3234-approval-note-codepoint.fixed.md b/changelog.d/3234-approval-note-codepoint.fixed.md new file mode 100644 index 000000000..568d92be0 --- /dev/null +++ b/changelog.d/3234-approval-note-codepoint.fixed.md @@ -0,0 +1 @@ +The approval note that a restart quotes back in the chat is now cut on a code point boundary, so a reason whose 200th UTF-16 unit fell inside an emoji no longer comes back with a replacement glyph in the middle of the user's own text ([#3234](https://github.com/ThinkFlowLab/FlyBot/issues/3234)). diff --git a/core/src/host/approvals.js b/core/src/host/approvals.js index be28d7c75..523f8f138 100644 --- a/core/src/host/approvals.js +++ b/core/src/host/approvals.js @@ -82,7 +82,7 @@ export function install(rt) { at: Date.now(), } pending.set(id, entry) - rt.noteApproval?.(sessionId, { tool: entry.tool, reason: entry.reason.slice(0, 200) }) + rt.noteApproval?.(sessionId, { tool: entry.tool, reason: cut(entry.reason, 200).text }) const settled = () => { pending.delete(id) rt.noteApproval?.(sessionId, undefined) diff --git a/core/test/follow-through.test.js b/core/test/follow-through.test.js index e2c4727bc..4447c79c8 100644 --- a/core/test/follow-through.test.js +++ b/core/test/follow-through.test.js @@ -5,6 +5,7 @@ import { join } from 'node:path' import { test } from 'node:test' import { createRuntime, provideFlyBot } from '../index.js' import { fakeContext } from './fake-ctx.js' +import { resumeText } from '../src/host/resume.js' import { stripWrittenCalls, writesToolCall } from '../src/host/turns.js' // Chief in its own chat 'a'; Writer, the admin of group 'r1', speaks there from 'gb'. @@ -188,6 +189,38 @@ test('a group turn cut off by a restart is picked up once, and the group is told assert.equal(third.agentOf('gb').calls.length, 0) }) +// #3234: the note the restart line quotes is bounded to 200 code points, not 200 UTF-16 units. A cut +// landing inside an emoji used to keep the lone high surrogate, and the harness flattens that on the +// wire with `toWellFormed`, so the user read a `U+FFFD` glyph in the middle of their own reason. +test('#3234: an approval note cut at 200 keeps an emoji whole and never stores half of one', async (t) => { + mockTimers(t) + const askWith = (host, sessionId, reason) => host.ctx.listeners.get('approval/request')[0]( + { agent: { session: { id: sessionId } }, toolName: 'pwsh', reason }, () => 'from-dsh') + // 200 code points, 201 UTF-16 units: a unit cut lands inside the emoji, so the note keeps the + // 199 units before it and drops the emoji whole (`cut` bounds units, on purpose). + const insideEmoji = 'x'.repeat(199) + '😀' + const home = freshHome(t) + const host = await start(home) + await openRoomTurn(t, host) + void askWith(host, 'gb', insideEmoji) + await settle(() => inflight(home).gb?.approval !== undefined) + const note = inflight(home).gb.approval.reason + assert.equal(note, 'x'.repeat(199)) + assert.ok(note.isWellFormed(), 'the note keeps a lone surrogate') + assert.ok(insideEmoji.startsWith(note) && note.length <= 200) + assert.ok(resumeText(inflight(home).gb).isWellFormed(), 'the restart line is flattened to a replacement glyph') + + // The control fits the same 200 units exactly, so it is unchanged. + const beforeCut = 'x'.repeat(198) + '😀' + const controlHome = freshHome(t) + const control = await start(controlHome) + await openRoomTurn(t, control) + void askWith(control, 'gb', beforeCut) + await settle(() => inflight(controlHome).gb?.approval !== undefined) + assert.equal(inflight(controlHome).gb.approval.reason, beforeCut) + assert.ok(inflight(controlHome).gb.approval.reason.isWellFormed()) +}) + test('a request cut off by a restart goes on under the same exchange; own-chat work resumes in the chat', async (t) => { mockTimers(t) const home = freshHome(t) diff --git a/docs/subsystems/core-host.md b/docs/subsystems/core-host.md index f4f00c9f8..7c4670d2a 100644 --- a/docs/subsystems/core-host.md +++ b/docs/subsystems/core-host.md @@ -646,7 +646,7 @@ A local, replayable record of every change to the saved team, for multi-Bot bugs ### Standing tool approvals (#540) -DSH's approval outcomes are one-shot (`ApprovalOutcome`: `allowed-once` / `rejected` / `cancelled` / `unavailable`; `ApprovalPolicy` is `ask` / `never`), so a permanent approval lives in FlyBot, not in DSH (no DSH patch). `src/host/approvals.js` keys a request by its kind (`approvalKind`: the tool, plus the sandbox mode when DSH's reason reads `escalate sandbox to :`, e.g. `bash:escalate:danger-full-access`). `approval-answer` takes `always-bot` (kept on the Bot as `standingApprovals: [{ kind, label, at, ownerId? }]`) and `always-room` (kept on the room the ask came from; refused for an own-chat ask); both save, answer this ask `allowed-once` and release other waiting asks the grant covers. A save the store refuses changes nothing: the grant is taken back out of memory and the error is rethrown, so an **Always allow** that was never written — the temp name its bytes land through is a link or a directory — cannot answer a later card (#2764), and `standing-approval-remove` keeps a grant whose removal its own refused save could not delete. Two answers that touch the same record are applied one after the other (`chains` / `inTurn` in `approvals.js`): the next reads the rolled-back list, so a refused save takes back only its own grant and can never write one that its own save refused (#2782). The `approval/request` listener answers `allowed-once` itself, before any card, only for a turn the owner started (`settled(...) === 'owner'` from `turn-origin.js`; a friend's message, another member's line or a room with other people in it still asks), or for a turn that carries **no verdict at all in the Bot's own chat** — DSH starts such a turn for its own retry after the owner answered, and nobody's request is in it (#608) — and only when the Bot (a local one, never `bot.remote`) has the kind, or the ask comes from a group Session of a room holding the kind for the Bot's owner (`ownerId` matches while authz is enforced; before anyone signs in every local Bot is the one person's); a group grant does not cover the Bot's own chat. DSH's audit pair (`approval/asked` / `approval/decided`) still logs each auto-answered ask. A grant that a turn cannot use is not skipped silently: the first such ask per Session and kind logs `the standing approval for was not used: the turn is ""`. `standing-approval-remove { scope: 'bot' | 'room', id, kind }` revokes (a signed-in remover takes only their own group grant). Authz: an `always-*` answer needs the Bot's owner (owning the room is enough only for Allow once); removing a Bot's grant needs the Bot's owner, a group's the member who gave it. Tests: `core/test/group-status.test.js`, `core/test/authz.test.js`. P-673: a save that writes nothing and still resolves is refused the same way — a stopped host, a failed takeover and `!writable()` (`store.js` `save()`) would otherwise leave a grant the file never received, or a removal the file never got, while the card reported success. The two approval writes and the board writes that must be in the file (`tasks.js` `commitTasks`: a claim, a release, a rework, a batch) ask the same question through `rt.saveOrRefuse()` (`store.js`, next to `savesQueued`): a save that queued nothing is a refusal answered with the store's `readOnly()` sentence and `bot/read-only`, and the board rolls back to its snapshot first — so a refused claim posts no claim line and the file's card stays queued for the next member. Ordinary `save()` callers keep the silent skip. An answer that changes nothing on disk — Allow once, Reject — still works in a read-only window, because those write nothing by design and `approval-answer` is one of the read path's names (#2310). Tests: `core/test/approval-silent-save.test.js`, `core/test/task-claim-silent-save.test.js`. A reminder turn is nobody's turn: when the ask reaches the `owner === undefined` branch for a Session `rt.scheduledRunOf` names as running a scheduled task, the listener answers `rejected` in place instead of handing it to DSH's `ask()` — which has no timeout — so the turn ends through `runEnded` and `state.scheduleRuns` leaves `running` rather than holding the next delivery behind it; the direction is only hang → denied (a grant this turn could use answered above, and a grant it cannot use is no licence to run unattended), one `warn` line records it, and one sentence naming the tool is injected into the run's own Session (`source` role `mention`, nobody's request) saying it was skipped and to run it from the chat or add a standing approval, because a scheduled job that quietly does one thing less is the same failure, quieter ([#2818](https://github.com/ThinkFlowLab/FlyBot/issues/2818)). Tests: `core/test/approvals-scheduled.test.js`. +DSH's approval outcomes are one-shot (`ApprovalOutcome`: `allowed-once` / `rejected` / `cancelled` / `unavailable`; `ApprovalPolicy` is `ask` / `never`), so a permanent approval lives in FlyBot, not in DSH (no DSH patch). `src/host/approvals.js` keys a request by its kind (`approvalKind`: the tool, plus the sandbox mode when DSH's reason reads `escalate sandbox to :`, e.g. `bash:escalate:danger-full-access`). `approval-answer` takes `always-bot` (kept on the Bot as `standingApprovals: [{ kind, label, at, ownerId? }]`) and `always-room` (kept on the room the ask came from; refused for an own-chat ask); both save, answer this ask `allowed-once` and release other waiting asks the grant covers. A save the store refuses changes nothing: the grant is taken back out of memory and the error is rethrown, so an **Always allow** that was never written — the temp name its bytes land through is a link or a directory — cannot answer a later card (#2764), and `standing-approval-remove` keeps a grant whose removal its own refused save could not delete. Two answers that touch the same record are applied one after the other (`chains` / `inTurn` in `approvals.js`): the next reads the rolled-back list, so a refused save takes back only its own grant and can never write one that its own save refused (#2782). The `approval/request` listener answers `allowed-once` itself, before any card, only for a turn the owner started (`settled(...) === 'owner'` from `turn-origin.js`; a friend's message, another member's line or a room with other people in it still asks), or for a turn that carries **no verdict at all in the Bot's own chat** — DSH starts such a turn for its own retry after the owner answered, and nobody's request is in it (#608) — and only when the Bot (a local one, never `bot.remote`) has the kind, or the ask comes from a group Session of a room holding the kind for the Bot's owner (`ownerId` matches while authz is enforced; before anyone signs in every local Bot is the one person's); a group grant does not cover the Bot's own chat. DSH's audit pair (`approval/asked` / `approval/decided`) still logs each auto-answered ask. A grant that a turn cannot use is not skipped silently: the first such ask per Session and kind logs `the standing approval for was not used: the turn is ""`. `standing-approval-remove { scope: 'bot' | 'room', id, kind }` revokes (a signed-in remover takes only their own group grant). Authz: an `always-*` answer needs the Bot's owner (owning the room is enough only for Allow once); removing a Bot's grant needs the Bot's owner, a group's the member who gave it. Tests: `core/test/group-status.test.js`, `core/test/authz.test.js`. P-673: a save that writes nothing and still resolves is refused the same way — a stopped host, a failed takeover and `!writable()` (`store.js` `save()`) would otherwise leave a grant the file never received, or a removal the file never got, while the card reported success. The two approval writes and the board writes that must be in the file (`tasks.js` `commitTasks`: a claim, a release, a rework, a batch) ask the same question through `rt.saveOrRefuse()` (`store.js`, next to `savesQueued`): a save that queued nothing is a refusal answered with the store's `readOnly()` sentence and `bot/read-only`, and the board rolls back to its snapshot first — so a refused claim posts no claim line and the file's card stays queued for the next member. Ordinary `save()` callers keep the silent skip. An answer that changes nothing on disk — Allow once, Reject — still works in a read-only window, because those write nothing by design and `approval-answer` is one of the read path's names (#2310). Tests: `core/test/approval-silent-save.test.js`, `core/test/task-claim-silent-save.test.js`. A reminder turn is nobody's turn: when the ask reaches the `owner === undefined` branch for a Session `rt.scheduledRunOf` names as running a scheduled task, the listener answers `rejected` in place instead of handing it to DSH's `ask()` — which has no timeout — so the turn ends through `runEnded` and `state.scheduleRuns` leaves `running` rather than holding the next delivery behind it; the direction is only hang → denied (a grant this turn could use answered above, and a grant it cannot use is no licence to run unattended), one `warn` line records it, and one sentence naming the tool is injected into the run's own Session (`source` role `mention`, nobody's request) saying it was skipped and to run it from the chat or add a standing approval, because a scheduled job that quietly does one thing less is the same failure, quieter ([#2818](https://github.com/ThinkFlowLab/FlyBot/issues/2818)). Tests: `core/test/approvals-scheduled.test.js`. The note the restart line quotes back is bounded the same way as every other stored text since [#3234](https://github.com/ThinkFlowLab/FlyBot/issues/3234): `noteApproval` keeps `cut(entry.reason, 200).text`, not a bare `slice(0, 200)`, so a reason whose 200th UTF-16 unit fell inside a character outside the BMP keeps the units before it and drops that character whole, instead of storing a lone surrogate that `resume.js` pastes into the resume turn the user reads and that the wire draws as U+FFFD. ### Discarded promise `.catch` (#213) diff --git a/patches/ds-bot/0001-core-fixes.patch b/patches/ds-bot/0001-core-fixes.patch index 9b0f9d6dc..cdd88c0a1 100644 --- a/patches/ds-bot/0001-core-fixes.patch +++ b/patches/ds-bot/0001-core-fixes.patch @@ -4219,7 +4219,7 @@ diff --git a/src/host/approvals.js b/src/host/approvals.js export function install(rt) { const { ctx } = rt -@@ -16,15 +16,28 @@ export function install(rt) { +@@ -16,25 +16,49 @@ export function install(rt) { const sessionId = request.agent?.session?.id ?? request.agent?.id const owner = sessionId === undefined ? undefined : rt.groupOwners.get(sessionId) const botId = owner?.botId ?? (sessionId === undefined ? undefined : rt.chatOwners.get(sessionId)) @@ -4251,7 +4251,10 @@ diff --git a/src/host/approvals.js b/src/host/approvals.js detail: rt.callDetail?.(sessionId, request.callId) ?? {}, at: Date.now(), } -@@ -34,7 +34,18 @@ export function install(rt) { + pending.set(id, entry) +- rt.noteApproval?.(sessionId, { tool: entry.tool, reason: entry.reason.slice(0, 200) }) ++ rt.noteApproval?.(sessionId, { tool: entry.tool, reason: cut(entry.reason, 200).text }) + const settled = () => { pending.delete(id) rt.noteApproval?.(sessionId, undefined) } @@ -12201,15 +12204,17 @@ diff --git a/test/connectors.test.js b/test/connectors.test.js diff --git a/test/follow-through.test.js b/test/follow-through.test.js --- a/test/follow-through.test.js +++ b/test/follow-through.test.js -@@ -3,7 +3,7 @@ import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'no +@@ -3,8 +3,9 @@ import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'no import { tmpdir } from 'node:os' import { join } from 'node:path' import { test } from 'node:test' -import { apply } from '../index.js' +import { createRuntime, provideFlyBot } from '../index.js' import { fakeContext } from './fake-ctx.js' ++import { resumeText } from '../src/host/resume.js' import { stripWrittenCalls, writesToolCall } from '../src/host/turns.js' + // Chief in its own chat 'a'; Writer, the admin of group 'r1', speaks there from 'gb'. @@ -27,9 +27,13 @@ const seed = () => ({ // One DS Bot host on `home`; a second start on the same home is a restart. // Hosts by home: a home is removed only after its hosts stopped and their writes landed. @@ -12325,9 +12330,42 @@ diff --git a/test/follow-through.test.js b/test/follow-through.test.js const home = freshHome(t) const first = await start(home) await openRoomTurn(t, first) -@@ -143,7 +143,7 @@ test('a group turn cut off by a restart is picked up once, and the group is told +@@ -142,8 +142,40 @@ test('a group turn cut off by a restart is picked up once, and the group is told + assert.equal(third.agentOf('gb').calls.length, 0) }) ++// #3234: the note the restart line quotes is bounded to 200 code points, not 200 UTF-16 units. A cut ++// landing inside an emoji used to keep the lone high surrogate, and the harness flattens that on the ++// wire with `toWellFormed`, so the user read a `U+FFFD` glyph in the middle of their own reason. ++test('#3234: an approval note cut at 200 keeps an emoji whole and never stores half of one', async (t) => { ++ mockTimers(t) ++ const askWith = (host, sessionId, reason) => host.ctx.listeners.get('approval/request')[0]( ++ { agent: { session: { id: sessionId } }, toolName: 'pwsh', reason }, () => 'from-dsh') ++ // 200 code points, 201 UTF-16 units: a unit cut lands inside the emoji, so the note keeps the ++ // 199 units before it and drops the emoji whole (`cut` bounds units, on purpose). ++ const insideEmoji = 'x'.repeat(199) + '😀' ++ const home = freshHome(t) ++ const host = await start(home) ++ await openRoomTurn(t, host) ++ void askWith(host, 'gb', insideEmoji) ++ await settle(() => inflight(home).gb?.approval !== undefined) ++ const note = inflight(home).gb.approval.reason ++ assert.equal(note, 'x'.repeat(199)) ++ assert.ok(note.isWellFormed(), 'the note keeps a lone surrogate') ++ assert.ok(insideEmoji.startsWith(note) && note.length <= 200) ++ assert.ok(resumeText(inflight(home).gb).isWellFormed(), 'the restart line is flattened to a replacement glyph') ++ ++ // The control fits the same 200 units exactly, so it is unchanged. ++ const beforeCut = 'x'.repeat(198) + '😀' ++ const controlHome = freshHome(t) ++ const control = await start(controlHome) ++ await openRoomTurn(t, control) ++ void askWith(control, 'gb', beforeCut) ++ await settle(() => inflight(controlHome).gb?.approval !== undefined) ++ assert.equal(inflight(controlHome).gb.approval.reason, beforeCut) ++ assert.ok(inflight(controlHome).gb.approval.reason.isWellFormed()) ++}) ++ test('a request cut off by a restart goes on under the same exchange; own-chat work resumes in the chat', async (t) => { - t.mock.timers.enable({ apis: ['setTimeout'] }) + mockTimers(t)