π€― LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
π fix(cli): tell a run what to do when its token cannot write the acceptance report (#23607)
AmAzing-35afe8b2773a
diff --git a/apps/cli/src/commands/acceptance/run/report.test.ts b/apps/cli/src/commands/acceptance/run/report.test.tsnew file mode 100644index 00000000000..945c81983c2--- /dev/null+++ b/apps/cli/src/commands/acceptance/run/report.test.ts@@ -0,0 +1,203 @@+import { Command } from 'commander';+import superjson from 'superjson';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';++import { OPERATION_TOKEN_REFUSAL } from '../../../api/operationTokenFallback';+import type * as RefreshModule from '../../../auth/refresh';+import { getValidToken } from '../../../auth/refresh';+import type * as SettingsModule from '../../../settings';+import { log } from '../../../utils/logger';+import { attachPlanCommands } from '../plan';+import { attachAcceptanceRunCommands } from '.';+import { reportRefusalGuidance, reportUpsertAction } from './report';++vi.mock('../../../auth/refresh', async (importOriginal) => ({+ ...(await importOriginal<typeof RefreshModule>()),+ getValidToken: vi.fn(),+}));+vi.mock('../../../settings', async (importOriginal) => ({+ ...(await importOriginal<typeof SettingsModule>()),+ resolveServerUrl: () => 'https://app.test',+}));++const jwt = (payload: Record<string, unknown>) =>+ `e30.${Buffer.from(JSON.stringify(payload)).toString('base64url')}.sig`;++const operationToken = jwt({ operation_id: 'op_run', purpose: 'hetero-operation', sub: 'user-1' });+const storedLogin = (sub: string) =>+ vi.mocked(getValidToken).mockResolvedValue({+ credentials: { accessToken: jwt({ sub }) },+ status: 'ok',+ });++type Answer = (credential: string | null) => Response;++const body = (status: number, payload: unknown) =>+ new Response(JSON.stringify(payload), {+ headers: { 'content-type': 'application/json' },+ status,+ });+const trpcData = (data: unknown) => body(200, { result: { data: superjson.serialize(data) } });+const trpcFailure = (code: string, httpStatus: number, message: string) =>+ body(httpStatus, {+ error: superjson.serialize({ code: -32_001, data: { code, httpStatus }, message }),+ });++const userState = () => trpcData({ settings: { general: { responseLanguage: 'en-US' } } });+const written = () => trpcData({ id: 'vrr_1' });+const runNotFound = () => trpcFailure('NOT_FOUND', 404, 'Verify run not found');++/** `oidcAuth` refusing the run's operation token; a stored login gets `asLogin`. */+const refusingOperationToken =+ (asLogin: () => Response): Answer =>+ (credential) =>+ credential === operationToken+ ? trpcFailure('UNAUTHORIZED', 401, OPERATION_TOKEN_REFUSAL)+ : asLogin();++/**+ * The lambda endpoint, answering each procedure per credential. Returns the+ * credentials each procedure was called with, in order.+ */+const serve = (routes: {+ 'user.getUserState': Answer;+ 'verify.upsertReport': Answer;+}): Record<string, (string | null)[]> => {+ const sent: Record<string, (string | null)[]> = {};+ vi.stubGlobal(+ 'fetch',+ vi.fn(async (url: string | URL | Request, init?: RequestInit) => {+ const path = new URL(String(url)).pathname.replace('/trpc/lambda/', '');+ const credential = new Headers(init?.headers).get('Oidc-Auth');+ (sent[path] ??= []).push(credential);+ const answer = routes[path as keyof typeof routes];+ return answer ? answer(credential) : trpcFailure('NOT_FOUND', 404, `No procedure ${path}`);+ }),+ );+ return sent;+};++const upsert = () => reportUpsertAction({ run: 'vr_missing', summary: 'probe' });++describe('lh acceptance run report upsert with a run operation token', () => {+ const errors: string[] = [];+ const printed: string[] = [];++ beforeEach(() => {+ errors.length = 0;+ printed.length = 0;+ vi.mocked(getValidToken).mockReset();+ vi.stubEnv('LOBEHUB_JWT', operationToken);+ vi.stubEnv('LOBEHUB_OPERATION_ID', 'op_run');+ vi.stubEnv('LOBEHUB_WORKSPACE_ID', 'ws_test');+ vi.spyOn(log, 'error').mockImplementation((line) => void errors.push(line));+ vi.spyOn(log, 'warn').mockImplementation(() => {});+ vi.spyOn(console, 'log').mockImplementation((line) => void printed.push(String(line)));+ vi.spyOn(process, 'exit').mockImplementation(((code?: number) => {+ throw new Error(`process.exit ${code}`);+ }) as typeof process.exit);+ });++ afterEach(() => {+ vi.restoreAllMocks();+ vi.unstubAllEnvs();+ vi.unstubAllGlobals();+ });++ it.each([+ [+ 'on upsertReport, which no operation token reaches',+ { 'user.getUserState': userState, 'verify.upsertReport': refusingOperationToken(written) },+ [operationToken],+ ],+ [+ 'on the language check, without acceptance:evidence',+ { 'user.getUserState': refusingOperationToken(userState), 'verify.upsertReport': written },+ undefined,+ ],+ ])(+ 'prints what the run can do instead and exits 1 when refused %s',+ async (_case, routes, upsertSentWith) => {+ // Another member's login on a shared device must not stand in for this run.+ storedLogin('user-2');+ const sent = serve(routes);++ await expect(upsert()).rejects.toThrow('process.exit 1');++ expect(sent['verify.upsertReport']).toEqual(upsertSentWith);+ expect(errors).toEqual(reportRefusalGuidance('vr_missing'));+ expect(errors.join('\n')).toContain('lh acceptance plan state op_run');+ expect(errors.join('\n')).toContain('report the handoff as blocked');+ expect(printed).toEqual([]);+ },+ );++ it.each([+ [+ 'the round, once the same-account login stood in',+ 'Verify run not found',+ refusingOperationToken(runNotFound),+ ],+ [+ 'an UNAUTHORIZED with another message',+ 'Operation token is bound to another workspace',+ () => trpcFailure('UNAUTHORIZED', 401, 'Operation token is bound to another workspace'),+ ],+ ])('passes any other failure through unchanged: %s', async (_case, message, answer) => {+ storedLogin('user-1');+ serve({ 'user.getUserState': userState, 'verify.upsertReport': answer });++ await expect(upsert()).rejects.toThrow(message);++ expect(process.exit).not.toHaveBeenCalled();+ expect(errors).toEqual([]);+ });++ it('writes the report when the stored login of the same account stands in', async () => {+ storedLogin('user-1');+ const sent = serve({+ 'user.getUserState': userState,+ 'verify.upsertReport': refusingOperationToken(written),+ });++ await expect(upsert()).resolves.toBeUndefined();++ expect(sent['verify.upsertReport']).toEqual([operationToken, jwt({ sub: 'user-1' })]);+ expect(printed.join('\n')).toContain('vrr_1');+ expect(process.exit).not.toHaveBeenCalled();+ expect(errors).toEqual([]);+ });++ it('names only commands and options the CLI parses', async () => {+ const commands = [+ 'plan state op_run',+ 'run ingest <reportDir> --operation op_run',+ 'run result submit --operation op_run --item <checkItemId> --type <type> --file <path>',+ ];+ const guidance = reportRefusalGuidance('vr_missing').join('\n');++ for (const line of commands) {+ expect(guidance).toContain(`lh acceptance ${line}`);++ const acceptance = new Command('acceptance');+ attachAcceptanceRunCommands(acceptance);+ attachPlanCommands(acceptance);+ const args = line.split(' ');+ let leaf = acceptance;+ for (const name of args) {+ leaf.exitOverride().configureOutput({ writeErr: () => {} });+ const sub = leaf.commands.find((command) => command.name() === name);+ if (!sub) break;+ leaf = sub;+ }+ const reached = vi.fn();+ leaf+ .exitOverride()+ .configureOutput({ writeErr: () => {} })+ .action(reached);++ await acceptance.parseAsync(args, { from: 'user' });+ expect(reached, line).toHaveBeenCalledOnce();+ }+ });+});diff --git a/apps/cli/src/commands/acceptance/run/report.ts b/apps/cli/src/commands/acceptance/run/report.tsindex b348a0d91d2..6845132034f 100644--- a/apps/cli/src/commands/acceptance/run/report.ts+++ b/apps/cli/src/commands/acceptance/run/report.ts@@ -2,11 +2,37 @@ import type { Command } from 'commander';import pc from 'picocolors';import { getTrpcClient } from '../../../api/client';+import { isOperationTokenRefusal } from '../../../api/operationTokenFallback';import { outputJson } from '../../../utils/format';+import { log } from '../../../utils/logger';import { assertReportLanguage } from '../language';// ββ report ββ+/**+ * What a run can do once its operation token is refused on `report upsert`.+ *+ * The refusal is deliberate: the report carries the round's verdict, so the+ * round's verifier writes it, and `upsertReport` stays off the operation-token+ * allowlist. A token without `acceptance:evidence` is refused one step earlier,+ * on the language check's `user.getUserState`, and is no closer to writing it.+ * A same-account stored login has already been tried by the time the refusal+ * reaches the command (`operationTokenFallbackLink`), so it is final; a bare+ * 401 only got retried. The commands below take the run's own operation id,+ * which the token is bound to.+ */+export const reportRefusalGuidance = (runId: string): string[] => {+ const operation = process.env.LOBEHUB_OPERATION_ID || '<operationId>';+ return [+ `This run's token cannot write the report of round ${runId}: a round's report and verdict are written by that round's verifier.`,+ ` Check for a plan round: lh acceptance plan state ${operation} (it has one when "confirmed" shows a time).`,+ ' With a plan round, hand in evidence instead:',+ ` lh acceptance run ingest <reportDir> --operation ${operation}`,+ ` lh acceptance run result submit --operation ${operation} --item <checkItemId> --type <type> --file <path>`,+ ' Without one, or if that check is refused too, report the handoff as blocked. Do not retry report upsert.',+ ];+};+export interface ReportUpsertOptions {content?: string;failed?: string;@@ -22,20 +48,27 @@ export interface ReportUpsertOptions {export async function reportUpsertAction(options: ReportUpsertOptions): Promise<void> {const num = (s?: string) => (s === undefined ? undefined : Number.parseInt(s, 10));const client = await getTrpcClient();- await assertReportLanguage(client, [- { path: '--summary', text: options.summary },- { path: '--content', text: options.content },- ]);- const created = await client.verify.upsertReport.mutate({- content: options.content,- failedChecks: num(options.failed),- passedChecks: num(options.passed),- summary: options.summary,- totalChecks: num(options.total),- uncertainChecks: num(options.uncertain),- verdict: options.verdict as any,- verifyRunId: options.run,- });+ let created: Awaited<ReturnType<typeof client.verify.upsertReport.mutate>>;+ try {+ await assertReportLanguage(client, [+ { path: '--summary', text: options.summary },+ { path: '--content', text: options.content },+ ]);+ created = await client.verify.upsertReport.mutate({+ content: options.content,+ failedChecks: num(options.failed),+ passedChecks: num(options.passed),+ summary: options.summary,+ totalChecks: num(options.total),+ uncertainChecks: num(options.uncertain),+ verdict: options.verdict as any,+ verifyRunId: options.run,+ });+ } catch (error) {+ if (!isOperationTokenRefusal(error)) throw error;+ for (const line of reportRefusalGuidance(options.run)) log.error(line);+ process.exit(1);+ }if (options.json !== undefined) {outputJson(created, typeof options.json === 'string' ? options.json : undefined);return;