Files
lincodex/backend/internal/adapter/secondary/code_runner/result.go
T

230 lines
8.1 KiB
Go

package coderunner
import (
"fmt"
"time"
"lincodex/backend/internal/adapter/secondary/code_runner/api"
"lincodex/backend/internal/port/outbound"
)
// maxRunDuration rejects a duration that no bounded run can have. It also
// keeps the conversion to time.Duration far from overflow.
const maxRunDuration = 24 * time.Hour
// convertResult checks a finished run against the contract rules that the
// schema cannot express and against the request, then converts it. A score is
// computed from the groups, so a result that contradicts the request or
// itself is refused rather than trusted.
//
// An infrastructure_failure run is not a result: it says nothing about the
// program and becomes outbound.ErrCodeRunnerInfrastructureFailure.
func convertResult(raw api.RunResult, request outbound.CodeRunRequest) (*outbound.CodeRunResult, error) {
if raw.Outcome == api.RunOutcomeInfrastructureFailure {
return nil, outbound.ErrCodeRunnerInfrastructureFailure
}
outcome, err := convertOutcome(raw.Outcome)
if err != nil {
return nil, err
}
compile, err := convertCompile(raw.Compile, outcome)
if err != nil {
return nil, err
}
if len(raw.Groups) != len(request.Groups) {
return nil, badResult("the number of groups differs from the request")
}
if raw.DurationMs < 0 || time.Duration(raw.DurationMs) > maxRunDuration/time.Millisecond {
return nil, badResult("the run duration is out of range")
}
compileFailed := outcome == outbound.CodeOutcomeCompileError
groups := make([]outbound.CodeGroupResult, 0, len(raw.Groups))
expectedOutcome := outbound.CodeOutcomeAccepted
for index, rawGroup := range raw.Groups {
group, err := convertGroup(rawGroup, request.Groups[index], compileFailed)
if err != nil {
return nil, err
}
if expectedOutcome == outbound.CodeOutcomeAccepted {
expectedOutcome = group.Outcome
}
groups = append(groups, group)
}
// The run outcome is compile_error when that happened, otherwise the
// outcome of the first failed group, otherwise accepted.
if outcome != expectedOutcome {
return nil, badResult("the run outcome contradicts its groups")
}
return &outbound.CodeRunResult{
Outcome: outcome,
Compile: compile,
Groups: groups,
Duration: time.Duration(raw.DurationMs) * time.Millisecond,
}, nil
}
func convertOutcome(raw api.RunOutcome) (outbound.CodeRunOutcome, error) {
switch raw {
case api.RunOutcomeAccepted:
return outbound.CodeOutcomeAccepted, nil
case api.RunOutcomeWrongAnswer:
return outbound.CodeOutcomeWrongAnswer, nil
case api.RunOutcomeCompileError:
return outbound.CodeOutcomeCompileError, nil
case api.RunOutcomeTimeLimitExceeded:
return outbound.CodeOutcomeTimeLimitExceeded, nil
case api.RunOutcomeMemoryLimitExceeded:
return outbound.CodeOutcomeMemoryLimitExceeded, nil
case api.RunOutcomeOutputLimitExceeded:
return outbound.CodeOutcomeOutputLimitExceeded, nil
case api.RunOutcomeRuntimeError:
return outbound.CodeOutcomeRuntimeError, nil
case api.RunOutcomeInfrastructureFailure:
// The caller handles it where it is legitimate (the run outcome);
// anywhere else it contradicts a graded run.
return "", badResult("the outcome is not valid here")
default:
return "", badResult("the outcome is unknown")
}
}
func convertCompile(raw api.OptCompileResult, runOutcome outbound.CodeRunOutcome) (*outbound.CodeCompileResult, error) {
compileFailed := runOutcome == outbound.CodeOutcomeCompileError
if !raw.Set {
if compileFailed {
return nil, badResult("a compile error has no compile result")
}
return nil, nil
}
outcome, err := convertOutcome(raw.Value.Outcome)
if err != nil {
return nil, err
}
// A compile step either passed or failed the run; nothing else is valid.
if compileFailed != (outcome == outbound.CodeOutcomeCompileError) ||
(!compileFailed && outcome != outbound.CodeOutcomeAccepted) {
return nil, badResult("the compile result contradicts the run outcome")
}
return &outbound.CodeCompileResult{Outcome: outcome, Diagnostic: raw.Value.Diagnostic.Or("")}, nil
}
// checkGroupShape compares a group result with the group that was requested.
func checkGroupShape(raw api.GroupResult, expected outbound.CodeTestGroup) error {
if string(raw.ID) != expected.ID || outbound.CodeTestVisibility(raw.Visibility) != expected.Visibility {
return badResult("a group differs from the request")
}
total := len(expected.Tests)
if int(raw.TotalTests) != total || int(raw.PassedTests) > total || len(raw.Tests) > total {
return badResult("a group count differs from the request")
}
return nil
}
func convertGroup(raw api.GroupResult, expected outbound.CodeTestGroup, compileFailed bool) (outbound.CodeGroupResult, error) {
if err := checkGroupShape(raw, expected); err != nil {
return outbound.CodeGroupResult{}, err
}
outcome, err := convertOutcome(raw.Outcome)
if err != nil {
return outbound.CodeGroupResult{}, err
}
total := len(expected.Tests)
group := outbound.CodeGroupResult{
ID: expected.ID,
Visibility: expected.Visibility,
Outcome: outcome,
PassedTests: int(raw.PassedTests),
TotalTests: total,
MaxTimeMs: raw.MaxTimeMs.Or(0),
MaxMemoryKB: raw.MaxMemoryKB.Or(0),
Tests: []outbound.CodeTestResult{},
}
if compileFailed {
if outcome != outbound.CodeOutcomeCompileError || len(raw.Tests) != 0 || raw.PassedTests != 0 {
return outbound.CodeGroupResult{}, badResult("a group after a compile error ran tests")
}
return group, nil
}
tests, passed, groupOutcome, err := convertGroupTests(raw.Tests, expected)
if err != nil {
return outbound.CodeGroupResult{}, err
}
if len(tests) < total && groupOutcome == outbound.CodeOutcomeAccepted {
return outbound.CodeGroupResult{}, badResult("a group stopped before its last test without a failure")
}
if group.PassedTests != passed || group.Outcome != groupOutcome {
return outbound.CodeGroupResult{}, badResult("a group result contradicts its tests")
}
group.Tests = tests
return group, nil
}
// convertGroupTests converts the evaluated tests of a group. A group stops at
// its first failed test, so they are a prefix of the request: all accepted,
// then at most one failed. It returns the tests, the number of accepted ones,
// and the outcome they imply for the group.
func convertGroupTests(raw []api.TestResult, expected outbound.CodeTestGroup) ([]outbound.CodeTestResult, int, outbound.CodeRunOutcome, error) {
tests := make([]outbound.CodeTestResult, 0, len(raw))
passed := 0
outcome := outbound.CodeOutcomeAccepted
for index, rawTest := range raw {
if outcome != outbound.CodeOutcomeAccepted {
return nil, 0, "", badResult("a group evaluated a test after a failed one")
}
test, err := convertTest(rawTest, expected.Tests[index].ID, expected.Visibility)
if err != nil {
return nil, 0, "", err
}
if test.Outcome == outbound.CodeOutcomeCompileError {
return nil, 0, "", badResult("a test has a compile error outcome")
}
if test.Outcome == outbound.CodeOutcomeAccepted {
passed++
}
outcome = test.Outcome
tests = append(tests, test)
}
return tests, passed, outcome, nil
}
func convertTest(raw api.TestResult, expectedID string, visibility outbound.CodeTestVisibility) (outbound.CodeTestResult, error) {
if string(raw.ID) != expectedID {
return outbound.CodeTestResult{}, badResult("a test differs from the request")
}
outcome, err := convertOutcome(raw.Outcome)
if err != nil {
return outbound.CodeTestResult{}, err
}
test := outbound.CodeTestResult{
ID: expectedID,
Outcome: outcome,
TimeMs: raw.TimeMs,
WallTimeMs: raw.WallTimeMs,
MemoryKB: raw.MemoryKB,
}
if raw.ExitCode.Set {
value := raw.ExitCode.Value
test.ExitCode = &value
}
if raw.Signal.Set {
value := raw.Signal.Value
test.Signal = &value
}
if raw.Limit.Set {
test.Limit = outbound.CodeTerminationLimit(raw.Limit.Value)
}
// Hidden tests never carry program output. The runner promises it; the
// client does not pass such text on even if a faulty runner sends it.
if visibility == outbound.CodeTestPublic {
test.Diagnostic = raw.Diagnostic.Or("")
}
return test, nil
}
func badResult(reason string) error {
return fmt.Errorf("%s: %w", reason, outbound.ErrCodeRunnerBadResponse)
}