Skip to content

Commit cf00e67

Browse files
HugoXKBirfy
andauthored
fix(preview): keep the words around a complete untrusted wrap (#2520)
Fixes #2443 Owner: Dev-5 Reviewer: Birfy Written-by: Dev-1 Claim-Id: 64f1efea39d040bbb2460674a667c098 Base: 43a80b9 (live `main` after #2997 merged; the branch was merged up onto it, never rebased) Verified commit: 37228bc7449a2825853a872141c464d493546c2c (tree 283c6cb) — the tree the pushed head carries Pushed head: bd1391b — same tree as the verified commit Risk tier: T3 ## What changed `visibleText` unwrapped a complete `<untrusted-message …>…</untrusted-message>` by keeping only the text **between** the first opener and the last closer, so a stored line that quoted a remote line and then answered it reached the screen as the quote alone — in the Bot chat's row, in a sidebar row, and in the host's room preview. The module's own comment promises the opposite ("words that never were part of a marker are kept, because a preview is still the line someone said"), so this is the code disagreeing with its documented contract. After: each complete wrap loses its markers **in place** — the head before it, its inner text and the tail after it all survive. Two wraps in one line both unwrap, a line that is only a wrap still reads as just its words, and a broken fragment keeps today's behaviour. `stripFence` is unchanged. The four readers this exercises, and what red first showed at the base: - `core/src/client/text.js` (`readableLine` → `previewOf`): drew `quoted`. - `core/src/client/sidebar.js` (`RoomRow` → `visibleText(room.preview)`): drew `quoted`. - `core/src/host/channel.js` (`view` → `visibleText(last.text)` on the `/api/bot` state answer): drew `quoted`. - `core/src/client/exchange.js` (`exchangeText`, the ⌘K sheet): **never had this defect** — it strips head and tail itself and only uses `stripFence`, so it stays a labelled control rather than a red assertion. Generated, not hand-edited: `core/client.js` (the module is inlined in the bundle) and `patches/ds-bot/0008-client-bundle.patch` + the `patches/ds-bot/files/**` mirrors, all from `node scripts/ds-bot-patches.mjs generate` after `npm run build`. `generate` rewrote nothing else: the other nine patch files are byte-identical, and `core/src/shared/preview.js` is modified on this branch (it is already on the base, `M` in the file list) with no `.patch` header of its own (`topics.json` has no entry; whole-file mirror only). - The two subsystem pages that own these paths say the new rule: `docs/subsystems/core-client.md` and `docs/subsystems/core-host.md` (the preview unwrap keeps the words before and after a complete wrap). ## Verification at the tip Tip SHA: bd1391b Ran: - red first on a clean `757ed34706e5ed1f7a649d54169e7c7a7ea08fee` tree carrying only `core/test/preview-fence-words.test.js`: `# tests 4 / # pass 1 / # fail 3` — the three consumer paths returned `He said quoted and I answered done.` as `quoted`; the exchange control passed. - after the fix, that suite: `# tests 4 / # pass 4 / # fail 0` - the same suite at this tip (main `43a80b98` merged in): `# tests 4 / # pass 4 / # fail 0` - `TZ=UTC ARCH_DOCS_BASE=43a80b98a61a4be5d1c28e54199cd11b465473b5 npm run verify`: exit 0 — core `# tests 3210 / # pass 3210 / # fail 0`, social 643, tree 149, timeline 172, workflow 531, project 147, `.github/scripts` + `policy` 675, interop 37, live-e2e 34, every step 0 fail; `arch-docs: OK (9 changed file(s))` - `node scripts/build-client.mjs --check`: exit 0 - `node scripts/ds-bot-patches.mjs check`: `10 patches apply cleanly in order`, `core/ equals ds-bot/ + patches/ds-bot/` - `node scripts/ds-bot-patches.mjs canonical`: `ok patches/ds-bot/ is what generate writes (generate is a no-op)` - `node .github/scripts/changelog.mjs check`: `14 fragment(s) in changelog.d/ are well formed` Not run: the smoke test (no `index.js` or `src/host` behaviour change beyond the preview string) and CI itself. Docs-not-needed: `core/src/shared/preview.js` is not on an owned_path of any subsystem page; this fixes the rule the module already documents, with no new module, contract or owned-path change. Merged up onto `43a80b98` (the squash of `#2997`) by Dev-5 and regenerated, never rebased: `npm run build` in `core/` followed by `generate`, with both generated files coming out byte-identical to what the scripts write (`canonical` reports the no-op), so no generated file was hand-merged and no source meaning moved. `build-client --check` and `patches check` pass at the pushed tree, `core/test/preview-fence-words.test.js` is 4/4 there, and the full `verify` is green. ## Checkpoint <!-- flybot:checkpoint --> ```json { "pr": 2520, "claim": "Dev-5", "issue": 2443, "summary": "A stored line that quotes a remote line and then answers it reached the Bot chat's row, a sidebar row and the host's room preview as the quote alone: visibleText kept only the text between the first opener and the last closer. Each complete wrap now loses its markers in place, so the words around it survive. Red first at 757ed34: 3 of 4 new tests failed; the exchange sheet never had the defect and stays a control. Rebased onto 43a80b9 and regenerated from core/, the full verify is green: core 3210, social 643, zero failures.", "files": [ "core/src/shared/preview.js", "core/test/preview-fence-words.test.js", "core/client.js", "patches/ds-bot/0008-client-bundle.patch", "patches/ds-bot/files/src/shared/preview.js", "patches/ds-bot/files/test/preview-fence-words.test.js", "changelog.d/2443-preview-fence-words.fixed.md" ], "checks": { "ran": "TZ=UTC ARCH_DOCS_BASE=43a80b98a61a4be5d1c28e54199cd11b465473b5 npm run verify; node core/scripts/build-client.mjs --check; node scripts/ds-bot-patches.mjs check; node scripts/ds-bot-patches.mjs canonical; node --test core/test/preview-fence-words.test.js", "result": "green", "at": "bd1391b98c424139634f20428cee8ac38b1d7a7b" }, "next": "waiting for the review side's Verdict naming bd1391b" } ``` <!-- /flybot:checkpoint --> Co-authored-by: Danyang Chen <birfychen@gmail.com>
1 parent 91f6845 commit cf00e67

9 files changed

Lines changed: 278 additions & 17 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
A preview now keeps the words on either side of a quoted remote line: a reply that quoted a line and then answered it showed the quote alone in the sidebar and the palette row.

‎core/client.js‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1972,10 +1972,12 @@ function visibleText(text2) {
19721972
for (let previous; previous !== out; ) {
19731973
previous = out;
19741974
const open = out.indexOf("<untrusted-message");
1975-
if (open < 0) break;
19761975
const close = out.lastIndexOf("</untrusted-message>");
1977-
const inner = close > open ? out.slice(out.indexOf(">", open) + 1, close) : out;
1978-
out = stripFence(inner);
1976+
if (open < 0 || close <= open) break;
1977+
const head = out.slice(0, open);
1978+
const inner = out.slice(out.indexOf(">", open) + 1, close);
1979+
const tail = out.slice(close + "</untrusted-message>".length);
1980+
out = stripFence(head) + inner + stripFence(tail);
19791981
}
19801982
return stripFence(out).replace(/\s+/g, " ").trim();
19811983
}

‎core/src/shared/preview.js‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,20 @@ export function stripFence(text) {
2727
}
2828

2929
// The words of a stored line as a preview shows them: inner text of a wrap, no marker of any shape,
30-
// and one line, because a row is one line.
30+
// and one line, because a row is one line. A wrap in the middle of a line loses its markers in place,
31+
// so the sentence around it survives: a reply that quotes a remote line and then answers it reads as
32+
// the reply, not as the quote.
3133
export function visibleText(text) {
3234
let out = String(text ?? '')
3335
for (let previous; previous !== out;) {
3436
previous = out
3537
const open = out.indexOf('<untrusted-message')
36-
if (open < 0) break
3738
const close = out.lastIndexOf('</untrusted-message>')
38-
const inner = close > open ? out.slice(out.indexOf('>', open) + 1, close) : out
39-
out = stripFence(inner)
39+
if (open < 0 || close <= open) break
40+
const head = out.slice(0, open)
41+
const inner = out.slice(out.indexOf('>', open) + 1, close)
42+
const tail = out.slice(close + '</untrusted-message>'.length)
43+
out = stripFence(head) + inner + stripFence(tail)
4044
}
4145
return stripFence(out).replace(/\s+/g, ' ').trim()
4246
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
// A preview is still the line someone said (#1347): the fence is data, but so are the words on
2+
// either side of it. Four readers show a stored line to a person — the Bot chat's row
3+
// (`readableLine`/`previewOf`), a sidebar row, the exchange sheet under ⌘K, and the host's room
4+
// preview on the /api/bot state answer — and every one of them must keep the words around a
5+
// complete wrap, not only the quoted half.
6+
import assert from 'node:assert/strict'
7+
import { test } from 'node:test'
8+
import { fileURLToPath } from 'node:url'
9+
import { runInNewContext } from 'node:vm'
10+
import { build } from 'esbuild'
11+
import { install } from '../src/host/channel.js'
12+
import { previewOf, readableLine } from '../src/client/text.js'
13+
14+
const MIXED = 'He said <untrusted-message from="Coder">quoted</untrusted-message> and I answered done.'
15+
const KEPT = 'He said quoted and I answered done.'
16+
17+
const root = fileURLToPath(new URL('..', import.meta.url))
18+
const h = (type, props, ...children) => ({ type, props: { ...(props ?? {}), children: children.flat(Infinity) } })
19+
class Component { constructor(props) { this.props = props } setState() {} }
20+
const react = {
21+
createElement: h, Fragment: Symbol('Fragment'), Component, PureComponent: Component,
22+
createRef: () => ({ current: null }), useState: value => [typeof value === 'function' ? value() : value, () => {}],
23+
useEffect() {}, useLayoutEffect() {}, useRef: value => ({ current: value }), useCallback: fn => fn,
24+
useMemo: fn => fn(), useReducer: (fn, initial) => [initial, () => {}],
25+
}
26+
const slots = { entries: () => [], subscribe: () => () => {}, version: () => 0 }
27+
28+
// A client module is bundled and loaded the way the client tests load one (sidebar-readonly.test.js):
29+
// esbuild for the browser half, a stub react in the context, so the module's own code runs here.
30+
async function clientModule(contents) {
31+
const { outputFiles } = await build({
32+
stdin: { contents, resolveDir: root },
33+
bundle: true, format: 'cjs', platform: 'browser', write: false, logLevel: 'silent',
34+
external: ['react', 'react-dom', '@deepseek-ai/dsh-client-ui-primitives'],
35+
})
36+
const mod = { exports: {} }
37+
runInNewContext(outputFiles[0].text, {
38+
module: mod, exports: mod.exports, console, Date, JSON, Promise, Symbol, setTimeout, clearTimeout,
39+
__flybot__: { slots, call: () => {} },
40+
require: name => (name === 'react' ? react : name === 'react-dom' ? { createPortal: node => node } : {}),
41+
})
42+
return mod.exports
43+
}
44+
45+
// Elements stay data; a function component is called so a row's own code runs, and the strings it
46+
// draws are collected in order.
47+
function drawnText(node, seen = []) {
48+
if (node === null || node === undefined) return seen
49+
if (typeof node === 'string') { seen.push(node); return seen }
50+
if (typeof node !== 'object') return seen
51+
if (Array.isArray(node)) { for (const child of node) drawnText(child, seen); return seen }
52+
if (typeof node.type === 'function') { drawnText(node.type(node.props), seen); return seen }
53+
for (const child of node.props?.children ?? []) drawnText(child, seen)
54+
return seen
55+
}
56+
57+
test('the Bot chat row keeps the words before and after a complete wrap', () => {
58+
assert.equal(readableLine(MIXED), KEPT)
59+
const list = { projectionsBySession: { s1: { values: { turnOutline: [{ turn: 1, response: MIXED }] } } }, byId: {} }
60+
assert.equal(previewOf(list, 's1'), KEPT)
61+
assert.equal(readableLine('a <untrusted-message from="C">one</untrusted-message> b <untrusted-message from="D">two</untrusted-message> c'), 'a one b two c')
62+
})
63+
64+
test('the sidebar row of a room keeps them too', async () => {
65+
const { TeamSidebar } = await clientModule("export { TeamSidebar } from './src/client/sidebar.js'")
66+
const room = { id: 'r1', name: 'Team room', kind: 'group', createdAt: 1, preview: MIXED }
67+
const list = { ids: ['r1'], byId: { r1: { updatedAt: 1 } } }
68+
const useSessions = selector => selector(list)
69+
const useUi = selector => selector({ unread: {} })
70+
const tree = TeamSidebar({
71+
wide: true,
72+
useRoster: selector => selector({ bots: [], byId: {}, rooms: [room], mainBotId: 'm', agents: [] }),
73+
useUi,
74+
useSessions,
75+
useRegistry: useUi,
76+
useWork: selector => selector({}),
77+
useSessionStatus: selector => selector(new Map()),
78+
useSessionRetainInfo: (id, selector) => selector({}),
79+
useActivity: selector => selector({}),
80+
useUpdate: selector => selector({}),
81+
actions: {},
82+
})
83+
const words = drawnText(tree)
84+
assert.ok(words.includes(KEPT), `the row drew ${JSON.stringify(words.filter(word => /quoted|He said|answered/.test(word)))}`)
85+
})
86+
87+
test('the host room preview on the state answer keeps them too', async () => {
88+
const state = {
89+
revision: 3, workspaceId: null, mainBotIds: ['main'],
90+
bots: { main: { id: 'main', name: 'Main' } },
91+
rooms: { r1: { id: 'r1', name: 'Team room', createdAt: 1, log: [{ text: MIXED, who: 'User', authorKind: 'human' }] } },
92+
humans: {}, agents: {}, questions: {}, prefs: {}, secretRequests: {},
93+
}
94+
let route
95+
const rt = {
96+
state,
97+
ctx: { effect: (fn) => { fn(); return () => {} }, connection: { fetch: { register: (r) => { route = r; return () => {} } } } },
98+
warn: () => {}, noteFailure: () => {},
99+
readOnly: () => undefined, authenticate: async () => null, authorize: () => ({ ok: true }),
100+
bootstrap: async () => {}, catalog: async () => [], loadSecrets: async () => {},
101+
cachedCatalog: () => [], cachedDefault: () => null,
102+
chatOf: id => `chat-${id}`, ownedLive: full => full, ownedView: full => full, load: async () => {}, refresh: async () => {},
103+
pluginToolNames: () => [], publicHumans: () => [], presentRoom: room => room,
104+
secretsView: () => [], capabilityLibraryView: () => ({}), flybotRemoteMembers: () => [],
105+
permissionCatalog: () => null, browserEnabled: false, memoryEnabled: true, authzEnforced: () => false,
106+
listRoomApprovals: () => [], roomBusyAcksView: () => ({}),
107+
activityView: () => [], busyAcksView: () => ({}), workView: () => ({}), roomWorkView: () => ({}),
108+
approvalsView: () => ({}), endedView: () => [], memoryNews: () => [], memoryReviews: () => [],
109+
connectorsView: () => [], capabilityStatusesView: () => [], stateLinkReplaced: () => ({ gen: 'g', count: 0 }),
110+
}
111+
install(rt)
112+
const answer = await route.fetch(new Request('http://host/api/bot', {
113+
method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ endpoint: 'state' }),
114+
}))
115+
const body = await answer.json()
116+
assert.equal(body.ok, true, JSON.stringify(body.error))
117+
assert.equal(body.value.rooms[0].preview, KEPT)
118+
})
119+
120+
test('the exchange sheet under the palette already kept them (control, unchanged by this fix)', async () => {
121+
const { exchangeText } = await clientModule("export { exchangeText } from './src/client/exchange.js'")
122+
assert.equal(exchangeText(MIXED), KEPT)
123+
assert.equal(exchangeText('<untrusted-message from="C">\nhello there\n</untrusted-message>'), 'hello there')
124+
})

0 commit comments

Comments
 (0)