npm-protocol-2PhW (@rootintf/protocol-2phw)
The two-phase warp protocol (2PhW) for IPSME participants: a sender asks a receiver to take a
user over (Warp → AckWarp), then commits (Engage → AckEngage).
2PhW is more than an interface: it has order and time windows. So it comes in two parts, and a
participant uses both:
| part |
what it is |
where it comes from |
| the interface |
the four messages on the wire: validators, constructors, VERSION, types |
generated in your project by iface-gen from schema_TwoPhW.json |
| the protocol |
the state machine: which step is legal now, inside which window, and the expiry timers |
this package |
The generated responder in your project is where the two meet: its dispatch validates a message with
the generated interface, then hands it to this package's typed handler.
ME -> bridge -> Responder_TwoPhW.handler_json_{msg,eff,ack} generated: validate as a type
-> Responder_TwoPhW.handler_json_<message> you: one forwarding line
-> twoPhW.handler_json_<message> this package: legal? in its window? log it
-> your callback you: what the step means for the app
The exchange
sender receiver
| -- JSON_MsgWarp --------------------> | phase 1: "take this user to this hyperport"
| <------------------- JSON_AckWarp --- | ok:true, or ok:false (ends the exchange)
| -- JSON_EffEngage ------------------> | phase 2: "commit" (carries the measured rtt_msec)
| <----------------- JSON_AckEngage --- | concludes the exchange
| message |
base |
body key |
_cause |
JSON_MsgWarp |
JSON_Msg |
warp: { id, user, hyperport, ttl_msec, instantaneous? } |
— |
JSON_AckWarp |
JSON_Ack |
ack: { ok } |
the JSON_MsgWarp |
JSON_EffEngage |
JSON_Eff |
engage: { ok, rtt_msec } |
the JSON_MsgWarp (not its ack) |
JSON_AckEngage |
JSON_Ack |
ack: { ok } |
the JSON_EffEngage |
Every message carries the marker "2PhW": "2". warp.id names the whole exchange: every later
message reaches it through its _cause chain.
Each step is accepted only inside a window computed from the messages themselves (TTL =
warp.ttl_msec, STT = the ack's dt minus the warp's dt, RTT = engage.rtt_msec):
| step |
accepted until |
JSON_MsgWarp |
the warp's dt + TTL |
JSON_AckWarp |
the warp's dt + 2·TTL |
JSON_EffEngage |
the ack's dt + 2·TTL + STT |
JSON_AckEngage |
the warp's dt + 2·TTL + RTT |
ttl_msec must be above 0 and rtt_msec 0 or more (the schema refuses anything else). RTT is also
capped at 2·TTL: an engage that claims more is refused. So an exchange never stays open longer
than 4·TTL after its warp, and that is how far back the package replays the event log.
A step that arrives out of order, twice, late, or after a nack is refused: its handler returns
false and nothing is logged.
Single phase. A warp with instantaneous: true has no phase 2: its JSON_AckWarp concludes the
exchange, and an engage for it is refused (received, or registered). The receiver acts on the warp
when it acknowledges it; the sender is done when the ack arrives.
Procedure
1. Install
npm i @rootintf/protocol-2phw @rootintf/json-msg @rootintf/eventlog
npm i -D @rootintf/iface-gen
2. Add the schema to your ifacegenconfig.json
Register TwoPhW once, pinned by hash, and list it in the target of the participant that warps:
{
"schemas": {
"TwoPhW": { "src": "~/protocols.git/schemas.readonly/schema_TwoPhW.json",
"sha256": "5fdbd096ad99962b7e3d77cde8bd355fe180c859f35a61cfd07ee36b96c6700a" }
},
"targets": [
{ "root": "src/renderer",
"IPSME": { "main": "App.js", "object": "App", "shell": "electron-renderer", "out": "IPSME" },
"schema-cache": "schema-cache", "out-npm": "generated",
"scope": "@rootintf", "prefix": "iface-", "delete": true,
"schemas": [ "Discovery", "TwoPhW" ] }
],
"version": 3
}
The pin must be the same hash this package was built against (see its own ifacegenconfig.json).
That is what keeps your validators and this package's constructors on the same definition.
3. Generate
npx iface-gen --config ifacegenconfig.json
This gives you, with nothing to write by hand:
generated/iface-TwoPhW/: JSON_MsgWarp, JSON_AckWarp, JSON_EffEngage, JSON_AckEngage
(.validate, and .merge / .create_with_cause_merge where the base has one) and VERSION;
IPSME/Responder_TwoPhW.js: a stub with one handler_json_<message> per message and the base
dispatch (handler_json_ack, handler_json_eff, handler_json_msg);
- the bridge wired to it (import, logging, one call per base level, the
reprocess list), and
TwoPhW in the introspection table.
If the bridge comes back with merge markers, resolve them as described in the iface-gen README.
4. Fill in the responder
Everything below goes in the generated IPSME/Responder_TwoPhW.js. Leave the generated dispatch
(handler_json_ack / _eff / _msg) as it is.
Imports and logging
import { v4 as uuidv4 } from 'uuid';
import { LOGR, l_array } from '@knev/bitlogr';
import { JSON_MsgWarp, JSON_AckWarp, JSON_EffEngage, JSON_AckEngage } from '../generated/iface-TwoPhW'; // generated
// this package's JSON_* are the generated ones extended with the protocol's own constructors,
// so import the ones you need under an alias
import { TwoPhW, JSON_EffEngage as TwoPhW_EffEngage, logr as logr_twoPhW_ } from '@rootintf/protocol-2phw';
const logr_= LOGR_.create({ name: "Responder_TwoPhW", labels : obj_labels_ }); // generated
const l_= logr_.l; // generated
// export this responder's logr AND the package's as one, so the bridge wires both
const logr_wired_= LOGR_.wire([ logr_, logr_twoPhW_ ]);
export { logr_wired_ as logr };
Constructor: one TwoPhW over this participant's event log, configured per role
constructor(interface_IPSME, interface_App) {
this.interface_IPSME = interface_IPSME || {};
this.interface_App = interface_App || {};
this._callback_ackWarp = this._callback_ackWarp.bind(this);
this._callback_ackEngage = this._callback_ackEngage.bind(this);
this._callback_msg_sent_expired = this._callback_msg_sent_expired.bind(this);
this.twoPhW = new TwoPhW(this.interface_IPSME.eventLog);
this.twoPhW.config.sender(this._callback_ackWarp, this._callback_ackEngage, this._callback_msg_sent_expired);
}
Configure only the role(s) the participant plays: config.sender(...), config.receiver(...).
Sender
The operation the app calls, and the two callbacks the package calls when it has accepted a step:
// phase 1
warp(json_user, json_hyperport) {
const json_msgWarp = JSON_MsgWarp.merge({ id: uuidv4(), user: json_user, hyperport: json_hyperport, ttl_msec: k_TTL_MSEC_ });
json_msgWarp.referer = this.interface_IPSME.karr_referer;
const str_msgWarp = JSON.stringify(json_msgWarp);
if (! this.twoPhW.register_msgWarp(str_msgWarp, json_msgWarp)) // legality + log + expiry timer
return false;
this.interface_IPSME.MsgEnv.publish(str_msgWarp);
return true;
}
// phase 1 answered: a nack ends the exchange; an ack is where phase 2 starts
_callback_ackWarp(msg, json_ackWarp) {
if (json_ackWarp.ack.ok !== true) {
this.interface_App.cancel_modal();
return true;
}
const json_effEngage = TwoPhW_EffEngage.create_with_Warp(json_ackWarp._cause); // measures rtt_msec
json_effEngage.referer = this.interface_IPSME.karr_referer;
const str_effEngage = JSON.stringify(json_effEngage);
if (! this.twoPhW.register_effEngage(str_effEngage, json_effEngage)) {
this.interface_App.cancel_modal();
return true;
}
this.interface_IPSME.MsgEnv.publish(str_effEngage);
return true;
}
// phase 2 answered
_callback_ackEngage(msg, json_ackEngage) {
if (json_ackEngage.ack.ok !== true) {
this.interface_App.cancel_modal();
return true;
}
this.interface_App.close_main_window();
return true;
}
// a warp or an engage we sent was never answered inside its window
_callback_msg_sent_expired(msg, json_msg) {
this.interface_App.cancel_modal();
}
This sender only sends two-phase warps. One that also sends instantaneous warps must treat an
ok ack of such a warp as the end of the exchange in _callback_ackWarp
(json_ackWarp._cause.warp.instantaneous), instead of building an engage: register_effEngage
refuses it.
The generated stubs for the messages a sender receives are one forwarding line each:
handler_json_ackWarp(msg, json_ackWarp) { return this.twoPhW.handler_json_ackWarp(msg, json_ackWarp); }
handler_json_ackEngage(msg, json_ackEngage) { return this.twoPhW.handler_json_ackEngage(msg, json_ackEngage); }
Receiver
// in the constructor
this.twoPhW.config.receiver(this._callback_msgWarp, this._callback_effEngage, this._callback_msg_sent_expired);
// a legal warp arrived: decide, answer
_callback_msgWarp(msg, json_msgWarp) {
const b_ok = this.interface_App.can_admit(json_msgWarp.warp.user, json_msgWarp.warp.hyperport);
const json_ackWarp = TwoPhW_AckWarp.create_with_Warp_Ok(json_msgWarp, b_ok);
json_ackWarp.referer = this.interface_IPSME.karr_referer;
const str_ackWarp = JSON.stringify(json_ackWarp);
if (this.twoPhW.register_ackWarp(str_ackWarp, json_ackWarp))
this.interface_IPSME.MsgEnv.publish(str_ackWarp);
return true;
}
// a legal engage arrived for a warp we acknowledged: commit, answer
_callback_effEngage(msg, json_effEngage) {
const b_ok = this.interface_App.admit(json_effEngage._cause.warp.user);
const json_ackEngage = TwoPhW_AckEngage.create_with_Engage_Ok(json_effEngage, b_ok);
json_ackEngage.referer = this.interface_IPSME.karr_referer;
const str_ackEngage = JSON.stringify(json_ackEngage);
if (this.twoPhW.register_ackEngage(str_ackEngage, json_ackEngage))
this.interface_IPSME.MsgEnv.publish(str_ackEngage);
return true;
}
// the generated stubs for the messages a receiver receives
handler_json_msgWarp(msg, json_msgWarp) { return this.twoPhW.handler_json_msgWarp(msg, json_msgWarp); }
handler_json_effEngage(msg, json_effEngage) { return this.twoPhW.handler_json_effEngage(msg, json_effEngage); }
(TwoPhW_AckWarp / TwoPhW_AckEngage are this package's JSON_AckWarp / JSON_AckEngage, imported
under an alias like TwoPhW_EffEngage above. can_admit / admit stand for your app interface.)
For an instantaneous warp there is no engage: a receiver that accepts one commits in
_callback_msgWarp, when it acknowledges with ok: true.
reprocess()
Leave it empty. The package's expiries are timers on the event log, not parked events.
5. Rules
- Configure only the role(s) you play. A step's callback is what makes the step yours: the
package refuses a step whose callback was never set, before logging anything. A sender-only
participant (only
config.sender(...) called) does not handle or log a warp or an engage; a
receiver-only one does not handle an ack. A handler of an unplayed role may therefore stay the
generated stub or forward to the package; both return false.
- An exchange is a dialogue. The package accepts a step you receive only after a step you sent,
and lets you register a send only after a step you received. Every participant hears every
exchange on the ME; this is what keeps a warp you merely heard from being "answered" by the ack
its real receiver sent, also when you play both roles.
- Register, then publish, and only if registration succeeded.
register_* is the legality test
for your own send, the log entry the answers are matched against, and the expiry timer. A message
published without it can never be answered.
- Stamp
referer before you stringify and register. The package identifies an exchange by the
warp's id, dt and referer.
- The typed handlers do not validate. They trust that the dispatch validated the message as that
type, which the generated dispatch does.
- The callback's return value is the handler's return value to the chain:
true = handled. The
step is already logged when the callback runs, so returning false does not undo it.
- Expiry is an outcome, not an error.
_callback_msg_sent_expired is how the app learns that a
send was never answered; there is no other signal. Each role has its own: the one passed to
config.sender fires for a warp or an engage you sent, the one passed to config.receiver for
an ack-warp you sent that an engage should have followed. A nack, and the ack of an
instantaneous warp, await nothing and never expire; neither does an ack-engage. A participant
playing both roles may pass two different functions.
- Never build a 2PhW message by hand. Use the generated constructors, and this package's for
what the schema cannot express (below).
What this package exports