( value: unknown, sessionId: string, shellRunId: string, )
| 324 | } |
| 325 | |
| 326 | export function normalizeShellRunRecord( |
| 327 | value: unknown, |
| 328 | sessionId: string, |
| 329 | shellRunId: string, |
| 330 | ): ShellRunRecord { |
| 331 | if (!value || typeof value !== 'object' || Array.isArray(value)) { |
| 332 | throw new Error(`Invalid ShellRun record for ${shellRunId}: expected an object`); |
| 333 | } |
| 334 | const record = value as Partial<ShellRunRecord>; |
| 335 | const requiredStrings = [ |
| 336 | record.shellRunId, |
| 337 | record.sessionId, |
| 338 | record.sourceTurnId, |
| 339 | record.sourceToolCallId, |
| 340 | record.cwd, |
| 341 | record.command, |
| 342 | ]; |
| 343 | const optionalStrings = [record.sourceRunId, record.failureMessage]; |
| 344 | const valid = |
| 345 | hasOnlyKeys(record, SHELL_RUN_RECORD_KEYS) && |
| 346 | requiredStrings.every((item) => typeof item === 'string') && |
| 347 | isShellRunSourceToolCallId(record.sourceToolCallId) && |
| 348 | record.sessionId === sessionId && |
| 349 | record.shellRunId === shellRunId && |
| 350 | isShellRunStatus(record.status) && |
| 351 | isFiniteNumber(record.startedAt) && |
| 352 | isFiniteNumber(record.updatedAt) && |
| 353 | isPositiveInteger(record.revision) && |
| 354 | isShellOutput(record.output) && |
| 355 | (record.completedAt === undefined || isFiniteNumber(record.completedAt)) && |
| 356 | (record.timeoutMs === undefined || isFiniteNumber(record.timeoutMs)) && |
| 357 | (record.exitCode === undefined || isFiniteNumber(record.exitCode)) && |
| 358 | (record.observedAt === undefined || isFiniteNumber(record.observedAt)) && |
| 359 | isShellRunSandboxExecution(record.sandboxExecution) && |
| 360 | isShellRunSandboxEscalation(record.sandboxEscalation, record.sandboxExecution) && |
| 361 | optionalStrings.every((item) => item === undefined || typeof item === 'string'); |
| 362 | if (!valid) { |
| 363 | throw new Error(`Invalid ShellRun record for ${shellRunId}: malformed fields`); |
| 364 | } |
| 365 | if (!isValidShellRunState(record)) { |
| 366 | throw new Error(`Invalid ShellRun record for ${shellRunId}: inconsistent state fields`); |
| 367 | } |
| 368 | return canonicalShellRunRecord(record as ShellRunRecord); |
| 369 | } |
| 370 | |
| 371 | export function assertShellRunPatch(patch: ShellRunPatch): void { |
| 372 | for (const key of Object.keys(patch)) { |
no test coverage detected