From 0f5b46f54436d5f31cc52d9dda163fde8cd47629 Mon Sep 17 00:00:00 2001 From: HonzaKirchner Date: Wed, 25 Nov 2026 08:58:56 +0100 Subject: [PATCH 1/6] fix(builds): handle reduced build data for users without write or build access The API returns a reduced build (no meta, stats, options, usage or log) to users who can read an Actor but can neither write to it nor build it. - builds info: omit origin, duration, compute units and image size when absent - builds log / builds wait: say the user has no access to the log Closes #1641 Co-Authored-By: Claude Fable 5.2 --- src/commands/builds/info.ts | 13 +- src/commands/builds/log.ts | 7 +- src/commands/builds/wait.ts | 6 +- src/lib/commands/agent-output.ts | 8 + src/lib/utils.ts | 11 +- .../commands/builds/reduced-build.test.ts | 141 ++++++++++++++++++ 6 files changed, 173 insertions(+), 13 deletions(-) create mode 100644 test/local/commands/builds/reduced-build.test.ts diff --git a/src/commands/builds/info.ts b/src/commands/builds/info.ts index 307183152..334ff4952 100644 --- a/src/commands/builds/info.ts +++ b/src/commands/builds/info.ts @@ -82,9 +82,11 @@ export class BuildsInfoCommand extends ApifyCommand { ]; if (build.finishedAt) { - message.push( - ` ${chalk.yellow('Finished')}: ${TimestampFormatter.display(build.finishedAt)} (took ${chalk.gray(DurationFormatter.format(build.stats?.durationMillis ?? 0))})`, - ); + const durationMillis = build.stats?.durationMillis; + const took = + durationMillis === undefined ? '' : ` (took ${chalk.gray(DurationFormatter.format(durationMillis))})`; + + message.push(` ${chalk.yellow('Finished')}: ${TimestampFormatter.display(build.finishedAt)}${took}`); } else { const diff = Date.now() - build.startedAt.getTime(); message.push(` ${chalk.yellow('Finished')}: ${chalk.gray(`Running for ${DurationFormatter.format(diff)}`)}`); @@ -102,7 +104,10 @@ export class BuildsInfoCommand extends ApifyCommand { message.push(` ${chalk.yellow('Docker Image Size')}: ${prettyPrintBytes({ bytes: dockerImageSize })}`); } - message.push(` ${chalk.yellow('Origin')}: ${build.meta.origin ?? 'UNKNOWN'}`); + // Missing when the user can neither write to nor build the Actor + if (build.meta) { + message.push(` ${chalk.yellow('Origin')}: ${build.meta.origin ?? 'UNKNOWN'}`); + } message.push(''); diff --git a/src/commands/builds/log.ts b/src/commands/builds/log.ts index 3329e1b5b..b8ddf3964 100644 --- a/src/commands/builds/log.ts +++ b/src/commands/builds/log.ts @@ -1,5 +1,6 @@ import { ApifyCommand } from '../../lib/command-framework/apify-command.js'; import { Args } from '../../lib/command-framework/args.js'; +import { isReducedBuild } from '../../lib/commands/agent-output.js'; import { info } from '../../lib/outputs.js'; import { getLoggedClientOrThrow, outputJobLog } from '../../lib/utils.js'; @@ -39,6 +40,10 @@ export class BuildsLogCommand extends ApifyCommand { info({ message: `Log for build with ID "${buildId}":\n` }); - await outputJobLog({ job: build, apifyClient }); + const result = await outputJobLog({ job: build, apifyClient }); + + if (result === 'no-logs' && isReducedBuild(build)) { + throw new Error(`You don't have access to the log of build with ID "${buildId}".`); + } } } diff --git a/src/commands/builds/wait.ts b/src/commands/builds/wait.ts index e64e97e5b..a6a416278 100644 --- a/src/commands/builds/wait.ts +++ b/src/commands/builds/wait.ts @@ -10,6 +10,7 @@ import { exitCodeForWaitResult, fetchLogTail, formatResultSummary, + isReducedBuild, waitForTerminalStatus, } from '../../lib/commands/agent-output.js'; import { error, simpleLog } from '../../lib/outputs.js'; @@ -118,13 +119,16 @@ export class BuildsWaitCommand extends ApifyCommand { const links = [{ label: 'Build URL', url }]; + const errorReason = + logTail.length === 0 && isReducedBuild(build) ? ["You don't have access to this build's log."] : logTail; + simpleLog({ message: formatResultSummary({ resultLabel: 'Apify build result', overallStatus: build.status, lines, links, - errorReason: ok ? undefined : logTail, + errorReason: ok ? undefined : errorReason, }), stdout: true, }); diff --git a/src/lib/commands/agent-output.ts b/src/lib/commands/agent-output.ts index 5d9021b32..d33ec412b 100644 --- a/src/lib/commands/agent-output.ts +++ b/src/lib/commands/agent-output.ts @@ -107,6 +107,14 @@ export async function fetchLogTail(apifyClient: ApifyClient, jobId: string, maxL } } +/** + * The API returns a reduced build, without `meta`, `stats`, `options`, `usage` or a log, + * to users who can read the Actor but can neither write to it nor build it. + */ +export function isReducedBuild(build: Build): boolean { + return !build.meta; +} + export function consoleActorUrl(actorId: string): string { return `${getConsoleUrl()}/actors/${actorId}`; } diff --git a/src/lib/utils.ts b/src/lib/utils.ts index cbf925200..ea7bd98b4 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -693,14 +693,11 @@ export const outputJobLog = async ({ // In case job was already done just output log if (ACTOR_JOB_TERMINAL_STATUSES.includes(status as never)) { - if (process.env.APIFY_NO_LOGS_IN_TESTS) { - return; - } - - // Undefined when the job failed before its container started. + // Undefined when the job failed before its container started, or when the user can't read the log. const log = await client.log(logId).get(); - if (log) process.stderr.write(log); - return; + if (log === undefined) return 'no-logs'; + if (!process.env.APIFY_NO_LOGS_IN_TESTS) process.stderr.write(log); + return 'finished'; } // In other case stream it to stderr diff --git a/test/local/commands/builds/reduced-build.test.ts b/test/local/commands/builds/reduced-build.test.ts new file mode 100644 index 000000000..f180a5e6a --- /dev/null +++ b/test/local/commands/builds/reduced-build.test.ts @@ -0,0 +1,141 @@ +import type { ApifyClient } from 'apify-client'; + +import { BuildsInfoCommand } from '../../../../src/commands/builds/info.js'; +import { BuildsLogCommand } from '../../../../src/commands/builds/log.js'; +import { BuildsLsCommand } from '../../../../src/commands/builds/ls.js'; +import { BuildsWaitCommand } from '../../../../src/commands/builds/wait.js'; +import { testRunCommand } from '../../../../src/lib/command-framework/apify-command.js'; +import { useAuthSetup } from '../../../__setup__/hooks/useAuthSetup.js'; +import { useConsoleSpy } from '../../../__setup__/hooks/useConsoleSpy.js'; + +const { mockGetLoggedClientOrThrow } = vitest.hoisted(() => ({ + mockGetLoggedClientOrThrow: vitest.fn(), +})); + +vitest.mock('../../../../src/lib/utils.js', async (importOriginal) => { + const original = await importOriginal(); + return { + ...original, + getLoggedClientOrThrow: mockGetLoggedClientOrThrow, + }; +}); + +useAuthSetup(); + +const { logMessages } = useConsoleSpy(); + +// What the API returns to users who can read the Actor but can neither write to it nor build it. +const reducedBuild = { + id: 'buildId', + actId: 'actorId', + buildNumber: '1.0.30', + status: 'FAILED', + startedAt: new Date('2026-01-01T10:00:00.000Z'), + finishedAt: new Date('2026-01-01T10:01:05.000Z'), +}; + +const fullBuild = { + ...reducedBuild, + meta: { origin: 'CLI' }, + stats: { durationMillis: 65_000, computeUnits: 0.0123, imageSizeBytes: 1024 * 1024 }, +}; + +const fakeClient = ({ build, log }: { build: object; log?: string }) => + ({ + actor: () => ({ + get: async () => ({ + id: 'actorId', + name: 'some-actor', + username: 'someone-else', + taggedBuilds: { latest: { buildId: 'buildId', buildNumber: '1.0.30' } }, + versions: [{ versionNumber: '1.0', buildTag: 'latest' }], + }), + builds: () => ({ list: async () => ({ total: 1, items: [build] }) }), + }), + build: () => ({ get: async () => build }), + log: () => ({ get: async () => log }), + }) as unknown as ApifyClient; + +// The command framework records a thrown error on the real `process`; put it back so it cannot leak. +let originalExitCode: typeof process.exitCode; + +beforeEach(() => { + originalExitCode = process.exitCode; +}); + +afterEach(() => { + process.exitCode = originalExitCode; + mockGetLoggedClientOrThrow.mockReset(); +}); + +describe('builds commands with a reduced build', () => { + it('builds info omits the lines whose data is missing', async () => { + mockGetLoggedClientOrThrow.mockResolvedValue(fakeClient({ build: reducedBuild })); + + await testRunCommand(BuildsInfoCommand, { args_buildId: 'buildId' }); + + const output = logMessages.log.join('\n'); + + expect(logMessages.error).toEqual([]); + expect(output).toContain('Build Number: 1.0.30'); + expect(output).toContain('Status: Failed'); + expect(output).toContain('Finished:'); + expect(output).not.toContain('took'); + expect(output).not.toContain('Compute Units'); + expect(output).not.toContain('Docker Image Size'); + expect(output).not.toContain('Origin'); + }); + + it('builds info still prints every line for a full build', async () => { + mockGetLoggedClientOrThrow.mockResolvedValue(fakeClient({ build: fullBuild })); + + await testRunCommand(BuildsInfoCommand, { args_buildId: 'buildId' }); + + const output = logMessages.log.join('\n'); + + expect(output).toContain('took'); + expect(output).toContain('Compute Units: 0.012'); + expect(output).toContain('Docker Image Size'); + expect(output).toContain('Origin: CLI'); + }); + + it('builds log says the user has no access to the log', async () => { + mockGetLoggedClientOrThrow.mockResolvedValue(fakeClient({ build: reducedBuild })); + + await testRunCommand(BuildsLogCommand, { args_buildId: 'buildId' }); + + const errors = logMessages.error.join('\n'); + + expect(errors).toContain(`You don't have access to the log of build with ID "buildId".`); + expect(errors).not.toContain('was not found'); + }); + + it('builds log stays quiet when a full build has no log', async () => { + mockGetLoggedClientOrThrow.mockResolvedValue(fakeClient({ build: fullBuild })); + + await testRunCommand(BuildsLogCommand, { args_buildId: 'buildId' }); + + expect(logMessages.error.join('\n')).not.toContain("don't have access"); + }); + + it('builds wait reports the missing log access as the reason', async () => { + mockGetLoggedClientOrThrow.mockResolvedValue(fakeClient({ build: reducedBuild })); + + await testRunCommand(BuildsWaitCommand, { args_buildId: 'buildId' }); + + expect(logMessages.log.join('\n')).toContain("You don't have access to this build's log."); + }); + + it('builds ls renders reduced rows', async () => { + mockGetLoggedClientOrThrow.mockResolvedValue(fakeClient({ build: reducedBuild })); + + await testRunCommand(BuildsLsCommand, { args_actorId: 'someone-else/some-actor' }); + + const output = logMessages.log.join('\n'); + + expect(logMessages.error).toEqual([]); + expect(output).toContain('1.0.30'); + expect(output).toContain('buildId'); + expect(output).toContain('1m 5s'); + }); +}); From 8047db22ab250427f7bf99feeccce68ea886c5e3 Mon Sep 17 00:00:00 2001 From: HonzaKirchner Date: Wed, 25 Nov 2026 09:07:07 +0100 Subject: [PATCH 2/6] fix(builds): detect reduced builds by missing userId and meta.origin The API keeps an empty meta object on reduced builds, so the presence of meta no longer tells them apart. Co-Authored-By: Claude Fable 5.2 --- src/commands/builds/info.ts | 9 +++++--- src/lib/commands/agent-output.ts | 21 ++++++++++++++++--- .../commands/builds/reduced-build.test.ts | 12 +++++++++++ 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/commands/builds/info.ts b/src/commands/builds/info.ts index 334ff4952..79f91db5c 100644 --- a/src/commands/builds/info.ts +++ b/src/commands/builds/info.ts @@ -104,9 +104,12 @@ export class BuildsInfoCommand extends ApifyCommand { message.push(` ${chalk.yellow('Docker Image Size')}: ${prettyPrintBytes({ bytes: dockerImageSize })}`); } - // Missing when the user can neither write to nor build the Actor - if (build.meta) { - message.push(` ${chalk.yellow('Origin')}: ${build.meta.origin ?? 'UNKNOWN'}`); + // A reduced build (see `isReducedBuild`) has an empty `meta`, so there's no origin to print. + // Optional chaining also covers `meta` missing entirely. + const origin = (build.meta as Partial | undefined)?.origin; + + if (origin) { + message.push(` ${chalk.yellow('Origin')}: ${origin}`); } message.push(''); diff --git a/src/lib/commands/agent-output.ts b/src/lib/commands/agent-output.ts index d33ec412b..41d1a8aa5 100644 --- a/src/lib/commands/agent-output.ts +++ b/src/lib/commands/agent-output.ts @@ -108,11 +108,26 @@ export async function fetchLogTail(apifyClient: ApifyClient, jobId: string, maxL } /** - * The API returns a reduced build, without `meta`, `stats`, `options`, `usage` or a log, - * to users who can read the Actor but can neither write to it nor build it. + * Tells whether the API returned a reduced build. + * + * Users who can read an Actor but can neither write to it nor build it get a build with only + * `id`, `actId`, `buildNumber`, `status`, the timestamps and the Actor definition fields. Its log + * returns 404, the same as for a build that has no log, so commands use this to tell the two apart. + * + * The API has no explicit flag for it, so this infers it from two fields that every full build + * carries and a reduced one doesn't: + * - `userId`: the user who started the build. It's left out of the reduced build. + * - `meta.origin`: where the build was started from. The reduced build keeps `meta`, to stay + * compatible with clients that read it without a guard, but as an empty object. + * + * Don't check for `meta` itself, it's present either way. Both fields must be missing, so a full + * build that lacks one of them for any reason isn't mistaken for a reduced one. */ export function isReducedBuild(build: Build): boolean { - return !build.meta; + // Typed as always present, but a reduced build breaks that contract. + const { userId, meta } = build as Partial> & { meta?: Partial }; + + return !userId && !meta?.origin; } export function consoleActorUrl(actorId: string): string { diff --git a/test/local/commands/builds/reduced-build.test.ts b/test/local/commands/builds/reduced-build.test.ts index f180a5e6a..5dabea211 100644 --- a/test/local/commands/builds/reduced-build.test.ts +++ b/test/local/commands/builds/reduced-build.test.ts @@ -25,7 +25,9 @@ useAuthSetup(); const { logMessages } = useConsoleSpy(); // What the API returns to users who can read the Actor but can neither write to it nor build it. +// `meta` stays, but empty. const reducedBuild = { + meta: {}, id: 'buildId', actId: 'actorId', buildNumber: '1.0.30', @@ -36,6 +38,7 @@ const reducedBuild = { const fullBuild = { ...reducedBuild, + userId: 'userId', meta: { origin: 'CLI' }, stats: { durationMillis: 65_000, computeUnits: 0.0123, imageSizeBytes: 1024 * 1024 }, }; @@ -110,6 +113,15 @@ describe('builds commands with a reduced build', () => { expect(errors).not.toContain('was not found'); }); + it('builds log treats a build without any meta as reduced too', async () => { + const { meta: _meta, ...buildWithoutMeta } = reducedBuild; + mockGetLoggedClientOrThrow.mockResolvedValue(fakeClient({ build: buildWithoutMeta })); + + await testRunCommand(BuildsLogCommand, { args_buildId: 'buildId' }); + + expect(logMessages.error.join('\n')).toContain("You don't have access to the log"); + }); + it('builds log stays quiet when a full build has no log', async () => { mockGetLoggedClientOrThrow.mockResolvedValue(fakeClient({ build: fullBuild })); From 66e095c4d1211bd8d460d034622be4a96d84ae04 Mon Sep 17 00:00:00 2001 From: HonzaKirchner Date: Thu, 26 Nov 2026 14:22:52 +0100 Subject: [PATCH 3/6] fix(builds): detect reduced builds by missing userId only The API keeps returning meta.origin on reduced builds, so only the missing userId tells them apart. builds info prints the origin again. Co-Authored-By: Claude Fable 5.2 --- src/commands/builds/info.ts | 8 +------- src/lib/commands/agent-output.ts | 15 ++++----------- test/local/commands/builds/reduced-build.test.ts | 16 +++------------- 3 files changed, 8 insertions(+), 31 deletions(-) diff --git a/src/commands/builds/info.ts b/src/commands/builds/info.ts index 79f91db5c..666607e16 100644 --- a/src/commands/builds/info.ts +++ b/src/commands/builds/info.ts @@ -104,13 +104,7 @@ export class BuildsInfoCommand extends ApifyCommand { message.push(` ${chalk.yellow('Docker Image Size')}: ${prettyPrintBytes({ bytes: dockerImageSize })}`); } - // A reduced build (see `isReducedBuild`) has an empty `meta`, so there's no origin to print. - // Optional chaining also covers `meta` missing entirely. - const origin = (build.meta as Partial | undefined)?.origin; - - if (origin) { - message.push(` ${chalk.yellow('Origin')}: ${origin}`); - } + message.push(` ${chalk.yellow('Origin')}: ${build.meta.origin ?? 'UNKNOWN'}`); message.push(''); diff --git a/src/lib/commands/agent-output.ts b/src/lib/commands/agent-output.ts index 41d1a8aa5..1f5210ecc 100644 --- a/src/lib/commands/agent-output.ts +++ b/src/lib/commands/agent-output.ts @@ -114,20 +114,13 @@ export async function fetchLogTail(apifyClient: ApifyClient, jobId: string, maxL * `id`, `actId`, `buildNumber`, `status`, the timestamps and the Actor definition fields. Its log * returns 404, the same as for a build that has no log, so commands use this to tell the two apart. * - * The API has no explicit flag for it, so this infers it from two fields that every full build - * carries and a reduced one doesn't: - * - `userId`: the user who started the build. It's left out of the reduced build. - * - `meta.origin`: where the build was started from. The reduced build keeps `meta`, to stay - * compatible with clients that read it without a guard, but as an empty object. - * - * Don't check for `meta` itself, it's present either way. Both fields must be missing, so a full - * build that lacks one of them for any reason isn't mistaken for a reduced one. + * The API has no explicit flag for it, so this infers it from `userId`, the user who started the + * build. Every full build carries it and a reduced one doesn't. `meta` is no use here, the reduced + * build keeps it, `origin` included. */ export function isReducedBuild(build: Build): boolean { // Typed as always present, but a reduced build breaks that contract. - const { userId, meta } = build as Partial> & { meta?: Partial }; - - return !userId && !meta?.origin; + return !(build as Partial>).userId; } export function consoleActorUrl(actorId: string): string { diff --git a/test/local/commands/builds/reduced-build.test.ts b/test/local/commands/builds/reduced-build.test.ts index 5dabea211..985e86efa 100644 --- a/test/local/commands/builds/reduced-build.test.ts +++ b/test/local/commands/builds/reduced-build.test.ts @@ -25,9 +25,9 @@ useAuthSetup(); const { logMessages } = useConsoleSpy(); // What the API returns to users who can read the Actor but can neither write to it nor build it. -// `meta` stays, but empty. +// `meta` stays, `userId` and the rest don't. const reducedBuild = { - meta: {}, + meta: { origin: 'CLI' }, id: 'buildId', actId: 'actorId', buildNumber: '1.0.30', @@ -39,7 +39,6 @@ const reducedBuild = { const fullBuild = { ...reducedBuild, userId: 'userId', - meta: { origin: 'CLI' }, stats: { durationMillis: 65_000, computeUnits: 0.0123, imageSizeBytes: 1024 * 1024 }, }; @@ -86,7 +85,7 @@ describe('builds commands with a reduced build', () => { expect(output).not.toContain('took'); expect(output).not.toContain('Compute Units'); expect(output).not.toContain('Docker Image Size'); - expect(output).not.toContain('Origin'); + expect(output).toContain('Origin: CLI'); }); it('builds info still prints every line for a full build', async () => { @@ -113,15 +112,6 @@ describe('builds commands with a reduced build', () => { expect(errors).not.toContain('was not found'); }); - it('builds log treats a build without any meta as reduced too', async () => { - const { meta: _meta, ...buildWithoutMeta } = reducedBuild; - mockGetLoggedClientOrThrow.mockResolvedValue(fakeClient({ build: buildWithoutMeta })); - - await testRunCommand(BuildsLogCommand, { args_buildId: 'buildId' }); - - expect(logMessages.error.join('\n')).toContain("You don't have access to the log"); - }); - it('builds log stays quiet when a full build has no log', async () => { mockGetLoggedClientOrThrow.mockResolvedValue(fakeClient({ build: fullBuild })); From dc95b2ff6359d36c3760058059e1842445cc122a Mon Sep 17 00:00:00 2001 From: HonzaKirchner Date: Thu, 26 Nov 2026 14:50:13 +0100 Subject: [PATCH 4/6] fix(builds): detect reduced builds by missing options The final platform change keeps userId and meta.origin on reduced builds, so infer a reduced build from the missing options instead. Also compute the duration in builds info from the timestamps when stats are missing. Co-Authored-By: Claude Fable 5.2 --- src/commands/builds/info.ts | 8 ++++---- src/lib/commands/agent-output.ts | 11 +++++------ test/local/commands/builds/reduced-build.test.ts | 8 ++++---- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/commands/builds/info.ts b/src/commands/builds/info.ts index 666607e16..d0750fce3 100644 --- a/src/commands/builds/info.ts +++ b/src/commands/builds/info.ts @@ -82,11 +82,11 @@ export class BuildsInfoCommand extends ApifyCommand { ]; if (build.finishedAt) { - const durationMillis = build.stats?.durationMillis; - const took = - durationMillis === undefined ? '' : ` (took ${chalk.gray(DurationFormatter.format(durationMillis))})`; + const durationMillis = build.stats?.durationMillis ?? build.finishedAt.getTime() - build.startedAt.getTime(); - message.push(` ${chalk.yellow('Finished')}: ${TimestampFormatter.display(build.finishedAt)}${took}`); + message.push( + ` ${chalk.yellow('Finished')}: ${TimestampFormatter.display(build.finishedAt)} (took ${chalk.gray(DurationFormatter.format(durationMillis))})`, + ); } else { const diff = Date.now() - build.startedAt.getTime(); message.push(` ${chalk.yellow('Finished')}: ${chalk.gray(`Running for ${DurationFormatter.format(diff)}`)}`); diff --git a/src/lib/commands/agent-output.ts b/src/lib/commands/agent-output.ts index 1f5210ecc..a2c84ded0 100644 --- a/src/lib/commands/agent-output.ts +++ b/src/lib/commands/agent-output.ts @@ -111,16 +111,15 @@ export async function fetchLogTail(apifyClient: ApifyClient, jobId: string, maxL * Tells whether the API returned a reduced build. * * Users who can read an Actor but can neither write to it nor build it get a build with only - * `id`, `actId`, `buildNumber`, `status`, the timestamps and the Actor definition fields. Its log + * the identifiers, `status`, the timestamps, `meta.origin` and the Actor definition fields. Its log * returns 404, the same as for a build that has no log, so commands use this to tell the two apart. * - * The API has no explicit flag for it, so this infers it from `userId`, the user who started the - * build. Every full build carries it and a reduced one doesn't. `meta` is no use here, the reduced - * build keeps it, `origin` included. + * The API has no explicit flag for it, so this infers it from `options`. The platform sets them + * when it creates a build, so every full build carries them and a reduced one doesn't. `userId` + * and `meta` are no use here, the reduced build keeps both. */ export function isReducedBuild(build: Build): boolean { - // Typed as always present, but a reduced build breaks that contract. - return !(build as Partial>).userId; + return build.options === undefined; } export function consoleActorUrl(actorId: string): string { diff --git a/test/local/commands/builds/reduced-build.test.ts b/test/local/commands/builds/reduced-build.test.ts index 985e86efa..8dd7941b7 100644 --- a/test/local/commands/builds/reduced-build.test.ts +++ b/test/local/commands/builds/reduced-build.test.ts @@ -25,11 +25,12 @@ useAuthSetup(); const { logMessages } = useConsoleSpy(); // What the API returns to users who can read the Actor but can neither write to it nor build it. -// `meta` stays, `userId` and the rest don't. +// `userId` and `meta.origin` stay, the build internals such as `options` and `stats` don't. const reducedBuild = { meta: { origin: 'CLI' }, id: 'buildId', actId: 'actorId', + userId: 'userId', buildNumber: '1.0.30', status: 'FAILED', startedAt: new Date('2026-01-01T10:00:00.000Z'), @@ -38,7 +39,7 @@ const reducedBuild = { const fullBuild = { ...reducedBuild, - userId: 'userId', + options: { useCache: true, memoryMbytes: 4096, diskMbytes: 8192 }, stats: { durationMillis: 65_000, computeUnits: 0.0123, imageSizeBytes: 1024 * 1024 }, }; @@ -81,8 +82,7 @@ describe('builds commands with a reduced build', () => { expect(logMessages.error).toEqual([]); expect(output).toContain('Build Number: 1.0.30'); expect(output).toContain('Status: Failed'); - expect(output).toContain('Finished:'); - expect(output).not.toContain('took'); + expect(output).toContain('took 1 minute 5 seconds'); expect(output).not.toContain('Compute Units'); expect(output).not.toContain('Docker Image Size'); expect(output).toContain('Origin: CLI'); From 5e8a7db1d3bb15adb2711d31cac28a1aa9899f8e Mon Sep 17 00:00:00 2001 From: HonzaKirchner Date: Thu, 26 Nov 2026 14:58:47 +0100 Subject: [PATCH 5/6] test(builds): trim reduced build tests to one per changed command Co-Authored-By: Claude Fable 5.2 --- .../commands/builds/reduced-build.test.ts | 75 ++++--------------- 1 file changed, 13 insertions(+), 62 deletions(-) diff --git a/test/local/commands/builds/reduced-build.test.ts b/test/local/commands/builds/reduced-build.test.ts index 8dd7941b7..e9929538c 100644 --- a/test/local/commands/builds/reduced-build.test.ts +++ b/test/local/commands/builds/reduced-build.test.ts @@ -2,7 +2,6 @@ import type { ApifyClient } from 'apify-client'; import { BuildsInfoCommand } from '../../../../src/commands/builds/info.js'; import { BuildsLogCommand } from '../../../../src/commands/builds/log.js'; -import { BuildsLsCommand } from '../../../../src/commands/builds/ls.js'; import { BuildsWaitCommand } from '../../../../src/commands/builds/wait.js'; import { testRunCommand } from '../../../../src/lib/command-framework/apify-command.js'; import { useAuthSetup } from '../../../__setup__/hooks/useAuthSetup.js'; @@ -37,44 +36,34 @@ const reducedBuild = { finishedAt: new Date('2026-01-01T10:01:05.000Z'), }; -const fullBuild = { - ...reducedBuild, - options: { useCache: true, memoryMbytes: 4096, diskMbytes: 8192 }, - stats: { durationMillis: 65_000, computeUnits: 0.0123, imageSizeBytes: 1024 * 1024 }, -}; - -const fakeClient = ({ build, log }: { build: object; log?: string }) => - ({ - actor: () => ({ - get: async () => ({ - id: 'actorId', - name: 'some-actor', - username: 'someone-else', - taggedBuilds: { latest: { buildId: 'buildId', buildNumber: '1.0.30' } }, - versions: [{ versionNumber: '1.0', buildTag: 'latest' }], - }), - builds: () => ({ list: async () => ({ total: 1, items: [build] }) }), +const fakeClient = { + actor: () => ({ + get: async () => ({ + id: 'actorId', + name: 'some-actor', + username: 'someone-else', + taggedBuilds: { latest: { buildId: 'buildId', buildNumber: '1.0.30' } }, + versions: [{ versionNumber: '1.0', buildTag: 'latest' }], }), - build: () => ({ get: async () => build }), - log: () => ({ get: async () => log }), - }) as unknown as ApifyClient; + }), + build: () => ({ get: async () => reducedBuild }), + log: () => ({ get: async () => undefined }), +} as unknown as ApifyClient; // The command framework records a thrown error on the real `process`; put it back so it cannot leak. let originalExitCode: typeof process.exitCode; beforeEach(() => { originalExitCode = process.exitCode; + mockGetLoggedClientOrThrow.mockResolvedValue(fakeClient); }); afterEach(() => { process.exitCode = originalExitCode; - mockGetLoggedClientOrThrow.mockReset(); }); describe('builds commands with a reduced build', () => { it('builds info omits the lines whose data is missing', async () => { - mockGetLoggedClientOrThrow.mockResolvedValue(fakeClient({ build: reducedBuild })); - await testRunCommand(BuildsInfoCommand, { args_buildId: 'buildId' }); const output = logMessages.log.join('\n'); @@ -88,22 +77,7 @@ describe('builds commands with a reduced build', () => { expect(output).toContain('Origin: CLI'); }); - it('builds info still prints every line for a full build', async () => { - mockGetLoggedClientOrThrow.mockResolvedValue(fakeClient({ build: fullBuild })); - - await testRunCommand(BuildsInfoCommand, { args_buildId: 'buildId' }); - - const output = logMessages.log.join('\n'); - - expect(output).toContain('took'); - expect(output).toContain('Compute Units: 0.012'); - expect(output).toContain('Docker Image Size'); - expect(output).toContain('Origin: CLI'); - }); - it('builds log says the user has no access to the log', async () => { - mockGetLoggedClientOrThrow.mockResolvedValue(fakeClient({ build: reducedBuild })); - await testRunCommand(BuildsLogCommand, { args_buildId: 'buildId' }); const errors = logMessages.error.join('\n'); @@ -112,32 +86,9 @@ describe('builds commands with a reduced build', () => { expect(errors).not.toContain('was not found'); }); - it('builds log stays quiet when a full build has no log', async () => { - mockGetLoggedClientOrThrow.mockResolvedValue(fakeClient({ build: fullBuild })); - - await testRunCommand(BuildsLogCommand, { args_buildId: 'buildId' }); - - expect(logMessages.error.join('\n')).not.toContain("don't have access"); - }); - it('builds wait reports the missing log access as the reason', async () => { - mockGetLoggedClientOrThrow.mockResolvedValue(fakeClient({ build: reducedBuild })); - await testRunCommand(BuildsWaitCommand, { args_buildId: 'buildId' }); expect(logMessages.log.join('\n')).toContain("You don't have access to this build's log."); }); - - it('builds ls renders reduced rows', async () => { - mockGetLoggedClientOrThrow.mockResolvedValue(fakeClient({ build: reducedBuild })); - - await testRunCommand(BuildsLsCommand, { args_actorId: 'someone-else/some-actor' }); - - const output = logMessages.log.join('\n'); - - expect(logMessages.error).toEqual([]); - expect(output).toContain('1.0.30'); - expect(output).toContain('buildId'); - expect(output).toContain('1m 5s'); - }); }); From 63475e09a3c3bea98ac4f5fd94aded2ead3afce3 Mon Sep 17 00:00:00 2001 From: HonzaKirchner Date: Thu, 26 Nov 2026 15:10:23 +0100 Subject: [PATCH 6/6] refactor(builds): trim reduced build handling to info and log Drop the builds wait change and shrink the test and comments to keep the diff small. Co-Authored-By: Claude Fable 5.2 --- src/commands/builds/info.ts | 4 +- src/commands/builds/wait.ts | 6 +-- src/lib/commands/agent-output.ts | 12 +----- .../commands/builds/reduced-build.test.ts | 41 ++++--------------- 4 files changed, 10 insertions(+), 53 deletions(-) diff --git a/src/commands/builds/info.ts b/src/commands/builds/info.ts index d0750fce3..f1a341c02 100644 --- a/src/commands/builds/info.ts +++ b/src/commands/builds/info.ts @@ -82,10 +82,8 @@ export class BuildsInfoCommand extends ApifyCommand { ]; if (build.finishedAt) { - const durationMillis = build.stats?.durationMillis ?? build.finishedAt.getTime() - build.startedAt.getTime(); - message.push( - ` ${chalk.yellow('Finished')}: ${TimestampFormatter.display(build.finishedAt)} (took ${chalk.gray(DurationFormatter.format(durationMillis))})`, + ` ${chalk.yellow('Finished')}: ${TimestampFormatter.display(build.finishedAt)} (took ${chalk.gray(DurationFormatter.format(build.stats?.durationMillis ?? build.finishedAt.getTime() - build.startedAt.getTime()))})`, ); } else { const diff = Date.now() - build.startedAt.getTime(); diff --git a/src/commands/builds/wait.ts b/src/commands/builds/wait.ts index a6a416278..e64e97e5b 100644 --- a/src/commands/builds/wait.ts +++ b/src/commands/builds/wait.ts @@ -10,7 +10,6 @@ import { exitCodeForWaitResult, fetchLogTail, formatResultSummary, - isReducedBuild, waitForTerminalStatus, } from '../../lib/commands/agent-output.js'; import { error, simpleLog } from '../../lib/outputs.js'; @@ -119,16 +118,13 @@ export class BuildsWaitCommand extends ApifyCommand { const links = [{ label: 'Build URL', url }]; - const errorReason = - logTail.length === 0 && isReducedBuild(build) ? ["You don't have access to this build's log."] : logTail; - simpleLog({ message: formatResultSummary({ resultLabel: 'Apify build result', overallStatus: build.status, lines, links, - errorReason: ok ? undefined : errorReason, + errorReason: ok ? undefined : logTail, }), stdout: true, }); diff --git a/src/lib/commands/agent-output.ts b/src/lib/commands/agent-output.ts index a2c84ded0..3d029da18 100644 --- a/src/lib/commands/agent-output.ts +++ b/src/lib/commands/agent-output.ts @@ -107,17 +107,7 @@ export async function fetchLogTail(apifyClient: ApifyClient, jobId: string, maxL } } -/** - * Tells whether the API returned a reduced build. - * - * Users who can read an Actor but can neither write to it nor build it get a build with only - * the identifiers, `status`, the timestamps, `meta.origin` and the Actor definition fields. Its log - * returns 404, the same as for a build that has no log, so commands use this to tell the two apart. - * - * The API has no explicit flag for it, so this infers it from `options`. The platform sets them - * when it creates a build, so every full build carries them and a reduced one doesn't. `userId` - * and `meta` are no use here, the reduced build keeps both. - */ +// The API has no flag for the reduced build it returns to users who can't write to or build the Actor; only full builds carry `options`. export function isReducedBuild(build: Build): boolean { return build.options === undefined; } diff --git a/test/local/commands/builds/reduced-build.test.ts b/test/local/commands/builds/reduced-build.test.ts index e9929538c..e175f2cc9 100644 --- a/test/local/commands/builds/reduced-build.test.ts +++ b/test/local/commands/builds/reduced-build.test.ts @@ -2,7 +2,6 @@ import type { ApifyClient } from 'apify-client'; import { BuildsInfoCommand } from '../../../../src/commands/builds/info.js'; import { BuildsLogCommand } from '../../../../src/commands/builds/log.js'; -import { BuildsWaitCommand } from '../../../../src/commands/builds/wait.js'; import { testRunCommand } from '../../../../src/lib/command-framework/apify-command.js'; import { useAuthSetup } from '../../../__setup__/hooks/useAuthSetup.js'; import { useConsoleSpy } from '../../../__setup__/hooks/useConsoleSpy.js'; @@ -11,20 +10,16 @@ const { mockGetLoggedClientOrThrow } = vitest.hoisted(() => ({ mockGetLoggedClientOrThrow: vitest.fn(), })); -vitest.mock('../../../../src/lib/utils.js', async (importOriginal) => { - const original = await importOriginal(); - return { - ...original, - getLoggedClientOrThrow: mockGetLoggedClientOrThrow, - }; -}); +vitest.mock('../../../../src/lib/utils.js', async (importOriginal) => ({ + ...(await importOriginal()), + getLoggedClientOrThrow: mockGetLoggedClientOrThrow, +})); useAuthSetup(); const { logMessages } = useConsoleSpy(); // What the API returns to users who can read the Actor but can neither write to it nor build it. -// `userId` and `meta.origin` stay, the build internals such as `options` and `stats` don't. const reducedBuild = { meta: { origin: 'CLI' }, id: 'buildId', @@ -37,15 +32,7 @@ const reducedBuild = { }; const fakeClient = { - actor: () => ({ - get: async () => ({ - id: 'actorId', - name: 'some-actor', - username: 'someone-else', - taggedBuilds: { latest: { buildId: 'buildId', buildNumber: '1.0.30' } }, - versions: [{ versionNumber: '1.0', buildTag: 'latest' }], - }), - }), + actor: () => ({ get: async () => undefined }), build: () => ({ get: async () => reducedBuild }), log: () => ({ get: async () => undefined }), } as unknown as ApifyClient; @@ -63,32 +50,18 @@ afterEach(() => { }); describe('builds commands with a reduced build', () => { - it('builds info omits the lines whose data is missing', async () => { + it('builds info computes the duration from the timestamps', async () => { await testRunCommand(BuildsInfoCommand, { args_buildId: 'buildId' }); const output = logMessages.log.join('\n'); - expect(logMessages.error).toEqual([]); - expect(output).toContain('Build Number: 1.0.30'); - expect(output).toContain('Status: Failed'); expect(output).toContain('took 1 minute 5 seconds'); expect(output).not.toContain('Compute Units'); - expect(output).not.toContain('Docker Image Size'); - expect(output).toContain('Origin: CLI'); }); it('builds log says the user has no access to the log', async () => { await testRunCommand(BuildsLogCommand, { args_buildId: 'buildId' }); - const errors = logMessages.error.join('\n'); - - expect(errors).toContain(`You don't have access to the log of build with ID "buildId".`); - expect(errors).not.toContain('was not found'); - }); - - it('builds wait reports the missing log access as the reason', async () => { - await testRunCommand(BuildsWaitCommand, { args_buildId: 'buildId' }); - - expect(logMessages.log.join('\n')).toContain("You don't have access to this build's log."); + expect(logMessages.error.join('\n')).toContain(`You don't have access to the log of build with ID "buildId".`); }); });