diff --git a/apps/app/src/groups.js b/apps/app/src/groups.js
index d293d209..951abc06 100644
--- a/apps/app/src/groups.js
+++ b/apps/app/src/groups.js
@@ -205,15 +205,18 @@ export function needsDeviceJoin (info) {
return MEMBER_ROLES.includes(info.role) && info.writerHere === false
}
-// The seeder never frees a device slot (8 per root); the app stops entering by
-// itself at 6 and leaves the last two to the person.
+// Each account has 8 live device slots per root (writersBound). A slot comes
+// back only when a device is disconnected, and only while the root's frozen
+// reserve lasts (#350); an older node never gives one back. The app stops
+// entering by itself at 6 and leaves the last two to the person.
export function reserveReachedAny (counts) {
if (!Array.isArray(counts)) return false
return counts.some((n) => Number.isSafeInteger(n) && n >= 6)
}
// At the reserve the first tap only shows the warning; the person confirms
-// with a second tap, because the slot is never given back.
+// with a second tap, because the slot comes back at best by disconnecting a
+// device.
export function needsReserveConfirm (counts, confirmed) {
return confirmed !== true && reserveReachedAny(counts)
}
@@ -230,6 +233,84 @@ export function communityReserveReached (info) {
return reserveReachedAny([info.writersBound, info.generalWritersBound])
}
+const ACCOUNT_SLOTS = 8
+
+// The roots a join spends a slot in, as { bound, room } (writersBound and
+// frozenRoom): group.join in the group, community.join in the community and
+// in Geral.
+export function groupSlots (info) {
+ if (!info || typeof info !== 'object') return []
+ return [{ bound: info.writersBound, room: info.frozenRoom }]
+}
+
+export function communitySlots (info) {
+ if (!info || typeof info !== 'object') return []
+ return [{ bound: info.writersBound, room: info.frozenRoom }, { bound: info.generalWritersBound, room: info.generalFrozenRoom }]
+}
+
+// What disconnecting a device does in those roots, decided by the field the
+// node reports, never by the platform: 'returns' while every root has frozen
+// reserve left, 'final' once one has none, 'legacy' when the node omits
+// frozenRoom (an older node, where a slot never comes back).
+export function slotReturn (slots) {
+ const rooms = (Array.isArray(slots) ? slots : []).map((s) => s && s.room).filter((n) => Number.isSafeInteger(n) && n >= 0)
+ if (rooms.length === 0) return 'legacy'
+ return rooms.some((n) => n === 0) ? 'final' : 'returns'
+}
+
+// The way to the screen, as Ajustes lists it (DEVICES_TITLE in devices.js;
+// the test holds the two together).
+const DEVICES_PATH = 'Ajustes, Aparelhos conectados'
+const SLOT_RETURNS_TEXT = 'Desconectar um aparelho antigo em ' + DEVICES_PATH + ', devolve a vaga.'
+// With no frozen reserve a disconnect's mark of 1 or more frees no slot; a
+// mark at 0 still releases one while the diary has releases left.
+const SLOT_FINAL_TEXT = 'desconectar um aparelho pode não devolver a vaga.'
+
+function slotPlace (where) {
+ return where === 'comunidade' ? 'nesta comunidade' : 'neste grupo'
+}
+
+// The warning at the reserve. `where` is 'grupo' or 'comunidade'.
+export function reserveText (slots, where = 'grupo') {
+ const r = slotReturn(slots)
+ if (r === 'legacy') return DEVICE_RESERVE_TEXT
+ return 'Cada aparelho usa uma das 8 vagas da conta ' + slotPlace(where) + '. ' + (r === 'final' ? 'Aqui, ' + SLOT_FINAL_TEXT : SLOT_RETURNS_TEXT)
+}
+
+// The community info note for a device not in the community yet, at the
+// reserve.
+export function reserveJoinText (slots) {
+ if (slotReturn(slots) === 'legacy') return DEVICE_RESERVE_JOIN_TEXT
+ return 'Este aparelho ainda não participa desta comunidade. Para participar por ele, toque em Participar neste aparelho na lista de grupos. ' + reserveText(slots, 'comunidade')
+}
+
+// The node refuses a full group and an account at its 8 live devices there
+// with the same words: the counts tell them apart. -> the text for the
+// person, or null to keep the node's message (a full group, an older node).
+export function slotFullText (slots, where = 'grupo') {
+ const full = (Array.isArray(slots) ? slots : []).filter((s) => s && Number.isSafeInteger(s.bound) && s.bound >= ACCOUNT_SLOTS)
+ const r = slotReturn(full)
+ if (full.length === 0 || r === 'legacy') return null
+ const head = 'Sua conta já usa as 8 vagas de aparelho ' + slotPlace(where)
+ return r === 'final' ? head + ', e aqui ' + SLOT_FINAL_TEXT : head + '. Para liberar a vaga, desconecte um aparelho antigo em ' + DEVICES_PATH + '.'
+}
+
+const FULL_REFUSALS = Object.freeze(['limite do grupo atingido', 'limite da comunidade atingido'])
+
+// After a full-root refusal the counts are read again (`read`, which may
+// throw): the rendered ones may predate a device that just enrolled.
+// `slotsOf` is groupSlots or communitySlots. -> the error to show: a new one
+// with the slot text, else `err` itself (an uncertain answer always is).
+export async function slotRefusal (err, read, slotsOf, where = 'grupo') {
+ if (!err || err.uncertain === true || !FULL_REFUSALS.includes(err.message)) return err
+ let info = null
+ try {
+ info = await read()
+ } catch {}
+ const text = slotFullText(slotsOf(info), where)
+ return text === null ? err : new Error(text)
+}
+
export function writerOnDevice (info) {
if (!info || typeof info !== 'object') return true
return info.writerHere !== false
diff --git a/apps/app/src/screens/Comunidade.js b/apps/app/src/screens/Comunidade.js
index b6b1911e..03f2e7fe 100644
--- a/apps/app/src/screens/Comunidade.js
+++ b/apps/app/src/screens/Comunidade.js
@@ -27,8 +27,8 @@ import { fetchCommunity } from '../indexClient'
import { relativeTime } from '../format'
import { readsAs } from '../who'
import {
- AS_PAGE_LABEL, COMMUNITY_JOIN_LABELS, DEVICE_JOIN_LABEL, DEVICE_JOIN_TEXT, DEVICE_RESERVE_TEXT, DEVICE_WAIT_TEXT, GENERAL_LABEL, OWN_LABEL, PAGE_JOIN_LABEL, PAGE_LEAVE_LABEL, PRIVATE_LABEL,
- can, communityReserveReached, lastIsOwn, needsReserveConfirm, lastLine, membersLabel, needsDeviceJoin, pageCommunities, pageErrorText, pageMode, pageRoleLabel, pageRoleOf, reconcile, roleLabel, showNewGroup, unreadLabel, visibilityLabel
+ AS_PAGE_LABEL, COMMUNITY_JOIN_LABELS, DEVICE_JOIN_LABEL, DEVICE_JOIN_TEXT, DEVICE_WAIT_TEXT, GENERAL_LABEL, OWN_LABEL, PAGE_JOIN_LABEL, PAGE_LEAVE_LABEL, PRIVATE_LABEL,
+ can, communityReserveReached, communitySlots, lastIsOwn, needsReserveConfirm, reserveText, slotRefusal, lastLine, membersLabel, needsDeviceJoin, pageCommunities, pageErrorText, pageMode, pageRoleLabel, pageRoleOf, reconcile, roleLabel, showNewGroup, unreadLabel, visibilityLabel
} from '../groups'
import { pagesOn } from '../pageGate'
import { actAs, pagesGroup } from '../pages'
@@ -378,7 +378,13 @@ export function Comunidade ({ community, afterGroup, isTop, seenTick, onBack, on
const doJoin = async () => {
setJoinNote('')
setCheck(null)
- const res = await join.run(() => requestRef.current('community.join', { community }))
+ const res = await join.run(async () => {
+ try {
+ return await requestRef.current('community.join', { community })
+ } catch (err) {
+ throw await slotRefusal(err, () => requestRef.current('community.info', { community }), communitySlots, 'comunidade')
+ }
+ })
if (res.ok) {
const v = res.value || {}
if (v.group) setJoinNote('Você entrou no Geral.')
@@ -405,8 +411,9 @@ export function Comunidade ({ community, afterGroup, isTop, seenTick, onBack, on
setJoinNote('')
setCheck(null)
// At the reserve the first tap only shows the warning: the slot it would
- // spend (community or Geral) never comes back. The counts are read fresh:
- // the rendered ones may predate a writer that just landed.
+ // spend (community or Geral) comes back at best by disconnecting a device.
+ // The counts are read fresh: the rendered ones may predate a writer that
+ // just landed.
let fresh = info
if (!reserveConfirm) {
try {
@@ -417,7 +424,13 @@ export function Comunidade ({ community, afterGroup, isTop, seenTick, onBack, on
setReserveConfirm(true)
return
}
- const res = await join.run(() => requestRef.current('community.join', { community }))
+ const res = await join.run(async () => {
+ try {
+ return await requestRef.current('community.join', { community })
+ } catch (err) {
+ throw await slotRefusal(err, () => requestRef.current('community.info', { community }), communitySlots, 'comunidade')
+ }
+ })
if (res.ok) {
setDeviceJoinSent(true)
load()
@@ -554,7 +567,7 @@ export function Comunidade ({ community, afterGroup, isTop, seenTick, onBack, on
? (
{DEVICE_JOIN_TEXT}
- {reserveConfirm || communityReserveReached(info) ? {DEVICE_RESERVE_TEXT} : null}
+ {reserveConfirm || communityReserveReached(info) ? {reserveText(communitySlots(info), 'comunidade')} : null}
{join.error}
diff --git a/apps/app/src/screens/ComunidadeInfo.js b/apps/app/src/screens/ComunidadeInfo.js
index 6e8ec0c7..bdedd2fb 100644
--- a/apps/app/src/screens/ComunidadeInfo.js
+++ b/apps/app/src/screens/ComunidadeInfo.js
@@ -27,8 +27,8 @@ import { useMemberSearch } from '../useMemberSearch'
import { whoOf } from '../who'
import { shortKey } from '../format'
import {
- COMMUNITY_JOIN_LABELS, DEVICE_PENDING_JOIN_TEXT, DEVICE_RESERVE_JOIN_TEXT, DEVICE_ROOT_TEXT, DEVICE_STAFF_TEXT,
- authorityHere, can, communityReserveReached, deviceStaffNote, membersLabel, rootAdminHere, roleLabel, writerOnDevice
+ COMMUNITY_JOIN_LABELS, DEVICE_PENDING_JOIN_TEXT, DEVICE_ROOT_TEXT, DEVICE_STAFF_TEXT,
+ authorityHere, can, communityReserveReached, communitySlots, deviceStaffNote, membersLabel, reserveJoinText, rootAdminHere, roleLabel, writerOnDevice
} from '../groups'
import { Btn, ConfirmStack, ErrorText, Field, SectionHeader, Soft, UNCERTAIN, errorText, useAction, useBottomPad } from '../ui'
import { refusalOf } from '../wordCheck'
@@ -493,7 +493,7 @@ export function ComunidadeInfo ({ community, onBack, onOpenMember, onMessage })
)
: null}
- {staffNote ? {staffNote === 'join' ? (communityReserveReached(info) ? DEVICE_RESERVE_JOIN_TEXT : DEVICE_PENDING_JOIN_TEXT) : staffNote === 'root' ? DEVICE_ROOT_TEXT : DEVICE_STAFF_TEXT} : null}
+ {staffNote ? {staffNote === 'join' ? (communityReserveReached(info) ? reserveJoinText(communitySlots(info)) : DEVICE_PENDING_JOIN_TEXT) : staffNote === 'root' ? DEVICE_ROOT_TEXT : DEVICE_STAFF_TEXT} : null}
{staff && authOk
? (
diff --git a/apps/app/src/screens/Grupo.js b/apps/app/src/screens/Grupo.js
index 138156b5..37a6dcc6 100644
--- a/apps/app/src/screens/Grupo.js
+++ b/apps/app/src/screens/Grupo.js
@@ -65,9 +65,9 @@ import { UNNAMED, readsAs, whoOf } from '../who'
import { copyText } from '../share'
import { shareNav } from '../shareNav'
import {
- AS_PAGE_LABEL, DEVICE_APPROVAL_TEXT, DEVICE_JOIN_LABEL, DEVICE_JOIN_TEXT, DEVICE_RESERVE_TEXT, DEVICE_WAIT_TEXT,
+ AS_PAGE_LABEL, DEVICE_APPROVAL_TEXT, DEVICE_JOIN_LABEL, DEVICE_JOIN_TEXT, DEVICE_WAIT_TEXT,
EMPTY_PAGES, OWN_LABEL, PAGE_GROUP_LOADING_TEXT, PAGE_NO_KEY_TEXT, PAGE_JOIN_LABEL, PAGE_LEAVE_LABEL, PAGE_REKEYING_TEXT, PAGE_SEAL_AGAIN_LABEL, PRIVATE_LABEL, REKEY_PENDING_LABEL,
- applyPage, mergeNewest, reloadDone, reloadWalks, can, canWriteHere, reserveReachedAny, needsReserveConfirm, itemsOf, knownVisibility, membersLabel, needsDeviceJoin, pageCan, pagePostVisibility, postAsPage, readAsPage, pageGroupErrorText, pageMode, pageRoleLabel, pageRoleOf, reconcile, roleLabel, roleOf, sealRaced, sealedLabel, visibilityLabel
+ applyPage, mergeNewest, reloadDone, reloadWalks, can, canWriteHere, communitySlots, groupSlots, reserveReachedAny, reserveText, slotRefusal, needsReserveConfirm, itemsOf, knownVisibility, membersLabel, needsDeviceJoin, pageCan, pagePostVisibility, postAsPage, readAsPage, pageGroupErrorText, pageMode, pageRoleLabel, pageRoleOf, reconcile, roleLabel, roleOf, sealRaced, sealedLabel, visibilityLabel
} from '../groups'
import { Btn, ConfirmStack, ErrorText, Notice, Soft, errorText, useAction, useNow } from '../ui'
import { useStyles, useTheme } from '../ui/ThemeProvider'
@@ -204,7 +204,7 @@ function GrupoScreen ({ group, onBack, onOpenCommunity, onOpenMember, onMessage,
const [communityJoin, setCommunityJoin] = useState(null)
const [communityWriterHere, setCommunityWriterHere] = useState(null)
// The community's and Geral's bound writers: the tap spends slots there too when it calls community.join.
- const [communityBound, setCommunityBound] = useState([])
+ const [communitySlotsKnown, setCommunitySlotsKnown] = useState([])
const [needCommunity, setNeedCommunity] = useState(false)
// Set once a device join was sent (answered or not): a second tap could bind another writer.
const [deviceJoinSent, setDeviceJoinSent] = useState(false)
@@ -1280,7 +1280,7 @@ function GrupoScreen ({ group, onBack, onOpenCommunity, onOpenMember, onMessage,
if (typeof c.name === 'string') setCommunityName(c.name)
setCommunityJoin(typeof c.join === 'string' ? c.join : null)
setCommunityWriterHere(typeof c.writerHere === 'boolean' ? c.writerHere : null)
- setCommunityBound([c.writersBound, c.generalWritersBound])
+ setCommunitySlotsKnown(communitySlots(c))
}, () => {})
return () => { live = false }
}, [hosted, heldCommunity, request, version])
@@ -1341,7 +1341,12 @@ function GrupoScreen ({ group, onBack, onOpenCommunity, onOpenMember, onMessage,
held = true
return null
}
- const joined = await request('community.join', { community: communityKey })
+ let joined
+ try {
+ joined = await request('community.join', { community: communityKey })
+ } catch (err) {
+ throw await slotRefusal(err, () => request('community.info', { community: communityKey }), communitySlots, 'comunidade')
+ }
// Landed: a later tap must not send community.join again.
setCommunityWriterHere(true)
if (joined && joined.group === group) return joined
@@ -1351,7 +1356,11 @@ function GrupoScreen ({ group, onBack, onOpenCommunity, onOpenMember, onMessage,
held = true
return null
}
- return await request('group.join', { group })
+ try {
+ return await request('group.join', { group })
+ } catch (err) {
+ throw await slotRefusal(err, () => request('group.info', { group }), groupSlots)
+ }
})
if (held) {
setReserveConfirm(true)
@@ -1693,10 +1702,11 @@ function GrupoScreen ({ group, onBack, onOpenCommunity, onOpenMember, onMessage,
const replyBar = replyKey !== null && !asPage.page ? (quoteOf(replyKey) || replyQuote) : null
let composer = null
if (reader && showJoinCard && !orphan) {
+ const reserveSlots = [...groupSlots(info), ...(communityWriterHere === false ? communitySlotsKnown : [])]
composer = (
{DEVICE_JOIN_TEXT}
- {reserveConfirm || reserveReachedAny([info && info.writersBound, ...(communityWriterHere === false ? communityBound : [])]) ? {DEVICE_RESERVE_TEXT} : null}
+ {reserveConfirm || reserveReachedAny(reserveSlots.map((s) => s.bound)) ? {reserveText(reserveSlots)} : null}
{join.error}
diff --git a/apps/app/src/vitrine/request.js b/apps/app/src/vitrine/request.js
index ac085252..a8d3aea6 100644
--- a/apps/app/src/vitrine/request.js
+++ b/apps/app/src/vitrine/request.js
@@ -1174,7 +1174,7 @@ export function createVitrineRequest (fixtures, options) {
},
'group.info': ({ group }) => {
const g = groupOf(group)
- return { ...groupRow(g), creator: g.creator, state: active(g) ? 'member' : g.role, orphan: communityOf(g.community).dropped.includes(g.group), visibility: g.visibility, memberInvite: g.memberInvite, writerHere: true, canWrite: active(g) && active(communityOf(g.community)) && !communityOf(g.community).dropped.includes(g.group), writersBound: 1 }
+ return { ...groupRow(g), creator: g.creator, state: active(g) ? 'member' : g.role, orphan: communityOf(g.community).dropped.includes(g.group), visibility: g.visibility, memberInvite: g.memberInvite, writerHere: true, canWrite: active(g) && active(communityOf(g.community)) && !communityOf(g.community).dropped.includes(g.group), writersBound: 1, writersFrozen: 0, frozenRoom: 8 }
},
'group.seen': ({ group, upTo }) => {
const g = groupOf(group)
@@ -1229,7 +1229,11 @@ export function createVitrineRequest (fixtures, options) {
rootHere: c.creator === data.own,
authorityHere: active(c) && (c.role === 'admin' || c.role === 'creator'),
writersBound: 1,
- generalWritersBound: 1
+ writersFrozen: 0,
+ frozenRoom: 8,
+ generalWritersBound: 1,
+ generalWritersFrozen: 0,
+ generalFrozenRoom: 8
}
},
'feed.read': ({ limit, before }) => ({
diff --git a/apps/app/test/groups.test.js b/apps/app/test/groups.test.js
index 70a5bc3f..ee4a142b 100644
--- a/apps/app/test/groups.test.js
+++ b/apps/app/test/groups.test.js
@@ -265,6 +265,62 @@ test('device join: this device writes, or the account still has to join here', a
t.is(DEVICE_APPROVAL_TEXT, 'Este aparelho precisa da aprovação de um administrador para escrever.')
})
+test('device slots: the reserve warning and the full refusal follow frozenRoom (#350)', async (t) => {
+ const { groupSlots, communitySlots, slotReturn, reserveText, reserveJoinText, slotFullText, slotRefusal, DEVICE_RESERVE_TEXT, DEVICE_RESERVE_JOIN_TEXT } = await load()
+ const returns = 'Cada aparelho usa uma das 8 vagas da conta neste grupo. Desconectar um aparelho antigo em Ajustes, Aparelhos conectados, devolve a vaga.'
+ const final = 'Cada aparelho usa uma das 8 vagas da conta neste grupo. Aqui, desconectar um aparelho pode não devolver a vaga.'
+ const { DEVICES_TITLE } = await import('../src/devices.js')
+ t.ok(returns.includes('Ajustes, ' + DEVICES_TITLE + ','), 'names the Ajustes entry by its label')
+ t.alike(groupSlots({ writersBound: 6, frozenRoom: 3 }), [{ bound: 6, room: 3 }])
+ t.alike(communitySlots({ writersBound: 2, frozenRoom: 8, generalWritersBound: 6, generalFrozenRoom: 1 }), [{ bound: 2, room: 8 }, { bound: 6, room: 1 }])
+ t.alike(groupSlots(null), [])
+ t.alike(communitySlots(undefined), [])
+
+ // Decided by the field: an older node omits it, and there a slot never
+ // comes back.
+ t.is(slotReturn([{ bound: 6 }]), 'legacy')
+ t.is(slotReturn([{ bound: 6, room: '3' }, { bound: 6, room: 2.5 }]), 'legacy')
+ t.is(slotReturn([{ bound: 6, room: 3 }]), 'returns')
+ t.is(slotReturn([{ bound: 6, room: 3 }, { bound: 2, room: 0 }]), 'final', 'one root without frozen reserve')
+ t.is(slotReturn(null), 'legacy')
+
+ t.is(reserveText([{ bound: 6, room: 3 }]), returns)
+ t.is(reserveText([{ bound: 6, room: 0 }]), final)
+ t.is(reserveText([{ bound: 6 }]), DEVICE_RESERVE_TEXT, 'older node: the old words')
+ t.is(reserveText([{ bound: 6, room: 8 }], 'comunidade'), returns.replace('neste grupo', 'nesta comunidade'))
+ t.is(reserveJoinText([{ bound: 6 }]), DEVICE_RESERVE_JOIN_TEXT)
+ t.ok(reserveJoinText([{ bound: 6, room: 1 }]).includes('Participar neste aparelho'))
+ t.ok(reserveJoinText([{ bound: 6, room: 1 }]).endsWith(returns.replace('neste grupo', 'nesta comunidade')))
+ t.ok(reserveJoinText([{ bound: 6, room: 0 }]).endsWith('Aqui, desconectar um aparelho pode não devolver a vaga.'))
+
+ // With 8 live the refusal points to Ajustes, Aparelhos conectados; with no frozen
+ // reserve it says a disconnect may not give the slot back there.
+ t.is(slotFullText([{ bound: 8, room: 2 }]), 'Sua conta já usa as 8 vagas de aparelho neste grupo. Para liberar a vaga, desconecte um aparelho antigo em Ajustes, Aparelhos conectados.')
+ t.is(slotFullText([{ bound: 8, room: 0 }]), 'Sua conta já usa as 8 vagas de aparelho neste grupo, e aqui desconectar um aparelho pode não devolver a vaga.')
+ t.is(slotFullText([{ bound: 3, room: 0 }, { bound: 8, room: 5 }], 'comunidade'), 'Sua conta já usa as 8 vagas de aparelho nesta comunidade. Para liberar a vaga, desconecte um aparelho antigo em Ajustes, Aparelhos conectados.', 'only the full root decides')
+ t.is(slotFullText([{ bound: 7, room: 2 }]), null, 'a full group, not the account: the node\'s words')
+ t.is(slotFullText([{ bound: 8 }]), null, 'older node: the node\'s words')
+ t.is(slotFullText([]), null)
+
+ const full = Object.assign(new Error('limite do grupo atingido'), { code: 'USAGE' })
+ let reads = 0
+ const read = (info) => async () => {
+ reads++
+ return info
+ }
+ t.is((await slotRefusal(full, read({ writersBound: 8, frozenRoom: 1 }), groupSlots)).message, 'Sua conta já usa as 8 vagas de aparelho neste grupo. Para liberar a vaga, desconecte um aparelho antigo em Ajustes, Aparelhos conectados.')
+ t.is((await slotRefusal(new Error('limite da comunidade atingido'), read({ writersBound: 4, frozenRoom: 8, generalWritersBound: 8, generalFrozenRoom: 0 }), communitySlots, 'comunidade')).message, 'Sua conta já usa as 8 vagas de aparelho nesta comunidade, e aqui desconectar um aparelho pode não devolver a vaga.')
+ t.is(await slotRefusal(full, read({ writersBound: 5, frozenRoom: 8 }), groupSlots), full, 'a full group keeps the error')
+ t.is(await slotRefusal(full, async () => { throw new Error('sem conexão com o nó') }, groupSlots), full, 'a failed read keeps the error')
+ const before = reads
+ const other = new Error('grupo não encontrado')
+ t.is(await slotRefusal(other, read({ writersBound: 8, frozenRoom: 1 }), groupSlots), other)
+ const uncertain = Object.assign(new Error('limite do grupo atingido'), { uncertain: true })
+ t.is(await slotRefusal(uncertain, read({ writersBound: 8, frozenRoom: 1 }), groupSlots), uncertain, 'an uncertain answer stays uncertain')
+ t.is(reads, before, 'only a full-root refusal reads again')
+ for (const text of [returns, final, reserveJoinText([{ bound: 6, room: 0 }]), slotFullText([{ bound: 8, room: 0 }])]) t.absent(text.includes('\u2014'))
+})
+
test('joinLink builds the path; the paste parsers are gone', async (t) => {
const g = await load()
const { joinLink } = g
diff --git a/apps/app/test/vitrine.test.js b/apps/app/test/vitrine.test.js
index ad9c0945..efdb0016 100644
--- a/apps/app/test/vitrine.test.js
+++ b/apps/app/test/vitrine.test.js
@@ -135,11 +135,11 @@ const resultKeys = {
'dm.lookup': ['box', 'gen'],
'group.list': ['groups', 'total'],
'group.read': ['items', 'next', 'hidden', 'incomplete', 'sealed', 'epoch', 'state'],
- 'group.info': ['group', 'name', 'join', 'creator', 'role', 'members', 'state', 'community', 'orphan', 'visibility', 'memberInvite', 'writerHere', 'canWrite', 'writersBound'],
+ 'group.info': ['group', 'name', 'join', 'creator', 'role', 'members', 'state', 'community', 'orphan', 'visibility', 'memberInvite', 'writerHere', 'canWrite', 'writersBound', 'writersFrozen', 'frozenRoom'],
'group.seen': ['ok'],
'group.post': ['seq'],
'community.list': ['communities'],
- 'community.info': ['community', 'name', 'join', 'creator', 'role', 'state', 'members', 'admins', 'groups', 'general', 'incomplete', 'dropped', 'repair', 'leavePending', 'memberInvite', 'writerHere', 'canWrite', 'rootHere', 'authorityHere', 'writersBound', 'generalWritersBound'],
+ 'community.info': ['community', 'name', 'join', 'creator', 'role', 'state', 'members', 'admins', 'groups', 'general', 'incomplete', 'dropped', 'repair', 'leavePending', 'memberInvite', 'writerHere', 'canWrite', 'rootHere', 'authorityHere', 'writersBound', 'writersFrozen', 'frozenRoom', 'generalWritersBound', 'generalWritersFrozen', 'generalFrozenRoom'],
'feed.read': ['items', 'skipped', 'skippedTruncated', 'future', 'futureTruncated', 'incomplete', 'incompleteTruncated'],
'notices.list': ['notices', 'next', 'incomplete', 'supported', 'activity'],
'profile.read': ['profiles', 'missing'],
diff --git a/apps/cli/lib/account/pages-groups.js b/apps/cli/lib/account/pages-groups.js
index c696b96a..82b99d51 100644
--- a/apps/cli/lib/account/pages-groups.js
+++ b/apps/cli/lib/account/pages-groups.js
@@ -901,8 +901,9 @@ function createPagesGroups ({ dir, writer, pagesStore, keys, association, blockl
}
// Before a new writer for `group`: at most writersCap writers ever bound
- // by the page diary in it (the fold keeps the earliest), at most groupsCap
- // groups and communities with a live join.
+ // by the page diary in it, at most groupsCap groups and communities with a
+ // live join. Every writer ever bound counts, which is stricter than the
+ // fold's slot walk (#350), where a diary cut gives a slot back.
function roomFor (loaded, page, group) {
const bound = new Set((loaded.input.joins.get(page) || []).filter((j) => j.on).map((j) => j.writer))
if (bound.size >= writersCap) throw fail('limite')
diff --git a/docs/app.md b/docs/app.md
index 85a5bb3f..6612696d 100644
--- a/docs/app.md
+++ b/docs/app.md
@@ -430,7 +430,7 @@ A tela da comunidade é a lista dos seus grupos. A barra do topo mostra o avatar
- **Grupos.** Lista com o **Geral** primeiro, marcado como grupo principal; cada linha mostra o nome atual do grupo, mesmo que o Geral tenha sido renomeado. **Novo grupo** só aparece para quem criou a comunidade, e só no aparelho que a criou. Quando quem criou voltou depois de sair (`repair` em `community.info`), cada grupo, menos o Geral, mostra **Tirar da lista**, com a confirmação "Tirar este grupo da lista da comunidade? Ele deixa de aparecer aqui e ninguém mais escreve nele." (chama `community.removeGroup`). Com `repair` ligado e algum grupo tirado (`dropped`), a tela mostra a seção **Tirados da lista**: cada grupo aparece pelo nome (lido em `group.info` quando a raiz está no aparelho; senão, "Grupo tirado da lista", nunca a chave) e tem **Devolver à lista**, com a confirmação "Devolver este grupo à lista da comunidade?" (chama `community.removeGroup` com `undo`).
- **Última mensagem e não lidas.** Cada linha mostra quem escreveu por último e o texto, a hora e, quando há, um ponto na cor de destaque no lugar da contagem de mensagens não lidas (o leitor de tela continua lendo o número). A sua própria última mensagem mostra "Você:" em destaque, desenhado à parte do texto, para que o nome de um par nunca se passe por ele. Mensagem escondida aparece só como "mensagem escondida"; num grupo privado que este aparelho não consegue abrir, a linha não mostra texto cifrado. Abrir o grupo zera o ponto neste aparelho e, pelo bloco de vistos, nos outros aparelhos da mesma passkey (veja **Avisos**, "Visto em todos os aparelhos"); nada sai daqui para o grupo nem para as outras pessoas. Em todos os aparelhos, a contagem de um grupo segue a marca compartilhada: um grupo que outro aparelho leu por inteiro sai da contagem aqui também, e o ponto da aba (`comunidades`) some quando nenhum grupo tem não lidas. O bloco guarda até 128 grupos; um grupo que sai dele pode voltar como não lido em outro aparelho. Voltar à lista relê as contagens, e elas continuam depois de recarregar o app (na web, a página). A marca de lido fica por aparelho, para no máximo 512 grupos; um grupo cuja marca foi descartada (a menos marcada recentemente) volta a contar como nunca lido. A primeira visita pode mostrar não lidas antigas até cada grupo ser aberto. Só a tela do grupo que está no topo marca como lido: um grupo coberto por outra tela continua sendo relido, mas não marca; quando volta ao topo, marca as mensagens mais novas sem recarregar. A lista é relida depois de cada tentativa de marcar (respondida ou não), quando volta ao topo e quando algum grupo listado muda.
- **Sem a comunidade neste aparelho.** Enquanto o aparelho não tem a comunidade, a lista vem do índice do semeador: o cartão de convite, **Compartilhar** e os grupos, sem última mensagem nem não lidas.
-- **Em todos os aparelhos.** Uma comunidade que a pessoa criou ou em que entrou por outro aparelho da mesma passkey aparece aqui depois da sincronização, e a leitura funciona. Enquanto os dados da conta ainda chegam, a comunidade aparece como "aguardando sincronização" e não oferece **Entrar**. Quando a pessoa já participa, mas este aparelho ainda não escreve, aparece o cartão **Participar neste aparelho** ("Sua conta já participa. Este aparelho costuma entrar sozinho depois da sincronização; se ainda não entrou, toque para participar por ele."). Um aparelho novo da mesma passkey entra sozinho, depois da sincronização, nas comunidades e nos grupos em que a conta já está. A entrada é um registro público e gasta uma das 8 vagas de escrita por comunidade ou grupo, que o semeador nunca devolve; por isso o app deixa de entrar sozinho quando a conta já usa 6 das 8, e o cartão **Participar neste aparelho** continua para a pessoa decidir. Nessa reserva, o primeiro toque só mostra o aviso e um segundo toque participa. Depois do toque o cartão some até a leitura seguinte responder, para o mesmo aparelho não gastar duas vagas; se o toque ficar sem resposta, o app lê de novo e o cartão volta se ainda faltar o registro, sem repetir o pedido sozinho. Numa comunidade com aprovação, o aparelho novo espera um administrador aceitar, e os outros grupos entram depois disso. O app com configuração manual tem o próprio diário e não recebe as comunidades de outro aparelho.
+- **Em todos os aparelhos.** Uma comunidade que a pessoa criou ou em que entrou por outro aparelho da mesma passkey aparece aqui depois da sincronização, e a leitura funciona. Enquanto os dados da conta ainda chegam, a comunidade aparece como "aguardando sincronização" e não oferece **Entrar**. Quando a pessoa já participa, mas este aparelho ainda não escreve, aparece o cartão **Participar neste aparelho** ("Sua conta já participa. Este aparelho costuma entrar sozinho depois da sincronização; se ainda não entrou, toque para participar por ele."). Um aparelho novo da mesma passkey entra sozinho, depois da sincronização, nas comunidades e nos grupos em que a conta já está. A entrada é um registro público e gasta uma das 8 vagas de escrita da conta por comunidade ou grupo; por isso o app deixa de entrar sozinho quando a conta já usa 6 das 8, e o cartão **Participar neste aparelho** continua para a pessoa decidir. Nessa reserva, o primeiro toque só mostra o aviso e um segundo toque participa. Com um nó que informa `frozenRoom` (rpc.md 5.59), o aviso diz que desconectar um aparelho antigo em Ajustes, Aparelhos conectados, devolve a vaga, ou, quando a reserva de congelados acabou (`frozenRoom` 0), que ali desconectar pode não devolver (um corte em 0 ainda libera a vaga enquanto a conta tem liberações); com um nó mais antigo, o aviso continua dizendo que a vaga não volta. Se a entrada for recusada com as 8 vagas em uso, o erro diz "Para liberar a vaga, desconecte um aparelho antigo em Ajustes, Aparelhos conectados." Depois do toque o cartão some até a leitura seguinte responder, para o mesmo aparelho não gastar duas vagas; se o toque ficar sem resposta, o app lê de novo e o cartão volta se ainda faltar o registro, sem repetir o pedido sozinho. Numa comunidade com aprovação, o aparelho novo espera um administrador aceitar, e os outros grupos entram depois disso. O app com configuração manual tem o próprio diário e não recebe as comunidades de outro aparelho.
- **Estados da lista.** Esqueletos ao carregar, e as linhas dos grupos alinhadas às de Conversas, com remetente e hora lidos pelo leitor de tela. Sem grupos, um estado vazio. Na tela Comunidades, a busca mostra um só aviso por vez (ainda indexando, pode estar incompleta ou nenhum grupo achado) e, sem comunidades, o vazio explica o limite de grupos e oferece criar. Os nomes na caixa de comunidades têm retorno ao toque e quebram em até duas linhas.
### Informações da comunidade
@@ -461,7 +461,7 @@ A tela de um grupo mostra, sob o cabeçalho, a linha da comunidade, a conversa,
- **Ações.** Tocar longo abre **Responder**, **Copiar**, **Esconder** ou **Mostrar**, **Banir** e **Denunciar**, conforme as permissões de cada um. Responder não aparece quando a pessoa age como página.
- **Respostas.** Uma resposta real mostra a citação na nova bolha. Tocar na citação rola até a mensagem original, quando ela está carregada, e a resposta continua aparecendo depois de recarregar o app.
- **Deslizar para responder, ir para o fim e sugestões.** Valem como em Conversa (veja lá). Tocar numa citação não carregada, ou na barra "Respondendo a" quando a citada está fora do trecho, mostra "Mensagem fora do trecho carregado". Num nó sem hospedagem a tela diz "Comunidades indisponíveis" (decidido por `node.hosted`).
-- **Em todos os aparelhos.** Um grupo que a pessoa criou ou em que entrou por outro aparelho da mesma passkey aparece aqui depois da sincronização, e a leitura funciona. Quando a pessoa já está no grupo, mas este aparelho ainda não escreve, a caixa de escrever dá lugar ao cartão **Participar neste aparelho**. Um aparelho novo da mesma passkey entra sozinho no grupo depois da sincronização, e o toque só é preciso se a conta já usa 6 das 8 vagas de escrita do grupo, que o semeador nunca devolve (a entrada é um registro público). Nessa reserva, o primeiro toque só mostra o aviso e um segundo toque participa. Se este aparelho ainda não escreve na comunidade, o toque primeiro faz este aparelho participar da comunidade. Depois que este aparelho já participa, se uma mensagem ainda não pode sair, a caixa avisa "Este aparelho ainda não pode escrever aqui. Aguarde a sincronização.". Numa comunidade com aprovação, o aparelho novo espera um administrador aceitar e a caixa avisa "Este aparelho precisa da aprovação de um administrador para escrever.". Num grupo privado, o aparelho que criou o grupo ou o de um administrador envia as chaves ao aparelho novo sozinho e o histórico abre sem toque; até lá, e a caixa responde "aguardando nova chave", também quando o aparelho já participa e só falta a chave. Depois do toque em Participar neste aparelho o cartão some até a leitura seguinte responder, e Sair do grupo só aparece no aparelho que participa. O app com configuração manual tem o próprio diário e não recebe as comunidades de outro aparelho.
+- **Em todos os aparelhos.** Um grupo que a pessoa criou ou em que entrou por outro aparelho da mesma passkey aparece aqui depois da sincronização, e a leitura funciona. Quando a pessoa já está no grupo, mas este aparelho ainda não escreve, a caixa de escrever dá lugar ao cartão **Participar neste aparelho**. Um aparelho novo da mesma passkey entra sozinho no grupo depois da sincronização, e o toque só é preciso se a conta já usa 6 das 8 vagas de escrita do grupo (a entrada é um registro público). Nessa reserva, o primeiro toque só mostra o aviso e um segundo toque participa; o aviso e a recusa com as 8 vagas em uso seguem `frozenRoom` (rpc.md 5.42) como na comunidade. Se este aparelho ainda não escreve na comunidade, o toque primeiro faz este aparelho participar da comunidade. Depois que este aparelho já participa, se uma mensagem ainda não pode sair, a caixa avisa "Este aparelho ainda não pode escrever aqui. Aguarde a sincronização.". Numa comunidade com aprovação, o aparelho novo espera um administrador aceitar e a caixa avisa "Este aparelho precisa da aprovação de um administrador para escrever.". Num grupo privado, o aparelho que criou o grupo ou o de um administrador envia as chaves ao aparelho novo sozinho e o histórico abre sem toque; até lá, e a caixa responde "aguardando nova chave", também quando o aparelho já participa e só falta a chave. Depois do toque em Participar neste aparelho o cartão some até a leitura seguinte responder, e Sair do grupo só aparece no aparelho que participa. O app com configuração manual tem o próprio diário e não recebe as comunidades de outro aparelho.
- **Formatação.** As mensagens do grupo (e das conversas de comunidade) têm a mesma formatação das conversas privadas: negrito, itálico, riscado, código, blocos de código com **Copiar código** e **ver tudo**, e spoiler, além do botão **Aa** no campo de escrever (veja "Conversa"). A última linha da lista de grupos mostra texto simples, com "spoiler" no lugar do segredo.
- **Ocultar e banir** aparecem conforme o papel de quem olha. A autora de uma mensagem e quem modera podem ocultá-la; o banimento é feito na comunidade e vale para todos os grupos dela. Ocultar tira a mensagem da conversa e não apaga as cópias que outros aparelhos já têm.
- **Denunciar.** Cada mensagem de outra pessoa que não está escondida tem "denunciar", que leva o grupo e a posição da mensagem; num grupo privado, a citação é o texto aberto neste aparelho.
diff --git a/docs/changelog/feat-writer-slot-clients.md b/docs/changelog/feat-writer-slot-clients.md
new file mode 100644
index 00000000..a46749d1
--- /dev/null
+++ b/docs/changelog/feat-writer-slot-clients.md
@@ -0,0 +1,7 @@
+### Alterado
+
+- Os aparelhos passam a contar as vagas de escritor pela caminhada do próprio diário (PR `feat/writer-slot-clients`, parte da #350): ao entrar num grupo ou numa comunidade, o nó core e o nó do navegador conferem as 8 vagas vivas da conta ali, e não mais os escritores guardados nem as 8 primeiras entradas. Um aparelho que troca o próprio escritor, num grupo ou numa comunidade (depois de uma saída, ou quando o escritor de comunidade que ele citava foi retirado), grava, antes da entrada nova, a entrada desligada e um corte no escritor antigo, que devolve a vaga enquanto houver reserva; sem vaga, nada é gravado e a entrada é recusada. A dobra da comunidade no nó do navegador lê os cortes do diário, como a dobra do grupo, e um rebaixamento na comunidade nunca é recusado pelo tamanho, como no nó core. A sincronização busca até 32 escritores por diário e grupo (8 vivos, 8 congelados e 16 liberados), como os semeadores guardam, e a troca periódica de chave deixa de contar os envelopes que um escritor cortado escreveu depois do corte. O app diz que desconectar um aparelho antigo em Ajustes, Aparelhos conectados, devolve a vaga, ou que ali pode não devolver, e a recusa com as 8 vagas em uso diz "Para liberar a vaga, desconecte um aparelho antigo em Ajustes, Aparelhos conectados." Conferência em `docs/verification/feat-writer-slot-clients.md`.
+
+### Adicionado
+
+- `group.info` e `community.info` do nó do navegador informam `writersFrozen` e `frozenRoom` (na comunidade, também `generalWritersFrozen` e `generalFrozenRoom`, os de Geral), e `writersBound` passa a ser o número de vagas vivas, de 0 a 8 (rpc.md 5.42 e 5.59).
diff --git a/docs/protocol.md b/docs/protocol.md
index 0f8d0ce6..d883d7e3 100644
--- a/docs/protocol.md
+++ b/docs/protocol.md
@@ -1889,7 +1889,7 @@ A CW record (admit, ban, hide or invites) whose `group` names a key outside C's
**Slot walk** (#350, `slotWalk` in `group.js`). Per diary d and root G (or community C), the counted writers come from d's own `group-join` and `group-cut` records for G in diary seq order and nothing else, so every reader and every seeder agrees, and a prefix of the diary never disagrees with the whole diary about the records it holds. A writer's state only moves forward: none, live, frozen, released. (1) An `on: true` join of a writer in state none makes it live while fewer than 8 are live (7 from d's first `on: true` join of the root key G itself: the root slot, which any diary joining writer G reserves from that join on); with no room the writer is over and never counts, even when a slot frees later. A repeated or `on: false` join changes no slot. (2) A mark on a live or frozen writer lowers its cut (the lowest wins, and it always applies to the writer's records, "Diary cuts on own writers"). Cut 0 releases the writer while d released fewer than `MAX_WRITER_RELEASES_PER_DIARY` = 16 writers in G; a cut >= 1 freezes a live writer while fewer than 8 are frozen; with no room the writer keeps its state and its slot, with the cut. (3) A mark on a writer in state none or released, every mark on the root, and a mark with no valid seq are ignored. The candidates are the live, frozen and released writers. A writer cut at 0 (released, or kept at cut 0 once the 16 releases are spent) gives its slot back when there is room, but its block 0, its leaves and its key blocks still count ("Diary cuts on own writers"), so it is awaited like a frozen writer, and a released one ranks after the frozen writers of its tier under the 512 cap. A released writer is awaited whatever its diary's newest join says, in a private group too, so a diary that turns off and cuts at 0 the writer holding its leave never gets a fold that looks complete without that leave. Once held, a writer cut at 0 whose header box does not match the group's kind (the boxless writer a node replaced, "Writers named by a node") is rejected, never awaited; seeders and sync refuse such a header with `header-invalid`, so a reader that never held it keeps the fold incomplete. A writer that is over is no candidate, is never awaited and never makes the fold incomplete. Releases bound the churn: a loop of join, cut 0, join stops after 16 releases and 8 writers held at cut 0, so a diary has at most 8 live, 8 frozen and 16 released writers in G. A rejoin naming a leave on a writer of the same diary that its walk voided (over) is rejected; one naming a leave of a released writer is valid, since that leave still counts. A hole in the history of a diary with an `on: true` join for G (`{ gap: true }` in its marks) may hide the mark that freed or froze a slot, so it sets `incomplete`. The fold returns `frozen` (the frozen writers, released ones included), `released` (the released writers it kept) and `slots` (diary -> `{ live, frozen, released }`, the counts of each diary with an `on: true` join for G, from which nodes derive the room left). `diaryGroupKeys` and the seeder ("Seeder group admission") use the same walk, so a second device of the account and the seeders open the same roots and writers.
-**Writers named by a node** (`countedCuts` and `groupCuts` in `member.js`). In a community, an admit, a ban or a demotion that a node writes names every counted writer of the member, a frozen one at its cut, never past it, and never a released one (8 live and 8 frozen fit `MAX_GROUP_CUTS`; 16 released writers would not, and only their block 0, leaves and key blocks count). In a group, an admit, a ban or a demotion names the writers a new post would count on (a probe post past each writer's length): a frozen writer whose cut is at or below its length takes no new post and is left out, its cut already fixes it, and one cut past its length is named, never past the diary's cut. A leave and an `on: true` grant name the live writers only, so they stay within 8 cuts. The writers an admission waits for (`unheldCountedWriters`) come from the walk too: a frozen or released writer is awaited (its leaves still count), except, in a private group, one whose newest join is off: an admit never names a released writer, and a replaced boxless one can never be held (seeders and sync refuse it), so waiting for it would hold the admission for good. The fold itself still awaits it ("Limits"). A node that replaces its own writer of a private group whose header box is not the device's (a join before the root arrived; `joinLoaded` in the core and browser nodes) writes, after that writer's join off and before the new writer's join, a `group-cut` on it (`replacedWriterCut` in `group.js`), and never a second one at the same or a higher cut. The cut is 0 for a header with no box: the walk releases it whatever it holds, and nothing past its block 0, leaves and key blocks ever counted in a private group, where a boxless writer is rejected. For a header with a box it is one past the last seq the node holds (1 for a header with a rejoin and nothing else, 0 for a bare header), so nothing it holds stops counting. The join off alone keeps its slot in the walk. With an unknown length (the core not held) no cut is written. Before writing the join off, the cut or the new join, the node steps its diary's own walk with those events (`walkHasRoom` in `group.js`): when the new writer would not take a live slot, it writes nothing and fails with `GROUP_FULL`. The other automatic cuts (a writer replaced after a leave or a retired CW) and the cut of a disconnected device come with the client nodes.
+**Writers named by a node** (`countedCuts` and `groupCuts` in `member.js`). In a community, an admit, a ban or a demotion that a node writes names every counted writer of the member, a frozen one at its cut, never past it, and never a released one (8 live and 8 frozen fit `MAX_GROUP_CUTS`; 16 released writers would not, and only their block 0, leaves and key blocks count). In a group, an admit, a ban or a demotion names the writers a new post would count on (a probe post past each writer's length) and every frozen writer the fold counts, a frozen writer whose cut is at or below its length included although it takes no new post, each at its length or at the diary's cut when that is lower, and never a released one, as the browser node does (`cutsFor`): a ban or a demotion covers a writer it does not name from seq 0, so leaving a frozen writer out would hide its earlier posts or void its earlier admin acts in one node and not in the other. A leave and an `on: true` grant name the live writers only, so they stay within 8 cuts. A community demotion is never refused for its size (both nodes): past `MAX_GROUP_CUTS` it names the first writers by key and the rest read as cut at 0, while an `on: true` grant past it fails with `COMMUNITY_FULL`. The writers an admission waits for (`unheldCountedWriters`) come from the walk too: a frozen or released writer is awaited (its leaves still count), except, in a private group, one whose newest join is off: an admit never names a released writer, and a replaced boxless one can never be held (seeders and sync refuse it), so waiting for it would hold the admission for good. The fold itself still awaits it ("Limits"). A node that replaces its own writer of a private group whose header box is not the device's (a join before the root arrived; `joinLoaded` in the core and browser nodes) writes, after that writer's join off and before the new writer's join, a `group-cut` on it (`replacedWriterCut` in `group.js`), and never a second one at the same or a higher cut. The cut is 0 for a header with no box: the walk releases it whatever it holds, and nothing past its block 0, leaves and key blocks ever counted in a private group, where a boxless writer is rejected. For a header with a box it is one past the last seq the node holds (1 for a header with a rejoin and nothing else, 0 for a bare header), so nothing it holds stops counting. The join off alone keeps its slot in the walk. With an unknown length (the core not held) no cut is written. Before writing the join off, the cut or the new join, the node steps its diary's own walk with those events (`walkHasRoom` in `group.js`): when the new writer would not take a live slot, it writes nothing and fails with `GROUP_FULL`. A node that replaces its own live writer because the fold no longer takes it (retired by a leave of any device, or naming a CW the gate retired; `joinLoaded` in both nodes) writes the same way, after that writer's join off (when its newest join is on) and before the new writer's join, a `group-cut` on it (`retiredWriterCut` in `group.js`): 0 for a bare header, 1 for a header with a rejoin and nothing else (its block 0 holds the generation), else one past the last seq it holds (a device holds its own core whole), so no record it wrote stops counting and the walk frees its live slot (a cut 0 releases it, a cut >= 1 freezes it, each while there is room). A mark already on that writer does not stop the cut while the walk still has the writer live (the mark came when the walk had no room to freeze or release it): the repeated cut is a new walk event, so the walk can take it now, and it goes at the lowest mark on the writer when that is lower, never higher. A writer the walk already froze or released gets no further cut. Only this device's writer is cut this way: another device's writer is cut only by a disconnect (section 1). With block 0 not held no cut is written. The join off and the cut are steps of the same `walkHasRoom` check: when the new writer would still not take a live slot (8 live, or no room to freeze or release the old one), nothing is written and the join fails with `GROUP_FULL`. A `community.join` that replaces this device's retired CW (`communityJoin` in both nodes) writes the same way, and its join off and cut take part in the same check before anything is written (`COMMUNITY_FULL` with nothing written when the new CW would not take a live slot). The cut goes after the leave the join may append to that CW first (`ownRejoinLeave`), so that leave stays below it, and before the new CW's join. Every per-diary check a node makes before a join (`GROUP_FULL`, `COMMUNITY_FULL`) counts the live slots of its own diary's walk, never stored cores or the earliest 8 joins, and the browser node's `writersBound` (rpc.md 5.42) is that live count. The policy facts of a periodic rekey (`epochFacts` in `group-rekey.js`) skip the `group-sealed` envelopes a frozen or released writer wrote at or past its cut, as the fold drops them.
### Folding: `buildGroup`
@@ -1954,7 +1954,7 @@ Late blocks can change the causal order of posts already displayed. The RPC surf
A community C is the parent of groups. C is the key of a single-writer root core whose block 0 is a `group-header` with `kind: 'community'` (`community`, `member` and `box` null, `visibility` `public`, "Cores and headers"; `diary` is the community creator). Unlike a group root, C has writer cores: each device of a member writes a **community writer** (CW), a `group-writer` core with `group` = C and `member: null`, bound to its diary exactly like any writer (the join `{ group: C, writer: CW, on: true }` and a `rejoin` that names a `group-leave` of the same diary inside C). A group G has exactly one parent, the `community` of its header, immutable. C lists its groups in a catalogue made of `group-create` records ("Community-core records"); the first group of a community, created with it, is called Geral. Readers decide the kind from the root's own header.
- **Bound root.** As for a group root, the header's `diary` is only a label until that diary binds the root with a join `{ group: C, writer: C, on: true }`. An unbound community root keeps only its header: it has no creator, no admins, no members and no catalogue, gives no power in any group, and `buildCommunity` returns `incomplete: true`.
-- **`buildCommunity`.** `buildCommunity({ root: { key: C, entries }, writers: [{ key, entries, diary }], joins: Map, now })` is the input of `buildGroup` without `community` and `keys`. It runs the group fold on C's logs in community mode: the same binding (`readJoins`), per-diary cap, 512 cap with the same tiers, rejoin check, causal order, leave withdrawal, root admin layer (its cuts are over CW keys), admit and ban layers, shield and results `members`, `requests`, `bans`, `leaves`, `last` and `writers`. The differences are:
+- **`buildCommunity`.** `buildCommunity({ root: { key: C, entries }, writers: [{ key, entries, diary }], joins: Map, now })` is the input of `buildGroup` without `community` and `keys`. The community fold reads the diaries' cuts (`marks`), like the group fold: the core node's `loadCommunity` and sync catalogue (`heldCatalogue`) and the browser node's `foldCommunity` and sync catalogue (`foldedCatalogue`) pass them. It runs the group fold on C's logs in community mode: the same binding (`readJoins`), per-diary cap, 512 cap with the same tiers, rejoin check, causal order, leave withdrawal, root admin layer (its cuts are over CW keys), admit and ban layers, shield and results `members`, `requests`, `bans`, `leaves`, `last` and `writers`. The differences are:
- Only records with `group: null` act in C. Records with `group !== null` (admit, ban, hide) are nodes in C's causal order and change nothing in C; a `group-hide` never sets a hide in C.
- **Members may invite in C.** C's `memberInvite` is the `on` of the last `group-invites` record with `group: null` in C's causal order that counts as an administrative node of C: on C's root, the creator's while the root has its powers (root seq below `rootCut`); on a CW, the creator's diary or a diary whose C admin layer is on and C ban layer off at that CW and seq, the CW counted, not retired (no authority after leaving, past records included) and the record not withdrawn by a leave (`authorizedC` in `lib/group.js`, the gate of a `group: null` admit). A returning creator's record on a creator CW also counts when it passes the repair gate ("Community repair after the creator's return"), as for a CW meta. A member's record never counts. Absent, it is `false` (also for an unbound or empty C). A `group-invites` naming a group changes nothing in C.
- A community has no posts: `posts: []`, `future: []`, `sealed: 0`, `epoch: null`, `rekeyPending: false`, `entitled: []`, `removals: []`. `join` and `name` come from the header and the last not-withdrawn `group-meta` by root seq, overridden by the latest counted CW meta ("Community repair after the creator's return").
@@ -2177,7 +2177,7 @@ Added to section 7 "Seeder index route", under the same caps, Host check, 405 an
- **Own writers.** Opened by nonce (`group:` in the core node; storage core `group/` in the browser) and pushed to the seeders. In the browser they are replicated only on the verified link and are never listed in `hello` or `have`. A push is confirmed by the seeder HOLDING the core, not by the blocks uploaded (the seeder may take them on its quarantine channel and store none): in the browser only with the replication peer's evidence (its contiguous length at no less than the local length) plus an agreeing page. **Known limitation (accepted):** a browser that shares a group writer key with another instance of the same profile (a second tab, or a device restored from older storage) trusts the length its one seeder reports. A malicious seeder that hides blocks of that writer it holds can make the device append at a stale length and fork its own writer or root. No proof can tell a seeder that hides blocks from one that never got them, and the browser has no other peer. An honest seeder that is merely behind is covered by `group-behind`. The browser's own-core write gate uses the larger of the page length and what the seeder's replication peer holds, never reports an own group core in sync (no group write is allowed) until the seeder's replication peer on that link has synced, even when the page lists the core, takes the seeder's length as the larger of the page and the peer, and refuses to append while the peer is ahead (`group-behind { key, length, theirs }`, surfaced by the session), so a device restored behind the seeder cannot fork its own writer. Browser revocation: the binding certificate (block 0) of each accepted core is kept in kv `groupCerts`, so a revocation is judged after a restart and after the binding diary is gone; active cores are re-checked by the revocation sweep, and a binding diary's block 0 is judged when it arrives. The join history used for the per-diary cap is cached per run. The session's write gate needs the reported synced length to be at least the core's current local length. Browser kv `boundRoots` = `{ v: 1, keys }` (at most `MAX_GROUP_REPLICAS`) is written when a gate proves a root's creator join, read at start, removed when the core is purged and cleared on reset, so a root bound in an earlier run goes with its blocked or revoked creator even if the join block was never stored; the revocation sweep judges a binding replica without block 0 by the certificate kept at the gate (kv `groupCerts`).
- **Binding diary comparison (core node).** The binding diary stored in the main store is compared with the blocks the gate read (block 0 and the join block). A mismatch is a hard error `bloco divergente: (diário de grupo)`, and the diary and the writers that depend on it are purged. A stored writer whose binding diary diverges while its gate runs is purged as `not-in-log`, and nothing is charged to the seeder. A stored forked diary is swept at startup, and a forked creator's bound root is purged (a root counts as bound when the stored creator replica proves it OR `bound-roots.json` names it, section "Data-dir files"). A binding diary that diverged is refused for the rest of the run: later writers of that diary are refused as not-in-log. In a continuous run, a binding diary first read through the quarantine re-attaches its main-store replica once the quarantine read ends, so a later writer of that diary is gated in the same run. Hypercore keeps one channel per key and connection, and the first replica to open it holds it: a gate (or a follow) that opens a diary in the main store first waits for the quarantine reads of that diary in flight, and while that main-store replica stays open every gate that reads the diary reads it through that replica, never through the quarantine (whose read would never pair and would end as a `timeout`); a read whose replica closes before it ends goes on through the next open replica, or through the quarantine. On a cold start the blocked-root sweep runs before blocked diaries are purged. A writer that fails before activation leaves the admission table, and a stored core always reaches its stored-header check.
- **Foreign writers.** The member joins the discovery keys of the roots and known writers, asks a verified seeder for the writer pages (groups channel), and gates each unknown writer before opening a replica: (1) quarantine fetch of block 0 (a valid writer header, or root header, naming G and a diary D), (2) D's block 0 and the join block (`on: true`, naming this writer) through quarantine on the same sockets, (3) a valid certificate (`certForKey`, revoked issuers included) and neither D nor G blocked, (4) the signed length within the quota. Only then the core is opened in the store with a fork watcher and the quota. Failures emit `{ event: 'group-skipped', key, reason }`. A page marked `incomplete` is gated but never completes the pass nor confirms an own core. **Binding diaries** of accepted foreign writers are stored in full: the core node keeps them in its main store, the browser as replicas listed in kv `groupDiaries`, never in `follows`; they feed the join evidence and the author names. Scale cost: a core node stores every binding diary entirely. A group operation reads only that group's cores (block 0 first, a full read only for writers of that group) and only the diaries that bind them (own diary, creator, the authors of its writers).
-- **Bounds on hints.** At most `MAX_GROUP_WRITERS_PER_DIARY` accepted cores per (group, diary), and a pass keeps at most `MAX_GROUP_WRITERS` items; in the browser the per-diary cap keeps the earliest proven joins: only live accepted cores count, and when (group, diary) is full a listing is gated as a contender (no slot, at most 2) only if a held writer has a higher proven join seq; after its gate proves a lower seq, the held writer with the highest proven seq fails `quota`. The per-diary cap reserves a slot synchronously during the gate. At run start, stored cores beyond `MAX_GROUP_WRITERS_PER_DIARY` per (group, diary) stay at rest (not opened), the earliest proven joins live, a bound root always live; before an eviction, each writer's join seq is refined to its earliest `on: true` join from an open binding diary. Writers stored by an earlier run prove their join seq at run start from their stored binding diary (the earliest `on: true` join naming them; each run start reads every stored binding diary of stored writers once); a writer whose binding diary is not stored stays unproven and is never evicted, and roots are never evicted. A browser listed key with no core is paged as a probe and unlisted after one complete page pass with no root for it; a core discarded during its local re-check is unlisted from kv `groupReplicas`; a writer evicted by the per-diary cap while still in its local re-check (not yet listed) is purged, a listed writer evicted is kept. In the core node the per-diary cap is applied at gate time to every entry, against the diary its own block 0 names and the join seq the binding diary proved (never a listed diary or seq): when (group, diary) already holds `MAX_GROUP_WRITERS_PER_DIARY` verified slots (active, or past the gate and opening), the entry gets the slot only if its proven seq is lower than the highest active one, whose writer then fails as `quota` (remembered as proven later while that diary stays full; a writer stored in this run is purged, while one stored before the run is never the victim, "Round 10, core node" below); otherwise the entry itself is refused as `quota` and remembered the same way. The group root is one of its creator's writers and is never evicted. A foreign writer stopped for quota with its blocks kept holds its (group, diary) slot for the rest of the run (except a stored writer refused for the cap, "Round 10, core node" below), and at run start the stored writer cores of listed groups count toward their (group, diary) cap. A listing is gated at most `MAX_GROUP_WRITERS_PER_DIARY` + `MAX_CAP_CONTENDERS` (2) at a time per hinted (group, diary), and the admission table is bounded; an item that finds either bound full only waits (the group is rescanned when a slot frees) and nothing is decided about it. An advertised length above the per-core log quota never changes a held writer: the item is ignored, reported once per unheld key as `quota`, and charged as a bad hint to that seeder without remembering the triple, so an honest identical listing is still gated (core node); the browser fails only an unheld entry, checks a held core's quota on its own length, and sets no once-run goal after an over-quota listing of a held core. Nothing is fetched in either case. Writer targets (the lengths used for `--once` goals) are recorded only for writers held in the bounded admission table, clamped to the log quota, capped in total, and deleted with the entry or on purge.
+- **Bounds on hints.** At most `MAX_GROUP_HELD_PER_DIARY` = 32 accepted cores per (group, diary), as the seeder holds them ("Seeder group admission": the 8 live, 8 frozen and 16 released writers of the diary's slot walk), so a frozen or released writer, which may hold a leave the fold reads, is still fetched in both nodes; and a pass keeps at most `MAX_GROUP_WRITERS` items. The slots go to the 32 earliest proven joins per (group, diary) in both nodes, not to the writers of the walk in walk order: a sync never reads the walk of a binding diary it may hold only in part. Only that diary itself can write joins its walk refuses (a seeder never binds a writer), so only its own extra joins can take those slots. In the browser the per-diary cap keeps the earliest proven joins: only live accepted cores count, and when (group, diary) is full a listing is gated as a contender (no slot, at most 2) only if a held writer has a higher proven join seq; after its gate proves a lower seq, the held writer with the highest proven seq fails `quota`. The per-diary cap reserves a slot synchronously during the gate. At run start, stored cores beyond `MAX_GROUP_HELD_PER_DIARY` per (group, diary) stay at rest (not opened), the earliest proven joins live, a bound root always live; before an eviction, each writer's join seq is refined to its earliest `on: true` join from an open binding diary. Writers stored by an earlier run prove their join seq at run start from their stored binding diary (the earliest `on: true` join naming them; each run start reads every stored binding diary of stored writers once); a writer whose binding diary is not stored stays unproven and is never evicted, and roots are never evicted. A browser listed key with no core is paged as a probe and unlisted after one complete page pass with no root for it; a core discarded during its local re-check is unlisted from kv `groupReplicas`; a writer evicted by the per-diary cap while still in its local re-check (not yet listed) is purged, a listed writer evicted is kept. In the core node the per-diary cap is applied at gate time to every entry, against the diary its own block 0 names and the join seq the binding diary proved (never a listed diary or seq): when (group, diary) already holds `MAX_GROUP_HELD_PER_DIARY` verified slots (active, or past the gate and opening), the entry gets the slot only if its proven seq is lower than the highest active one, whose writer then fails as `quota` (remembered as proven later while that diary stays full; a writer stored in this run is purged, while one stored before the run is never the victim, "Round 10, core node" below); otherwise the entry itself is refused as `quota` and remembered the same way. The group root is one of its creator's writers and is never evicted. A foreign writer stopped for quota with its blocks kept holds its (group, diary) slot for the rest of the run (except a stored writer refused for the cap, "Round 10, core node" below), and at run start the stored writer cores of listed groups count toward their (group, diary) cap. A listing is gated at most `MAX_GROUP_HELD_PER_DIARY` + `MAX_CAP_CONTENDERS` (2) at a time per hinted (group, diary), and the admission table is bounded; an item that finds either bound full only waits (the group is rescanned when a slot frees) and nothing is decided about it. An advertised length above the per-core log quota never changes a held writer: the item is ignored, reported once per unheld key as `quota`, and charged as a bad hint to that seeder without remembering the triple, so an honest identical listing is still gated (core node); the browser fails only an unheld entry, checks a held core's quota on its own length, and sets no once-run goal after an over-quota listing of a held core. Nothing is fetched in either case. Writer targets (the lengths used for `--once` goals) are recorded only for writers held in the bounded admission table, clamped to the log quota, capped in total, and deleted with the entry or on purge.
- **Bad hints and bounds on memory (core node).** Four rules. (1) What keeps a writer out for longer than one gate (refused, a per-diary cap slot, an eviction, remembered as proven later or cap-refused) comes only from data this node verified: the writer's own block 0 (its group and diary) and the binding diary's join block, never from a page item's diary or seq alone. A never-admitted, unstored writer is refused for the run (dropped from the admission table and remembered in a bounded set) only on such a verdict: a blocked writer key or group, an invalid header, a header naming another group, a fork or over-quota of the writer core itself; `header-invalid` and `wrong-group` are charged as a bad hint to the listing seeder. (2) A verdict that depends only on the page item (diary, seq, length: a blocked, forked or revoked hinted diary, the hinted diary's quota, `wrong-diary`, `unbound`, not in log) discards only that hint: it emits `group-skipped`, removes the entry without purging, remembers the hint per (group, key, diary, seq) so the same listing from any seeder is not gated again (a different listing is), charges the listing seeder once per hint, and rescans the group skipping that seeder for that key, so other seeders' listings of the writer (and of the other writers of that diary) are gated now. The group is part of the hint, so a key disproved under one group never judges an honest listing under another. The writer's header is checked against the hinted diary before anything about that diary is believed, and a join seq at or past the diary's known length is a disproof (`unbound`), not a wait. (3) Every run-wide or per-seeder memory (disproved hints, refused writers, proven-later and cap-refused marks, timeouts, charges) is bounded: in long-running mode it also expires after `pageRefreshMs`, and when full the oldest entry is evicted, so remembering never stops. A seeder charged `MAX_BAD_HINTS` = 32 bad hints or `MAX_TIMEOUT_HINTS` = 16 timeouts of unstored writers (each hint counts once) is cut off: closed for good for the rest of a `--once` run, suspended for `pageRefreshMs` in long-running mode and then paged again with a clean slate. In `--once` the gate timeout of an unaccepted writer always drops the entry, and `MAX_GATE_TIMEOUTS` = 2 timeouts per (seeder, key) refuse that key from that seeder for the run (one seeder's listings never drop a writer another seeder lists); in long-running mode a timeout of an unstored writer is charged to the listing seeder and the writer is rescanned from the other seeders. A timeout is transient, never a disproof: the quarantine path waits for a join block past the length learned with block 0 until the gate deadline, and a group gate naming the device's own diary likewise waits for block 0 and the join block until the gate deadline (a missing block is a `timeout`, never a disproved hint). An item refused because the admission table or the gates in flight are full schedules a rescan of its group when a slot frees. A writer another seeder lists correctly stays a goal until gated, and an accepted writer evicted by a proven-earlier contender is still condemned if its binding diary later forks. A writer core already stored binds by the diary named in its own validated block 0, not by the hint. (4) A core already stored that is listed under a group it does not write in leaves the admission table (its blocks stay), so its honest listing under its own group proceeds. In the browser at most `MAX_GATE_HINTS` = 4 distinct listings re-gate a key per run, a key that failed `header-invalid` is never gated again, each seeder is charged once per distinct bad hint (at `MAX_BAD_HINTS` its writer pages are no longer requested for the run), and a gate failure caused only by the page item (`wrong-group`, `wrong-diary`, `unbound`, `header-invalid`, `diary-unavailable`) on a core never stored is remembered per (group, diary, join seq). A core stored or listed before the current browser run is never purged by a cap eviction, an abandoned gate or a regate discarded at the end of a run; nothing never accepted is purged. Round 7, core node: block 0 in the main store is compared with the gated block 0 before any quota decision (a mismatch purges); writers evicted by the per-diary cap are remembered for the whole run, and a diary fork also purges stored cores whose block 0 names that diary; each writer's DHT discovery session is released when it stops; an accepted writer that later fails leaves the admission table (remembered so it is not gated again); a suspended seeder is not paged for the rest of a pass; the blocked-root sweep purges a stored root only when its header diary bound it. Round 7, browser: the binding certificate is checked again against the current blocklist right before storing a core and before activating it, revocation sweeps include cores still being gated, a stored root is purged for a blocked or revoked header diary only when that diary bound it, and keys un-listed during a run no longer count as stored before the run.
- **Round 10, core node.** (1) A revoked issuer takes a stored writer or root even when its binding diary's block 0 is not stored, by the certificate recorded in `group-certs.json` (section "Data-dir files"). (2) At start, while the snapshot revokes any issuer, a stored foreign writer with no binding evidence (no stored block 0 and no record) is purged (`group-skipped` reason `not-in-log`); roots are not covered by this check. (3) A diverged binding diary also purges the stored writers it binds that left the admission table, and a root it bound (`not-in-log`). (4) The per-diary cap uses each writer's earliest proven `on: true` join when the binding diary is held locally (as the fold and the browser do), otherwise the gated seq. (5) The per-diary cap converges on the fold's earliest proven joins across runs: a writer stored in an earlier run (active or only held on disk) gives its slot to a newcomer with an earlier proven join, and its blocks are purged (`group-skipped` reason `quota`); a newcomer whose join is not earlier is still refused (`quota`); a holder whose join this node has not proved never gives way. The browser also evicts a stored victim but keeps its data (`evictionPurges`), where the core purges it. A stored writer's gate counts the other keys held on disk; a stored writer refused for the cap keeps its blocks but holds no slot for the run; excess left by an earlier run is not purged, and it never grows. (6) A listing refused for this node's own verdict (its blocklist, a fork or divergence it saw in this run) is not charged to the seeder and is not gated again while the verdict holds.
- **Round 13, core node.** (1) A new writer key of a group is refused (`group-skipped` reason `quota`) while the group's foreign writer cores in the admission table plus those stored on disk, including writers kept after a quota stop, reach `MAX_GROUP_WRITERS` (`runSync` option `maxGroupWriters`; the group root is no writer slot and is never refused by this cap); a stored writer keeps its place, and a refused key is gated again once a place frees. (2) Block-0 divergences of group writers become one hard-error line `bloco 0 divergente em escritores de grupo: ()` (the result is still not ok), and a run keeps at most 32 hard-error strings.
@@ -2185,8 +2185,8 @@ Added to section 7 "Seeder index route", under the same caps, Host check, 405 an
- **Binding diary release (core node).** A group writer's binding diary is downloaded and kept in the main store only while something needs it: a follow holds it, a writer in the admission table names it, or a stored foreign writer or root names it (the census is rebuilt from disk at start). When a writer is purged, retires or is evicted and nothing else needs its diary, the node stops downloading the diary and purges it from the main store. A writer stopped for quota keeps its diary (and its blocks). At run start a binding diary recorded in `group-certs.json` that no stored core names and that no follow holds is purged. At core node start, a member of any group also purges an unfollowed, unlisted diary unless a stored group writer or root names it or `group-certs.json` records it; that cleanup always rebuilds the census first, also when `groups.json` and `group-certs.json` are empty, and never purges a group core this node can write. A delete of a `group-join` block has no effect (section 2 "Deletion"): a held one keeps its writer bound and downloaded. One the node does not hold is fetched once (the binding diary is group-bound), and the gate waits a bounded time for it; only when the block is still missing after that wait does the core node drop the writer from the run (`group-skipped` reason `withdrawn`), without purging its blocks, and the group goal stops waiting for it. At browser node start, a stored replica that a stored group core binds (or that the run holds as a binding diary) stays indexed and is never purged by the unfollowed-replica cleanup.
- **Completion goals** (`--once` and the browser's once runs). Each group goal runs once per run and is met when (a) every writer page of every joined group was fetched (not `incomplete`) or the channel timed out, (b) every admitted foreign writer, and the diary that binds it up to its join, reached the length its seeder advertised, exactly like followed replicas (on the core node a core admitted through quarantine counts only after the block 0 its main store received was compared with the gated one), and (c) own writers were pushed to the length the seeder advertises for them (browser: confirmed by `group-pushed { key, length }`); all bounded by the run's deadline. Against a seeder without `varanda/groups/1` (groups unsupported) the browser's own group pushes are not once-run goals, so a once run finishes. The goal of a binding diary is met at its present length (section 2 "Clearing deleted bytes"), so a deleted post no longer keeps a once run open, and join blocks after a cleared hole are read. Group writer and root cores keep their own tracking, since no delete records exist for them. Goals G1 to G3 of section 6 are unchanged. The browser's write gate for a group write confirms the own writer's length, not only the diary. For each community the goals are C's root, every page of its CWs, and the binding diaries, as for groups.
- **Parent paging.** For every local group root held the node reads its header's `community`, and for every local community its key. For each community C it pages C on the groups channel (the root and its CWs), gates each CW like a group writer (a valid header with `group` = C and `member: null`, then the binding diary's block 0 and join block), stores the CWs and their binding diaries, and pages only the roots of C's catalogue (the `/index/community/` rows are a hint, the fold verifies) so that `community.info` can name them. Each root is judged by its own block 0 under a role fixed for the run: a key joined as a group must be a `group` root, a key reached as a parent must be a `community` root, and a `v: 1` header is `header-invalid`; the `group-skipped` reason `not-community` (a parent that is not a community root; the node's own verdict, the seeder is not charged) is kept. A run holds at most groups x 512 plus communities x 512 cores. A node folding a group passes `buildGroup` the parent's input (C's root, its CWs and their joins). The core sync emits `community-parent { key, group }` (`key` the community root C, `group` the group G) the first time a held group root names a community this node does not already hold, and `group-parent { key: C, group: G }` once per local group whose held root names C, held or not (the session uses it to fan `groups.changed` out from C's growth to its groups; the CLI prints neither).
-- **Diary-bound roots (browser).** Besides kv `groupWriters` and kv `groupReplicas`, the browser sync pages every root its own diary binds now as a replica: the newest `group-join` per writer still `on: true`, among the writers the slot walk counts per group ("Limits"), the first 64 groups by live join seq (`diaryGroupKeys`, the seeder's live-join rule). It reads them at run start from the stored diary and again whenever the own diary is caught up with the seeder, so a second device of the same passkey (same diary, fresh storage) rebuilds its communities and groups from the diary. A writer the diary newly binds to a root already adopted is paged at once (that group alone, ahead of the pass in progress or in a pass of its own), and again after waits of 2, 5 and 10 s (each counted from the previous page, so at about 2, 7 and 17 s) while the seeder does not list it yet; the writers of the run's first scan are not chased, the regular pass asks for them. A diary block the read could not get (not downloaded yet, or a read that failed) is kept, read again on every later scan, when it downloads and, after a failed read, on a short timer, and never holds back the joins after it. A once run does not end while that read of the own length is pending: the roots it adopts are paged before the run's goals are met. Such a root's writers are gated like any replica's, a catalogue child the diary joined is not paged root only (one already paged root only in the run is paged again in full), and a community among them is paged in full with its catalogue. Its own header decides its kind: another root's header naming it as its community never makes it a parent key, so it is never refused as `not-community`. A root no join binds now (every device left) is not adopted, a blocked key never is, and a key with no root at the seeder is dropped by the probe rule and not asked for again in that run. Nothing is appended, no writer is created and nothing is listed in kv for an adopted key until its block 0 is stored (the gate then lists it in kv `groupReplicas`). The core node is unchanged: its diary is per device (section 1), so another device never shares it.
-- **Automatic device enrolment (browser).** After a sync, a device of the same passkey enrols by itself in each diary-bound root it holds and has no kv `groupWriters` entry for, while the fold says the diary is a `member` there and the diary binds fewer than `MAX_GROUP_WRITERS_PER_DIARY` - 2 (6) writers in it: the last two slots, which the seeder never releases, are spent only by an explicit join. It writes what `community.join` or `group.join` writes (a new writer naming the diary's latest leave, and the diary's public `group-join`), communities first, then a group only when this device's community writer is admitted in C (an open community or its creator; in an approval community the diary's admission covers the new community writer, "Admits list their writers", and its groups follow on a later run, once the fold shows it admitted). Geral is planned like any group of C with its own checks: a diary that left Geral is not put back, and Geral's last two slots stay reserved too. A kv writer the diary never bound (an interrupted join) is bound again, reusing that writer only when this device derives its key from the entry's nonce; otherwise the root is skipped (`foreign`), and an explicit join writes a new writer instead. Only a writer this device derived is ever reused by a join. A root that is left, banned, requested, blocked, unheld, an orphan, not joined by the own diary (an opened link) or not foldable yet (no pending join) is skipped, and no admit or grant is written automatically (the fold's coverage of a new device by the diary's admission writes nothing). Rules in [rpc.md](rpc.md) 5.63. The core node is unchanged (its diary is per device, section 1).
+- **Diary-bound roots (browser).** Besides kv `groupWriters` and kv `groupReplicas`, the browser sync pages every root its own diary binds now as a replica: the newest `group-join` per writer still `on: true`, among the writers the slot walk counts live or frozen per group ("Limits"), the first 64 groups by live join seq (`diaryGroupKeys`, the seeder's live-join rule). The scan keeps the diary's `group-cut` records with its joins, so a writer that took a slot a cut released binds its root, and the same walk picks the newly bound writers it chases. It reads them at run start from the stored diary and again whenever the own diary is caught up with the seeder, so a second device of the same passkey (same diary, fresh storage) rebuilds its communities and groups from the diary. A writer the diary newly binds to a root already adopted is paged at once (that group alone, ahead of the pass in progress or in a pass of its own), and again after waits of 2, 5 and 10 s (each counted from the previous page, so at about 2, 7 and 17 s) while the seeder does not list it yet; the writers of the run's first scan are not chased, the regular pass asks for them. A diary block the read could not get (not downloaded yet, or a read that failed) is kept, read again on every later scan, when it downloads and, after a failed read, on a short timer, and never holds back the joins after it. A once run does not end while that read of the own length is pending: the roots it adopts are paged before the run's goals are met. Such a root's writers are gated like any replica's, a catalogue child the diary joined is not paged root only (one already paged root only in the run is paged again in full), and a community among them is paged in full with its catalogue. Its own header decides its kind: another root's header naming it as its community never makes it a parent key, so it is never refused as `not-community`. A root no join binds now (every device left) is not adopted, a blocked key never is, and a key with no root at the seeder is dropped by the probe rule and not asked for again in that run. Nothing is appended, no writer is created and nothing is listed in kv for an adopted key until its block 0 is stored (the gate then lists it in kv `groupReplicas`). The core node is unchanged: its diary is per device (section 1), so another device never shares it.
+- **Automatic device enrolment (browser).** After a sync, a device of the same passkey enrols by itself in each diary-bound root it holds and has no kv `groupWriters` entry for, while the fold says the diary is a `member` there and fewer than `MAX_GROUP_WRITERS_PER_DIARY` - 2 (6) writers of the diary are live in its slot walk there (the root slot included): the last two live slots are spent only by an explicit join. A slot comes back only through the diary's own cut (a disconnected device, section 1, or the cut a join writes on a writer it replaces, "Writers named by a node"), while the walk has room to freeze or release the writer; an older seeder never releases one. It writes what `community.join` or `group.join` writes (a new writer naming the diary's latest leave, and the diary's public `group-join`), communities first, then a group only when this device's community writer is admitted in C (an open community or its creator; in an approval community the diary's admission covers the new community writer, "Admits list their writers", and its groups follow on a later run, once the fold shows it admitted). Geral is planned like any group of C with its own checks: a diary that left Geral is not put back, and Geral's last two slots stay reserved too. A kv writer the diary never bound (an interrupted join) is bound again, reusing that writer only when this device derives its key from the entry's nonce; otherwise the root is skipped (`foreign`), and an explicit join writes a new writer instead. Only a writer this device derived is ever reused by a join. A root that is left, banned, requested, blocked, unheld, an orphan, not joined by the own diary (an opened link) or not foldable yet (no pending join) is skipped, and no admit or grant is written automatically (the fold's coverage of a new device by the diary's admission writes nothing). Rules in [rpc.md](rpc.md) 5.63. The core node is unchanged (its diary is per device, section 1).
- **Legacy sweep.** At the start of a run, before any fold, the node purges every `v: 1` group core. Core node: for each `groups.json` entry it opens the core by nonce and runs the block 0 validator; a `legacy` verdict purges the core and drops the entry (the one case in which a created entry is not kept), and every other stored core whose block 0 is a v1 `group-header` or `group-writer` is purged too. Browser: the same over kv `groupWriters`, `groupReplicas` and `groupCores`. Each purge emits `group-skipped { key, reason: 'legacy' }` (in the sync's JSON events like every other `group-skipped`; the session turns it into a warning). Diary `group-join` records of old roots stay (a diary is append-only); seeders refuse those roots as `header-invalid`.
- **Events.** Core sync: `group-downloaded { key, group, length }`, printed as `grupo `, and `group-skipped { key, reason }`. Browser sync: `groups-changed { group }` (coalesced per group over 50 ms of growth, also when no diary grew), `group-skipped { key, group, reason }`, `group-pushed { key, length }`, `group-writer { group, writer, private, own, account }` (a writer core, never a root, stored for the first time in the run; `private` is the root's verified visibility, `own` whether this device holds a writer of its own in the group and `account` whether the own diary binds the writer; it arms the session's device upkeep only when `account` is true or the group is private with an own writer, where a key re-share can be due). Both sessions emit the RPC event `groups.changed { group }` ([rpc.md](rpc.md), section 6), the core session also from sync growth (`group-downloaded`).
diff --git a/docs/rpc.md b/docs/rpc.md
index d5ebb957..f338924e 100644
--- a/docs/rpc.md
+++ b/docs/rpc.md
@@ -536,7 +536,7 @@ Who started following the caller, including a follower the caller already follow
- Params: `{ group, community? }`, nothing else (`community` an optional lowercase hex64): `group` the group root key (lowercase hex64). Both mutating. `group.join` joins an open group at once or files a request in an approval group; `group.leave` ends the caller's own membership (and every device's writer of the same diary, protocol section 10) and writes `cuts`: every counted writer of the caller's diary at its local length (the fold reads them only for the creator's root; the records of the diary's other devices are withdrawn whatever they say, protocol section 10), at most `MAX_GROUP_WRITERS_PER_DIARY` entries. The caller needs a live CW in the community of the group (status `'member'` in it, see 5.63), else `USAGE` `'entre na comunidade primeiro'`. `community` is the hint for a group whose root is not held yet (state `'pending'`): a pending root and a call without `community` -> `USAGE` `'informe a comunidade do grupo'`; a held root whose header names another community than `community` -> `USAGE` `'o grupo não é dessa comunidade'`; an orphan group (its header names a community that is not a valid community root) -> `USAGE` `'grupo sem comunidade'`; the group's community not held -> `USAGE` `'aguarde a sincronização da comunidade'`. A group the caller already belongs to answers its state without a new writer. When the caller's own diary is not among the fold's members the state is `'left'`, never null.
- `group.join` on a disconnected device (5.152 `off: true`) -> `USAGE` `'este aparelho foi desconectado da conta'`, before anything is written, whether the root is held or not: only `device.reconnect` (5.155) brings it back. `group.leave` is not affected.
- A G root its creator's diary marked (protocol §10, "Creator marks on group roots") is never the device's own writer again: it is not live and is not used for leaving. A returning creator's `group.join`, or the Geral step of `community.join`, writes a new G writer bound to its live CW.
-- Result: `{ state: 'member' | 'requested' | 'left' | 'pending' }`. `'pending'`: the node does not hold the group root yet; `group.join` then creates the caller's writer, writes the diary join and lists the group, and the next sync fetches the root (the ban, approval and rejoin checks apply once it arrives). A repeated `group.leave` answers `{ state: 'left' }` without writing anything. Errors (pt-BR messages, never echoing a key): a `group.join` while `bans` lists the caller's diary, a `group.leave` only when the caller's member status is `'banned'`, and a join while the caller's own diary is blocked -> `USAGE` `'sem permissão no grupo'`; a join whose writer would fall outside the 512-writer cap, or when the caller's diary already binds `MAX_GROUP_WRITERS_PER_DIARY` (8) writers in the group -> `USAGE` `'limite do grupo atingido'` (a writer the fold did not count never reports `'member'`); a `group.leave` with nothing to end -> `USAGE` `'nada a alterar'`; a `group.leave` while the group is `'pending'` (root not stored yet) when the caller's own diary join history binds any writer of the group other than this device's -> `USAGE` `'aguarde a sincronização do grupo para sair'`; a loaded `group.leave` never waits for other devices (anyone leaves at once), and its only refusal is the creator's: the caller's diary is the creator's, the root is bound and the root is not among the leave's cuts -> `USAGE` `'saia pelo aparelho que criou o grupo'` (holding the root core suffices; in a loaded group this practically never triggers); a root key on the blocklist, or a malformed key, -> `NOT_FOUND` `'grupo não encontrado'`. A joined group whose root is not held is not an error, see `'pending'`.
+- Result: `{ state: 'member' | 'requested' | 'left' | 'pending' }`. `'pending'`: the node does not hold the group root yet; `group.join` then creates the caller's writer, writes the diary join and lists the group, and the next sync fetches the root (the ban, approval and rejoin checks apply once it arrives). A repeated `group.leave` answers `{ state: 'left' }` without writing anything. Errors (pt-BR messages, never echoing a key): a `group.join` while `bans` lists the caller's diary, a `group.leave` only when the caller's member status is `'banned'`, and a join while the caller's own diary is blocked -> `USAGE` `'sem permissão no grupo'`; a join whose writer would fall outside the 512-writer cap, or when the caller's diary already holds the `MAX_GROUP_WRITERS_PER_DIARY` (8) live slots of its own slot walk in the group, counting the cut a join writes on this device's retired writer (protocol §10, "Writers named by a node") -> `USAGE` `'limite do grupo atingido'` (a writer the fold did not count never reports `'member'`); a `group.leave` with nothing to end -> `USAGE` `'nada a alterar'`; a `group.leave` while the group is `'pending'` (root not stored yet) when the caller's own diary join history binds any writer of the group other than this device's -> `USAGE` `'aguarde a sincronização do grupo para sair'`; a loaded `group.leave` never waits for other devices (anyone leaves at once), and its only refusal is the creator's: the caller's diary is the creator's, the root is bound and the root is not among the leave's cuts -> `USAGE` `'saia pelo aparelho que criou o grupo'` (holding the root core suffices; in a loaded group this practically never triggers); a root key on the blocklist, or a malformed key, -> `NOT_FOUND` `'grupo não encontrado'`. A joined group whose root is not held is not an error, see `'pending'`.
### 5.39 `group.post`
@@ -558,7 +558,7 @@ Who started following the caller, including a follower the caller already follow
### 5.42 `group.info`
-- Params: `{ group }`. Not mutating. Result: `{ group, name, join, creator, role, members, state, community, orphan, visibility, memberInvite, writerHere, canWrite, writersBound }`, where `writerHere` (boolean) is whether THIS device has its own writer in the group, and `canWrite` (boolean) is whether a post from this device would succeed: the caller's fold status is `'member'`, this device has a live writer, that writer is counted by the fold (the same check that refuses an uncounted writer with `'limite do grupo atingido'`), the group is not an orphan, the group's community root is held here (and not blocklisted), and a private group's post would not be refused with `'aguardando nova chave'` (the fold is rekey-pending, it has no current epoch, or this device does not hold that epoch's key); a pending group answers `writerHere: true, canWrite: false`; `writersBound` (browser node only, a non-negative integer, not capped at 8: the fold counts only the earliest 8, but every bound writer stays in the count) counts the distinct writers the caller's own diary ever bound in the group with an `on: true` `group-join` (a later leave frees no slot at the seeder), `0` for a pending group, so the app can say why the automatic enrolment of a second device stopped (5.63); a missing field is how an older node answers; `visibility` is `'public'` or `'private'` (`'public'` for a pending group), `name` of a private group is the sealed title when the caller can decrypt it, else `null`, and `role` is as in 5.41 and `state` is the caller's fold state (`'member'`, `'requested'`, `'banned'`, `'left'`, or `'pending'`). On a held root this device has no kv `groupWriters` entry for, that the caller's diary binds now, while the fold status for that diary is absent or `'left'`, `role` is `'pending'` and `state` stays the fold's (`'left'` when the diary is absent), so the screen does not offer a newcomer join while the account's writers are still arriving. `'banned'` and `'requested'` are unchanged, and a device that already has its own entry keeps the fold's role; `memberInvite` is a boolean, whether plain members may invite (default `false`, `false` on a pending result; 5.71); `community` is the root key of the group's community (hex64, `null` for a pending group) and `orphan` is `true` when the header names a community that is not a valid community root (an orphan answers its fold state and every write method refuses it with `USAGE` `'grupo sem comunidade'`); the former `communities` array was removed in protocol v2; `'left'` also when the caller's own diary is not among the fold's members (never null). A malformed or blocklisted root -> `NOT_FOUND` `'grupo não encontrado'`. A group joined before its root arrived answers `{ group, name: null, join: null, creator: null, role: 'pending', members: 0, state: 'pending', community: null, orphan: false, memberInvite: false, writerHere: true, canWrite: false, writersBound: 0 }`; until then `group.read` answers an empty page with `incomplete: true`, `group.leave` works, and every other group method answers `NOT_FOUND` `'grupo não encontrado'`.
+- Params: `{ group }`. Not mutating. Result: `{ group, name, join, creator, role, members, state, community, orphan, visibility, memberInvite, writerHere, canWrite, writersBound, writersFrozen, frozenRoom }`, where `writerHere` (boolean) is whether THIS device has its own writer in the group, and `canWrite` (boolean) is whether a post from this device would succeed: the caller's fold status is `'member'`, this device has a live writer, that writer is counted by the fold (the same check that refuses an uncounted writer with `'limite do grupo atingido'`), the group is not an orphan, the group's community root is held here (and not blocklisted), and a private group's post would not be refused with `'aguardando nova chave'` (the fold is rekey-pending, it has no current epoch, or this device does not hold that epoch's key); a pending group answers `writerHere: true, canWrite: false`; `writersBound` (browser node only, an integer 0 to 8) counts the live slots of the caller's own diary in the group's slot walk (protocol section 10, "Limits": its live writers, plus the root slot once the diary bound the root key; a later leave frees no slot, only the diary's own cut does), `0` for a pending group, so the app can say why the automatic enrolment of a second device stopped (5.63); `writersFrozen` (browser node only, an integer 0 to 8) counts the diary's frozen writers there (released ones not included), and `frozenRoom` (browser node only) is the frozen reserve left, 8 minus `writersFrozen`: while it is above 0, a disconnect (5.154) whose mark freezes a live writer gives its live slot back, and at 0 it may not in that group: `frozenRoom` covers marks of 1 or more, which freeze a writer, while a mark at 0 still releases one as long as the diary has released fewer than 16 writers there; a pending group answers `writersFrozen: 0, frozenRoom: 8`; a missing field is how an older node answers; `visibility` is `'public'` or `'private'` (`'public'` for a pending group), `name` of a private group is the sealed title when the caller can decrypt it, else `null`, and `role` is as in 5.41 and `state` is the caller's fold state (`'member'`, `'requested'`, `'banned'`, `'left'`, or `'pending'`). On a held root this device has no kv `groupWriters` entry for, that the caller's diary binds now, while the fold status for that diary is absent or `'left'`, `role` is `'pending'` and `state` stays the fold's (`'left'` when the diary is absent), so the screen does not offer a newcomer join while the account's writers are still arriving. `'banned'` and `'requested'` are unchanged, and a device that already has its own entry keeps the fold's role; `memberInvite` is a boolean, whether plain members may invite (default `false`, `false` on a pending result; 5.71); `community` is the root key of the group's community (hex64, `null` for a pending group) and `orphan` is `true` when the header names a community that is not a valid community root (an orphan answers its fold state and every write method refuses it with `USAGE` `'grupo sem comunidade'`); the former `communities` array was removed in protocol v2; `'left'` also when the caller's own diary is not among the fold's members (never null). A malformed or blocklisted root -> `NOT_FOUND` `'grupo não encontrado'`. A group joined before its root arrived answers `{ group, name: null, join: null, creator: null, role: 'pending', members: 0, state: 'pending', community: null, orphan: false, memberInvite: false, writerHere: true, canWrite: false, writersBound: 0, writersFrozen: 0, frozenRoom: 8 }`; until then `group.read` answers an empty page with `incomplete: true`, `group.leave` works, and every other group method answers `NOT_FOUND` `'grupo não encontrado'`.
### 5.43 `group.members`
@@ -646,7 +646,7 @@ Who started following the caller, including a follower the caller already follow
### 5.59 `community.info`
-- Params: `{ community }`. Not mutating. Result: `{ community, name, join, creator, role, state, members, admins, groups: [{ group, name, visibility, join, role, general, last, unread }], general, incomplete, memberInvite, writerHere, canWrite, rootHere, authorityHere, writersBound, generalWritersBound }`: `writersBound` (browser node only) is as in 5.42 for the community root (`0` on a pending result); `generalWritersBound` (browser node only) is the same count for Geral's root, read from the caller's own diary so it holds even before Geral's root is here (`0` when Geral is unknown and on a pending result), because `community.join` also joins Geral and spends a slot there; `writerHere` (boolean) is whether THIS device has its own community writer, `canWrite` (boolean) whether a community write from this device would succeed: the caller's fold status is `'member'` and this device has a live community writer that the fold counts (in an approval community a community writer is live when the diary is a member; a writer past the cap still looks live so the write can name the cap, but it is not counted, so `canWrite` is false), and `authorityHere` (boolean) whether this device's own community writer carries community authority in the fold now (the creator's root or writers, or an admin's granted writers; pending answers `false`), and `rootHere` (boolean) whether this device holds the community root as its own writer (the creating device); a device that only holds the root answers all three `false`, and a second tap of `community.join` there creates the device's writer; a pending result answers `canWrite: false`; `writerHere` is `true` when this device already has a kv `groupWriters` entry for the community, and `rootHere` is `true` when that entry holds the root nonce (the creating device, including an interrupted `community.create` that already stored the root); with no entry all of them are `false`; a missing field is how an older node answers. `state` is `'ready'`, or `'pending'` when the community root is not held yet; `role` is as in 5.41 (`'left'` when the caller is not a member; `'pending'` on a held root this device has no kv entry for, that the caller's diary binds now, while the fold status for that diary is absent or `'left'`, with `state` still `'ready'`, so the screen does not offer a newcomer join while the account's writers are still arriving; `'banned'` and `'requested'` stay, and a device that already has its own entry keeps the fold's role); `members` counts the diaries whose state is `member`; `admins` is an integer count of admins (the creator included); `groups` is the catalogue in key order, at most `MAX_GROUPS_PER_COMMUNITY` (32), and `name`, `visibility`, `join` and `role` of a row are `null` when the node does not hold that group root; `last` and `unread` are on every row, an unheld root included (`last: null`, `unread: 0`): `last` is `null` or `{ name, username, preview, at, mine, hidden }`, the newest post in causal order among the posts `group.read` (5.40) would list, only when the row's `role` is `creator`, `admin` or `member` and the root is held, else `null` (`name` and `username` are `string|null`, `preview` is the peer text cut to 140 code points and `''` when hidden, `at` a canonical ISO string, `mine` and `hidden` booleans, `mine` true when the post's diary is the own diary key); `unread` is an integer `0..99` (99 means 99 or more) counting those posts after this device's seen mark (`group.seen`, 5.82) in causal order: the posts listed after the marked post when it is still listed, else those whose group-order position `{ at, writer, seq }` (the `before` order of 5.40) is greater; no mark, or a mark dropped by the 512 cap, counts all of them; it excludes the own diary's posts and hidden posts, and is `0` whenever `last` is `null`; a private group this device cannot open (browser locked or no epoch key) contributes no post and no sealed plaintext, and the read never writes, not even the seen file; `general` is the key of Geral, `null` while unknown; `incomplete` is `true` when the fold reports a missing part; `memberInvite` (boolean) is whether plain members may invite (default `false`, `false` on a pending result; 5.72); `dropped` (sorted hex keys, protocol §10) lists the groups currently taken off the list; `repair` (boolean) is `true` when the caller is C's creator, C's root has no authority, the community result's `cwDemote` is true and the caller has a live CW: the creator can then use `community.meta` through its CW and `community.removeGroup` (5.70); `leavePending` (boolean) is `true` only when this device holds a local note of the creator's own C leave (protocol §10, `community-leave.json`) whose writer is the local CW, the diary join of that CW is still on, and the block at the note's seq, on an unforked own CW, is the `group-leave` whose `leaveDigest` is the note's `digest`, whatever the diary's status in C: the creator's leave was interrupted and `community.leave` finishes it. The read writes nothing. A pending community answers `{ community, name: null, join: null, creator: null, role: 'pending', state: 'pending', members: 0, admins: 0, groups: [], general: null, incomplete: true, dropped: [], repair: false, leavePending: false, memberInvite: false, writerHere: false, canWrite: false, rootHere: false, authorityHere: false, writersBound: 0, generalWritersBound: 0 }`. An interrupted `community.create` that already stored this device's community writer answers that same pending result with `writerHere: true`, and with `rootHere: true` when the entry holds the root nonce. A malformed or blocklisted root -> `NOT_FOUND` `'comunidade não encontrada'`. Peer text goes through `displayText` (section 10).
+- Params: `{ community }`. Not mutating. Result: `{ community, name, join, creator, role, state, members, admins, groups: [{ group, name, visibility, join, role, general, last, unread }], general, incomplete, memberInvite, writerHere, canWrite, rootHere, authorityHere, writersBound, writersFrozen, frozenRoom, generalWritersBound, generalWritersFrozen, generalFrozenRoom }`: `writersBound`, `writersFrozen` and `frozenRoom` (browser node only) are as in 5.42 for the community root (`0`, `0` and `8` on a pending result); `generalWritersBound`, `generalWritersFrozen` and `generalFrozenRoom` (browser node only) are the same for Geral's root, read from the caller's own diary so they hold even before Geral's root is here (`0`, `0` and `8` when Geral is unknown and on a pending result), because `community.join` also joins Geral and spends a slot there; `writerHere` (boolean) is whether THIS device has its own community writer, `canWrite` (boolean) whether a community write from this device would succeed: the caller's fold status is `'member'` and this device has a live community writer that the fold counts (in an approval community a community writer is live when the diary is a member; a writer past the cap still looks live so the write can name the cap, but it is not counted, so `canWrite` is false), and `authorityHere` (boolean) whether this device's own community writer carries community authority in the fold now (the creator's root or writers, or an admin's granted writers; pending answers `false`), and `rootHere` (boolean) whether this device holds the community root as its own writer (the creating device); a device that only holds the root answers all three `false`, and a second tap of `community.join` there creates the device's writer; a pending result answers `canWrite: false`; `writerHere` is `true` when this device already has a kv `groupWriters` entry for the community, and `rootHere` is `true` when that entry holds the root nonce (the creating device, including an interrupted `community.create` that already stored the root); with no entry all of them are `false`; a missing field is how an older node answers. `state` is `'ready'`, or `'pending'` when the community root is not held yet; `role` is as in 5.41 (`'left'` when the caller is not a member; `'pending'` on a held root this device has no kv entry for, that the caller's diary binds now, while the fold status for that diary is absent or `'left'`, with `state` still `'ready'`, so the screen does not offer a newcomer join while the account's writers are still arriving; `'banned'` and `'requested'` stay, and a device that already has its own entry keeps the fold's role); `members` counts the diaries whose state is `member`; `admins` is an integer count of admins (the creator included); `groups` is the catalogue in key order, at most `MAX_GROUPS_PER_COMMUNITY` (32), and `name`, `visibility`, `join` and `role` of a row are `null` when the node does not hold that group root; `last` and `unread` are on every row, an unheld root included (`last: null`, `unread: 0`): `last` is `null` or `{ name, username, preview, at, mine, hidden }`, the newest post in causal order among the posts `group.read` (5.40) would list, only when the row's `role` is `creator`, `admin` or `member` and the root is held, else `null` (`name` and `username` are `string|null`, `preview` is the peer text cut to 140 code points and `''` when hidden, `at` a canonical ISO string, `mine` and `hidden` booleans, `mine` true when the post's diary is the own diary key); `unread` is an integer `0..99` (99 means 99 or more) counting those posts after this device's seen mark (`group.seen`, 5.82) in causal order: the posts listed after the marked post when it is still listed, else those whose group-order position `{ at, writer, seq }` (the `before` order of 5.40) is greater; no mark, or a mark dropped by the 512 cap, counts all of them; it excludes the own diary's posts and hidden posts, and is `0` whenever `last` is `null`; a private group this device cannot open (browser locked or no epoch key) contributes no post and no sealed plaintext, and the read never writes, not even the seen file; `general` is the key of Geral, `null` while unknown; `incomplete` is `true` when the fold reports a missing part; `memberInvite` (boolean) is whether plain members may invite (default `false`, `false` on a pending result; 5.72); `dropped` (sorted hex keys, protocol §10) lists the groups currently taken off the list; `repair` (boolean) is `true` when the caller is C's creator, C's root has no authority, the community result's `cwDemote` is true and the caller has a live CW: the creator can then use `community.meta` through its CW and `community.removeGroup` (5.70); `leavePending` (boolean) is `true` only when this device holds a local note of the creator's own C leave (protocol §10, `community-leave.json`) whose writer is the local CW, the diary join of that CW is still on, and the block at the note's seq, on an unforked own CW, is the `group-leave` whose `leaveDigest` is the note's `digest`, whatever the diary's status in C: the creator's leave was interrupted and `community.leave` finishes it. The read writes nothing. A pending community answers `{ community, name: null, join: null, creator: null, role: 'pending', state: 'pending', members: 0, admins: 0, groups: [], general: null, incomplete: true, dropped: [], repair: false, leavePending: false, memberInvite: false, writerHere: false, canWrite: false, rootHere: false, authorityHere: false, writersBound: 0, writersFrozen: 0, frozenRoom: 8, generalWritersBound: 0, generalWritersFrozen: 0, generalFrozenRoom: 8 }`. An interrupted `community.create` that already stored this device's community writer answers that same pending result with `writerHere: true`, and with `rootHere: true` when the entry holds the root nonce. A malformed or blocklisted root -> `NOT_FOUND` `'comunidade não encontrada'`. Peer text goes through `displayText` (section 10).
### 5.60 `community.admin`
@@ -673,8 +673,8 @@ Who started following the caller, including a follower the caller already follow
- Interrupted creator leave: when this device holds a local note of the creator's own C leave that matches (the same check as the `community.leave` retry and `leavePending`, 5.59 and 5.64), the join first finishes that leave, whatever the diary's status in C and before any new CW: the missing root marks at the noted cuts, the `group-confirm` of exactly that leave unless present, the off join, and clearing the note. Only then does it write the new CW and its join. A failure there throws (`uncertain: true`) and nothing is joined. The new CW's `rejoin` then names that leave, now confirmed.
- Binding join: when a node binds a new CW of a community whose header has a `rejoin` (a fresh CW, a reused CW never bound, or a CW the pending path of `community.join` reuses while C's root is not held yet), its first `on: true` `group-join` carries the same `rejoin` (the 6-key join, protocol §2); a CW without `rejoin` gets the 5-key join. The fold counts the CW's generation only when the two match (protocol §10).
- Rejoin (a diary that left): the new CW's `rejoin` names a leave of the caller's own device when it can, so a leave a stolen sibling appends never becomes the boundary of this device's history (protocol §10, "Leave sets and stale leaves"). In order: the counted leave of this device's CW when it is of the diary's latest leave set; else, when this device's CW (not the community root) is bound and counted and holds no leave, the node first appends a `group-leave` on it (cuts listing every counted own CW held, each at its contiguous length, never C's root, so a creator returning after a forged leave keeps its root powers; `seen` the fold's latest leave), then the diary's `group-confirm` for exactly that leave (the confirmation, protocol §10 "Confirmed leaves") and the `group-join` `on: false` for that CW, and names it once counted; else the first confirmed leave of the diary's latest leave set (`confirmedLeaveOf`); else the diary's causally latest counted leave (`lastLeave`). A failure after that extra leave surfaces like any other failure of the call (`uncertain: true`).
-- Result: `{ state: 'member' | 'requested' | 'left' | 'pending', group: hex64 | null }`: `group` is Geral when a Geral writer of the caller exists after the call. `'pending'`: the community root is not held yet; the next sync fetches it. Errors: the caller banned -> `USAGE` `'sem permissão na comunidade'`; a writer outside the cap -> `USAGE` `'limite da comunidade atingido'`; a full group list -> `USAGE` `'limite de grupos atingido'`; a malformed or blocklisted key -> `NOT_FOUND` `'comunidade não encontrada'`. A partially landed join (the CW written, Geral not) is reconciled by re-reading `community.info` and repeating the call, which is idempotent; the client never resends it by itself.
-- Automatic enrolment (browser node): a device of the same passkey enrols by itself, after a sync, through the same path as this method and `group.join` (a public `group-join` in the diary, and one of the 8 writer slots per account per root that the seeder never releases). Only a root the caller's own diary binds now (an `on: true` join, chosen like the seeder), that this device holds and has no kv `groupWriters` entry for (so it is enrolled at most once: an approval community never gets a second CW while the first waits), whose fold says the diary is a `member` there, and where the diary binds fewer than 6 of the 8 writers (`AUTO_ENROL_RESERVE` = 2: the last two are spent only by an explicit tap; `writersBound`, 5.42 and 5.59, shows the count). Communities first; then groups, only when this device's community writer is admitted (in an open community, or for its creator, the CW the same pass writes counts at once). Geral is planned like any group, with its own checks (the diary's status in Geral, the reserve, a full root): a diary that left Geral is not put back, and the automatic path never takes `community.join`'s Geral step, which the explicit method keeps. A listed writer the own diary never bound (a join whose diary append failed) is bound again through the same writer, never a second one; binding it spends a slot, so the reserve applies. Never a left, banned, requested, blocklisted, unheld or orphan root, never a root of an opened link or a catalogue the diary did not join, never through the pending path (a root not held, or a community whose creator is not bound yet), and never a private group while the browser is locked. No admit or grant is written automatically, and an admitted member's new device needs none (owner decision, #350): the diary's admission covers its new CW and its new writers in the community's groups, approval and private groups included, while the diary has not left or been banned (protocol section 10, "Admits list their writers"); in an approval community its groups follow on a later run, once the fold shows the new CW admitted. An admin's new device still needs a new grant for its authority. One more case, the rebox: a writer the own diary binds in a private group whose header has `box: null` (a `group.join` before the root arrived) never counts once the root arrives, so the same pass replaces it through the `group.join` path, without a new tap: the old writer's diary join goes off and a boxed writer is bound, spending a slot, so the reserve applies. A writer with another, non-null box is counted by the fold and is left to an explicit tap. Never when the fold says the diary left (a counted leave of the diary) or is banned, when the own diary already turned off a writer other than this device's in that root (local evidence of a leave or unbind on another device, even before that writer's core arrives), when the own diary is blocked, or for an orphan root, a parent community that does not admit this device or a locked browser; an approval group ends as a request, as the tap would. While the root is not held, such a boxless writer (a public one too, since the root's visibility is unknown) keeps the upkeep armed so the root's arrival plans it; a public root then plans nothing. After the rebox the kv entry names the boxed writer, so nothing is planned again. The pass is re-planned for a new foreign writer (the sync event `group-writer`, listed with the browser sync events in protocol.md) only when the writer is bound by the own diary (another device of the account) or sits in a private group where this device writes, at most 64 writer keys per session. A skipped root keeps the explicit `community.join` / `group.join`. The core node is unchanged (its diary is per device).
+- Result: `{ state: 'member' | 'requested' | 'left' | 'pending', group: hex64 | null }`: `group` is Geral when a Geral writer of the caller exists after the call. `'pending'`: the community root is not held yet; the next sync fetches it. Errors: the caller banned -> `USAGE` `'sem permissão na comunidade'`; a writer outside the cap, or when the caller's diary already holds the `MAX_GROUP_WRITERS_PER_DIARY` (8) live slots of its own slot walk in the community, counting the cut a join writes on this device's retired writer (protocol §10, "Writers named by a node") -> `USAGE` `'limite da comunidade atingido'`; a full group list -> `USAGE` `'limite de grupos atingido'`; a malformed or blocklisted key -> `NOT_FOUND` `'comunidade não encontrada'`. A partially landed join (the CW written, Geral not) is reconciled by re-reading `community.info` and repeating the call, which is idempotent; the client never resends it by itself.
+- Automatic enrolment (browser node): a device of the same passkey enrols by itself, after a sync, through the same path as this method and `group.join` (a public `group-join` in the diary, and one of the 8 live writer slots per account per root of the diary's slot walk, protocol section 10, "Limits"; a slot comes back only through the diary's own cut, a disconnect (5.154) or the cut a join writes on a writer it replaces, while the walk has room to freeze or release the writer, and an older seeder never releases one). Only a root the caller's own diary binds now (an `on: true` join, chosen like the seeder), that this device holds and has no kv `groupWriters` entry for (so it is enrolled at most once: an approval community never gets a second CW while the first waits), whose fold says the diary is a `member` there, and where fewer than 6 of the diary's 8 live slots are taken (`AUTO_ENROL_RESERVE` = 2: the last two are spent only by an explicit tap; `writersBound`, 5.42 and 5.59, shows the count). Communities first; then groups, only when this device's community writer is admitted (in an open community, or for its creator, the CW the same pass writes counts at once). Geral is planned like any group, with its own checks (the diary's status in Geral, the reserve, a full root): a diary that left Geral is not put back, and the automatic path never takes `community.join`'s Geral step, which the explicit method keeps. A listed writer the own diary never bound (a join whose diary append failed) is bound again through the same writer, never a second one; binding it spends a slot, so the reserve applies. Never a left, banned, requested, blocklisted, unheld or orphan root, never a root of an opened link or a catalogue the diary did not join, never through the pending path (a root not held, or a community whose creator is not bound yet), and never a private group while the browser is locked. No admit or grant is written automatically, and an admitted member's new device needs none (owner decision, #350): the diary's admission covers its new CW and its new writers in the community's groups, approval and private groups included, while the diary has not left or been banned (protocol section 10, "Admits list their writers"); in an approval community its groups follow on a later run, once the fold shows the new CW admitted. An admin's new device still needs a new grant for its authority. One more case, the rebox: a writer the own diary binds in a private group whose header has `box: null` (a `group.join` before the root arrived) never counts once the root arrives, so the same pass replaces it through the `group.join` path, without a new tap: the old writer's diary join goes off and a boxed writer is bound, spending a slot, so the reserve applies. A writer with another, non-null box is counted by the fold and is left to an explicit tap. Never when the fold says the diary left (a counted leave of the diary) or is banned, when the own diary already turned off a writer other than this device's in that root (local evidence of a leave or unbind on another device, even before that writer's core arrives), when the own diary is blocked, or for an orphan root, a parent community that does not admit this device or a locked browser; an approval group ends as a request, as the tap would. While the root is not held, such a boxless writer (a public one too, since the root's visibility is unknown) keeps the upkeep armed so the root's arrival plans it; a public root then plans nothing. After the rebox the kv entry names the boxed writer, so nothing is planned again. The pass is re-planned for a new foreign writer (the sync event `group-writer`, listed with the browser sync events in protocol.md) only when the writer is bound by the own diary (another device of the account) or sits in a private group where this device writes, at most 64 writer keys per session. A skipped root keeps the explicit `community.join` / `group.join`. The core node is unchanged (its diary is per device).
### 5.64 `community.leave`
diff --git a/docs/verification/feat-writer-slot-clients.md b/docs/verification/feat-writer-slot-clients.md
new file mode 100644
index 00000000..f5404ac4
--- /dev/null
+++ b/docs/verification/feat-writer-slot-clients.md
@@ -0,0 +1,116 @@
+# Vagas de escritor nos clientes (branch feat/writer-slot-clients)
+
+Parte da #350, item 8a, segunda parte (8a-2): o nó core, o nó do navegador e o app passam a contar e a devolver vagas de escritor pela caminhada do próprio diário (`slotWalk`, da primeira parte, `feat/writer-slot-walk`, #713). A leitura esparsa e a limpeza no semeador (8a-3) ficam em outra branch; `seeder.js` não muda aqui. Decisões do dono que este PR segue sem reabrir: 8 vivos, 8 congelados e 16 liberações por diário e raiz; corte 0 libera e corte a partir de 1 congela; o bloco 0, as saídas e os blocos de chave de um escritor cortado continuam contando, e uma saída depois do corte também; o total por diário no semeador continua 1024; o autocorte não espera `CAP_WRITER_CUTS`.
+
+## O que mudou
+
+Nó core:
+
+- `group.js`: `retiredWriterCut` (nova) dá o corte que um nó marca no próprio escritor que troca: 0 para um cabeçalho sem `rejoin` e mais nada, 1 para um cabeçalho com `rejoin` e mais nada (a geração lê o bloco 0), senão um a mais que a última seq guardada (o aparelho guarda o próprio núcleo inteiro), então nenhum registro dele deixa de contar. `null` sem o bloco 0. `replacedWriterCut` (escritor sem caixa de um grupo privado) continua dando 0 para o cabeçalho sem caixa e usa `retiredWriterCut` para o com caixa.
+- `member.js`, `joinLoaded`: ao trocar o próprio escritor vivo (aposentado por uma saída de qualquer aparelho, ou citando um CW que a porta retirou), `retireLocalWriter` grava a entrada desligada (se a última entrada dele está ligada) e o corte, antes da entrada nova. Uma marca que o diário já tem nele não impede o corte enquanto a caminhada ainda o tem vivo (a marca veio quando não havia espaço para congelá-lo ou liberá-lo): o corte repetido é um evento novo da caminhada, vai na marca mais baixa quando ela é menor, nunca acima, e um escritor já congelado ou liberado não recebe outro. `makeGroupRoom` vem antes da entrada desligada e do corte, como em `communityJoin`. Só o escritor deste aparelho: o de outro aparelho só é cortado por `device.disconnect`. A entrada desligada, o corte e a entrada nova passam juntos por `walkHasRoom` antes de qualquer escrita: sem vaga viva para o novo (8 vivos, ou sem espaço para congelar ou liberar o antigo), nada é gravado e a resposta é `GROUP_FULL`.
+- `member.js`, `communityJoin`: `COMMUNITY_FULL` pela caminhada do próprio diário (`walkHasRoom`), não mais pelos CWs guardados (`boundWriterCount` saiu). Um CW congelado ou liberado devolve a vaga; um que nunca contou não ocupa nenhuma. Ao trocar o CW aposentado deste aparelho, a entrada desligada e o corte dele entram na mesma conferência antes de qualquer escrita (`retireLocalWriter`), como em `joinLoaded`; o corte vai depois da saída que a entrada pode gravar nesse CW primeiro (`ownRejoinLeave`, a condição agora em `rejoinLeaveDue`), então essa saída fica abaixo dele, e antes da entrada do CW novo. O plano feito antes da saída já conta com ela (`leaving`), e a caminhada é conferida de novo depois dela, antes de gravar o corte.
+- `member.js`, `groupCuts` (pedido da coordenação): uma admissão, um banimento ou um rebaixamento num grupo nomeia os escritores que aceitariam um post novo e também todo congelado que a dobra conta, no comprimento ou no corte do diário quando ele é menor, nunca um liberado, como `cutsFor` do navegador. Antes, um congelado com corte até o comprimento não recebia o post de sondagem e ficava de fora; um escritor que o registro não nomeia é coberto desde a seq 0, então um banimento escrito pelo nó core escondia os posts dele abaixo do corte e um rebaixamento anulava os atos de administração anteriores, enquanto o navegador os mantinha.
+- `sync.js`: os tetos por diário ao buscar escritores de grupo (núcleos guardados na partida, portas em andamento, `othersHolding`, e os dois candidatos a mais de `MAX_CAP_CONTENDERS`) usam `MAX_GROUP_HELD_PER_DIARY` = 32 (8 vivos, 8 congelados e 16 liberados), como o semeador guarda, no lugar de 8. Um congelado ou liberado pode guardar uma saída que a dobra lê. As vagas continuam indo para as entradas provadas mais antigas.
+- `group-rekey.js`, `epochFacts({ cuts })`: um `group-sealed` de um escritor com corte, na seq igual ou acima de `max(corte, 1)`, não conta para a política da troca periódica, como a dobra o descarta. Os blocos de chave continuam contando. O nó core passa `view.cuts`.
+- `sync.js`, catálogo da comunidade (pedido da coordenação): a dobra que dá o catálogo (`foldHeld`) lê os cortes do diário, como a dobra do grupo, e o nó core fica como os dois caminhos do navegador. Ela passa por `heldCatalogue`, uma função pura no módulo, para o teste de unidade alcançá-la.
+
+Nó do navegador (`packages/web-member`):
+
+- `member-ops.js`: `GROUP_FULL` e `COMMUNITY_FULL` (entrada normal e pendente) pela caminhada do próprio diário (`ownWalkHasRoom`); `retireOwnWriter` em `joinLoaded`, com as mesmas regras do nó core (o corte repetido num escritor que a caminhada ainda tem vivo e `makeGroupRoom` antes da entrada desligada e do corte); `missingWriters` (o que uma admissão espera) escolhe pela caminhada (`walkCounted`), com a exceção do grupo privado da primeira parte; saídas e concessões nomeiam só os vivos (`countedCuts(..., { frozen: false })`), e uma admissão, um banimento ou um rebaixamento nomeia todo escritor contado que não foi liberado, nunca além do corte do diário (`cutsFor` e `cappedCut`), como `countedCuts` do core e, nesta rodada, `groupCuts` do core, que antes deixava de fora um congelado com corte até o comprimento; a dobra da comunidade lê os cortes do diário, como a dobra do grupo (`foldCommunity`).
+- `member-ops.js`, `communityJoin`: o mesmo autocorte do nó core ao trocar o CW aposentado deste aparelho (`retireOwnWriter` com `leaving`, `rejoinLeaveDue`), com a caminhada conferida antes de qualquer escrita e de novo antes do corte.
+- `member-ops.js`, `communitySetAdmin`: um rebaixamento com mais de `MAX_GROUP_CUTS` escritores nomeia os primeiros pela chave e nunca é recusado, como no core; só uma concessão além do teto recebe `COMMUNITY_FULL` (`adminCuts`, exportado para o teste). Com a caminhada, um diário conta no máximo 8 vivos e 8 congelados, então a recusa antiga não chegava a acontecer: a mudança deixa os dois nós com a mesma regra.
+- `group.info` (5.42) e `community.info` (5.59): `writersBound` passa a ser as vagas vivas do diário (vivos mais a vaga da raiz, de 0 a 8); novos `writersFrozen` (congelados, sem os liberados) e `frozenRoom` (8 menos `writersFrozen`); na comunidade, também `generalWritersFrozen` e `generalFrozenRoom`, de Geral, lidos do próprio diário. Resposta pendente: `0`, `0` e `8`. Sem mudança de versão do RPC: os campos de resultado não são validados por esquema, e `writersBound` entrou do mesmo jeito.
+- `sync.js`: `capVictim` e `capExcess` com `MAX_GROUP_HELD_PER_DIARY` = 32, pelas entradas provadas mais antigas, como no core.
+- `sync.js`, catálogo da comunidade: a dobra que dá o catálogo lê os cortes do diário, como a dobra do grupo. Ela saiu de `communityCatalogue` para `foldedCatalogue`, uma função pura no módulo, para o teste de unidade alcançá-la; um CW que um corte deixou entrar é esperado antes de a dobra dar o catálogo (até lá vale a dica, como antes).
+- `sync.js`, adoção de raízes ligadas ao diário (item da coordenação, achado numa revisão da `main` depois da #713): as duas leituras do próprio diário (`fold.joinEntries`) guardavam só `group-join`, então `diaryGroupKeys` caminhava sem os cortes, e um 9º escritor que ocupava uma vaga que um corte liberou contava para o semeador e para a dobra, mas não para essa adoção. Agora guardam `group-join` e `group-cut` (`WALK_TYPES`). `liveDiaryWriters`, que ainda usava a regra dos 8 primeiros, passa à caminhada (vivos e congelados) e sai do fechamento para o módulo, para o teste de unidade alcançá-la.
+- A troca periódica passa `g.built.cuts` para `epochFacts`.
+
+App (`apps/app`):
+
+- `src/groups.js`: auxiliares puros `groupSlots`, `communitySlots`, `slotReturn` (`legacy`, `returns` ou `final`, pela presença de `frozenRoom`), `reserveText`, `reserveJoinText`, `slotFullText` e `slotRefusal`. Com um nó que informa `frozenRoom`, o aviso da reserva diz que cada aparelho usa uma das 8 vagas da conta neste grupo (ou nesta comunidade) e que desconectar um aparelho antigo em Ajustes, Aparelhos conectados, devolve a vaga, ou, com `frozenRoom` 0, que ali desconectar pode não devolver (um corte em 0 ainda libera a vaga enquanto a conta tem liberações). O caminho segue o nome da tela da #710 (`DEVICES_TITLE` em `src/devices.js`), e o teste liga os dois. Com um nó antigo, o texto antigo (`DEVICE_RESERVE_TEXT`).
+- Telas `Grupo.js`, `Comunidade.js` (as duas chamadas de `community.join`) e `ComunidadeInfo.js`: o aviso pelos auxiliares; uma recusa `GROUP_FULL` ou `COMMUNITY_FULL` (sem `uncertain`) relê as informações e, se uma raiz está com as 8 vagas vivas, mostra "Sua conta já usa as 8 vagas de aparelho neste grupo. Para liberar a vaga, desconecte um aparelho antigo em Ajustes, Aparelhos conectados." (ou a variante sem reserva). Senão, o erro original. O segundo toque de confirmação continua.
+- `src/vitrine/request.js`: as respostas de `group.info` e `community.info` com os campos novos.
+
+Documentos: `docs/protocol.md` ("Writers named by a node", agora também com o autocorte de `community.join` e o rebaixamento sem recusa na comunidade; `buildCommunity`, com a dobra da comunidade lendo os cortes do diário, também no catálogo do nó core; "Bounds on hints", "Diary-bound roots (browser)", "Automatic device enrolment"), `docs/rpc.md` (5.37, o limite pela caminhada; 5.42, 5.59 e o parágrafo da entrada automática em 5.63, que dizia que o semeador nunca devolve a vaga) e `docs/app.md` (aviso da reserva, com o caminho Ajustes, Aparelhos conectados).
+
+## Merge da main
+
+`origin/main` entrou em 6046211c (com #713, #711, #714 e #710, a tela Aparelhos conectados em Ajustes). O único conflito foi a lista de `module.exports` de `packages/web-member/lib/sync.js`: as duas listas ficaram, as funções desta branch (`WALK_TYPES`, `liveDiaryWriters`) e as da `main`. Depois do merge, 1122d006 faz os textos do app apontarem para Ajustes, Aparelhos conectados.
+
+## Como verificar
+
+Cada arquivo sozinho, a partir da raiz, com `node ` (os do app com `cd apps/app && node test/`), mais `npx standard` nos arquivos mudados. A suíte inteira e a demo ficam para a CI de chegada da sessão que coordena.
+
+Testes novos ou mudados:
+
+- `packages/core/test/writer-slot-member.test.js` (novo), 9/9 (61 asserções): "a rejoin after a leave cuts the retired writer at its length before the new join, so the new one counts at the cap"; "with no frozen room left, a rejoin after a leave gets GROUP_FULL with nothing written"; "a group rejoin after the CW was retired cuts a header-only writer at 0, after its off join"; "community.join counts the own diary's slot walk, not the CWs held: COMMUNITY_FULL with nothing written"; "community.join takes a slot a cut freed in the own diary's walk"; "a community rejoin after a leave cuts the retired CW at its length before the new join, and takes its slot (#350)" (sete CWs de outros aparelhos ocupam as outras vagas); "a community rejoin after another device left plans the cut past the leave it writes on the own CW (#350)" (as 16 liberações gastas, uma a uma, então só um corte depois da saída libera a vaga: o corte fica em 2); "with no frozen room left, a community rejoin after a leave gets COMMUNITY_FULL with nothing written (#350)"; e, nesta rodada, "a rejoin repeats the cut on a retired writer a mark left live, once the walk has room to freeze it (#350)" (8 irmãos congelados enchem a reserva, a entrada, 7 irmãos vivos, a saída, uma marca no escritor no comprimento dele que o deixa vivo, e um corte 0 que libera um irmão congelado: a volta repete o corte, a caminhada congela o antigo e o novo fica vivo).
+- `packages/core/test/group.test.js`, 154/154 (1303): novo "retiredWriterCut and replacedWriterCut: the cut a node marks on the own writer it replaces (#350)".
+- `packages/core/test/group-member-4.test.js`, 10/10 (83): "a grant lists every live writer of the promoted diary at its local length" (antes "every counted writer"): a volta agora corta o escritor aposentado, a caminhada o congela, e a concessão nomeia só o vivo.
+- `packages/core/test/sync-groups-2.test.js`, 27/27 (96): novos "writers a diary cut froze or released are still fetched past its live slots (#350)" e, nesta rodada, "the catalogue fold reads the diaries' cuts, like the group fold: a creator CW a cut let in bounds the root's creates (#350)" (o diário do criador liga a raiz e 7 CWs, libera o primeiro com corte 0 e liga um 8º, que sai citando a raiz antes do segundo `group-create`, com a saída confirmada no diário: `heldCatalogue` dá só Geral; sem o corte, o 8º fica além do teto e o catálogo traz os dois grupos). A asserção de controle desse teste compara as listas ordenadas: a dobra devolve os grupos pela chave, e as chaves são aleatórias, então a ordem fixa `[geral, later]` falhava em parte das rodadas. Com a correção, 10 rodadas do arquivo sozinho, todas 27/27 (96). `sync-groups-3` 16/16 (71), `sync-groups-4` 14/14 (73) e `sync-groups-5` 8/8 (19) usam o teto novo (`sync-groups-4` lista 32 + 8 escritores).
+- `packages/core/test/group-rekey.test.js`, 16/16 (118): novo "facts: envelopes of a frozen or released writer at or past its cut do not count (#350)". `private-rekey` 10/10 (49).
+- `packages/web-member/test/group-ops.test.js`, 63/63 (480): novos "a rejoin at the per-diary cap cuts the retired writer at its length before the new join, and takes its slot (#350)", "a leave names the live writers only, never one a diary cut froze (#350)", "with the frozen reserve spent, a rejoin at the per-diary cap refuses with nothing written (#350)", "a group rejoin after the CW was retired cuts a header-only writer at 0, after its off join (#350)" e "a pending join counts the own diary's slot walk: a cut frees a slot, eight live refuse (#350)"; e, nesta rodada, "a ban from the core node and from the browser node fold the same for a frozen writer: it is named at its cut, its post below stays (#350)" (no nó core e no navegador, Bia tem um escritor congelado no comprimento depois do post "antes", um vivo com o post "vivo" e um liberado; o banimento dos dois nós nomeia o congelado e o vivo, cada um na seq 2, nunca o liberado, e os dois posts continuam visíveis nas duas dobras) e "a rejoin repeats the cut on a retired writer a mark left live, once the walk has room to freeze it (#350)" (o mesmo cenário do nó core).
+- `packages/web-member/test/community-ops.test.js`, 77/77 (564): novos "community.join counts the own diary's slot walk, not the CWs held: COMMUNITY_FULL with nothing written (#350)", "community.join takes a slot a cut freed; community.info counts live and frozen slots, Geral's too (#350)" e, nesta rodada, os três de volta à comunidade do nó core com os mesmos nomes ("a community rejoin after a leave cuts the retired CW...", "a community rejoin after another device left plans the cut past the leave...", "with no frozen room left, a community rejoin after a leave gets COMMUNITY_FULL...") e "a community demotion past MAX_GROUP_CUTS writers names the first by key and is never refused; a grant is, as in the core node (#350)" (17 cortes: o rebaixamento nomeia os 16 primeiros pela chave, a concessão é recusada, e com 16 os dois nomeiam todos).
+- `packages/web-member/test/device-upkeep-ops.test.js`, 31/31 (214): novo "a cut that freezes a writer gives its live slot back to the automatic enrolment (#350)"; o teste do escritor sem caixa que não é trocado sob um CW passa a criar 6 CWs a mais (antes 5), porque o corte agora congela o CW antigo e devolve a vaga.
+- `packages/web-member/test/sync-groups-2.test.js`, 29/29 (139): novos "the per-diary cap holds every writer the slot walk counts: 8 live, 8 frozen and 16 released (#350)" e "the own diary scan keeps the cuts: a writer joined after released slots binds its root and is live (#350)" (8 escritores ligados, desligados e cortados em 0, mais um ligado depois: `diaryGroupKeys` devolve o grupo, `liveDiaryWriters` devolve o último, e o controle só com as entradas não devolve nada) e, nesta rodada, "the catalogue fold reads the diaries' cuts, like the group fold: a CW a cut let in waits to be held (#350)" (o diário de Bia liga 8 CWs, libera um com corte 0 e liga um 9º; com os 8 guardados `foldedCatalogue` dá `null`, e com o 9º também dá Geral).
+- `packages/web-member/test/sync-diary-roots.test.js`, 9/9 (85): novo "a root the diary binds through a writer joined after released slots is fetched (#350)": ponta a ponta na DHT local, B (outro aparelho de A) adota e busca a raiz de uma comunidade que o diário de A liga por um escritor que entrou depois de 8 liberações. Usa uma sincronização contínua com `lender()`, porque o semeador nunca admite os escritores fantasmas e a página fica incompleta numa sincronização única.
+- `apps/app/test/groups.test.js`, 37/37 (555): novo "device slots: the reserve warning and the full refusal follow frozenRoom (#350)", que também confere o caminho "Ajustes, " + `DEVICES_TITLE`. `vitrine.test.js` 43/43 (4094, com os testes que vieram da `main`), com as listas de chaves atualizadas.
+
+Também verdes, sozinhos, com o código da última rodada (`groupCuts`, o corte repetido): no nó core, `group` 154/154 (1303), `group-member` 13/13 (151), `group-member-2` 10/10 (53), `group-member-3` 10/10 (84), `group-member-4` 10/10 (83), `group-member-5` 9/9 (47), `group-member-6` 9/9 (66), `group-private` 126/126 (886), `group-events` 18/18 (101), `community-fold` 116/116 (1099), `community-member` 25/25 (262), `community-member-leave` 10/10 (70), `community-member-leave-3` 9/9 (62), `private-member` 5/5 (115), `private-member-2` 5/5 (66), `private-member-3` 7/7 (37), `private-member-4` 5/5 (22), `private-rekey` 10/10 (49), `sync-private` 4/4 (16); no navegador, `community-ops` 77/77 (564), `device-upkeep-ops` 31/31 (214) e `devices-ops` 8/8 (70); `npm run lint` limpo. Na rodada anterior: no nó core, `devices` 13/13 (95) e `devices-member` 9/9 (77); no navegador, `devices-ops` 8/8 (70), `community-devices` 10/10 (47) e `periodic-rekey` 12/12 (92); no app, `devices` 32/32 (188) e `platformGate` 6/6 (773). Antes disso: no navegador, `private-ops` 24/24 (231), `session-groups` 25/25 (249), `community-devices` 10/10 (47), `page-groups` 7/7 (60), `sync-private` 4/4 (16), `devices-private-e2e` 1/1 (35), `devices-auto-e2e` 1/1 (24), `groups-e2e` 2/2 (94); no app, mais 24 arquivos, entre eles `platformGate` 6/6 (762) e `group-flow` 61/61 (473).
+
+## Prova de regressão
+
+Cada correção foi conferida tirando só ela, de forma temporária (cópia guardada fora do repositório, sem `git stash`), rodando o teste novo sozinho e restaurando o arquivo.
+
+- Nó core, `member.js` sem `retireLocalWriter` e com `boundWriterCount`: `writer-slot-member` teste 1 cai com `limite do grupo atingido`, o 3 passa 4 de 9 asserções, o 4 passa 2 de 4. Os testes 2 e 5 são controles (recusa sem nada gravado; vaga livre).
+- Navegador, sem o autocorte: `group-ops` teste "a rejoin at the per-diary cap..." cai com `limite do grupo atingido`; "a group rejoin after the CW was retired..." passa 6 de 10 (faltam a entrada desligada, o corte 0, a ordem e a liberação).
+- Navegador, saídas nomeando os congelados (`frozen: true`): "a leave names the live writers only..." cai com `registro inválido: cuts-invalid`.
+- Navegador, entrada pendente pela contagem antiga: "a pending join counts the own diary's slot walk..." cai com `limite do grupo atingido`.
+- Navegador, `COMMUNITY_FULL` pela contagem antiga: "community.join counts the own diary's slot walk..." passa 1 de 3. Com o `member-ops.js` da base inteiro, "community.join takes a slot a cut freed..." cai com `limite da comunidade atingido`.
+- Navegador, `writersBound` pela contagem antiga: "a cut that freezes a writer gives its live slot back..." passa 1 de 3 (0 de 3 com o `member-ops.js` da base inteiro).
+- Navegador, teto de sincronização em 8: `sync-groups-2` "the per-diary cap holds every writer..." passa 0 de 4.
+- Nó core, teto de sincronização em 8: `sync-groups-2` "writers a diary cut froze or released are still fetched..." passa 8 de 11 (um recusado pelo teto, dois não guardados).
+- `epochFacts` sem o corte: `group-rekey` 15/16 (117/118), falha o teste novo.
+- Adoção de raízes com `WALK_TYPES` só com `group-join`: `sync-groups-2` "the own diary scan keeps the cuts..." passa 1 de 3, e `sync-diary-roots` "a root the diary binds through a writer joined after released slots..." estoura o tempo em "B adopted L" (4 de 5). Com `liveDiaryWriters` pela regra antiga dos 8 primeiros (e os cortes lidos): o teste de unidade passa 2 de 3 (o escritor tardio não é vivo).
+- App, `slotReturn` sempre `legacy`: `groups.test.js` "device slots..." falha 12 asserções. `vitrine/request.js` sem os campos novos: `vitrine.test.js` 37/42.
+- Nó core, `communityJoin` sem o autocorte (nada planejado, nada gravado): `writer-slot-member` testes 6 e 7 caem com `limite da comunidade atingido`; o 8 (recusa sem reserva congelada) é controle e passa. Com o plano sem a saída que a entrada grava antes (`leaving: false`): o 7 cai com `limite da comunidade atingido` (o corte planejado em 0 não acha liberação), e o 6 passa, porque ali a saída já estava gravada.
+- Navegador, `communityJoin` sem o autocorte: `community-ops` "a community rejoin after a leave cuts the retired CW..." e "a community rejoin after another device left plans the cut past the leave..." caem com `limite da comunidade atingido`; o controle sem reserva congelada passa. Com `leaving: false`: o segundo cai com `limite da comunidade atingido`, o primeiro passa.
+- Navegador, rebaixamento recusado além do teto como antes: `community-ops` "a community demotion past MAX_GROUP_CUTS writers..." cai com `limite da comunidade atingido`.
+- Navegador, dobra da comunidade sem os cortes, um caminho de cada vez:
+ - só em `foldCommunity` (`member-ops.js`): `community-ops` "community.join takes a slot a cut freed..." cai com `limite da comunidade atingido` (a dobra não conta o CW novo), "a community rejoin after a leave cuts the retired CW..." passa 7 de 8 (o estado não é `member`) e "a community rejoin after another device left..." cai com `limite da comunidade atingido`. O teste de `sync-groups-2` "the catalogue fold reads the diaries' cuts..." passa, porque não usa esse caminho.
+ - só em `foldedCatalogue` (`sync.js`): `sync-groups-2` "the catalogue fold reads the diaries' cuts..." passa 1 de 2 (sem os cortes, o 9º CW fica além do teto e a dobra dá o catálogo sem esperá-lo). Os três testes de `community-ops` acima passam, porque não usam esse caminho.
+- Nó core, `heldCatalogue` sem os cortes: `sync-groups-2` do core "the catalogue fold reads the diaries' cuts, like the group fold: a creator CW a cut let in..." passa 1 de 2 (sem os cortes, o 8º CW fica além do teto, a saída dele não limita a raiz e o catálogo traz o segundo grupo); a asserção de controle, sem o corte no diário, passa.
+- Nó core, `groupCuts` só com os escritores da sondagem: `group-ops` "a ban from the core node and from the browser node fold the same..." passa 2 de 4: o nó core nomeia só o vivo (`[['live', 2]]` no lugar de `[['frozen', 2], ['live', 2]]`) e a dobra dele não dá os mesmos posts. Com o congelado de volta mas sem tirar os liberados, passa 3 de 4: o nó core nomeia também o liberado.
+- Nó core, `retireLocalWriter` sem o corte quando já há uma marca no mesmo corte ou abaixo: `writer-slot-member` "a rejoin repeats the cut on a retired writer a mark left live..." passa 3 asserções e cai com `limite do grupo atingido` (a volta planeja só a entrada desligada, e a caminhada não tem vaga viva para o novo).
+- Navegador, `retireOwnWriter` com o mesmo salto: `group-ops` "a rejoin repeats the cut on a retired writer a mark left live..." passa 2 asserções e cai com `limite do grupo atingido`.
+- `makeGroupRoom` antes da entrada desligada e do corte: nenhum teste cai sem a mudança (decisão 11).
+- Com cada correção de volta, todos passam (contagens acima).
+
+## Decisões tomadas
+
+Escolhas que o roteiro não fixava, com o motivo:
+
+1. Só o escritor deste aparelho recebe o autocorte numa entrada; o de outro aparelho só por `device.disconnect`, porque um aparelho não sabe o comprimento que outro guarda.
+2. A entrada pendente do nó core (antes de a raiz chegar) continua sem conferência por diário, como antes deste PR (não havia contagem ali para trocar). A do navegador já conferia e passa à caminhada, com a mensagem `GROUP_FULL` também numa comunidade pendente, como antes.
+3. Tetos de sincronização em 32 nos dois nós, pelas entradas provadas mais antigas, e não pela caminhada: uma sincronização nunca lê a caminhada de um diário que pode ter só em parte, e só o próprio diário liga um escritor além da caminhada.
+4. A dobra da comunidade lê os cortes do diário, como a dobra do grupo, nos dois caminhos do navegador (`foldCommunity` e `foldedCatalogue`) e no catálogo do nó core (`heldCatalogue`, pedido da coordenação), e a adoção de raízes lê `group-cut` com as entradas; `liveDiaryWriters`, `foldedCatalogue` e `heldCatalogue` ficam no módulo. O catálogo do nó core continua sem esperar a dobra completa, como antes.
+5. A conferência de "atrás" dos CWs próprios do navegador (`ownCwsBehind`) usa a caminhada. `countedCuts` do navegador espelha o do core (6ef7b3ab): congelados só em admissões e rebaixamentos, nunca além do corte do diário.
+6. Sem mudança de versão do RPC (motivo acima).
+7. App: o aviso decide pela presença de `frozenRoom`; a recusa relê as informações só numa recusa `GROUP_FULL` ou `COMMUNITY_FULL` sem `uncertain`; as duas chamadas de `community.join` em `Comunidade.js` passam pela mesma recusa; telas de comunidade dizem "nesta comunidade".
+8. O nó core não informa campos de vaga: ele é um aparelho por diário e não tem a tela que os usa.
+9. `community.join` aplica o mesmo autocorte de `group.join` ao CW aposentado deste aparelho, nos dois nós (pedido da coordenação). O plano conta com a saída que a própria entrada grava antes, e a caminhada é conferida de novo antes do corte.
+10. O rebaixamento na comunidade do navegador apara a lista como o core (pedido da coordenação), por uma função pura com teste próprio, já que com a caminhada a lista não passa de 16.
+11. `makeGroupRoom` antes da entrada desligada e do corte em `joinLoaded`, nos dois nós, como em `communityJoin` (pedido da coordenação). Não muda nada que se veja: só há escritor para trocar quando a entrada do grupo já está na lista local, e aí `makeGroupRoom` volta sem fazer nada. Por isso nenhum teste cai sem a mudança.
+12. O corte repetido num escritor aposentado que a caminhada ainda tem vivo vai na marca mais baixa que o diário já tem nele, quando ela é menor que o corte calculado, e nunca acima dela (pedido da coordenação).
+13. Num grupo, os dois nós nomeiam numa admissão, num banimento ou num rebaixamento todo escritor contado e não liberado, até `MAX_GROUP_CUTS` pela chave (8 vivos e 8 congelados cabem), como `countedCuts` já fazia na comunidade.
+
+Confirmadas pela coordenação, sem mudança:
+
+- Q4: a entrada pendente do nó core continua sem conferência por diário (item 2).
+- Q5: sem mudança de versão do RPC (item 6).
+- Q6: a entrada simples (PLAIN) de `'limite da comunidade atingido'` no mapa de erros do app continua. Com um nó antigo, uma comunidade cheia e uma conta com 8 vagas vivas dão a mesma mensagem; com um nó que informa as vagas, `slotFullText` as separa.
+
+## Fica de fora
+
+- Leitura esparsa e limpeza no semeador: 8a-3.
+- `heldCatalogue` do nó core continua dando o catálogo sem esperar a dobra completa. Já era assim antes desta branch; esta rodada não muda isso.
+- A troca do escritor sem caixa de um grupo privado (`joinLoaded`, no `member.js` do core e no `member-ops.js` do navegador, o salto por `marked`) continua sem gravar o corte quando o diário já tem uma marca nele no mesmo corte ou abaixo. O caso corrigido para os escritores aposentados (uma marca que deixou o escritor vivo porque a reserva estava cheia) continua possível ali, mas só depois de gastas as 16 liberações.
diff --git a/packages/core/lib/group-rekey.js b/packages/core/lib/group-rekey.js
index 35d64cf2..8a350c29 100644
--- a/packages/core/lib/group-rekey.js
+++ b/packages/core/lib/group-rekey.js
@@ -107,21 +107,24 @@ const parseAt = (at) => {
// The policy's facts from a private group's cores: `logs` [{ key, entries:
// [{ seq, record }] }], `counted` the writer keys the fold counts, `root`
-// the group key, `epoch` the fold's { writer, seq }. Only records valid for
-// their place count. -> { epochAt, sealedAts }.
-function epochFacts ({ logs, counted, root, epoch }) {
+// the group key, `epoch` the fold's { writer, seq }, `cuts` the fold's cut
+// per writer (its `cuts`). Only records valid for their place count, and a
+// frozen or released writer's envelopes stop at its cut as the fold drops
+// them (#350); its key blocks stay. -> { epochAt, sealedAts }.
+function epochFacts ({ logs, counted, root, epoch, cuts = null }) {
const out = { epochAt: null, sealedAts: [] }
if (epoch === null || typeof epoch !== 'object') return out
for (const log of Array.isArray(logs) ? logs : []) {
if (!log || !(counted instanceof Set) || !counted.has(log.key)) continue
const opts = { root: log.key === root, private: true }
+ const cut = cuts instanceof Map && Number.isSafeInteger(cuts.get(log.key)) ? Math.max(cuts.get(log.key), 1) : Infinity
for (const e of Array.isArray(log.entries) ? log.entries : []) {
const r = e && e.record
if (!r || typeof r !== 'object' || (r.type !== 'group-sealed' && r.type !== 'group-key')) continue
if (validateGroupRecord(r, e.seq, opts) !== null) continue
if (r.type === 'group-key') {
if (r.kid === null && log.key === epoch.writer && e.seq === epoch.seq) out.epochAt = parseAt(r.at)
- } else if (r.kid.writer === epoch.writer && r.kid.seq === epoch.seq) {
+ } else if (e.seq < cut && r.kid.writer === epoch.writer && r.kid.seq === epoch.seq) {
const ms = parseAt(r.at)
out.sealedAts.push(ms === null ? NaN : ms)
}
diff --git a/packages/core/lib/group.js b/packages/core/lib/group.js
index 1aa0aabe..72d70136 100644
--- a/packages/core/lib/group.js
+++ b/packages/core/lib/group.js
@@ -814,11 +814,30 @@ function slotEventOrder (a, b) {
// The cut a diary marks on an own writer of a private group it replaces, so
// the slot walk gives its slot back to the new writer. A boxless header is
// header-invalid there, so nothing of the writer ever counts: 0, whatever it
-// holds. A writer boxed to another key: 0 for a header with no rejoin, 1
-// for a header with a rejoin (the generation reads its block 0), else past
-// its last record, so no record it holds stops counting. `entries` are the
-// writer's records as held ({ seq, record }). null when block 0 is not held.
+// holds. A writer boxed to another key is cut like any retired writer
+// (retiredWriterCut). null when block 0 is not held.
function replacedWriterCut (entries) {
+ const held = heldWriterRecords(entries)
+ if (held === null) return null
+ if (held.head.box === null || held.head.box === undefined) return 0
+ return retiredWriterCut(entries)
+}
+
+// The cut a diary marks on its own writer when a join replaces it (retired
+// by a leave, or naming a CW the gate retired), so the slot walk frees its
+// live slot for the new writer while no record it holds stops counting: 0
+// for a header with no rejoin, 1 for a header with a rejoin (the generation
+// reads its block 0), else the length held (the device holds its own core
+// whole). `entries` are the writer's records as held ({ seq, record }).
+// null when block 0 is not held.
+function retiredWriterCut (entries) {
+ const held = heldWriterRecords(entries)
+ if (held === null) return null
+ if (held.last > 0) return held.last + 1
+ return held.head.rejoin ? 1 : 0
+}
+
+function heldWriterRecords (entries) {
if (!Array.isArray(entries)) return null
let head = null
let last = -1
@@ -827,10 +846,7 @@ function replacedWriterCut (entries) {
if (e.seq === 0) head = e.record
if (e.seq > last) last = e.seq
}
- if (!isPlainObject(head)) return null
- if (head.box === null || head.box === undefined) return 0
- if (last > 0) return last + 1
- return head.rejoin ? 1 : 0
+ return isPlainObject(head) ? { head, last } : null
}
// The walk of one diary for G (see slotWalkState): { live, frozen,
@@ -3907,6 +3923,7 @@ module.exports = {
slotWalk,
slotWalkState,
replacedWriterCut,
+ retiredWriterCut,
slotWalkStep,
slotWalkResult,
walkHasRoom,
diff --git a/packages/core/lib/member.js b/packages/core/lib/member.js
index 96a160ef..d2fc8be0 100644
--- a/packages/core/lib/member.js
+++ b/packages/core/lib/member.js
@@ -2044,13 +2044,17 @@ async function makeGroupRoom (ctx, identity, group, need = 1) {
// buildGroup does not list the writers it counted. A probe post asks the
// same fold (rejoin binding and the writer cap) so a cut names only those,
-// never past the diary's own cut on a writer (#350): a frozen writer cut
-// past its local length still takes the probe, and its signed length may
-// be past the cut.
+// never past the diary's own cut on a writer (#350). A writer the diary
+// froze takes no probe once its cut is at or below its length, and is named
+// from the fold's frozen set, as the browser node does: a ban, admission or
+// demotion that left it out would cover it from seq 0. A released writer is
+// never named (countedCuts); the fold's frozen set holds it too.
function groupCuts (loaded, member, { signed = false } = {}) {
const accepted = new Set(probePosts(loaded).map((p) => p.writer))
+ const frozen = loaded.view.frozen instanceof Set ? loaded.view.frozen : new Set()
+ const released = loaded.view.released instanceof Set ? loaded.view.released : new Set()
const cuts = loaded.view.cuts instanceof Map ? loaded.view.cuts : new Map()
- return loaded.logs.filter((l) => accepted.has(l.key) && l.diary === member)
+ return loaded.logs.filter((l) => l.diary === member && !released.has(l.key) && (accepted.has(l.key) || (frozen.has(l.key) && loaded.view.writers.has(l.key))))
.sort((a, b) => a.key.localeCompare(b.key))
.slice(0, groups.MAX_GROUP_CUTS)
.map((l) => {
@@ -2116,8 +2120,42 @@ function unheldCountedWriters (loaded, diary) {
return counted.filter((w) => !held.has(w) && !unbound.has(w))
}
-function boundWriterCount (loaded, diary) {
- return loaded.logs.filter((l) => l.diary === diary).length
+// The off join and the cut that retire this device's writer of the group
+// when a join replaces it (#350): retired by a leave, or naming a CW the
+// gate retired, it would keep its live slot in the diary's walk for good.
+// The cut sits at the length this device holds (retiredWriterCut), so every
+// record it wrote keeps counting, and goes before the new writer's join.
+// `own`: the own diary's entries. `leaving`: a community join appends a
+// leave to it first (ownRejoinLeave), so the cut planned before anything is
+// written sits past that leave, as the one written after it does.
+// A writer the walk still has live gets the cut even when a mark is already
+// there (written while the walk had no room to freeze or release it), so
+// the walk can take it now; never above the lowest mark on it.
+function retireLocalWriter (loaded, own, { leaving = false } = {}) {
+ const local = loaded.local
+ const group = loaded.group
+ if (!local || local.writer === group) return []
+ const joins = groups.groupJoinHistory(own, group)
+ const marks = groups.groupCutHistory(own, group)
+ if (!groups.slotWalk(joins, marks, group).live.has(local.writer)) return []
+ const held = loaded.input.writers.find((l) => l.key === local.writer)
+ let entries = held ? held.entries : null
+ if (leaving && Array.isArray(entries)) entries = [...entries, { seq: entries.reduce((n, e) => Math.max(n, e.seq + 1), 0), record: null }]
+ const cut = groups.retiredWriterCut(entries)
+ if (cut === null) return []
+ const out = []
+ const last = joins.filter((j) => j.writer === local.writer).pop()
+ if (last && last.on) out.push({ kind: 'join', writer: local.writer, on: false })
+ out.push({ kind: 'cut', writer: local.writer, cut: lowestCut(marks, local.writer, cut) })
+ return out
+}
+
+// `cut`, or the lowest mark `marks` (groupCutHistory items) hold on `writer`
+// when that is lower.
+function lowestCut (marks, writer, cut) {
+ let low = cut
+ for (const m of marks) if (m.writer === writer && Number.isSafeInteger(m.cut) && m.cut < low) low = m.cut
+ return low
}
async function ownGroupWriter (loaded, identity, { root = false } = {}) {
@@ -2299,7 +2337,7 @@ function foldRejoin (view, ownHex) {
// diary's latest leave. A failure after the extra leave surfaces like any
// other write of the join.
async function ownRejoinLeave (ctx, identity, c, C) {
- const { ownHex, store } = identity
+ const { ownHex } = identity
const leaveOfIn = (view) => view.leaveOf instanceof Map ? view.leaveOf : new Map()
// Only a leave of the diary's latest set: an older one would make the new
// CW a past generation.
@@ -2311,8 +2349,7 @@ async function ownRejoinLeave (ctx, identity, c, C) {
const local = c.local
const ref = (r) => ({ writer: r.writer, seq: r.seq })
if (local && leaveOfIn(c.view).has(local.writer) && current(c.view, local.writer)) return { c, rejoin: ref(leaveOfIn(c.view).get(local.writer)) }
- const bound = !!local && local.writer !== C && c.logs.some((l) => l.key === local.writer && l.diary === ownHex)
- if (bound && c.view.writers.has(local.writer) && !(await localWriterHasLeave(store, local))) {
+ if (await rejoinLeaveDue(identity, c, C)) {
const writer = await ownGroupWriter(c, identity)
const seen = foldRejoin(c.view, ownHex)
// Every counted own CW held here, as any leave of the diary: the fold
@@ -2335,6 +2372,15 @@ async function ownRejoinLeave (ctx, identity, c, C) {
return { c, rejoin: foldRejoin(c.view, ownHex) }
}
+// Whether ownRejoinLeave appends a leave to the own CW: bound by the own
+// diary, still counted, and holding no leave of its own.
+async function rejoinLeaveDue (identity, c, C) {
+ const local = c.local
+ if (!local || local.writer === C) return false
+ if (!c.logs.some((l) => l.key === local.writer && l.diary === identity.ownHex)) return false
+ return c.view.writers.has(local.writer) && !(await localWriterHasLeave(identity.store, local))
+}
+
// The local writer exists but no diary join binds it yet.
function unboundLocal (loaded) {
return !!loaded.local && loaded.local.writer !== loaded.group && !loaded.logs.some((l) => l.key === loaded.local.writer)
@@ -2383,17 +2429,19 @@ async function joinLoaded (ctx, identity, loaded, cw) {
if (!stale && !left && unboundLocal(loaded) && !(await localWriterHasLeave(store, loaded.local))) writerKey = loaded.local.writer
if (writerKey === null) {
if (groupWritersFull(loaded.view, group)) throw new UsageError(GROUP_FULL)
+ const own = await readAll(diary)
+ if (!stale) replace.push(...retireLocalWriter(loaded, own))
// The diary's own slot walk, after the off join and the cut above: a
// new writer it would not count live never counts.
- const own = await readAll(diary)
if (!groups.walkHasRoom(groups.groupJoinHistory(own, group), groups.groupCutHistory(own, group), group, replace)) throw new UsageError(GROUP_FULL)
+ // Before the off join and the cut, as communityJoin does.
+ await makeGroupRoom(ctx, identity, group)
}
for (const e of replace) {
if (e.kind === 'join') await appendRecord(diary, groupJoinRecord({ group, writer: e.writer, on: false }))
else await appendRecord(diary, groupCutRecord({ group, writer: e.writer, cut: e.cut }))
}
if (writerKey === null) {
- await makeGroupRoom(ctx, identity, group)
const rejoin = foldRejoin(loaded.view, ownHex)
const box = loaded.private ? ownBoxPublic(ctx) : null
const nonce = hex(randomBytes(16))
@@ -2629,7 +2677,7 @@ async function groupRekey (ctx, { group }) {
// `core`. jitter(group, epoch) -> [0, 1), fixed per epoch by the caller.
function periodicDecision (loaded, core, now, jitter) {
const { view } = loaded
- const facts = rekeyPolicy.epochFacts({ logs: [loaded.input.root, ...loaded.input.writers], counted: view.writers, root: loaded.group, epoch: view.epoch })
+ const facts = rekeyPolicy.epochFacts({ logs: [loaded.input.root, ...loaded.input.writers], counted: view.writers, root: loaded.group, epoch: view.epoch, cuts: view.cuts })
const mine = loaded.secrets === null ? new Set() : loaded.secrets.mine
const entitled = entitledWriters(loaded)
return rekeyPolicy.rekeyDue({
@@ -3304,13 +3352,28 @@ async function communityJoin (ctx, { community }, { affected = null } = {}) {
}
if (writerKey === null) {
if (groupWritersFull(c.view, C)) throw new UsageError(COMMUNITY_FULL)
- if (boundWriterCount(c, ownHex) >= groups.MAX_GROUP_WRITERS_PER_DIARY) throw new UsageError(COMMUNITY_FULL)
+ // The own diary's slot walk, not the CWs held: a frozen or released
+ // CW frees its slot, and one never counted holds none (#350). The
+ // CW this join replaces is retired as in a group join: its off join
+ // and its cut count in the check, before anything is written.
+ const own = await readAll(diary)
+ const planned = retireLocalWriter(c, own, { leaving: await rejoinLeaveDue(identity, c, C) })
+ if (!groups.walkHasRoom(groups.groupJoinHistory(own, C), groups.groupCutHistory(own, C), C, planned)) throw new UsageError(COMMUNITY_FULL)
const general = c.view.general
const need = 1 + (general !== null && !localGroups(ctx.dataDir).some((e) => e.group === general) ? 1 : 0)
await makeGroupRoom(ctx, identity, null, need)
const named = await ownRejoinLeave(ctx, identity, c, C)
c = named.c
rejoin = named.rejoin
+ // The cut goes after the leave ownRejoinLeave may have appended, so
+ // that leave counts below it too, and before the new CW's join.
+ const now = await readAll(diary)
+ const retire = retireLocalWriter(c, now)
+ if (!groups.walkHasRoom(groups.groupJoinHistory(now, C), groups.groupCutHistory(now, C), C, retire)) throw new UsageError(COMMUNITY_FULL)
+ for (const e of retire) {
+ if (e.kind === 'join') await appendRecord(diary, groupJoinRecord({ group: C, writer: e.writer, on: false }))
+ else await appendRecord(diary, groupCutRecord({ group: C, writer: e.writer, cut: e.cut }))
+ }
const nonce = hex(randomBytes(16))
const writer = await openGroupWriter(store, nonce)
writerKey = hex(writer.key)
diff --git a/packages/core/lib/sync.js b/packages/core/lib/sync.js
index 18df7320..2301df44 100644
--- a/packages/core/lib/sync.js
+++ b/packages/core/lib/sync.js
@@ -73,7 +73,7 @@ const { pageLifecycle, originOf, verifyClose, verifyMoved } = require('./page-li
const { certForKey, seederCertForKey } = require('./cert')
const { createBlocklistState, verifySnapshot, compareSnapshots } = require('./blocklist')
const { attachBlocklist } = require('./blocklist-net')
-const { validateGroupHeader, validateWriterHeader, buildCommunity, groupJoinHistory, groupConfirmHistory, MAX_GROUP_WRITERS, MAX_GROUPS_PER_DIARY, MAX_GROUP_WRITERS_PER_DIARY, MAX_GROUPS_PER_COMMUNITY } = require('./group')
+const { validateGroupHeader, validateWriterHeader, buildCommunity, groupJoinHistory, groupCutHistory, groupConfirmHistory, MAX_GROUP_WRITERS, MAX_GROUPS_PER_DIARY, MAX_GROUP_WRITERS_PER_DIARY, MAX_FROZEN_WRITERS_PER_DIARY, MAX_WRITER_RELEASES_PER_DIARY, MAX_GROUPS_PER_COMMUNITY } = require('./group')
const { attachGroups, GROUPS_LIMIT_MAX, GROUPS_REQUEST_TIMEOUT_MS, MESSAGE_BURST: GROUPS_BURST, MESSAGE_RATE: GROUPS_RATE } = require('./groups-net')
const {
presentState,
@@ -141,6 +141,12 @@ const GROUP_REQUEST_RATE = GROUPS_RATE / 2
// Page items whose diary or join seq a gate disproved, per seeder, before
// this run stops paging that seeder.
const MAX_BAD_HINTS = 32
+// The cores of one (group, diary) a node holds, as the seeder holds them
+// (#350): the diary's slot walk counts 8 live, 8 frozen and 16 released
+// writers at most, and a frozen or released writer may hold a leave the
+// fold reads. Slots go to the earliest proven joins: only the diary itself
+// can bind a writer past its walk, never a seeder's listing.
+const MAX_GROUP_HELD_PER_DIARY = MAX_GROUP_WRITERS_PER_DIARY + MAX_FROZEN_WRITERS_PER_DIARY + MAX_WRITER_RELEASES_PER_DIARY
// Gates in flight per hinted (group, diary) beyond the per-diary cap: a
// gated writer takes a cap slot only once its gate proves its diary and join.
const MAX_CAP_CONTENDERS = 2
@@ -385,6 +391,24 @@ function sameSet (a, b) {
return true
}
+// The groups buildCommunity counts for C from the held root, the held CWs
+// ({ key, diary, entries }) and the binding diaries' entries (diary ->
+// entries), or null while C does not fold. The community fold reads the
+// diaries' cuts, like the group fold.
+function heldCatalogue (community, root, writers, diaries) {
+ const joins = new Map()
+ const marks = new Map()
+ // A creator's confirmed leave bounds the root's creates.
+ const confirms = new Map()
+ for (const [d, entries] of diaries) {
+ joins.set(d, groupJoinHistory(entries, community))
+ marks.set(d, groupCutHistory(entries, community))
+ confirms.set(d, groupConfirmHistory(entries, community))
+ }
+ const view = buildCommunity({ root: { key: community, entries: root.entries }, writers, joins, marks, confirms })
+ return view.creator === null ? null : view.groups.slice(0, MAX_GROUPS_PER_COMMUNITY)
+}
+
// The download range of a followed diary. While nothing is deleted it is one
// linear range (Hypercore otherwise requests from a random offset, so a
// follower stores blocks out of order and readAll sees a gap until the end);
@@ -2444,20 +2468,15 @@ async function runSync ({
}
const root = logs.find((l) => l.key === community)
if (!root) return null
- const joins = new Map()
- // A creator's confirmed leave bounds the root's creates.
- const confirms = new Map()
+ const diaries = new Map()
for (const d of new Set(logs.map((l) => l.diary))) {
const core = heldBinding(d, null)
if (core === null) continue
try {
- const entries = await readAll(core)
- joins.set(d, groupJoinHistory(entries, community))
- confirms.set(d, groupConfirmHistory(entries, community))
+ diaries.set(d, await readAll(core))
} catch {}
}
- const view = buildCommunity({ root: { key: community, entries: root.entries }, writers: logs.filter((l) => l !== root), joins, confirms })
- return view.creator === null ? null : view.groups.slice(0, MAX_GROUPS_PER_COMMUNITY)
+ return heldCatalogue(community, root, logs.filter((l) => l !== root), diaries)
}
// The catalogue roots to page, per paged community: the groups its held
@@ -2991,7 +3010,7 @@ async function runSync ({
// A writer whose proven join lost the cap of its verified diary stays
// out while that diary is still full.
if (provenLater.has(gk)) {
- if (othersHolding(group, provenLater.value(gk), item.key).length >= MAX_GROUP_WRITERS_PER_DIARY) {
+ if (othersHolding(group, provenLater.value(gk), item.key).length >= MAX_GROUP_HELD_PER_DIARY) {
refuseForCap(group, item.key)
return
}
@@ -3006,7 +3025,7 @@ async function runSync ({
return
}
if (groupCores.size >= coreLimit() ||
- gatesInFlight(group, item.diary) >= MAX_GROUP_WRITERS_PER_DIARY + MAX_CAP_CONTENDERS) {
+ gatesInFlight(group, item.diary) >= MAX_GROUP_HELD_PER_DIARY + MAX_CAP_CONTENDERS) {
rescanDue.add(group)
return
}
@@ -3612,7 +3631,7 @@ async function runSync ({
// The root is one of its creator's writers and never gives way.
if (e.key !== e.group) {
const held = othersHolding(e.group, e.diary, e.key)
- if (held.length >= MAX_GROUP_WRITERS_PER_DIARY) {
+ if (held.length >= MAX_GROUP_HELD_PER_DIARY) {
// Protecting the cap from a seeder's one-time listing order: a
// writer stored in an earlier run, active or only held on disk,
// gives way to an earlier proven join as one stored in this run
@@ -4957,11 +4976,13 @@ module.exports = {
readBoundRoots,
recordBoundRoot,
runSync,
+ heldCatalogue,
createRequestPacer,
GATE_TIMEOUT_MS,
GATE_MIN_TIMEOUT_MS,
GATE_RETRY_MS,
UNVERIFIED_BUDGET_MS,
GROUP_REQUEST_BURST,
- GROUP_REQUEST_RATE
+ GROUP_REQUEST_RATE,
+ MAX_GROUP_HELD_PER_DIARY
}
diff --git a/packages/core/test/community-member-leave-5.test.js b/packages/core/test/community-member-leave-5.test.js
index 656ace49..6ac46ae1 100644
--- a/packages/core/test/community-member-leave-5.test.js
+++ b/packages/core/test/community-member-leave-5.test.js
@@ -130,7 +130,11 @@ test('after an unconfirmed on-root leave and a return whose own confirmed leave
const cw3 = entryOf(a, community).writer
await exchange(a, b)
t.is((await foldC(a, community, [entryOf(b, community).writer, cw2, cw3])).creatorConfirmSeq, Infinity, 'no confirmed leave bounds the root')
- t.alike(await cutsOf(a), [], 'no mark yet')
+ // The return cut the cw2 it replaced (#350), past the leave it wrote there
+ // so that leave still counts, and marked no root.
+ const cw2Entries = await coreEntries(a, cw2)
+ t.is(cw2Entries[cw2Entries.length - 1].record.type, 'group-leave', 'the return left on cw2')
+ t.alike((await cutsOf(a)).map((m) => [m.group, m.writer, m.cut]), [[community, cw2, cw2Entries.length]], 'only the replacement cut on cw2, at its length: no root mark yet')
t.alike(await member.communityLeave(a, { community }), { state: 'left' })
const marks = (await cutsOf(a)).map((m) => [m.group, m.writer])
t.ok(marks.some(([g, w]) => g === horta && w === horta), 'the later leave marks Horta')
diff --git a/packages/core/test/group-member-4.test.js b/packages/core/test/group-member-4.test.js
index 7fed9ba2..c0bb90f7 100644
--- a/packages/core/test/group-member-4.test.js
+++ b/packages/core/test/group-member-4.test.js
@@ -121,7 +121,7 @@ test('a leave lists every counted writer of the own diary at its local length',
t.alike(after.entries[after.length - 1].record.cuts, [{ writer: group, seq: root.length }])
})
-test('a grant lists every counted writer of the promoted diary at its local length', async (t) => {
+test('a grant lists every live writer of the promoted diary at its local length', async (t) => {
const { a, b, group } = await pair(t)
await member.groupPost(b, { group, text: 'antes' })
await member.groupLeave(b, { group })
@@ -133,7 +133,11 @@ test('a grant lists every counted writer of the promoted diary at its local leng
t.alike(await member.groupAdmin(a, { group, member: b.key, on: true }), { ok: true })
const root = await writerEntries(a, group)
const cuts = root.entries[root.length - 1].record.cuts
- t.alike(cuts, [{ writer: first.key, seq: first.length }, { writer: second.key, seq: second.length }].sort((x, y) => x.writer.localeCompare(y.writer)))
+ // The rejoin cut the retired writer at its length, so the diary's walk
+ // froze it: a grant names the live writers only (#350).
+ t.alike(cuts, [{ writer: second.key, seq: second.length }])
+ const { entries } = await member.logRead(b)
+ t.alike(groupLib.groupCutHistory(entries, group).map((m) => [m.writer, m.cut]), [[first.key, first.length]], 'the retired writer is fixed by the diary cut')
t.absent(cuts.some((c) => c.seq === 0))
})
diff --git a/packages/core/test/group-rekey.test.js b/packages/core/test/group-rekey.test.js
index 3dffa1c5..4691a470 100644
--- a/packages/core/test/group-rekey.test.js
+++ b/packages/core/test/group-rekey.test.js
@@ -229,3 +229,27 @@ test('facts: epoch at and the counted, valid envelopes under it', (t) => {
t.alike(facts.sealedAts, [T0 + DAY, T0 + 2 * DAY], 'other epochs, invalid records and uncounted writers left out')
t.alike(epochFacts({ logs, counted: new Set([G]), root: G, epoch: null }), { epochAt: null, sealedAts: [] })
})
+
+test('facts: envelopes of a frozen or released writer at or past its cut do not count (#350)', (t) => {
+ const G = 'cc'.repeat(32)
+ const F = 'dd'.repeat(32)
+ const R = 'ee'.repeat(32)
+ const kid = { writer: G, seq: 1 }
+ const nonce = '00'.repeat(24)
+ const box = Buffer.alloc(40).toString('base64')
+ const at = (d) => new Date(T0 + d * DAY).toISOString()
+ const first = grp.groupKey({ kid: null, basis: [], keys: [{ writer: G, sealed: Buffer.alloc(96).toString('base64') }], commit: 'ab'.repeat(32), at: at(0) })
+ const sealed = (d) => grp.groupSealed({ kid, nonce, box, at: at(d) })
+ const logs = [
+ { key: G, entries: [{ seq: 0, record: { type: 'group-header' } }, { seq: 1, record: first }] },
+ // F frozen at 2: seq 1 below the cut counts, seq 2 and 3 do not.
+ { key: F, entries: [{ seq: 1, record: sealed(1) }, { seq: 2, record: sealed(2) }, { seq: 3, record: sealed(3) }] },
+ // R released (cut 0): no envelope counts.
+ { key: R, entries: [{ seq: 1, record: sealed(4) }] }
+ ]
+ const counted = new Set([G, F, R])
+ const facts = epochFacts({ logs, counted, root: G, epoch: kid, cuts: new Map([[F, 2], [R, 0]]) })
+ t.is(facts.epochAt, T0)
+ t.alike(facts.sealedAts, [T0 + DAY])
+ t.is(epochFacts({ logs, counted, root: G, epoch: kid }).sealedAts.length, 4, 'control: with no cuts every envelope counts')
+})
diff --git a/packages/core/test/group.test.js b/packages/core/test/group.test.js
index 67b07052..c964c0cc 100644
--- a/packages/core/test/group.test.js
+++ b/packages/core/test/group.test.js
@@ -8,7 +8,7 @@ const {
groupCreateRecord, communityAdmit, communityBan, communityHide, groupInvites,
groupJoins, groupJoinHistory, diaryGroupKeys, orderKey, compareOrder, isFuture, buildGroup, buildCommunity,
GROUP_POST_V2_KEYS, groupKey, groupSealed, sealedInner, groupReact, groupTopic, groupPoll, groupChoice,
- slotWalk, walkHasRoom, MAX_FROZEN_WRITERS_PER_DIARY, MAX_WRITER_RELEASES_PER_DIARY
+ slotWalk, walkHasRoom, MAX_FROZEN_WRITERS_PER_DIARY, MAX_WRITER_RELEASES_PER_DIARY, replacedWriterCut, retiredWriterCut
} = require('../lib/group')
const { groupJoinRecord, groupCutRecord } = require('../lib/records')
const { K, BASE, AT, NOW, community: communityOf, groupIn, diaryLog } = require('./helpers/groups')
@@ -3585,6 +3585,23 @@ test('walkHasRoom: a new writer counts only where the walk has a live slot, afte
t.absent(walkHasRoom(full, [], G, 5), 'a bad event list: no room')
})
+test('retiredWriterCut and replacedWriterCut: the cut a node marks on the own writer it replaces (#350)', (t) => {
+ const header = (extra = {}) => ({ seq: 0, record: { type: 'group-writer', box: 'ab'.repeat(32), rejoin: null, ...extra } })
+ const post = (seq) => ({ seq, record: { type: 'group-post' } })
+ const rejoin = { writer: K(0x7400), seq: 3 }
+ t.is(retiredWriterCut([header()]), 0, 'header only, no rejoin: released')
+ t.is(retiredWriterCut([header({ rejoin })]), 1, 'header only with a rejoin: the generation still reads block 0')
+ t.is(retiredWriterCut([header(), post(1), post(2)]), 3, 'more blocks: the length held')
+ t.is(retiredWriterCut([post(2), header({ rejoin }), post(1)]), 3, 'any order of the held entries')
+ t.is(retiredWriterCut([header({ box: null }), post(1)]), 2, 'a retired writer of a public group keeps its records')
+ t.is(retiredWriterCut([post(1)]), null, 'block 0 not held: no cut')
+ t.is(retiredWriterCut(null), null)
+ t.is(replacedWriterCut([header({ box: null }), post(1)]), 0, 'a boxless writer of a private group is released whatever it holds')
+ t.is(replacedWriterCut([header(), post(1)]), 2, 'a writer boxed to another key is cut like a retired one')
+ t.is(replacedWriterCut([header({ rejoin })]), 1)
+ t.is(replacedWriterCut([]), null)
+})
+
// The slot walk reads the diary's joins and marks as the nodes hand them to
// the fold and to walkHasRoom (after the deletion fold): a deleted group-cut
// keeps its writer frozen or released, and a deleted join keeps its slot.
diff --git a/packages/core/test/sync-groups-2.test.js b/packages/core/test/sync-groups-2.test.js
index ae55c5e5..b4968d26 100644
--- a/packages/core/test/sync-groups-2.test.js
+++ b/packages/core/test/sync-groups-2.test.js
@@ -12,11 +12,11 @@ const crypto = require('hypercore-crypto')
const Corestore = require('corestore')
const { testnet, until } = require('./helpers/net')
const { tmpDir } = require('./helpers/tmp')
-const { runSync, recordBoundRoot } = require('../lib/sync')
+const { runSync, recordBoundRoot, heldCatalogue, MAX_GROUP_HELD_PER_DIARY: CAP } = require('../lib/sync')
const { createCert, createModeratorCert, createIssuerCert, createIssuedCert, generateAssociationKeyPair } = require('../lib/cert')
const { createSnapshot } = require('../lib/blocklist')
-const { writerHeader, groupHeader, groupPost, MAX_GROUP_WRITERS_PER_DIARY } = require('../lib/group')
-const { memberCertRecord, groupJoinRecord } = require('../lib/records')
+const { writerHeader, groupHeader, groupPost, groupCreateRecord, groupLeave, leaveDigest, MAX_GROUP_WRITERS_PER_DIARY } = require('../lib/group')
+const { memberCertRecord, groupJoinRecord, groupCutRecord, groupConfirmRecord } = require('../lib/records')
const store = require('../lib/store')
const { hex, PARENT, syncOnce, stored, fakeSeeder, diaryAndWriter, copyInto, runUntil, hasBlocklist, groupMember, never, inBackground, joinedRoot, blockDiary, readCerts, revokeIssuer } = require('./helpers/sync-groups')
@@ -107,7 +107,7 @@ test('stored writers of one diary listed under other diaries never exceed the pe
await d.append(b4a.from(JSON.stringify(memberCertRecord(createCert({ member: dkey }, kp)))))
const cores = [d]
const keys = []
- for (let i = 0; i < MAX_GROUP_WRITERS_PER_DIARY + 2; i++) {
+ for (let i = 0; i < CAP + 2; i++) {
const w = fake.store.get({ name: 'w' + i })
await w.ready()
await d.append(b4a.from(JSON.stringify(groupJoinRecord({ group, writer: hex(w.key), on: true }))))
@@ -125,10 +125,43 @@ test('stored writers of one diary listed under other diaries never exceed the pe
// what counts is what stays active, not who was ever downloaded.
const gone = new Set(events.filter((ev) => ev.event === 'group-skipped' && ev.reason === 'quota').map((ev) => ev.key))
const active = new Set(events.filter((ev) => ev.event === 'group-downloaded' && keys.includes(ev.key) && !gone.has(ev.key)).map((ev) => ev.key))
- t.ok(active.size <= MAX_GROUP_WRITERS_PER_DIARY, 'at most the cap stays active: ' + active.size)
+ t.ok(active.size <= CAP, 'at most the cap stays active: ' + active.size)
t.ok(events.some((ev) => ev.event === 'group-skipped' && keys.includes(ev.key) && ev.reason === 'quota'), 'the rest are refused for quota')
})
+test('writers a diary cut froze or released are still fetched past its live slots (#350)', async (t) => {
+ t.timeout(120000)
+ const { bootstrap } = await testnet(t)
+ const kp = generateAssociationKeyPair()
+ const group = hex(crypto.randomBytes(32))
+ const items = []
+ const fake = await fakeSeeder(t, bootstrap, kp, items)
+ const d = fake.store.get({ name: 'diary' })
+ await d.ready()
+ const dkey = hex(d.key)
+ await d.append(b4a.from(JSON.stringify(memberCertRecord(createCert({ member: dkey }, kp)))))
+ const cores = [d]
+ const keys = []
+ // Two writers more than the live slots, one released and one frozen by
+ // the diary's cuts: each may hold a leave the fold reads.
+ for (let i = 0; i < MAX_GROUP_WRITERS_PER_DIARY + 2; i++) {
+ const w = fake.store.get({ name: 'w' + i })
+ await w.ready()
+ await d.append(b4a.from(JSON.stringify(groupJoinRecord({ group, writer: hex(w.key), on: true }))))
+ await w.append(b4a.from(JSON.stringify(writerHeader({ group, diary: dkey, member: PARENT }))))
+ keys.push(hex(w.key))
+ items.push({ key: hex(w.key), diary: dkey, length: 1, seq: i + 1 })
+ }
+ await d.append(b4a.from(JSON.stringify(groupCutRecord({ group, writer: keys[0], cut: 0 }))))
+ await d.append(b4a.from(JSON.stringify(groupCutRecord({ group, writer: keys[1], cut: 1 }))))
+ items.sort((x, y) => x.key < y.key ? -1 : 1)
+ const m = await groupMember(t, kp, group)
+ await copyInto(fake.store, m, cores)
+ const { events } = await syncOnce(bootstrap, m)
+ t.absent(events.some((ev) => ev.event === 'group-skipped' && keys.includes(ev.key) && ev.reason === 'quota'), 'none refused for the cap')
+ for (const k of keys) t.ok(await stored(m, k), 'stored ' + k.slice(0, 8))
+})
+
test('writers kept on disk after a quota stop hold their per-diary slots, in the run and after a restart', async (t) => {
t.timeout(180000)
const { bootstrap } = await testnet(t)
@@ -141,7 +174,7 @@ test('writers kept on disk after a quota stop hold their per-diary slots, in the
const dkey = hex(d.key)
await d.append(b4a.from(JSON.stringify(memberCertRecord(createCert({ member: dkey }, kp)))))
const writers = []
- for (let i = 0; i <= MAX_GROUP_WRITERS_PER_DIARY; i++) {
+ for (let i = 0; i <= CAP; i++) {
const w = fake.store.get({ name: 'w' + i })
await w.ready()
await d.append(b4a.from(JSON.stringify(groupJoinRecord({ group, writer: hex(w.key), on: true }))))
@@ -149,29 +182,29 @@ test('writers kept on disk after a quota stop hold their per-diary slots, in the
writers.push(w)
}
const keys = writers.map((w) => hex(w.key))
- const late = keys[MAX_GROUP_WRITERS_PER_DIARY]
- for (let i = 0; i < MAX_GROUP_WRITERS_PER_DIARY; i++) items.push({ key: keys[i], diary: dkey, length: 1, seq: i + 1 })
+ const late = keys[CAP]
+ for (let i = 0; i < CAP; i++) items.push({ key: keys[i], diary: dkey, length: 1, seq: i + 1 })
items.sort((x, y) => x.key < y.key ? -1 : 1)
const m = await groupMember(t, kp, group)
- // The quota leaves room for the diary (cert and nine joins).
- const quota = MAX_GROUP_WRITERS_PER_DIARY + 4
+ // The quota leaves room for the diary (its cert and CAP + 1 joins).
+ const quota = CAP + 4
const opts = { maxLogBlocks: quota, pageRetryMs: 100, pageRefreshMs: 300 }
const decided = (evs) => evs.some((ev) => ev.key === late && (ev.event === 'group-skipped' || ev.event === 'group-downloaded'))
let phase = 0
const { events } = await runUntil(m, bootstrap, (evs) => {
const has = (ev, k) => ev.key === k
- if (phase === 0 && keys.slice(0, MAX_GROUP_WRITERS_PER_DIARY).every((k) => evs.some((ev) => ev.event === 'group-downloaded' && has(ev, k)))) {
+ if (phase === 0 && keys.slice(0, CAP).every((k) => evs.some((ev) => ev.event === 'group-downloaded' && has(ev, k)))) {
phase = 1
// Every held writer grows past the log quota and stops (blocks kept).
inBackground(async () => {
- for (const w of writers.slice(0, MAX_GROUP_WRITERS_PER_DIARY)) {
+ for (const w of writers.slice(0, CAP)) {
for (let i = 0; i < quota; i++) await w.append(b4a.from(JSON.stringify(groupPost({ text: 'p' + i }))))
}
})
}
- if (phase === 1 && keys.slice(0, MAX_GROUP_WRITERS_PER_DIARY).every((k) => evs.some((ev) => ev.event === 'group-skipped' && ev.reason === 'quota' && has(ev, k)))) {
+ if (phase === 1 && keys.slice(0, CAP).every((k) => evs.some((ev) => ev.event === 'group-skipped' && ev.reason === 'quota' && has(ev, k)))) {
phase = 2
- items.push({ key: late, diary: dkey, length: 1, seq: MAX_GROUP_WRITERS_PER_DIARY + 1 })
+ items.push({ key: late, diary: dkey, length: 1, seq: CAP + 1 })
items.sort((x, y) => x.key < y.key ? -1 : 1)
}
return phase === 2 && decided(evs)
@@ -237,7 +270,7 @@ test('writers listed latest join first converge on the earliest joins within the
const dkey = hex(d.key)
await d.append(b4a.from(JSON.stringify(memberCertRecord(createCert({ member: dkey }, kp)))))
const writers = []
- const n = MAX_GROUP_WRITERS_PER_DIARY + 2
+ const n = CAP + 2
for (let i = 0; i < n; i++) {
const w = fake.store.get({ name: 'w' + i })
await w.ready()
@@ -279,12 +312,12 @@ test('writers listed latest join first converge on the earliest joins within the
t.ok(skipped(r1.events, keys[n - 2]), 'and so does the next latest joiner')
t.absent(await stored(m, keys[n - 1]), 'its blocks are purged')
t.absent(await stored(m, keys[n - 2]), 'and so are the other evicted holder\'s')
- t.is(await storedCount(), MAX_GROUP_WRITERS_PER_DIARY, 'stored writers of the diary stay at the cap')
+ t.is(await storedCount(), CAP, 'stored writers of the diary stay at the cap')
// After a restart every stored writer counts the others on disk, and the
// later joiners are refused against their proven joins.
const r2 = await runUntil(m, bootstrap, (evs) => skipped(evs, keys[n - 1]) && skipped(evs, keys[n - 2]), { pageRetryMs: 100, pageRefreshMs: 300 })
t.absent(downloaded(r2.events, keys[n - 1]) || downloaded(r2.events, keys[n - 2]), 'no later joiner comes back after a restart')
- t.is(await storedCount(), MAX_GROUP_WRITERS_PER_DIARY, 'still at the cap: ' + (await storedCount()))
+ t.is(await storedCount(), CAP, 'still at the cap: ' + (await storedCount()))
})
test('a writer stored in an earlier run gives its slot to an earlier proven join listed later', async (t) => {
@@ -299,7 +332,7 @@ test('a writer stored in an earlier run gives its slot to an earlier proven join
const dkey = hex(d.key)
await d.append(b4a.from(JSON.stringify(memberCertRecord(createCert({ member: dkey }, kp)))))
const writers = []
- const n = MAX_GROUP_WRITERS_PER_DIARY + 1
+ const n = CAP + 1
for (let i = 0; i < n; i++) {
const w = fake.store.get({ name: 'w' + i })
await w.ready()
@@ -323,7 +356,7 @@ test('a writer stored in an earlier run gives its slot to an earlier proven join
// Run 1: the seeder lists every writer but the earliest joiner.
for (let i = 1; i < n; i++) list(i)
await runUntil(m, bootstrap, (evs) => keys.slice(1).every((k) => downloaded(evs, k)), opts)
- t.is(await storedCount(), MAX_GROUP_WRITERS_PER_DIARY, 'run 1 stores the cap')
+ t.is(await storedCount(), CAP, 'run 1 stores the cap')
t.absent(await stored(m, keys[0]), 'without the earliest joiner')
// Run 2: the earliest joiner is listed too.
list(0)
@@ -332,7 +365,7 @@ test('a writer stored in an earlier run gives its slot to an earlier proven join
t.ok(r2.events.some((ev) => ev.event === 'group-skipped' && ev.key === keys[n - 1] && ev.reason === 'quota'), 'the latest joiner gives way')
t.ok(await stored(m, keys[0]), 'the earliest joiner is stored')
t.absent(await stored(m, keys[n - 1]), 'the latest joiner is purged')
- t.is(await storedCount(), MAX_GROUP_WRITERS_PER_DIARY, 'the stored count stays at the cap')
+ t.is(await storedCount(), CAP, 'the stored count stays at the cap')
})
test('a stored writer listed under the wrong group leaves the table so its own group admits it', async (t) => {
@@ -758,3 +791,39 @@ test('a corrupt group-certs record reads as none: a writer without binding evide
t.absent(await stored(m, wkey), 'a writer with no binding evidence goes while an issuer is revoked')
t.ok(events.some((ev) => ev.event === 'group-skipped' && ev.key === wkey && ev.reason === 'not-in-log'))
})
+
+test('the catalogue fold reads the diaries\' cuts, like the group fold: a creator CW a cut let in bounds the root\'s creates (#350)', (t) => {
+ const kp = generateAssociationKeyPair()
+ const key = () => hex(crypto.randomBytes(32))
+ const community = key()
+ const creator = key()
+ const geral = key()
+ const later = key()
+ const log = (k, header) => ({ key: k, diary: creator, entries: [{ seq: 0, record: header }] })
+ const push = (l, record) => l.entries.push({ seq: l.entries.length, record })
+ const root = log(community, groupHeader({ kind: 'community', diary: creator, join: 'open', name: 'Bairro' }))
+ push(root, groupCreateRecord({ group: geral }))
+ const geralLength = root.entries.length
+ push(root, groupCreateRecord({ group: later }))
+ // The root takes one of the creator's live slots: seven CWs fill the rest,
+ // a cut at 0 releases the first and the eighth takes its place. The eighth
+ // leaves, listing the root before the second create, and the diary
+ // confirms it.
+ const cws = Array.from({ length: MAX_GROUP_WRITERS_PER_DIARY }, () => log(key(), writerHeader({ group: community, diary: creator })))
+ const eighth = cws[cws.length - 1]
+ const leave = groupLeave({ cuts: [{ writer: community, seq: geralLength }, { writer: eighth.key, seq: 1 }] })
+ push(eighth, leave)
+ const diary = (cut) => {
+ const d = { key: creator, entries: [{ seq: 0, record: memberCertRecord(createCert({ member: creator }, kp)) }] }
+ push(d, groupJoinRecord({ group: community, writer: community, on: true }))
+ for (const cw of cws.slice(0, -1)) push(d, groupJoinRecord({ group: community, writer: cw.key, on: true }))
+ if (cut) push(d, groupCutRecord({ group: community, writer: cws[0].key, cut: 0 }))
+ push(d, groupJoinRecord({ group: community, writer: eighth.key, on: true }))
+ push(d, groupConfirmRecord({ group: community, writer: eighth.key, seq: 1, digest: leaveDigest(leave) }))
+ push(d, groupJoinRecord({ group: community, writer: eighth.key, on: false }))
+ return new Map([[creator, d.entries]])
+ }
+ t.alike(heldCatalogue(community, root, cws, diary(true)), [geral], 'the cut lets the eighth in: its confirmed leave bounds the root, so the later create does not count')
+ // The fold lists the groups sorted by key, and the keys are random.
+ t.alike([...heldCatalogue(community, root, cws, diary(false))].sort(), [geral, later].sort(), 'with no cut the eighth is over the cap and bounds nothing')
+})
diff --git a/packages/core/test/sync-groups-3.test.js b/packages/core/test/sync-groups-3.test.js
index 6c2425a2..36b35fca 100644
--- a/packages/core/test/sync-groups-3.test.js
+++ b/packages/core/test/sync-groups-3.test.js
@@ -11,10 +11,10 @@ const Corestore = require('corestore')
const Hypercore = require('hypercore')
const { testnet, until, localSwarmOptions } = require('./helpers/net')
const { tmpDir } = require('./helpers/tmp')
-const { runSync } = require('../lib/sync')
+const { runSync, MAX_GROUP_HELD_PER_DIARY: CAP } = require('../lib/sync')
const { createCert, createModeratorCert, generateAssociationKeyPair } = require('../lib/cert')
const { createSnapshot } = require('../lib/blocklist')
-const { writerHeader, groupHeader, groupPost, groupCutHistory, MAX_GROUP_WRITERS_PER_DIARY } = require('../lib/group')
+const { writerHeader, groupHeader, groupPost, groupCutHistory } = require('../lib/group')
const { memberCertRecord, groupJoinRecord, groupConfirmRecord, groupCutRecord, postRecord, deleteRecord } = require('../lib/records')
const store = require('../lib/store')
const { hex, PARENT, syncOnce, stored, fakeSeeder, diaryAndWriter, copyInto, runUntil, groupMember } = require('./helpers/sync-groups')
@@ -64,7 +64,7 @@ test('the per-diary cap compares the earliest proven join, not the latest one a
await early.append(b4a.from(JSON.stringify(writerHeader({ group, diary: dkey, member: PARENT }))))
await d.append(b4a.from(JSON.stringify(groupJoinRecord({ group, writer: ekey, on: true })))) // seq 1
const writers = []
- for (let i = 0; i < MAX_GROUP_WRITERS_PER_DIARY; i++) {
+ for (let i = 0; i < CAP; i++) {
const w = fake.store.get({ name: 'w' + i })
await w.ready()
await d.append(b4a.from(JSON.stringify(groupJoinRecord({ group, writer: hex(w.key), on: true })))) // seq i + 2
diff --git a/packages/core/test/sync-groups-4.test.js b/packages/core/test/sync-groups-4.test.js
index b2512f6a..7eb372f1 100644
--- a/packages/core/test/sync-groups-4.test.js
+++ b/packages/core/test/sync-groups-4.test.js
@@ -6,9 +6,9 @@ const test = require('brittle')
const b4a = require('b4a')
const crypto = require('hypercore-crypto')
const { testnet, localSwarmOptions } = require('./helpers/net')
-const { runSync } = require('../lib/sync')
+const { runSync, MAX_GROUP_HELD_PER_DIARY: CAP } = require('../lib/sync')
const { createCert, generateAssociationKeyPair } = require('../lib/cert')
-const { writerHeader, groupPost, MAX_GROUP_WRITERS_PER_DIARY } = require('../lib/group')
+const { writerHeader, groupPost } = require('../lib/group')
const { memberCertRecord, groupJoinRecord } = require('../lib/records')
const { hex, PARENT, syncOnce, stored, fakeSeeder, diaryAndWriter, copyInto, runUntil, groupMember } = require('./helpers/sync-groups')
@@ -121,7 +121,7 @@ test('one diary listed with many writers: targets and gates stay within the per-
const group = hex(crypto.randomBytes(32))
const diary = hex(crypto.randomBytes(32))
const items = []
- for (let i = 0; i < 30; i++) items.push({ key: hex(crypto.randomBytes(32)), diary, length: 1, seq: i + 1 })
+ for (let i = 0; i < CAP + 8; i++) items.push({ key: hex(crypto.randomBytes(32)), diary, length: 1, seq: i + 1 })
items.sort((x, y) => x.key < y.key ? -1 : 1)
await fakeSeeder(t, bootstrap, kp, items)
const m = await groupMember(t, kp, group)
@@ -138,7 +138,7 @@ test('one diary listed with many writers: targets and gates stay within the per-
})
t.ok(result.ok, result.errors.join('; '))
// The held writers plus at most two contenders gated without a slot.
- t.ok(most <= MAX_GROUP_WRITERS_PER_DIARY + 2, 'targets only for admitted writers: ' + most)
+ t.ok(most <= CAP + 2, 'targets only for admitted writers: ' + most)
})
test('a wrong join seq hint does not hide a writer another seeder lists correctly', async (t) => {
@@ -299,7 +299,7 @@ test('fabricated low-seq writers of a full diary never stop its held writers', a
const dkey = hex(d.key)
await d.append(b4a.from(JSON.stringify(memberCertRecord(createCert({ member: dkey }, kp)))))
const writers = []
- for (let i = 0; i < MAX_GROUP_WRITERS_PER_DIARY; i++) {
+ for (let i = 0; i < CAP; i++) {
const w = fake.store.get({ name: 'w' + i })
await w.ready()
await d.append(b4a.from(JSON.stringify(groupJoinRecord({ group, writer: hex(w.key), on: true }))))
@@ -309,7 +309,7 @@ test('fabricated low-seq writers of a full diary never stop its held writers', a
}
for (let i = 0; i < 3; i++) forged.push({ key: hex(crypto.randomBytes(32)), diary: dkey, length: 1, seq: 1 })
const m = await groupMember(t, kp, group)
- // The diary is held already: eight gates of one diary read it through the
+ // The diary is held already: the gates of one diary read it through the
// main store, never through concurrent quarantine reads. A stored writer
// naming it is what keeps it at start (an unnamed diary is purged).
await copyInto(fake.store, m, [d, writers[0]])
diff --git a/packages/core/test/sync-groups-5.test.js b/packages/core/test/sync-groups-5.test.js
index 67f3ef4d..bddf1551 100644
--- a/packages/core/test/sync-groups-5.test.js
+++ b/packages/core/test/sync-groups-5.test.js
@@ -6,9 +6,10 @@ const test = require('brittle')
const b4a = require('b4a')
const crypto = require('hypercore-crypto')
const { testnet } = require('./helpers/net')
+const { MAX_GROUP_HELD_PER_DIARY: CAP } = require('../lib/sync')
const { createCert, createModeratorCert, generateAssociationKeyPair } = require('../lib/cert')
const { createSnapshot } = require('../lib/blocklist')
-const { writerHeader, groupHeader, groupPost, MAX_GROUP_WRITERS_PER_DIARY } = require('../lib/group')
+const { writerHeader, groupHeader, groupPost } = require('../lib/group')
const { memberCertRecord, groupJoinRecord } = require('../lib/records')
const { hex, PARENT, syncOnce, stored, fakeSeeder, diaryAndWriter, copyInto, runUntil, groupMember, never } = require('./helpers/sync-groups')
@@ -121,7 +122,7 @@ test('a writer evicted by an earlier proven join is still purged when its diary
await d.ready()
const dkey = hex(d.key)
await d.append(b4a.from(JSON.stringify(memberCertRecord(createCert({ member: dkey }, kp)))))
- for (let i = 0; i <= MAX_GROUP_WRITERS_PER_DIARY; i++) {
+ for (let i = 0; i <= CAP; i++) {
const w = fake.store.get({ name: 'w' + i })
await w.ready()
await d.append(b4a.from(JSON.stringify(groupJoinRecord({ group, writer: hex(w.key), on: true }))))
diff --git a/packages/core/test/writer-slot-member.test.js b/packages/core/test/writer-slot-member.test.js
new file mode 100644
index 00000000..cc2a125d
--- /dev/null
+++ b/packages/core/test/writer-slot-member.test.js
@@ -0,0 +1,257 @@
+'use strict'
+
+// #350, the clients part: the core node joins by its own diary's slot walk
+// and cuts the writer it replaces before the new join.
+
+const test = require('brittle')
+const crypto = require('hypercore-crypto')
+const groupLib = require('../lib/group')
+const store = require('../lib/store')
+const member = require('../lib/member')
+const { groupJoinRecord, groupCutRecord } = require('../lib/records')
+const { hex, person, exchange, sync, coreEntries, entryOf, rejects, world, otherDevice, joinGroup } = require('./helpers/community-member')
+
+const MAX = groupLib.MAX_GROUP_WRITERS_PER_DIARY
+
+// Appends diary records of `ctx` with no other check.
+async function diaryAppend (ctx, records) {
+ await member.withIdentity(ctx, async ({ diary }) => {
+ for (const r of records) await store.appendRecord(diary, r)
+ })
+}
+
+// A writer of `group` of another device of `ctx`, bound by its diary.
+let nonces = 0
+function sibling (ctx, group, cw) {
+ const nonce = (++nonces).toString(16).padStart(32, '0')
+ return otherDevice(ctx, group, nonce, groupLib.writerHeader({ group, diary: ctx.key, member: cw }))
+}
+
+async function ownWalk (ctx, group) {
+ const { entries } = await member.logRead(ctx)
+ return { entries, walk: groupLib.slotWalk(groupLib.groupJoinHistory(entries, group), groupLib.groupCutHistory(entries, group), group) }
+}
+
+const indexOf = (entries, type, writer) => entries.findIndex((e) => e.record && e.record.type === type && e.record.writer === writer)
+
+test('a rejoin after a leave cuts the retired writer at its length before the new join, so the new one counts at the cap', async (t) => {
+ const { a, others: [b], community } = await world(t, { names: ['Bia'] })
+ const { group } = await member.groupCreate(a, { name: 'Horta', join: 'open', community })
+ await sync(a, b)
+ t.is((await joinGroup(b, group)).state, 'member')
+ await member.groupPost(b, { group, text: 'oi' })
+ const w1 = entryOf(b, group).writer
+ const cw = entryOf(b, community).writer
+ for (let i = 1; i < MAX; i++) await sibling(b, group, cw)
+ t.alike(await member.groupLeave(b, { group }), { state: 'left' })
+ const held = (await coreEntries(b, w1)).length
+ t.is(held, 3, 'header, post and leave')
+ t.is((await joinGroup(b, group)).state, 'member')
+ const w2 = entryOf(b, group).writer
+ t.not(w2, w1, 'a new writer')
+ const { entries, walk } = await ownWalk(b, group)
+ t.alike(groupLib.groupCutHistory(entries, group).map((m) => [m.writer, m.cut]), [[w1, held]], 'one cut, at the length this device holds')
+ t.alike(groupLib.groupJoinHistory(entries, group).filter((j) => j.writer === w1).map((j) => j.on), [true, false], 'the leave already wrote the off join')
+ t.ok(indexOf(entries, 'group-cut', w1) < indexOf(entries, 'group-join', w2), 'the cut comes before the new join')
+ t.ok(walk.frozen.has(w1), 'the retired writer is frozen')
+ t.ok(walk.live.has(w2), 'the new one is live')
+ t.is(walk.live.size, MAX)
+ await sync(a, b)
+ const posts = (await member.groupRead(a, { group })).items.map((p) => p.text)
+ t.ok(posts.includes('oi'), 'the post below the cut still counts')
+})
+
+test('with no frozen room left, a rejoin after a leave gets GROUP_FULL with nothing written', async (t) => {
+ const { a, others: [b], community } = await world(t, { names: ['Bia'] })
+ const { group } = await member.groupCreate(a, { name: 'Horta', join: 'open', community })
+ await sync(a, b)
+ const cw = entryOf(b, community).writer
+ // Eight writers of other devices, all frozen: the frozen reserve is spent.
+ const old = []
+ for (let i = 0; i < groupLib.MAX_FROZEN_WRITERS_PER_DIARY; i++) old.push(await sibling(b, group, cw))
+ await diaryAppend(b, old.map((writer) => groupCutRecord({ group, writer, cut: 1 })))
+ t.is((await joinGroup(b, group)).state, 'member')
+ const w1 = entryOf(b, group).writer
+ for (let i = 1; i < MAX; i++) await sibling(b, group, cw)
+ t.alike(await member.groupLeave(b, { group }), { state: 'left' })
+ const before = (await member.logRead(b)).entries.length
+ await rejects(t, joinGroup(b, group), 'limite do grupo atingido')
+ const { entries, walk } = await ownWalk(b, group)
+ t.is(entries.length, before, 'no cut, no new join')
+ t.is(entryOf(b, group).writer, w1, 'no new writer either')
+ t.ok(walk.live.has(w1), 'the retired writer keeps its slot')
+})
+
+test('a group rejoin after the CW was retired cuts a header-only writer at 0, after its off join', async (t) => {
+ const { a, others: [b], community, geral } = await world(t, { names: ['Bia'] })
+ const { group } = await member.groupCreate(a, { name: 'Horta', join: 'open', community })
+ await sync(a, b)
+ t.is((await joinGroup(b, group)).state, 'member')
+ const w1 = entryOf(b, group).writer
+ const cw1 = entryOf(b, community).writer
+ // Another device of Bia leaves the community, listing this CW: the gate
+ // retires it, and the group writer naming it.
+ await otherDevice(b, community, 'f2'.repeat(16), groupLib.writerHeader({ group: community, diary: b.key }), [
+ (key) => groupLib.groupLeave({ cuts: [{ writer: cw1, seq: 1 }, { writer: key, seq: 1 }].sort((x, y) => x.writer.localeCompare(y.writer)) })
+ ], true)
+ t.alike(await member.communityJoin(b, { community }), { state: 'member', group: geral })
+ t.is((await coreEntries(b, w1)).length, 1, 'the group writer holds its header only')
+ await joinGroup(b, group)
+ const w2 = entryOf(b, group).writer
+ t.not(w2, w1, 'a new writer naming the new CW')
+ const { entries, walk } = await ownWalk(b, group)
+ t.alike(groupLib.groupJoinHistory(entries, group).filter((j) => j.writer === w1).map((j) => j.on), [true, false], 'the off join is still written')
+ t.alike(groupLib.groupCutHistory(entries, group).map((m) => [m.writer, m.cut]), [[w1, 0]], 'a header with no rejoin is cut at 0')
+ const off = entries.findIndex((e) => e.record && e.record.type === 'group-join' && e.record.writer === w1 && e.record.on === false)
+ t.ok(off < indexOf(entries, 'group-cut', w1), 'off join, then the cut')
+ t.ok(indexOf(entries, 'group-cut', w1) < indexOf(entries, 'group-join', w2), 'then the new join')
+ t.ok(walk.released.has(w1) && walk.live.has(w2), 'the old writer is released, the new one live')
+})
+
+test('community.join counts the own diary\'s slot walk, not the CWs held: COMMUNITY_FULL with nothing written', async (t) => {
+ const kp = crypto.keyPair()
+ const a = await person(t, kp)
+ const c = await person(t, kp, 'Caio')
+ const { community } = await member.communityCreate(a, { name: 'Bairro', join: 'open' })
+ await exchange(a, c)
+ // Eight CWs of other devices this node never held take every live slot.
+ await diaryAppend(c, Array.from({ length: MAX }, () => groupJoinRecord({ group: community, writer: hex(crypto.randomBytes(32)), on: true })))
+ const before = (await member.logRead(c)).entries.length
+ await rejects(t, member.communityJoin(c, { community }), 'limite da comunidade atingido')
+ t.is((await member.logRead(c)).entries.length, before, 'no CW bound')
+ t.absent(entryOf(c, community), 'no CW made')
+})
+
+test('community.join takes a slot a cut freed in the own diary\'s walk', async (t) => {
+ const kp = crypto.keyPair()
+ const a = await person(t, kp)
+ const c = await person(t, kp, 'Caio')
+ const { community, group: geral } = await member.communityCreate(a, { name: 'Bairro', join: 'open' })
+ await exchange(a, c)
+ const ws = Array.from({ length: MAX }, () => hex(crypto.randomBytes(32)))
+ await diaryAppend(c, ws.map((writer) => groupJoinRecord({ group: community, writer, on: true })))
+ await diaryAppend(c, [groupCutRecord({ group: community, writer: ws[0], cut: 0 })])
+ t.alike(await member.communityJoin(c, { community }), { state: 'member', group: geral })
+ const { walk } = await ownWalk(c, community)
+ t.ok(walk.live.has(entryOf(c, community).writer), 'the new CW is live')
+})
+
+// A CW of another device of `ctx` in community `community`, held here and
+// bound by its diary.
+function siblingCw (ctx, community) {
+ const nonce = (++nonces).toString(16).padStart(32, '0')
+ return otherDevice(ctx, community, nonce, groupLib.writerHeader({ group: community, diary: ctx.key, member: null }), [], true)
+}
+
+test('a community rejoin after a leave cuts the retired CW at its length before the new join, and takes its slot (#350)', async (t) => {
+ const kp = crypto.keyPair()
+ const a = await person(t, kp)
+ const c = await person(t, kp, 'Caio')
+ const { community } = await member.communityCreate(a, { name: 'Bairro', join: 'open' })
+ await exchange(a, c)
+ await member.communityJoin(c, { community })
+ const old = entryOf(c, community).writer
+ // Seven CWs of other devices of the diary: every live slot is taken.
+ for (let i = 0; i < MAX - 1; i++) await siblingCw(c, community)
+ await member.communityLeave(c, { community })
+ const held = (await coreEntries(c, old)).length
+ t.ok(held >= 2, 'the old CW holds its header and its leave')
+ t.is((await member.communityJoin(c, { community })).state, 'member')
+ const now = entryOf(c, community).writer
+ t.not(now, old, 'a new CW')
+ const { entries, walk } = await ownWalk(c, community)
+ const cuts = groupLib.groupCutHistory(entries, community).filter((m) => m.writer === old)
+ t.alike(cuts.map((m) => m.cut), [held], 'the old CW is cut once, at its length')
+ const join = entries.findIndex((e) => e.record && e.record.type === 'group-join' && e.record.writer === now && e.record.on)
+ t.ok(indexOf(entries, 'group-cut', old) < join, 'the cut comes before the new join')
+ t.ok(walk.frozen.has(old), 'the old CW is frozen')
+ t.ok(walk.live.has(now), 'the new CW is live')
+})
+
+test('a community rejoin after another device left plans the cut past the leave it writes on the own CW (#350)', async (t) => {
+ const kp = crypto.keyPair()
+ const a = await person(t, kp)
+ const c = await person(t, kp, 'Caio')
+ const { community } = await member.communityCreate(a, { name: 'Bairro', join: 'open' })
+ await exchange(a, c)
+ // Every release is spent (each join released before the next, so all of
+ // them count), so only a cut past 0 frees a slot: one planned at 0 for the
+ // header-only CW would refuse the join.
+ const released = Array.from({ length: groupLib.MAX_WRITER_RELEASES_PER_DIARY }, () => hex(crypto.randomBytes(32)))
+ await diaryAppend(c, released.flatMap((writer) => [
+ groupJoinRecord({ group: community, writer, on: true }),
+ groupCutRecord({ group: community, writer, cut: 0 })
+ ]))
+ t.is((await ownWalk(c, community)).walk.released.size, groupLib.MAX_WRITER_RELEASES_PER_DIARY, 'every release spent')
+ await member.communityJoin(c, { community })
+ const old = entryOf(c, community).writer
+ t.is((await coreEntries(c, old)).length, 1, 'the own CW holds its header only')
+ const sibs = []
+ for (let i = 0; i < MAX - 2; i++) sibs.push(await siblingCw(c, community))
+ // The eighth CW, of another device, leaves for the diary.
+ const nonce = (++nonces).toString(16).padStart(32, '0')
+ await otherDevice(c, community, nonce, groupLib.writerHeader({ group: community, diary: c.key, member: null }), [
+ (key) => groupLib.groupLeave({ cuts: [old, ...sibs, key].map((writer) => ({ writer, seq: 1 })).sort((x, y) => x.writer.localeCompare(y.writer)) })
+ ], true)
+ t.is((await member.communityJoin(c, { community })).state, 'member')
+ const now = entryOf(c, community).writer
+ const entries = await coreEntries(c, old)
+ t.alike(entries.map((e) => e.record.type), ['group-writer', 'group-leave'], 'the join wrote a leave on the own CW first')
+ const { entries: log, walk } = await ownWalk(c, community)
+ t.alike(groupLib.groupCutHistory(log, community).filter((m) => m.writer === old).map((m) => m.cut), [2], 'then the cut, past that leave')
+ t.ok(walk.frozen.has(old), 'the old CW is frozen')
+ t.ok(walk.live.has(now), 'the new CW is live')
+})
+
+test('with no frozen room left, a community rejoin after a leave gets COMMUNITY_FULL with nothing written (#350)', async (t) => {
+ const kp = crypto.keyPair()
+ const a = await person(t, kp)
+ const c = await person(t, kp, 'Caio')
+ const { community } = await member.communityCreate(a, { name: 'Bairro', join: 'open' })
+ await exchange(a, c)
+ // Eight CWs of other devices this node never held, cut past 0: the frozen
+ // reserve is spent.
+ const frozen = Array.from({ length: groupLib.MAX_FROZEN_WRITERS_PER_DIARY }, () => hex(crypto.randomBytes(32)))
+ await diaryAppend(c, [
+ ...frozen.map((writer) => groupJoinRecord({ group: community, writer, on: true })),
+ ...frozen.map((writer) => groupCutRecord({ group: community, writer, cut: 1 }))
+ ])
+ await member.communityJoin(c, { community })
+ for (let i = 0; i < MAX - 1; i++) await siblingCw(c, community)
+ await member.communityLeave(c, { community })
+ const before = (await member.logRead(c)).entries.length
+ const old = entryOf(c, community).writer
+ await rejects(t, member.communityJoin(c, { community }), 'limite da comunidade atingido')
+ t.is((await member.logRead(c)).entries.length, before, 'nothing written')
+ t.is(entryOf(c, community).writer, old, 'no new CW')
+})
+
+test('a rejoin repeats the cut on a retired writer a mark left live, once the walk has room to freeze it (#350)', async (t) => {
+ const { a, others: [b], community } = await world(t, { names: ['Bia'] })
+ const { group } = await member.groupCreate(a, { name: 'Horta', join: 'open', community })
+ await sync(a, b)
+ const cw = entryOf(b, community).writer
+ const old = []
+ for (let i = 0; i < groupLib.MAX_FROZEN_WRITERS_PER_DIARY; i++) old.push(await sibling(b, group, cw))
+ await diaryAppend(b, old.map((writer) => groupCutRecord({ group, writer, cut: 1 })))
+ t.is((await joinGroup(b, group)).state, 'member')
+ const w1 = entryOf(b, group).writer
+ for (let i = 1; i < MAX; i++) await sibling(b, group, cw)
+ t.alike(await member.groupLeave(b, { group }), { state: 'left' })
+ const held = (await coreEntries(b, w1)).length
+ // With a device id, a mark on its own writer does not read as a
+ // disconnect of this device.
+ await member.deviceHello(b, { name: 'Celular' })
+ // A mark written while the frozen reserve was spent leaves the writer
+ // live; then a frozen writer is released, so the reserve has room again.
+ await diaryAppend(b, [groupCutRecord({ group, writer: w1, cut: held }), groupCutRecord({ group, writer: old[0], cut: 0 })])
+ t.ok((await ownWalk(b, group)).walk.live.has(w1), 'the mark alone left the retired writer live')
+ t.is((await joinGroup(b, group)).state, 'member')
+ const w2 = entryOf(b, group).writer
+ t.not(w2, w1, 'a new writer')
+ const { entries, walk } = await ownWalk(b, group)
+ t.alike(groupLib.groupCutHistory(entries, group).filter((m) => m.writer === w1).map((m) => m.cut), [held, held], 'the cut is written again, at the same seq')
+ t.ok(indexOf(entries, 'group-join', w2) > entries.map((e) => e.record && e.record.type === 'group-cut' && e.record.writer === w1).lastIndexOf(true), 'the new join comes after it')
+ t.ok(walk.frozen.has(w1), 'the walk freezes the retired writer now')
+ t.ok(walk.live.has(w2), 'the new one is live')
+})
diff --git a/packages/web-member/lib/member-ops.js b/packages/web-member/lib/member-ops.js
index e8899584..4d45fb29 100644
--- a/packages/web-member/lib/member-ops.js
+++ b/packages/web-member/lib/member-ops.js
@@ -116,8 +116,9 @@ const devices = require('@varanda/core/lib/devices')
const store = require('./node-store')
const {
- buildGroup, groupJoinHistory, diaryGroupKeys, groupCutHistory, groupConfirmHistory, validateGroupHeader, validateWriterHeader, replacedWriterCut, walkHasRoom,
- orderKey, compareOrder, MAX_GROUPS_PER_DIARY, MAX_GROUP_CUTS, MAX_GROUP_WRITERS_PER_DIARY, MAX_GROUPS_PER_COMMUNITY
+ buildGroup, groupJoinHistory, diaryGroupKeys, groupCutHistory, groupConfirmHistory, validateGroupHeader, validateWriterHeader, replacedWriterCut, retiredWriterCut, walkHasRoom, slotWalk,
+ orderKey, compareOrder, MAX_GROUPS_PER_DIARY, MAX_GROUP_CUTS, MAX_GROUP_WRITERS_PER_DIARY, MAX_GROUPS_PER_COMMUNITY,
+ MAX_FROZEN_WRITERS_PER_DIARY
} = grp
const HEX64 = /^[0-9a-fA-F]{64}$/
@@ -2768,24 +2769,21 @@ async function rootUnfoldable (ctx, group) {
return root.fork !== 0 || !foldableHeader(root.entries[0].record)
}
-// The writers an admit waits for, from diary d's join history alone: the
-// MAX_GROUP_WRITERS_PER_DIARY earliest bound by an on:true join (the bound
-// root is one of its creator's), the per-diary rule from before the slot
-// walk (@varanda/core lib/group.js slotWalk), which the fold now uses; the
-// switch to the walk comes with the client nodes (#350).
-function joinedCounted (history, d, rootDiary, group) {
- const first = new Map()
- for (const j of history || []) {
- if (j.on !== true || typeof j.writer !== 'string') continue
- const seq = Number.isSafeInteger(j.seq) ? j.seq : Infinity
- if (!first.has(j.writer) || seq < first.get(j.writer)) first.set(j.writer, seq)
- }
- const rootBound = d === rootDiary && first.has(group)
- const rest = [...first].filter(([w]) => w !== group)
- .sort((x, y) => (x[1] - y[1]) || (x[0] < y[0] ? -1 : x[0] > y[0] ? 1 : 0))
- .slice(0, MAX_GROUP_WRITERS_PER_DIARY - (rootBound ? 1 : 0))
- .map(([w]) => w)
- return rootBound ? [group, ...rest] : rest
+// The slot walk of diary d in fold g (@varanda/core lib/group.js
+// slotWalk), from its join history and its marks as the fold read them.
+function diaryWalk (g, d, history = g.joins.get(d)) {
+ const marks = g.input && g.input.marks instanceof Map ? g.input.marks.get(d) : undefined
+ return slotWalk(history || [], marks, g.group)
+}
+
+// The writers of diary d the fold awaits, as the core node's
+// unheldCountedWriters: the live, frozen and released writers of its slot
+// walk (a released one may hold a leave), and the root when d is its
+// creator's diary and bound it.
+function walkCounted (g, d, history) {
+ const walk = diaryWalk(g, d, history)
+ const counted = [...walk.live, ...walk.frozen, ...walk.released]
+ return d === g.cores.get(g.group).diary && walk.rootSlot ? [g.group, ...counted] : counted
}
// Whether diary d binds a writer the fold would count that is not HELD
@@ -2804,7 +2802,7 @@ function missingWriters (g, d, history) {
for (const j of history || []) latest.set(j.writer, j.on === true)
for (const [w, on] of latest) if (!on) off.add(w)
}
- return joinedCounted(history, d, g.cores.get(g.group).diary, g.group).some((w) => !held.has(w) && !off.has(w))
+ return walkCounted(g, d, history).some((w) => !held.has(w) && !off.has(w))
}
function hasLeave (entries) {
@@ -2972,21 +2970,33 @@ function releasedWriter (g, writer) {
return g.built.released instanceof Set && g.built.released.has(writer)
}
+// The cut a record names on a counted writer, never past its diary's own
+// cut on it (#350), as the core node's countedCuts and groupCuts.
+function cappedCut (g, writer, grant) {
+ const seq = cutSeq(g.cores.get(writer), grant)
+ const cut = g.built.cuts instanceof Map ? g.built.cuts.get(writer) : undefined
+ return { writer, seq: Number.isSafeInteger(cut) && cut < seq ? cut : seq }
+}
+
function cutsFor (g, d, grant = false) {
return boundWriters(g, d).filter((writer) => g.built.writers.has(writer) && !releasedWriter(g, writer)).slice(0, MAX_GROUP_CUTS)
- .map((writer) => ({ writer, seq: cutSeq(g.cores.get(writer), grant) }))
+ .map((writer) => cappedCut(g, writer, grant))
}
// The writers of diary d the fold counts (the creator's bound root
// included), each at its local length: a leave or a grant listing them
-// covers exactly what this device saw of them.
-function countedCuts (g, d, grant = false) {
+// covers exactly what this device saw of them. `frozen: false` leaves out
+// the writers a diary cut froze (#350), as the core node: a new leave or
+// grant never names them (their cut already fixes them), so a diary with
+// eight live writers stays within the record's cuts.
+function countedCuts (g, d, grant = false, { frozen = true } = {}) {
+ const still = g.built.frozen instanceof Set ? g.built.frozen : new Set()
const out = []
for (const writer of g.built.writers) {
const c = g.cores.get(writer)
- if (c !== undefined && c.diary === d && !releasedWriter(g, writer)) out.push(writer)
+ if (c !== undefined && c.diary === d && !releasedWriter(g, writer) && (frozen || !still.has(writer))) out.push(writer)
}
- return out.sort().map((writer) => ({ writer, seq: cutSeq(g.cores.get(writer), grant) }))
+ return out.sort().map((writer) => cappedCut(g, writer, grant))
}
function statusOf (g, d) {
@@ -3031,11 +3041,22 @@ function roleHere (fold, ownHex, base, keys = null) {
return 'pending'
}
-// How many writers the own diary ever bound in this root (on:true joins):
-// the seeder never releases those slots, a later leave included.
+// The live slots the own diary takes in this root (#350): the live writers
+// of its slot walk, and the root's slot once it bound the root. A frozen or
+// released writer gave its slot back.
function diaryBound (fold, ownHex) {
- const history = fold.joins.get(ownHex) || []
- return new Set(history.filter((j) => j.on === true).map((j) => j.writer)).size
+ return liveSlots(diaryWalk(fold, ownHex))
+}
+
+function liveSlots (walk) {
+ return walk.live.size + (walk.rootSlot ? 1 : 0)
+}
+
+// The frozen writers of the own diary in this root, and the frozen reserve
+// left (rpc.md 5.42 writersFrozen, frozenRoom): while it lasts, a cut that
+// freezes a writer gives a live slot back.
+function diaryFrozen (walk) {
+ return { writersFrozen: walk.frozen.size, frozenRoom: Math.max(0, MAX_FROZEN_WRITERS_PER_DIARY - walk.frozen.size) }
}
function isActingAdmin (g, d) {
@@ -3071,12 +3092,11 @@ async function ownRejoinLeave (ctx, base, diary, c, ownHex) {
const own = c.own
const ref = (r) => ({ writer: r.writer, seq: r.seq })
if (own !== null && leaveOfIn(c).has(own.key) && current(c, own.key)) return { c, rejoin: ref(leaveOfIn(c).get(own.key)) }
- const core = own === null || own.key === c.key ? undefined : c.cores.get(own.key)
- if (core !== undefined && !hasLeave(core.entries) && boundWriters(c, ownHex).includes(own.key) && c.built.writers.has(own.key)) {
+ if (rejoinLeaveDue(c, ownHex)) {
// Every counted own CW held here, as any leave of the diary: the fold
// withdraws an unlisted one from seq 0. Never C's root: a creator back
// after a forged leave keeps its powers (cycle 14).
- const cuts = countedCuts(c, ownHex).filter((x) => x.writer !== c.key)
+ const cuts = countedCuts(c, ownHex, false, { frozen: false }).filter((x) => x.writer !== c.key)
const record = buildRecord(() => grp.groupLeave({ seen: latestLeaveOf(c, ownHex), cuts }))
const seq = await appendToOwnGroupCore(ctx, own, record)
// The diary confirms it, as for any honest leave.
@@ -3090,6 +3110,14 @@ async function ownRejoinLeave (ctx, base, diary, c, ownHex) {
return { c, rejoin: diaryRejoin(c, ownHex) }
}
+// Whether ownRejoinLeave appends a leave to this device's CW: bound by the
+// own diary, still counted, and holding no leave of its own.
+function rejoinLeaveDue (c, ownHex) {
+ const own = c.own
+ const core = own === null || own.key === c.key ? undefined : c.cores.get(own.key)
+ return core !== undefined && !hasLeave(core.entries) && boundWriters(c, ownHex).includes(own.key) && c.built.writers.has(own.key)
+}
+
// A new device: the first leave of the latest set this diary confirmed
// (turned its writer off), never a later one a stolen device forged; else
// the diary's latest leave.
@@ -3329,8 +3357,7 @@ async function pendingJoin (ctx, { group, own }, { diary, ownHex, base }, { memb
// does: without it the fold takes the CW as generation 0.
const rejoin = written && community ? headerRejoin(core) : null
if (writer === null) {
- const bound = new Set(groupJoinHistory(base.ownEntries, group).filter((j) => j.on).map((j) => j.writer))
- if (bound.size >= MAX_GROUP_WRITERS_PER_DIARY) throw new UsageError(store.GROUP_FULL)
+ if (!ownWalkHasRoom(base.ownEntries, group)) throw new UsageError(store.GROUP_FULL)
await makeGroupRoom(ctx, base, group)
const record = buildRecord(() => grp.writerHeader({ group, diary: ownHex, member, rejoin: null }))
const created = await newOwnGroupCore(ctx)
@@ -3402,7 +3429,8 @@ async function joinLoaded (ctx, { diary, ownHex, base, g }, member) {
// The off join keeps its slot in the slot walk: a cut before the new join
// gives it back (#350). Both are written only once the room check passed.
const replace = []
- if (g.private && g.own !== null && g.own.key !== g.group && !usable(g.own.key)) {
+ const stale = g.private && g.own !== null && g.own.key !== g.group && !usable(g.own.key)
+ if (stale) {
const last = groupJoinHistory(base.ownEntries, g.group).filter((j) => j.writer === g.own.key).pop()
if (last !== undefined && last.on) replace.push({ kind: 'join', writer: g.own.key, on: false })
const held = g.cores.get(g.own.key)
@@ -3419,18 +3447,20 @@ async function joinLoaded (ctx, { diary, ownHex, base, g }, member) {
let writer = !left && sameMember && !cut(g.own.key) && ownPending(g, ownHex) && usable(g.own.key) && await ownWriterDerived(ctx, g.own) ? g.own.key : null
if (writer === null) {
if (groupWritersFull(g)) throw new UsageError(store.GROUP_FULL)
+ if (!stale) replace.push(...retireOwnWriter(g, base.ownEntries))
// The diary's own slot walk, after the off join and the cut above: a
// new writer it would not count live never counts.
if (!walkHasRoom(groupJoinHistory(base.ownEntries, g.group), groupCutHistory(base.ownEntries, g.group), g.group, replace)) {
throw new UsageError(live !== null ? GROUP_BOXLESS_FULL : store.GROUP_FULL)
}
+ // Before the off join and the cut, as communityJoin does.
+ await makeGroupRoom(ctx, base, g.group)
}
for (const e of replace) {
if (e.kind === 'join') await appendOwn(ctx, diary, groupJoinRecord({ group: g.group, writer: e.writer, on: false }))
else await appendOwn(ctx, diary, groupCutRecord({ group: g.group, writer: e.writer, cut: e.cut }))
}
if (writer === null) {
- await makeGroupRoom(ctx, base, g.group)
// The causal latest leave, never the newest timestamp.
const rejoin = latestLeaveOf(g, ownHex)
const record = buildRecord(() => grp.writerHeader({ group: g.group, diary: ownHex, member, rejoin, box }))
@@ -3449,6 +3479,45 @@ async function joinLoaded (ctx, { diary, ownHex, base, g }, member) {
return { state: joinState(await refold(ctx, diary, base, g.group), ownHex, writer) }
}
+// Whether a new writer the own diary binds to `group` takes a live slot of
+// its slot walk (#350): not the writers bound or held, since a frozen or
+// released writer frees its slot and one past the cap never held any.
+function ownWalkHasRoom (ownEntries, group) {
+ return walkHasRoom(groupJoinHistory(ownEntries, group), groupCutHistory(ownEntries, group), group)
+}
+
+// The off join and the cut that retire this device's writer of the group
+// when a join replaces it (#350), as the core node's retireLocalWriter:
+// retired by a leave of any device of the diary, or naming another
+// community writer than the live one, it would keep its live slot in the
+// diary's walk for good. The cut sits at the length this device holds of
+// its own core (retiredWriterCut), so every record it wrote keeps counting.
+// `leaving`: a community join appends a leave to it first (ownRejoinLeave),
+// so the cut planned before anything is written sits past that leave, as
+// the one written after it does. A writer the walk still has live gets the
+// cut even when a mark is already there (written while the walk had no room
+// to freeze or release it), so the walk can take it now; never above the
+// lowest mark on it.
+function retireOwnWriter (g, ownEntries, { leaving = false } = {}) {
+ if (g.own === null || g.own.key === g.group) return []
+ const key = g.own.key
+ const joins = groupJoinHistory(ownEntries, g.group)
+ const marks = groupCutHistory(ownEntries, g.group)
+ if (!slotWalk(joins, marks, g.group).live.has(key)) return []
+ const held = g.cores.get(key)
+ let entries = held === undefined ? null : held.entries
+ if (leaving && Array.isArray(entries)) entries = [...entries, { seq: entries.reduce((n, e) => Math.max(n, e.seq + 1), 0), record: null }]
+ const cut = retiredWriterCut(entries)
+ if (cut === null) return []
+ const out = []
+ const last = joins.filter((j) => j.writer === key).pop()
+ if (last !== undefined && last.on) out.push({ kind: 'join', writer: key, on: false })
+ let low = cut
+ for (const m of marks) if (m.writer === key && Number.isSafeInteger(m.cut) && m.cut < low) low = m.cut
+ out.push({ kind: 'cut', writer: key, cut: low })
+ return out
+}
+
// -> { state: 'left' }: a group-leave in this device's writer, then the
// diary's join off (an index hint only). No local core is deleted.
async function groupLeave (ctx, { group }) {
@@ -3473,7 +3542,7 @@ async function groupLeave (ctx, { group }) {
// another device of the diary included.
// Every counted writer of the own diary is listed (retired ones and the
// creator's root included): the fold withdraws an unlisted one from seq 0.
- const cuts = countedCuts(g, ownHex)
+ const cuts = countedCuts(g, ownHex, false, { frozen: false })
// The fold withdraws the leaving diary's records on its other writers
// anyway, so anyone leaves at once. The creator's bound root keeps the
// cut rule: the leave must list it, or the group's charter would be
@@ -3914,7 +3983,7 @@ function groupAdmin (ctx, { group, member, on }) {
if (!on) return buildRecord(() => grp.groupAdmin({ member: d, on, cuts: cutsFor(g, d), seen: g.built.last }))
// A grant covers only the listed writers from their local length on, so
// records the member wrote before the promotion never gain authority.
- const cuts = countedCuts(g, d, true)
+ const cuts = countedCuts(g, d, true, { frozen: false })
if (cuts.length > MAX_GROUP_CUTS) throw new UsageError(store.GROUP_FULL)
return buildRecord(() => grp.groupAdmin({ member: d, on, cuts, seen: g.built.last }))
}, grantKeyChange, on ? null : demoteThroughCw(member))
@@ -4272,13 +4341,22 @@ async function groupInfo (ctx, { group }) {
memberInvite: g.built.memberInvite === true,
writerHere: g.own !== null,
canWrite: groupCanWrite(ctx, g, ownHex),
- writersBound: diaryBound(g, ownHex)
+ ...walkInfo(diaryWalk(g, ownHex))
}), (p) => {
requirePendingOwn(p)
- return { group: p.group, ...PENDING_INFO, creator: null, state: 'pending', community: null, orphan: false, visibility: 'public', memberInvite: false, writerHere: true, canWrite: false, writersBound: 0 }
+ return { group: p.group, ...PENDING_INFO, creator: null, state: 'pending', community: null, orphan: false, visibility: 'public', memberInvite: false, writerHere: true, canWrite: false, ...PENDING_WALK }
})
}
+// rpc.md 5.42 and 5.59: the own diary's live slots and frozen reserve in a
+// root, from its slot walk.
+function walkInfo (walk) {
+ const { writersFrozen, frozenRoom } = diaryFrozen(walk)
+ return { writersBound: liveSlots(walk), writersFrozen, frozenRoom }
+}
+
+const PENDING_WALK = { writersBound: 0, writersFrozen: 0, frozenRoom: MAX_FROZEN_WRITERS_PER_DIARY }
+
// The header times of the diary's writers in the fold, oldest first.
function writerTimes (g, d) {
return boundWriters(g, d).map((k) => g.cores.get(k).at).sort()
@@ -4703,7 +4781,7 @@ const rekeyPolicy = require('@varanda/core/lib/group-rekey')
async function periodicDecision (ctx, g, own, now, jitter) {
const epoch = g.built.epoch
const logs = [...g.cores].map(([key, c]) => ({ key, entries: c.entries }))
- const facts = rekeyPolicy.epochFacts({ logs, counted: g.built.writers, root: g.group, epoch })
+ const facts = rekeyPolicy.epochFacts({ logs, counted: g.built.writers, root: g.group, epoch, cuts: g.built.cuts })
const held = await withGroupCore(ctx, own.key, async (core) => ({ length: core.length, bytes: core.byteLength }))
if (held === null) return { due: false, at: null, reason: 'unheld' }
const limits = { maxLogBlocks: MAX_LOG_BLOCKS, maxLogBytes: MAX_LOG_BYTES, ...ctx.limits }
@@ -4870,16 +4948,20 @@ async function foldCommunity (ctx, base, key, kv, rec) {
}
const diaries = new Map()
const joins = new Map()
+ // The diaries' group-cut marks for C, as the core node reads them: the
+ // slot walk frees a CW's slot and stops its records at its cut (#350).
+ const marks = new Map()
const confirms = new Map()
for (const d of new Set([ownHex, header.diary, ...writers.map((w) => w.diary)])) {
const entries = await foldEntriesOf(ctx, base, d, rec)
diaries.set(d, entries)
if (entries !== null) {
joins.set(d, groupJoinHistory(entries, key))
+ marks.set(d, groupCutHistory(entries, key))
confirms.set(d, groupConfirmHistory(entries, key))
}
}
- const input = { root: { key, entries: root.entries }, writers, joins, confirms }
+ const input = { root: { key, entries: root.entries }, writers, joins, marks, confirms }
const built = grp.buildCommunity({ ...input, now: Date.now() })
return { key, group: key, header, own, built, cores, diaries, joins, input }
}
@@ -5100,7 +5182,11 @@ async function communityJoin (ctx, { community }, { general: withGeneral = true
let writer = !left && !cut && current !== undefined && ownPending(c, ownHex) && await ownWriterDerived(ctx, c.own) ? c.own.key : null
let rejoin = writer === null ? null : headerRejoin(current)
if (writer === null) {
- if (groupWritersFull(c) || boundWriters(c, ownHex).length >= MAX_GROUP_WRITERS_PER_DIARY) throw new UsageError(COMMUNITY_FULL)
+ // The CW this join replaces is retired as in a group join (#350):
+ // its off join and its cut count in the walk check, before anything
+ // is written.
+ const planned = retireOwnWriter(c, base.ownEntries, { leaving: !cut && rejoinLeaveDue(c, ownHex) })
+ if (groupWritersFull(c) || !walkHasRoom(groupJoinHistory(base.ownEntries, c.key), groupCutHistory(base.ownEntries, c.key), c.key, planned)) throw new UsageError(COMMUNITY_FULL)
// Geral's slot only when this call joins it: the upkeep's room
// check must not refuse, or evict a left entry, for a slot unused.
await makeGroupRoom(ctx, base, c.key, general === null || !withGeneral ? [] : [general])
@@ -5110,6 +5196,15 @@ async function communityJoin (ctx, { community }, { general: withGeneral = true
const named = cut ? { c, rejoin: diaryRejoin(c, ownHex) } : await ownRejoinLeave(ctx, base, diary, c, ownHex)
c = named.c
rejoin = named.rejoin
+ // The cut goes after the leave ownRejoinLeave may have appended, so
+ // that leave counts below it too, and before the new CW's join.
+ const now = await store.readAll(diary)
+ const retire = retireOwnWriter(c, now)
+ if (!walkHasRoom(groupJoinHistory(now, c.key), groupCutHistory(now, c.key), c.key, retire)) throw new UsageError(COMMUNITY_FULL)
+ for (const e of retire) {
+ if (e.kind === 'join') await appendOwn(ctx, diary, groupJoinRecord({ group: c.key, writer: e.writer, on: false }))
+ else await appendOwn(ctx, diary, groupCutRecord({ group: c.key, writer: e.writer, cut: e.cut }))
+ }
const record = buildRecord(() => grp.writerHeader({ group: c.key, diary: ownHex, member: null, rejoin }))
const created = await newOwnGroupCore(ctx)
try {
@@ -5273,7 +5368,7 @@ async function finishNotedLeave (ctx, diary, c, ownHex, ownEntries) {
function ownCwsBehind (c, d) {
const held = new Set()
for (const [key, core] of c.cores) if (core.diary === d) held.add(key)
- const counted = joinedCounted(c.joins.get(d) || [], d, c.cores.get(c.key).diary, c.key)
+ const counted = walkCounted(c, d)
if (counted.some((w) => w !== c.key && !held.has(w))) return true
for (const w of c.built.writers) {
const core = c.cores.get(w)
@@ -5304,7 +5399,7 @@ async function communityLeave (ctx, { community, force = false }) {
const own = liveOwnWriter(c, ownHex) || (statusOf(c, ownHex) === 'requested' && ownPending(c, ownHex) ? c.own : null) || (lingering ? c.own : null)
if (own === null) throw new UsageError(GROUP_UNCHANGED)
if (!c.built.writers.has(own.key)) throw new UsageError(COMMUNITY_FULL)
- const cuts = countedCuts(c, ownHex)
+ const cuts = countedCuts(c, ownHex, false, { frozen: false })
if (c.header.diary === ownHex && c.built.writers.has(c.key) && !cuts.some((x) => x.writer === c.key)) {
throw new UsageError(COMMUNITY_LEAVE_CREATOR)
}
@@ -5407,7 +5502,7 @@ async function plannedGroupLeaves (ctx, base, C, ownHex) {
if (g === null || g.community !== C || statusOf(g, ownHex) !== 'member') continue
const own = liveOwnWriter(g, ownHex)
if (own === null || !g.built.writers.has(own.key)) continue
- const cuts = countedCuts(g, ownHex)
+ const cuts = countedCuts(g, ownHex, false, { frozen: false })
if (g.cores.get(g.group).diary === ownHex && g.built.writers.has(g.group) && !cuts.some((x) => x.writer === g.group)) continue
out.push({ group: g.group, own, cuts, seen: g.built.last, priv: g.private })
} catch {}
@@ -5570,6 +5665,16 @@ function communityRemoveGroup (ctx, { community, group, undo = false }) {
})
}
+// The cuts of a community admin record over `cuts` (countedCuts, lowest
+// keys first): a grant past MAX_GROUP_CUTS is refused; a demotion never is,
+// as in the core node: the first writers by key keep their length and the
+// rest are cut at 0.
+function adminCuts (cuts, grant) {
+ if (cuts.length <= MAX_GROUP_CUTS) return cuts
+ if (grant) throw new UsageError(COMMUNITY_FULL)
+ return cuts.slice(0, MAX_GROUP_CUTS)
+}
+
// -> { community, member, on }: a group-admin in the community root, by its
// creator only, with cuts over the member's counted community writers (on
// at max(contiguous, signed) length, off at the contiguous one).
@@ -5589,8 +5694,7 @@ async function communitySetAdmin (ctx, { community, member, on }) {
// outlives it as a shield against a ban.
if (on === true && statusOf(c, d) !== 'member') throw new UsageError(COMMUNITY_MEMBER_NEEDED)
if (on !== true && !everGranted(c, d)) throw new UsageError(GROUP_UNCHANGED)
- const cuts = countedCuts(c, d, on === true)
- if (cuts.length > MAX_GROUP_CUTS) throw new UsageError(COMMUNITY_FULL)
+ const cuts = adminCuts(countedCuts(c, d, on === true, { frozen: on !== true }), on === true)
await appendToOwnGroupCore(ctx, root, rootGone
? buildRecord(() => grp.communityDemote({ member: d, cuts, seen: c.built.last }))
: buildRecord(() => grp.groupAdmin({ member: d, on: on === true, cuts, seen: c.built.last })))
@@ -5686,7 +5790,7 @@ function lastAndUnread (g, base, mark) {
return { last, unread }
}
-const PENDING_COMMUNITY = { name: null, join: null, creator: null, role: 'pending', state: 'pending', members: 0, admins: 0, groups: [], general: null, incomplete: true, leavePending: false, repair: false, dropped: [], memberInvite: false, writerHere: false, canWrite: false, rootHere: false, authorityHere: false, writersBound: 0, generalWritersBound: 0 }
+const PENDING_COMMUNITY = { name: null, join: null, creator: null, role: 'pending', state: 'pending', members: 0, admins: 0, groups: [], general: null, incomplete: true, leavePending: false, repair: false, dropped: [], memberInvite: false, writerHere: false, canWrite: false, rootHere: false, authorityHere: false, ...PENDING_WALK, generalWritersBound: 0, generalWritersFrozen: 0, generalFrozenRoom: MAX_FROZEN_WRITERS_PER_DIARY }
// rpc.md 5.59. A root not held yet is pending; a held root that is no
// community root is NOT_FOUND. Each catalogue row names its root when held.
@@ -5725,9 +5829,10 @@ async function communityInfo (ctx, { community }) {
}
// Geral's slots are spent by the same tap (community.join also joins it),
// so the app's reserve warning needs its count too.
- // From the own diary's joins, as diaryBound counts them: Geral's root
- // need not be here for the slots the account already spent in it.
- const generalBound = c.built.general ? new Set(groupJoinHistory(base.ownEntries, c.built.general).filter((j) => j.on === true).map((j) => j.writer)).size : 0
+ // From the own diary's joins and marks: Geral's root need not be here
+ // for the slots the account already spent in it.
+ const G = c.built.general
+ const general = G ? walkInfo(slotWalk(groupJoinHistory(base.ownEntries, G), groupCutHistory(base.ownEntries, G), G)) : PENDING_WALK
return {
community: C,
name: c.built.name,
@@ -5748,8 +5853,10 @@ async function communityInfo (ctx, { community }) {
canWrite: countedLive(c, ownHex),
rootHere: c.own !== null && c.own.rootNonce !== null,
authorityHere: c.own !== null && c.built.authorityWriters instanceof Set && c.built.authorityWriters.has(c.own.key),
- writersBound: diaryBound(c, ownHex),
- generalWritersBound: generalBound
+ ...walkInfo(diaryWalk(c, ownHex)),
+ generalWritersBound: general.writersBound,
+ generalWritersFrozen: general.writersFrozen,
+ generalFrozenRoom: general.frozenRoom
}
}, (p) => ({ community: p.group, ...PENDING_COMMUNITY, writerHere: p.own !== null }))
}
@@ -5832,10 +5939,10 @@ async function communityGroups (ctx, { community }) {
// ---- device upkeep (docs/rpc.md 5.61 and 5.63, browser node only) ----
-// The automatic enrolment of a second device stops while the diary binds
-// MAX_GROUP_WRITERS_PER_DIARY - AUTO_ENROL_RESERVE writers in a root: the
-// seeder never releases a bound slot, so the last two are spent only by an
-// explicit tap (owner decision).
+// The automatic enrolment of a second device stops while the diary takes
+// MAX_GROUP_WRITERS_PER_DIARY - AUTO_ENROL_RESERVE live slots in a root: a
+// slot comes back only from a cut, while the frozen reserve lasts (#350),
+// so the last two are spent only by an explicit tap (owner decision).
const AUTO_ENROL_RESERVE = 2
function autoEnrolRoom (fold, ownHex) {
@@ -6487,6 +6594,7 @@ module.exports = {
moderation,
memberBlocks,
readerModeration,
+ adminCuts,
idCreate,
idShow,
identityState,
diff --git a/packages/web-member/lib/sync.js b/packages/web-member/lib/sync.js
index 4f0e9e3c..f2848713 100644
--- a/packages/web-member/lib/sync.js
+++ b/packages/web-member/lib/sync.js
@@ -259,7 +259,7 @@
// is the fold's business. A stored group core is purged only
// when D or its group is blocked, D's issuer is revoked, on a
// fork, or when its local block 0 differs. At most
-// MAX_GROUP_WRITERS_PER_DIARY cores per (diary, group) are
+// MAX_GROUP_HELD_PER_DIARY cores per (diary, group) are
// gated, the rest fail 'quota' (never purged); a gate refused
// on the page item alone is tried again for another listing
// of the key. Gates take
@@ -342,7 +342,7 @@ const { attachAdmission, MAX_INVENTORY } = require('@varanda/core/lib/admission'
const { createBlocklistState, compareSnapshots } = require('@varanda/core/lib/blocklist')
const { attachBlocklist } = require('@varanda/core/lib/blocklist-net')
const { attachGroups, GROUPS_LIMIT_MAX } = require('@varanda/core/lib/groups-net')
-const { validateGroupHeader, validateWriterHeader, validateGroupRecord, groupJoinHistory, groupConfirmHistory, groupJoins, buildCommunity, diaryGroupKeys, MAX_GROUP_WRITERS, MAX_GROUP_WRITERS_PER_DIARY, MAX_GROUPS_PER_COMMUNITY } = require('@varanda/core/lib/group')
+const { validateGroupHeader, validateWriterHeader, validateGroupRecord, groupJoinHistory, groupConfirmHistory, groupCutHistory, groupJoins, buildCommunity, diaryGroupKeys, slotWalk, MAX_GROUP_WRITERS, MAX_GROUP_WRITERS_PER_DIARY, MAX_FROZEN_WRITERS_PER_DIARY, MAX_WRITER_RELEASES_PER_DIARY, MAX_GROUPS_PER_COMMUNITY } = require('@varanda/core/lib/group')
const {
presentState,
presentLength,
@@ -487,8 +487,15 @@ const DIARY_UNAVAILABLE = 'diário indisponível'
function noop () {}
-// The per-diary writer cap, as @varanda/core lib/group.js capPerDiary keeps
-// the MAX_GROUP_WRITERS_PER_DIARY EARLIEST joins, not the first stored:
+// The cores of one (group, diary) the fold may await, as the seeder holds
+// them (#350): the slot walk of the diary (@varanda/core lib/group.js
+// slotWalk) counts 8 live, 8 frozen and 16 released writers at most, so a
+// frozen or released writer, which may hold a leave, is still fetched.
+const MAX_GROUP_HELD_PER_DIARY = MAX_GROUP_WRITERS_PER_DIARY + MAX_FROZEN_WRITERS_PER_DIARY + MAX_WRITER_RELEASES_PER_DIARY
+
+// The per-diary writer cap keeps the MAX_GROUP_HELD_PER_DIARY EARLIEST
+// joins, not the first stored: the walk gives slots in join order, and only
+// the diary itself can bind a writer past its walk, never a seeder.
// held = the live accepted cores of (group, diary) besides the candidate,
// each { key, group, seq, proven }; seq = the candidate's proven join seq.
// -> { full, victim }: when full, the held writer with the highest PROVEN
@@ -497,7 +504,7 @@ function noop () {}
// seeder listing later writers first to push out the one holding a leave
// or moderation.
function capVictim (held, seq) {
- if (held.length < MAX_GROUP_WRITERS_PER_DIARY) return { full: false, victim: null }
+ if (held.length < MAX_GROUP_HELD_PER_DIARY) return { full: false, victim: null }
let victim = null
for (const x of held) {
if (!x.proven || x.key === x.group || !(x.seq > seq)) continue
@@ -541,7 +548,7 @@ function capExcess (live) {
}
for (const list of byPair.values()) {
const { group, diary: d } = list[0]
- const room = MAX_GROUP_WRITERS_PER_DIARY - (live.some((r) => r.key === group && r.diary === d) ? 1 : 0)
+ const room = MAX_GROUP_HELD_PER_DIARY - (live.some((r) => r.key === group && r.diary === d) ? 1 : 0)
if (list.length <= room) continue
list.sort((x, y) => (y.proven - x.proven) || (x.seq - y.seq) || (x.key < y.key ? -1 : 1))
for (const x of list.slice(room)) excess.add(x)
@@ -617,6 +624,46 @@ function ownGroupVerdict (length, pageLength, peerLength) {
// Listings of a full (group, diary) gated at once as contenders.
const MAX_CAP_CONTENDERS = 2
+// The own diary's records the scan keeps for diaryGroupKeys and
+// liveDiaryWriters: the slot walk reads the cuts with the joins (#350).
+const WALK_TYPES = new Set(['group-join', 'group-cut'])
+
+// The on:true writers the own diary binds now to the roots it binds, as
+// diaryGroupKeys counts them: the root's own key once bound, then the live
+// and frozen writers of the diary's slot walk (#350). `entries` the diary's
+// [{ seq, record }]. -> Map writer -> group.
+function liveDiaryWriters (entries, roots) {
+ const allowed = new Set()
+ for (const group of roots) {
+ const walk = slotWalk(groupJoinHistory(entries, group), groupCutHistory(entries, group), group)
+ if (walk.rootSlot) allowed.add(group + ':' + group)
+ for (const writer of [...walk.live, ...walk.frozen]) allowed.add(group + ':' + writer)
+ }
+ const out = new Map()
+ for (const j of groupJoins(entries).values()) if (allowed.has(j.group + ':' + j.writer)) out.set(j.writer, j.group)
+ return out
+}
+
+// The groups community C's held cores fold to, in causal order (at most
+// MAX_GROUPS_PER_COMMUNITY), or null while the fold is unbound or
+// incomplete. The community fold reads the diaries' cuts, like the group
+// fold (#350); a creator's confirmed leave bounds the root's creates.
+// `writers` [{ key, entries, diary }], `diaries` Map diary -> entries.
+function foldedCatalogue (c, rootEntries, writers, diaries) {
+ const joins = new Map()
+ const marks = new Map()
+ const confirms = new Map()
+ for (const [d, entries] of diaries) {
+ joins.set(d, groupJoinHistory(entries, c))
+ marks.set(d, groupCutHistory(entries, c))
+ confirms.set(d, groupConfirmHistory(entries, c))
+ }
+ const built = buildCommunity({ root: { key: c, entries: rootEntries }, writers, joins, marks, confirms, now: Date.now() })
+ if (built.creator === null || built.incomplete) return null
+ const keys = built.created instanceof Map ? [...built.created.keys()] : built.groups
+ return keys.slice(0, MAX_GROUPS_PER_COMMUNITY)
+}
+
const NO_READER = { set: noop, clear: noop }
// A startup that has not reached the seeder link ('ready') by then logs the
// step it is on, and again at the same interval (local diagnostics only).
@@ -3857,7 +3904,7 @@ async function syncRun ({
live.push(s)
}
// Kept victims of an earlier run come back from the storage: the cap
- // holds across runs only if at most MAX_GROUP_WRITERS_PER_DIARY of a
+ // holds across runs only if at most MAX_GROUP_HELD_PER_DIARY of a
// (group, diary) go live, the earliest proven joins (a bound root is one
// of them), the rest at rest.
const excess = capExcess(live)
@@ -4518,22 +4565,15 @@ async function syncRun ({
if (ending) return []
ws.push({ key: w.key, entries: await store.readBlocks(w.core), diary: w.diary })
}
- const joins = new Map()
- // A creator's confirmed leave bounds the root's creates.
- const confirms = new Map()
+ const diaries = new Map()
for (const d of new Set([root.diary, ...writers.map((w) => w.diary)])) {
if (ending) return []
const entries = await entriesOf(d)
- if (!entries) continue
- joins.set(d, groupJoinHistory(entries, c))
- confirms.set(d, groupConfirmHistory(entries, c))
+ if (entries) diaries.set(d, entries)
}
if (ending) return []
- const built = buildCommunity({ root: { key: c, entries: await store.readBlocks(root.core) }, writers: ws, joins, confirms, now: Date.now() })
- if (built.creator !== null && !built.incomplete) {
- const keys = built.created instanceof Map ? [...built.created.keys()] : built.groups
- return keys.slice(0, MAX_GROUPS_PER_COMMUNITY)
- }
+ const keys = foldedCatalogue(c, await store.readBlocks(root.core), ws, diaries)
+ if (keys !== null) return keys
} catch {}
if (ending) return []
return catalogueKeys(c, root, 2 * MAX_GROUPS_PER_COMMUNITY)
@@ -5324,7 +5364,7 @@ async function syncRun ({
const record = validRecordCopy(decodeBlock(await diary.get(seq, { wait: false, raw: true })), seq)
fold.holes.delete(seq)
fold.readFails.delete(seq)
- if (record !== null && record.type === 'group-join') fold.joinEntries.push({ seq, record })
+ if (record !== null && WALK_TYPES.has(record.type)) fold.joinEntries.push({ seq, record })
} catch {
// Once per scan: a diary of failing blocks must not flood the log.
if (!ended && !fold.scanLogged) {
@@ -5370,35 +5410,6 @@ async function syncRun ({
scheduleFold()
}
- // The on:true writers the own diary binds now to the roots it binds, as
- // diaryGroupKeys counts them: the root's own key always, then the earliest
- // MAX_GROUP_WRITERS_PER_DIARY by first join seq. -> Map writer -> group.
- function liveDiaryWriters (entries, roots) {
- const first = new Map()
- for (const { seq, record } of entries) {
- if (record.type !== 'group-join' || record.on !== true || !roots.has(record.group)) continue
- let writers = first.get(record.group)
- if (writers === undefined) first.set(record.group, (writers = new Map()))
- if (!writers.has(record.writer) || seq < writers.get(record.writer)) writers.set(record.writer, seq)
- }
- const allowed = new Set()
- for (const [group, writers] of first) {
- let room = MAX_GROUP_WRITERS_PER_DIARY
- if (writers.has(group)) {
- allowed.add(group + ':' + group)
- room--
- }
- for (const [writer] of [...writers].sort((a, b) => a[1] - b[1] || (a[0] < b[0] ? -1 : 1))) {
- if (writer === group) continue
- if (room-- <= 0) break
- allowed.add(group + ':' + writer)
- }
- }
- const out = new Map()
- for (const j of groupJoins(entries).values()) if (allowed.has(j.group + ':' + j.writer)) out.set(j.writer, j.group)
- return out
- }
-
// Pages `group` for its new diary-bound writer, and again on a short
// backoff (NEW_WRITER_PAGE_DELAYS, each wait after the previous page)
// while the seeder, which may not have admitted it yet, does not list it;
@@ -5546,7 +5557,7 @@ async function syncRun ({
if (entry.seq < 1 || entry.seq >= state.present || entry.record === null) continue
read.add(entry.seq)
const record = validRecordCopy(entry.record, entry.seq)
- if (record !== null && record.type === 'group-join') fold.joinEntries.push({ seq: entry.seq, record })
+ if (record !== null && WALK_TYPES.has(record.type)) fold.joinEntries.push({ seq: entry.seq, record })
}
// A block below the present length that is not here is read again when
// it arrives: a hole a tombstone explains once it leaves the deleted set
@@ -5889,4 +5900,4 @@ async function syncRun ({
}
}
-module.exports = { runSync, retryWait, restartWait, groupVerdict, boxMismatch, addJoins, earliestJoin, groupPushHeld, ownGroupVerdict, bindingRecheck, forgetListed, evictionPurges, capVictim, capExcess, storedJoinProof, refineJoinSeq, gateVerdict, trackPresent, linearExcept, MAX_RANGES, NEWEST_BLOCKS, TAIL_MAX, HEAD_LOW, HEAD_STALL_MS, GATE_TIMEOUT_MS, CONNECT_TIMEOUT_MS, RETRY_FAST_MAX_MS, RETRY_FAST_WINDOW_MS, RETRY_JITTER, RESTART_WAIT_MS, RESTART_JITTER_MS, MAX_PARALLEL_GATES, MAX_PARALLEL_OPENS, SEEDER_UNAVAILABLE, DIARY_UNAVAILABLE }
+module.exports = { runSync, retryWait, restartWait, groupVerdict, boxMismatch, addJoins, earliestJoin, groupPushHeld, ownGroupVerdict, bindingRecheck, forgetListed, evictionPurges, capVictim, capExcess, storedJoinProof, refineJoinSeq, MAX_GROUP_HELD_PER_DIARY, WALK_TYPES, liveDiaryWriters, foldedCatalogue, gateVerdict, trackPresent, linearExcept, MAX_RANGES, NEWEST_BLOCKS, TAIL_MAX, HEAD_LOW, HEAD_STALL_MS, GATE_TIMEOUT_MS, CONNECT_TIMEOUT_MS, RETRY_FAST_MAX_MS, RETRY_FAST_WINDOW_MS, RETRY_JITTER, RESTART_WAIT_MS, RESTART_JITTER_MS, MAX_PARALLEL_GATES, MAX_PARALLEL_OPENS, SEEDER_UNAVAILABLE, DIARY_UNAVAILABLE }
diff --git a/packages/web-member/test/community-ops.test.js b/packages/web-member/test/community-ops.test.js
index f074449f..15b9bd9c 100644
--- a/packages/web-member/test/community-ops.test.js
+++ b/packages/web-member/test/community-ops.test.js
@@ -130,9 +130,9 @@ test('community create writes the root and Geral; info shapes and refusals', asy
const geralRow = { group: general, name: 'Geral', visibility: 'public', join: 'open', role: 'creator', general: true, last: null, unread: 0 }
t.alike(await ops.communityInfo(a.ctx, { community }), {
- community, name: 'Bairro', join: 'open', creator: a.key, role: 'creator', state: 'ready', members: 1, admins: 1, groups: [geralRow], general, incomplete: false, leavePending: false, repair: false, dropped: [], memberInvite: false, writerHere: true, canWrite: true, rootHere: true, authorityHere: true, writersBound: 1, generalWritersBound: 1
+ community, name: 'Bairro', join: 'open', creator: a.key, role: 'creator', state: 'ready', members: 1, admins: 1, groups: [geralRow], general, incomplete: false, leavePending: false, repair: false, dropped: [], memberInvite: false, writerHere: true, canWrite: true, rootHere: true, authorityHere: true, writersBound: 1, writersFrozen: 0, frozenRoom: 8, generalWritersBound: 1, generalWritersFrozen: 0, generalFrozenRoom: 8
})
- t.alike(await ops.groupInfo(a.ctx, { group: general }), { group: general, name: 'Geral', join: 'open', creator: a.key, role: 'creator', members: 1, state: 'member', community, orphan: false, visibility: 'public', memberInvite: false, writerHere: true, canWrite: true, writersBound: 1 })
+ t.alike(await ops.groupInfo(a.ctx, { group: general }), { group: general, name: 'Geral', join: 'open', creator: a.key, role: 'creator', members: 1, state: 'member', community, orphan: false, visibility: 'public', memberInvite: false, writerHere: true, canWrite: true, writersBound: 1, writersFrozen: 0, frozenRoom: 8 })
t.alike(await ops.groupList(a.ctx), { groups: [{ group: general, name: 'Geral', join: 'open', role: 'creator', members: 1, community }], total: 1 }, 'a community is not listed as a group')
const { group: horta } = await ops.groupCreate(a.ctx, { name: 'Horta', join: 'approval', community })
@@ -158,7 +158,7 @@ test('community create writes the root and Geral; info shapes and refusals', asy
await t.exception(() => ops.groupCreate(b.ctx, { name: 'Feira', join: 'open', community }), /só quem criou a comunidade cria grupos/)
await t.exception(() => ops.groupJoin(b.ctx, { group: general }), /entre na comunidade primeiro/)
const pending = 'cd'.repeat(32)
- t.alike(await ops.communityInfo(b.ctx, { community: pending }), { community: pending, name: null, join: null, creator: null, role: 'pending', state: 'pending', members: 0, admins: 0, groups: [], general: null, incomplete: true, leavePending: false, repair: false, dropped: [], memberInvite: false, writerHere: false, canWrite: false, rootHere: false, authorityHere: false, writersBound: 0, generalWritersBound: 0 })
+ t.alike(await ops.communityInfo(b.ctx, { community: pending }), { community: pending, name: null, join: null, creator: null, role: 'pending', state: 'pending', members: 0, admins: 0, groups: [], general: null, incomplete: true, leavePending: false, repair: false, dropped: [], memberInvite: false, writerHere: false, canWrite: false, rootHere: false, authorityHere: false, writersBound: 0, writersFrozen: 0, frozenRoom: 8, generalWritersBound: 0, generalWritersFrozen: 0, generalFrozenRoom: 8 })
await t.exception(() => ops.communityInfo(b.ctx, { community: general }), /comunidade não encontrada/, 'a group root is no community')
})
@@ -1717,3 +1717,140 @@ test('invitePolicy: the creator toggles C, an admin sets C and a group through i
t.alike([cFlag.type, cFlag.group, cFlag.on], ['group-invites', null, true], 'an admin on its own CW')
t.is((await ops.communityInfo(c.ctx, { community })).memberInvite, true)
})
+
+// Appends diary records of `s` with no other check.
+async function diaryAppend (s, list) {
+ const diary = await store.openDiary(s.storage, s.ctx.keyPair)
+ try {
+ for (const r of list) await store.appendRecord(diary, r)
+ } finally {
+ await diary.close()
+ }
+}
+
+test('community.join counts the own diary\'s slot walk, not the CWs held: COMMUNITY_FULL with nothing written (#350)', async (t) => {
+ const { members: [b], community } = await world(t, 1)
+ // Eight CWs of other devices this node never held take every live slot.
+ await diaryAppend(b, Array.from({ length: grp.MAX_GROUP_WRITERS_PER_DIARY }, () => groupJoinRecord({ group: community, writer: hex(crypto.randomBytes(32)), on: true })))
+ const before = (await ops.logRead(b.ctx)).entries.length
+ await t.exception(() => ops.communityJoin(b.ctx, { community }), /limite da comunidade atingido/)
+ t.is((await ops.logRead(b.ctx)).entries.length, before, 'no CW bound')
+ t.is(await ownWriter(b, community), null, 'no CW made')
+})
+
+test('community.join takes a slot a cut freed; community.info counts live and frozen slots, Geral\'s too (#350)', async (t) => {
+ const { members: [b], community, general } = await world(t, 1)
+ const ws = Array.from({ length: grp.MAX_GROUP_WRITERS_PER_DIARY }, () => hex(crypto.randomBytes(32)))
+ const gw = hex(crypto.randomBytes(32))
+ await diaryAppend(b, [
+ ...ws.map((writer) => groupJoinRecord({ group: community, writer, on: true })),
+ groupJoinRecord({ group: general, writer: gw, on: true }),
+ // A cut at 0 releases a writer, a cut past 0 freezes one.
+ groupCutRecord({ group: community, writer: ws[0], cut: 0 }),
+ groupCutRecord({ group: community, writer: ws[1], cut: 2 }),
+ groupCutRecord({ group: general, writer: gw, cut: 1 })
+ ])
+ t.is((await ops.communityJoin(b.ctx, { community })).state, 'member')
+ const info = await ops.communityInfo(b.ctx, { community })
+ t.alike([info.writersBound, info.writersFrozen, info.frozenRoom], [grp.MAX_GROUP_WRITERS_PER_DIARY - 1, 1, grp.MAX_FROZEN_WRITERS_PER_DIARY - 1], 'six old CWs and the new one live, one frozen')
+ t.alike([info.generalWritersBound, info.generalWritersFrozen, info.generalFrozenRoom], [1, 1, grp.MAX_FROZEN_WRITERS_PER_DIARY - 1], 'Geral: the new writer live, the old one frozen')
+ const gi = await ops.groupInfo(b.ctx, { group: general })
+ t.alike([gi.writersBound, gi.writersFrozen, gi.frozenRoom], [info.generalWritersBound, info.generalWritersFrozen, info.generalFrozenRoom], 'group.info of Geral agrees')
+})
+
+// A CW of another device of `s`'s diary, bound by it: its header, then the
+// records `make(key)` returns.
+async function siblingCw (s, community, make = () => []) {
+ const kp = await s.ctx.groupKeyPair(new Uint8Array(crypto.randomBytes(32)))
+ const key = hex(kp.publicKey)
+ const core = await store.openGroupWriter(s.storage, kp)
+ try {
+ for (const record of [grp.writerHeader({ group: community, diary: s.key, member: null }), ...make(key)]) {
+ await store.appendGroupRecord(core, record, { root: false, community: true })
+ }
+ } finally { await core.close() }
+ await store.addGroupReplica(s.storage, key)
+ await diaryAppend(s, [groupJoinRecord({ group: community, writer: key, on: true })])
+ return key
+}
+
+async function ownWalk (s, group) {
+ const entries = (await ops.logRead(s.ctx)).entries
+ return { entries, walk: grp.slotWalk(grp.groupJoinHistory(entries, group), grp.groupCutHistory(entries, group), group) }
+}
+
+const recordAt = (entries, type, writer, on) => entries.findIndex((e) => e.record && e.record.type === type && e.record.writer === writer && (on === undefined || e.record.on === on))
+
+test('a community rejoin after a leave cuts the retired CW at its length before the new join, and takes its slot (#350)', async (t) => {
+ const { members: [b], community } = await world(t, 1)
+ await ops.communityJoin(b.ctx, { community })
+ const old = await ownWriter(b, community)
+ // Seven CWs of other devices of the diary: every live slot is taken.
+ for (let i = 0; i < grp.MAX_GROUP_WRITERS_PER_DIARY - 1; i++) await siblingCw(b, community)
+ t.alike(await ops.communityLeave(b.ctx, { community }), { state: 'left' })
+ const held = await coreLength(b, old)
+ t.ok(held >= 2, 'the old CW holds its header and its leave')
+ t.is((await ops.communityJoin(b.ctx, { community })).state, 'member')
+ const now = await ownWriter(b, community)
+ t.not(now, old, 'a new CW')
+ const { entries, walk } = await ownWalk(b, community)
+ t.alike(grp.groupCutHistory(entries, community).filter((m) => m.writer === old).map((m) => m.cut), [held], 'the old CW is cut once, at its length')
+ t.ok(recordAt(entries, 'group-cut', old) < recordAt(entries, 'group-join', now, true), 'the cut comes before the new join')
+ t.ok(walk.frozen.has(old), 'the old CW is frozen')
+ t.ok(walk.live.has(now), 'the new CW is live')
+})
+
+test('a community rejoin after another device left plans the cut past the leave it writes on the own CW (#350)', async (t) => {
+ const { members: [b], community } = await world(t, 1)
+ // Every release is spent (each join released before the next, so all of
+ // them count), so only a cut past 0 frees a slot: one planned at 0 for the
+ // header-only CW would refuse the join.
+ const released = Array.from({ length: grp.MAX_WRITER_RELEASES_PER_DIARY }, () => hex(crypto.randomBytes(32)))
+ await diaryAppend(b, released.flatMap((writer) => [
+ groupJoinRecord({ group: community, writer, on: true }),
+ groupCutRecord({ group: community, writer, cut: 0 })
+ ]))
+ t.is((await ownWalk(b, community)).walk.released.size, grp.MAX_WRITER_RELEASES_PER_DIARY, 'every release spent')
+ await ops.communityJoin(b.ctx, { community })
+ const old = await ownWriter(b, community)
+ t.is(await coreLength(b, old), 1, 'the own CW holds its header only')
+ const sibs = []
+ for (let i = 0; i < grp.MAX_GROUP_WRITERS_PER_DIARY - 2; i++) sibs.push(await siblingCw(b, community))
+ // The eighth CW, of another device, leaves for the diary.
+ await siblingCw(b, community, (key) => [grp.groupLeave({ cuts: [old, ...sibs, key].map((writer) => ({ writer, seq: 1 })).sort((x, y) => x.writer.localeCompare(y.writer)) })])
+ t.is((await ops.communityJoin(b.ctx, { community })).state, 'member')
+ const now = await ownWriter(b, community)
+ t.alike((await records(b, old)).map((r) => r.type), ['group-writer', 'group-leave'], 'the join wrote a leave on the own CW first')
+ const { entries, walk } = await ownWalk(b, community)
+ t.alike(grp.groupCutHistory(entries, community).filter((m) => m.writer === old).map((m) => m.cut), [2], 'then the cut, past that leave')
+ t.ok(walk.frozen.has(old), 'the old CW is frozen')
+ t.ok(walk.live.has(now), 'the new CW is live')
+})
+
+test('a community demotion past MAX_GROUP_CUTS writers names the first by key and is never refused; a grant is, as in the core node (#350)', (t) => {
+ const cuts = Array.from({ length: grp.MAX_GROUP_CUTS + 1 }, () => ({ writer: hex(crypto.randomBytes(32)), seq: 3 })).sort((x, y) => x.writer.localeCompare(y.writer))
+ t.alike(ops.adminCuts(cuts, false), cuts.slice(0, grp.MAX_GROUP_CUTS), 'the first writers by key keep their length, the rest are cut at 0')
+ t.exception(() => ops.adminCuts(cuts, true), /limite da comunidade atingido/, 'a grant past the cap is refused')
+ const fit = cuts.slice(0, grp.MAX_GROUP_CUTS)
+ t.alike(ops.adminCuts(fit, true), fit, 'a grant within the cap names every writer')
+ t.alike(ops.adminCuts(fit, false), fit, 'a demotion within the cap names every writer')
+})
+
+test('with no frozen room left, a community rejoin after a leave gets COMMUNITY_FULL with nothing written (#350)', async (t) => {
+ const { members: [b], community } = await world(t, 1)
+ // Eight CWs of other devices this node never held, cut past 0: the frozen
+ // reserve is spent.
+ const frozen = Array.from({ length: grp.MAX_FROZEN_WRITERS_PER_DIARY }, () => hex(crypto.randomBytes(32)))
+ await diaryAppend(b, [
+ ...frozen.map((writer) => groupJoinRecord({ group: community, writer, on: true })),
+ ...frozen.map((writer) => groupCutRecord({ group: community, writer, cut: 1 }))
+ ])
+ await ops.communityJoin(b.ctx, { community })
+ for (let i = 0; i < grp.MAX_GROUP_WRITERS_PER_DIARY - 1; i++) await siblingCw(b, community)
+ await ops.communityLeave(b.ctx, { community })
+ const before = (await ops.logRead(b.ctx)).entries.length
+ const old = await ownWriter(b, community)
+ await t.exception(() => ops.communityJoin(b.ctx, { community }), /limite da comunidade atingido/)
+ t.is((await ops.logRead(b.ctx)).entries.length, before, 'nothing written')
+ t.is(await ownWriter(b, community), old, 'no new CW')
+})
diff --git a/packages/web-member/test/device-upkeep-ops.test.js b/packages/web-member/test/device-upkeep-ops.test.js
index f3159b17..4bd25470 100644
--- a/packages/web-member/test/device-upkeep-ops.test.js
+++ b/packages/web-member/test/device-upkeep-ops.test.js
@@ -246,6 +246,23 @@ test('reserve: the diary binding 6 of the 8 writers skips the automatic path, an
t.is((await ops.communityInfo(d.ctx, { community })).writersBound, 7)
})
+test('a cut that freezes a writer gives its live slot back to the automatic enrolment (#350)', async (t) => {
+ const { a, community, general } = await world(t)
+ // The creator's root writer and five more devices: six bound, one frozen.
+ const extra = []
+ for (let i = 0; i < 5; i++) {
+ extra.push(await rawDevice(a, community, [grp.writerHeader({ group: community, diary: a.key, member: null })], true))
+ }
+ const diary = await store.openDiary(a.storage, a.ctx.keyPair)
+ try { await store.appendRecord(diary, groupCutRecord({ group: community, writer: extra[0], cut: 1 })) } finally { await diary.close() }
+ const d = await secondDevice(t, a, [community, general, ...extra])
+ const info = await ops.communityInfo(d.ctx, { community })
+ t.alike([info.writersBound, info.writersFrozen], [5, 1], 'five live slots, one frozen')
+ const plan = await ops.deviceUpkeepPlan(d.ctx)
+ t.alike(plan.enrol.map((x) => x.root), [community, general], 'below the reserve')
+ t.alike(reasons(plan), [])
+})
+
test('a left, blocklisted or unheld root is never enrolled', async (t) => {
const { a, members: [b], community, general } = await world(t, 1)
await ops.communityJoin(b.ctx, { community })
@@ -804,7 +821,9 @@ test('a boxless private writer is not reboxed under a community writer the own d
const { a, b, community, group } = await boxlessJoin(t)
await copyCore(a, b, group)
await ops.deviceHello(b.ctx, { name: 'Celular' })
- for (let i = 0; i < 5; i++) await rawDevice(b, community, [grp.writerHeader({ group: community, diary: b.key, member: null })], true)
+ // Six more CWs: once the cut freezes this one, six live slots are still
+ // taken, so the reserve keeps the automatic path from a new CW (#350).
+ for (let i = 0; i < 6; i++) await rawDevice(b, community, [grp.writerHeader({ group: community, diary: b.key, member: null })], true)
const before = await ops.deviceUpkeepPlan(b.ctx)
t.alike(before.enrol.map((x) => [x.root, x.rebox]), [[group, true]], 'reboxed while the CW is uncut')
diff --git a/packages/web-member/test/group-ops.test.js b/packages/web-member/test/group-ops.test.js
index 57e9e7c8..5611e176 100644
--- a/packages/web-member/test/group-ops.test.js
+++ b/packages/web-member/test/group-ops.test.js
@@ -12,11 +12,15 @@ const { createCert, createModeratorCert, generateAssociationKeyPair } = require(
const { createSnapshot } = require('@varanda/core/lib/blocklist')
const grp = require('@varanda/core/lib/group')
const { MAX_GROUPS_PER_DIARY, MAX_GROUP_CUTS } = grp
-const { groupJoinRecord, validAudioDescriptor } = require('@varanda/core/lib/records')
+const { groupJoinRecord, groupCutRecord, validAudioDescriptor } = require('@varanda/core/lib/records')
const { GROUP_LIMIT_DEFAULT } = require('@varanda/rpc')
const { memoryNodeStorage } = require('../lib/node-storage')
const { groupKeyPairFromPrf } = require('../lib/passkey')
const { createGroupKeys } = require('../lib/group-keys')
+// The core node, for the tests that compare what both nodes write.
+const coreMember = require('@varanda/core/lib/member')
+const coreStore = require('@varanda/core/lib/store')
+const coreWorld = require('../../core/test/helpers/community-member')
const CORE_FILES = ['oplog', 'tree', 'bitfield', 'data', 'header']
@@ -265,7 +269,7 @@ test('create: the root is an own core derived from its nonce, bound by the diary
t.alike({ group: join.group, writer: join.writer, on: join.on }, { group, writer: group, on: true })
const info = await ops.groupInfo(a.ctx, { group })
- t.alike(info, { group, name: 'Horta', join: 'open', creator: a.ownHex, role: 'creator', members: 1, state: 'member', community: groupCommunity.get(group), orphan: false, visibility: 'public', memberInvite: false, writerHere: true, canWrite: true, writersBound: 1 })
+ t.alike(info, { group, name: 'Horta', join: 'open', creator: a.ownHex, role: 'creator', members: 1, state: 'member', community: groupCommunity.get(group), orphan: false, visibility: 'public', memberInvite: false, writerHere: true, canWrite: true, writersBound: 1, writersFrozen: 0, frozenRoom: 8 })
t.alike(await ops.groupList(a.ctx), { groups: [{ group, name: 'Horta', join: 'open', role: 'creator', members: 1, community: groupCommunity.get(group) }], total: 1 })
t.alike(await ops.groupInfo(a.locked, { group }), info, 'reads work locked')
@@ -798,19 +802,93 @@ test('an admin grant cuts the promoted writers at their local length', async (t)
t.alike((await lastRecord(a, group)).cuts, [{ writer: w, seq: 2 }], 'never seq 0')
})
-test('a join refuses a new writer when the own diary has the most writers it can count', async (t) => {
+// The own diary's slot walk for `group`, from the diary as it is now.
+async function ownWalk (m, group) {
+ const { entries } = await ops.logRead(m.ctx)
+ return { entries, walk: grp.slotWalk(grp.groupJoinHistory(entries, group), grp.groupCutHistory(entries, group), group) }
+}
+
+const recordAt = (entries, type, writer, extra = () => true) => entries.findIndex((e) => e.record && e.record.type === type && e.record.writer === writer && extra(e.record))
+
+test('a rejoin at the per-diary cap cuts the retired writer at its length before the new join, and takes its slot (#350)', async (t) => {
const { a, b } = await pair()
const { group } = await ops.groupCreate(a.ctx, { name: 'Horta', join: 'open' })
await shareGroupCore(a, b, group)
await shareDiary(a, b)
await ops.groupJoin(b.ctx, { group })
+ await ops.groupPost(b.ctx, { group, text: 'oi' })
+ const w1 = (await store.readGroupWriters(b.storage))[0].key
for (let i = 1; i < grp.MAX_GROUP_WRITERS_PER_DIARY; i++) {
await otherDevice(b, group, [grp.writerHeader({ group, diary: b.ownHex })])
}
t.alike(await ops.groupLeave(b.ctx, { group }), { state: 'left' })
- const before = (await store.readGroupWriters(b.storage))[0].key
+ const held = (await readGroupCoreEntries(b, w1)).length
+ t.is(held, 3, 'header, post and leave')
+ t.alike(await ops.groupJoin(b.ctx, { group }), { state: 'member' })
+ const w2 = (await store.readGroupWriters(b.storage))[0].key
+ t.not(w2, w1, 'a new writer')
+ const { entries, walk } = await ownWalk(b, group)
+ t.alike(grp.groupCutHistory(entries, group).map((m) => [m.writer, m.cut]), [[w1, held]], 'one cut, at the length this device holds')
+ t.alike(grp.groupJoinHistory(entries, group).filter((j) => j.writer === w1).map((j) => j.on), [true, false], 'the leave already wrote the off join')
+ t.ok(recordAt(entries, 'group-cut', w1) < recordAt(entries, 'group-join', w2), 'the cut comes before the new join')
+ t.ok(walk.frozen.has(w1), 'the retired writer is frozen')
+ t.ok(walk.live.has(w2), 'the new one is live')
+ const info = await ops.groupInfo(b.ctx, { group })
+ t.alike([info.writersBound, info.writersFrozen, info.frozenRoom], [grp.MAX_GROUP_WRITERS_PER_DIARY, 1, grp.MAX_FROZEN_WRITERS_PER_DIARY - 1], 'group.info counts live and frozen slots')
+ t.ok((await ops.groupRead(b.ctx, { group })).items.some((p) => p.text === 'oi'), 'the post below the cut still counts')
+})
+
+test('a leave names the live writers only, never one a diary cut froze (#350)', async (t) => {
+ const { a, b } = await pair()
+ const { group } = await ops.groupCreate(a.ctx, { name: 'Horta', join: 'open' })
+ await shareGroupCore(a, b, group)
+ await shareDiary(a, b)
+ await ops.groupJoin(b.ctx, { group })
+ const w1 = (await store.readGroupWriters(b.storage))[0].key
+ const old = await otherDevice(b, group, [grp.writerHeader({ group, diary: b.ownHex }), grp.groupPost({ text: 'antes' })])
+ const diary = await store.openDiary(b.storage, b.ctx.keyPair)
+ try {
+ await store.appendRecord(diary, groupCutRecord({ group, writer: old, cut: 1 }))
+ } finally {
+ await diary.close()
+ }
+ for (let i = 1; i < grp.MAX_GROUP_WRITERS_PER_DIARY; i++) {
+ await otherDevice(b, group, [grp.writerHeader({ group, diary: b.ownHex })])
+ }
+ t.alike(await ops.groupLeave(b.ctx, { group }), { state: 'left' }, 'eight live writers and a frozen one: the leave fits its cuts')
+ const leave = (await readGroupCoreEntries(b, w1)).pop().record
+ t.is(leave.type, 'group-leave')
+ t.is(leave.cuts.length, grp.MAX_GROUP_WRITERS_PER_DIARY, 'one cut per live writer')
+ t.absent(leave.cuts.some((c) => c.writer === old), 'the frozen writer is fixed by its diary cut')
+})
+
+test('with the frozen reserve spent, a rejoin at the per-diary cap refuses with nothing written (#350)', async (t) => {
+ const { a, b } = await pair()
+ const { group } = await ops.groupCreate(a.ctx, { name: 'Horta', join: 'open' })
+ await shareGroupCore(a, b, group)
+ await shareDiary(a, b)
+ // Eight writers of other devices, all frozen: the frozen reserve is spent.
+ const old = []
+ for (let i = 0; i < grp.MAX_FROZEN_WRITERS_PER_DIARY; i++) old.push(await otherDevice(b, group, [grp.writerHeader({ group, diary: b.ownHex })]))
+ const diary = await store.openDiary(b.storage, b.ctx.keyPair)
+ try {
+ for (const writer of old) await store.appendRecord(diary, groupCutRecord({ group, writer, cut: 1 }))
+ } finally {
+ await diary.close()
+ }
+ await ops.groupJoin(b.ctx, { group })
+ const w1 = (await store.readGroupWriters(b.storage))[0].key
+ for (let i = 1; i < grp.MAX_GROUP_WRITERS_PER_DIARY; i++) {
+ await otherDevice(b, group, [grp.writerHeader({ group, diary: b.ownHex })])
+ }
+ t.alike(await ops.groupLeave(b.ctx, { group }), { state: 'left' })
+ const before = (await ops.logRead(b.ctx)).entries.length
+ t.is((await ops.groupInfo(b.ctx, { group })).frozenRoom, 0, 'no frozen room left')
await t.exception(() => ops.groupJoin(b.ctx, { group }), /limite do grupo atingido/)
- t.is((await store.readGroupWriters(b.storage))[0].key, before, 'no writer created')
+ const { entries, walk } = await ownWalk(b, group)
+ t.is(entries.length, before, 'no cut, no new join')
+ t.is((await store.readGroupWriters(b.storage))[0].key, w1, 'no writer created')
+ t.ok(walk.live.has(w1), 'the retired writer keeps its slot')
})
test('info and list answer left for a diary the fold does not list', async (t) => {
@@ -1155,8 +1233,8 @@ test('a join by key before the root arrives is pending until a sync brings it',
t.is(b.derived.length, 3, 'still the three writers of the first join')
t.is(groupJoins(await diaryRecords(b), group).length, 1, 'no second diary join')
t.alike(await ops.groupList(b.ctx), { groups: [{ group, name: null, join: null, role: 'pending', members: 0, community: null }], total: 1 })
- t.alike(await ops.groupInfo(b.ctx, { group }), { group, name: null, join: null, creator: null, role: 'pending', members: 0, state: 'pending', community: null, orphan: false, visibility: 'public', memberInvite: false, writerHere: true, canWrite: false, writersBound: 0 })
- t.alike(await ops.groupInfo(b.locked, { group }), { group, name: null, join: null, creator: null, role: 'pending', members: 0, state: 'pending', community: null, orphan: false, visibility: 'public', memberInvite: false, writerHere: true, canWrite: false, writersBound: 0 }, 'reads work locked')
+ t.alike(await ops.groupInfo(b.ctx, { group }), { group, name: null, join: null, creator: null, role: 'pending', members: 0, state: 'pending', community: null, orphan: false, visibility: 'public', memberInvite: false, writerHere: true, canWrite: false, writersBound: 0, writersFrozen: 0, frozenRoom: 8 })
+ t.alike(await ops.groupInfo(b.locked, { group }), { group, name: null, join: null, creator: null, role: 'pending', members: 0, state: 'pending', community: null, orphan: false, visibility: 'public', memberInvite: false, writerHere: true, canWrite: false, writersBound: 0, writersFrozen: 0, frozenRoom: 8 }, 'reads work locked')
t.alike(await ops.groupRead(b.ctx, { group }), { items: [], next: null, hidden: 0, incomplete: true, sealed: 0, epoch: null, state: 'ok' })
await rootArrives(a, b, group)
@@ -1819,6 +1897,176 @@ test('forum: a private group refuses topics, polls, votes and the topic list bef
await t.exception(() => ops.groupTopics(a.ctx, { group }), /fóruns e enquetes só em grupo público/, 'no topic list in a private group')
})
+test('a group rejoin after the CW was retired cuts a header-only writer at 0, after its off join (#350)', async (t) => {
+ const { a, b } = await pair()
+ const { group } = await ops.groupCreate(a.ctx, { name: 'Horta', join: 'open' })
+ const community = groupCommunity.get(group)
+ await shareGroupCore(a, b, group)
+ await shareDiary(a, b)
+ t.alike(await ops.groupJoin(b.ctx, { group }), { state: 'member' })
+ const w1 = (await store.readGroupWriters(b.storage))[0].key
+ const cw1 = (await realStore.readGroupWriters(b.storage)).find((e) => e.group === community).key
+ // Another device of Bia leaves the community listing this CW: the fold
+ // retires it, and the group writer naming it.
+ const kp = await groupKeyPairFromPrf(b.prf, new Uint8Array(crypto.randomBytes(32)))
+ const core = await store.openGroupWriter(b.storage, kp)
+ const key = hex(core.key)
+ try {
+ await store.appendGroupRecord(core, grp.writerHeader({ group: community, diary: b.ownHex, member: null }), { root: false, community: true })
+ await store.appendGroupRecord(core, grp.groupLeave({ cuts: [{ writer: cw1, seq: 1 }, { writer: key, seq: 1 }].sort((x, y) => x.writer.localeCompare(y.writer)) }), { root: false, community: true })
+ } finally {
+ await core.close()
+ }
+ await store.addGroupReplica(b.storage, key)
+ const diary = await store.openDiary(b.storage, b.ctx.keyPair)
+ try {
+ await store.appendRecord(diary, groupJoinRecord({ group: community, writer: key, on: true }))
+ } finally {
+ await diary.close()
+ }
+ t.is((await memberOps.communityJoin(b.ctx, { community })).state, 'member')
+ t.is((await readGroupCoreEntries(b, w1)).length, 1, 'the group writer holds its header only')
+ t.alike(await ops.groupJoin(b.ctx, { group }), { state: 'member' })
+ const w2 = (await store.readGroupWriters(b.storage))[0].key
+ t.not(w2, w1, 'a new writer naming the new CW')
+ const { entries, walk } = await ownWalk(b, group)
+ t.alike(grp.groupJoinHistory(entries, group).filter((j) => j.writer === w1).map((j) => j.on), [true, false], 'the off join is still written')
+ t.alike(grp.groupCutHistory(entries, group).map((m) => [m.writer, m.cut]), [[w1, 0]], 'a header with no rejoin is cut at 0')
+ const off = recordAt(entries, 'group-join', w1, (r) => r.on === false)
+ t.ok(off < recordAt(entries, 'group-cut', w1), 'off join, then the cut')
+ t.ok(recordAt(entries, 'group-cut', w1) < recordAt(entries, 'group-join', w2), 'then the new join')
+ t.ok(walk.released.has(w1) && walk.live.has(w2), 'the old writer is released, the new one live')
+})
+
+test('a pending join counts the own diary\'s slot walk: a cut frees a slot, eight live refuse (#350)', async (t) => {
+ const { b, group } = await pendingPair()
+ await enterCommunity(b.ctx, groupCommunity.get(group))
+ const ws = Array.from({ length: grp.MAX_GROUP_WRITERS_PER_DIARY }, () => hex(crypto.randomBytes(32)))
+ const diary = await store.openDiary(b.storage, b.ctx.keyPair)
+ try {
+ for (const writer of ws) await store.appendRecord(diary, groupJoinRecord({ group, writer, on: true }))
+ } finally {
+ await diary.close()
+ }
+ const before = (await ops.logRead(b.ctx)).entries.length
+ await t.exception(() => ops.groupJoin(b.ctx, { group }), /limite do grupo atingido/, 'eight live writers take every slot')
+ t.is((await ops.logRead(b.ctx)).entries.length, before, 'nothing written')
+ const again = await store.openDiary(b.storage, b.ctx.keyPair)
+ try {
+ await store.appendRecord(again, groupCutRecord({ group, writer: ws[0], cut: 0 }))
+ } finally {
+ await again.close()
+ }
+ t.alike(await ops.groupJoin(b.ctx, { group }), { state: 'pending' }, 'the released writer gave its slot back')
+ const { walk } = await ownWalk(b, group)
+ t.ok(walk.live.has((await store.readGroupWriters(b.storage))[0].key), 'the new writer is live')
+})
+
+// A ban of Bia written by the group's creator, whose fold holds two writers
+// of hers: one her diary froze at its length after a post ("antes"), a live
+// one with a post ("vivo"), and one her diary released (cut at 0). The ban's
+// cuts by role and the creator's read of her posts afterwards.
+function frozenBanOutcome (ban, { frozen, live, released }, read) {
+ const role = (w) => w === frozen ? 'frozen' : w === live ? 'live' : w === released ? 'released' : w
+ const cuts = ban.cuts.map((c) => [role(c.writer), c.seq]).sort((x, y) => x[0] < y[0] ? -1 : 1)
+ const posts = read.items.filter((i) => i.text === 'antes' || i.text === 'vivo').map((i) => [i.text, i.hidden === true]).sort()
+ return { cuts, posts }
+}
+
+async function coreFrozenBan (t) {
+ const { a, others: [b], community } = await coreWorld.world(t, { names: ['Bia'] })
+ const { group } = await coreMember.groupCreate(a, { name: 'Horta', join: 'open', community })
+ await coreWorld.sync(a, b)
+ await coreMember.groupJoin(b, { group })
+ await coreMember.groupPost(b, { group, text: 'antes' })
+ const frozen = coreWorld.entryOf(b, group).writer
+ const cw = coreWorld.entryOf(b, community).writer
+ const live = await coreWorld.otherDevice(b, group, hex(crypto.randomBytes(16)), grp.writerHeader({ group, diary: b.key, member: cw }), [() => grp.groupPost({ text: 'vivo' })])
+ const released = await coreWorld.otherDevice(b, group, hex(crypto.randomBytes(16)), grp.writerHeader({ group, diary: b.key, member: cw }))
+ const cut = (await coreWorld.coreEntries(b, frozen)).length
+ await coreMember.withIdentity(b, async ({ diary }) => {
+ await coreStore.appendRecord(diary, groupCutRecord({ group, writer: frozen, cut }))
+ await coreStore.appendRecord(diary, groupCutRecord({ group, writer: released, cut: 0 }))
+ })
+ await coreWorld.sync(a, b)
+ await coreMember.groupBan(a, { group, member: b.key, on: true })
+ const ban = (await coreWorld.coreEntries(a, group)).pop().record
+ return frozenBanOutcome(ban, { frozen, live, released }, await coreMember.groupRead(a, { group }))
+}
+
+async function browserFrozenBan () {
+ const { a, b } = await pair()
+ const { group } = await ops.groupCreate(a.ctx, { name: 'Horta', join: 'open' })
+ await shareGroupCore(a, b, group)
+ await shareDiary(a, b)
+ await ops.groupJoin(b.ctx, { group })
+ await ops.groupPost(b.ctx, { group, text: 'antes' })
+ const frozen = (await store.readGroupWriters(b.storage))[0].key
+ const live = await otherDevice(b, group, [grp.writerHeader({ group, diary: b.ownHex }), grp.groupPost({ text: 'vivo' })])
+ const released = await otherDevice(b, group, [grp.writerHeader({ group, diary: b.ownHex })])
+ const cut = (await readGroupCoreEntries(b, frozen)).length
+ const diary = await store.openDiary(b.storage, b.ctx.keyPair)
+ try {
+ await store.appendRecord(diary, groupCutRecord({ group, writer: frozen, cut }))
+ await store.appendRecord(diary, groupCutRecord({ group, writer: released, cut: 0 }))
+ } finally {
+ await diary.close()
+ }
+ for (const key of [frozen, live, released]) await shareGroupCore(b, a, key)
+ await shareDiary(b, a)
+ await ops.groupBan(a.ctx, { group, member: b.ownHex, on: true })
+ return frozenBanOutcome(await lastRecord(a, group), { frozen, live, released }, await ops.groupRead(a.ctx, { group }))
+}
+
+test('a ban from the core node and from the browser node fold the same for a frozen writer: it is named at its cut, its post below stays (#350)', async (t) => {
+ const core = await coreFrozenBan(t)
+ const browser = await browserFrozenBan()
+ t.alike(browser.cuts, [['frozen', 2], ['live', 2]], 'the browser names the frozen writer at its cut and the live one at its length, never the released one')
+ t.alike(core.cuts, browser.cuts, 'the core node names the same writers at the same seqs')
+ t.alike(browser.posts, [['antes', false], ['vivo', false]], 'both posts written before the ban stay visible')
+ t.alike(core.posts, browser.posts, 'the core node\'s fold gives the same')
+})
+
+test('a rejoin repeats the cut on a retired writer a mark left live, once the walk has room to freeze it (#350)', async (t) => {
+ const { a, b } = await pair()
+ const { group } = await ops.groupCreate(a.ctx, { name: 'Horta', join: 'open' })
+ await shareGroupCore(a, b, group)
+ await shareDiary(a, b)
+ const old = []
+ for (let i = 0; i < grp.MAX_FROZEN_WRITERS_PER_DIARY; i++) old.push(await otherDevice(b, group, [grp.writerHeader({ group, diary: b.ownHex })]))
+ const append = async (records) => {
+ const diary = await store.openDiary(b.storage, b.ctx.keyPair)
+ try {
+ for (const r of records) await store.appendRecord(diary, r)
+ } finally {
+ await diary.close()
+ }
+ }
+ await append(old.map((writer) => groupCutRecord({ group, writer, cut: 1 })))
+ await ops.groupJoin(b.ctx, { group })
+ const w1 = (await store.readGroupWriters(b.storage))[0].key
+ for (let i = 1; i < grp.MAX_GROUP_WRITERS_PER_DIARY; i++) {
+ await otherDevice(b, group, [grp.writerHeader({ group, diary: b.ownHex })])
+ }
+ t.alike(await ops.groupLeave(b.ctx, { group }), { state: 'left' })
+ const held = (await readGroupCoreEntries(b, w1)).length
+ // With a device id, a mark on its own writer does not read as a
+ // disconnect of this device.
+ await ops.deviceHello(b.ctx, { name: 'Celular' })
+ // A mark written while the frozen reserve was spent leaves the writer
+ // live; then a frozen writer is released, so the reserve has room again.
+ await append([groupCutRecord({ group, writer: w1, cut: held }), groupCutRecord({ group, writer: old[0], cut: 0 })])
+ t.ok((await ownWalk(b, group)).walk.live.has(w1), 'the mark alone left the retired writer live')
+ t.alike(await ops.groupJoin(b.ctx, { group }), { state: 'member' })
+ const w2 = (await store.readGroupWriters(b.storage))[0].key
+ t.not(w2, w1, 'a new writer')
+ const { entries, walk } = await ownWalk(b, group)
+ t.alike(grp.groupCutHistory(entries, group).filter((m) => m.writer === w1).map((m) => m.cut), [held, held], 'the cut is written again, at the same seq')
+ t.ok(recordAt(entries, 'group-join', w2) > entries.map((e) => e.record && e.record.type === 'group-cut' && e.record.writer === w1).lastIndexOf(true), 'the new join comes after it')
+ t.ok(walk.frozen.has(w1), 'the walk freezes the retired writer now')
+ t.ok(walk.live.has(w2), 'the new one is live')
+})
+
// Copies the blocks `from`'s own diary holds into `to` as a followed replica:
// a block `from` cleared never reaches `to`.
async function shareHeldDiary (from, to) {
diff --git a/packages/web-member/test/sync-diary-roots.test.js b/packages/web-member/test/sync-diary-roots.test.js
index bf68cdd5..99862f64 100644
--- a/packages/web-member/test/sync-diary-roots.test.js
+++ b/packages/web-member/test/sync-diary-roots.test.js
@@ -12,7 +12,7 @@ const crypto = require('hypercore-crypto')
const Hypercore = require('hypercore')
const { testnet, until } = require('./helpers/net')
const { generateAssociationKeyPair } = require('@varanda/core/lib/cert')
-const { groupJoinRecord, deleteRecord } = require('@varanda/core/lib/records')
+const { groupJoinRecord, groupCutRecord, deleteRecord } = require('@varanda/core/lib/records')
const grp = require('@varanda/core/lib/group')
const { memoryNodeStorage } = require('../lib/node-storage')
const store = require('../lib/node-store')
@@ -201,6 +201,52 @@ test('a catalogue child paged root only is paged in full once the diary joins it
t.alike(await store.readGroupWriters(b.storage), [], 'B created no writer')
})
+test('a root the diary binds through a writer joined after released slots is fetched (#350)', async (t) => {
+ t.timeout(110000)
+ const { bootstrap } = await testnet(t)
+ const assocKp = generateAssociationKeyPair()
+ const { seeder, url } = await seederOverWs(t, assocKp, bootstrap)
+ const a = await member(assocKp, 'Ana')
+ const e = await member(assocKp, 'Edu')
+ t.ok((await once(a, url, [])).ok, 'A pushed its diary')
+ const b = await device(a)
+ t.ok((await once(b, url, [])).ok, 'B fetched the diary')
+ const { community: L } = await ops.communityCreate(e.ctx, { name: 'Largo', join: 'open' })
+ t.ok((await once(e, url, [])).ok, 'E pushed its community')
+ await until(() => indexed(seeder, L, L), { name: 'root indexed' })
+ // Every live slot of A's diary in L taken once, left and released by a
+ // cut 0, then one more writer: only the slot walk read with the cuts
+ // binds L now.
+ const ghost = () => b4a.toString(crypto.keyPair().publicKey, 'hex')
+ const diary = await store.openDiary(a.storage, a.ctx.keyPair)
+ try {
+ for (let i = 0; i < grp.MAX_GROUP_WRITERS_PER_DIARY; i++) {
+ const w = ghost()
+ await store.appendRecord(diary, groupJoinRecord({ group: L, writer: w, on: true }))
+ await store.appendRecord(diary, groupJoinRecord({ group: L, writer: w, on: false }))
+ await store.appendRecord(diary, groupCutRecord({ group: L, writer: w, cut: 0 }))
+ }
+ await store.appendRecord(diary, groupJoinRecord({ group: L, writer: ghost(), on: true }))
+ } finally {
+ await diary.close()
+ }
+ // The writers never reach the seeder, so L's pages stay incomplete and
+ // a once run cannot end: a continuous run, read through its lender.
+ t.ok((await once(a, url, [])).ok, 'A pushed the joins and cuts')
+ const ev = []
+ const ac = new AbortController()
+ const lent = lender()
+ const run = sync(b, url, ev, { signal: ac.signal, reader: lent.reader, groupPageMs: 600000 })
+ try {
+ if (!await waitFor(t, () => (lent.held(L) || { contiguous: 0 }).contiguous > 0, { name: 'B adopted L', timeoutMs: 40000 })) return
+ t.pass('B holds the root its diary binds now')
+ } finally {
+ ac.abort()
+ await run
+ }
+ t.alike(await store.readGroupWriters(b.storage), [], 'B created no writer')
+})
+
test('a diary-bound group another bound root names as its community is still held', async (t) => {
t.timeout(110000)
const { bootstrap } = await testnet(t)
diff --git a/packages/web-member/test/sync-groups-2.test.js b/packages/web-member/test/sync-groups-2.test.js
index 7bf9602b..50986cd8 100644
--- a/packages/web-member/test/sync-groups-2.test.js
+++ b/packages/web-member/test/sync-groups-2.test.js
@@ -5,10 +5,10 @@ const test = require('brittle')
const crypto = require('hypercore-crypto')
const { testnet, until } = require('./helpers/net')
const { createIssuerCert, createIssuedCert, generateAssociationKeyPair } = require('@varanda/core/lib/cert')
-const { groupJoinRecord } = require('@varanda/core/lib/records')
+const { groupJoinRecord, groupCutRecord } = require('@varanda/core/lib/records')
const grp = require('@varanda/core/lib/group')
const { MAX_GROUP_WRITERS, MAX_GROUP_WRITERS_PER_DIARY } = grp
-const { addJoins, earliestJoin, groupPushHeld, ownGroupVerdict, bindingRecheck, forgetListed, evictionPurges, capVictim, capExcess, storedJoinProof, refineJoinSeq } = require('../lib/sync')
+const { addJoins, earliestJoin, groupPushHeld, ownGroupVerdict, bindingRecheck, forgetListed, evictionPurges, capVictim, capExcess, storedJoinProof, refineJoinSeq, MAX_GROUP_HELD_PER_DIARY: HELD, WALK_TYPES, liveDiaryWriters, foldedCatalogue } = require('../lib/sync')
const { memoryNodeStorage } = require('../lib/node-storage')
const store = require('../lib/node-store')
const ops = require('../lib/member-ops')
@@ -201,12 +201,12 @@ test('a once run against a seeder without groups ends without waiting for own gr
test('writers stored in an earlier run prove their join seq from the binding diary, so an earlier writer listed later takes a slot', (t) => {
// Unit test of the run-start derivation plus capVictim (the end-to-end
- // harness needs 9 real writers of one diary through a seeder).
+ // harness needs 33 real writers of one diary through a seeder).
const g = 'aa'.repeat(32)
const at = '2026-01-01T00:00:00.000Z'
const earliest = randomKey()
const later = []
- for (let i = 0; i < MAX_GROUP_WRITERS_PER_DIARY; i++) later.push(randomKey())
+ for (let i = 0; i < HELD; i++) later.push(randomKey())
const entries = [{ seq: 0, record: { type: 'genesis' } }]
entries.push({ seq: 1, record: groupJoinRecord({ group: g, writer: earliest, on: true, at }) })
for (const w of later) entries.push({ seq: entries.length, record: groupJoinRecord({ group: g, writer: w, on: true, at }) })
@@ -225,10 +225,10 @@ test('stored cores beyond the per-diary cap stay at rest, the earliest proven jo
const g = 'aa'.repeat(32)
const d = 'bb'.repeat(32)
const live = [{ key: g, group: g, diary: d, seq: 0, proven: false }]
- for (let i = 0; i < MAX_GROUP_WRITERS_PER_DIARY + 1; i++) live.push({ key: randomKey(), group: g, diary: d, seq: 10 + i, proven: true })
+ for (let i = 0; i < HELD + 1; i++) live.push({ key: randomKey(), group: g, diary: d, seq: 10 + i, proven: true })
live.push({ key: randomKey(), group: g, diary: d, seq: 1, proven: false })
const excess = capExcess(live)
- t.is(excess.size, 3, 'root + 7 writers stay live')
+ t.is(excess.size, 3, 'root + 31 writers stay live')
t.ok(excess.has(live[live.length - 1]), 'an unproven core goes to rest first')
t.ok(excess.has(live[live.length - 2]) && excess.has(live[live.length - 3]), 'then the latest proven joins')
t.absent(excess.has(live[0]), 'never the root')
@@ -239,7 +239,7 @@ test('a later duplicate join never makes an earlier writer lose its slot', (t) =
const g = 'aa'.repeat(32)
const at = '2026-01-01T00:00:00.000Z'
const held = []
- for (let i = 0; i < MAX_GROUP_WRITERS_PER_DIARY; i++) held.push({ key: randomKey(), group: g, seq: 10 + i, proven: true })
+ for (let i = 0; i < HELD; i++) held.push({ key: randomKey(), group: g, seq: 10 + i, proven: true })
const early = held[held.length - 1]
// The seeder named early's duplicate join at 17; its first join is seq 3.
const entries = [{ seq: 3, record: groupJoinRecord({ group: g, writer: early.key, on: true, at }) }]
@@ -476,7 +476,7 @@ test('a cap eviction purges a victim still in regate and keeps a listed one', (t
test('the writer cap keeps the earliest proven joins, never evicting on an unproven seq or a root', (t) => {
const g = 'aa'.repeat(32)
const held = []
- for (let i = 0; i < MAX_GROUP_WRITERS_PER_DIARY; i++) held.push({ key: randomKey(), group: g, seq: 10 + i, proven: true })
+ for (let i = 0; i < HELD; i++) held.push({ key: randomKey(), group: g, seq: 10 + i, proven: true })
t.alike(capVictim(held.slice(1), 5), { full: false, victim: null }, 'room left')
t.is(capVictim(held, 5).victim, held[held.length - 1], 'an earlier join takes the latest one\'s slot')
t.is(capVictim(held, 99).victim, null, 'a later join is refused')
@@ -652,3 +652,70 @@ test('a peek never fetches a seq the binding diary knows deleted, on a diary hel
await replica.close()
}
})
+
+test('the per-diary cap holds every writer the slot walk counts: 8 live, 8 frozen and 16 released (#350)', (t) => {
+ // A frozen or released writer may hold the diary's leave: past the 8 live
+ // slots it is still fetched, as the seeder holds it.
+ t.is(HELD, MAX_GROUP_WRITERS_PER_DIARY + grp.MAX_FROZEN_WRITERS_PER_DIARY + grp.MAX_WRITER_RELEASES_PER_DIARY)
+ t.is(HELD, 32)
+ const g = 'aa'.repeat(32)
+ const d = 'bb'.repeat(32)
+ const held = []
+ for (let i = 0; i < MAX_GROUP_WRITERS_PER_DIARY; i++) held.push({ key: randomKey(), group: g, seq: 10 + i, proven: true })
+ t.alike(capVictim(held, 5), { full: false, victim: null }, '8 held writers leave room')
+ const live = [{ key: g, group: g, diary: d, seq: 0, proven: false }]
+ for (let i = 0; i < 2 * MAX_GROUP_WRITERS_PER_DIARY; i++) live.push({ key: randomKey(), group: g, diary: d, seq: 10 + i, proven: true })
+ t.is(capExcess(live).size, 0, 'a root and 16 writers all stay live')
+})
+
+test('the own diary scan keeps the cuts: a writer joined after released slots binds its root and is live (#350)', (t) => {
+ // The scan keeps WALK_TYPES of the own diary for diaryGroupKeys and
+ // liveDiaryWriters, as readDiaryJoin and seedFold do.
+ const g = 'aa'.repeat(32)
+ const at = '2026-01-01T00:00:00.000Z'
+ const entries = []
+ let seq = 1
+ const scan = (record) => {
+ if (WALK_TYPES.has(record.type)) entries.push({ seq, record })
+ seq++
+ }
+ // Every live slot taken once, then left and released by a cut 0: the
+ // slots are free again, so a later writer takes one.
+ for (let i = 0; i < MAX_GROUP_WRITERS_PER_DIARY; i++) {
+ const w = randomKey()
+ scan(groupJoinRecord({ group: g, writer: w, on: true, at }))
+ scan(groupJoinRecord({ group: g, writer: w, on: false, at }))
+ scan(groupCutRecord({ group: g, writer: w, cut: 0, at }))
+ }
+ const late = randomKey()
+ scan(groupJoinRecord({ group: g, writer: late, on: true, at }))
+ t.alike(grp.diaryGroupKeys(entries), [g], 'the root is adopted for the later writer')
+ t.alike([...liveDiaryWriters(entries, new Set([g]))], [[late, g]], 'the later writer is live, so a new one is chased')
+ const joinsOnly = entries.filter((e) => e.record.type === 'group-join')
+ t.alike(grp.diaryGroupKeys(joinsOnly), [], 'control: without the cuts the walk has no slot for it')
+})
+
+test('the catalogue fold reads the diaries\' cuts, like the group fold: a CW a cut let in waits to be held (#350)', async (t) => {
+ const assocKp = generateAssociationKeyPair()
+ const a = await member(assocKp, 'Ana')
+ const b = await member(assocKp, 'Bia')
+ const { community, group } = await ops.communityCreate(a.ctx, { name: 'Dicas', join: 'open' })
+ const root = await store.openGroupCoreReadOnly(a.storage, community)
+ let rootEntries
+ try { rootEntries = await store.readBlocks(root) } finally { await root.close() }
+ // Bia's diary binds the eight live CWs it may, releases one with a cut at
+ // 0 and binds a ninth in the slot it freed; this node holds the eight.
+ const eight = Array.from({ length: MAX_GROUP_WRITERS_PER_DIARY }, () => randomKey())
+ const ninth = randomKey()
+ const diary = await store.openDiary(b.storage, b.ctx.keyPair)
+ try {
+ for (const writer of eight) await store.appendRecord(diary, groupJoinRecord({ group: community, writer, on: true }))
+ await store.appendRecord(diary, groupCutRecord({ group: community, writer: eight[0], cut: 0 }))
+ await store.appendRecord(diary, groupJoinRecord({ group: community, writer: ninth, on: true }))
+ } finally { await diary.close() }
+ const cw = (key) => ({ key, diary: b.ownHex, entries: [{ seq: 0, record: grp.writerHeader({ group: community, diary: b.ownHex, member: null }) }] })
+ const bia = (await ops.logRead(b.ctx)).entries
+ const diaries = new Map([[a.ownHex, (await ops.logRead(a.ctx)).entries], [b.ownHex, bia]])
+ t.is(foldedCatalogue(community, rootEntries, eight.map(cw), diaries), null, 'the ninth CW counts and is not held: no catalogue from the fold yet')
+ t.alike(foldedCatalogue(community, rootEntries, [...eight, ninth].map(cw), diaries), [group], 'once it is held, the fold gives the catalogue')
+})