Fix the secret cache in a partner API Lambda

Review the AI's codeEasy10 minAmazon style

Your task

Your Lambda calls a partner API with a key it reads from Secrets Manager. The first draft fetched the key on every request and fell back to a hardcoded test key when the secret was empty. The new version drops the fallback, caches the key in the warm container, and fetches it again once it is older than a time to live (TTL).

An assistant wrote secret_fetch_times(invocation_times, ttl_seconds), which models when the cache fetches. It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • invocation_times: the time of each invocation of one warm container, in seconds, in the order they ran. Times are whole numbers, 0 or more. They usually increase, but not always.
  • ttl_seconds: how long a fetched key stays fresh, 0 or more.
  • Return the times at which the key is fetched, in invocation order.

Rules the tests check:

  • The first invocation always fetches, because nothing is cached yet.
  • A later invocation fetches when its time minus the time of the last fetch is at least ttl_seconds. Otherwise it reuses the cached key.
  • The TTL clock restarts at each fetch, not at each invocation.
  • When times go backwards, the difference is negative. That is less than any TTL, so there is no fetch, even with a TTL of 0.

Examples

secret_fetch_times(invocation_times: list[int], ttl_seconds: int) -> list[int]
Example 1
Inputsecret_fetch_times([0, 10, 300, 305, 700], 300)
Output[0, 300, 700]
Example 2
Inputsecret_fetch_times([1, 2, 3], 0)
Output[1, 2, 3]