-
Notifications
You must be signed in to change notification settings - Fork 122
馃摝 fix: Surface Code API Artifact Truncation #565
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6d83897
馃摝 fix: Surface Code API Artifact Truncation
lia-librechat 824d07d
馃摝 fix: Preserve Artifact Truncation Across Code API Executors
lia-librechat 94db639
馃帹 style: Sort artifact-truncation imports
lia-librechat a05906b
馃摝 fix: Reject Contradictory Artifact Truncation Counts
lia-librechat 147386f
馃摝 fix: Validate Artifact Truncation Reason Totals
lia-librechat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import type { ArtifactTruncation, ArtifactTruncationReason } from '@/types'; | ||
|
|
||
| const MAX_REPORTED_TRUNCATED_PATHS = 20; | ||
| const ARTIFACT_TRUNCATION_REASONS = new Set<string>([ | ||
| 'max_files', | ||
| 'depth', | ||
| 'size', | ||
| 'path', | ||
| 'unreadable', | ||
| ]); | ||
|
|
||
| function isNonNegativeInteger(value: unknown): value is number { | ||
| return typeof value === 'number' && Number.isSafeInteger(value) && value >= 0; | ||
| } | ||
|
|
||
| export function normalizeArtifactTruncation( | ||
| value: unknown | ||
| ): ArtifactTruncation | undefined { | ||
| if (value == null || typeof value !== 'object' || Array.isArray(value)) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const candidate = value as Partial<ArtifactTruncation>; | ||
| if ( | ||
| candidate.code !== 'artifact_truncated' || | ||
| !isNonNegativeInteger(candidate.skipped_count) || | ||
| candidate.skipped_count === 0 || | ||
| candidate.reasons == null || | ||
| typeof candidate.reasons !== 'object' || | ||
| Array.isArray(candidate.reasons) || | ||
| !Array.isArray(candidate.skipped) || | ||
| candidate.skipped.length > MAX_REPORTED_TRUNCATED_PATHS || | ||
| candidate.skipped.length > candidate.skipped_count || | ||
| candidate.skipped.some((path) => typeof path !== 'string') | ||
| ) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const reasons: ArtifactTruncation['reasons'] = {}; | ||
| let reasonTotal = 0; | ||
| for (const [reason, count] of Object.entries(candidate.reasons)) { | ||
| if ( | ||
| !ARTIFACT_TRUNCATION_REASONS.has(reason) || | ||
| !isNonNegativeInteger(count) | ||
|
lia-by-librechat[bot] marked this conversation as resolved.
|
||
| ) { | ||
| return undefined; | ||
| } | ||
| reasonTotal += count; | ||
| if (reasonTotal > candidate.skipped_count) { | ||
| return undefined; | ||
| } | ||
| reasons[reason as ArtifactTruncationReason] = count; | ||
| } | ||
|
|
||
| if (reasonTotal !== candidate.skipped_count) { | ||
| return undefined; | ||
| } | ||
|
|
||
| return { | ||
| code: candidate.code, | ||
| reasons, | ||
| skipped: [...candidate.skipped], | ||
| skipped_count: candidate.skipped_count, | ||
| }; | ||
| } | ||
|
|
||
| export function appendArtifactTruncationWarning( | ||
| output: string, | ||
| truncation: ArtifactTruncation | undefined | ||
| ): string { | ||
| if (truncation == null) { | ||
| return output; | ||
| } | ||
|
|
||
| const reasons = Object.entries(truncation.reasons) | ||
| .map(([reason, count]) => `${reason}: ${count}`) | ||
| .join(', '); | ||
| const shown = truncation.skipped.length; | ||
| const paths = | ||
| shown > 0 | ||
| ? ` Not delivered: ${truncation.skipped.join(', ')}${shown < truncation.skipped_count ? ` (${shown} of ${truncation.skipped_count} shown)` : ''}.` | ||
| : ''; | ||
| const warning = `Note: ${truncation.skipped_count} file(s) were omitted from delivery${reasons ? ` (${reasons})` : ''}.${paths} Write fewer files per execution or combine them into an archive. The code itself ran; do not rerun automatically because it may have had side effects.`; | ||
| return `${output.trimEnd()}\n${warning}\n`; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| import { describe, expect, it } from '@jest/globals'; | ||
| import { | ||
| appendArtifactTruncationWarning, | ||
| normalizeArtifactTruncation, | ||
| } from '../ArtifactTruncation'; | ||
|
|
||
| const marker = { | ||
| code: 'artifact_truncated', | ||
| reasons: { max_files: 70 }, | ||
| skipped: ['report_070.csv', 'report_068.csv'], | ||
| skipped_count: 70, | ||
| }; | ||
|
|
||
| describe('artifact truncation', () => { | ||
| it('normalizes the Code API marker without echoing unexpected fields', () => { | ||
| expect( | ||
| normalizeArtifactTruncation({ ...marker, detail: 'private data' }) | ||
| ).toEqual(marker); | ||
| }); | ||
|
|
||
| it.each([ | ||
| null, | ||
| [], | ||
| { ...marker, code: 'not_truncated' }, | ||
| { ...marker, skipped_count: -1 }, | ||
| { ...marker, skipped_count: 0, skipped: [] }, | ||
| { ...marker, skipped_count: 1 }, | ||
| { ...marker, skipped_count: 1.5 }, | ||
| { ...marker, skipped_count: '70' }, | ||
| { ...marker, reasons: null }, | ||
| { ...marker, reasons: {} }, | ||
| { ...marker, reasons: { max_files: 0 } }, | ||
| { ...marker, reasons: { max_files: 69 } }, | ||
| { ...marker, reasons: { max_files: 71 } }, | ||
| { ...marker, reasons: { max_files: 35, size: 36 } }, | ||
| { ...marker, reasons: { max_files: 35, size: 34 } }, | ||
| { | ||
| ...marker, | ||
| skipped: ['report.csv'], | ||
| skipped_count: 1, | ||
| }, | ||
| { | ||
| ...marker, | ||
| reasons: { max_files: Number.MAX_SAFE_INTEGER + 1 }, | ||
| skipped_count: Number.MAX_SAFE_INTEGER + 1, | ||
| }, | ||
| { | ||
| ...marker, | ||
| reasons: { max_files: Number.MAX_SAFE_INTEGER, size: 1 }, | ||
| skipped_count: Number.MAX_SAFE_INTEGER, | ||
| }, | ||
| { ...marker, skipped_count: NaN }, | ||
| { ...marker, reasons: { max_files: Infinity } }, | ||
| { ...marker, reasons: { max_files: 1, unexpected: 1 } }, | ||
| { ...marker, reasons: { size: -1 } }, | ||
| { ...marker, reasons: { path: 1.2 } }, | ||
| { ...marker, skipped: [null] }, | ||
| { | ||
| ...marker, | ||
| skipped: Array.from({ length: 21 }, (_, index) => `report_${index}.csv`), | ||
| }, | ||
| ])('rejects a malformed or oversized marker: %j', (value) => { | ||
| expect(normalizeArtifactTruncation(value)).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('accepts omission counts that match or exceed the reported paths', () => { | ||
| const fullyReported = { | ||
| ...marker, | ||
| reasons: { max_files: 2 }, | ||
| skipped_count: 2, | ||
| }; | ||
| expect(normalizeArtifactTruncation(fullyReported)).toEqual(fullyReported); | ||
| expect(normalizeArtifactTruncation({ ...marker, skipped: [] })).toEqual({ | ||
| ...marker, | ||
| skipped: [], | ||
| }); | ||
| }); | ||
|
|
||
| it.each([ | ||
| { max_files: 14, depth: 14, size: 14, path: 14, unreadable: 14 }, | ||
| { max_files: 70, size: 0 }, | ||
| ])('accepts consistent reason totals: %j', (reasons) => { | ||
| const value = { ...marker, reasons }; | ||
| expect(normalizeArtifactTruncation(value)).toEqual(value); | ||
| }); | ||
|
|
||
| it('accepts the largest exactly representable omission count', () => { | ||
| const value = { | ||
| ...marker, | ||
| reasons: { max_files: Number.MAX_SAFE_INTEGER - 1, size: 1 }, | ||
| skipped_count: Number.MAX_SAFE_INTEGER, | ||
| }; | ||
| expect(normalizeArtifactTruncation(value)).toEqual(value); | ||
| }); | ||
|
|
||
| it('copies metadata without sharing mutable state across responses', () => { | ||
| const value = { | ||
| ...marker, | ||
| reasons: { ...marker.reasons }, | ||
| skipped: [...marker.skipped], | ||
| }; | ||
| const first = normalizeArtifactTruncation(value); | ||
| const second = normalizeArtifactTruncation(value); | ||
| value.reasons.max_files = 1; | ||
| value.skipped.push('later.csv'); | ||
|
|
||
| expect(first).toEqual(marker); | ||
| expect(second).toEqual(marker); | ||
| expect(first?.reasons).not.toBe(second?.reasons); | ||
| expect(first?.skipped).not.toBe(second?.skipped); | ||
| }); | ||
|
|
||
| it('reports omissions and bounds the displayed paths without suggesting a rerun', () => { | ||
| const truncation = normalizeArtifactTruncation(marker); | ||
| const output = appendArtifactTruncationWarning( | ||
| 'stdout:\ndone\n', | ||
| truncation | ||
| ); | ||
|
|
||
| expect(output).toContain('70 file(s) were omitted from delivery'); | ||
| expect(output).toContain('(max_files: 70)'); | ||
| expect(output).toContain('report_070.csv, report_068.csv (2 of 70 shown)'); | ||
| expect(output).toContain('do not rerun automatically'); | ||
| expect(appendArtifactTruncationWarning('done', undefined)).toBe('done'); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.