fix(tasks): 每请求重读 agentTeams/agents,晚挂载的实验层不再永久 503
buildTeamsApi() 把两个可选服务取成构建期快照;agent-teams 是本插件之后才可能挂载的实验层,快照到 undefined 后 membershipOf() 永远返回 503。改为在 membershipOf() 内逐请求 ctx.get()(与 subagent-live-route 的可选运行时同模式),并补一条晚挂载的回归测试。
This commit is contained in:
1 parent
11e6c8e63a
commit
e70b4e516d
2 files changed
+22
-3
No files matched your search
+6
-3
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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<unknown> } } = {}
|
||||
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 }))
|
||||
|
||||
Reference in new issue
Block a user