| 1 |
package clnkr |
| 2 |
|
| 3 |
import "strings" |
| 4 |
|
| 5 |
const CompletionAccept, CompletionReject, CompletionChallenge = "accept", "reject", "challenge" |
| 6 |
|
| 7 |
type CompletionGate struct{ challengesUsed int } |
| 8 |
|
| 9 |
func (g *CompletionGate) Decide( |
| 10 |
done *DoneTurn, |
| 11 |
commandsUsed, maxSteps int, |
| 12 |
) (string, []string, string) { |
| 13 |
if reasons := completionRejectReasons(done, commandsUsed, maxSteps); len(reasons) > 0 { |
| 14 |
return CompletionReject, reasons, completionRejectGuidance(reasons[0]) |
| 15 |
} |
| 16 |
if reasons := completionChallengeReasons(done, commandsUsed); len(reasons) > 0 { |
| 17 |
if g.challengesUsed > 0 { |
| 18 |
return CompletionAccept, []string{"challenge_limit_reached"}, "" |
| 19 |
} |
| 20 |
g.challengesUsed++ |
| 21 |
return CompletionChallenge, reasons, completionChallengeGuidance() |
| 22 |
} |
| 23 |
return CompletionAccept, nil, "" |
| 24 |
} |
| 25 |
|
| 26 |
func completionRejectReasons(done *DoneTurn, commandsUsed, maxSteps int) []string { |
| 27 |
if done == nil { |
| 28 |
return []string{"missing_done_turn"} |
| 29 |
} |
| 30 |
var reasons []string |
| 31 |
reasons = appendReason(reasons, strings.TrimSpace(done.Summary) == "", "empty_summary") |
| 32 |
reasons = appendReason( |
| 33 |
reasons, |
| 34 |
containsAny(strings.ToLower(done.Summary), incompleteSummaryPhrases), |
| 35 |
"incomplete_summary", |
| 36 |
) |
| 37 |
switch done.Verification.Status { |
| 38 |
case VerificationVerified: |
| 39 |
reasons = appendReason( |
| 40 |
reasons, |
| 41 |
len(done.Verification.Checks) == 0, |
| 42 |
"verified_without_checks", |
| 43 |
) |
| 44 |
case VerificationPartiallyVerified: |
| 45 |
reasons = appendReason(reasons, len(done.KnownRisks) == 0, "partial_without_risks") |
| 46 |
case VerificationNotVerified: |
| 47 |
reasons = appendReason( |
| 48 |
reasons, |
| 49 |
maxSteps <= 0 || commandsUsed < maxSteps, |
| 50 |
"not_verified_with_budget_remaining", |
| 51 |
) |
| 52 |
default: |
| 53 |
reasons = append(reasons, "invalid_verification_status") |
| 54 |
} |
| 55 |
return reasons |
| 56 |
} |
| 57 |
|
| 58 |
var incompleteSummaryPhrases = []string{ |
| 59 |
"protocol correction", |
| 60 |
"cannot proceed", |
| 61 |
"need to continue", |
| 62 |
"ready to run", |
| 63 |
"no file changes have been made", |
| 64 |
"need create", |
| 65 |
} |
| 66 |
|
| 67 |
type completionChallengeRule struct { |
| 68 |
claims, checks []string |
| 69 |
reason string |
| 70 |
} |
| 71 |
|
| 72 |
var completionChallengeRules = []completionChallengeRule{ |
| 73 |
{ |
| 74 |
[]string{"created ", "wrote ", "saved ", "/", ".go", ".md", ".json", ".txt"}, |
| 75 |
[]string{"test -f", "cat ", "ls ", "stat ", "grep ", "exists", "contains"}, |
| 76 |
"artifact_claim_without_check", |
| 77 |
}, |
| 78 |
{ |
| 79 |
[]string{"server", "service", "daemon", "listening"}, |
| 80 |
[]string{"curl ", "nc ", "ss ", "lsof ", "listening", "responded"}, |
| 81 |
"service_claim_without_liveness_check", |
| 82 |
}, |
| 83 |
{ |
| 84 |
[]string{"implemented", "fixed", "updated", "changed"}, |
| 85 |
[]string{"test", "go test", "pytest", "npm test", "make check", "passed"}, |
| 86 |
"implementation_claim_without_behavior_check", |
| 87 |
}, |
| 88 |
} |
| 89 |
|
| 90 |
func completionChallengeReasons(done *DoneTurn, commandsUsed int) []string { |
| 91 |
if done == nil { |
| 92 |
return nil |
| 93 |
} |
| 94 |
summary := strings.ToLower(done.Summary) |
| 95 |
checks := checksText(done.Verification.Checks) |
| 96 |
var reasons []string |
| 97 |
for _, rule := range completionChallengeRules { |
| 98 |
if containsAny(summary, rule.claims) && !containsAny(checks, rule.checks) { |
| 99 |
reasons = append(reasons, rule.reason) |
| 100 |
} |
| 101 |
} |
| 102 |
if commandsUsed == 0 && thinChecks(done.Verification.Checks) { |
| 103 |
reasons = append(reasons, "early_completion_thin_evidence") |
| 104 |
} |
| 105 |
return reasons |
| 106 |
} |
| 107 |
|
| 108 |
func checksText(checks []VerificationCheck) string { |
| 109 |
var b strings.Builder |
| 110 |
for _, check := range checks { |
| 111 |
b.WriteString(check.Command) |
| 112 |
b.WriteByte(' ') |
| 113 |
b.WriteString(check.Evidence) |
| 114 |
b.WriteByte(' ') |
| 115 |
} |
| 116 |
return strings.ToLower(b.String()) |
| 117 |
} |
| 118 |
|
| 119 |
func containsAny(haystack string, needles []string) bool { |
| 120 |
for _, needle := range needles { |
| 121 |
if strings.Contains(haystack, needle) { |
| 122 |
return true |
| 123 |
} |
| 124 |
} |
| 125 |
return false |
| 126 |
} |
| 127 |
|
| 128 |
func appendReason(reasons []string, ok bool, reason string) []string { |
| 129 |
if ok { |
| 130 |
return append(reasons, reason) |
| 131 |
} |
| 132 |
return reasons |
| 133 |
} |
| 134 |
|
| 135 |
func thinChecks(checks []VerificationCheck) bool { |
| 136 |
if len(checks) != 1 { |
| 137 |
return len(checks) == 0 |
| 138 |
} |
| 139 |
check := checks[0] |
| 140 |
combined := strings.TrimSpace(check.Command + " " + check.Outcome + " " + check.Evidence) |
| 141 |
return len(combined) < 40 || strings.EqualFold(strings.TrimSpace(check.Command), "true") |
| 142 |
} |
| 143 |
|
| 144 |
func completionChallengeGuidance() string { |
| 145 |
return "Completion challenged: verification evidence is too thin.\nRun one cheap check of the exact deliverable: file path/content, executable behavior, API signature, service liveness, or numeric threshold. If no meaningful check is possible, finish with partially_verified and list known_risks." |
| 146 |
} |
| 147 |
|
| 148 |
func completionRejectGuidance(reason string) string { |
| 149 |
return "Completion rejected: " + reason + ".\nRun a concrete verification check before finishing, or finish with partially_verified and known_risks when full verification is impossible." |
| 150 |
} |