Add stage-exclusive resource waiting
Add durable FIFO capacity-one resource leases for executable stages, including cleanup-confirmed release, conservative restart quarantine, queue state events, and CLI/TUI/Android waiting presentation. Add scheduler, cleanup, protocol, client, and Android coverage plus a proposed Android v3 resource draft. Checked packaged templates and authoring skill references: neither needs updating in this item. The per-item docs note records the shared documentation that docs-fold must update; no shared README or packaged reference was edited.
This commit is contained in:
+23
-2
@@ -27,6 +27,14 @@ type AgentClient struct {
|
||||
}
|
||||
|
||||
func (c OpenCodeClient) Interrupt(ctx context.Context, id string) error {
|
||||
return c.interrupt(ctx, id, false)
|
||||
}
|
||||
|
||||
func (c OpenCodeClient) InterruptAndConfirm(ctx context.Context, id string) error {
|
||||
return c.interrupt(ctx, id, true)
|
||||
}
|
||||
|
||||
func (c OpenCodeClient) interrupt(ctx context.Context, id string, confirm bool) error {
|
||||
if !sessionIDPattern.MatchString(id) {
|
||||
return fmt.Errorf("invalid session ID")
|
||||
}
|
||||
@@ -46,7 +54,20 @@ func (c OpenCodeClient) Interrupt(ctx context.Context, id string) error {
|
||||
c.Backend = connection.Backend
|
||||
}
|
||||
_, err := c.call(ctx, "post", "/api/session/"+id+"/interrupt", map[string]any{})
|
||||
return err
|
||||
if err != nil || !confirm {
|
||||
return err
|
||||
}
|
||||
// An interrupt acknowledgement is not a cleanup receipt. Keep ownership
|
||||
// until the harness positively reports settlement, bounded by the caller.
|
||||
if c.Backend == "v1" {
|
||||
_, err := c.call(ctx, "post", "/api/experimental/session/"+id+"/wait", nil)
|
||||
return err
|
||||
}
|
||||
// The failed stage wait already records deadline evidence. Cleanup settlement
|
||||
// must still be confirmed, but must not add a second duplicate operation entry.
|
||||
confirmed := c
|
||||
confirmed.Operations = nil
|
||||
return confirmed.waitForSessionIdle(ctx, id)
|
||||
}
|
||||
|
||||
func (c *AgentClient) Generate(ctx context.Context, config ModelConfig, prompt string) (text string, err error) {
|
||||
@@ -100,7 +121,7 @@ func (c *AgentClient) Generate(ctx context.Context, config ModelConfig, prompt s
|
||||
defer cancel()
|
||||
if err != nil {
|
||||
c.captureTimedOutEvidence(ctx, err, config, watch)
|
||||
if stopErr := c.Interrupt(cleanup, c.SessionID); stopErr != nil {
|
||||
if stopErr := c.InterruptAndConfirm(cleanup, c.SessionID); stopErr != nil {
|
||||
err = fmt.Errorf("agent interrupt failed; retained session %s needs inspection: %w", c.SessionID, errors.Join(err, stopErr, ErrStageCleanup))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -52,8 +52,8 @@ func TestAgentOwnershipPermissionsAndInterrupt(t *testing.T) {
|
||||
t.Fatal("unexpected call", args)
|
||||
return nil, nil
|
||||
}
|
||||
if _, err := client.Generate(context.Background(), ModelConfig{}, "This call has no tools."); err == nil || !interrupted || owned {
|
||||
t.Fatal("failed generation did not stop and release ownership")
|
||||
if _, err := client.Generate(context.Background(), ModelConfig{}, "This call has no tools."); !errors.Is(err, ErrStageCleanup) || !interrupted || !owned {
|
||||
t.Fatal("interrupt acknowledgement without idle confirmation must retain ownership")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -82,6 +82,9 @@ func TestBudgetsAccumulateAcrossRetriesAndBlockFurtherGeneration(t *testing.T) {
|
||||
var sessions, generations atomic.Int64
|
||||
client := &OpenCodeClient{command: func(ctx context.Context, _ string, args ...string) ([]byte, error) {
|
||||
path := args[2]
|
||||
if path == "/api/session/active" {
|
||||
return []byte(`{"data":{}}`), nil
|
||||
}
|
||||
if path == "/api/session" {
|
||||
return []byte(fmt.Sprintf(`{"data":{"id":"ses_attempt_%d"}}`, sessions.Add(1))), nil
|
||||
}
|
||||
@@ -147,6 +150,9 @@ func TestLiveAgentBudgetInterruptsAndDoesNotReplay(t *testing.T) {
|
||||
return []byte(`{"data":{"id":"ses_agent_budget"}}`), nil
|
||||
}
|
||||
if path == "/api/session/active" {
|
||||
if interrupts.Load() > 0 {
|
||||
return []byte(`{"data":{}}`), nil
|
||||
}
|
||||
<-ctx.Done()
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
|
||||
@@ -321,7 +321,7 @@ func (c *OpenCodeClient) Generate(ctx context.Context, config ModelConfig, promp
|
||||
cleanup, cancel := context.WithTimeout(context.WithoutCancel(ctx), 10*time.Second)
|
||||
defer cancel()
|
||||
if err != nil {
|
||||
if stopErr := c.Interrupt(cleanup, session.Data.ID); stopErr != nil {
|
||||
if stopErr := c.InterruptAndConfirm(cleanup, session.Data.ID); stopErr != nil {
|
||||
err = fmt.Errorf("retained OpenCode session %s; interrupt failed: %w", session.Data.ID, errors.Join(err, stopErr, ErrStageCleanup))
|
||||
return
|
||||
}
|
||||
|
||||
+13
-2
@@ -238,6 +238,10 @@ func (c *PiClient) Generate(ctx context.Context, config ModelConfig, prompt stri
|
||||
}
|
||||
close(done)
|
||||
close(heartbeatDone)
|
||||
// agent_end/abort acknowledgement and the parent's exit alone do not
|
||||
// prove its shell descendants exited. A surviving process group retains
|
||||
// ownership rather than handing the resource to another stage.
|
||||
quiet = quiet && errors.Is(syscall.Kill(-c.cmd.Process.Pid, 0), syscall.ESRCH)
|
||||
if quiet {
|
||||
_ = os.WriteFile(filepath.Join(c.Reference, "quiescent"), []byte("quiescent"), 0600)
|
||||
}
|
||||
@@ -532,9 +536,16 @@ func InterruptPi(ctx context.Context, reference string) error {
|
||||
if !filepath.IsAbs(reference) {
|
||||
return fmt.Errorf("invalid retained Pi reference")
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(reference, "owner.json")); err != nil {
|
||||
data, err := os.ReadFile(filepath.Join(reference, "owner.json"))
|
||||
if err != nil {
|
||||
return errors.Join(err, ErrStageCleanup)
|
||||
}
|
||||
var owner struct {
|
||||
PID int `json:"pid"`
|
||||
}
|
||||
if json.Unmarshal(data, &owner) != nil || owner.PID <= 1 {
|
||||
return errors.Join(fmt.Errorf("invalid retained Pi owner"), ErrStageCleanup)
|
||||
}
|
||||
old := time.Unix(0, 0)
|
||||
if err := os.Chtimes(filepath.Join(reference, "lease"), old, old); err != nil {
|
||||
return err
|
||||
@@ -542,7 +553,7 @@ func InterruptPi(ctx context.Context, reference string) error {
|
||||
ticker := time.NewTicker(100 * time.Millisecond)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
if data, err := os.ReadFile(filepath.Join(reference, "quiescent")); err == nil && string(data) == "quiescent" {
|
||||
if data, err := os.ReadFile(filepath.Join(reference, "quiescent")); err == nil && string(data) == "quiescent" && errors.Is(syscall.Kill(owner.PID, 0), syscall.ESRCH) && errors.Is(syscall.Kill(-owner.PID, 0), syscall.ESRCH) {
|
||||
return nil
|
||||
}
|
||||
select {
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package stage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestResourcePiReceiptRequiresProcessExit(t *testing.T) {
|
||||
// A test-owned process only: no Pi provider, shell/ADB command or phone.
|
||||
cmd := exec.Command("sleep", "5")
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
||||
if err := cmd.Start(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() { cmd.Process.Kill(); cmd.Wait() }()
|
||||
dir := t.TempDir()
|
||||
owner, _ := json.Marshal(map[string]any{"pid": cmd.Process.Pid})
|
||||
for name, data := range map[string][]byte{"owner.json": owner, "lease": []byte("lease"), "quiescent": []byte("quiescent")} {
|
||||
if err := os.WriteFile(filepath.Join(dir, name), data, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
|
||||
defer cancel()
|
||||
if err := InterruptPi(ctx, dir); !errors.Is(err, ErrStageCleanup) {
|
||||
t.Fatal("receipt authorized handoff while process lived", err)
|
||||
}
|
||||
if err := cmd.Process.Kill(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cmd.Wait()
|
||||
ctx2, cancel2 := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel2()
|
||||
if err := InterruptPi(ctx2, dir); err != nil {
|
||||
t.Fatal("confirmed process exit not accepted", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user