Skip to content

cache: add WriteEnvelope verbatim atomic writer (MON-5369 §5 first-port generalization)#20

Merged
rianjs merged 1 commit into
mainfrom
feature/MON-5369-cache-writeenvelope
May 19, 2026
Merged

cache: add WriteEnvelope verbatim atomic writer (MON-5369 §5 first-port generalization)#20
rianjs merged 1 commit into
mainfrom
feature/MON-5369-cache-writeenvelope

Conversation

@rianjs
Copy link
Copy Markdown
Contributor

@rianjs rianjs commented May 19, 2026

Prerequisite (PR-A) for MON-5369 (jtk cache → cli-common, working-with-state.md §6.4 unit 1). The §5 release-train guardrail's "first port surfaces a tier-1 constraint that generalizes" case — pre-blessed by the architect session.

Why

jtk's Touch marks a cache stale by writing an envelope with FetchedAt zeroed. Tier-1 had no verbatim writer: WriteResource always re-stamps FetchedAt=now, and the path composer is unexported — so jtk could not express writeRaw against cli-common without duplicating atomic-write + path validation (divergence risk).

What (purely additive)

  • WriteEnvelope[T](loc Locator, env Envelope[T]) error — writes a caller-supplied envelope verbatim (FetchedAt/TTL/Version preserved). Uses env.Resource as the resource name; requires env.Instance == loc.InstanceKey. Because ReadResource treats a resource/instance mismatch as ErrCacheMiss, WriteEnvelope refuses to write a self-inflicted miss: a mismatch returns the new ErrInstanceMismatch and writes nothing.
  • WriteResource now delegates to WriteEnvelopebehavior unchanged (still stamps a fresh non-zero FetchedAt; regression-tested).
  • Existing exported API unchanged; pure stdlib; go.mod/go.sum byte-identical (the make check tidy gate enforces this).

Tests

Verbatim zero-FetchedAt round-trip (readable, not a self-inflicted miss); env.Instance != loc.InstanceKeyErrInstanceMismatch, nothing written; unsafe env.ResourceErrInvalidName; WriteResource still stamps FetchedAt. make check green (lint 0, -race all packages incl. credstore).

Review note

Architect session 019e4019 prescribed this exact identity-safe contract during MON-5369 plan review (round 1). Codex quota is temporarily exhausted; per the workstream's Codex policy it does not block — the same session will review this diff alongside PR-B (jtk) once quota resets.

[MON-5369]

The §5 first-port generalization (MON-5369, jtk cache). Tier-1 had no way
to persist an envelope verbatim: WriteResource always re-stamps FetchedAt,
so an invalidation "stale marker" (FetchedAt zeroed) could not survive a
round-trip. jtk's Touch/writeRaw needs exactly this.

WriteEnvelope writes a caller-supplied envelope verbatim (FetchedAt/TTL/
Version preserved), using env.Resource as the name and requiring
env.Instance == loc.InstanceKey — because ReadResource treats a
resource/instance mismatch as a miss, it refuses (ErrInstanceMismatch,
nothing written) to persist an envelope the next read would reject.
WriteResource now delegates to it (behavior unchanged: still stamps a
fresh FetchedAt). Purely additive; existing API and go.mod/go.sum
untouched.

[MON-5369]
@rianjs rianjs merged commit 2c4a5b8 into main May 19, 2026
4 checks passed
@rianjs rianjs deleted the feature/MON-5369-cache-writeenvelope branch May 19, 2026 13:32
rianjs added a commit that referenced this pull request May 20, 2026
…ore-close [MON-5375] (#22)

* docs(state): finalize §6 step 6 + tier-2 deferral for INT-310 tag-before-close [MON-5375]

Adds §6.6 close-out retrospective: 5 of 6 port units shipped (jtk-cache,
Atlassian shared config, gro, slck, nrq); sfdc parked and explicitly
excluded from the v0.1.0 train (no cli-common dependency today).

Commons API additions during the rollout: cache.WriteEnvelope (#20) and
underscore in the cache-component regex (#21). Both additive; no
breaking change required across 5 ports.

Codifies the ten reusable patterns that emerged across the matrix:
mutation-free Detect/Apply split, Load/LoadForRuntime + cfg!=nil
contract, unexported xxxFromNewDir testable seam, malformed-old fails
loud before CopyNeeded, companion-plaintext-file dual-probe with
parsed-projection equality, 7-var statedirtest.Hermetic, the cleanup-
command recovery contract (cleanup must not be blocked by the broken
state it exists to wipe), init-gate ordering proof, reflect.DeepEqual
on default-applied Config, path-identity dedup.

Reaffirms §5b tier-2 deferral: only jtk needs the registry/DAG/fetcher
shape today (gro uses tier-1 only; cfl has no cache). Promoting from
one consumer would just relocate jtk code. Re-evaluate when cfl gains
a cache.

Closes #19

* fixup: Codex r1 factual corrections

- Atlassian PR numbers: #371→#373 (MON-5369 jtk-cache), #372→#374
  (MON-5370 shared config). SHAs were already correct.
- Pattern 5 (companion-secret-file dual-probe): format-dependent
  equality, not a single rule — text key=value uses parsed projection
  (nrq); opaque blobs use trimmed raw serialized value (gro's
  token.json). Generic shape described as old/new candidate
  enumeration + path-identity dedup, not nrq's specific helper trio.
- Pattern 9 (material equality): use whole-struct DeepEqual on
  default-applied Config ONLY when every field is directly comparable
  and user-meaningful (slck/nrq); build an explicit projection when
  fields need semantic normalization (gro: OAuthClientPath default
  equivalence, sorted GrantedScopes).
- §5b tier-2 reaffirmation: "second tier-2-shape consumer," not
  "third" (the criteria are ≥2 consumers).

* fixup: Codex r2 — pattern 5 conditional cleanup-helper rule; pattern 9 typo

* fixup: Codex r3 nit — narrow gro cleanup-scope wording
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant