Import: images keyed by their file, not the minute-rounded edit time; the real file host
The first read-only run against Paul's workspace found two things. Every hosted file was at the virtual-hosted prod-files-secure bucket, which the module's pin refused: re-pinned to the module that allows it (and whose fake builds that shape). And every last_edited_time ended in :00: image reuse and copies compared the block's edit time, so a file replaced in the minute the last run read it kept the old picture. notion_images now keeps the file's key (notion.FileKey), and reuse, copies and permanent refusals compare it. Pages were already decided on the rendered body; a test now holds an edit in the same minute, with a mutation adding the edit-time shortcut. I14 downloads and attaches an image at the real shape. Spec §12, §15, §19 and §25 note 17 record it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
9 files changed,
+244
−35
docs/superpowers/specs/2026-11-17-notion-import.md+44 −7go.mod+1 −1go.sum+2 −2internal/mapa/import_images.go+18 −5internal/mapa/import_images_place.go+19 −11internal/mapa/import_images_test.go+104 −2internal/mapa/import_lows_test.go+1 −1internal/mapa/import_rerun_test.go+36 −1scripts/mutate/mutations_import.go+19 −5
diff --git a/docs/superpowers/specs/2026-11-17-notion-import.md b/docs/superpowers/specs/2026-11-17-notion-import.md| index dc85a39..aa4a147 100644 |
| --- a/docs/superpowers/specs/2026-11-17-notion-import.md |
| +++ b/docs/superpowers/specs/2026-11-17-notion-import.md |
| @@ -442,12 +442,12 @@ Mapa has **no escapes** (level-up D11). The editor accepts that because a person |
| - **Leaks:** the `images` table shows how many images each page holds, who uploaded each and when, and its size to within 64 KiB (§1.3). The import adds nothing to that row. |
| **What the import does:** |
| -- **When:** during Write, **before** the page's transaction, for each image block that is new to this page or whose `last_edited_time` changed since its last import. Notion's file URLs are signed and expire after about an hour, and Survey may finish long after it read the block. So Write fetches the block again (`GET /v1/blocks/{id}`) for a fresh URL, downloads it through the file transport (§5), and hands the bytes to `AddImage` as the run's person. No network call and no `AddImage` happens inside a page transaction. |
| +- **When:** during Write, **before** the page's transaction, for each image block that is new to this page or whose file changed since its last import: its file's key (host and path, which hold the file's own id) differs from the one kept, never its `last_edited_time`, which Notion rounds to the minute (§25 note 17). Notion's file URLs are signed and expire after about an hour, and Survey may finish long after it read the block. So Write fetches the block again (`GET /v1/blocks/{id}`) for a fresh URL, downloads it through the file transport (§5), and hands the bytes to `AddImage` as the run's person. No network call and no `AddImage` happens inside a page transaction. |
| - **Then** `ImportCreate` or `ImportUpdate` writes the body holding `image.Markdown(caption)` (the Notion caption as plain text; empty when there is none) and its commit attaches the image. |
| - **Commit as it goes** (images §7). Images are downloaded and added only for the page about to be written, and that page is committed before the next page's images are added. So pending images never pile up against the runner's own allowance. **If the page's write returns *kept* or fails, the run deletes that page's pending images at once**, through the images spec's removal (one transaction, `removed`, the runner as actor, §3.2 item 3's shape), so nothing holds quota for seven days. |
| - **The lease is checked just before each `AddImage` and `CopyImage`.** Those are their own writes, outside the page transaction. The one gap left: a lease that moves between the check and the call lets one pending image be added by a run that no longer holds it. It is never attached, and the sweep removes it after seven days. That is the named exception to §8.3's "changes nothing". |
| - **Reuse.** `notion_images` maps (Notion block, Mapa page) to the Mapa image's public id, sealed. An unchanged block on the same page reuses it, after the run checks, in its own read, that the image row still exists and is attached to that page; one the sweep or a member's "Remove from Mapa" took away is fetched again. **A block that moves to another page**, or a synced block that shows one Notion file on several pages, is found by its block alone (`block_print`, §15), and gets `CopyImage` from the existing Mapa image where the runner may read it, and a fresh download otherwise. |
| -- **A failure is not the page's failure.** A refused or failed image becomes the images spec's text line, `(Image not imported: <file name>, <reason>)`, the file name taken from the last segment of Notion's file URL path, decoded (or the caption when the path has none),, in its place, and the page is written. The report counts images by reason. **A permanent refusal** (type, size, dimensions, host not allowed) is recorded on its `notion_images` row with the block's edit time and is not downloaded again until the block changes. **A transient one** (network, 429, 5xx, an expired URL) is tried again on the next run. |
| +- **A failure is not the page's failure.** A refused or failed image becomes the images spec's text line, `(Image not imported: <file name>, <reason>)`, the file name taken from the last segment of Notion's file URL path, decoded (or the caption when the path has none),, in its place, and the page is written. The report counts images by reason. **A permanent refusal** (type, size, dimensions, host not allowed) is recorded on its `notion_images` row with the file's key and is not downloaded again until the block shows another file (§25 note 17). **A transient one** (network, 429, 5xx, an expired URL) is tried again on the next run. |
| - **Quota.** Notion's block objects carry no file size, so the run cannot total the images before it starts. Instead, the Start page shows **both** allowances the images spec checks: the team's (`MAPA_IMAGE_QUOTA_TEAM`) and the runner's own (`MAPA_IMAGE_QUOTA_PERSON`, which counts their pending and private images). `ErrImageQuota` can come from either. On the first one, the run reads both figures to say which, and stops downloading for the rest of that run: every later image becomes the text line, and the report says at the top which allowance is full. Raising that allowance, or freeing the runner's own, and running again fills them in, because those pages render differently and are still untouched (§13.2). `ErrNoImageStore` does the same from the first image: "images aren't set up on this Mapa yet". |
| - **External images** stay links (D13). The images spec leaves fetching them to this spec (its §7), and this spec does not propose it. **Q2** asks Paul. |
| - **Literal image syntax.** Notion text that happens to read `` would become an image reference once the images spec is built. §11.4 neutralises it. |
| @@ -524,7 +524,7 @@ The import writes pages only through two methods in package `mapa`, each **one ` |
| ## 15. Sealing |
| -**New purposes:** `notion.token`, `notion.workspace` (its name and id), `notion.coordinate` (a row's Notion id, its Mapa page id, the revision the import last wrote, the ids it mentioned that were not imported, its placeholders' ids), `notion.staged` (a surveyed tree), `notion.image` (an image block's id, edit time and Mapa image). Print keys are derived apart, as base build note 21 does: `oficina/mapa:print:notion.page` and `…:notion.image`. |
| +**New purposes:** `notion.token`, `notion.workspace` (its name and id), `notion.coordinate` (a row's Notion id, its Mapa page id, the revision the import last wrote, the ids it mentioned that were not imported, its placeholders' ids), `notion.staged` (a surveyed tree), `notion.image` (an image block's id, its file's key (§25 note 17) and Mapa image). Print keys are derived apart, as base build note 21 does: `oficina/mapa:print:notion.page` and `…:notion.image`. |
| **New tables and their plaintext,** to be added to base §4's list and to `plaintext_test.go` with the build: |
| @@ -533,7 +533,7 @@ The import writes pages only through two methods in package `mapa`, each **one ` |
| | `notion_connections` (one row) | `id`, `created_by` (member id), `created_at`, `verified_at` | token, workspace name | that a Notion connection exists, who made it, when it was last checked | |
| | `notion_runs` | `id`, `actor`, `state`, `lease` (random, redrawn on start, resume and Stop), `started_at`, `heartbeat_at`, `progress_at`, `finished_at`, the counters, `error_code` | — | when imports ran, who ran them, how many pages each created, kept, failed. The lease is random and says nothing | |
| | `notion_pages` (the map) | `id`, `notion_print` (unique), `state`, `error_code`, `surveyed_run`, `seen_run` | the coordinate record (Notion id, last Notion title, Mapa page id, last written revision, unresolved mention ids, placeholder ids); the staged tree | how many Notion pages exist and their states | |
| -| `notion_images` | `id`, `place_print` (unique: an HMAC of the Notion block id and the **Notion** page id together, which the map pins to one Mapa page; §25.7), `block_print` (an HMAC of the block id alone, not unique, to find a block that moved or is shown on several pages), `state` (`imported`, `refused`; a transient failure keeps no row, so the next run tries again) | the image record (block id, block edit time, Mapa page id, public id, refusal reason) | how many images were imported, refused and failed, and, through equal `block_print`s, how many Notion images appear on more than one page. The images spec's own `images` table holds the rest (§12) | |
| +| `notion_images` | `id`, `place_print` (unique: an HMAC of the Notion block id and the **Notion** page id together, which the map pins to one Mapa page; §25.7), `block_print` (an HMAC of the block id alone, not unique, to find a block that moved or is shown on several pages), `state` (`imported`, `refused`; a transient failure keeps no row, so the next run tries again) | the image record (block id, the file's key (§25 note 17), Mapa page id, public id, refusal reason) | how many images were imported, refused and failed, and, through equal `block_print`s, how many Notion images appear on more than one page. The images spec's own `images` table holds the rest (§12) | |
| - **What the file still shows, said plainly.** The coordinate is sealed, so no column joins a ledger row to a Mapa page. **But the plaintext event log does.** A run's `created` events, by the run's actor, inside the run's window in `notion_runs`, mark which team pages it made, and sequential `pages.id` and `notion_pages.id` in survey order line them up row for row. So the file shows **which team pages came from a Notion import, and roughly their order in Notion**. It does not show their titles, bodies or Notion ids. This goes in the leak list. `notion_pages` carries no `updated_at`, which would be a second join. |
| - **Prints are HMACs under the instance key**, equality only, as every print in Mapa (base §4). |
| @@ -654,8 +654,8 @@ Against Paul's workspace. **Read-only on Notion's side**, owned by Paul, and a s |
| 1. Paul makes an internal integration, "Oficina import": **Read content** only; no update, no insert, no comments; user information **without email addresses**. He checks the capabilities page shows exactly that. |
| 2. He shares a small set of top-level pages with it, a few hundred pages at most for the first run, not the workspace root. |
| 3. `cmd/notion-record` records that set, from Paul's machine, into a directory outside every repository. Scrub it, commit the scrubbed fixtures to the module, delete the raw recording. Compare its block types with §10.1: any type not in the table is added to the table before the run. |
| -4. Check the image file URLs in the recording against the file transport's (host, path prefix) pairs. Fix the allowlist from what Notion actually sends, not from the documentation's one example. **Expect a virtual-hosted bucket**, for example `prod-files-secure.s3.us-west-2.amazonaws.com` with the workspace id as the first path segment: it is its own pair, host and prefix both, never the host alone. |
| -5. Check whether Notion rounds `last_edited_time` to the minute: image reuse and copies compare a block's edit time (§12), so two edits within a minute would look like one. Check that user mentions carry names (§11.2), what a synced block's copy returns for its children (§10.1), and whether `GET /v1/users/me` carries a workspace id (§8.3). **If it carries none, the map's workspace is matched by name** (§25.8): two workspaces with one name pass as one, and a workspace renamed in Notion is refused as another until the import is forgotten. Record which it is in the build notes before the run on `paulca`. |
| +4. Check the image file URLs in the recording against the file transport's (host, path prefix) pairs. Fix the allowlist from what Notion actually sends, not from the documentation's one example. **Expect a virtual-hosted bucket**, for example `prod-files-secure.s3.us-west-2.amazonaws.com` with the workspace id as the first path segment: it is its own pair, host and prefix both, never the host alone. **Done, by the first read-only run** (831 objects, 158 pages): all 144 hosted files were at `https://prod-files-secure.s3.us-west-2.amazonaws.com/<workspace id>/<file id>/<name>`, which the documented pair refused. That host is now its own pair, prefix `/` (§25 note 17). |
| +5. Check whether Notion rounds `last_edited_time` to the minute: image reuse and copies compare a block's edit time (§12), so two edits within a minute would look like one. **Done, by the first read-only run: it does**, all 3,815 edit times ended in `:00`. Image reuse and copies now compare the file's key; pages were already decided on the rendered body (§13.2), now gated for an edit in the same minute (§25 note 17). User mentions came back `@Anonymous`: the integration lacked step 1's user information, which Paul turns on; with it the import writes the name (§11.2; I1's `@Ana`, notionmd's `mentions-other`). Check that user mentions carry names (§11.2), what a synced block's copy returns for its children (§10.1), and whether `GET /v1/users/me` carries a workspace id (§8.3). **If it carries none, the map's workspace is matched by name** (§25.8): two workspaces with one name pass as one, and a workspace renamed in Notion is refused as another until the import is forgotten. Record which it is in the build notes before the run on `paulca`. |
| **The run, on the `test` team first** (`test.mapa.oficina.works`, disposable) |
| 6. Connect, run, and watch the status page through to the end. |
| @@ -815,7 +815,8 @@ Where the build chose, or departed: |
| move to §19 step 3, with the recording made against the new pin. |
| 2. **The file allowlist is the documented pair only** |
| (`s3.us-west-2.amazonaws.com`, `/secure.notion-static.com/`). §19 step |
| - 4 adds what a recording shows Notion sends. |
| + 4 adds what a recording shows Notion sends. *Superseded by note 17:* |
| + the virtual-hosted pair is added. |
| 3. **notionmd has its own copy of the typed tree**, filled by |
| `import_convert.go`. §4 has the mapper map "the block tree"; I8 forbids |
| notionmd importing the module, so the tree the mapper reads, and the |
| @@ -893,6 +894,42 @@ Where the build chose, or departed: |
| import's migration is `0007_notion_import`, after the locale's |
| `0006_alter_members`. |
| +17. **Found by the first real run** (read-only, `notion-record`, 831 |
| + objects and 158 pages of Paul's workspace; §19 steps 4 and 5), fixed |
| + before he imports (branch `fix/real-workspace`, module and Mapa): |
| + - **Every Notion-hosted file was refused.** All 144 were at the |
| + virtual-hosted `https://prod-files-secure.s3.us-west-2.amazonaws.com/ |
| + <workspace id>/<file id>/<name>?…`; the allowlist held only the |
| + documented path-style pair. The module adds |
| + (`prod-files-secure.s3.us-west-2.amazonaws.com`, `/`): the host, |
| + matched exactly, names the bucket, so it pins as much as the old |
| + host and prefix did. The old pair stays, and every other pin |
| + (https, no userinfo, the port, the escaped-path prefix, no |
| + redirects, no token, the `..` refusal) is unchanged. The fake builds |
| + the real shape (`Host`) and still the old one (`PathStyle`), and |
| + production's own pin is checked against both. M5 refuses |
| + look-alikes (`…amazonaws.com.evil.example`, |
| + `evil.s3.us-west-2.amazonaws.com`, another region, a trailing dot), |
| + with mutations for a suffix or prefix host match and the missing |
| + pair; I14 downloads and attaches an image at the real shape. |
| + - **`last_edited_time` is rounded to the minute** (all 3,815 ended in |
| + `:00`), so nothing may decide "unchanged" from it alone. Pages |
| + never did: Survey reads every page, and Write compares the rendered |
| + title and body with what Mapa holds (§13.2); there is no survey |
| + shortcut. `TestI2AnEditInTheSameMinuteIsPickedUp` now holds it, |
| + with a mutation that adds the edit-time shortcut. **Images did:** |
| + reuse (`prior.Edited == block.Edited`) and copies compared the |
| + block's edit time, so a file replaced in the same minute as the |
| + last run kept the old picture. `notion_images` now keeps the file's |
| + key, `notion.FileKey` (host and escaped path, the signature |
| + dropped), which changes when the file is replaced; reuse, copies |
| + and permanent refusals compare it. An unchanged image is still not |
| + downloaded again (I2). A row from before keys has none, matches |
| + nothing, and is fetched once more. Gate |
| + `TestI14AnImageReplacedInTheSameMinuteIsFetched`, and the copy's |
| + `TestI14AChangedSharedImageIsFetchedOnce`, now replace the file |
| + without moving the edit time; each has a mutation. |
| + |
| ### Fable's review of the build (2026-11-17): CHANGES REQUIRED, answered |
| No egress, credential, sealing or scope hole, and no path that |
diff --git a/go.mod b/go.mod| index 897ff0c..d2a7373 100644 |
| --- a/go.mod |
| +++ b/go.mod |
| @@ -6,7 +6,7 @@ toolchain go1.26.11 |
| require ( |
| amadan.net/oficina/estilo.git v0.0.0-20261118055615-d004805f40f2 |
| - amadan.net/oficina/notion.git v0.0.0-20261117112255-7eb9a9e888cb |
| + amadan.net/oficina/notion.git v0.0.0-20261122103431-770a9bb3668f |
| amadan.net/rastrillo/rastrillo v0.35.1-0.20261116213651-96812beacacc |
| github.com/chromedp/cdproto v0.158.6 |
| github.com/chromedp/chromedp v0.21.0 |
diff --git a/go.sum b/go.sum| index 1947ca5..14e1ead 100644 |
| --- a/go.sum |
| +++ b/go.sum |
| @@ -1,7 +1,7 @@ |
| amadan.net/oficina/estilo.git v0.0.0-20261118055615-d004805f40f2 h1:E2+IhBdGcjhX3IX8UgQj5t6Bu+4zkGG0J+W1CIeoTAo= |
| amadan.net/oficina/estilo.git v0.0.0-20261118055615-d004805f40f2/go.mod h1:2UFIFrbTrhew8EFM0C94WmTo2OASM2pHEgT4jzeE/s8= |
| -amadan.net/oficina/notion.git v0.0.0-20261117112255-7eb9a9e888cb h1:P1KhTWoKSfvm+v2rwcEqnfwsftrrpZRjEe1zoJvwceQ= |
| -amadan.net/oficina/notion.git v0.0.0-20261117112255-7eb9a9e888cb/go.mod h1:2WuyjBTLHwurTGri00zfPxSKc9b3pomdT6wSs/UIvA4= |
| +amadan.net/oficina/notion.git v0.0.0-20261122103431-770a9bb3668f h1:i1B5WWVVLUmMvOSJ+T31PTvCuoDKMtVgBmHnFeRb1Zo= |
| +amadan.net/oficina/notion.git v0.0.0-20261122103431-770a9bb3668f/go.mod h1:2WuyjBTLHwurTGri00zfPxSKc9b3pomdT6wSs/UIvA4= |
| amadan.net/rastrillo/fiesta v1.3.0 h1:7SsHw4aeWPLStR3RHFEEtX4CwqKu+Ig3PgNX8FvD8WM= |
| amadan.net/rastrillo/fiesta v1.3.0/go.mod h1:LpO391W1dIRUxOIKklHm+bGyWXtlFyBRRPQCysb9LjY= |
| amadan.net/rastrillo/rastrillo v0.35.1-0.20261116213651-96812beacacc h1:QakboVkj9nqA6ZaKXeDVXWLPobQSVvd80ic521aA8/U= |
diff --git a/internal/mapa/import_images.go b/internal/mapa/import_images.go| index 1ceb74d..315a010 100644 |
| --- a/internal/mapa/import_images.go |
| +++ b/internal/mapa/import_images.go |
| @@ -144,18 +144,24 @@ type gotImage struct { |
| placed placedImage |
| } |
| -// imageFor is a block's image: its last place reused where the block is |
| -// unchanged and the page still holds the image, a permanent refusal |
| -// remembered, or a copy or a fresh download (spec §12). |
| +// imageFor is a block's image: its last place reused where the block |
| +// still shows the same file and the page still holds the image, a |
| +// permanent refusal remembered, or a copy or a fresh download (spec |
| +// §12). |
| +// |
| +// "The same file" is the file's key (notion.FileKey: its host and path, |
| +// which hold the file's own id), never the block's last_edited_time: |
| +// Notion rounds that to the minute (§25 note 17), so a file replaced |
| +// within a minute of the last run would carry the time the run saw. |
| func (job *importJob) imageFor(ctx context.Context, entry *importEntry, |
| block notionmd.Block, alt string) (gotImage, error) { |
| - placed := placedImage{block: block.ID, edited: block.Edited, |
| + placed := placedImage{block: block.ID, key: notion.FileKey(block.File.URL), |
| page: entry.staged.ID} |
| prior, found, err := job.priorPlace(ctx, entry.staged.ID, block.ID) |
| if err != nil { |
| return gotImage{}, err |
| } |
| - if found && prior.Edited == block.Edited { |
| + if found && sameFile(prior.Key, placed.key) { |
| if prior.Reason != "" { |
| placed.reason, placed.permanent = prior.Reason, true |
| return gotImage{reason: prior.Reason, placed: placed}, nil |
| @@ -170,6 +176,13 @@ func (job *importJob) imageFor(ctx context.Context, entry *importEntry, |
| return job.freshImage(ctx, entry, block, alt, placed) |
| } |
| +// sameFile is whether two file keys name one file. A key that is empty |
| +// (a row from before keys, or a URL that names no file) matches nothing, |
| +// so the file is fetched again once rather than reused on a guess. |
| +func sameFile(kept, now string) bool { |
| + return kept != "" && kept == now |
| +} |
| + |
| // freshImage copies the block's image from another page that holds it, |
| // or downloads it and adds it. |
| func (job *importJob) freshImage(ctx context.Context, entry *importEntry, |
diff --git a/internal/mapa/import_images_place.go b/internal/mapa/import_images_place.go| index ee289fa..4f5ee8a 100644 |
| --- a/internal/mapa/import_images_place.go |
| +++ b/internal/mapa/import_images_place.go |
| @@ -8,6 +8,7 @@ import ( |
| "amadan.net/oficina/mapa/internal/mapa/notionmd" |
| "amadan.net/oficina/mapa/internal/mapa/seal" |
| + "amadan.net/oficina/notion.git/notion" |
| ) |
| // notion_images (Notion import spec §12, §15): where each Notion-hosted |
| @@ -18,15 +19,19 @@ import ( |
| // placedImage is one block's outcome on one page, before it is kept. |
| type placedImage struct { |
| - page, block, edited string |
| - publicID, reason string |
| - permanent bool |
| + page, block, key string |
| + publicID, reason string |
| + permanent bool |
| } |
| -// imagePlace is a notion_images row's sealed record. |
| +// imagePlace is a notion_images row's sealed record. Key is the file's |
| +// notion.FileKey when the place was made. A row written before keys |
| +// carries "edited" instead, the block's minute-rounded edit time, which |
| +// nothing reads now: its empty Key matches nothing, so its file is |
| +// fetched once more and the row rewritten with a key. |
| type imagePlace struct { |
| Block string `json:"block"` |
| - Edited string `json:"edited,omitempty"` |
| + Key string `json:"key,omitempty"` |
| Page int64 `json:"page,omitempty"` |
| PublicID string `json:"image,omitempty"` |
| Reason string `json:"reason,omitempty"` |
| @@ -78,21 +83,23 @@ func (job *importJob) priorPlace(ctx context.Context, page, |
| // copyImage copies the block's image from another page that still holds |
| // it, where the runner may read it (spec §12: one image, one place), |
| -// and only from a place made of the block as it is now: a block changed |
| -// since is downloaded afresh, once, and the other page copies that |
| -// (Fable round 3, M2). |
| +// and only from a place made of the file the block shows now: a file |
| +// replaced since is downloaded afresh, once, and the other page copies |
| +// that (Fable round 3, M2). The file's key decides, not the block's |
| +// minute-rounded edit time (§25 note 17). |
| func (job *importJob) copyImage(ctx context.Context, entry *importEntry, |
| block notionmd.Block) (AddedImage, bool) { |
| _, alone, err := job.store.imagePrints(entry.staged.ID, block.ID) |
| if err != nil { |
| return nil, false |
| } |
| + key := notion.FileKey(block.File.URL) |
| var rows []NotionImage |
| job.store.database.WithContext(ctx).Where("block_print = ? AND "+ |
| "state = ?", alone, imageImported).Find(&rows) |
| for _, row := range rows { |
| other, err := job.store.openPlace(row) |
| - if err != nil || other.Page == 0 || other.Edited != block.Edited || |
| + if err != nil || other.Page == 0 || !sameFile(other.Key, key) || |
| other.Page == entry.place.PageID || |
| !job.images.Held(ctx, other.PublicID, other.Page) { |
| continue |
| @@ -119,7 +126,8 @@ func (job *importJob) recordImages(ctx context.Context, entry *importEntry, |
| // keepPlaces keeps where each image went. A transient refusal keeps |
| // nothing, so the next run tries again; a permanent one is kept with the |
| -// block's edit time, and not tried again until the block changes (§12). |
| +// file's key, and not tried again until the block shows another file |
| +// (§12). |
| func keepPlaces(db *gorm.DB, store *Store, pageID int64, |
| placed []placedImage) error { |
| for _, each := range placed { |
| @@ -137,7 +145,7 @@ func keepPlace(db *gorm.DB, store *Store, pageID int64, |
| return err |
| } |
| row, found := notionImageOf(db, place) |
| - record := imagePlace{Block: placed.block, Edited: placed.edited} |
| + record := imagePlace{Block: placed.block, Key: placed.key} |
| switch { |
| case placed.publicID != "": |
| row.State = imageImported |
diff --git a/internal/mapa/import_images_test.go b/internal/mapa/import_images_test.go| index 846d0b4..4e97d80 100644 |
| --- a/internal/mapa/import_images_test.go |
| +++ b/internal/mapa/import_images_test.go |
| @@ -9,6 +9,7 @@ import ( |
| "amadan.net/rastrillo/rastrillo/blobs" |
| + "amadan.net/oficina/notion.git/notion" |
| "amadan.net/oficina/notion.git/notiontest" |
| ) |
| @@ -216,16 +217,117 @@ func (world *importWorld) notWritten(act string, page Page) { |
| } |
| // changeImage replaces an image block's file, as an edit in Notion does: |
| -// a new file and a later edit time, the same block. |
| +// a new file (a new file id in its URL), the same block. Its |
| +// last_edited_time does not move: Notion rounds it to the minute (spec |
| +// §25 note 17), so a replacement in the minute the last run read it |
| +// carries the time that run saw, and only the file's key tells. |
| func changeImage(t *testing.T, ws *small, block string, seed int) { |
| t.Helper() |
| changed, ok := ws.Block(block) |
| if !ok { |
| t.Fatal("no such block") |
| } |
| + minute := changed["last_edited_time"] |
| fresh := notiontest.Image(ws.Host("changed.png", picture(t, seed))) |
| changed["image"] = fresh["image"] |
| - changed["last_edited_time"] = "2026-12-01T10:00:00.000Z" |
| + if changed["last_edited_time"] != minute { |
| + t.Fatal("the replacement moved the edit time") |
| + } |
| +} |
| + |
| +// hostedURL is an image block's file URL, as the workspace holds it. |
| +func hostedURL(t *testing.T, ws *small, block string) string { |
| + t.Helper() |
| + found, ok := ws.Block(block) |
| + if !ok { |
| + t.Fatal("no such block") |
| + } |
| + image, _ := found["image"].(map[string]any) |
| + file, _ := image["file"].(map[string]any) |
| + url, _ := file["url"].(string) |
| + return url |
| +} |
| + |
| +// I14 at the shape a real workspace sends (spec §19 step 4, §25 note |
| +// 17): every hosted file in the first real run was at the virtual-hosted |
| +// prod-files-secure bucket, which the documented pair alone refused. The |
| +// fixture is that shape, production's own pin takes it, and it is |
| +// downloaded and attached through the fake; the documentation's |
| +// path-style shape still is too. |
| +// |
| +// Its mutation is the module's (M5, "no virtual-hosted pair, the |
| +// fixtures"): Mapa's make mutate cannot edit a dependency. Red by hand |
| +// on 2026-11-22, against the module with that pair removed through a |
| +// replace. |
| +func TestI14TheRealFileShapeIsDownloadedAndAttached(t *testing.T) { |
| + ws, block := imagePage(t) |
| + path := ws.PathStyle("old.png", picture(t, 7)) |
| + ws.Add(ws.onboarding, notiontest.Image(path, notiontest.Text("Old"))) |
| + url := hostedURL(t, ws, block) |
| + if !strings.HasPrefix(url, "https://prod-files-secure.s3."+ |
| + "us-west-2.amazonaws.com/"+notiontest.FileWorkspace+"/") { |
| + t.Fatalf("the fixture's file is not the real shape: %s", url) |
| + } |
| + files := notion.NewFiles() |
| + if !files.Allowed(url) || !files.Allowed(path) { |
| + t.Fatal("production's file pin refuses a shape Notion sends") |
| + } |
| + world := newImportWorld(t, ws.Workspace) |
| + world.useImages() |
| + world.connect(ana) |
| + world.importAs(ana) |
| + for _, title := range []string{"Handbook", "Onboarding"} { |
| + page, record, _ := world.pageTitled(title) |
| + attached, _ := world.imagesOf(page.ID) |
| + if attached != 1 || !strings.Contains(record.Body, "](/i/") || |
| + strings.Contains(record.Body, "Image not imported") { |
| + t.Errorf("%s: %d attached, body %q", title, attached, |
| + record.Body) |
| + } |
| + } |
| + if world.fileRequests() != 2 { |
| + t.Errorf("%d files fetched, want 2", world.fileRequests()) |
| + } |
| +} |
| + |
| +// I14 against the minute-rounded edit time: an image replaced in Notion |
| +// within the minute the last run read it (so its block's edit time is |
| +// the one that run kept) is downloaded on the next run and the page |
| +// updated, twice in a row; a run after that downloads nothing. |
| +// |
| +// Mutation (make mutate, first red 2026-11-22): reuse ignores the |
| +// file's key, which is what comparing the unchanged edit time did. |
| +func TestI14AnImageReplacedInTheSameMinuteIsFetched(t *testing.T) { |
| + ws, block := imagePage(t) |
| + world := newImportWorld(t, ws.Workspace) |
| + world.useImages() |
| + world.connect(ana) |
| + world.importAs(ana) |
| + _, before, _ := world.pageTitled("Handbook") |
| + for seed := 8; seed <= 9; seed++ { |
| + changeImage(t, ws, block, seed) |
| + world.server.Reset() |
| + run := world.importAs(ana) |
| + _, after, _ := world.pageTitled("Handbook") |
| + if world.fileRequests() != 1 || run.Updated != 1 || |
| + after.Body == before.Body { |
| + t.Fatalf("a replacement in the same minute: %d fetched, %+v", |
| + world.fileRequests(), run) |
| + } |
| + before = after |
| + } |
| + world.server.Reset() |
| + if run := world.importAs(ana); world.fileRequests() != 0 || |
| + run.Updated != 0 { |
| + t.Errorf("the run after: %d fetched, %+v", world.fileRequests(), run) |
| + } |
| +} |
| + |
| +// sameFile: an empty key, a row's from before keys, matches nothing. |
| +func TestAnEmptyFileKeyMatchesNothing(t *testing.T) { |
| + if sameFile("", "") || sameFile("", "h/a") || !sameFile("h/a", "h/a") { |
| + t.Error("sameFile") |
| + } |
| } |
| // L15 and round 2's M2: a kept page is decided before its images, so a |
diff --git a/internal/mapa/import_lows_test.go b/internal/mapa/import_lows_test.go| index 4877b31..03c415a 100644 |
| --- a/internal/mapa/import_lows_test.go |
| +++ b/internal/mapa/import_lows_test.go |
| @@ -18,7 +18,7 @@ import ( |
| func (world *importWorld) fileRequests() int { |
| count := 0 |
| for _, request := range world.server.Requests() { |
| - if strings.HasPrefix(request.Path, "/secure.notion-static.com/") { |
| + if request.File { |
| count++ |
| } |
| } |
diff --git a/internal/mapa/import_rerun_test.go b/internal/mapa/import_rerun_test.go| index fd710b8..5eae49f 100644 |
| --- a/internal/mapa/import_rerun_test.go |
| +++ b/internal/mapa/import_rerun_test.go |
| @@ -64,12 +64,47 @@ func TestI2ARerunWritesNothing(t *testing.T) { |
| t.Error("a re-run of an unchanged workspace wrote a revision") |
| } |
| for _, request := range world.server.Requests() { |
| - if strings.HasPrefix(request.Path, "/secure.notion-static.com/") { |
| + if request.File { |
| t.Errorf("a re-run downloaded %s", request.Path) |
| } |
| } |
| } |
| +// I2's other half, from the first real run: Notion rounds |
| +// last_edited_time to the minute (spec §25 note 17), so an edit made in |
| +// the same minute as the last run's survey carries the time that run |
| +// saw, on the page and on the block. It is picked up anyway: "changed" |
| +// is the rendered body against what Mapa holds (§13.2), never the edit |
| +// time. Two such edits in a row, each in that minute, are each picked |
| +// up, and a run after them writes nothing. |
| +// |
| +// Mutation (make mutate, first red 2026-11-22): a page whose Notion edit |
| +// time is the one last written is read as unchanged. |
| +func TestI2AnEditInTheSameMinuteIsPickedUp(t *testing.T) { |
| + ws := newSmall() |
| + world := newImportWorld(t, ws.Workspace) |
| + world.connect(ana) |
| + world.importAs(ana) |
| + minute := ws.Objects[ws.handbook]["last_edited_time"] |
| + for _, words := range []string{"Hello.", "Hello again."} { |
| + ws.set(ws.handbook, 1, notiontest.Para(notiontest.Text(words))) |
| + if ws.Objects[ws.handbook]["last_edited_time"] != minute || |
| + ws.Children[ws.handbook][1]["last_edited_time"] != minute { |
| + t.Fatal("the edit moved an edit time: the test needs it held") |
| + } |
| + run := world.importAs(ana) |
| + _, record, _ := world.pageTitled("Handbook") |
| + if run.Updated != 1 || !strings.Contains(record.Body, words) { |
| + t.Fatalf("an edit in the same minute: %+v, body %q", run, |
| + record.Body) |
| + } |
| + } |
| + if run := world.importAs(ana); run.Unchanged != 3 || |
| + run.Updated != 0 { |
| + t.Errorf("the run after: %+v", run) |
| + } |
| +} |
| + |
| // I3, untouched and kept (spec §13.2, D8). Two pages change in Notion, |
| // one of them renamed; one of those is edited in Mapa, and a third is |
| // renamed in Mapa. The untouched one updates as the runner, renamed |
diff --git a/scripts/mutate/mutations_import.go b/scripts/mutate/mutations_import.go| index b2ba42e..6e83143 100644 |
| --- a/scripts/mutate/mutations_import.go |
| +++ b/scripts/mutate/mutations_import.go |
| @@ -280,9 +280,23 @@ var importMutations = []mutation{ |
| "TestI14AnImageGoneOrChangedIsFetchedAgain", edit{importImages, |
| "job.images.Held(ctx, prior.PublicID, entry.place.PageID) {", |
| "true {"}), |
| - inMapa("I14", "reuse ignores the block's edit time", |
| + inMapa("I14", "reuse ignores the file's key", |
| "TestI14AnImageGoneOrChangedIsFetchedAgain", edit{importImages, |
| - "if found && prior.Edited == block.Edited {", "if found {"}), |
| + "if found && sameFile(prior.Key, placed.key) {", "if found {"}), |
| + // What comparing the minute-rounded edit time did, for a file |
| + // replaced in that minute (§25 note 17). |
| + inMapa("I14", "reuse ignores the file's key, the same minute", |
| + "TestI14AnImageReplacedInTheSameMinuteIsFetched", edit{importImages, |
| + "if found && sameFile(prior.Key, placed.key) {", "if found {"}), |
| + inMapa("I14", "an empty file key matches", |
| + "TestAnEmptyFileKeyMatchesNothing", edit{importImages, |
| + "return kept != \"\" && kept == now", "return kept == now"}), |
| + inMapa("I2", "an unchanged Notion edit time read as an unchanged page", |
| + "TestI2AnEditInTheSameMinuteIsPickedUp", edit{importWrite, |
| + "\tif entry.current.Title == entry.title &&\n", |
| + "\tif entry.current.Origin != nil &&\n" + |
| + "\t\tentry.current.Origin.Edited == entry.staged.Edited ||\n" + |
| + "\t\tentry.current.Title == entry.title &&\n"}), |
| inMapa("I14", "no copy is tried", "TestI14ABlockOnASecondPageIsCopied", |
| edit{importImages, |
| "if added, ok := job.copyImage(ctx, entry, block); ok {", |
| @@ -307,10 +321,10 @@ var importMutations = []mutation{ |
| inMapa("Low5", "the fix-up renders with no image lines", |
| "TestLow5TheFixUpKeepsImagesAfterAResume", edit{importJob, |
| "\t\timages = storedImages(entry)", "\t\timages = &pageImages{}"}), |
| - inMapa("R3M2", "a copy ignores the block's edit time", |
| + inMapa("R3M2", "a copy ignores the file's key", |
| "TestI14AChangedSharedImageIsFetchedOnce", edit{importPlace, |
| - "other.Page == 0 || other.Edited != block.Edited ||", |
| - "other.Page == 0 ||"}), |
| + "other.Page == 0 || !sameFile(other.Key, key) ||", |
| + "other.Page == 0 || !sameFile(key, key) ||"}), |
| inMapa("R3M3", "Forget leaves notion_images", "TestM6ForgetDeletes", |
| edit{importCredential, |
| "err = tx.db.Where(\"1 = 1\").Delete(&NotionImage{}).Error", |