Skip to content

A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.

Count a node as emptied only while every child in its slot is a mark

Fei2411e9061465557b
diff --git a/ds-bot/client.js b/ds-bot/client.js
index 5cec1bfe..a3fb1654 100644
--- a/ds-bot/client.js
+++ b/ds-bot/client.js
@@ -3938,14 +3938,28 @@ var SETTINGS_ICONS = { general: GearIcon, bots: PeopleIcon, groups: ChatsIcon, u
// src/client/cell-empty.js
var import_react4 = require("react");
+var emptied = (slot, gone) => {
+ let marks = 0;
+ for (const node of slot.childNodes) {
+ if (node === gone) continue;
+ if (node.nodeType !== 1 || !node.hasAttribute("data-bt-empty")) return false;
+ marks += 1;
+ }
+ return marks > 0;
+};
function CellEmpty({ className }) {
const ref = (0, import_react4.useRef)(null);
(0, import_react4.useLayoutEffect)(() => {
- const slot = ref.current?.closest("[data-slot]");
+ const marker = ref.current;
+ const slot = marker?.closest("[data-slot]");
if (!slot) return void 0;
- slot.dataset.btEmptied = "";
+ const sync = (gone) => emptied(slot, gone) ? slot.dataset.btEmptied = "" : delete slot.dataset.btEmptied;
+ sync();
+ const observer = typeof MutationObserver === "function" ? new MutationObserver(() => sync()) : null;
+ observer?.observe(slot, { childList: true });
return () => {
- if (slot.querySelectorAll("[data-bt-empty]").length <= 1) delete slot.dataset.btEmptied;
+ observer?.disconnect();
+ sync(marker);
};
}, []);
return (0, import_react4.createElement)("span", { ref, className, hidden: true, "data-bt-empty": true });
diff --git a/ds-bot/src/client/cell-empty.js b/ds-bot/src/client/cell-empty.js
index 681b96d9..36db6b4d 100644
--- a/ds-bot/src/client/cell-empty.js
+++ b/ds-bot/src/client/cell-empty.js
@@ -5,13 +5,34 @@ import { createElement as h, useLayoutEffect, useRef } from 'react'
// no element child for a descendant check to see. The mark also reaches the slot that
// carries it: :has() cannot nest, so no selector can ask whether every node in a seat
// holds just the mark; the slot's own attribute can say it.
+// The slot counts as emptied only while every child in it is a mark, `gone` aside —
+// a node whose cell puts a visible event next to a mark stays shown.
+const emptied = (slot, gone) => {
+ let marks = 0
+ for (const node of slot.childNodes) {
+ if (node === gone) continue
+ if (node.nodeType !== 1 || !node.hasAttribute('data-bt-empty')) return false
+ marks += 1
+ }
+ return marks > 0
+}
+
export function CellEmpty({ className }) {
const ref = useRef(null)
useLayoutEffect(() => {
- const slot = ref.current?.closest('[data-slot]')
+ const marker = ref.current
+ const slot = marker?.closest('[data-slot]')
if (!slot) return undefined
- slot.dataset.btEmptied = ''
- return () => { if (slot.querySelectorAll('[data-bt-empty]').length <= 1) delete slot.dataset.btEmptied }
+ const sync = gone => (emptied(slot, gone) ? (slot.dataset.btEmptied = '') : delete slot.dataset.btEmptied)
+ sync()
+ // A node's content keeps changing as its calls settle: a mark that was alone
+ // stops counting once a visible part lands beside it, and marks again once
+ // that part goes.
+ const observer = typeof MutationObserver === 'function' ? new MutationObserver(() => sync()) : null
+ observer?.observe(slot, { childList: true })
+ // This mark may still sit in the slot when the cleanup runs, so it is left
+ // out of the count itself.
+ return () => { observer?.disconnect(); sync(marker) }
}, [])
return h('span', { ref, className, hidden: true, 'data-bt-empty': true })
}
diff --git a/ds-bot/test/tool-cells.test.js b/ds-bot/test/tool-cells.test.js
index 910ce7d6..2ff6da82 100644
--- a/ds-bot/test/tool-cells.test.js
+++ b/ds-bot/test/tool-cells.test.js
@@ -9,7 +9,7 @@ import { build } from 'esbuild'
const { outputFiles } = await build({
stdin: {
contents: [
- "export { ToolCell, TriggerCell, callTree, stackReplies } from './src/client/cells.js'",
+ "export { ToolCell, TriggerCell, callTree, stackReplies, CellEmpty } from './src/client/cells.js'",
"export { QuestionDock } from './src/client/questions.js'",
"export { SecretDock } from './src/client/secret-card.js'",
"export { countCards } from './src/client/sources.js'",
@@ -36,10 +36,17 @@ const react = {
useSyncExternalStore: (subscribe, getSnapshot) => getSnapshot(),
}
const timers = new Set()
+// The observers CellEmpty sets on its slot, so a test can deliver a mutation.
+const observers = []
+class FakeObserver {
+ constructor(fn) { this.fn = fn; this.off = false; observers.push(this) }
+ observe() {}
+ disconnect() { this.off = true }
+}
const module = { exports: {} }
runInNewContext(outputFiles[0].text, {
module, exports: module.exports,
- document: { hidden: false }, TextEncoder,
+ document: { hidden: false }, TextEncoder, MutationObserver: FakeObserver,
setTimeout: (fn) => { const timer = { fn }; timers.add(timer); return timer },
clearTimeout: timer => timers.delete(timer),
require: (name) => {
@@ -49,7 +56,7 @@ runInNewContext(outputFiles[0].text, {
throw new Error(`Unexpected platform module ${name}`)
},
})
-const { ToolCell, TriggerCell, callTree, stackReplies, QuestionDock, SecretDock, countCards, ViewMenuList, WidgetView } = module.exports
+const { ToolCell, TriggerCell, callTree, stackReplies, CellEmpty, QuestionDock, SecretDock, countCards, ViewMenuList, WidgetView } = module.exports
// Runs the effects collected since the last render; returns the cleanups.
const mount = () => {
@@ -332,6 +339,84 @@ test('every call that draws nothing leaves only the empty mark, so its seat can
}
})
+// #667: the slot a node's cell renders into counts as emptied only while every
+// child in it is an empty mark — a row or seat hides on that mark, so a node
+// holding anything else must not be taken for empty. `slotOf` builds the slot
+// the way the DOM would: one child per rendered part, mark or not.
+const slotOf = (out) => {
+ const slot = { childNodes: [], dataset: {} }
+ const lift = (node) => {
+ if (Array.isArray(node)) return node.forEach(lift)
+ if (node === null || node === undefined || typeof node === 'boolean') return
+ if (typeof node !== 'object') return slot.childNodes.push({ nodeType: 3, hasAttribute: () => false })
+ if (node.type === Fragment) return lift(node.props.children)
+ const dom = { nodeType: 1, hasAttribute: name => node.props?.[name] !== undefined, closest: () => slot }
+ if (node.props?.ref) node.props.ref.current = dom
+ slot.childNodes.push(dom)
+ }
+ lift(expand(out))
+ return slot
+}
+
+test('a node holding a visible event next to a mark is not emptied', () => {
+ const actions = { noteLive() {}, noteCards() {} }
+ // Marks alone: emptied, so the row or seat wrapping the slot may hide.
+ const alone = slotOf(ToolCell(props(call('ask_user', 'direct'), { actions })))
+ mount()
+ assert.equal(alone.dataset.btEmptied, '')
+
+ // A run_code node holding "Messaged Writer" and an answered question's mark:
+ // the event stays, so the node is not emptied.
+ const mixed = slotOf(ToolCell(props(program([
+ call('message_bot', 'program:ptc:1', { args: { to: 'Writer' } }),
+ call('ask_user', 'program:ptc:2'),
+ ]), { actions })))
+ mount()
+ assert.ok(mixed.childNodes.some(kid => kid.hasAttribute('data-bt-empty')), 'the answered question marks itself')
+ assert.ok(mixed.childNodes.some(kid => !kid.hasAttribute('data-bt-empty')), 'the event shows')
+ assert.ok(!('btEmptied' in mixed.dataset))
+ effects = []
+})
+
+test('a slot keeps its emptied state in step with what it holds', () => {
+ observers.length = 0
+ const slot = { childNodes: [], dataset: {} }
+ const mark = () => ({ nodeType: 1, hasAttribute: name => name === 'data-bt-empty', closest: () => slot })
+ const event = () => ({ nodeType: 1, hasAttribute: () => false, closest: () => slot })
+ const text = () => ({ nodeType: 3, hasAttribute: () => false })
+ // Mounts one CellEmpty into the slot beside `siblings`; returns its unmount.
+ const draw = (...siblings) => {
+ const self = mark()
+ CellEmpty({}).props.ref.current = self
+ slot.childNodes = [...siblings, self]
+ return mount()
+ }
+ const changed = () => observers.filter(observer => !observer.off).forEach(observer => observer.fn())
+
+ // A mark mounted beside a visible part, or beside plain text another
+ // renderer left, does not empty the slot.
+ draw(event(), mark())
+ assert.ok(!('btEmptied' in slot.dataset))
+ draw(text(), mark())
+ assert.ok(!('btEmptied' in slot.dataset))
+
+ // A mark alone empties the slot; a visible part landing later takes the mark
+ // back, and that part leaving empties the slot again.
+ const unmount = draw()
+ assert.equal(slot.dataset.btEmptied, '')
+ slot.childNodes = [...slot.childNodes, event()]
+ changed()
+ assert.ok(!('btEmptied' in slot.dataset))
+ slot.childNodes = slot.childNodes.slice(0, 1)
+ changed()
+ assert.equal(slot.dataset.btEmptied, '')
+
+ // The last mark leaving takes the emptied state with it.
+ unmount()
+ assert.ok(!('btEmptied' in slot.dataset))
+ effects = []
+})
+
test('a hidden Bot-to-Bot turn draws none of its sub-calls', () => {
const roster = team({ exchangeTurns: { chief: [1] }, secretRequests: { chief: [request('program:ptc:1')] } })
const root = program([call('request_secret', 'program:ptc:1', { args: { name: 'DEMO_TOKEN' } }), call('message_bot', 'program:ptc:2', { args: { to: 'Writer' } })])