Roadmap: beyond tokens
The effect analysis closed the largest gap: psync no longer runs recognized writes before an earlier exit, and it keeps operations on one file or service in order. But it still reads names, not meaning. This chapter collects what would make the guarantees stronger, roughly in the order it would pay off. Each item says what problem it solves, the idea, and what it costs.
The goal stays the one in the proposal: the generated program is logically identical to the sequential one. In practice that means two properties:
- Same exits. The function returns the same value or error from the same place. psync guarantees this today by replaying exits in program order.
- Same effects. The outside world sees the same operations, each resource in the same order, and nothing the sequential program would not have done. psync guarantees this today only for effects it recognizes, and only as far as targets can be told apart from tokens.
Everything below strengthens the second property.
1. Runtime disambiguation of targets
Problem. fs::write(&a, ..) and fs::read(&b) conflict today, because a and b may name the
same file. In a loop, psync assumes that items name different objects, which is false for a list
with duplicates.
Idea. Do what vectorizing compilers do with runtime alias checks. When both targets are pure expressions, evaluate them first and branch:
if __psync_target(&a) != __psync_target(&b) {
/* concurrent schedule */
} else {
/* sequential schedule */
}
For loops, collect the targets of the prefetched writes into a set before starting. Fall back to
sequential order from the first duplicate. File paths can be compared after lexical normalization,
and optionally after canonicalize (one extra syscall per path).
Result. The loop assumption becomes a check, and computed targets stop blocking concurrency. Cost. Code size, one comparison per pair, and the rule that target expressions must be pure.
2. Effect contracts checked by the type system
Problem. Names lie. cache.get(k) may write a refresh, and factory.create(..) may only build a
value in memory. A proc macro cannot look up what a callee does.
Idea. Let crates declare effects on types, and let rustc check psync’s guess. Most client APIs
already give each operation its own type: AWS fluent builders (GetObjectFluentBuilder), kube’s
Api<K> methods, reqwest’s RequestBuilder. psync would ship a trait:
pub trait Effect { const KIND: EffectKind; } // Read, Write, Ordered
impl Effect for aws_sdk_s3::operation::get_object::builders::GetObjectFluentBuilder {
const KIND: EffectKind = EffectKind::Read;
}
Those impls would live in small psync-effects-* crates per ecosystem, which the orphan rule
allows because psync owns the trait. At every call it classified, the generated code would emit a
zero-cost assertion, psync::__private::claims::<Read, _>(&builder). When the type has an impl
that disagrees with the name, compilation fails with a message pointing at the call. For your own
functions, #[psync::effect(read)] on the definition would generate the impl for a marker type
that the function returns.
Result. Name-based classification becomes a checked claim wherever an ecosystem crate covers
the API, which is most of the I/O in the audit.
Cost. Maintaining impls for popular crates, and const evaluation in generated code.
3. An effect sanitizer for tests
Problem. Static analysis will always miss something: a callee that writes, two clients to one server, a symlink. ReSynchronizer and similar tools rely on the project’s tests passing, which says little about effects that tests do not assert on.
Idea. A dynamic checker in the spirit of ThreadSanitizer, but for the outside world:
- With
PSYNC_AUDIT=1, generated code wraps each chain’spoll_stepin__private::enter(chain_id)/leave(), which sets a thread-local current chain and records when each chain started and finished, and whether its result was used or discarded by an earlier exit. - An effect log captures what actually happened, attributed to the current chain:
- files and processes: an
LD_PRELOADshim, orseccompuser notification on Linux, recordsopenatwith write flags,unlink,rename,mkdir,execve; - network: a
tracinglayer, or atower/reqwestmiddleware that records method and URL, and a hyper header (x-psync-chain) for server-side logs; - services: kube’s and the AWS SDK’s existing interceptors.
- files and processes: an
- After the test, the checker reports
- speculative writes: a write in a chain whose output an earlier exit discarded;
- effect races: two chains in flight at once, touching one path, URL or object, at least one writing;
- unexplained effects: effects of calls the static analysis classified as reads or left
unclassified, with the call site, ready to paste into
effects(..).
A cheaper variant is differential tracing: run the test suite with PSYNC_GRAPH=off and with
psync, record effect traces, and require that each resource sees the same sequence of operations.
The end-to-end benchmarks already did this informally (“both builds sent the same API requests”).
Result. Every gap in the static model becomes a test failure with a precise report, instead of a silent behaviour change. Cost. A Linux-only shim for full coverage. Middleware alone is portable.
4. HTTP semantics
Problem. A service call is a read or a write, nothing finer. HTTP says more.
Idea. When a call names an HTTP method or uses a builder with a method argument
(request(Method::PUT, url)), use RFC 9110’s classes:
- GET, HEAD and OPTIONS are safe: they behave as reads;
- PUT and DELETE are idempotent, which makes retries safe, though not running them when sequential code would not have;
- POST and PATCH are neither.
When the URL is a literal or a format! with a literal prefix, use its host and path as the target,
so writes to different hosts or collections do not conflict.
Idempotency is not the same as being safe to speculate. Deleting an object the sequential program would have kept is still wrong. So idempotency only loosens ordering among repeated identical writes, never the speculation rule.
5. Staging speculative writes
Problem. A write must wait for every earlier exit, even when its result is not needed until much later.
Idea. For writes whose result only feeds ?, use write buffering as in thread-level
speculation:
- files: write to a temporary file in the same directory when the chain runs, then
renameit into place when the exits before it are replayed, or delete it if one of them leaves; - services with transactions or idempotency keys: prepare the operation, then commit it at the replay point;
- channels: queue the message locally and send it at the replay point.
Result. The slow part (uploading the bytes) overlaps earlier work, and the visible part (the rename or commit) still happens in program order, and only if sequential code would have done it. Cost. Only some APIs offer a prepare/commit split, and it changes when errors surface: a rename can fail after the upload succeeded.
6. Cancellation instead of completion
Today a join runs every chain to completion, and a loop runs every item’s prefix, even after an earlier exit. Dropping the futures of chains that an exit has made pointless would bound wasted work. But cancelling a future in the middle of a write leaves partial state: a half-written file, or a request the server got without the client reading the reply. So cancellation is only safe for chains with no write effects, which the effect analysis can now tell. A follow-up: cancel read-only chains early, and keep running chains that write.
7. Processes, the environment and the working directory
- Arguments of
Command.git config --get,lsandcatread. Recognizing read-only subcommands of common tools would stop every process from conflicting with everything. - Working directory.
env::set_current_dirchanges what every relative path means. Today it is an environment write. It should conflict with every file effect whose path is not absolute. - Environment variables are process-global, shared with other threads that psync does not see. A write to them should be a barrier, not just an ordered effect.
8. Resolved types and calls (proposal Phase I, step 3)
All of the above still works from tokens. A rustc driver that runs after type checking (on THIR or MIR) would know:
- the exact callee of every call;
- which arguments are
&mut; - whether a
&selfmethod uses interior mutability (Cell,RefCell,Mutex, atomics); - the callee’s own effects, transitively, as in the MOD/REF analyses that ReSynchronizer computes for JavaScript.
psync would keep the macro as the user interface and the code generator. The driver would refine the graph and fail the build when the macro’s token-level guess was too optimistic.
9. A machine-checked argument
The correctness argument is small enough to formalize. Model a region as statements with read/write sets over variables and effect targets, plus an exit flag. Then prove in Lean that every schedule which
- respects def-use and conflict edges,
- never starts a write before the exits preceding it, and
- replays exits in program order
produces the same final variable state, the same exit, and the same per-target sequence of effects as sequential execution. The proof needs commutation axioms for non-conflicting effects, which are exactly the assumptions this chapter lists. Formalizing it would turn “Assumed by you” in What is checked, what is assumed into the precise hypotheses of a theorem.
Priorities
| Step | Closes | Effort |
|---|---|---|
| 1. runtime target checks | the duplicate-item assumption, computed paths | small: code generation only |
| 3. effect sanitizer (middleware + differential traces) | everything the static model misses, in tests | medium |
| 2. type-checked effect contracts | lying names | medium, ongoing per ecosystem |
| 4. HTTP semantics | coarse service conflicts | small |
| 7. processes and cwd | over-conservative processes; cwd hazard | small |
| 5. staged writes, 6. cancellation | wasted work, writes waiting on exits | medium to large |
| 8. compiler driver, 9. Lean model | the token-level foundation | large |