Skip to content

The agent that grows with you

refactor(desktop): trim hermes-bots group/relay complexity

Teknium7e653b451443
diff --git a/apps/desktop/src/plugins/hermes-bots/canonical-group-workspace.tsx b/apps/desktop/src/plugins/hermes-bots/canonical-group-workspace.tsx
index 05d3cfe3850..f84c8c4061c 100644
--- a/apps/desktop/src/plugins/hermes-bots/canonical-group-workspace.tsx
+++ b/apps/desktop/src/plugins/hermes-bots/canonical-group-workspace.tsx
@@ -136,6 +136,8 @@ function CanonicalRoomView({ binding: initialBinding, visible, onBack }: {
const act = (action: CanonicalPendingAction, choice?: 'once' | 'deny') =>
mutate(() => actCanonicalGroup(binding, action, choice))
+ const composerLocked = !restored || busy || !!pending
+
return <section className="flex h-full min-h-0 flex-col gap-3 p-3">
<header className="flex items-center gap-2">
{onBack && <Button onClick={onBack}>{labels.back}</Button>}
@@ -161,8 +163,8 @@ function CanonicalRoomView({ binding: initialBinding, visible, onBack }: {
</div>}
{pending && <p role="status">{labels.restoredPendingSend}</p>}
<form className="flex gap-2" onSubmit={event => { event.preventDefault(); send() }}>
- <CanonicalGroupAttachments attachments={attachments} binding={binding} disabled={!restored || busy || !!pending} onChange={setAttachments} />
- <textarea aria-label={labels.groupMessage} className="min-w-0 flex-1" disabled={!restored || busy || !!pending} onChange={e => setDraft(e.target.value)} value={draft} />
+ <CanonicalGroupAttachments attachments={attachments} binding={binding} disabled={composerLocked} onChange={setAttachments} />
+ <textarea aria-label={labels.groupMessage} className="min-w-0 flex-1" disabled={composerLocked} onChange={e => setDraft(e.target.value)} value={draft} />
<Button disabled={!restored || busy || (!pending && !draft.trim() && !attachments.length) || !state?.driver_status} type="submit">{pending ? labels.retry : labels.send}</Button>
</form>
</section>
diff --git a/apps/desktop/src/plugins/hermes-bots/group-rounds.ts b/apps/desktop/src/plugins/hermes-bots/group-rounds.ts
index 6f820430acb..c14a558ea21 100644
--- a/apps/desktop/src/plugins/hermes-bots/group-rounds.ts
+++ b/apps/desktop/src/plugins/hermes-bots/group-rounds.ts
@@ -27,7 +27,7 @@ import {
groupSessionKey,
hasThreadScopedGroupSession
} from './group-membership'
-import { runGroupContinuationMembers, runGroupRoundMember } from './group-round-members'
+import { type GroupRoundMemberContext, runGroupContinuationMembers, runGroupRoundMember } from './group-round-members'
import { rejectGroupSlashCommand } from './group-slash'
import { GROUP_TURN_HARD_CAP_MS, harvestStrandedGroupReply } from './group-turns'
import { botsText } from './i18n'
@@ -613,6 +613,31 @@ export async function stopGroupThread(group: string, thread: null | string, memb
}
}
+/** Deliver any replies that finished after their turn timed out — every
+ * member, not just this round's responders, so long work is late, never
+ * lost. False = the drive must end (cancelled or the room went away). */
+async function harvestGroupRoundMembers(context: GroupRoundMemberContext): Promise<boolean> {
+ for (const member of context.members) {
+ if (!context.isCurrent()) {
+ recordGroupActivity(context.group, {
+ kind: 'cancelled',
+ member: null,
+ thread: context.thread
+ })
+
+ return false
+ }
+
+ await harvestStrandedGroupReply(context.group, member)
+
+ if (!context.binding.isLive()) {
+ return false
+ }
+ }
+
+ return true
+}
+
/** Drive one bounded round-robin turn for ONE THREAD. Serial — one member at
* a time. User follow-ups queue behind this drive; Stop invalidates its
* epoch and discards queued continuations.
@@ -656,25 +681,8 @@ export async function runGroupChatRounds(group: string, members: GroupMember[],
try {
for (let round = 0; round < GROUP_CHAT_MAX_ROUNDS; round++) {
- // Deliver any replies that finished after their turn timed out —
- // every member, not just this round's responders, so long work is
- // late, never lost.
- for (const member of members) {
- if (!isCurrent()) {
- recordGroupActivity(group, {
- kind: 'cancelled',
- member: null,
- thread
- })
-
- return
- }
-
- await harvestStrandedGroupReply(group, member)
-
- if (!binding.isLive()) {
- return
- }
+ if (!(await harvestGroupRoundMembers(context))) {
+ return
}
const roomLog = (($groupChats.get()[group] || {}).log || []).filter(
diff --git a/apps/desktop/src/plugins/hermes-bots/group-test-utils.ts b/apps/desktop/src/plugins/hermes-bots/group-test-utils.ts
index df51e2043fb..5d0bf531e8e 100644
--- a/apps/desktop/src/plugins/hermes-bots/group-test-utils.ts
+++ b/apps/desktop/src/plugins/hermes-bots/group-test-utils.ts
@@ -176,61 +176,66 @@ export function createGroupGateway(options: GatewayOptions = {}): ScriptedGatewa
return stored ? sessions.get(stored) || null : null
}
- const handle = async (method: string, params: Record<string, unknown>): Promise<unknown> => {
- // A legacy Desktop room: no hosted-room driver, nonpersistent owner.
- if (method === 'groups.capabilities') {
- return { driver: false, persistent_process: false }
- }
+ // `profiles.configure`: CAS-checked ui_meta writes, with an optional one-shot conflict.
+ const configureProfiles = (params: Record<string, unknown>) => {
+ if (options.conflictOnce && !conflicted) {
+ conflicted = true
+ const { key, value } = options.conflictOnce
+ uiMeta[key] = value
+ uiMetaRevisions[key] = (uiMetaRevisions[key] || 0) + 1
- if (method === 'profiles.list') {
return {
- profiles: [{ name: 'default', ui_meta: { ...uiMeta }, ui_meta_revisions: { ...uiMetaRevisions } }]
+ applied: {
+ ui_meta: false,
+ ui_meta_conflicts: { [key]: { actual: uiMetaRevisions[key], expected: uiMetaRevisions[key] - 1 } },
+ ui_meta_revisions: { ...uiMetaRevisions }
+ }
}
}
- if (method === 'profiles.configure') {
- if (options.conflictOnce && !conflicted) {
- conflicted = true
- const { key, value } = options.conflictOnce
- uiMeta[key] = value
- uiMetaRevisions[key] = (uiMetaRevisions[key] || 0) + 1
+ const expected = params.ui_meta_expected_revisions as Record<string, number> | undefined
+ const incoming = (params.ui_meta || {}) as Record<string, unknown>
- return {
- applied: {
- ui_meta: false,
- ui_meta_conflicts: { [key]: { actual: uiMetaRevisions[key], expected: uiMetaRevisions[key] - 1 } },
- ui_meta_revisions: { ...uiMetaRevisions }
+ if (expected) {
+ for (const key of Object.keys(incoming)) {
+ if ((uiMetaRevisions[key] || 0) !== expected[key]) {
+ return {
+ applied: {
+ ui_meta: false,
+ ui_meta_conflicts: { [key]: { actual: uiMetaRevisions[key] || 0, expected: expected[key] } }
+ }
}
}
}
+ }
- const expected = params.ui_meta_expected_revisions as Record<string, number> | undefined
- const incoming = (params.ui_meta || {}) as Record<string, unknown>
-
- if (expected) {
- for (const key of Object.keys(incoming)) {
- if ((uiMetaRevisions[key] || 0) !== expected[key]) {
- return {
- applied: {
- ui_meta: false,
- ui_meta_conflicts: { [key]: { actual: uiMetaRevisions[key] || 0, expected: expected[key] } }
- }
- }
- }
- }
+ for (const [key, value] of Object.entries(incoming)) {
+ if (value === null) {
+ delete uiMeta[key]
+ } else {
+ uiMeta[key] = value
}
- for (const [key, value] of Object.entries(incoming)) {
- if (value === null) {
- delete uiMeta[key]
- } else {
- uiMeta[key] = value
- }
+ uiMetaRevisions[key] = (uiMetaRevisions[key] || 0) + 1
+ }
+
+ return { applied: { ui_meta: true, ui_meta_revisions: { ...uiMetaRevisions } } }
+ }
- uiMetaRevisions[key] = (uiMetaRevisions[key] || 0) + 1
+ const handle = async (method: string, params: Record<string, unknown>): Promise<unknown> => {
+ // A legacy Desktop room: no hosted-room driver, nonpersistent owner.
+ if (method === 'groups.capabilities') {
+ return { driver: false, persistent_process: false }
+ }
+
+ if (method === 'profiles.list') {
+ return {
+ profiles: [{ name: 'default', ui_meta: { ...uiMeta }, ui_meta_revisions: { ...uiMetaRevisions } }]
}
+ }
- return { applied: { ui_meta: true, ui_meta_revisions: { ...uiMetaRevisions } } }
+ if (method === 'profiles.configure') {
+ return configureProfiles(params)
}
if (method === 'session.create') {
diff --git a/apps/desktop/src/plugins/hermes-bots/relay.ts b/apps/desktop/src/plugins/hermes-bots/relay.ts
index 12e1d3b0a4f..ff2cb96b081 100644
--- a/apps/desktop/src/plugins/hermes-bots/relay.ts
+++ b/apps/desktop/src/plugins/hermes-bots/relay.ts
@@ -648,6 +648,50 @@ function enqueueRelayDelivery(sender: RelayConnection, envelope: RelayEnvelope,
})
}
+function relayDeliverParams(sender: RelayConnection, envelope: RelayEnvelope, envelopeId: string) {
+ return {
+ id: envelopeId,
+ profile: String(envelope?.target_profile || ''),
+ message: String(envelope?.message || ''),
+ from_profile: String(envelope?.from_profile || ''),
+ from_handle: String(envelope?.from_handle || ''),
+ from_connection: String(sender.id)
+ }
+}
+
+/** Act on a `bot_relay.deliver` answer: only a settled/failed receipt for THIS
+ * envelope posts back; anything else stays retained for recovery. */
+async function settleRelayDelivery(
+ res: { status?: string; delivery_id?: string; admission_id?: string; reply?: string; error?: string; reason?: string },
+ envelopeId: string,
+ attentionKey: string,
+ postReply: (payload: { error?: string; reason?: string; reply?: string }) => Promise<void>
+) {
+ if (res.delivery_id !== envelopeId || !res.admission_id) {
+ noteBotAttention(attentionKey, 'Delivery identity unavailable; retained for recovery')
+
+ return
+ }
+
+ if (res.status !== 'settled' && res.status !== 'failed') {
+ if (res.status === 'ambiguous') {noteBotAttention(attentionKey, 'unknown_execution')}
+
+ return
+ }
+
+ if (res.status === 'failed') {
+ noteBotAttention(attentionKey, res.reason || res.error || 'delivery failed')
+ await postReply({ error: res.error || res.reply || 'delivery failed', reason: res.reason })
+
+ return
+ }
+
+ clearBotAttention(attentionKey)
+ await postReply({
+ reply: String(res?.reply || '')
+ })
+}
+
/** Deliver one claimed envelope on the target connection's own socket and post
* the reply (or the error) back to the sender gateway for its waiter. */
async function deliverRelayEnvelope(
@@ -693,40 +737,11 @@ async function deliverRelayEnvelope(
const res = await host.requestProfile<{ status?: string; delivery_id?: string; admission_id?: string; reply?: string; error?: string; reason?: string }>(
{ ...target.route, profile: String(envelope.target_profile), targetProfile: String(envelope.target_profile) },
'bot_relay.deliver',
- {
- id: envelopeId,
- profile: String(envelope?.target_profile || ''),
- message: String(envelope?.message || ''),
- from_profile: String(envelope?.from_profile || ''),
- from_handle: String(envelope?.from_handle || ''),
- from_connection: String(sender.id)
- },
+ relayDeliverParams(sender, envelope, envelopeId),
RELAY_DELIVER_TIMEOUT_MS
)
- if (res.delivery_id !== envelopeId || !res.admission_id) {
- noteBotAttention(attentionKey, 'Delivery identity unavailable; retained for recovery')
-
- return
- }
-
- if (res.status !== 'settled' && res.status !== 'failed') {
- if (res.status === 'ambiguous') {noteBotAttention(attentionKey, 'unknown_execution')}
-
- return
- }
-
- if (res.status === 'failed') {
- noteBotAttention(attentionKey, res.reason || res.error || 'delivery failed')
- await postReply({ error: res.error || res.reply || 'delivery failed', reason: res.reason })
-
- return
- }
-
- clearBotAttention(attentionKey)
- await postReply({
- reply: String(res?.reply || '')
- })
+ await settleRelayDelivery(res, envelopeId, attentionKey, postReply)
} catch (error: any) {
// #93091: bot_relay.deliver classifies the failed turn and ships the
// typed code in the JSON-RPC error's `data.reason`; forward it into