Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ For connectors: a small kit makes a connector a page of code, a template to copy
- `shared/`: private workspace packages shared by related connectors, today the Google sign-in in `shared/google`.
- `template/`: what a new connector starts from. `template/connector/` is copied to `connectors/<name>/`; beside it is its README. A connector runs under the connector host.
- `connectors/<name>/`: one folder per connector, run and never published.
- `connectors/<name>/plugin/`: the plugin each connector is published as, in the shape `types/<name>/` has (`plugin.json` with a `connector` section, `types/<id>.json` with `<id>.md` beside it, `edge-types/<id>.json`). Everything but the Markdown is generated from the connector's `connector()` in its `src/definition.ts` (`src/main.ts` only starts it): `title`, `mark`, `maker`, `source`, its `settings` (each a `name` in lowercase snake case, a `label`, a `kind` of `text`, `number`, `boolean` or `choice`, `secret` for text only and never with a `default`, `default`, `required` and `choices`) and its `types`, each a kind with a `name`, a `direction`, and `tags` and `favorite` where it brings them. The connector's `edge_types` are the edge types of its `connections`, its own and Marfa's, so the key a host mints writes exactly those. A host-minted key writes only its kinds' types, so a definition declares every type the connector can ever write in its widest shape; one that changes with a setting takes the settings as `connector(settings)`, `main.ts` hands `connector` itself to `main`, which builds it again from the settings Marfa holds at the start of every run, and the definition exports `widest`, the settings that give it its widest shape, which the generator builds it from. A definition reads no `process.env`. After `pnpm build`, `pnpm connectors:generate` rewrites the JSON and `pnpm connectors:check`, which `Checks` runs, fails when any is out of date, when a type has no guidance or when a guidance file has no type; never edit the JSON by hand. Write each type's guidance, at most 1,000 characters, in `types/<id>.md`.
- `connectors/<name>/plugin/`: the plugin each connector is published as, in the shape `types/<name>/` has (`plugin.json` with a `connector` section, `types/<id>.json` with `<id>.md` beside it, `edge-types/<id>.json`). Everything but the Markdown is generated from the connector's `connector()` in its `src/definition.ts` (`src/main.ts` only starts it): `title`, `mark`, `maker`, `source`, its `settings` (each a `name` in lowercase snake case, a `label`, a `kind` of `text`, `number`, `boolean` or `choice`, `secret` for text only and never with a `default`, `default`, `required` and `choices`) and its `types`, each a kind with a `name`, a `direction`, `default_mapping` with `enabled: false` where it ships off, and `tags` and `favorite` where it brings them. The connector's `edge_types` are the edge types of its `connections`, its own and Marfa's, so the key a host mints writes exactly those. A host-minted key writes only its kinds' types, so a definition declares every type the connector can ever write in its widest shape; one that changes with a setting takes the settings as `connector(settings)`, `main.ts` hands `connector` itself to `main`, which builds it again from the settings Marfa holds at the start of every run, and the definition exports `widest`, the settings that give it its widest shape, which the generator builds it from. A definition reads no `process.env`. After `pnpm build`, `pnpm connectors:generate` rewrites the JSON and `pnpm connectors:check`, which `Checks` runs, fails when any is out of date, when a type has no guidance or when a guidance file has no type; never edit the JSON by hand. Write each type's guidance, at most 1,000 characters, in `types/<id>.md`.
- `host/`: the connector host, `marfa-connector-host` (`@withmarfa/connector-host`, private), with `host/README.md` saying how an owner uses it. It links a machine to a Marfa with a one-time link, runs as a LaunchAgent (`so.marfa.connector-host`) or a systemd user unit (`marfa-connector-host.service`), asks `GET /connector-hosts/self` every 60 seconds what to run, fetches each connector's code from the `connector.code` its plugin names (https only, cut past `size`, kept only when its SHA-256 matches), mints each connector's key through `POST /connector-hosts/self/connectors/{plugin}/key`, and supervises one process per connector: started as `<node> --permission --allow-fs-read=<code> --allow-net --no-experimental-sqlite --no-experimental-ffi --max-old-space-size=512 <code> --every 15m` (`src/permission-flags.ts`) with only `MARFA_API_URL`, `MARFA_API_KEY`, `PATH`, `HOME`, `TZ`, `LANG`, `LC_*` and, when the host has `OTEL_EXPORTER_OTLP_ENDPOINT`, `OTEL_*` and `MARFA_OTEL_*` in its environment, restarted or held by its exit code (`host/src/supervisor.ts`), woken with `SIGUSR2` for run now and once for a pause, which the connector ends by stopping itself after the run under way (it sends `SIGTERM` only when a connector leaves the host, its version or code changes, a file its sandbox was given is replaced, the sandbox can start again, or the host stops), and its key and code deleted when it leaves the host. On macOS the process is `sandbox-exec` with the profile in `host/src/macos-profile.ts`, and on Linux it is `lib/sandbox`. The host records the executable it spawned as `launch`. Both modes keep the Node flags. The host probes the sandbox for each start (`src/sandbox.ts`, one probe shared by starts at the same time, no answer kept), passes that answer to that start, and logs why it is not `on` once for each change; an exit never decides it. The answer is `off` only for `SandboxUnavailable` (`src/sandbox-plan.ts`), which names what no connector can cause: no launcher or one that cannot run, the launcher's exit `69` (it decides by the kernel's error), `sandbox-exec` refused or its profile not understood, or something the sandbox must refuse left open. Any other failure, a timeout or a lack of ports, memory, processes or descriptors included, is `unknown`: the connector is not started, and the host checks again after 5 seconds, doubling to 5 minutes, whatever the owner asks for. While a connector runs with `off`, the host checks again after a minute, doubling to an hour until a start is `on`, and once the answer is `on` stops it with `SIGTERM` and starts it again. A Landlock rule holds a file, not its path, so on Linux the host takes the identity (device, inode and birth time, since a file system reuses inode numbers) of each path `lib/sandbox --inputs <node>` prints before the probe (`src/inputs.ts`), and restarts a sandboxed connector once one is replaced and they have then stayed the same for 5 seconds. A write in place counts only for `/etc/nsswitch.conf`, which the rules are made from. At least a minute passes between these restarts, doubling to 15 minutes, until an hour passes without one. The launcher allows the libraries of each service on the `hosts:` line of `/etc/nsswitch.conf` and the one Unix socket `resolve` or an `mdns` service uses (Landlock ABI 9), and allows route netlink sockets for `myhostname`; keeps the fence with `mymachines` and lets only the names of registered machines fail; it refuses to start for an installed service it does not know. Its filter lets `clone` start only a thread, with no namespace flag, and answers `clone3` with `ENOSYS`, so glibc uses `clone`. It is a static musl binary (`host/sandbox/.cargo/config.toml`). The host durably reserves a sequence before each launch and reports that sequence and mode with the current connector key ID to `PUT /connector-hosts/self/connectors/{plugin}/sandbox`. `marfa-connector-host status` prints "This connector is running without the operating-system sandbox, because it could not be started on this machine. It still has the host's usual limits." and still says the connector is running. The owner reads this report as `host_sandbox`, independently of connector state and problems. `src/supervisor.ts` holds the loop and its waits, `src/code.ts` the fetch, `src/child.ts` the command line and environment, `src/service.ts` the service files and `src/commands.ts` the commands. Under the permission model Node never delivers `SIGUSR1` to a listener, so the wake signal is `SIGUSR2`. The service runs `<home>/current/bin/marfa-connector-host`, a shell script that starts the Node beside it (`../lib/node ../lib/host.mjs`) with no extra flags, and `run` listens for `SIGUSR1` itself, because `SIGUSR1` from any process of the same user opens a Node process's inspector. An update points `current` at `versions/<version>/` and `run` exits `75`, which the service restarts on; a clean stop stays `0`. `marfa-connector-host update` installs a newer release the same way, and `run` checks once a day, off its loop and one check at a time, unless `host.json` holds `"updates": false` (`marfa-connector-host updates on|off` writes it) or a `run` the owner supervises has `MARFA_CONNECTOR_HOST_NO_UPDATE=1`; the service does not take that variable from a shell. Every fetch has a deadline (30 seconds to the first byte, 60 seconds in all, 10 minutes for the archive) and stops when the host stops. The repository it updates from is written into the bundle at build time from `scripts/release/repository.ts`, so a fork's release updates from the fork. Each stager (the daily check, `update`, `install-host.sh`) unpacks into its own `versions/.stage-<pid>-*` folder; placing a release (`src/update.ts`, and `marfa-connector-host activate <version>`, which `install-host.sh` runs from the staged copy) holds `update.lock`, taken like `run.lock`, sets an existing copy of that version aside and puts it back if anything after fails, and switches `current` by renaming a new link over it. Every step that changes the tree at `versions/<version>` or at a copy set aside is one rename, and nothing is deleted under either name: a copy to delete (a failed copy, a copy set aside that is no longer needed, a pruned release) is first renamed to a name of its own, `.discard-<random>`, so a stop at any point leaves a whole copy there or none. The next placement puts back a copy set aside when none is there, deletes it when one is, and deletes any copy to discard. Pruning removes only finished version folders and keeps the two newest, the one `current` names and the one a live `run` holds `run.lock` for. It also holds `node.json`, the Node 26 release the host carries: its version and, for `darwin-arm64`, `darwin-x64`, `linux-arm64` and `linux-x64`, the archive's file name and SHA-256, copied from that release's `SHASUMS256.txt` after its signature was checked against the Node.js release keys. Nothing else names the version: CI and the release read it from here.
- `scripts/`: the script that fetches the pinned release's server and `marfa`, `check-plugins.ts` and `release/` (the release build, and what a plugin owes the published versions), the proof harness, which builds a connector of its own on the kit, with the vendor stubs it shares with the connectors' tests, and tests that refuse a version in a manifest or a file holding one, a home directory, machine name or personal address, tracked build output, a package left out of the build, and a workflow job off standard GitHub-hosted runners. It also holds the scripts that carry Node and bundle the connectors: `node-pin.ts` fetches the pinned Node archive for a platform, refuses it unless its SHA-256 is the pin's, keeps only `bin/node` and the license and, on macOS, checks the signature (`codesign --verify --strict`, Developer ID team `HX7739G8FX`, hardened runtime); `bundle-connectors.ts` bundles each connector into one file; `bundle-files.ts` names a bundle and finds one in a folder; `check-bundles.ts` runs each bundle's `--help` under them; and `follow-node.sh` decides the Node pin's move; `only-staged-paths.sh` is what a workflow that applies a patch from a job that ran fetched code calls to refuse any path but its own.

Expand Down
49 changes: 43 additions & 6 deletions connectors/readwise/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# Readwise

Brings the sources and complete highlights of one Readwise account into Marfa, with their annotations and tags. With Reader turned on, it also brings Reader documents, highlights and available content, and carries supported edits back to Reader. By default, Reader is off and the connector writes nothing to Readwise.
Brings the sources and complete highlights of one Readwise account into Marfa, with their annotations, tags and favorites. With Reader turned on, it also brings Reader documents, highlights and available content, and carries supported edits back to Reader. By default, Reader is off and the connector writes nothing to Readwise.

## What it brings

| Kind | Type | Carries changes back |
| ----------- | -------------------- | -------------------- |
| `source` | `readwise.source` | Yes, with Reader on |
| `highlight` | `readwise.highlight` | Yes, with Reader on |
| `file` | `readwise.file` | No |
| Kind | Type | Carries changes back | Ships |
| ----------- | -------------------- | -------------------- | --------------------- |
| `source` | `readwise.source` | Yes, with Reader on | On |
| `highlight` | `readwise.highlight` | Yes, with Reader on | On |
| `file` | `readwise.file` | No | On |
| `picture` | `readwise.picture` | No | Off, until you enable |

## Add it

Expand All @@ -18,6 +19,42 @@ Brings the sources and complete highlights of one Readwise account into Marfa, w
4. Store your Readwise access token: `marfa plugins secret readwise readwise_access_token`.
5. To bring Reader documents too, turn Reader on: `marfa plugins settings readwise --set readwise_reader=true`. Default: `false`, which writes nothing to Readwise.
6. Optionally map the kinds to your own types and tiers: `marfa plugins mapping show readwise`, then `marfa plugins mapping set readwise <file>`.
7. Optionally save the pictures that Reader documents draw from other websites: the `picture` kind ships off, so turn it on as [Pictures](#pictures) says.

## Tags and favorites

The tags of a source and of a highlight arrive as Marfa tags, and a favorite highlight arrives with the Marfa tag `favorite`. They are no longer properties of the item. The first run after an upgrade clears the old `tags` and `is_favorite` properties of every row it writes, whatever you choose for `tags` and `favorite` in the mapping. With the default, `tags: marfa`, and the favorite on, it writes the same tags as Marfa tags and the favorite as the `favorite` tag, so nothing is lost. If you map `tags` to `none`, or turn the favorite off, the old values are cleared and kept nowhere in Marfa: they stay in Readwise only. If you map `tags` to a field, the tags go to that field and the old `tags` property is still cleared.

Marfa follows Readwise tag by tag. A tag added in Readwise is added in Marfa, a tag removed in Readwise is removed in Marfa, and a tag you added in Marfa stays.

A tag you add or remove in Marfa goes back to Reader only for an item Reader manages, and only with Reader on and not inbound only. The connector sends the one tag that changed and leaves tags that only Reader holds. Readwise has no way to set a favorite, so a `favorite` tag you add or remove in Marfa stays in Marfa. Where the export gave the highlight's favorite, the connector also raises a condition that says so. A tag on an item that only the export holds cannot reach Readwise: it stays in Marfa, with a condition that says so.

## Thumbnails

Each source's `thumbnail` is made from its `image_url`: the connector fetches the image and keeps a small copy. It is made again only when the image's address changes. A source whose image cannot be fetched or made has no thumbnail, and that is not an error. A refusal that comes again for the same address, such as an image that is not a picture the connector can decode, is remembered, so the address is not fetched again until it changes. A failure that may pass, such as a timeout or a busy host, is tried again after 30 minutes, so a few sources that keep failing do not hold back the rest. A run makes at most 50 thumbnails, so an account read for the first time fills in over a few runs. Only public hosts are fetched.

## Pictures

The `picture` kind saves a picture that a Reader document draws from another website as a `readwise.picture` file, attached to the source. It ships off. While it is off the connector plans no picture, saves none and keeps no picture work.

To turn it on, read the mapping with `marfa plugins mapping show readwise`, then save a file that enables the kind at the version the mapping shows:

```yaml
version: 1
kinds:
picture:
enabled: true
```

```bash
marfa plugins mapping set readwise mapping.yaml
```

A kind you leave out of the file is stored as its default, so keep the choices you already made for the other kinds in the file. Turn the kind off again with `enabled: false`: the pictures already saved stay where they are.

Pictures saved by the earlier release stay as `readwise.file` items and are still counted as kept, so none is fetched again. New pictures are saved as `readwise.picture` items.

Content that Readwise hosts itself, such as a document's HTML and the images and original it keeps in Readwise's own storage, is saved as `readwise.file` items. That kind ships on, and this setting does not change it. Only a picture a document draws from another website is the `picture` kind.

## Read more

Expand Down
27 changes: 16 additions & 11 deletions connectors/readwise/plugin/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,26 @@
"source": "readwise",
"source_name": "Readwise",
"kinds": [
{ "name": "source", "type": "readwise.source", "direction": "both" },
{
"name": "source",
"type": "readwise.source",
"direction": "both",
"tags": true
},
{
"name": "highlight",
"type": "readwise.highlight",
"direction": "both"
"direction": "both",
"tags": true,
"favorite": true
},
{ "name": "file", "type": "readwise.file", "direction": "in" }
{ "name": "file", "type": "readwise.file", "direction": "in" },
{
"name": "picture",
"type": "readwise.picture",
"direction": "in",
"default_mapping": { "enabled": false }
}
],
"edge_types": ["excerpt-of", "references", "attached-to"],
"settings": [
Expand Down Expand Up @@ -67,14 +80,6 @@
"default": false,
"required": false
},
{
"name": "readwise_reader_pictures",
"label": "Reader pictures",
"kind": "choice",
"default": "addresses",
"required": false,
"choices": ["addresses", "save"]
},
{
"name": "readwise_reader_inbound_only",
"label": "Never send edits to Reader",
Expand Down
4 changes: 2 additions & 2 deletions connectors/readwise/plugin/types/readwise.highlight.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"tags": {
"type": "array",
"items_type": "string",
"description": "Trimmed, distinct Readwise tag names, preserving case and Unicode."
"description": "No longer written: the connector clears it. Readwise tags are the row's Marfa tags."
},
"readwise_source_id": {
"type": "string",
Expand Down Expand Up @@ -60,7 +60,7 @@
},
"is_favorite": {
"type": "boolean",
"description": "The flag supplied by Readwise."
"description": "No longer written: the connector clears it. A favorite highlight is Marfa's favorite tag."
},
"is_discard": {
"type": "boolean",
Expand Down
Loading