perf(httpclient): remember the server's external-dependency policy across processes (#7700)
* perf(httpclient): remember the server's external-dependency policy across processes Every bd ready over an HTTP workspace fetched GET /v0/beads/context before its listing. listReadyWork itself needs no handshake; the fetch came from the external-dependency decorator asking ServerEnforcesExternalDependencyPolicy whether the server already applies the policy. In a one-shot CLI that is one extra round trip per process (about 54 ms at a 52 ms RTT) to relearn a bit that only changes when the server is restarted with a different composition. The store opened from a workspace now keeps that answer in the user cache directory (beads/http-policy/), keyed by the server URL and the project the workspace pinned at bd connect, for BEADS_HTTP_POLICY_CACHE_TTL (5 minutes by default; off disables it). A fresh entry answers the probe without dialing; a handshake this process already holds wins and refreshes the entry. A workspace with no project pin, and a store an embedder opens through OpenWith, are not cached. Writing the entry is best effort. The staleness this allows is bounded by the TTL: a server restarted without the policy within the window is still trusted to enforce it until the entry ages out. Carrying the policy on every response would close that window but needs a server change and does nothing for servers already deployed. Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of gascity-agent Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * fix(httpclient): claims and closes ask the server for the policy, never the cache Review follow-up to the policy cache. The claim and close guards (direct claim, claim-next, batch close, the lifecycle close and claim, and the legacy ClaimReadyIssue and CloseIssueChecked) handshake anyway, and a stale cached "the server enforces it" would stand down their client-side refusal of externally blocked work. Those entry points now mark their context with storage.WithLivePolicyProbe, and httpclient.Store answers such a probe from the server, refreshing the cache entry. Reads keep the cached answer. Tests: externaldeps TestClaimsAndClosesAskForALivePolicyProbe and TestRemoteClaimsAndClosesAskForALivePolicyProbe pin which entry points probe live (every claim and close) and which do not (ready listings), over a local and a remote store; httpclient TestALiveProbeSkipsThePolicyCache pins the store side; TestPolicyCacheRefusesEntriesItCannotTrust now also covers an entry written for another server URL. Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of gascity-agent Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
d097b6c8a4
commit
2563f021ed
15 changed files with 504 additions and 2 deletions
|
|
@ -510,6 +510,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
initialization triggers do not stop the world across every CPU of a large
|
||||
host. On a 192-CPU machine this takes about 17 ms off every bd process. An
|
||||
explicit `GOMAXPROCS` is restored as set.
|
||||
- Over an HTTP workspace, `bd ready` no longer fetches `GET /v0/beads/context`
|
||||
before its listing in every process only to learn whether the server
|
||||
enforces the external-dependency policy. The answer is kept for 5 minutes
|
||||
per server URL and pinned project under the user cache directory
|
||||
(`BEADS_HTTP_POLICY_CACHE_TTL`, a duration, a number of seconds, or `off`),
|
||||
and a handshake the process already holds still wins. Claims and closes
|
||||
always ask the server.
|
||||
- `bd serve` normalizes every RFC 3339 time bound on the issue listing and
|
||||
count (`created_*`, `updated_*`, `closed_*`, and the new `defer_*` and
|
||||
`due_*`) to UTC before it reaches storage. A bound carrying an offset
|
||||
|
|
|
|||
11
README.md
11
README.md
|
|
@ -282,6 +282,17 @@ the host name that did not resolve. `off` turns off only the
|
|||
header timer: a reset read is still retried, and a write's timeout is still
|
||||
reported as above.
|
||||
|
||||
Whether the server applies bd's external-dependency policy itself (the
|
||||
`policy.external_dependencies` capability) is remembered for 5 minutes per
|
||||
server URL and pinned project, in `beads/http-policy/` under the user cache
|
||||
directory, so `bd ready` does not fetch the server's context before every
|
||||
listing just to learn it. `BEADS_HTTP_POLICY_CACHE_TTL` sets that window (a
|
||||
duration such as `1m`, a number of seconds, or `off`); a server restarted
|
||||
with a different composition is seen once it passes. Claims and closes never
|
||||
use the cached answer: they ask the server, since a stale one could skip the
|
||||
client-side refusal of externally blocked work. A workspace connected without
|
||||
a project pin is not cached.
|
||||
|
||||
`bd search`, `bd show --current` and `bd recompute-blocked` work over HTTP
|
||||
too, each in its own way. `bd search` asks the server's listing with its text (`q`, behind the `issues.list.search` capability) and
|
||||
refuses before dialing against a server that does not advertise it. `bd list
|
||||
|
|
|
|||
|
|
@ -58,6 +58,10 @@ func TestE2E_PerCommandRequestBudget(t *testing.T) {
|
|||
{"ready --limit 1", 2, []string{"ready", "--json", "--limit", "1"}},
|
||||
{"list", 4, []string{"list", "--json"}},
|
||||
{"show", 1, []string{"show", target, "--json"}},
|
||||
// The first ready learned the server's external-dependency
|
||||
// policy from the handshake; a later process reads it from the
|
||||
// policy cache instead of fetching the context again.
|
||||
{"ready, policy cached", 1, []string{"ready", "--json"}},
|
||||
{"create", 2, []string{"create", "budget created", "-t", "task", "--json"}},
|
||||
{"update --append-notes", 3, []string{"update", target, "--append-notes", "more", "--json"}},
|
||||
{"update --status", 3, []string{"update", target, "--status", "in_progress", "--json"}},
|
||||
|
|
|
|||
|
|
@ -41,6 +41,7 @@ go_library(
|
|||
"offrole.go",
|
||||
"open_with.go",
|
||||
"order.go",
|
||||
"policy_cache.go",
|
||||
"readyclaimer.go",
|
||||
"refusal.go",
|
||||
"register.go",
|
||||
|
|
@ -67,6 +68,7 @@ go_library(
|
|||
importpath = "github.com/steveyegge/beads/internal/httpclient",
|
||||
visibility = ["//:__subpackages__"],
|
||||
deps = [
|
||||
"//internal/atomicfile",
|
||||
"//internal/config",
|
||||
"//internal/configfile",
|
||||
"//internal/creds",
|
||||
|
|
@ -136,6 +138,7 @@ go_test(
|
|||
"metadatacas_test.go",
|
||||
"molecule_test.go",
|
||||
"open_with_test.go",
|
||||
"policy_cache_test.go",
|
||||
"race_detector_off_test.go",
|
||||
"race_detector_on_test.go",
|
||||
"required_served_tier_test.go",
|
||||
|
|
|
|||
128
internal/httpclient/policy_cache.go
Normal file
128
internal/httpclient/policy_cache.go
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
package httpclient
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/atomicfile"
|
||||
)
|
||||
|
||||
// PolicyCacheTTLEnv sets how long one bd process may trust what an earlier one
|
||||
// learned from the handshake about the external-dependency policy
|
||||
// (ServerEnforcesExternalDependencyPolicy): a Go duration ("5m") or a number
|
||||
// of seconds ("300"), or "0"/"off" to ask the server every time. An
|
||||
// unparseable value turns the cache off.
|
||||
const PolicyCacheTTLEnv = "BEADS_HTTP_POLICY_CACHE_TTL"
|
||||
|
||||
// DefaultPolicyCacheTTL bounds how stale a cached answer can be. A server's
|
||||
// answer changes only when it is restarted with a different composition.
|
||||
const DefaultPolicyCacheTTL = 5 * time.Minute
|
||||
|
||||
// policyCache remembers, per server URL and pinned project, whether that
|
||||
// server enforces bd's external-dependency policy itself. Without it every
|
||||
// short-lived `bd ready` against a remote store pays a GET /v0/beads/context
|
||||
// before its listing only to learn this one bit, which listReadyWork itself
|
||||
// never needs a handshake for.
|
||||
//
|
||||
// It is a file under the user cache directory, keyed by the URL and the
|
||||
// project the workspace pinned at `bd connect`; a workspace with no pin gets
|
||||
// no cache. A nil *policyCache is a disabled one.
|
||||
type policyCache struct {
|
||||
path string
|
||||
url string
|
||||
projectID string
|
||||
ttl time.Duration
|
||||
now func() time.Time
|
||||
}
|
||||
|
||||
type policyCacheEntry struct {
|
||||
URL string `json:"url"`
|
||||
ProjectID string `json:"project_id"`
|
||||
ExternalDependencies bool `json:"external_dependencies"`
|
||||
CheckedAt time.Time `json:"checked_at"`
|
||||
}
|
||||
|
||||
// newPolicyCache returns the cache for target, or nil when the target has no
|
||||
// project pin, the TTL is off, or there is no user cache directory.
|
||||
func newPolicyCache(target Target) *policyCache {
|
||||
if target.BaseURL == nil || target.ExpectProjectID == "" {
|
||||
return nil
|
||||
}
|
||||
ttl := policyCacheTTL()
|
||||
if ttl <= 0 {
|
||||
return nil
|
||||
}
|
||||
dir, err := os.UserCacheDir()
|
||||
if err != nil || dir == "" {
|
||||
return nil
|
||||
}
|
||||
url := target.BaseURL.String()
|
||||
sum := sha256.Sum256([]byte(url + "\x00" + target.ExpectProjectID))
|
||||
return &policyCache{
|
||||
path: filepath.Join(dir, "beads", "http-policy", hex.EncodeToString(sum[:16])+".json"),
|
||||
url: target.BaseURL.Redacted(),
|
||||
projectID: target.ExpectProjectID,
|
||||
ttl: ttl,
|
||||
now: time.Now,
|
||||
}
|
||||
}
|
||||
|
||||
func policyCacheTTL() time.Duration {
|
||||
raw := strings.TrimSpace(os.Getenv(PolicyCacheTTLEnv))
|
||||
if raw == "" {
|
||||
return DefaultPolicyCacheTTL
|
||||
}
|
||||
if strings.EqualFold(raw, "off") {
|
||||
return 0
|
||||
}
|
||||
d, err := time.ParseDuration(raw)
|
||||
if err != nil {
|
||||
d, err = time.ParseDuration(raw + "s")
|
||||
}
|
||||
if err != nil || d < 0 {
|
||||
return 0
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
// load returns the cached answer while it is fresh. An entry written in the
|
||||
// future (a clock that stepped back) is not trusted.
|
||||
func (c *policyCache) load() (enforced, ok bool) {
|
||||
if c == nil {
|
||||
return false, false
|
||||
}
|
||||
raw, err := os.ReadFile(c.path)
|
||||
if err != nil {
|
||||
return false, false
|
||||
}
|
||||
var e policyCacheEntry
|
||||
if json.Unmarshal(raw, &e) != nil || e.URL != c.url || e.ProjectID != c.projectID {
|
||||
return false, false
|
||||
}
|
||||
age := c.now().Sub(e.CheckedAt)
|
||||
if age < 0 || age >= c.ttl {
|
||||
return false, false
|
||||
}
|
||||
return e.ExternalDependencies, true
|
||||
}
|
||||
|
||||
// store records an answer the handshake just gave. It is best effort: a
|
||||
// cache that cannot be written only costs the next process a handshake.
|
||||
func (c *policyCache) store(enforced bool) {
|
||||
if c == nil {
|
||||
return
|
||||
}
|
||||
raw, err := json.Marshal(policyCacheEntry{URL: c.url, ProjectID: c.projectID, ExternalDependencies: enforced, CheckedAt: c.now().UTC()})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(c.path), 0o700); err != nil {
|
||||
return
|
||||
}
|
||||
_ = atomicfile.WriteFile(c.path, raw, 0o600)
|
||||
}
|
||||
159
internal/httpclient/policy_cache_test.go
Normal file
159
internal/httpclient/policy_cache_test.go
Normal file
|
|
@ -0,0 +1,159 @@
|
|||
package httpclient
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/httpapi/apigen"
|
||||
"github.com/steveyegge/beads/internal/httpclient/wire"
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
)
|
||||
|
||||
func testPolicyCache(t *testing.T, path, projectID string, now time.Time) *policyCache {
|
||||
t.Helper()
|
||||
return &policyCache{
|
||||
path: path,
|
||||
url: "https://example.com",
|
||||
projectID: projectID,
|
||||
ttl: time.Minute,
|
||||
now: func() time.Time { return now },
|
||||
}
|
||||
}
|
||||
|
||||
func enforcingContext() *apigen.ContextResponse {
|
||||
return &apigen.ContextResponse{Capabilities: []string{wire.CapExternalDependencies}}
|
||||
}
|
||||
|
||||
// TestPolicyProbeIsAnsweredFromTheCacheAcrossStores pins the point of the
|
||||
// cache: the second store (a later bd process) learns the server's
|
||||
// external-dependency policy without a handshake, until the entry ages out.
|
||||
func TestPolicyProbeIsAnsweredFromTheCacheAcrossStores(t *testing.T) {
|
||||
ctx := t.Context()
|
||||
path := filepath.Join(t.TempDir(), "policy.json")
|
||||
now := time.Now()
|
||||
target := Target{BaseURL: mustParseURL(t, "https://example.com"), ExpectProjectID: "prj_a"}
|
||||
|
||||
first := &fakeWire{res: enforcingContext()}
|
||||
s := New(target, first, nil)
|
||||
s.policy = testPolicyCache(t, path, "prj_a", now)
|
||||
if enforced, err := s.ServerEnforcesExternalDependencyPolicy(ctx); err != nil || !enforced {
|
||||
t.Fatalf("first probe = %v, %v; want true from the handshake", enforced, err)
|
||||
}
|
||||
if first.hits != 1 {
|
||||
t.Fatalf("first store dialed %d handshakes, want 1", first.hits)
|
||||
}
|
||||
|
||||
second := &fakeWire{err: errors.New("must not dial")}
|
||||
s = New(target, second, nil)
|
||||
s.policy = testPolicyCache(t, path, "prj_a", now.Add(30*time.Second))
|
||||
if enforced, err := s.ServerEnforcesExternalDependencyPolicy(ctx); err != nil || !enforced {
|
||||
t.Fatalf("cached probe = %v, %v; want true without a handshake", enforced, err)
|
||||
}
|
||||
if second.hits != 0 {
|
||||
t.Fatalf("second store dialed %d handshakes, want 0", second.hits)
|
||||
}
|
||||
|
||||
expired := &fakeWire{res: &apigen.ContextResponse{}}
|
||||
s = New(target, expired, nil)
|
||||
s.policy = testPolicyCache(t, path, "prj_a", now.Add(time.Minute))
|
||||
if enforced, err := s.ServerEnforcesExternalDependencyPolicy(ctx); err != nil || enforced {
|
||||
t.Fatalf("probe past the TTL = %v, %v; want the handshake's false", enforced, err)
|
||||
}
|
||||
if expired.hits != 1 {
|
||||
t.Fatalf("store past the TTL dialed %d handshakes, want 1", expired.hits)
|
||||
}
|
||||
if enforced, ok := testPolicyCache(t, path, "prj_a", now.Add(time.Minute)).load(); !ok || enforced {
|
||||
t.Fatalf("cache after the refresh = %v, %v; want the fresh false", enforced, ok)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPolicyCacheRefusesEntriesItCannotTrust covers the entries load must not
|
||||
// answer from: another project's, one from the future, and a corrupt file.
|
||||
func TestPolicyCacheRefusesEntriesItCannotTrust(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
now := time.Now()
|
||||
|
||||
path := filepath.Join(dir, "a.json")
|
||||
testPolicyCache(t, path, "prj_a", now).store(true)
|
||||
if _, ok := testPolicyCache(t, path, "prj_b", now).load(); ok {
|
||||
t.Error("an entry written for another project was trusted")
|
||||
}
|
||||
if _, ok := testPolicyCache(t, path, "prj_a", now.Add(-time.Second)).load(); ok {
|
||||
t.Error("an entry written in the future was trusted")
|
||||
}
|
||||
otherURL := testPolicyCache(t, path, "prj_a", now)
|
||||
otherURL.url = "https://other.example"
|
||||
if _, ok := otherURL.load(); ok {
|
||||
t.Error("an entry written for another server URL was trusted")
|
||||
}
|
||||
if _, ok := testPolicyCache(t, filepath.Join(dir, "missing.json"), "prj_a", now).load(); ok {
|
||||
t.Error("a missing entry was trusted")
|
||||
}
|
||||
var disabled *policyCache
|
||||
disabled.store(true)
|
||||
if _, ok := disabled.load(); ok {
|
||||
t.Error("a disabled cache answered")
|
||||
}
|
||||
}
|
||||
|
||||
// TestHeldHandshakeWinsOverThePolicyCache pins that a process already holding
|
||||
// the server's context answers from it, and corrects a stale entry.
|
||||
func TestHeldHandshakeWinsOverThePolicyCache(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "policy.json")
|
||||
now := time.Now()
|
||||
testPolicyCache(t, path, "prj_a", now).store(true)
|
||||
|
||||
target := Target{BaseURL: mustParseURL(t, "https://example.com"), ExpectProjectID: "prj_a"}
|
||||
s := New(target, &fakeWire{err: errors.New("must not dial")}, &apigen.ContextResponse{})
|
||||
s.policy = testPolicyCache(t, path, "prj_a", now)
|
||||
if enforced, err := s.ServerEnforcesExternalDependencyPolicy(t.Context()); err != nil || enforced {
|
||||
t.Fatalf("probe = %v, %v; want the held handshake's false", enforced, err)
|
||||
}
|
||||
if enforced, ok := testPolicyCache(t, path, "prj_a", now).load(); !ok || enforced {
|
||||
t.Fatalf("cache = %v, %v; want it corrected to false", enforced, ok)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewPolicyCacheNeedsAProjectPinAndATTL(t *testing.T) {
|
||||
url := mustParseURL(t, "https://example.com")
|
||||
if newPolicyCache(Target{BaseURL: url}) != nil {
|
||||
t.Error("a workspace with no project pin got a policy cache")
|
||||
}
|
||||
for _, off := range []string{"0", "off", "OFF", "soon", "-5s"} {
|
||||
t.Setenv(PolicyCacheTTLEnv, off)
|
||||
if newPolicyCache(Target{BaseURL: url, ExpectProjectID: "prj_a"}) != nil {
|
||||
t.Errorf("%s=%q left the policy cache on", PolicyCacheTTLEnv, off)
|
||||
}
|
||||
}
|
||||
for raw, want := range map[string]time.Duration{"": DefaultPolicyCacheTTL, "90": 90 * time.Second, "2m": 2 * time.Minute} {
|
||||
t.Setenv(PolicyCacheTTLEnv, raw)
|
||||
if got := policyCacheTTL(); got != want {
|
||||
t.Errorf("%s=%q: TTL %v, want %v", PolicyCacheTTLEnv, raw, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestALiveProbeSkipsThePolicyCache pins that a claim or close
|
||||
// (storage.WithLivePolicyProbe) asks the server even with a fresh entry, and
|
||||
// refreshes the entry with what the server said.
|
||||
func TestALiveProbeSkipsThePolicyCache(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "policy.json")
|
||||
now := time.Now()
|
||||
testPolicyCache(t, path, "prj_a", now).store(true)
|
||||
|
||||
target := Target{BaseURL: mustParseURL(t, "https://example.com"), ExpectProjectID: "prj_a"}
|
||||
w := &fakeWire{res: &apigen.ContextResponse{}}
|
||||
s := New(target, w, nil)
|
||||
s.policy = testPolicyCache(t, path, "prj_a", now)
|
||||
if enforced, err := s.ServerEnforcesExternalDependencyPolicy(storage.WithLivePolicyProbe(t.Context())); err != nil || enforced {
|
||||
t.Fatalf("live probe = %v, %v; want the server's false despite the cached true", enforced, err)
|
||||
}
|
||||
if w.hits != 1 {
|
||||
t.Fatalf("live probe dialed %d handshakes, want 1", w.hits)
|
||||
}
|
||||
if enforced, ok := testPolicyCache(t, path, "prj_a", now).load(); !ok || enforced {
|
||||
t.Fatalf("cache = %v, %v; want it corrected to false", enforced, ok)
|
||||
}
|
||||
}
|
||||
|
|
@ -46,6 +46,7 @@ type Store struct {
|
|||
target Target
|
||||
wire WireClient
|
||||
local *localMetadata
|
||||
policy *policyCache
|
||||
|
||||
// mu guards the lazily fetched handshake snapshot. D6 makes the context
|
||||
// fetch lazy (first post-baseline dispatch) and cached for the process, so
|
||||
|
|
@ -97,7 +98,19 @@ var _ storage.RemoteBackendStore = (*Store)(nil)
|
|||
// specific true/false meaning but would still hide a transport failure behind
|
||||
// a policy decision) or to "yes, so skip" (which would be the actual bug this
|
||||
// interface exists to prevent — the policy must never be silently skipped).
|
||||
//
|
||||
// A store opened from a workspace answers a read from the policy cache
|
||||
// (policy_cache.go) while it is fresh, so a short-lived command whose own
|
||||
// requests need no handshake does not dial one just for this. A claim or a
|
||||
// close (storage.WithLivePolicyProbe) always asks the server: it handshakes
|
||||
// anyway, and a stale answer must not stand down its client-side refusal. A
|
||||
// handshake this process already holds wins over the cache and refreshes it.
|
||||
func (s *Store) ServerEnforcesExternalDependencyPolicy(ctx context.Context) (bool, error) {
|
||||
if s.cachedSnapshot() == nil && !storage.LivePolicyProbe(ctx) {
|
||||
if enforced, ok := s.policy.load(); ok {
|
||||
return enforced, nil
|
||||
}
|
||||
}
|
||||
snap, err := s.snapshot(ctx)
|
||||
if err != nil {
|
||||
return false, err
|
||||
|
|
@ -105,7 +118,9 @@ func (s *Store) ServerEnforcesExternalDependencyPolicy(ctx context.Context) (boo
|
|||
if snap == nil {
|
||||
return false, nil
|
||||
}
|
||||
return slices.Contains(snap.Capabilities, wire.CapExternalDependencies), nil
|
||||
enforced := slices.Contains(snap.Capabilities, wire.CapExternalDependencies)
|
||||
s.policy.store(enforced)
|
||||
return enforced, nil
|
||||
}
|
||||
|
||||
var _ storage.ExternalDependencyPolicyProber = (*Store)(nil)
|
||||
|
|
@ -187,6 +202,7 @@ func open(ctx context.Context, beadsDir string) (storage.DoltStorage, error) {
|
|||
}
|
||||
s := New(target, wire, nil)
|
||||
s.local = newLocalMetadata(beadsDir)
|
||||
s.policy = newPolicyCache(target)
|
||||
return s, nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -34,6 +34,7 @@ go_test(
|
|||
"blocked_lister_test.go",
|
||||
"blocking_annotator_batch_test.go",
|
||||
"decorator_test.go",
|
||||
"live_probe_test.go",
|
||||
"ready_wake_test.go",
|
||||
"remote_roles_composition_test.go",
|
||||
"remote_roles_test.go",
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ func (s *Store) BatchCloser() (issueops.BatchCloser, error) {
|
|||
type batchCloser struct{ policy *Store }
|
||||
|
||||
func (c *batchCloser) CloseBatch(ctx context.Context, request issueops.CloseBatchRequest) (issueops.CloseBatchResult, error) {
|
||||
ctx = storage.WithLivePolicyProbe(ctx)
|
||||
if err := storageissueops.ValidateCloseBatchRequest(request); err != nil {
|
||||
return issueops.CloseBatchResult{}, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -83,6 +83,7 @@ func (l *lifecycle) Create(ctx context.Context, request publicops.CreateRequest)
|
|||
}
|
||||
|
||||
func (l *lifecycle) Update(ctx context.Context, request publicops.UpdateRequest) (publicops.UpdateResult, error) {
|
||||
ctx = storage.WithLivePolicyProbe(ctx)
|
||||
if request.Claim {
|
||||
if err := l.policy.guardExternalClose(ctx, request.IssueID, false); err != nil {
|
||||
return publicops.UpdateResult{}, err
|
||||
|
|
@ -97,6 +98,7 @@ func (l *lifecycle) Update(ctx context.Context, request publicops.UpdateRequest)
|
|||
}
|
||||
|
||||
func (l *lifecycle) Close(ctx context.Context, request publicops.CloseRequest) (publicops.CloseResult, error) {
|
||||
ctx = storage.WithLivePolicyProbe(ctx)
|
||||
if err := l.policy.guardExternalClose(ctx, request.IssueID, request.Force); err != nil {
|
||||
return publicops.CloseResult{}, err
|
||||
}
|
||||
|
|
@ -561,6 +563,7 @@ type readyCounterWrapper interface {
|
|||
// compare-and-swap claim operation. Cross-project state cannot be atomic with
|
||||
// the local claim, but local claim ownership remains race-safe.
|
||||
func (s *Store) ClaimReadyIssue(ctx context.Context, filter types.WorkFilter, actor string) (*types.Issue, error) {
|
||||
ctx = storage.WithLivePolicyProbe(ctx)
|
||||
state, err := s.loadBlockingState(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -905,6 +908,7 @@ func (s *Store) IsBlockedBatch(ctx context.Context, issueIDs []string) (map[stri
|
|||
// that serves its roles refuses this method; bd close goes through
|
||||
// remoteBatchCloser (remote_roles.go) there.
|
||||
func (s *Store) CloseIssueChecked(ctx context.Context, issueID, actor string, opts storage.CloseIssueOptions) (storage.CloseIssueResult, error) {
|
||||
ctx = storage.WithLivePolicyProbe(ctx)
|
||||
if !opts.Force {
|
||||
issue, err := s.inner.GetIssue(ctx, issueID)
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -31,6 +31,9 @@ type fakeStore struct {
|
|||
batch *fakeBatchCloser
|
||||
enforced bool
|
||||
enforcedErr error
|
||||
// liveProbes records storage.LivePolicyProbe for every policy probe, in
|
||||
// order.
|
||||
liveProbes []bool
|
||||
|
||||
excludeIDsUnsupported bool
|
||||
// defaultPage, when positive, is how many rows a zero-Limit ready listing
|
||||
|
|
@ -53,7 +56,8 @@ func (f *fakeStore) IssueLifecycle() (publicops.Lifecycle, error) { return f.lif
|
|||
// every existing test's raw store on today's client-side-enforcement path
|
||||
// unchanged; enforced:true is set explicitly only by the test below that
|
||||
// pins loadBlockingState's skip branch.
|
||||
func (f *fakeStore) ServerEnforcesExternalDependencyPolicy(_ context.Context) (bool, error) {
|
||||
func (f *fakeStore) ServerEnforcesExternalDependencyPolicy(ctx context.Context) (bool, error) {
|
||||
f.liveProbes = append(f.liveProbes, storage.LivePolicyProbe(ctx))
|
||||
return f.enforced, f.enforcedErr
|
||||
}
|
||||
|
||||
|
|
|
|||
142
internal/storage/externaldeps/live_probe_test.go
Normal file
142
internal/storage/externaldeps/live_probe_test.go
Normal file
|
|
@ -0,0 +1,142 @@
|
|||
package externaldeps
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
publicops "github.com/steveyegge/beads/issueops"
|
||||
)
|
||||
|
||||
// TestClaimsAndClosesAskForALivePolicyProbe pins which entry points mark
|
||||
// their policy probe live (storage.WithLivePolicyProbe): every claim and close
|
||||
// guard, so a cached "the server enforces it" can never stand down their
|
||||
// client-side refusal, and no read, so a listing may answer from the cache.
|
||||
func TestClaimsAndClosesAskForALivePolicyProbe(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
live bool
|
||||
run func(t *testing.T, s *Store)
|
||||
}{
|
||||
{"ready listing", false, func(t *testing.T, s *Store) {
|
||||
if _, err := s.GetReadyWork(ctx, types.WorkFilter{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}},
|
||||
{"lifecycle close", true, func(t *testing.T, s *Store) {
|
||||
lc, err := s.IssueLifecycle()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := lc.Close(ctx, publicops.CloseRequest{IssueID: "a"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}},
|
||||
{"lifecycle claim", true, func(t *testing.T, s *Store) {
|
||||
lc, err := s.IssueLifecycle()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := lc.Update(ctx, publicops.UpdateRequest{IssueID: "a", Claim: true}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}},
|
||||
{"claim ready", true, func(t *testing.T, s *Store) {
|
||||
if _, err := s.ClaimReadyIssue(ctx, types.WorkFilter{}, "me"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}},
|
||||
{"batch close", true, func(t *testing.T, s *Store) {
|
||||
closer, err := s.BatchCloser()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := closer.CloseBatch(ctx, publicops.CloseBatchRequest{Actor: "me", Items: []publicops.BatchCloseItem{{IssueID: "a"}}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
raw := &fakeStore{lifecycle: &fakeLifecycle{}, batch: &fakeBatchCloser{}}
|
||||
tc.run(t, testStore(raw, &fakeStore{}, false))
|
||||
if len(raw.liveProbes) == 0 {
|
||||
t.Fatal("the policy was never probed")
|
||||
}
|
||||
for i, live := range raw.liveProbes {
|
||||
if live != tc.live {
|
||||
t.Errorf("probe %d: live = %v, want %v", i, live, tc.live)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
if storage.LivePolicyProbe(ctx) {
|
||||
t.Error("a plain context reads as live")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRemoteClaimsAndClosesAskForALivePolicyProbe is the same pin over a
|
||||
// remote store's role views (remote_roles.go): direct claim, claim-next and
|
||||
// batch close probe live; the reader's ready listing does not.
|
||||
func TestRemoteClaimsAndClosesAskForALivePolicyProbe(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
live bool
|
||||
run func(t *testing.T, s *Store)
|
||||
}{
|
||||
{"ready", false, func(t *testing.T, s *Store) {
|
||||
r, err := s.IssueReader()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := r.Ready(t.Context(), publicops.ReadyRequest{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}},
|
||||
{"claim", true, func(t *testing.T, s *Store) {
|
||||
c, err := s.IssueClaimer()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := c.Claim(t.Context(), publicops.ClaimRequest{Actor: "a", IssueID: "be-a"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}},
|
||||
{"claim next", true, func(t *testing.T, s *Store) {
|
||||
c, err := s.ReadyClaimer()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := c.ClaimNext(t.Context(), publicops.ClaimNextRequest{Actor: "a"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}},
|
||||
{"batch close", true, func(t *testing.T, s *Store) {
|
||||
c, err := s.BatchCloser()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := c.CloseBatch(t.Context(), publicops.CloseBatchRequest{Actor: "a", Items: []publicops.BatchCloseItem{{IssueID: "be-a"}}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
base, _ := newRemotePolicyStore(true)
|
||||
raw := &passthroughFake{remoteFake: base.inner.(*remoteFake), claimer: &recordingClaimer{}}
|
||||
store := New(raw, func(ProjectName) (string, bool) { return "", false }, nil)
|
||||
store.warnProject = nil
|
||||
tc.run(t, store)
|
||||
probes := raw.liveProbes
|
||||
if len(probes) == 0 {
|
||||
t.Fatal("the policy was never probed")
|
||||
}
|
||||
for i, live := range probes {
|
||||
if live != tc.live {
|
||||
t.Errorf("probe %d: live = %v, want %v", i, live, tc.live)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -253,6 +253,7 @@ type remoteClaimer struct {
|
|||
// a server that applies the policy, externalBlockersFor reads nothing and the
|
||||
// claim goes through untouched.
|
||||
func (c *remoteClaimer) Claim(ctx context.Context, req issueops.ClaimRequest) (issueops.ClaimResult, error) {
|
||||
ctx = storage.WithLivePolicyProbe(ctx)
|
||||
blockers, err := c.policy.externalBlockersFor(ctx, []string{req.IssueID})
|
||||
if err != nil {
|
||||
return issueops.ClaimResult{}, err
|
||||
|
|
@ -286,6 +287,7 @@ type remoteReadyClaimer struct {
|
|||
// set, so the second pass reads the candidates behind them. Only a claim that
|
||||
// tried candidates and won none is an error.
|
||||
func (c *remoteReadyClaimer) ClaimNext(ctx context.Context, req issueops.ClaimNextRequest) (issueops.ClaimNextResult, error) {
|
||||
ctx = storage.WithLivePolicyProbe(ctx)
|
||||
// externallyBlockedIDs is empty against a server that applies the policy
|
||||
// (loadBlockingState's probe), so that server's atomic claim answers.
|
||||
blocked, err := c.policy.externallyBlockedIDs(ctx)
|
||||
|
|
@ -527,6 +529,7 @@ type remoteBatchCloser struct {
|
|||
// externalBlockersFor reads nothing and the whole request goes through
|
||||
// untouched.
|
||||
func (c *remoteBatchCloser) CloseBatch(ctx context.Context, request issueops.CloseBatchRequest) (issueops.CloseBatchResult, error) {
|
||||
ctx = storage.WithLivePolicyProbe(ctx)
|
||||
if request.ClaimNext != nil {
|
||||
return c.closeAndClaimNext(ctx, request)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,6 +50,7 @@ type issueClaimer struct {
|
|||
}
|
||||
|
||||
func (c *issueClaimer) Claim(ctx context.Context, req issueops.ClaimRequest) (issueops.ClaimResult, error) {
|
||||
ctx = storage.WithLivePolicyProbe(ctx)
|
||||
blocked, blockers, err := c.policy.IsBlocked(ctx, req.IssueID)
|
||||
if err != nil {
|
||||
return issueops.ClaimResult{}, err
|
||||
|
|
@ -76,6 +77,7 @@ func (s *Store) ReadyClaimer() (issueops.ReadyClaimer, error) {
|
|||
type readyClaimer struct{ policy *Store }
|
||||
|
||||
func (c *readyClaimer) ClaimNext(ctx context.Context, req issueops.ClaimNextRequest) (issueops.ClaimNextResult, error) {
|
||||
ctx = storage.WithLivePolicyProbe(ctx)
|
||||
if err := storageissueops.ValidateClaimNextRequest(req); err != nil {
|
||||
return issueops.ClaimNextResult{}, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1122,6 +1122,23 @@ type ExternalDependencyPolicyProber interface {
|
|||
ServerEnforcesExternalDependencyPolicy(ctx context.Context) (bool, error)
|
||||
}
|
||||
|
||||
type livePolicyProbeKey struct{}
|
||||
|
||||
// WithLivePolicyProbe marks ctx as carrying a claim or a close: an
|
||||
// ExternalDependencyPolicyProber asked under it must answer from the server,
|
||||
// never from a cache another process wrote. A read can tolerate a cached
|
||||
// answer for its TTL; a write guard cannot, since a stale "the server
|
||||
// enforces it" would skip the client-side refusal of externally blocked work.
|
||||
func WithLivePolicyProbe(ctx context.Context) context.Context {
|
||||
return context.WithValue(ctx, livePolicyProbeKey{}, true)
|
||||
}
|
||||
|
||||
// LivePolicyProbe reports whether ctx was marked by WithLivePolicyProbe.
|
||||
func LivePolicyProbe(ctx context.Context) bool {
|
||||
live, _ := ctx.Value(livePolicyProbeKey{}).(bool)
|
||||
return live
|
||||
}
|
||||
|
||||
// MoleculeAutoCloseProber is implemented by a DoltStorage whose closes may not
|
||||
// accept issueops.CloseRequest.AutoCloseMolecule — today exactly
|
||||
// httpclient.Store, whose server advertises it with
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue