* 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>
128 lines
3.8 KiB
Go
128 lines
3.8 KiB
Go
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)
|
|
}
|