Two writers
The core model. Each key has one layer per actor; the person's shows on top; history is kept per actor.
The core, @coui-ai/core, is plain data and pure functions. It depends on no UI framework and no agent framework.
Layers
Two actors write: human and agent. A CoState is what the store keeps for one key: its initial value and at most one layer per actor.
type Actor = 'human' | 'agent';
type State<T> = {
initial: T;
layers: { human?: Layer<T>; agent?: Layer<T> };
/** Agent values the person rejected since they last wrote this key. */
rejected: T[];
};
/** The person's layer wins, then the agent's, then initial. */
function resolve<T>(state: State<T>): { value: T; by: Actor | 'default' };Like the CSS cascade, both layers are kept and the person's shows. The person's layer does not overwrite the agent's; it only sits in front of it. What matters is whether an actor has a layer, not whether its value is empty: a person who wrote null has written.
The reducer
The store is one pure reducer with three ops: init creates a CoState; set and clear belong to an actor. One invariant: an op only touches layers[op.actor].
| What happens | Op | Effect |
|---|---|---|
| The person writes | set human | Applies at once |
| The agent writes, no human layer | set agent | Applies at once, by: 'agent' |
| The agent writes, there is a human layer | set agent | Stored underneath; waits for approval |
| The person approves | set human, the agent's value | The waiting value shows; the person owns it |
| The person rejects | clear agent | The waiting value is gone, and remembered as rejected |
| The person releases | clear human | Handed back: the agent's value shows again |
So approval, take over and hand back are not three features: they are three readings of the two layers.
Version
Every op increases the store's version by one. The context the agent sees records the version it was made at; an agent write to a key the person changed after that comes back stale, and nothing is written. The agent never passes a version itself.
History
Undo and redo are kept per actor and never interfere: undoing the agent's last three steps changes nothing the person wrote, and Cmd+Z undoes only the person's own.
- The unit of undo is a group: every write the agent makes in one turn is one group, so
undo('agent')reverts the agent's whole turn. Each humansetis its own group, unless it runs insidehistory.batch(). - When the person undoes their own write, the agent's latest value shows, not the one it had before.
- A callback has side effects: its commits cannot be undone unless it was declared with
undo.
UI state and CoState
A drag or a typed value has two kinds of state: the preview while it happens goes in useState, seen only by the component; the value committed when it ends goes in useCoState, seen by both and recorded in history. Component libraries already give the two events: onValueChange / onValueCommit, onChange / onChangeEnd, input / change. One drag is one write and one undo group, and the agent sees only the committed value.