Skip to content

Honor numeric zero TTL values in ScopedCache.make - #6948

Merged
tim-smart merged 2 commits into
mainfrom
audit/repro-core-scopedcache-zero-ttl
Aug 4, 2026
Merged

Honor numeric zero TTL values in ScopedCache.make#6948
tim-smart merged 2 commits into
mainfrom
audit/repro-core-scopedcache-zero-ttl

Conversation

@fubhy

@fubhy fubhy commented Aug 4, 2026

Copy link
Copy Markdown
Member

Summary

ScopedCache.make retains entries indefinitely when its fixed timeToLive is the valid numeric value zero.

Important

This PR includes focused regression tests and the implementation fix.

Numeric zero TTL becomes infinity

Module: ScopedCache
Audit ID: core-s-z-testing-scoped-cache-zero-ttl
Severity / confidence: medium / high

What happens

ScopedCache.make retains entries indefinitely when its fixed timeToLive is the valid numeric value zero.

Why it happens

options.timeToLive ? ... : defaultTimeToLive treats numeric zero as absent and silently installs an infinite TTL, unlike makeWith, which honors Duration.zero.

Expected behavior

The fixed timeToLive accepted by make is a Duration.Input, for which numeric 0 means immediate expiry.

Relevant implementation

These links and excerpts are pinned to audit base c9b56ab507f224426ee8388dc450da447ec4715f.

View problematic code at packages/effect/src/ScopedCache.ts:212-215
  makeWith<Key, A, E, R, ServiceMode>({
    ...options,
    timeToLive: options.timeToLive ? () => options.timeToLive! : defaultTimeToLive
  })

View exact lines on GitHub

Reproduction

pnpm test --run packages/effect/test/ScopedCache.test.ts -t "uses a numeric zero TTL"

Observed failure: has is true instead of false

Implementation

The fixed-TTL constructors now distinguish an omitted TTL from numeric 0, so zero expires immediately in both ScopedCache.make and Cache.make. The scoped reproduction remains unchanged, and matching regression coverage was added for Cache.make.

Validation

  • pnpm test --run packages/effect/test/Cache.test.ts packages/effect/test/ScopedCache.test.ts
  • pnpm lint-fix
  • pnpm check

Audit provenance

  • Audit base: c9b56ab507f224426ee8388dc450da447ec4715f
  • Reproduction base: c9b56ab507f224426ee8388dc450da447ec4715f
  • Findings: core-s-z-testing-scoped-cache-zero-ttl
  • Initial patch: focused reproduction tests; implementation fix completed in this PR

Closes EFF-394

@fubhy fubhy added the audit Findings originating from the Effect runtime correctness audit label Aug 4, 2026
@changeset-bot

changeset-bot Bot commented Aug 4, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 72d6acd

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 30 packages
Name Type
effect Patch
@effect/opentelemetry Patch
@effect/platform-browser Patch
@effect/platform-bun Patch
@effect/platform-deno Patch
@effect/platform-node-shared Patch
@effect/platform-node Patch
@effect/vitest Patch
@effect/ai-anthropic Patch
@effect/ai-openai-compat Patch
@effect/ai-openai Patch
@effect/ai-openrouter Patch
@effect/atom-react Patch
@effect/atom-solid Patch
@effect/atom-vue Patch
@effect/sql-clickhouse Patch
@effect/sql-d1 Patch
@effect/sql-libsql Patch
@effect/sql-mssql Patch
@effect/sql-mysql2 Patch
@effect/sql-pg Patch
@effect/sql-pglite Patch
@effect/sql-sqlite-bun Patch
@effect/sql-sqlite-do Patch
@effect/sql-sqlite-node Patch
@effect/sql-sqlite-react-native Patch
@effect/sql-sqlite-wasm Patch
@effect/docgen Patch
@effect/doctest Patch
@effect/openapi-generator Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@effect-slopcop effect-slopcop Bot added 4.0 bug Something isn't working labels Aug 4, 2026

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important

The reproduction test is correct, but the implementation fix it specifies is not yet included. CI is expected to fail until ScopedCache.make stops treating timeToLive: 0 as a falsy "missing" value.

Reviewed changes — Added one regression test in packages/effect/test/ScopedCache.test.ts that asserts ScopedCache.make({ timeToLive: 0 }) immediately expires entries; verified locally that this test currently fails because the implementation still uses a falsy TTL guard.

⚠️ Implementation fix is still missing

The PR description says this branch intentionally starts with failing reproduction tests and expects the fix to follow in the same branch. The current diff adds only the test. Before merging, ScopedCache.ts needs to stop using a falsy check for timeToLive, because 0 is a valid Duration.Input.

Technical details
# ScopedCache.make falsy TTL guard

## Affected sites
- `packages/effect/src/ScopedCache.ts:214``options.timeToLive ? () => options.timeToLive! : defaultTimeToLive` treats numeric `0` as missing and installs `Duration.infinity`.

## Required outcome
- `timeToLive: 0` must result in immediate expiry, not infinite TTL.
- The reproduction test added in this PR (`packages/effect/test/ScopedCache.test.ts:47-58`) must pass without weakening its assertion.

## Suggested approach
Change the guard to an explicit `undefined` check, e.g.:

```ts
timeToLive: options.timeToLive !== undefined
  ? () => options.timeToLive!
  : defaultTimeToLive
```

Then run the focused reproduction command and the affected package tests.

⚠️ Same bug exists in Cache.ts

Cache.make uses the identical options.timeToLive ? ... : defaultTimeToLive guard at packages/effect/src/Cache.ts:308. Consider applying the same fix and a matching regression test so the non-scoped cache gets the same correction.

ℹ️ Changeset needed

Once the implementation fix lands, add a .changeset entry for the effect package because the runtime behavior of ScopedCache.make changes for timeToLive: 0.

Pullfrog  | ⚠️ this action is pinned to a commit SHA, which freezes the cleanup step — switch to @v0 or keep the SHA fresh with Dependabot | Fix all ➔Fix 👍s ➔View workflow run | Using Kimi K2 (free via Pullfrog for OSS) | 𝕏

Comment thread packages/effect/test/ScopedCache.test.ts
@tim-smart
tim-smart enabled auto-merge (squash) August 4, 2026 22:08

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes — Since the prior review, the implementation fix landed in 72d6acd and addressed all remaining feedback.

  • Fixed the falsy TTL guard in both cache constructorsCache.make and ScopedCache.make now use options.timeToLive !== undefined, so timeToLive: 0 is honored as immediate expiry instead of falling back to infinite TTL.
  • Added a matching Cache.make regression testpackages/effect/test/Cache.test.ts now covers the same numeric-zero TTL behavior already tested for ScopedCache.
  • Added a changeset.changeset/fuzzy-caches-expire.md records the effect patch.

Validation run: pnpm test --run packages/effect/test/Cache.test.ts packages/effect/test/ScopedCache.test.ts and pnpm check both pass.

Pullfrog  | ⚠️ this action is pinned to a commit SHA, which freezes the cleanup step — switch to @v0 or keep the SHA fresh with Dependabot | View workflow run | Using Kimi K2 (free via Pullfrog for OSS) | 𝕏

@tim-smart
tim-smart merged commit c987a12 into main Aug 4, 2026
20 checks passed
@tim-smart
tim-smart deleted the audit/repro-core-scopedcache-zero-ttl branch August 4, 2026 22:28
@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Bundle Size Analysis

Generated from PR build output; treat the content below as untrusted.

File Name Current Size Previous Size Difference
basic.ts 7.06 KB 7.06 KB 0.00 KB (0.00%)
batching.ts 9.86 KB 9.86 KB 0.00 KB (0.00%)
brand.ts 6.34 KB 6.34 KB 0.00 KB (0.00%)
cache.ts 10.63 KB 10.71 KB -0.09 KB (-0.79%)
config.ts 20.60 KB 20.60 KB 0.00 KB (0.00%)
differ.ts 20.20 KB 20.20 KB 0.00 KB (0.00%)
http-client.ts 21.49 KB 21.58 KB -0.09 KB (-0.41%)
logger.ts 10.76 KB 10.84 KB -0.08 KB (-0.76%)
metric.ts 8.98 KB 8.98 KB 0.00 KB (0.00%)
optic.ts 7.18 KB 7.18 KB 0.00 KB (0.00%)
pubsub.ts 14.90 KB 14.99 KB -0.09 KB (-0.57%)
queue.ts 11.58 KB 11.66 KB -0.08 KB (-0.68%)
schedule.ts 10.74 KB 10.83 KB -0.09 KB (-0.80%)
schema-class.ts 19.14 KB 19.14 KB 0.00 KB (0.00%)
schema-fromJsonSchemaDocument.ts 28.96 KB 28.96 KB 0.00 KB (0.00%)
schema-representation-roundtrip.ts 25.29 KB 25.29 KB 0.00 KB (0.00%)
schema-string-transformation.ts 13.30 KB 13.38 KB -0.09 KB (-0.64%)
schema-string.ts 10.94 KB 10.94 KB 0.00 KB (0.00%)
schema-template-literal.ts 15.17 KB 15.17 KB 0.00 KB (0.00%)
schema-toArbitraryLazy.ts 21.94 KB 21.94 KB 0.00 KB (0.00%)
schema-toCodeDocument.ts 24.34 KB 24.34 KB 0.00 KB (0.00%)
schema-toCodecJson.ts 19.18 KB 19.18 KB 0.00 KB (0.00%)
schema-toEquivalence.ts 19.01 KB 19.01 KB 0.00 KB (0.00%)
schema-toFormatter.ts 18.87 KB 18.87 KB 0.00 KB (0.00%)
schema-toJsonSchemaDocument.ts 22.60 KB 22.60 KB 0.00 KB (0.00%)
schema-toRepresentation.ts 19.52 KB 19.52 KB 0.00 KB (0.00%)
schema.ts 18.41 KB 18.41 KB 0.00 KB (0.00%)
stm.ts 12.54 KB 12.63 KB -0.09 KB (-0.74%)
stream.ts 9.80 KB 9.80 KB 0.00 KB (0.00%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.0 audit Findings originating from the Effect runtime correctness audit bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants