Repository navigation
fix(harnesswire): prepare Connect before journaling #282
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
Anish Ramasekar (aramase)
merged 5 commits into
agent-primitives:main
from
Tatsinnit:fix/276-connect-preflight
Nov 17, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b0627ed
fix(harnesswire): prepare Connect before journaling
Tatsinnit 58d4147
fix(harnesswire): cancel sink calls on close
Tatsinnit 90ab5fa
fix(harnesswire): preserve completion during close
Tatsinnit 0f78259
docs(harnesswire): clarify preparation boundary
Tatsinnit 29427e0
chore(harnesswire): satisfy lint and compatibility notes
Tatsinnit 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
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,10 @@ | ||
| --- | ||
| category: Fixed | ||
| --- | ||
|
|
||
| - A new `Exec` over `Harness.Connect` now opens its actual stream before recording | ||
| `EXECUTION_START`. If Connect cannot be opened, no execution records are appended and retrying the | ||
| same session starts its first turn normally. The host still sends `Start` only after the start and | ||
| every input are durable; cancellation after successful preparation and send or receive failures | ||
| remain uncertain and keep any recorded attempt in later history. Go harness adapters can opt into | ||
| the same boundary with `api.HarnessRunPreparer` (`EXE-45`). | ||
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,9 @@ | ||
| --- | ||
| category: Added | ||
| --- | ||
|
|
||
| - Go harness adapters can implement `api.HarnessRunPreparer` to acquire the transport for one | ||
| execution before it is recorded, without invoking the harness or sending `Start`. The returned | ||
| `api.PreparedHarnessRun` is used at most once or closed; a concurrent close either prevents Run | ||
| or cancels it. `harnesswire.ClientHarness` implements the extension for its `Harness.Connect` | ||
| stream (`EXE-45`). |
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,12 @@ | ||
| --- | ||
| category: Compatibility | ||
| --- | ||
|
|
||
| - `Harness.Connect` now fails an execution for four wire shapes the host previously accepted: clean | ||
| closure without `EVENT_END`; harness-sent `EVENT_INPUT`, `EVENT_LIFECYCLE`, `EVENT_ERROR`, or an | ||
| unspecified/unknown kind; any frame after `EVENT_END`; and a harness-owned non-OK final RPC status | ||
| after `EVENT_END{COMPLETED}` while the caller remains live. Caller cancellation or deadline during | ||
| the bounded post-END telemetry flush does not replace a completed harness result (`WIRE-12`). | ||
| - A new `Exec` whose Connect preparation fails with `UNAVAILABLE` or `DEADLINE_EXCEEDED` while the | ||
| caller remains live now returns `UNAVAILABLE` with `agentsessions/HARNESS_UNAVAILABLE` and fixed | ||
| host text. Before this change it returned `INTERNAL` with the raw Connect diagnostic. |
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,186 @@ | ||
| package controller_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "testing" | ||
|
|
||
| "github.com/agent-primitives/sessions/api" | ||
| "github.com/agent-primitives/sessions/controller" | ||
| "github.com/agent-primitives/sessions/eventlog" | ||
| ) | ||
|
|
||
| type preparedRunHarness struct { | ||
| executionConfigHarness | ||
| prepareErr error | ||
| prepareValueOnErr bool | ||
| prepare, runs, closes int | ||
| beforeRun func() | ||
| runErr error | ||
| } | ||
|
|
||
| func (h *preparedRunHarness) PrepareRun(context.Context) (api.PreparedHarnessRun, error) { | ||
| h.prepare++ | ||
| if h.prepareErr != nil { | ||
| if h.prepareValueOnErr { | ||
| return &testPreparedRun{h: h}, h.prepareErr | ||
| } | ||
| return nil, h.prepareErr | ||
| } | ||
| return &testPreparedRun{h: h}, nil | ||
| } | ||
|
|
||
| type testPreparedRun struct { | ||
| h *preparedRunHarness | ||
| used bool | ||
| closed bool | ||
| } | ||
|
|
||
| func (r *testPreparedRun) Run(ctx context.Context, start *api.Start, sink api.EventSink) error { | ||
| if r.used { | ||
| return errors.New("prepared run reused") | ||
| } | ||
| r.used = true | ||
| r.h.runs++ | ||
| if r.h.beforeRun != nil { | ||
| r.h.beforeRun() | ||
| } | ||
| if r.h.runErr != nil { | ||
| return r.h.runErr | ||
| } | ||
| return r.h.Run(ctx, start, sink) | ||
| } | ||
|
|
||
| func (r *testPreparedRun) Close() { | ||
| if !r.closed { | ||
| r.closed = true | ||
| r.h.closes++ | ||
| } | ||
| } | ||
|
|
||
| func TestExecPreparesHarnessBeforeFirstAppend(t *testing.T) { | ||
| log := eventlog.AsStore(eventlog.New()) | ||
| c, err := controller.New(log, echoModel) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| h := &preparedRunHarness{} | ||
| h.beforeRun = func() { | ||
| recs, err := log.Read(1) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if len(recs) != 2 || recs[0].Event.Kind != api.EventExecutionStart || recs[1].Event.Kind != api.EventInput { | ||
| t.Fatalf("prepared Run saw records %+v, want committed start and input", recs) | ||
| } | ||
| } | ||
| if err := c.Exec(t.Context(), h, []api.Message{msg("hello")}, 0); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if h.prepare != 1 || h.runs != 1 || h.closes != 1 { | ||
| t.Fatalf("prepare/run/close=%d/%d/%d, want 1/1/1", h.prepare, h.runs, h.closes) | ||
| } | ||
| } | ||
|
|
||
| func TestExecPrepareFailureLeavesJournalUntouched(t *testing.T) { | ||
| log := eventlog.AsStore(eventlog.New()) | ||
| c, err := controller.New(log, echoModel) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| cause := errors.New("connect failed before Start") | ||
| h := &preparedRunHarness{prepareErr: cause} | ||
| err = c.Exec(t.Context(), h, []api.Message{msg("hello")}, 0) | ||
| if !errors.Is(err, controller.ErrHarnessPrepareFailed) || !errors.Is(err, cause) { | ||
| t.Fatalf("Exec=%v, want preparation failure retaining %v", err, cause) | ||
| } | ||
| if head, headErr := log.Head(); headErr != nil || head != 0 { | ||
| t.Fatalf("head=%d err=%v, want untouched journal", head, headErr) | ||
| } | ||
| if h.prepare != 1 || h.runs != 0 || h.closes != 0 { | ||
| t.Fatalf("prepare/run/close=%d/%d/%d, want 1/0/0", h.prepare, h.runs, h.closes) | ||
| } | ||
| } | ||
|
|
||
| func TestExecDefensivelyClosesPreparedValueReturnedWithError(t *testing.T) { | ||
| log := eventlog.AsStore(eventlog.New()) | ||
| c, err := controller.New(log, echoModel) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| cause := errors.New("prepare failed after allocation") | ||
| h := &preparedRunHarness{prepareErr: cause, prepareValueOnErr: true} | ||
| err = c.Exec(t.Context(), h, nil, 0) | ||
| if !errors.Is(err, controller.ErrHarnessPrepareFailed) || !errors.Is(err, cause) { | ||
| t.Fatalf("Exec=%v, want preparation failure retaining %v", err, cause) | ||
| } | ||
| if h.prepare != 1 || h.runs != 0 || h.closes != 1 { | ||
| t.Fatalf("prepare/run/close=%d/%d/%d, want 1/0/1", h.prepare, h.runs, h.closes) | ||
| } | ||
| } | ||
|
|
||
| func TestExecClosesPreparedRunWhenAppendFails(t *testing.T) { | ||
| cause := errors.New("append failed") | ||
| log := rejectAppendStore{Store: eventlog.AsStore(eventlog.New()), err: cause} | ||
| c, err := controller.New(log, echoModel) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| h := &preparedRunHarness{} | ||
| err = c.Exec(t.Context(), h, []api.Message{msg("hello")}, 0) | ||
| if !errors.Is(err, cause) || errors.Is(err, controller.ErrHarnessPrepareFailed) { | ||
| t.Fatalf("Exec=%v, want append failure %v", err, cause) | ||
| } | ||
| if h.prepare != 1 || h.runs != 0 || h.closes != 1 { | ||
| t.Fatalf("prepare/run/close=%d/%d/%d, want 1/0/1", h.prepare, h.runs, h.closes) | ||
| } | ||
| } | ||
|
|
||
| func TestExecClosesPreparedRunWhenInputAppendFails(t *testing.T) { | ||
| cause := errors.New("input append failed") | ||
| base := eventlog.AsStore(eventlog.New()) | ||
| log := &failInputStore{Store: base, failAt: 2, err: cause} | ||
| c, err := controller.New(log, echoModel) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| h := &preparedRunHarness{} | ||
| err = c.Exec(t.Context(), h, []api.Message{msg("one"), msg("two")}, 0) | ||
| if !errors.Is(err, cause) || errors.Is(err, controller.ErrHarnessPrepareFailed) { | ||
| t.Fatalf("Exec=%v, want input append failure %v", err, cause) | ||
| } | ||
| if h.prepare != 1 || h.runs != 0 || h.closes != 1 { | ||
| t.Fatalf("prepare/run/close=%d/%d/%d, want 1/0/1", h.prepare, h.runs, h.closes) | ||
| } | ||
| recs, readErr := base.Read(1) | ||
| if readErr != nil { | ||
| t.Fatal(readErr) | ||
| } | ||
| if len(recs) != 2 || recs[0].Event.Kind != api.EventExecutionStart || recs[1].Event.Kind != api.EventInput { | ||
| t.Fatalf("records=%+v, want existing incomplete invocation", recs) | ||
| } | ||
| } | ||
|
|
||
| func TestExecPreparedRunFailureRemainsDurable(t *testing.T) { | ||
| log := eventlog.AsStore(eventlog.New()) | ||
| c, err := controller.New(log, echoModel) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| cause := errors.New("Start send outcome unknown") | ||
| h := &preparedRunHarness{runErr: cause} | ||
| err = c.Exec(t.Context(), h, []api.Message{msg("hello")}, 0) | ||
| if !errors.Is(err, cause) || errors.Is(err, controller.ErrHarnessPrepareFailed) { | ||
| t.Fatalf("Exec=%v, want Run failure %v without preparation marker", err, cause) | ||
| } | ||
| recs, readErr := log.Read(1) | ||
| if readErr != nil { | ||
| t.Fatal(readErr) | ||
| } | ||
| if len(recs) != 3 || recs[0].Event.Kind != api.EventExecutionStart || recs[1].Event.Kind != api.EventInput || recs[2].Event.Kind != api.EventError { | ||
| t.Fatalf("records=%+v, want durable failed execution", recs) | ||
| } | ||
| if h.prepare != 1 || h.runs != 1 || h.closes != 1 { | ||
| t.Fatalf("prepare/run/close=%d/%d/%d, want 1/1/1", h.prepare, h.runs, h.closes) | ||
| } | ||
| } |
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.