Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
@@ -1,12 +1,4 @@
|
||||
import type { SetupScriptImportCandidate, SetupScriptImportFileRead } from './setup-script-imports'
|
||||
import {
|
||||
isSetupScriptImportFieldWithinLimit,
|
||||
SETUP_SCRIPT_IMPORT_MAX_FIELD_BYTES,
|
||||
SETUP_SCRIPT_IMPORT_MAX_FIELD_CODE_UNITS,
|
||||
SETUP_SCRIPT_IMPORT_MAX_TOML_LINES,
|
||||
SETUP_SCRIPT_IMPORT_MAX_UNSUPPORTED_FIELDS
|
||||
} from './setup-script-import-limits'
|
||||
import { measureUtf8ByteLength } from './utf8-byte-limits'
|
||||
|
||||
const CODEX_ENVIRONMENT_PATH = '.codex/environments/environment.toml'
|
||||
|
||||
@@ -25,7 +17,7 @@ export async function inspectCodexEnvironmentConfig(
|
||||
}
|
||||
|
||||
const parsed = parseCodexEnvironmentToml(content)
|
||||
const setup = normalizeCodexScript(parsed.setupScript)
|
||||
const setup = parsed.setupScript?.trim()
|
||||
if (!setup) {
|
||||
return null
|
||||
}
|
||||
@@ -35,15 +27,12 @@ export async function inspectCodexEnvironmentConfig(
|
||||
label: 'Codex environment',
|
||||
files: [CODEX_ENVIRONMENT_PATH],
|
||||
setup,
|
||||
archive: normalizeCodexScript(parsed.cleanupScript) || undefined,
|
||||
archive: parsed.cleanupScript?.trim() || undefined,
|
||||
unsupportedFields: parsed.unsupportedFields
|
||||
}
|
||||
}
|
||||
|
||||
function parseCodexEnvironmentToml(content: string): CodexEnvironmentToml {
|
||||
if (countTomlLines(content) > SETUP_SCRIPT_IMPORT_MAX_TOML_LINES) {
|
||||
return { unsupportedFields: [] }
|
||||
}
|
||||
const lines = content.split(/\r?\n/)
|
||||
const unsupportedFields: string[] = []
|
||||
let section = ''
|
||||
@@ -54,13 +43,13 @@ function parseCodexEnvironmentToml(content: string): CodexEnvironmentToml {
|
||||
const line = lines[index]
|
||||
const trimmed = line.trim()
|
||||
if (/^actions\s*=/.test(trimmed)) {
|
||||
pushUnsupportedField(unsupportedFields, 'actions')
|
||||
unsupportedFields.push('actions')
|
||||
}
|
||||
const sectionMatch = trimmed.match(/^\[([A-Za-z0-9_.-]+)\]\s*(?:#.*)?$/)
|
||||
if (sectionMatch) {
|
||||
section = sectionMatch[1]
|
||||
if (section === 'actions' || section.startsWith('actions.')) {
|
||||
pushUnsupportedField(unsupportedFields, `[${section}]`)
|
||||
unsupportedFields.push(`[${section}]`)
|
||||
}
|
||||
continue
|
||||
}
|
||||
@@ -111,49 +100,22 @@ function parseTomlMultilineString(
|
||||
firstLineRemainder: string,
|
||||
delimiter: '"""' | "'''"
|
||||
): { value: string; endLineIndex: number } {
|
||||
const chunks: string[] = []
|
||||
let retainedBytes = 0
|
||||
let retainedCodeUnits = 0
|
||||
let content = ''
|
||||
let remainder = firstLineRemainder
|
||||
let oversized = false
|
||||
const append = (value: string): boolean => {
|
||||
if (retainedCodeUnits + value.length > SETUP_SCRIPT_IMPORT_MAX_FIELD_CODE_UNITS) {
|
||||
return false
|
||||
}
|
||||
const measurement = measureUtf8ByteLength(value, {
|
||||
stopAfterBytes: SETUP_SCRIPT_IMPORT_MAX_FIELD_BYTES - retainedBytes
|
||||
})
|
||||
if (measurement.exceededLimit) {
|
||||
return false
|
||||
}
|
||||
chunks.push(value)
|
||||
retainedBytes += measurement.byteLength
|
||||
retainedCodeUnits += value.length
|
||||
return true
|
||||
}
|
||||
for (let index = startLineIndex; index < lines.length; index++) {
|
||||
if (index > startLineIndex) {
|
||||
remainder = lines[index]
|
||||
}
|
||||
const closeIndex = remainder.indexOf(delimiter)
|
||||
if (closeIndex >= 0) {
|
||||
if (!oversized && !append(remainder.slice(0, closeIndex))) {
|
||||
oversized = true
|
||||
}
|
||||
return {
|
||||
value: oversized ? '' : chunks.join(''),
|
||||
value: content + remainder.slice(0, closeIndex),
|
||||
endLineIndex: index
|
||||
}
|
||||
}
|
||||
if (!oversized && !append(`${remainder}\n`)) {
|
||||
oversized = true
|
||||
chunks.length = 0
|
||||
}
|
||||
}
|
||||
return {
|
||||
value: oversized ? '' : chunks.join('').trimEnd(),
|
||||
endLineIndex: lines.length - 1
|
||||
content += `${remainder}\n`
|
||||
}
|
||||
return { value: content.trimEnd(), endLineIndex: lines.length - 1 }
|
||||
}
|
||||
|
||||
function parseTomlBasicString(value: string): string {
|
||||
@@ -189,26 +151,3 @@ function isEscaped(value: string, index: number): boolean {
|
||||
}
|
||||
return slashCount % 2 === 1
|
||||
}
|
||||
|
||||
function normalizeCodexScript(value: string | undefined): string {
|
||||
if (!value || !isSetupScriptImportFieldWithinLimit(value)) {
|
||||
return ''
|
||||
}
|
||||
return value.trim()
|
||||
}
|
||||
|
||||
function countTomlLines(content: string): number {
|
||||
let lines = 1
|
||||
for (let index = 0; index < content.length; index++) {
|
||||
if (content.charCodeAt(index) === 10 && ++lines > SETUP_SCRIPT_IMPORT_MAX_TOML_LINES) {
|
||||
return lines
|
||||
}
|
||||
}
|
||||
return lines
|
||||
}
|
||||
|
||||
function pushUnsupportedField(fields: string[], value: string): void {
|
||||
if (fields.length < SETUP_SCRIPT_IMPORT_MAX_UNSUPPORTED_FIELDS) {
|
||||
fields.push(value)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user