Skip to content

🤯 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.

🐛 fix(cli): recover library uploads with same-account credentials (#23259)

Mochaf67d90ee3564
diff --git a/apps/cli/src/api/operationTokenFallback.test.ts b/apps/cli/src/api/operationTokenFallback.test.ts
index b36c9cd1c99..f227371f14f 100644
--- a/apps/cli/src/api/operationTokenFallback.test.ts
+++ b/apps/cli/src/api/operationTokenFallback.test.ts
@@ -161,6 +161,78 @@ describe('operation token fallback', () => {
);
});
+ const upload = (
+ respond: (authorization: string) => unknown,
+ input: object,
+ route = 'file.createFile',
+ ) => {
+ const client = createTRPCClient<AnyRouter>({
+ links: [operationTokenFallbackLink(() => operationToken), serverLink(respond)],
+ }) as any;
+ return route === 'file.createFile'
+ ? client.file.createFile.mutate(input)
+ : client.doc.createDocument.mutate(input);
+ };
+ const uploadRefusal =
+ "This run's token can only upload acceptance evidence or tool-result images";
+
+ it.each([
+ { destination: { knowledgeBaseId: 'kb-1' }, message: uploadRefusal },
+ { destination: { parentId: 'folder-1' }, message: uploadRefusal },
+ {
+ destination: { knowledgeBaseId: 'kb-1' },
+ message: "This run's token can only upload acceptance evidence",
+ },
+ ])(
+ 'uses the same account after a library upload refusal for %j',
+ async ({ destination, message }) => {
+ storedLogin('user-1');
+ const respond = vi.fn((authorization: string) =>
+ authorization === operationToken ? trpcError(message, 'FORBIDDEN') : 'uploaded',
+ );
+ await expect(upload(respond, destination)).resolves.toBe('uploaded');
+ expect(respond).toHaveBeenCalledTimes(2);
+ },
+ );
+
+ it('reports a missing stored login without repeating the upload', async () => {
+ vi.mocked(getValidToken).mockResolvedValue({ status: 'no-login' });
+ const respond = vi.fn(() => trpcError(uploadRefusal, 'FORBIDDEN'));
+ await expect(upload(respond, { knowledgeBaseId: 'kb-1' })).rejects.toThrow(uploadRefusal);
+ expect(respond).toHaveBeenCalledTimes(1);
+ expect(log.warn).toHaveBeenCalled();
+ });
+
+ it('never borrows another account for a library upload', async () => {
+ storedLogin('another-user');
+ const respond = vi.fn(() => trpcError(uploadRefusal, 'FORBIDDEN'));
+ await expect(upload(respond, { knowledgeBaseId: 'kb-1' })).rejects.toThrow(uploadRefusal);
+ expect(respond).toHaveBeenCalledTimes(1);
+ expect(log.warn).toHaveBeenCalled();
+ });
+
+ it.each([
+ { input: { knowledgeBaseId: 'kb-1' }, message: 'Permission denied', route: 'file.createFile' },
+ { input: {}, message: uploadRefusal, route: 'file.createFile' },
+ { input: { knowledgeBaseId: 'kb-1' }, message: uploadRefusal, route: 'doc.createDocument' },
+ ])(
+ 'does not retry unrelated permissions, evidence, or routes',
+ async ({ input, message, route }) => {
+ storedLogin('user-1');
+ const respond = vi.fn(() => trpcError(message, 'FORBIDDEN'));
+ await expect(upload(respond, input, route)).rejects.toThrow(message);
+ expect(respond).toHaveBeenCalledTimes(1);
+ expect(getValidToken).not.toHaveBeenCalled();
+ },
+ );
+
+ it('does not retry a user permission denial after the library fallback', async () => {
+ storedLogin('user-1');
+ const respond = vi.fn(() => trpcError(uploadRefusal, 'FORBIDDEN'));
+ await expect(upload(respond, { knowledgeBaseId: 'kb-1' })).rejects.toThrow(uploadRefusal);
+ expect(respond).toHaveBeenCalledTimes(2);
+ });
+
it('does not retry other authentication errors', async () => {
storedLogin('user-1');
const expiredToken = vi.fn(() => trpcError('jwt expired', 'UNAUTHORIZED'));
diff --git a/apps/cli/src/api/operationTokenFallback.ts b/apps/cli/src/api/operationTokenFallback.ts
index 8f7c43ff993..a067b57aeb7 100644
--- a/apps/cli/src/api/operationTokenFallback.ts
+++ b/apps/cli/src/api/operationTokenFallback.ts
@@ -50,6 +50,28 @@ export const isOperationTokenRefusal = (error: unknown): boolean =>
error.data?.code === 'UNAUTHORIZED' &&
error.message === OPERATION_TOKEN_REFUSAL;
+/** A library destination cannot be filed as run-scoped evidence. The refusal
+ * happens before createFile writes, so it can use the same one-shot, same-owner
+ * fallback as a user-scoped endpoint. Never retry a general permission denial.
+ */
+const isLibraryUploadRefusal = (op: Operation, error: unknown): boolean => {
+ if (
+ op.path !== 'file.createFile' ||
+ !isTRPCClientError(error) ||
+ error.data?.code !== 'FORBIDDEN' ||
+ ![
+ "This run's token can only upload acceptance evidence",
+ "This run's token can only upload acceptance evidence or tool-result images",
+ ].includes(error.message)
+ )
+ return false;
+
+ const input = op.input as { knowledgeBaseId?: unknown; parentId?: unknown } | undefined;
+ return [input?.knowledgeBaseId, input?.parentId].some(
+ (id) => typeof id === 'string' && id.length > 0,
+ );
+};
+
/**
* The stored login, when it is usable and belongs to the operation token's own
* account.
@@ -87,7 +109,10 @@ export const operationTokenFallbackLink =
let subscription = next(op).subscribe({
complete: () => observer.complete(),
error: (error) => {
- if (op.context[STORED_LOGIN] || !isOperationTokenRefusal(error)) {
+ if (
+ op.context[STORED_LOGIN] ||
+ (!isOperationTokenRefusal(error) && !isLibraryUploadRefusal(op, error))
+ ) {
observer.error(error);
return;
}