Skip to content

refactor: deduplicate agent hydration in classroom page load #354

@YizukiAme

Description

@YizukiAme

Problem

In app/classroom/[id]/page.tsx, the loadClassroom function hydrates server-generated agents twice via two independent code paths:

Path 1 (L55-62): Inline hydration after server fetch

if (stage.generatedAgentConfigs?.length) {
  const { saveGeneratedAgents } = await import('@/lib/orchestration/registry/store');
  const { useSettingsStore } = await import('@/lib/store/settings');
  const agentIds = await saveGeneratedAgents(stage.id, stage.generatedAgentConfigs);
  useSettingsStore.getState().setSelectedAgentIds(agentIds);
}

This writes agents to IndexedDB + Registry and sets selectedAgentIds.

Path 2 (L73-98): General agent restoration (always runs)

const generatedAgentIds = await loadGeneratedAgentsForStage(classroomId);
if (generatedAgentIds.length > 0) {
  useSettingsStore.getState().setAgentMode('auto');
  useSettingsStore.getState().setSelectedAgentIds(generatedAgentIds);
}

This clears all isGenerated agents from the Registry, re-loads them from IndexedDB (the same data Path 1 just wrote), and re-sets selectedAgentIds.

Result

The exact same agents are saved → cleared → re-loaded → re-selected. The operations are:

  • saveGeneratedAgents(): clear IndexedDB + clear Registry + write IndexedDB + add to Registry
  • loadGeneratedAgentsForStage(): clear Registry + read IndexedDB + add to Registry

Net effect is correct, but the clear-then-reload cycle is redundant.

Impact

Functional impact: none. The end state is identical. This is purely a code cleanliness issue — the double operation adds ~50ms of unnecessary IndexedDB I/O on classroom load.

Suggested Fix

Remove the setSelectedAgentIds call from Path 1 (L60), since Path 2 will handle it along with setAgentMode:

// Path 1: just persist to IndexedDB so Path 2 can find them
if (stage.generatedAgentConfigs?.length) {
  const { saveGeneratedAgents } = await import('@/lib/orchestration/registry/store');
  await saveGeneratedAgents(stage.id, stage.generatedAgentConfigs);
}

Context

Discovered during review of #336.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions