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
35 changes: 35 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: MDXP CI

on:
push:
branches: [main]
pull_request:

permissions:
contents: read

concurrency:
group: mdxp-ci-${{ github.ref }}
cancel-in-progress: true

jobs:
contract:
name: MDXP contract checks
runs-on: ubuntu-24.04
timeout-minutes: 10
steps:
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
persist-credentials: false
- uses: pnpm/setup@84cb39b217b10273981911c288cd62326dc7c6d2 # v2.0.2
with:
runtime: node@24.18.0
cache: true
install: false
- run: pnpm install --frozen-lockfile
- run: pnpm run lint
- run: pnpm run typecheck
- run: pnpm test
- run: pnpm run build
- name: Verify packaged public entry points
run: pnpm run check:package
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,10 @@ an invalid request. A pre-dispatch directory rejection uses `InvalidParams` with
included. Filesystem checks are not an OS sandbox: a local process that can
replace directories concurrently is outside this guarantee.

## Download handoff (unreleased contract)

The optional `downloadHandoff` capability defines prepare/commit/status/abort for extension-owned confirmation. This package adds wire schemas, typed methods and shared payload fingerprint encoding; it does not enable application support. See the [handoff v1 contract](docs/download-handoff.md) for durable ledger, cancellation and recovery requirements. Hosts must keep the capability absent until those integration requirements pass.

## License

[MIT](./LICENSE) © Dr_rOot
4 changes: 4 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,10 @@ RPC handler 需要保留错误码和 data 时,应抛出从 `@motrix/mdxp` 导
不返回底层文件系统错误细节。目录校验不是操作系统沙箱,不能防御有权限并发替换
目录的本地进程。

## 下载交接(尚未发布的契约)

可选 `downloadHandoff` 能力定义扩展端确认使用的 prepare/commit/status/abort。本包新增传输 schema、强类型方法及共享载荷摘要编码,不直接启用应用支持。持久账本、取消与恢复要求见[交接 v1 契约](docs/download-handoff.zh-CN.md);服务端通过对应集成验收前必须保持该能力缺省。

## License

[MIT](./LICENSE) © Dr_rOot
243 changes: 243 additions & 0 deletions docs/download-handoff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
# Download handoff v1

This document defines the opt-in contract added by this change. The package
provides schemas, typed RPC methods and canonical payload encoding. It does not
implement a durable ledger, browser controller or download engine. A host must
not advertise support until the persistence and fault requirements below are
implemented and tested. Existing `download/submit` behavior is unchanged.

## Negotiation and scope

An authenticated extension uses these methods only when initialize returns:

```json
{
"capabilities": {
"downloadHandoff": {
"version": 1,
"instanceId": "c2b6a2d2-430c-4bc1-bdb0-c10f35004c90",
"maxPreparedTtlMs": 30000,
"acceptingNew": true
}
}
}
```

Absence means unsupported. All four handlers and the durable activation path must
be ready before advertising this capability. These methods are extension-only:
no agent catalog or unary HTTP exposure. `agentFacing: false` is not authorization.
The transport must authenticate the principal before dispatch, independently of
caller-supplied initialize fields. No user confirmation is requested by Motrix;
the extension owns the confirmation UI and freezes its options before prepare.

V1 supports a direct HTTP(S) GET only. POST, bodies, blob URLs, media resolution,
HLS, DASH, mux and magnet are outside this handoff contract. Strict nested request
schemas reject unknown options instead of silently downgrading them to GET. A
valid URL is not permission to access it; normal host policy still applies. An
already-open browser response cannot be transferred to the engine; accepting a
Motrix handoff still needs another request and cannot guarantee one-use URLs.

## Independent release and rollback

Browser-store review delay and disabled automatic updates are normal supported
states, not exceptions. Neither side may require synchronized installation.
Keep protocolVersion `1.0` and the existing initialize, submit, task and pairing
contracts. `download/submit` remains a first-class path with its existing request,
response and error semantics, even after handoff is available. Do not redirect it
to prepare/commit or require an instance/key field that older extensions do not
send. Do not raise the minimum extension version just to enable this feature.

| Extension | Motrix | New download path |
| --- | --- | --- |
| Existing | Existing | Existing submit |
| Existing | Upgraded | Existing submit, unchanged |
| Upgraded | Existing | Existing submit, unchanged |
| Upgraded | Upgraded, either rollout gate off | Existing submit |
| Upgraded | Upgraded, both gates on and v1 supported | Handoff for eligible requests only |
| Upgraded | Host advertises an unknown/invalid handoff capability | Existing submit for new operations; base handshake still works |

Capability support and rollout are separate. `acceptingNew` defaults to false;
`canStartDownloadHandoff(advertisement, clientEnabled)` also defaults the client
gate to false. This helper is only for **new operations before any handoff
mutation is sent**. It must never be used to reroute an existing operation.
Invalid/unknown optional capability data is discarded by initialize parsing;
invalid mandatory base-protocol data still fails validation. Supported v1 mutation
params and results remain strict. Client implementations must retain the legacy
adapter/submit path and recover pending handoffs independently of these gates.

Release order is additive, without a deadline tied to store approval:

1. Release the SDK and a host retaining all legacy handlers, with new handoffs
disabled. Verify an actual released extension against this host.
2. Ship an extension containing both paths and a disabled client rollout gate.
It must work against hosts without this capability throughout store review.
3. Enable new operations only after both sides meet the integration acceptance
requirements, at their own release cadence. Never infer support from a
version string, store approval date or the presence of one method.

To stop rollout, set `acceptingNew` false and reject the first prepare of an
absent key with `CapabilityNotSupported`, without effects. Already prepared keys
must still support repeated prepare, commit, status and abort. Capability changes
may require renegotiation, so servers enforce the gate on dispatch, not only in
initialize. A stale client denied before prepare still uses same-key recovery
and must not blindly start legacy submit. It can choose a legacy path for a
fresh operation only before any handoff mutation was transmitted.

Do not remove handlers, discard the ledger or roll back to a binary without
handoff recovery after any handoff could have been accepted. Use a compatible
rollback build retaining the journal, activation reconciliation and handlers;
new operations use legacy while existing ones finish/recover. A crash, reset or
forced downgrade does not justify replaying unresolved work. Reverting an SDK
version alone is not an application rollback procedure.

The SDK CI tests use the **published 0.8.1 package**, through an exact dev-only
alias, for old/new initialize and submit round trips. They establish wire
compatibility, not production host route retention, browser-store distribution
or database migration safety. Released extension/host cross-version E2E and
rollback with in-flight operations remain mandatory before enabling rollout.

## Identity and payload

Every request carries `{ instanceId, operationId }`, both UUIDs. The operation is
keyed by **durable ledger instance + authenticated principal + operationId**, not
URL, socket, tab or claimed extension ID. Separate clicks get separate operation
IDs. Reconnect/retry uses the original ID and immutable payload. Persist the key,
authenticated backend binding and confirmation state before sending mutations.
Never move a pending operation to another backend or credential identity.

`instanceId` identifies a durable ledger generation. It survives ordinary server
restarts, but MUST change if records are lost, reset or restored to an older
snapshot. Every handler compares it before any effect, returning
`HandoffInstanceChanged` (-32007) for a mismatch. The client must treat an old
pending operation as unresolved; a new instance is not evidence that the old
engine never started it. Never automatically create a replacement operation.

Prepare carries `download`: the existing submit source, metadata and optional
`saveDir`, restricted to a direct selection, without a second idempotency key.
`canonicalDownloadHandoffPayload()` parses and normalizes schema defaults, sorts
object keys by UTF-16 code units recursively, preserves array order, and encodes
with JSON string/number escaping. SHA-256 over the resulting UTF-8 bytes is the
lowercase 64-character `payloadHash`. This is an MDXP-specific encoding, not a
claim of general JSON canonicalization. Do not normalize URL queries, header
names or reorder cookies independently. Freeze timestamps and options on retry.

The host computes this hash itself, stores it with the reserved task ID, and
rejects a reused ID with different content using `HandoffPayloadConflict`
(-32008). Clients verify response key and hash against the frozen operation;
commit includes that same hash. Do not log payloads: they may contain cookies,
authorization headers or signed URLs. Hashes are correlation values, not secrets
or authorization proofs.

## Methods and results

| Method | Params | Possible result states |
| --- | --- | --- |
| `download/handoff.prepare` | key + `download` | prepared, committed, aborted, expired |
| `download/handoff.commit` | key + `payloadHash` | committed, aborted, expired, not-found |
| `download/handoff.status` | key | prepared, committed, aborted, expired, not-found |
| `download/handoff.abort` | key | aborted, committed, expired |

All results echo the key. `prepared` includes reserved `taskId`, `payloadHash`
and `expiresAt` (server Unix milliseconds). `committed` includes the same task ID
and hash. Other results contain only key and state. `committed` means durable
ownership/activation intent, not completed download; later task failures stay
with Motrix and must not cause browser replay. A removed or failed task does not
make its handoff record disappear or reusable.

Prepare performs only local validation and creates an inactive durable record.
It must not probe/fetch the source, call URL adapters, start the engine, or enqueue
active work. Resolve an omitted default directory to a concrete permitted path
and freeze it in the record, independently of the original payload hash. A later
default change must not redirect the task. Revalidate permissions and the frozen
path before commit; refusal leaves it inactive. Never silently substitute paths.
The host chooses an expiry no later than `maxPreparedTtlMs` after its current
clock. Repeating prepare does not extend that expiry or replace the task ID.

All operations on a key, including expiry, are serialized in the same durable
transaction domain. At the expiry boundary (`now >= expiresAt`), an uncommitted
record becomes expired and cannot commit. Do not evaluate deadlines only when
receiving RPC bytes; check again while holding the transaction's serialization
boundary. Crash recovery must not grant an expired record a new lifetime.

| Current state | prepare (same payload) | commit (same hash) | abort | status |
| --- | --- | --- | --- | --- |
| absent | persist prepared | not-found; no effect | persist aborted tombstone | not-found; no effect |
| prepared, not expired | unchanged prepared | persist committed + activation intent | persist aborted | prepared |
| prepared, expired | persist expired | persist expired | persist expired | persist expired |
| committed | committed | committed | committed | committed |
| aborted | aborted | aborted | aborted | aborted |
| expired | expired | expired | expired | expired |

Conflicting hashes/payloads fail before mutation when a stored hash exists,
including terminal records. A tombstone created before prepare has no payload;
any later prepare returns aborted and must not bind or start a task. Abort of an
absent operation MUST create that tombstone. A status miss is only an observation:
a delayed prepare or commit could still arrive. It is never permission to replay.

Commit atomically records the ledger transition, task and durable activation
outbox/intent before replying. A recoverable engine dispatcher must reconcile a
stable task/engine identity before retrying an activation after a crash. An
in-memory Promise cache or a database transaction around only the ledger does
not meet this contract. Do not claim exactly-once network transfer from these
requirements: the engine/source boundary and browser cancellation are separate.

Terminal records may compact to key, state, task ID and hash as applicable, but
must remain deduplicating for the lifetime of the instance. Never evict them by
URL TTL or cache pressure. To reclaim an entire generation, fence its live
sessions and rotate the instance; historical tasks may still exist and clients
must not replay old operations. Capacity exhaustion refuses new prepare/abort
mutations explicitly rather than acknowledging a tombstone that was not stored.

## Browser ownership and recovery

1. Keep the original browser request under adapter control while the extension
collects confirmation. Choosing browser continues that original response;
explicit cancel stops it. UI drafts expire when the adapter's hold expires;
a UI timeout is not a promise that the browser can remain held that long.
2. Freeze the chosen options, persist the operation binding, then prepare.
Validate the prepared reply. Do not stop the browser merely because a request
was sent or a transport returned without a schema-valid response.
3. Stop the original browser transfer and verify the adapter's outcome, then
persist that evidence before commit. A failed or unknown browser stop must
not commit; attempt abort. Never replay a browser GET to hide that uncertainty.
4. Once commit might have been sent, timeout/EOF/malformed reply is unknown.
Reconnect only to the same instance/principal and query the same operation.
An abort result of aborted/expired is a durable fence; committed belongs to
Motrix. A lost abort response is also unknown. `not-found` alone is not a fence.
5. Restored drafts must not re-create operations or fabricate ownership of a lost
blocking Promise. Backend/config changes invalidate the live UI operation.
Recovery may query or abort its original authenticated backend; it must never
send that commit to the newly selected backend.

`$/cancelRequest` cancels RPC waiting/cooperative work; it does not create an abort
tombstone and cannot roll back a committed task. Application task cancellation is
separate. Errors, including InternalError, do not by themselves establish whether
a mutation committed. Use same-key recovery. Do not fall back to legacy
`download/submit` after a possibly transmitted handoff mutation.

## Acceptance required before host advertisement

These are integration requirements for the simulator and real host. Schema and
in-memory RPC tests in this package do not establish their persistence guarantees.
Both implementations should exercise the same scenarios with named barriers,
independently observing the ledger, engine activation and source HTTP requests.

| Fault/scenario | Required observation |
| --- | --- |
| prepare, repeat prepare | one inactive record/task ID; no source request or engine work; unchanged expiry |
| different payload or commit hash | conflict; no additional activation, including after commit |
| same URL, new operation IDs | independent records |
| different authenticated principal | no access to the other principal's record |
| abort arrives before delayed prepare/commit | durable tombstone; later messages cannot activate |
| commit wins abort race | abort returns same committed task; exactly one activation intent |
| reply lost after commit transaction | status/retry returns same task and hash; no browser replay |
| engine accepted activation, crash before acknowledgement | recover by stable identity; no second task |
| status not-found while prepare is in flight | no automatic fallback; abort fences the late prepare |
| expiry exactly at commit boundary, restart after expiry | terminal expired; no activation or TTL renewal |
| reset/restore loses ledger | new instance; old requests refused; old client outcome remains unresolved |
| disk full/transaction rollback | no success acknowledgement without durable record/outbox/tombstone |
| original browser cancellation fails or is unknown | no commit; no replacement GET |
| config/backend change or extension restart | no stale UI submission and no cross-instance replay |

Until these checks pass in the host and browser integrations, keep the capability
absent. The package contract alone does not enable or certify application support.
Loading