Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions docs/architecture/rolling-upgrades.md
Original file line number Diff line number Diff line change
Expand Up @@ -1818,8 +1818,7 @@ behind does not check:
without `code`, from N-1, is shown by its `reason`.
- Build N-1 compares no hash on a rejection, accepts one without it, and rejects the
newest row of the key, whichever pending proposal the reviewer read. While both builds
serve, decide a key that several pending proposals hold only once every server is on N;
the dashboard offers no decision for a key that several of the proposals it lists hold.
serve, decide a key that several pending proposals hold only once every server is on N.
A page listed oldest first (`order="oldest"`, the dashboard's **Oldest first**) can hold
a key's older proposal while the key's newest lies past the page, and nothing on that
page shows the key is shared. While both builds serve, reject from the newest order.
15 changes: 11 additions & 4 deletions docs/features/dashboard.md
Original file line number Diff line number Diff line change
Expand Up @@ -957,10 +957,17 @@ The Proposals tab's toolbar lists the queue **Newest first** or **Oldest first**
proposals that waited longest. It counts the list, and says which end it shows when the list
is cut ("the oldest 100 of 140 proposals"). A server a release behind lists the newest first
only: the toggle goes back to **Newest first** and says the other order is not available.
For an administrator of the namespace each row has a checkbox, except
a sensitive row and a row whose key another listed proposal holds, which say why and are
decided from their own review. While rows are selected the toolbar holds **Reject…** and
**Accept…**, and each opens a plan before anything is sent:
For an administrator of the namespace each row has a checkbox, except a sensitive row,
which says why and is decided from its own review. Each of several pending proposals under
one key is decided from its own row, in its review or in a batch, and the decision names the
row by the hash of the text read. When another listed proposal of the key lists that hash,
the decision would land on that proposal, or on neither: the review offers no decision and
says why, and an **Accept…** plan leaves the text out. A batch holds one row of a key:
while one proposal of a key is selected, the checkbox of another proposal of that key is
unavailable and says why, and the select-all checkbox takes one row of each key. A link
that names such a key opens none of its proposals and says how many hold it. While rows
are selected the toolbar holds **Reject…** and **Accept…**, and each opens a plan before
anything is sent:

- **Reject…** lists every selected row's key, proposer and preview, takes one optional reason
kept on each rejected fact, and rejects the rows as listed. A rejection cannot be undone: a
Expand Down
16 changes: 11 additions & 5 deletions docs/features/memory/writing.md
Original file line number Diff line number Diff line change
Expand Up @@ -457,17 +457,21 @@ agent, when the fact was first proposed, its graded runs and the advice above th
text, and holds both decisions: the tab supplies the displayed hash when an administrator accepts,
and a rejection can carry a reason. A rejection cannot be undone: the fact has to be
proposed again.
A call decides one pending row of a key, and the tab offers no decision for a key
that more than one listed proposal holds. Accepting a proposal that recorded a
A call decides one pending row of a key, and the tab decides each of several pending
proposals of one key from its own row. When another listed proposal of the key lists the
hash of the text a review read, the review offers no decision and says why: the decision
would land on that proposal, or on neither. Accepting a proposal that recorded a
contradiction while on trial stores it `conflicted`, and it waits on the **Conflicts** tab.
An administrator can also tick rows and decide them in one call. **Reject…** takes any
number of the listed rows: its plan shows each row's key, proposer and preview, takes one
optional reason for all of them, and sends the hashes the list served. **Accept…** takes
at most 20: its plan reads each text whole, shows it with format and bidirectional
control characters drawn as code points, says which proposals land in Conflicts, leaves
out a text it could not read, and sends the hash of each read. A `high` row and a key
that more than one listed proposal holds have no checkbox and are decided from their own
review. The result counts the proposals decided and gives one line for each one left
out a text it could not read or whose hash another listed proposal of its key lists, and
sends the hash of each read. A `high` row has no checkbox and is decided from its own
review. A batch takes one row of a key: while one proposal of a key is selected, the
checkbox of another proposal of that key is unavailable.
The result counts the proposals decided and gives one line for each one left
pending, worded from its `code`, or with its `reason` sentence when the dashboard does
not know the code or the server sends none.
Learnings have a review of their own:
Expand Down Expand Up @@ -495,6 +499,8 @@ two of them share decides neither. Among several pending proposals of one key a
named by the hash `memory.proposals` lists, so a value whose listed and full-read hashes
differ, such as an object with a null field, is rejected with the listed hash, answers
`changed` to an acceptance, and is accepted once it is the key's only pending proposal.
A full-read hash that another pending proposal of the key lists names that proposal, so
compare it with the key's listed hashes before sending it.
A call whose keys hold more pending proposals than `memory_proposals_scan_cap` is refused
with a 422 and decides nothing: send fewer keys. The owner's automatic policy names no
text, so it rejects a key only while one pending proposal holds it.
Expand Down
41 changes: 39 additions & 2 deletions frontend/src/lib/useRowSelection.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import { useRowSelection } from "./useRowSelection";

const NAMES = ["one", "two", "three", "four", "five"];

function List({ names = NAMES }: { names?: string[] }) {
const sel = useRowSelection(names);
/** "two" and "three" are one group; every other row is its own. */
const paired = (name: string) => (name === "three" ? "two" : name);

function List({ names = NAMES, groupOf }: { names?: string[]; groupOf?: (name: string) => string }) {
const sel = useRowSelection(names, { groupOf });
return (
<>
<input type="checkbox" aria-label="Select all on this page" checked={sel.allSelected} onChange={sel.toggleAll} />
Expand Down Expand Up @@ -88,4 +91,38 @@ describe("useRowSelection", () => {
rerender(<List />);
expect(picked()).toBe("four");
});

it("keeps one selected row of a group on a tick and on select-all", async () => {
// Fails when a tick or the select-all checkbox selects two rows of one group, or when
// select-all drops the row of a group that was selected before it.
render(<List groupOf={paired} />);
const user = userEvent.setup();
const all = screen.getByLabelText("Select all on this page");
await user.click(screen.getByLabelText("Select three"));
await user.click(screen.getByLabelText("Select two"));
expect(picked()).toBe("three");
await user.click(all);
expect(new Set(picked()?.split(","))).toEqual(new Set(["one", "three", "four", "five"]));
// One row of every group is selected, so the next click clears them.
expect(all).toBeChecked();
await user.click(all);
expect(picked()).toBe("");
// With none selected, each group gives its first row on the page.
await user.click(all);
expect(picked()).toBe("one,two,four,five");
});

it("a range passes over the other row of a group, and takes the row clicked", async () => {
// Fails when a Shift-click selects two rows of one group, or leaves the clicked row out
// for an unselected row of its group earlier in the range.
render(<List groupOf={paired} />);
const user = userEvent.setup();
await user.click(screen.getByLabelText("Select one"));
await user.keyboard("{Shift>}");
await user.click(screen.getByLabelText("Select three"));
expect(picked()).toBe("one,three");
await user.click(screen.getByLabelText("Select five"));
await user.keyboard("{/Shift}");
expect(new Set(picked()?.split(","))).toEqual(new Set(["one", "three", "four", "five"]));
});
});
42 changes: 36 additions & 6 deletions frontend/src/lib/useRowSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface RowSelection {
/** Toggle one row; with `range` (a Shift-click), select every row from the last one
* toggled to this one instead. */
toggle: (key: string, range?: boolean) => void;
/** Every row on the page is selected (and there is one). */
/** Every row on the page is selected (and there is one); with `groupOf`, one of each group. */
allSelected: boolean;
/** Select every row on the page, or clear them all once they are. */
toggleAll: () => void;
Expand All @@ -16,6 +16,19 @@ export interface RowSelection {
drop: (keys: readonly string[]) => void;
}

/** The group of a row on a page that names none: the row alone. */
const alone = (key: string) => key;

/** Adds each of `more` to `next`, in its order, unless a row of its group is there already. */
function add(next: Set<string>, more: readonly string[], group: (key: string) => string) {
const held = new Set([...next].map(group));
for (const key of more) {
if (held.has(group(key))) continue;
held.add(group(key));
next.add(key);
}
}

/**
* The checkbox selection of a list page: one row at a time as Click UI's selectable Table,
* plus Loom's own range and clear (Click UI's Table has neither): a Shift-click selects the
Expand All @@ -24,8 +37,15 @@ export interface RowSelection {
* page). `keys` are the page's rows in their order on screen; a selected row that leaves the
* page (a new page, a search) leaves the selection, so a bulk action only ever acts on rows
* in view.
*
* `groupOf` names a row's group, for a bulk action that takes one row of each: a tick, a
* range and the select-all checkbox pass over a row while another row of its group is
* selected. A range takes the row clicked before any other row of its group.
*/
export function useRowSelection(keys: readonly string[], { enabled = true }: { enabled?: boolean } = {}): RowSelection {
export function useRowSelection(
keys: readonly string[],
{ enabled = true, groupOf = alone }: { enabled?: boolean; groupOf?: (key: string) => string } = {},
): RowSelection {
const [picked, setPicked] = useState<ReadonlySet<string>>(new Set());
const onPage = useMemo(() => new Set(keys), [keys]);
const selected = useMemo(() => {
Expand All @@ -41,6 +61,8 @@ export function useRowSelection(keys: readonly string[], { enabled = true }: { e
keysRef.current = keys;
const selectedRef = useRef(selected);
selectedRef.current = selected;
const groupRef = useRef(groupOf);
groupRef.current = groupOf;
const anchor = useRef<string | null>(null);

const toggle = useCallback((key: string, range = false) => {
Expand All @@ -50,11 +72,14 @@ export function useRowSelection(keys: readonly string[], { enabled = true }: { e
const to = all.indexOf(key);
if (range && from >= 0 && to >= 0) {
const [lo, hi] = from < to ? [from, to] : [to, from];
for (const k of all.slice(lo, hi + 1)) next.add(k);
const group = groupRef.current;
// Of the rows of the clicked row's group, the range holds the clicked one alone.
const span = all.slice(lo, hi + 1).filter((k) => k === key || group(k) !== group(key));
add(next, span, group);
} else if (next.has(key)) {
next.delete(key);
} else {
next.add(key);
add(next, [key], groupRef.current);
}
anchor.current = key;
setPicked(next);
Expand All @@ -72,11 +97,16 @@ export function useRowSelection(keys: readonly string[], { enabled = true }: { e
setPicked((prev) => new Set([...prev].filter((k) => !out.has(k))));
}, []);

const allSelected = keys.length > 0 && selected.size === keys.length;
// The most rows a selection holds: one of each group.
const most = useMemo(() => new Set(keys.map(groupOf)).size, [keys, groupOf]);
const allSelected = keys.length > 0 && selected.size === most;
const toggleAll = useCallback(() => {
anchor.current = null;
const all = keysRef.current;
setPicked(selectedRef.current.size === all.length ? new Set() : new Set(all));
const next = new Set(selectedRef.current);
add(next, all, groupRef.current);
// Nothing was left to take: every row that can be selected is, so the click clears them.
setPicked(next.size === selectedRef.current.size ? new Set() : new Set(all.filter((k) => next.has(k))));
}, []);

const escape = useMemo(() => ({ Escape: clear }), [clear]);
Expand Down
Loading
Loading