Skip to content

Commit 42851df

Browse files
committed
social: a failed Auto-send read is drawn as a failure, not as a setting
The friend settings card read the pair's Auto-send setting with an empty rejection branch: a failed read left the checkbox at its initial false, drawn exactly like a setting that is off, while the switch stayed live — the person could read the setting as one they chose. The value is now unknown (null) until a read answers, the failed read draws one quiet status row, and the switch is withheld while unknown. Provenance: the vendored plugins/social/client.js moves from the identical list to modified_files, and the four added test files are recorded with it. Ports the intent of #3370 (stays open).
1 parent be7b1da commit 42851df

7 files changed

Lines changed: 589 additions & 44 deletions

File tree

‎docs/upstream/flybot-files.json‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,6 @@
6565
"mode": "100644",
6666
"bytes": 821
6767
},
68-
"plugins/social/client.js": {
69-
"sha256": "351206abc51ba011f0425c060292515b931dd8462109fe9d8e4d343d86ff88af",
70-
"git_blob_sha": "df78f71716e0713e9151942f3ec0f97b8f0c1e3a",
71-
"mode": "100644",
72-
"bytes": 270636
73-
},
7468
"plugins/social/test/rest-timeout.test.js": {
7569
"sha256": "0c59a063d2c308c1eb2d1cbce5bee57d09579bf6cbb92de6c73eab4f1278b18f",
7670
"git_blob_sha": "49f23d0c8f4a5a4dcf8b78e08d0894b9ef3af68d",

‎docs/upstream/migration-manifest.json‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1707,7 +1707,7 @@
17071707
"scripts/logo.mjs",
17081708
"scripts/self-test-catalog.mjs"
17091709
],
1710-
"file_count": 46
1710+
"file_count": 45
17111711
},
17121712
"bot-social": {
17131713
"destination": "services/bot-social",
@@ -2258,6 +2258,48 @@
22582258
"license_notice": "MIT (Bot Social snapshot)",
22592259
"new_sha256": "2a477b7e35cdf1d1245486e6980aeb4769d85fc05c82dac774b32814f6325785",
22602260
"bytes": 22655
2261+
},
2262+
"runtime/flybot/plugins/social/client.js": {
2263+
"destination": "runtime/flybot",
2264+
"path": "plugins/social/client.js",
2265+
"original_sha256": "351206abc51ba011f0425c060292515b931dd8462109fe9d8e4d343d86ff88af",
2266+
"original_git_blob_sha": "df78f71716e0713e9151942f3ec0f97b8f0c1e3a",
2267+
"new_sha256": "4cc8163ed62299a8bad62a42a126ec7ad4bdc1c3f5e4610cc80747d88b38ad4d",
2268+
"bytes": 272278,
2269+
"reason": "Repackages the intent of #3356/#3364/#3370 into the vendored plugin: poll only while visible, and draw a failed read as a failed read.",
2270+
"license_notice": "Apache-2.0 (FlyBot) file modified in this tree; see runtime/flybot/ORIGIN.md."
2271+
},
2272+
"runtime/flybot/plugins/social/test/poll-visibility.test.js": {
2273+
"destination": "runtime/flybot",
2274+
"path": "plugins/social/test/poll-visibility.test.js",
2275+
"new_sha256": "0256e99a2f45db639db3c8ec073243fdbe70e958276fb69520e034aa792cda16",
2276+
"bytes": 8036,
2277+
"reason": "Test added with the repackage of #3356: a hidden tab holds no poll timer in the approval dock or the requests panel.",
2278+
"license_notice": "Apache-2.0 (FlyBot) file modified in this tree; see runtime/flybot/ORIGIN.md."
2279+
},
2280+
"runtime/flybot/plugins/social/test/paused-chat-read-error.test.js": {
2281+
"destination": "runtime/flybot",
2282+
"path": "plugins/social/test/paused-chat-read-error.test.js",
2283+
"new_sha256": "a26d4df8cda2601df6490480050442df6c5a05245333a5f09f33e527400c8851",
2284+
"bytes": 3885,
2285+
"reason": "Test added with the repackage of #3364: a failed paused-chat read is drawn as a failure, not as no pause.",
2286+
"license_notice": "Apache-2.0 (FlyBot) file modified in this tree; see runtime/flybot/ORIGIN.md."
2287+
},
2288+
"runtime/flybot/plugins/social/test/friend-auto-read-error.test.js": {
2289+
"destination": "runtime/flybot",
2290+
"path": "plugins/social/test/friend-auto-read-error.test.js",
2291+
"new_sha256": "db1ec269a7b6c3e89813d8cc240dd21b671f64657774a80542e7f53e96e39d44",
2292+
"bytes": 3777,
2293+
"reason": "Test added with the repackage of #3370: a failed Auto-send read is drawn as a failure and the switch is withheld until the value is known.",
2294+
"license_notice": "Apache-2.0 (FlyBot) file modified in this tree; see runtime/flybot/ORIGIN.md."
2295+
},
2296+
"runtime/flybot/plugins/social/test/client-harness.mjs": {
2297+
"destination": "runtime/flybot",
2298+
"path": "plugins/social/test/client-harness.mjs",
2299+
"new_sha256": "3dba189f2e9a54be2b3a65cebdcc48ed3ca3a02fca8651cabfc6097f487b967e",
2300+
"bytes": 5665,
2301+
"reason": "Shared render/document/timer bench for the client tests added with the #3364 and #3370 repackage.",
2302+
"license_notice": "Apache-2.0 (FlyBot) file modified in this tree; see runtime/flybot/ORIGIN.md."
22612303
}
22622304
},
22632305
"core_fork_removed": {

‎runtime/flybot/plugins/social/client.js‎

Lines changed: 62 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,30 @@ window.__ModuleLoader__.load({
9191
return message
9292
}
9393

94+
// A poll exists to refresh what the person can see: it does not run while the tab is hidden, it
95+
// reads once as the tab comes back, and the cadence restarts from that read rather than from the
96+
// hidden phase (#3315).
97+
function pollWhileVisible(fn, ms) {
98+
if (typeof setInterval !== 'function') return undefined
99+
const doc = typeof document !== 'undefined' && typeof document.addEventListener === 'function' ? document : null
100+
const visible = () => !doc || !doc.hidden
101+
let timer = null
102+
const stop = () => { if (timer !== null) { clearInterval(timer); timer = null } }
103+
// The first read and the interval start together, so a mount that is already visible refreshes
104+
// at once and a hidden one refreshes when it is shown — one read, then a full period later.
105+
const start = () => {
106+
stop()
107+
if (!visible()) return
108+
fn()
109+
timer = setInterval(fn, ms)
110+
timer?.unref?.()
111+
}
112+
const onShow = () => start()
113+
doc?.addEventListener('visibilitychange', onShow)
114+
start()
115+
return () => { stop(); doc?.removeEventListener('visibilitychange', onShow) }
116+
}
117+
94118
// Styles reuse ds-bot's sidebar tokens (--bt-ink, --bt-ink-2, --bt-ink-3, --bt-hover, --bt-line,
95119
// --bt-line-2, --bt-card, --bt-accent, --bt-tag-bg/-ink), like @flybot/tree, so both themes follow the app.
96120
const CSS = `
@@ -2556,8 +2580,6 @@ window.__ModuleLoader__.load({
25562580
useEffect(() => onDockRecords(() => bumpRecord(value => value + 1)), [sessionId])
25572581
const failures = dockFailuresOf(sessionId)
25582582
const load = () => {
2559-
// #3080: a tab that is not on screen has nobody reading these cards; showing it catches up below.
2560-
if (typeof document !== 'undefined' && document.hidden) return
25612583
call('social/status', {}).then(value => {
25622584
setItems(Array.isArray(value?.approvals) ? value.approvals : [])
25632585
setStatusLoaded(true)
@@ -2567,18 +2589,9 @@ window.__ModuleLoader__.load({
25672589
setListLoaded(true)
25682590
}, () => {})
25692591
}
2570-
useEffect(() => {
2571-
load()
2572-
if (typeof setInterval !== 'function') return undefined
2573-
const timer = setInterval(load, 5000)
2574-
timer?.unref?.()
2575-
// #3080: a tab that sat in the background catches up the moment it is shown, not one beat later
2576-
// (the same shape `everySecond` uses above).
2577-
const onShow = () => { if (typeof document === 'undefined' || !document.hidden) load() }
2578-
const doc = typeof document !== 'undefined' && typeof document.addEventListener === 'function' ? document : null
2579-
doc?.addEventListener('visibilitychange', onShow)
2580-
return () => { clearInterval(timer); doc?.removeEventListener('visibilitychange', onShow) }
2581-
}, [])
2592+
// #3080: a tab nobody is reading keeps no timer; coming back reads once at once and restarts the
2593+
// cadence from that read (`pollWhileVisible`).
2594+
useEffect(() => pollWhileVisible(load, 5000), [])
25822595
const session = { sessionId, roster, room, bot, handle: bot?.remote?.handle }
25832596
// The person who gets it (#418, #498), not the friend's Bot: a local hold already names the person.
25842597
const named = list => list.map((item) => {
@@ -3254,17 +3267,9 @@ window.__ModuleLoader__.load({
32543267
const dismissShareRequest = item => call('social/share-request-dismiss', { id: item.id }).then(applyStatus, e => setError(errorText(e)))
32553268
const dismissAccept = item => call('social/accept-notice-dismiss', { id: item.id }).then(applyStatus, e => setError(errorText(e)))
32563269
const askShareBack = item => call('social/ask-share-back', { botId: item.botId, to: item.from, noticeId: item.id }).then(applyStatus, e => setError(errorText(e)))
3257-
useEffect(() => {
3258-
loadApprovals()
3259-
if (typeof setInterval !== 'function') return undefined
3260-
const tick = () => {
3261-
if (typeof document !== 'undefined' && document.hidden) return
3262-
loadApprovals()
3263-
}
3264-
const timer = setInterval(tick, 15_000)
3265-
timer?.unref?.()
3266-
return () => clearInterval(timer)
3267-
}, [])
3270+
// A hidden tab is not looking at these requests either, so the 15 s poll runs only while the page
3271+
// is on screen and a tab that comes back reads once at once (`pollWhileVisible`).
3272+
useEffect(() => pollWhileVisible(loadApprovals, 15_000), [])
32683273
useEffect(() => facePanel.bind(mode => { setFaceMode(mode === 'start' ? 'start' : 'join'); setFaceOpen(true) }), [])
32693274
useEffect(() => addFriendPanel.bind(() => setAdding(true)), [])
32703275
const unavailable = String(error).startsWith('Social is not running:')
@@ -3705,14 +3710,25 @@ window.__ModuleLoader__.load({
37053710
const remote = bot?.remote
37063711
const home = remote?.home
37073712
const handle = remote?.handle
3708-
const [auto, setAuto] = useState(false)
3713+
// #3366: `null` is "nobody read it yet". A read that failed used to leave `false` here, which is
3714+
// drawn exactly like a setting that is off — and the switch stayed live, so the person could read
3715+
// the setting as one they chose. The failure is the card's answer while it lasts, and the value
3716+
// stays unknown until a read answers (#3323/#3325/#3359 for the same shape elsewhere).
3717+
const [auto, setAuto] = useState(null)
37093718
const [saving, setSaving] = useState(false)
37103719
const [busy, setBusy] = useState(false)
37113720
const [asking, setAsking] = useState(false)
37123721
const [error, setError] = useState('')
3722+
const [readError, setReadError] = useState('')
37133723
useEffect(() => {
37143724
if (!home) return undefined
3715-
call('social/settings', {}).then(value => setAuto(value?.autoAccept?.[home] === true), () => {})
3725+
let live = true
3726+
call('social/settings', {}).then(value => {
3727+
if (!live) return
3728+
setAuto(value?.autoAccept?.[home] === true)
3729+
setReadError('')
3730+
}, e => { if (live) setReadError(errorText(e)) })
3731+
return () => { live = false }
37163732
}, [home])
37173733
if (!handle) return null
37183734
const toggleAuto = async (on) => {
@@ -3759,9 +3775,10 @@ window.__ModuleLoader__.load({
37593775
// The box holds the setting only; the destructive link sits apart, below it (#274).
37603776
h('div', { className: 'fs-card fs-friend-auto', 'data-flybot': 'friend-auto-box' },
37613777
h('label', { className: 'fs-auto', title: AUTO_SEND_NOTE, 'data-flybot': 'friend-auto-send' },
3762-
h('input', { type: 'checkbox', checked: auto === true, disabled: saving || !home, onChange: e => toggleAuto(e.target.checked) }),
3778+
h('input', { type: 'checkbox', checked: auto === true, disabled: saving || !home || auto === null, onChange: e => toggleAuto(e.target.checked) }),
37633779
`Auto-send my Bots' replies to ${who}`),
3764-
h('div', { className: 'fs-hint', 'data-flybot': 'friend-auto-hint' }, `Your Bots' replies to ${who} go out without asking you first. What you type is not affected.`)),
3780+
h('div', { className: 'fs-hint', 'data-flybot': 'friend-auto-hint' }, `Your Bots' replies to ${who} go out without asking you first. What you type is not affected.`),
3781+
readError ? h('div', { className: 'fs-status', 'data-tone': 'error', role: 'status', 'data-flybot': 'friend-auto-read-error' }, readError) : null),
37653782
h('div', { className: 'fs-friend-remove', 'data-flybot': 'friend-remove-row' }, asking
37663783
? h('span', { className: 'fs-leave-ask', 'data-flybot': 'remove-friend-bot-confirm' },
37673784
h('span', { className: 'fs-hint' }, `Remove ${botWithOwner(bot.name, named)} from your list? ${keepLine}`),
@@ -3953,21 +3970,29 @@ window.__ModuleLoader__.load({
39533970
useStyles()
39543971
const handle = typeof bot?.remote?.handle === 'string' ? bot.remote.handle : ''
39553972
const [paused, setPaused] = useState(null)
3973+
// #3359: a read that failed is not a chat that is not paused. The failure is the card's answer
3974+
// while it lasts (the friends list draws it the same way, #3323), so it gets its own state and is
3975+
// drawn before the card; a later success replaces it.
3976+
const [error, setError] = useState('')
39563977
useEffect(() => {
39573978
setPaused(null)
3979+
setError('')
39583980
if (!handle) return undefined
39593981
let live = true
39603982
const load = () => {
3961-
if (typeof document !== 'undefined' && document.hidden) return
3962-
call('social/paused', { handle }).then(value => { if (live) setPaused(value?.paused ?? null) }, () => {})
3983+
call('social/paused', { handle }).then(value => {
3984+
if (!live) return
3985+
setPaused(value?.paused ?? null)
3986+
setError('')
3987+
}, e => { if (live) setError(errorText(e)) })
39633988
}
3964-
load()
3965-
if (typeof setInterval !== 'function') return () => { live = false }
3966-
const timer = setInterval(load, 5000)
3967-
timer?.unref?.()
3968-
return () => { live = false; clearInterval(timer) }
3989+
const stop = pollWhileVisible(load, 5000)
3990+
return () => { live = false; stop?.() }
39693991
}, [handle])
3970-
if (!handle || !paused) return null
3992+
if (!handle) return null
3993+
// The 5 s read is the retry: no button, no Extend, no turn count (owner 2026-10-19).
3994+
if (error) return h('div', { className: 'fs-hold', 'data-flybot': 'social-paused-error', role: 'status' }, error)
3995+
if (!paused) return null
39713996
return h('div', { className: 'fs-hold fs-chat-approval', 'data-flybot': 'chat-paused', role: 'status' },
39723997
h('div', { className: 'fs-label' }, 'Chat paused'),
39733998
h('div', { className: 'fs-draft' }, `${paused.reason ? `Bot Social paused the last chat with this Bot (${paused.reason}).` : 'Bot Social paused the last chat with this Bot.'} Send your message again: it starts a new chat.`))
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
// Shared bench for the client-side tests that draw a component from `client.js` and answer its
2+
// `__flybot__.call` reads themselves: a tiny hook runner instead of React, a document whose `hidden`
3+
// the case can flip and whose `visibilitychange` listeners it fires, and a timer table it advances.
4+
// No jsdom — the client is a classic script, so it is loaded into a fake `window` by `loadApp`.
5+
import { readFileSync } from 'node:fs'
6+
7+
// The hook slots of the component being rendered. `mount` swaps in a fresh table per component, so a
8+
// child's hooks never land in its parent's slots (the way React keeps them apart).
9+
export const live = { inst: null, idx: 0 }
10+
11+
export const reactStub = {
12+
createElement: (type, props, ...children) => ({ type, props: props ?? {}, children: children.flat(Infinity) }),
13+
Fragment: 'F',
14+
Component: class {},
15+
useState(init) {
16+
const inst = live.inst
17+
const i = live.idx++
18+
if (!(i in inst.hooks)) inst.hooks[i] = { value: typeof init === 'function' ? init() : init }
19+
const slot = inst.hooks[i]
20+
return [slot.value, (next) => { slot.value = typeof next === 'function' ? next(slot.value) : next; inst.dirty = true }]
21+
},
22+
useEffect(fn, deps) {
23+
live.inst.effects.push([live.idx++, fn, deps])
24+
},
25+
useRef(value) {
26+
const inst = live.inst
27+
const i = live.idx++
28+
if (!(i in inst.hooks)) inst.hooks[i] = { current: value }
29+
return inst.hooks[i]
30+
},
31+
useMemo(fn) { live.idx++; return fn() },
32+
useCallback(fn) { live.idx++; return fn },
33+
}
34+
35+
// The browser bundle registers its factory under its package name; load it the way the other client
36+
// tests do and hand the factory the hook stub above.
37+
export function loadApp() {
38+
const modules = new Map()
39+
const window = { __ModuleLoader__: { load: entry => modules.set(entry.id, entry.factory) } }
40+
new Function('window', readFileSync(new URL('../client.js', import.meta.url), 'utf8'))(window)
41+
return modules.get('@flybot/social')(name => (name === 'react' ? reactStub : {}))
42+
}
43+
44+
export function makeDocument({ hidden = false } = {}) {
45+
const listeners = new Map()
46+
return {
47+
hidden,
48+
getElementById: () => null,
49+
createElement: () => ({}),
50+
head: { appendChild() {} },
51+
addEventListener(type, fn) {
52+
const set = listeners.get(type) ?? new Set()
53+
set.add(fn)
54+
listeners.set(type, set)
55+
},
56+
removeEventListener(type, fn) { listeners.get(type)?.delete(fn) },
57+
listening: type => (listeners.get(type) ?? new Set()).size,
58+
emit(type) { for (const fn of [...(listeners.get(type) ?? [])]) fn() },
59+
}
60+
}
61+
62+
export function makeTimers() {
63+
let next = 0
64+
const timers = new Map()
65+
return {
66+
setInterval(fn, ms) { const id = `t${++next}`; timers.set(id, { fn, ms }); return id },
67+
clearInterval(id) { timers.delete(id) },
68+
periods: () => [...timers.values()].map(timer => timer.ms),
69+
tick() { for (const timer of [...timers.values()]) timer.fn() },
70+
}
71+
}
72+
73+
// One case: a fresh document, timer table, call log and module instance. Each `call` takes the next
74+
// queued answer; an `Error` in the queue is a rejection, and an empty queue answers `{}`.
75+
export function scene({ hidden = false, answers = [] } = {}) {
76+
const doc = makeDocument({ hidden })
77+
const timers = makeTimers()
78+
const calls = []
79+
const app = loadApp()
80+
const saved = {
81+
document: globalThis.document,
82+
setInterval: globalThis.setInterval,
83+
clearInterval: globalThis.clearInterval,
84+
flybot: globalThis.__flybot__,
85+
}
86+
let queued = [...answers]
87+
globalThis.document = doc
88+
globalThis.setInterval = timers.setInterval
89+
globalThis.clearInterval = timers.clearInterval
90+
globalThis.__flybot__ = {
91+
call: (endpoint, payload) => {
92+
calls.push([endpoint, payload])
93+
const answer = queued.length > 0 ? queued.shift() : {}
94+
return answer instanceof Error ? Promise.reject(answer) : Promise.resolve(answer)
95+
},
96+
canRetry: () => true,
97+
}
98+
return {
99+
doc, timers, calls, app,
100+
restore() {
101+
globalThis.document = saved.document
102+
globalThis.setInterval = saved.setInterval
103+
globalThis.clearInterval = saved.clearInterval
104+
globalThis.__flybot__ = saved.flybot
105+
},
106+
}
107+
}
108+
109+
export const settle = () => new Promise(resolve => setTimeout(resolve, 0))
110+
export const unmount = (cleanups) => { for (const off of cleanups.reverse()) off() }
111+
112+
// React would run the first-mount effects after drawing; here that is the call site's own immediate
113+
// read plus its timer, which is exactly what the cases observe.
114+
export function mount(app, name, props) {
115+
live.inst = { hooks: {}, effects: [], dirty: false }
116+
live.idx = 0
117+
const out = app[name](props)
118+
const cleanups = []
119+
for (const [, fn] of live.inst.effects) {
120+
const off = fn()
121+
if (typeof off === 'function') cleanups.push(off)
122+
}
123+
return { out, cleanups }
124+
}
125+
126+
// A state update asks React for a new render; this reuses the hook table and runs no effect again.
127+
export function rerender(app, name, props) {
128+
live.idx = 0
129+
return app[name](props)
130+
}
131+
132+
// Every element in a drawn tree, depth first, for counting what a state shows.
133+
export function nodes(node, out = []) {
134+
if (node == null || node === false || node === true) return out
135+
if (typeof node === 'string' || typeof node === 'number') return out
136+
if (Array.isArray(node)) { for (const child of node) nodes(child, out); return out }
137+
if (typeof node !== 'object') return out
138+
out.push(node)
139+
nodes(node.children ?? [], out)
140+
return out
141+
}
142+
143+
export const byMark = (tree, mark) => nodes(tree).filter(node => node?.props?.['data-flybot'] === mark)
144+
export const textOf = node => (Array.isArray(node?.children) ? node.children : [node?.children]).join('')

0 commit comments

Comments
 (0)