feat: abandonAll, explicitly, instead of replacing a session on start

Ending a session breaks every reference and pending answer that lives
only in it, so it takes its own update method rather than happening as
a side effect of start. A second start traps again; the clients take
--abandon-all to end the session a previous run left open.

Generated-by: Claude Code (claude-opus-5-5)
This commit is contained in:
dckc committed 2026-11-18 06:43:15 -06:00
1 parent 9db91b1b9c
commit 6c437ec2f1
9 files changed
+73 -26

No files matched your search

+9 -1
View File
@@ -12,6 +12,7 @@ const idlFactory = ({ IDL }) => {
return IDL.Service({
start: IDL.Func([Frames], [Frames], []),
deliver: IDL.Func([Frames], [Frames], []),
abandonAll: IDL.Func([], [], []),
});
};
@@ -34,8 +35,11 @@ const keySeed = async path => {
* @param {string} host the replica, e.g. https://icp-api.io
* @param {string} canisterId
* @param {string} keyFile
* @param {object} [opts]
* @param {boolean} [opts.abandonAll] first end the principal's open session,
* breaking every reference and pending answer that lives only in it
*/
export const connect = async (host, canisterId, keyFile) => {
export const connect = async (host, canisterId, keyFile, { abandonAll = false } = {}) => {
const { hostname } = new URL(host);
const agent = await HttpAgent.create({
host,
@@ -44,6 +48,10 @@ export const connect = async (host, canisterId, keyFile) => {
shouldFetchRootKey: hostname === '127.0.0.1' || hostname === 'localhost',
});
const actor = Actor.createActor(idlFactory, { agent, canisterId });
if (abandonAll) {
await actor.abandonAll();
console.error(' abandoned the previous session');
}
const carry = async (method, frames) => {
const t0 = performance.now();
const reply = await actor[method](frames);
+8 -5
View File
@@ -2,8 +2,8 @@
/**
* @file Call the Counter canister over OCapN.
*/
const HELP = `usage: counter.mjs HOST CANISTER_ID KEY_FILE init NAME
counter.mjs HOST CANISTER_ID KEY_FILE use NAME [VERB...]
const HELP = `usage: counter.mjs HOST CANISTER_ID KEY_FILE init NAME [--abandon-all]
counter.mjs HOST CANISTER_ID KEY_FILE use NAME [VERB...] [--abandon-all]
Open a CapTP session to the canister as the principal whose Ed25519 seed is
in KEY_FILE (32 bytes, made if missing), then:
@@ -14,18 +14,21 @@ in KEY_FILE (32 bytes, made if missing), then:
(incr, decr, value; default: incr value)
Each run's sends are pipelined: one update call after the handshake, plus
one for init. HOST is the replica, e.g. http://127.0.0.1:4960, or
one for init. A principal has one session at a time; --abandon-all first
ends the one a previous run left open. HOST is the replica, e.g. http://127.0.0.1:4960, or
https://icp-api.io.`;
import { connect } from './connect.js';
const main = async ([host, canisterId, keyFile, command, name, ...verbs]) => {
const main = async argv => {
const abandonAll = argv.includes('--abandon-all');
const [host, canisterId, keyFile, command, name, ...verbs] = argv.filter(arg => arg !== '--abandon-all');
if (!name || !['init', 'use'].includes(command)) {
console.error(HELP);
process.exitCode = 2;
return;
}
const peer = await connect(host, canisterId, keyFile);
const peer = await connect(host, canisterId, keyFile, { abandonAll });
console.log('session open');
const home = peer.fetch('home');
/** Send each verb without waiting, then print each answer. */
+8 -4
View File
@@ -2,7 +2,7 @@
/**
* @file Alice buys a widget from Bob, over ERTP in the canister.
*/
const HELP = `usage: shop.mjs HOST CANISTER_ID KEY_FILE STEP
const HELP = `usage: shop.mjs HOST CANISTER_ID KEY_FILE STEP [--abandon-all]
Each STEP runs as the principal whose key is in KEY_FILE (made if missing):
@@ -14,7 +14,9 @@ Each STEP runs as the principal whose key is in KEY_FILE (made if missing):
purse at home as "closet"
balances show each purse kept at home
Run mint and buy as Alice, open as Bob.`;
Run mint and buy as Alice, open as Bob. A principal has one session at a
time; --abandon-all first ends the one a previous run left open, breaking
everything that lived only in it.`;
import { connect } from './connect.js';
@@ -86,13 +88,15 @@ const steps = {
},
};
const main = async ([host, canisterId, keyFile, step]) => {
const main = async argv => {
const abandonAll = argv.includes('--abandon-all');
const [host, canisterId, keyFile, step] = argv.filter(arg => arg !== '--abandon-all');
if (!Object.hasOwn(steps, step ?? '')) {
console.error(HELP);
process.exitCode = 2;
return;
}
const peer = await connect(host, canisterId, keyFile);
const peer = await connect(host, canisterId, keyFile, { abandonAll });
await steps[step](peer, peer.fetch('home'), peer.fetch('board'));
};