tryflop 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +66 -2
- package/bin/flop-mcp.mjs +22 -0
- package/bin/tryflop.mjs +36 -0
- package/e2e/sandbox-smoke.mjs +136 -0
- package/npm-shrinkwrap.json +2648 -0
- package/package.json +52 -5
- package/src/agent-settings.js +76 -0
- package/src/claim.js +516 -0
- package/src/cli.js +428 -0
- package/src/daily.js +363 -0
- package/src/files.js +104 -0
- package/src/flop-core.js +189 -0
- package/src/flop-node.js +346 -0
- package/src/format.js +180 -0
- package/src/gate.js +19 -0
- package/src/guard.js +231 -0
- package/src/home.js +96 -0
- package/src/install.js +110 -0
- package/src/keyfile.js +70 -0
- package/src/layout.js +55 -0
- package/src/miners.js +65 -0
- package/src/moments.js +50 -0
- package/src/outcome.js +128 -0
- package/src/outer.js +969 -0
- package/src/payer.js +834 -0
- package/src/questions.js +47 -0
- package/src/requests.js +178 -0
- package/src/roots.js +48 -0
- package/src/sandbox.js +223 -0
- package/src/scheduler.js +221 -0
- package/src/scoring.js +67 -0
- package/src/secrets.js +41 -0
- package/src/server.js +13 -0
- package/src/shared/config.js +366 -0
- package/src/shared/direct-core.js +226 -0
- package/src/shared/grants-core.js +69 -0
- package/src/shared/units.js +47 -0
- package/src/statusfile.js +70 -0
- package/src/tools.js +107 -0
- package/src/tryflop.js +622 -0
- package/src/wallet.js +123 -0
- package/bin/tryflop.js +0 -5
package/src/moments.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// The only times Try Flop speaks to the human on its own (SPEC.md "tryflop daily", NOTIFY): the human moments, each
|
|
2
|
+
// with its exact words, each notified once (or once a week for the backup reminder and the weekly line), and nothing
|
|
3
|
+
// daily. The daily run decides which moments are new (moments.json in the wallet folder); the outer process shows them.
|
|
4
|
+
// Node built-ins only.
|
|
5
|
+
|
|
6
|
+
const DAY_MS = 86_400_000;
|
|
7
|
+
const WEEK_MS = 7 * DAY_MS;
|
|
8
|
+
|
|
9
|
+
/** The exact words of each moment (tests pin them). */
|
|
10
|
+
export const MOMENT_TEXT = Object.freeze({
|
|
11
|
+
backup: 'Try Flop: save your wallet\'s 12 recovery words. Open a terminal window yourself and run: ~/.tryflop/bin/tryflop backup (your AI agent must not run it for you).',
|
|
12
|
+
faucet_connection: 'Try Flop: Flop\'s free test FLOP limit for your internet connection is used up for today. Nothing to do: it tries again tomorrow.',
|
|
13
|
+
verified_grant: 'Try Flop: Flop opened a bigger grant for verified humans. Nothing was done. Ask your AI agent about it before doing anything.',
|
|
14
|
+
scoring_started: 'Try Flop: Flop says testnet scoring has started. Your daily run now spends each day\'s free FLOP on real questions. Nothing to do.',
|
|
15
|
+
needs_you: 'Try Flop has not managed a daily run for 3 days. Ask your AI agent to run: ~/.tryflop/bin/tryflop status',
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
/** The same words on Windows (PowerShell expands ~, not %USERPROFILE%). @param {string} text @param {string} platform */
|
|
19
|
+
export const forPlatform = (text, platform) => (platform === 'win32' ? text.replaceAll('~/.tryflop/bin/tryflop', '~/.tryflop/bin/tryflop.cmd') : text);
|
|
20
|
+
|
|
21
|
+
/** @typedef {'backup'|'faucet_connection'|'verified_grant'|'scoring_started'|'needs_you'|'weekly'} MomentKind */
|
|
22
|
+
/** @typedef {{ kind: MomentKind, text: string }} Moment */
|
|
23
|
+
/** @typedef {Partial<Record<MomentKind, number>>} MomentsSeen when each kind was last notified (ms) */
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The weekly line: one sentence about the last 7 days.
|
|
27
|
+
* @param {{ answers: number, spentFlop: string, scoringOn: boolean }} week
|
|
28
|
+
*/
|
|
29
|
+
export const weeklyText = ({ answers, spentFlop, scoringOn }) => `Try Flop this week: ${answers} paid answer${answers === 1 ? '' : 's'}, ${spentFlop} test FLOP paid into channels, ${scoringOn ? 'scoring on' : 'scoring not started yet'}. Details: ~/TryFlop/status.md`;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Which moments to notify now, given what happened and what was already notified.
|
|
33
|
+
* @param {{ now: number, seen: MomentsSeen, backupShown: boolean, walletAgeMs: number, faucetConnectionRefused: boolean,
|
|
34
|
+
* verifiedGrantOpen: boolean, scoringOn: boolean, failedDays: number,
|
|
35
|
+
* week: { answers: number, spentFlop: string, scoringOn: boolean } | null }} facts
|
|
36
|
+
* walletAgeMs: how long the wallet has existed (the backup reminder waits a day, then repeats weekly until shown)
|
|
37
|
+
* @returns {Moment[]}
|
|
38
|
+
*/
|
|
39
|
+
export function momentsToNotify({ now, seen, backupShown, walletAgeMs, faucetConnectionRefused, verifiedGrantOpen, scoringOn, failedDays, week }) {
|
|
40
|
+
/** @type {Moment[]} */
|
|
41
|
+
const out = [];
|
|
42
|
+
const since = (/** @type {MomentKind} */ kind) => (seen[kind] === undefined ? Infinity : now - /** @type {number} */ (seen[kind]));
|
|
43
|
+
if (!backupShown && walletAgeMs >= DAY_MS && since('backup') >= WEEK_MS) out.push({ kind: 'backup', text: MOMENT_TEXT.backup });
|
|
44
|
+
if (faucetConnectionRefused && since('faucet_connection') >= DAY_MS - 3_600_000) out.push({ kind: 'faucet_connection', text: MOMENT_TEXT.faucet_connection });
|
|
45
|
+
if (verifiedGrantOpen && seen.verified_grant === undefined) out.push({ kind: 'verified_grant', text: MOMENT_TEXT.verified_grant });
|
|
46
|
+
if (scoringOn && seen.scoring_started === undefined) out.push({ kind: 'scoring_started', text: MOMENT_TEXT.scoring_started });
|
|
47
|
+
if (failedDays >= 3 && since('needs_you') >= 3 * DAY_MS) out.push({ kind: 'needs_you', text: MOMENT_TEXT.needs_you });
|
|
48
|
+
if (week && since('weekly') >= WEEK_MS) out.push({ kind: 'weekly', text: weeklyText(week) });
|
|
49
|
+
return out;
|
|
50
|
+
}
|
package/src/outcome.js
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
// How a failed turn ends (SPEC.md, "One ask_flop call", PAYING_TURN). The money rule: anything that does
|
|
2
|
+
// not prove "nothing was sent" is UNKNOWN and is never re-run automatically. Facts come from the SDK's own
|
|
3
|
+
// wording via the shared classifier (src/shared/direct-core.js), from the connector's own limit errors, and
|
|
4
|
+
// from the guarded journal (did this request's turn start? is one of its spends unresolved?).
|
|
5
|
+
// Messages are fixed texts: an error's own message can carry a miner's words and is never repeated.
|
|
6
|
+
import { DIRECT } from './shared/config.js';
|
|
7
|
+
import { formatFlop } from './shared/units.js';
|
|
8
|
+
import { classifyDirectError } from './shared/direct-core.js';
|
|
9
|
+
|
|
10
|
+
/** Classes that prove nothing was sent for this answer (refused before or by the chain). A purchase the
|
|
11
|
+
* answer needed may still have been sent and is reported on its own: only the turn is accounted for here. */
|
|
12
|
+
const NOTHING_SENT = new Set(['frozen', 'budget', 'limits', 'locked', 'too_large', 'allowance', 'price_ceiling', 'miner_rejected', 'turn_refused', 'fees', 'session_ended', 'stale_head', 'refused', 'network']);
|
|
13
|
+
/** Refusals that still leave this answer unpaid after the turn reached the miner (refused before serving, or its receipt was refused). */
|
|
14
|
+
const NOT_CHARGED_AFTER_START = new Set(['miner_rejected', 'turn_refused', 'too_large', 'allowance', 'price_ceiling']);
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @typedef {{ kind: 'answered'|'retry'|'failed'|'unknown', reason: string }} TurnOutcome
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The shared classifier's class, with one case of "allowance" told apart for its message: the SDK's own
|
|
22
|
+
* "one worst-case turn (<n> atoms) exceeds maxChannelPlanck" (long-session.js turnTariff, 0.12.1; 0.9.0 had
|
|
23
|
+
* no number), which means the miner's price for one worst-case turn is above the per-channel ceiling. The
|
|
24
|
+
* worst case is the model's worstTurnGn (agent-app config.js MODELS), a constant, so "a shorter question" cannot help. Only a class the
|
|
25
|
+
* classifier already gave as "allowance" is refined (its own anchored rule matched the SDK's wording).
|
|
26
|
+
* @param {unknown} error
|
|
27
|
+
*/
|
|
28
|
+
function classify(error) {
|
|
29
|
+
const c = classifyDirectError(error);
|
|
30
|
+
const message = String(/** @type {any} */ (error)?.message ?? '');
|
|
31
|
+
return c === 'allowance' && /^one worst-case turn (\(\d+ atoms\) )?exceeds maxChannelPlanck/.test(message) ? 'price_ceiling' : c;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @param {unknown} error what session.turn() threw
|
|
36
|
+
* @param {{ started: boolean, unresolvedSpend: boolean, journalDone: boolean, staleRetriesLeft: number }} facts
|
|
37
|
+
* started: the guarded journal saw beginRequest for this request (the turn may have reached the miner);
|
|
38
|
+
* (rules: SPEC.md PAYING_TURN; before the turn started with nothing unresolved, any error is "not charged")
|
|
39
|
+
* unresolvedSpend: a spend this request reserved is still pending or ambiguous in the journal;
|
|
40
|
+
* journalDone: the SDK journal holds this request's verified answer
|
|
41
|
+
* @returns {TurnOutcome}
|
|
42
|
+
*/
|
|
43
|
+
export function turnOutcome(error, { started, unresolvedSpend, journalDone, staleRetriesLeft }) {
|
|
44
|
+
if (journalDone) return { kind: 'answered', reason: 'replayed' };
|
|
45
|
+
const e = /** @type {any} */ (error);
|
|
46
|
+
if (!started && !unresolvedSpend) {
|
|
47
|
+
// The guard proves it: no spend of this request is in doubt and the turn never reached the miner, so
|
|
48
|
+
// nothing was sent for this answer, whatever the error says (a chain read timeout included).
|
|
49
|
+
if (e?.code === 'CONNECTOR_LIMIT' && e?.name === 'LimitError') return { kind: 'failed', reason: String(e.which) };
|
|
50
|
+
const c = classify(error);
|
|
51
|
+
if (c === 'stale_head' && staleRetriesLeft > 0) return { kind: 'retry', reason: 'stale_head' };
|
|
52
|
+
return { kind: 'failed', reason: NOTHING_SENT.has(c) ? c : 'not_sent' };
|
|
53
|
+
}
|
|
54
|
+
if (!started) return { kind: 'unknown', reason: 'payment_unresolved' };
|
|
55
|
+
const c = classify(error);
|
|
56
|
+
if (NOT_CHARGED_AFTER_START.has(c)) return { kind: 'failed', reason: c };
|
|
57
|
+
return { kind: 'unknown', reason: 'turn_unknown' };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Why a failed answer was not charged, in plain English. */
|
|
61
|
+
const WHY = /** @type {Record<string, string>} */ ({
|
|
62
|
+
per_ask: 'Answering needed a purchase above this answer\'s FLOP limit (max_flop, or maxFlopPerAsk in config.json).',
|
|
63
|
+
per_day: 'Today\'s FLOP limit on this computer (maxFlopPerDay in config.json) is used up.',
|
|
64
|
+
no_request: 'A purchase outside a question was refused.',
|
|
65
|
+
ledger_damaged: 'The connector\'s spend ledger is damaged, so no purchase is made until it is fixed (see flop_status).',
|
|
66
|
+
chain_cap: `Your wallet's spending limit on Flop has less left than this purchase needs (a new channel locks an escrow of up to ${formatFlop(DIRECT.maxChannelPlanck)} FLOP, never more than this answer's FLOP limit). Grant it a new limit from your wallet.`,
|
|
67
|
+
chain_per_tx: 'This purchase is above the per-payment limit your wallet granted. Raise that limit in your wallet.',
|
|
68
|
+
chain_per_day: 'Your wallet\'s daily spending limit on Flop is used up for today. Try again tomorrow, or raise that limit in your wallet.',
|
|
69
|
+
no_chain_check: 'The spending limit on Flop\'s chain could not be checked before the purchase, so nothing was bought.',
|
|
70
|
+
per_batch: 'One batch buys at most one channel, and this batch already had one that could not serve this answer. Nothing was charged for it; ask again and a new batch will buy a channel if it needs to.',
|
|
71
|
+
budget: 'The spending limit is used up: the one your wallet granted on chain, or this connector\'s lifetime limit (maxFlopTotal in config.json).',
|
|
72
|
+
limits: 'Flop\'s chain refused the payment: it is above your limit\'s per-payment cap, or your limit does not allow compute payments.',
|
|
73
|
+
locked: 'Another Claude session on this computer is paying with this wallet right now.',
|
|
74
|
+
too_large: 'The question (with its answer length) is too large for the miner to take.',
|
|
75
|
+
allowance: 'The miner cannot settle this much work in one channel; try a shorter question or a shorter answer.',
|
|
76
|
+
price_ceiling: `The miner's price for one question is above the most one channel may hold (${formatFlop(DIRECT.maxChannelPlanck)} FLOP), so nothing more was bought for it. The price is set by the miner's model on Flop's chain; try again later.`,
|
|
77
|
+
miner_rejected: 'The miner turned this question down before answering (busy, or over its own limits). Try again in a few minutes.',
|
|
78
|
+
turn_refused: 'The miner turned this question down before answering (busy, or over its own limits). Try again in a few minutes.',
|
|
79
|
+
fees: 'The session key has no FLOP left for network fees. Send it 0.5 FLOP from your wallet.',
|
|
80
|
+
session_ended: 'The spending limit your wallet gave this key has ended (expired or revoked). Grant a new one from your wallet.',
|
|
81
|
+
stale_head: 'Flop\'s chain is running behind (its newest confirmed block is too old), so nothing was signed. Try again in a few minutes.',
|
|
82
|
+
refused: 'Flop\'s chain refused the payment (a counter or amount did not match). Try again.',
|
|
83
|
+
network: 'Flop\'s network could not be reached.',
|
|
84
|
+
frozen: 'An earlier payment is still being confirmed on Flop\'s chain; new purchases wait until it is (usually a minute or two).',
|
|
85
|
+
// That failure can follow a channel purchase (the SDK opens, then cannot read the channel back; run of 24 Nov
|
|
86
|
+
// 09:28 UTC), so the retry's cost is named: Claude asked 11 times and bought 4 channels for one answer.
|
|
87
|
+
// tryflop's own wallet (SPEC.md "Asking as the wallet itself").
|
|
88
|
+
wallet_balance: 'The wallet holds too little FLOP for this purchase beyond what it keeps for network fees. The daily run claims Flop\'s free test FLOP for it.',
|
|
89
|
+
per_channel: `This purchase is above what one channel may hold (${formatFlop(DIRECT.maxChannelPlanck)} FLOP).`,
|
|
90
|
+
per_run: 'This run, or today, has already spent what it may (the mode\'s cap: one channel a day before Flop\'s scoring starts). Nothing more was bought.',
|
|
91
|
+
no_flop: 'The wallet holds too little FLOP to buy an answer. The daily run claims Flop\'s free test FLOP for it.',
|
|
92
|
+
not_sent: `Something failed before the question reached the miner (for example a slow chain read), so this answer was not paid for. Waiting a minute before asking again matters: a retry can buy another channel (an escrow of up to ${formatFlop(DIRECT.maxChannelPlanck)} FLOP, never more than this answer's FLOP limit).`,
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* The user-facing explanation of a failed or unknown answer.
|
|
97
|
+
* @param {TurnOutcome} outcome
|
|
98
|
+
* @param {{ requestId: string, clockSkewMs?: number, boughtUnservedPlanck?: bigint, boughtOrphaned?: boolean }} facts
|
|
99
|
+
* clockSkewMs: this computer's clock minus the chain's latest finalized timestamp;
|
|
100
|
+
* boughtUnservedPlanck: escrow of a channel this request bought that served no answer (both of its
|
|
101
|
+
* possible endings are stated: it serves a later question, or its unused escrow comes back);
|
|
102
|
+
* boughtOrphaned: the SDK journal already holds that channel as orphaned, so only the refund is said
|
|
103
|
+
*/
|
|
104
|
+
export function userMessage(outcome, { requestId, clockSkewMs = 0, boughtUnservedPlanck = 0n, boughtOrphaned = false }) {
|
|
105
|
+
const lines = [];
|
|
106
|
+
if (outcome.kind === 'failed') {
|
|
107
|
+
lines.push(`Not charged for this answer. ${WHY[outcome.reason] ?? 'Flop refused it before anything was sent.'}`);
|
|
108
|
+
// The SDK refuses to sign when the finalized head looks older than 16 s by this computer's clock, so a
|
|
109
|
+
// clock that is off by more than that refuses every purchase. Real chain lag is usually under 10 s.
|
|
110
|
+
if (outcome.reason === 'stale_head' && Math.abs(clockSkewMs) > 30_000) {
|
|
111
|
+
const skew = Math.abs(clockSkewMs) >= 90_000 ? `${Math.round(Math.abs(clockSkewMs) / 60_000)} minutes` : `${Math.round(Math.abs(clockSkewMs) / 1000)} seconds`;
|
|
112
|
+
lines.push(`This computer's clock reads about ${skew} off from Flop's newest confirmed block; if this keeps happening, check the date and time settings.`);
|
|
113
|
+
}
|
|
114
|
+
} else {
|
|
115
|
+
lines.push(`Flop could not confirm an answer, and the question was not re-sent (sending it again could pay twice). Request id: ${requestId}. Call flop_result with that id later: if the answer arrived, it is there.`);
|
|
116
|
+
if (outcome.reason === 'payment_unresolved') lines.push('A payment for it is still being confirmed on Flop\'s chain; new purchases wait until it is.');
|
|
117
|
+
}
|
|
118
|
+
if (boughtUnservedPlanck > 0n) {
|
|
119
|
+
// The window is the miner's offer or a bare open's: between DIRECT.windowSeconds and maxLockSeconds.
|
|
120
|
+
const back = `its unused escrow comes back to your wallet once the channel's window ends (${DIRECT.windowSeconds / 60} to ${DIRECT.maxLockSeconds / 60} minutes after it was bought)`;
|
|
121
|
+
// Recorded as unusable already: only the refund is left. Otherwise both endings are possible and the SDK records
|
|
122
|
+
// the truth only later, so neither is promised (SPEC.md).
|
|
123
|
+
lines.push(boughtOrphaned
|
|
124
|
+
? `A channel was bought for it (${formatFlop(boughtUnservedPlanck)} FLOP escrow) and cannot serve: ${back}.`
|
|
125
|
+
: `A channel was bought for it (${formatFlop(boughtUnservedPlanck)} FLOP escrow): if that channel can still serve, a later question is answered on it at no new cost; if it cannot, ${back}.`);
|
|
126
|
+
}
|
|
127
|
+
return lines.join(' ');
|
|
128
|
+
}
|