Free self-study · No account needed · Progress stays in this browser. Back it up before clearing site data or changing devices.

← Week 8: Persistent Memory

Week 08 / Lesson 2

Correcting and forgetting stored information

Resolve stale or conflicting memory without losing data integrity.

Section 2.1 1 of 7

Overview

Focus

Resolve stale or conflicting memory without losing data integrity.

Your route through the lesson

  • 2.2 Explicit corrections and conflicting sources
  • 2.3 Versioned writes and lost-update prevention
  • 2.4 Expiry, deletion, and derived indexes
  • 2.5 Put it into practice
  • 2.6 Guided comparison
  • 2.7 A correction racing with an old write

All required teaching is included here. Review selections are saved. Choose a correct answer for every question to continue; retries are unlimited. Study time and personal notes are optional.

Section 2.2 2 of 7

Explicit corrections and conflicting sources

Focus

Resolve explicit corrections using scope and source rather than simple recency.

Memory becomes unreliable when old facts compete with corrections. A correction states that an earlier value should be replaced within a defined scope. A later unrelated mention is not necessarily a correction. “Ship this sample to Office B” can coexist with “My default office is A.” The task-specific destination should not silently overwrite the default.

Suppose Lee explicitly says, “My default delivery office changed from A to B today.” A completed update finds the active default-office record for Lee, preserves its provenance in an audit event if retention permits, and stores B as the current value with a new version. A retrieved old note mentioning A remains historical evidence, not an instruction to revert.

Conflicting sources need a policy. An authenticated user’s explicit correction of their own preference usually outranks an inference from earlier behavior. Two authoritative records with incompatible current values may require clarification rather than an arbitrary choice. Recency is useful only after checking identity, scope, and whether the new statement actually supersedes the old one.

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. A user’s default office is A. They say “Send this sample to B.” What should happen to the default?
2. Lee explicitly changes the default office from A to B. An older note still mentions A. Which is current?
3. Two current authoritative sources disagree and neither supersedes the other. Which response is justified?

Section 2.3 3 of 7

Versioned writes and lost-update prevention

Focus

Use expected versions to reject a write based on stale state.

A lost update occurs when two writers read the same old value and one overwrites the other’s completed change. Version checking prevents this by requiring the writer to state the version it read. The database updates only if that expected version still matches. This is optimistic concurrency control: writers proceed without holding a long lock, but conflicts are checked when committing.

Start with office A, version 3. Writer One reads version 3; Writer Two also reads version 3. Writer One changes the office to B and the database stores version 4. Writer Two’s attempt to write C with expected version 3 matches no current row and is rejected. It must reread version 4 and resolve the changed situation rather than overwrite B automatically.

The version comparison and update must occur in one database operation or transaction. Checking the version in application code and then issuing an unconditional update leaves a gap where another writer can intervene. A conflict is not a random database failure; it tells the caller that its assumptions are stale. Return a distinct conflict result so the application can handle it honestly.

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. Two writers read version 3. One commits version 4. The other submits expected version 3. What should happen?
2. Why combine version comparison and update atomically?
3. A stale write is rejected. What should the caller do before considering another write?

Section 2.4 4 of 7

Expiry, deletion, and derived indexes

Focus

Remove expired or deleted information from active and derived stores.

Expiry makes information ineligible after a time. Deletion removes it from active use according to a retention policy. Both differ from merely ranking it lower. A deleted address must not reappear because its old embedding remains in a secondary index. A derived index is any searchable representation built from source records, including vectors, keyword entries, caches, and summaries.

Consider address record addr-9, version 4. Its vector entry refers to addr-9 and version 4. When the address is corrected, the index should be updated or invalidated. During the update window, retrieval can verify that each candidate still matches a current active source version. A stale derived entry then cannot become evidence.

Deletion can occur transactionally when source and index share a database. Across separate systems, a tombstone records that the source identifier is deleted, and a reliable cleanup process removes derived entries. Retrieval checks the tombstone immediately. Backups and audit retention need their own documented policy; a successful active-store deletion is not proof that every historical backup has been erased. The lab checks active retrieval, not a universal legal deletion claim.

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. An address is deleted from the main table but still retrieved from its vector index. What failed?
2. A vector entry is version 3 while the active source is version 4. Which guard helps?
3. The lab confirms active-store deletion. What remains a separate retention question?

Section 2.5 5 of 7

Put it into practice

Focus

Reproduce a stale writer and verify that correction and deletion affect retrieval.

Save week08.py in your exercise folder and run python week08.py --lesson 2. The lab simulates two writers by reading a version twice, commits one correction, and attempts the stale update. It then deletes the fact and checks both the source table and its derived index. All effects stay inside a temporary database; no personal address is used.

The database command performs the comparison in its WHERE condition. rowcount tells how many rows changed:

import sqlite3
db = sqlite3.connect(":memory:")
db.execute("CREATE TABLE memory (id TEXT PRIMARY KEY, value TEXT, version INTEGER)")
db.execute("INSERT INTO memory VALUES (?, ?, ?)", ("addr-9", "Office A", 3))
changed = db.execute(
    "UPDATE memory SET value = ?, version = version + 1 WHERE id = ? AND version = ?",
    ("Office B", "addr-9", 3)
).rowcount
assert changed == 1
assert db.execute("SELECT version FROM memory WHERE id = ?", ("addr-9",)).fetchone()[0] == 4
db.close()

This complete isolated example starts at version 3 and confirms the new version is 4. The weekly file adds persistent storage and a stale second writer. If changed is zero, the identifier or expected version did not match; the application must not report a completed correction. In the lab the identifier is known, so zero identifies the intended stale-version conflict. A PASS line means the second write was rejected and deleted information stopped being actively retrievable.

Download this week’s Python lab ↓

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. A version-checked UPDATE changes zero rows for a known record. What should the caller report?
2. The lab uses synthetic offices in a temporary database. Which effects should occur?
3. After deletion, which pair of checks is strongest for this lab?

Section 2.6 6 of 7

Guided comparison

Focus

Explain the observed conflict and the cleanup sequence using concrete state.

The completed trace begins with Office A at version 1. Both simulated writers hold expected version 1. The correction to Office B succeeds and increments the source to version 2. The derived entry is replaced with the new value and version in the same transaction. The second writer’s attempt to restore Office A with expected version 1 changes zero rows.

An authorized read now returns only Office B. The trace does not label the old writer a malicious actor; it simply records that its read was stale. A caller may reload and decide whether a new correction is warranted, but the lab does not automatically reinterpret a stale write as renewed user intent.

Next the deletion removes the active source and index entry together. Reading by the original identifier returns nothing. The test also checks a different owner’s unrelated record remains present. If cleanup merely deleted the entire index, the sensitive fact might disappear while the application unnecessarily lost everyone else’s search state. The intended invariant is precise removal plus preservation of unrelated authorized data.

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. Office B is version 2. A writer holding version 1 tries to restore A. Why is rejection useful?
2. Deleting one record empties every user’s index. Which acceptance property is missing?
3. A stale writer rereads the current record. Does that alone authorize restoring an old value?

Section 2.7 7 of 7

A correction racing with an old write

Focus

Resolve a correction racing with an old background write.

A background summarizer reads “default office A” at version 6. Before it finishes, the user explicitly corrects the default to B, producing version 7. The summarizer later proposes its old summary with expected version 6. Without a check, its delayed output can resurrect the old value even though it was generated after the correction.

The completed design rejects the summary write because its source version is stale. It may regenerate from version 7, preserving the user’s correction and provenance. Generation completion time does not tell when the underlying information was read; the source version does.

Now suppose the user deletes the fact while regeneration is running. The deletion tombstone must also prevent a delayed summary or embedding job from recreating it. Derived jobs carry source identity and version, and verify that the source is still active immediately before publishing. This is a common pattern beyond memory: asynchronous work must not publish artifacts based on revoked or superseded inputs. The result is a clear conflict or discarded stale job, rather than a quietly restored fact.

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. A summarizer finishes after a correction but used an older source version. Is its output necessarily current?
2. A fact is deleted while an embedding job is running. What should publishing check?
3. A stale summary is rejected and regenerated from version 7. Which provenance should the new artifact carry?

Your personal notes

Optional, ungraded, and saved only in this browser. Export a backup from the Academy homepage to keep them.

References and further reading
Ask your own AI assistant

Copy this prompt into an assistant you already use. Nothing is sent automatically, and your personal notes are not included.

Next lesson: Testing a knowledge assistant with memory →