diff --git a/src/team-routes.ts b/src/team-routes.ts index eb7e34dc..bb492afe 100644 --- a/src/team-routes.ts +++ b/src/team-routes.ts @@ -85,17 +85,20 @@ function errorCodeOf(error: unknown): string | undefined { * @param ctx - host plugin context. */ export function buildTeamsApi(ctx: Context): SidebarTeamsRoutes { - const teams = ctx.get('agentTeams') as SidebarAgentTeamsService | undefined - const agents = ctx.get('agents') as SidebarAgentsService | undefined - /** * The live lead agent of the requested tree plus its membership check. * Throws 503 when the experimental layer is absent and 404 when the root * simply leads no team (a normal page state, but never for a mutation). + * + * Both services are read per request, not captured when the routes are + * built: the experimental layer is a separate plugin and can mount after + * this one, and a captured `undefined` would pin the 503 forever (#1148). */ const membershipOf = async ( payload: unknown, ): Promise<{ svc: SidebarAgentTeamsService; agent: unknown }> => { + const teams = ctx.get('agentTeams') as SidebarAgentTeamsService | undefined + const agents = ctx.get('agents') as SidebarAgentsService | undefined if (teams === undefined) { throw new SidebarError('team-error', 'the agent-teams layer is not mounted in this deployment', 503) } diff --git a/tests/team-routes.spec.ts b/tests/team-routes.spec.ts index 0f70fbcd..80f82cbf 100644 --- a/tests/team-routes.spec.ts +++ b/tests/team-routes.spec.ts @@ -69,6 +69,22 @@ describe('teams.taskCreate route', () => { ) }) + it('re-reads the layer per request so a late mount is picked up (#1148)', async () => { + // The routes are built once at plugin apply; the experimental layer is a + // separate plugin that can mount afterwards. Capturing `ctx.get('agentTeams')` + // at build time pins the 503 forever even though the service is live by the + // time the user submits the form. + const mounted: { teams?: { tryMembership: (agent: unknown) => unknown; createTask: (agent: unknown, req: unknown) => Promise } } = {} + const ctx = { + get: (key: string) => (key === 'agentTeams' ? mounted.teams : key === 'agents' ? agentsWithRoot() : undefined), + } as unknown as Context + const api = buildTeamsApi(ctx) + const created = task({ id: 'task-late' }) + mounted.teams = { tryMembership: vi.fn(() => ({ role: 'lead' })), createTask: vi.fn(async () => created) } + await expect(api.taskCreate({ rootSessionId: 'root', subject: '晚挂载' })) + .resolves.toEqual({ ok: true, value: created }) + }) + it('degrades to 404 when the root agent is not live in this process', async () => { const teams = { tryMembership: vi.fn(() => ({ role: 'lead' })), createTask: vi.fn() } const api = buildTeamsApi(ctxWith(teams, { get: () => undefined }))