π€― 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): reject acceptance ingest scope mismatches before writes (#19824)
AmAzing-fb69a3bb9a1b
diff --git a/apps/cli/src/commands/acceptanceRun.ts b/apps/cli/src/commands/acceptanceRun.tsindex 324ebf2e406..9f25bcc8e37 100644--- a/apps/cli/src/commands/acceptanceRun.ts+++ b/apps/cli/src/commands/acceptanceRun.ts@@ -7,6 +7,7 @@ import type { Command } from 'commander';import pc from 'picocolors';import { getTrpcClient } from '../api/client';+import { resolveWorkspaceId } from '../api/workspace';import { resolveServerUrl } from '../settings';import { ensureAcceptanceDirIgnored, ensureAcceptanceDirIgnoredFor } from '../utils/acceptanceDir';import { confirm, outputJson, printTable, timeAgo, truncate } from '../utils/format';@@ -702,6 +703,30 @@ async function ingestReportAction(reportDir: string, options: IngestReportOptionif (requestedAcceptanceId) {const bundle = await client.acceptance.getBundle.query({ id: requestedAcceptanceId });acceptance = bundle.acceptance;+ // ID-based reads can cross scopes, but creating a run uses the CLI's scope.+ // Reject before any writes instead of leaving an unattachable run behind.+ const currentWorkspaceId = resolveWorkspaceId();+ const targetWorkspaceId = acceptance.workspaceId ?? undefined;+ if (currentWorkspaceId !== targetWorkspaceId) {+ const current = currentWorkspaceId ? `workspace "${currentWorkspaceId}"` : 'personal space';+ const target = targetWorkspaceId ? `workspace "${targetWorkspaceId}"` : 'personal space';+ const hint = targetWorkspaceId+ ? `Set LOBEHUB_WORKSPACE_ID=${targetWorkspaceId} for this command and retry.`+ : "Unset LOBEHUB_WORKSPACE_ID and run 'lh workspace use --personal', then retry.";+ throw new Error(+ `Acceptance "${acceptance.id}" belongs to ${target}, but the CLI is using ${current}. ${hint} No run was created.`,+ );+ }+ if (targetWorkspaceId) {+ // Revoked membership can make the server fall back to personal scope+ // even when the locally selected workspace still matches the target.+ const workspace = await client.workspace.getById.query();+ if (workspace?.id !== targetWorkspaceId) {+ throw new Error(+ `The server did not resolve workspace "${targetWorkspaceId}" for this account. Check your access with 'lh workspace list' before retrying. No run was created.`,+ );+ }+ }plan = plan?.map((item) => ({...item,sourceCriterionId:diff --git a/apps/cli/src/commands/verify.test.ts b/apps/cli/src/commands/verify.test.tsindex 0e3246a4c83..e950962db83 100644--- a/apps/cli/src/commands/verify.test.ts+++ b/apps/cli/src/commands/verify.test.ts@@ -43,7 +43,10 @@ const { getTrpcClient: mockGetTrpcClient } = vi.hoisted(() => ({}));vi.mock('../api/client', () => ({ getTrpcClient: mockGetTrpcClient }));-vi.mock('../settings', () => ({ resolveServerUrl: () => 'https://app.lobehub.com' }));+vi.mock('../settings', () => ({+ loadActiveWorkspace: () => undefined,+ resolveServerUrl: () => 'https://app.lobehub.com',+}));describe('verify rubric config commands', () => {let consoleSpy: ReturnType<typeof vi.spyOn>;@@ -640,6 +643,7 @@ describe('verify ingest-report β every run is an immutable acceptance round',beforeEach(() => {consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {});+ vi.stubEnv('LOBEHUB_WORKSPACE_ID', '');mockGetTrpcClient.mockResolvedValue(mockTrpcClient);const verify = mockTrpcClient.verify as Record<string, any>;verify.createRun = { mutate: vi.fn().mockResolvedValue({ id: 'run-new' }) };@@ -650,6 +654,9 @@ describe('verify ingest-report β every run is an immutable acceptance round',ensure: { mutate: vi.fn().mockResolvedValue({ id: 'acceptance-1' }) },getBundle: { query: vi.fn() },};+ mockTrpcClient.workspace = {+ getById: { query: vi.fn().mockResolvedValue({ id: 'workspace-1' }) },+ };dir = mkdtempSync(path.join(tmpdir(), 'lh-ingest-'));writeFileSync(path.join(dir, 'result.json'), JSON.stringify({ cases: [] }));@@ -658,6 +665,7 @@ describe('verify ingest-report β every run is an immutable acceptance round',afterEach(() => {consoleSpy.mockRestore();+ vi.unstubAllEnvs();delete process.env.LOBEHUB_TOPIC_ID;rmSync(dir, { force: true, recursive: true });});@@ -776,28 +784,99 @@ describe('verify ingest-report β every run is an immutable acceptance round',);});- it('appends a re-verification round directly to an existing acceptance', async () => {+ it.each([null, 'workspace-1'])(+ 'appends a re-verification round when both scopes are %s',+ async (workspaceId) => {+ vi.stubEnv('LOBEHUB_WORKSPACE_ID', workspaceId ?? '');+ mockTrpcClient.acceptance.getBundle.query.mockResolvedValue({+ acceptance: {+ id: 'acceptance-existing',+ status: 'delivered',+ subjectId: 'standalone-subject',+ subjectType: 'standalone',+ workspaceId,+ },+ });++ await run(['ingest-report', dir, '--acceptance', 'acceptance-existing', '--json']);++ expect(mockTrpcClient.acceptance.ensure.mutate).not.toHaveBeenCalled();+ expect(mockTrpcClient.acceptance.attachRun.mutate).toHaveBeenCalledWith({+ acceptanceId: 'acceptance-existing',+ verifyRunId: 'run-new',+ });+ expect(mockTrpcClient.verify.upsertReport.mutate).toHaveBeenCalled();+ expect(process.env.LOBEHUB_WORKSPACE_ID).toBe(workspaceId ?? '');+ if (!workspaceId) expect(mockTrpcClient.workspace.getById.query).not.toHaveBeenCalled();+ },+ );++ it.each([+ { effectiveWorkspace: null, name: 'revoked workspace membership' },+ { effectiveWorkspace: { id: 'workspace-other' }, name: 'a different server workspace' },+ { effectiveWorkspace: new Error('Workspace lookup unavailable'), name: 'a failed lookup' },+ ])('rejects $name before creating a run', async ({ effectiveWorkspace }) => {+ vi.stubEnv('LOBEHUB_WORKSPACE_ID', 'workspace-1');mockTrpcClient.acceptance.getBundle.query.mockResolvedValue({acceptance: {id: 'acceptance-existing',status: 'delivered',subjectId: 'standalone-subject',subjectType: 'standalone',+ workspaceId: 'workspace-1',},});+ const query = mockTrpcClient.workspace.getById.query;+ if (effectiveWorkspace instanceof Error) query.mockRejectedValue(effectiveWorkspace);+ else query.mockResolvedValue(effectiveWorkspace);- await run(['ingest-report', dir, '--acceptance', 'acceptance-existing', '--json']);+ await expect(+ run(['ingest-report', dir, '--acceptance', 'acceptance-existing', '--json']),+ ).rejects.toThrow(+ effectiveWorkspace instanceof Error ? effectiveWorkspace.message : 'No run was created.',+ );- expect(mockTrpcClient.acceptance.getBundle.query).toHaveBeenCalledWith({- id: 'acceptance-existing',- });expect(mockTrpcClient.acceptance.ensure.mutate).not.toHaveBeenCalled();- expect(mockTrpcClient.acceptance.attachRun.mutate).toHaveBeenCalledWith({- acceptanceId: 'acceptance-existing',- verifyRunId: 'run-new',- });+ expect(mockTrpcClient.verify.createRun.mutate).not.toHaveBeenCalled();+ expect(mockTrpcClient.acceptance.attachRun.mutate).not.toHaveBeenCalled();+ expect(mockTrpcClient.verify.upsertReport.mutate).not.toHaveBeenCalled();+ expect(process.env.LOBEHUB_WORKSPACE_ID).toBe('workspace-1');});+ it.each([+ { current: '', hint: 'LOBEHUB_WORKSPACE_ID=workspace-target', target: 'workspace-target' },+ {+ current: 'workspace-current',+ hint: 'LOBEHUB_WORKSPACE_ID=workspace-target',+ target: 'workspace-target',+ },+ { current: 'workspace-current', hint: 'lh workspace use --personal', target: null },+ ])(+ 'rejects scope $current β $target before any remote writes',+ async ({ current, target, hint }) => {+ vi.stubEnv('LOBEHUB_WORKSPACE_ID', current);+ mockTrpcClient.acceptance.getBundle.query.mockResolvedValue({+ acceptance: {+ id: 'acceptance-existing',+ status: 'delivered',+ subjectId: 'standalone-subject',+ subjectType: 'standalone',+ workspaceId: target,+ },+ });++ await expect(+ run(['ingest-report', dir, '--acceptance', 'acceptance-existing', '--json']),+ ).rejects.toThrow(hint);++ expect(mockTrpcClient.acceptance.ensure.mutate).not.toHaveBeenCalled();+ expect(mockTrpcClient.verify.createRun.mutate).not.toHaveBeenCalled();+ expect(mockTrpcClient.acceptance.attachRun.mutate).not.toHaveBeenCalled();+ expect(mockTrpcClient.verify.upsertReport.mutate).not.toHaveBeenCalled();+ expect(process.env.LOBEHUB_WORKSPACE_ID).toBe(current);+ },+ );+it('passes a non-coding scenario and its context bag through to the run', async () => {const verify = mockTrpcClient.verify as Record<string, any>;writeFileSync(