Repository navigation
fix(social): a friend Bot's held reply shows its approval card (overlay hook crash, in-chat match) (#254) #256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| A friend Bot's reply to a typed DM was held for its owner's approval, but no card ever showed: the approvals overlay crashed (a React hook after an early return) and the replying Bot's own chat did not list holds its Bot wrote. The overlay now draws, and the held reply shows as an Approve / Always approve / Reject card in the replying Bot's chat. (#254) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // The held-send overlay (FlyBotApprovals) must run the same hooks on every render: a hook after its | ||
| // early `return null` made React throw "Rendered more hooks than during the previous render" when | ||
| // the first held message arrived, so no approval card ever drew (#254). React is replaced by a small | ||
| // renderer that enforces React's rule (the hook count must not change between renders). | ||
| import assert from 'node:assert/strict' | ||
| import { registerHooks } from 'node:module' | ||
| import { test } from 'node:test' | ||
|
|
||
| const live = { inst: null, idx: 0 } | ||
| const reactStub = { | ||
| createElement: (type, props, ...children) => ({ type, props: props ?? {}, children: children.flat(Infinity) }), | ||
| Fragment: 'F', | ||
| Component: class {}, | ||
| useState(init) { | ||
| const { inst } = live | ||
| const i = live.idx++ | ||
| if (!(i in inst.hooks)) { | ||
| if (inst.rendered) throw new Error('Rendered more hooks than during the previous render.') | ||
| inst.hooks[i] = { value: typeof init === 'function' ? init() : init } | ||
| } | ||
| const slot = inst.hooks[i] | ||
| return [slot.value, (next) => { slot.value = typeof next === 'function' ? next(slot.value) : next; inst.dirty = true }] | ||
| }, | ||
| useEffect(fn, deps) { | ||
| const { inst } = live | ||
| const i = live.idx++ | ||
| if (!(i in inst.hooks)) { | ||
| if (inst.rendered) throw new Error('Rendered more hooks than during the previous render.') | ||
| inst.hooks[i] = { deps: undefined } | ||
| } | ||
| inst.effects.push([i, fn, deps]) | ||
| }, | ||
| } | ||
| // Hooks the host modules import but this test never calls. | ||
| for (const name of ['useRef', 'useMemo', 'useCallback', 'useLayoutEffect', 'useContext', 'useReducer', 'useId', 'useSyncExternalStore', 'useTransition', 'useDeferredValue', 'memo', 'forwardRef', 'createContext', 'createPortal', 'cloneElement', 'isValidElement', 'startTransition']) reactStub[name] = (value) => value | ||
| globalThis.__reactStub = reactStub | ||
| registerHooks({ | ||
| resolve(specifier, context, nextResolve) { | ||
| if (specifier === 'react') return { url: 'data:text/javascript,const r=globalThis.__reactStub;export default r;export const {createElement,Fragment,Component,useState,useEffect,useRef,useMemo,useCallback,useLayoutEffect,useContext,useReducer,useId,useSyncExternalStore,useTransition,useDeferredValue,memo,forwardRef,createContext,createPortal,cloneElement,isValidElement,startTransition}=r', shortCircuit: true } | ||
| return nextResolve(specifier, context) | ||
| }, | ||
| }) | ||
|
|
||
| globalThis.window ??= globalThis | ||
| globalThis.document ??= { createElement: () => ({ setAttribute() {} }), head: { appendChild() {} }, getElementById: () => null, addEventListener() {}, removeEventListener() {} } | ||
| function render(Component, inst) { | ||
| live.inst = inst | ||
| live.idx = 0 | ||
| inst.effects = [] | ||
| const out = Component({}) | ||
| inst.rendered = true | ||
| return out | ||
| } | ||
|
|
||
| test('#254: the approvals overlay renders again with the same hooks when the first held message arrives', async () => { | ||
| const { FlyBotApprovals } = await import('../src/client/flybot-slots.js') | ||
| const inst = { hooks: {}, effects: [], rendered: false } | ||
| assert.equal(render(FlyBotApprovals, inst), null) // nothing held yet: nothing drawn | ||
| const setPending = inst.hooks[2] // useSlot takes hooks 0 and 1 (version, subscription); pending is next | ||
| assert.ok(setPending, 'pending state exists') | ||
| setPending.value = [{ id: 'a1', status: 'pending', by: 'social', reason: 'needs approval', senderName: 'Helper', targetName: 'Chief', request: { to: { handle: '@chief.uia.home' }, text: 'Which file?', hop: 0 } }] | ||
| let tree | ||
| assert.doesNotThrow(() => { tree = render(FlyBotApprovals, inst) }) | ||
| assert.ok(tree && tree.props.className === 'bt-flybot-approvals') | ||
| const text = JSON.stringify(tree) | ||
| assert.match(text, /Which file\?/) | ||
| assert.match(text, /Approve/) | ||
| // And it goes back to nothing, still without a hook mismatch. | ||
| setPending.value = [] | ||
| assert.doesNotThrow(() => render(FlyBotApprovals, inst)) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // The held-send overlay (FlyBotApprovals) must run the same hooks on every render: a hook after its | ||
| // early `return null` made React throw "Rendered more hooks than during the previous render" when | ||
| // the first held message arrived, so no approval card ever drew (#254). React is replaced by a small | ||
| // renderer that enforces React's rule (the hook count must not change between renders). | ||
| import assert from 'node:assert/strict' | ||
| import { registerHooks } from 'node:module' | ||
| import { test } from 'node:test' | ||
|
|
||
| const live = { inst: null, idx: 0 } | ||
| const reactStub = { | ||
| createElement: (type, props, ...children) => ({ type, props: props ?? {}, children: children.flat(Infinity) }), | ||
| Fragment: 'F', | ||
| Component: class {}, | ||
| useState(init) { | ||
| const { inst } = live | ||
| const i = live.idx++ | ||
| if (!(i in inst.hooks)) { | ||
| if (inst.rendered) throw new Error('Rendered more hooks than during the previous render.') | ||
| inst.hooks[i] = { value: typeof init === 'function' ? init() : init } | ||
| } | ||
| const slot = inst.hooks[i] | ||
| return [slot.value, (next) => { slot.value = typeof next === 'function' ? next(slot.value) : next; inst.dirty = true }] | ||
| }, | ||
| useEffect(fn, deps) { | ||
| const { inst } = live | ||
| const i = live.idx++ | ||
| if (!(i in inst.hooks)) { | ||
| if (inst.rendered) throw new Error('Rendered more hooks than during the previous render.') | ||
| inst.hooks[i] = { deps: undefined } | ||
| } | ||
| inst.effects.push([i, fn, deps]) | ||
| }, | ||
| } | ||
| // Hooks the host modules import but this test never calls. | ||
| for (const name of ['useRef', 'useMemo', 'useCallback', 'useLayoutEffect', 'useContext', 'useReducer', 'useId', 'useSyncExternalStore', 'useTransition', 'useDeferredValue', 'memo', 'forwardRef', 'createContext', 'createPortal', 'cloneElement', 'isValidElement', 'startTransition']) reactStub[name] = (value) => value | ||
| globalThis.__reactStub = reactStub | ||
| registerHooks({ | ||
| resolve(specifier, context, nextResolve) { | ||
| if (specifier === 'react') return { url: 'data:text/javascript,const r=globalThis.__reactStub;export default r;export const {createElement,Fragment,Component,useState,useEffect,useRef,useMemo,useCallback,useLayoutEffect,useContext,useReducer,useId,useSyncExternalStore,useTransition,useDeferredValue,memo,forwardRef,createContext,createPortal,cloneElement,isValidElement,startTransition}=r', shortCircuit: true } | ||
| return nextResolve(specifier, context) | ||
| }, | ||
| }) | ||
|
|
||
| globalThis.window ??= globalThis | ||
| globalThis.document ??= { createElement: () => ({ setAttribute() {} }), head: { appendChild() {} }, getElementById: () => null, addEventListener() {}, removeEventListener() {} } | ||
| function render(Component, inst) { | ||
| live.inst = inst | ||
| live.idx = 0 | ||
| inst.effects = [] | ||
| const out = Component({}) | ||
| inst.rendered = true | ||
| return out | ||
| } | ||
|
|
||
| test('#254: the approvals overlay renders again with the same hooks when the first held message arrives', async () => { | ||
| const { FlyBotApprovals } = await import('../src/client/flybot-slots.js') | ||
| const inst = { hooks: {}, effects: [], rendered: false } | ||
| assert.equal(render(FlyBotApprovals, inst), null) // nothing held yet: nothing drawn | ||
| const setPending = inst.hooks[2] // useSlot takes hooks 0 and 1 (version, subscription); pending is next | ||
| assert.ok(setPending, 'pending state exists') | ||
| setPending.value = [{ id: 'a1', status: 'pending', by: 'social', reason: 'needs approval', senderName: 'Helper', targetName: 'Chief', request: { to: { handle: '@chief.uia.home' }, text: 'Which file?', hop: 0 } }] | ||
| let tree | ||
| assert.doesNotThrow(() => { tree = render(FlyBotApprovals, inst) }) | ||
| assert.ok(tree && tree.props.className === 'bt-flybot-approvals') | ||
| const text = JSON.stringify(tree) | ||
| assert.match(text, /Which file\?/) | ||
| assert.match(text, /Approve/) | ||
| // And it goes back to nothing, still without a hook mismatch. | ||
| setPending.value = [] | ||
| assert.doesNotThrow(() => render(FlyBotApprovals, inst)) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a local Bot Social send reaches
failed(for example, approval succeeds but dispatch hits a network error), this newly admitted item is rendered as a fresh pending card:ChatApprovalDock.load()explicitly fetches failed approvals, butlocalHoldView()dropsapproval.status, so every offered action sendsapproveorreject, whileapprovals.decide()accepts onlyretryfor a failed record. On the next poll the card therefore reappears and all of its controls reject as stale; preserve and handle the failed state (or leave failed entries to the global Retry card).Useful? React with 👍 / 👎.