-
Notifications
You must be signed in to change notification settings - Fork 0
feat(relay/delivery): delivery, intent, admission, reconcile and recover (todo 21, CRW-153) #175
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’ll 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
18c6b3c
feat(relay/delivery): delivery, intent, admission, reconcile and recover
thisisjun786 820c110
fix(relay/delivery): race-free anchor binding, recover acknowledged a…
thisisjun786 00db632
Merge branch 'dev' into codex/crw-153-delivery
thisisjun786 106b4eb
Merge dev into codex/crw-153-delivery (registry + delivery dispatch b…
thisisjun786 5a8d455
fix(relay/delivery): reconcile gate fingerprint includes the receipt …
thisisjun786 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,6 @@ | ||
| # Python defects not carried over | ||
|
|
||
| - **Python defect not carried over:** `daemon._gate` fingerprint omits the receipt turn id (`daemon.py:~993`), so an accepted receipt that gains a turn id later is never reconciled; Go includes it (`TestReconcilePass_receipt_gains_turn_id`). | ||
|
|
||
| - **Python defect not carried over:** `registry.bind_anchor` (`registry.py:790-822`) reads the pending anchor before beginning its transaction. Concurrent binders can overwrite a generation's dispatch turn after one has already succeeded, violating the never-rebind invariant. Go reads and decides inside the write transaction; `TestBindAnchor_concurrent_turns_never_replace_the_winner` proves only one distinct turn binds and the loser is refused. | ||
| - **Python defect not carried over:** `ack.bind_dispatched_revision` (`ack.py:1026-1035`) rejects acknowledged revisions even though `ack.bind_pending_anchors` (`ack.py:1037-1060`) selects them for recovery. This violates the invariant that every dispatched revision's pending anchor can recover after acknowledgement. Go accepts both states; `TestBindPendingAnchors_recovers_acknowledged_revision` proves the recovery binds and is idempotent. |
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,157 @@ | ||
| package cli_test | ||
|
|
||
| import ( | ||
| "database/sql" | ||
| "encoding/json" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "reflect" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| _ "modernc.org/sqlite" | ||
| ) | ||
|
|
||
| type amaCapture struct { | ||
| Captures []any `json:"captures"` | ||
| Problems []string `json:"problems"` | ||
| } | ||
|
|
||
| func amaTree(t *testing.T, method string) (string, amaCapture) { | ||
| t.Helper() | ||
| amaRoot := t.TempDir() | ||
| home := t.TempDir() | ||
| cmd := exec.Command("uv", "run", "--no-sync", "python", filepath.Join(repositoryRoot(t), "internal/relay/delivery/testdata/capture.py"), amaRoot, "test_attempt_message_atomicity") | ||
| cmd.Dir = filepath.Join(repositoryRoot(t), "packages/codex-session-relay") | ||
| cmd.Env = append(os.Environ(), "HOME="+home, "XDG_STATE_HOME="+filepath.Join(home, "state"), "XDG_DATA_HOME="+filepath.Join(home, "data"), "XDG_CONFIG_HOME="+filepath.Join(home, "config"), "CODEX_HOME="+filepath.Join(home, "codex"), "TMPDIR="+home, "PYTHONPATH="+filepath.Join(repositoryRoot(t), "packages/codex-session-relay/src")+":"+filepath.Join(repositoryRoot(t), "packages/codex-session-relay")) | ||
| if output, err := cmd.CombinedOutput(); err != nil { | ||
| t.Fatal(&captureFailure{err, string(output)}) | ||
| } | ||
| tree := filepath.Join(amaRoot, "AttemptMessageAtomicity."+method) | ||
| raw, err := os.ReadFile(filepath.Join(tree, "capture.json")) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| var captured amaCapture | ||
| if err := json.Unmarshal(raw, &captured); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if len(captured.Problems) > 0 { | ||
| t.Fatal(captured.Problems) | ||
| } | ||
| return tree, captured | ||
| } | ||
|
|
||
| type captureFailure struct { | ||
| err error | ||
| output string | ||
| } | ||
|
|
||
| func (e *captureFailure) Error() string { return e.err.Error() + "\n" + e.output } | ||
| func amaShow(t *testing.T, tree string, event string) map[string]any { | ||
| t.Helper() | ||
| home := pythonHome(t) | ||
| state := filepath.Join(tree, "state") | ||
| result := golang(t, home, "--state", state, "show", "--event", event, "--message") | ||
| if result.code != 0 { | ||
| t.Fatalf("show: %+v", result) | ||
| } | ||
| return decode(t, result.stdout) | ||
| } | ||
| func amaEvent(t *testing.T, tree string) string { | ||
| t.Helper() | ||
| // The Python case has exactly one event. Read its ID from its captured store. | ||
| state := filepath.Join(tree, "state", "relay.sqlite3") | ||
| db, err := sql.Open("sqlite", state) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer db.Close() | ||
| var event string | ||
| if err := db.QueryRow("SELECT event_id FROM events").Scan(&event); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| return event | ||
| } | ||
| func amaTokenCLI(t *testing.T, value any) string { | ||
| t.Helper() | ||
| for _, line := range strings.Split(value.(string), "\n") { | ||
| if strings.HasPrefix(line, "requestId: ") { | ||
| return strings.TrimPrefix(line, "requestId: ") | ||
| } | ||
| } | ||
| t.Fatal("message carries no requestId") | ||
| return "" | ||
| } | ||
| func amaCompare(t *testing.T, got, want []any) { | ||
| t.Helper() | ||
| if !reflect.DeepEqual(got, want) { | ||
| t.Fatalf("assertions Go=%v Python=%v", got, want) | ||
| } | ||
| } | ||
| func Test21_AMA4_show_message_returns_the_sent_attempt_after_a_send(t *testing.T) { | ||
| tree, py := amaTree(t, "test_show_message_returns_the_sent_attempt_after_a_send") | ||
| payload := amaShow(t, tree, amaEvent(t, tree)) | ||
| entries := payload["attemptMessages"].([]any) | ||
| e := entries[0].(map[string]any) | ||
| _, preview := payload["previewMessage"] | ||
| amaCompare(t, []any{float64(len(entries)), e["requestId"], e["status"], amaTokenCLI(t, e["message"]), preview}, py.Captures) | ||
| } | ||
| func Test21_AMA5_show_message_offers_a_preview_only_before_anything_is_prepared(t *testing.T) { | ||
| tree, py := amaTree(t, "test_show_message_offers_a_preview_only_before_anything_is_prepared") | ||
| event := amaEvent(t, tree) | ||
| // Rewind a copy of the Python fixture to the queued, unprepared state. The | ||
| // original remains intact for the after-send command. | ||
| before := t.TempDir() | ||
| raw, err := os.ReadFile(filepath.Join(tree, "state", "relay.sqlite3")) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := os.WriteFile(filepath.Join(before, "relay.sqlite3"), raw, 0600); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| db, err := sql.Open("sqlite", filepath.Join(before, "relay.sqlite3")) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| for _, statement := range []string{"DELETE FROM attempt_messages", "DELETE FROM attempts", "UPDATE deliveries SET attempt_count = 0, state = 'queued', next_eligible_at = NULL"} { | ||
| if _, err := db.Exec(statement); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } | ||
| if err := db.Close(); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| home := pythonHome(t) | ||
| prior := golang(t, home, "--state", before, "show", "--event", event, "--message") | ||
| if prior.code != 0 { | ||
| t.Fatalf("before show: %+v", prior) | ||
| } | ||
| pre := decode(t, prior.stdout) | ||
| preview, present := pre["previewMessage"] | ||
| got := []any{pre["attemptMessages"], present, strings.HasSuffix(amaTokenCLI(t, preview), "-a1")} | ||
| payload := amaShow(t, tree, event) | ||
| entries := payload["attemptMessages"].([]any) | ||
| _, afterPreview := payload["previewMessage"] | ||
| got = append(got, afterPreview, eRequest(entries)) | ||
| amaCompare(t, got, py.Captures) | ||
| } | ||
| func eRequest(entries []any) any { return entries[0].(map[string]any)["requestId"] } | ||
| func Test21_AMA6_show_message_after_a_retry_lists_both_attempts_distinctly(t *testing.T) { | ||
| tree, py := amaTree(t, "test_show_message_after_a_retry_lists_both_attempts_distinctly") | ||
| payload := amaShow(t, tree, amaEvent(t, tree)) | ||
| entries := payload["attemptMessages"].([]any) | ||
| ids, statuses := []any{}, []any{} | ||
| for _, item := range entries { | ||
| e := item.(map[string]any) | ||
| ids = append(ids, e["requestId"]) | ||
| statuses = append(statuses, e["status"]) | ||
| } | ||
| got := []any{ids, statuses} | ||
| for _, item := range entries { | ||
| e := item.(map[string]any) | ||
| got = append(got, amaTokenCLI(t, e["message"])) | ||
| } | ||
| amaCompare(t, got, py.Captures) | ||
| } |
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
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.