@swarm-tips/client 0.4.6 → 0.4.8
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/SBOM.spdx.json +5 -5
- package/dist/agent.d.ts +40 -3
- package/dist/agent.js +116 -15
- package/dist/cli/keyfile.d.ts +3 -2
- package/dist/cli/keyfile.js +21 -4
- package/dist/cli/lean-init.d.ts +39 -0
- package/dist/cli/lean-init.js +231 -0
- package/dist/cli/swarm.js +197 -15
- package/dist/coordination-game/api.d.ts +61 -26
- package/dist/coordination-game/api.js +47 -18
- package/dist/coordination-game/evm/cross-chain.d.ts +25 -4
- package/dist/coordination-game/evm/cross-chain.js +28 -2
- package/dist/coordination-game/evm/index.d.ts +1 -1
- package/dist/coordination-game/evm/session.d.ts +81 -26
- package/dist/coordination-game/evm/session.js +273 -75
- package/dist/coordination-game/index.d.ts +1 -1
- package/dist/errors.d.ts +4 -0
- package/dist/errors.js +6 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +3 -3
- package/dist/shillbot/api.d.ts +155 -7
- package/dist/shillbot/api.js +32 -6
- package/dist/shillbot/index.d.ts +1 -1
- package/dist/shillbot/index.js +1 -1
- package/dist/shillbot/verify-tx.d.ts +8 -3
- package/dist/shillbot/verify-tx.js +47 -12
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +2 -1
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, statSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { join, resolve } from "node:path";
|
|
3
|
+
import { CliUsageError } from "./keyfile.js";
|
|
4
|
+
export const DEFAULT_LEAN_TOOLCHAINS = {
|
|
5
|
+
1: "leanprover/lean4:v4.31.0",
|
|
6
|
+
2: "leanprover/lean4:v4.30.0",
|
|
7
|
+
3: "leanprover/lean4:v4.30.0",
|
|
8
|
+
4: "leanprover/lean4:v4.33.0-rc2",
|
|
9
|
+
};
|
|
10
|
+
export const DEFAULT_MATHLIB_REV_V2_V3 = "c5ea00351c28e24afc9f0f84379aa41082b1188f";
|
|
11
|
+
export const DEFAULT_MATHLIB_REV_V4 = "51e6992efd06126df61a496bebf8f49482a4e129";
|
|
12
|
+
export const DEFAULT_ZETA23_REV_V4 = "3635e74826a4c1fcece7d1cd2b6fa75e43a00510";
|
|
13
|
+
function parsePositiveIntDirective(statementLean, key) {
|
|
14
|
+
const re = new RegExp(`^\\s*--\\s*swarm:${key}\\s*=\\s*(\\d+)\\s*$`, "m");
|
|
15
|
+
const match = re.exec(statementLean);
|
|
16
|
+
if (!match)
|
|
17
|
+
return undefined;
|
|
18
|
+
const parsed = Number(match[1]);
|
|
19
|
+
return Number.isSafeInteger(parsed) && parsed > 0 ? parsed : undefined;
|
|
20
|
+
}
|
|
21
|
+
function hasBidirectionalDirective(statementLean) {
|
|
22
|
+
return /^\s*--\s*swarm:polarity\s*=\s*bidirectional\s*$/m.test(statementLean);
|
|
23
|
+
}
|
|
24
|
+
function asLeanEnv(value) {
|
|
25
|
+
return value && typeof value === "object"
|
|
26
|
+
? value
|
|
27
|
+
: undefined;
|
|
28
|
+
}
|
|
29
|
+
function nonEmptyString(value) {
|
|
30
|
+
return typeof value === "string" && value.trim().length > 0 ? value.trim() : undefined;
|
|
31
|
+
}
|
|
32
|
+
function positiveInt(value) {
|
|
33
|
+
return typeof value === "number" && Number.isSafeInteger(value) && value > 0 ? value : undefined;
|
|
34
|
+
}
|
|
35
|
+
export function extractLeanWorkspaceSpec(task, taskId) {
|
|
36
|
+
if (task.platform !== 10) {
|
|
37
|
+
throw new CliUsageError(`Task "${taskId}" is not a LeanProof task (platform=${String(task.platform)}, expected 10).`);
|
|
38
|
+
}
|
|
39
|
+
const statementLean = typeof task.statement_lean === "string" ? task.statement_lean : "";
|
|
40
|
+
if (!statementLean.trim()) {
|
|
41
|
+
throw new CliUsageError(`Task "${taskId}" has no statement_lean; cannot scaffold a Lean 4 workspace.`);
|
|
42
|
+
}
|
|
43
|
+
const env = asLeanEnv(task.lean_environment);
|
|
44
|
+
const rawPolicy = positiveInt(env?.lean_policy) ?? positiveInt(task.lean_policy) ?? 1;
|
|
45
|
+
const leanToolchain = nonEmptyString(env?.lean_toolchain) ??
|
|
46
|
+
DEFAULT_LEAN_TOOLCHAINS[rawPolicy] ??
|
|
47
|
+
DEFAULT_LEAN_TOOLCHAINS[1];
|
|
48
|
+
const defaultMode = rawPolicy === 4 ? "composable" : rawPolicy === 2 || rawPolicy === 3 ? "mathlib" : "self-contained";
|
|
49
|
+
const mode = nonEmptyString(env?.mode) ?? defaultMode;
|
|
50
|
+
const defaultImports = rawPolicy === 4
|
|
51
|
+
? ["Mathlib", "Zeta23", "SwarmProofs"]
|
|
52
|
+
: rawPolicy === 2 || rawPolicy === 3
|
|
53
|
+
? ["Mathlib"]
|
|
54
|
+
: [];
|
|
55
|
+
const importAllowlist = Array.isArray(env?.import_allowlist)
|
|
56
|
+
? env.import_allowlist.filter((item) => typeof item === "string" && item.trim().length > 0)
|
|
57
|
+
: defaultImports;
|
|
58
|
+
const mathlibRev = nonEmptyString(env?.mathlib_rev) ??
|
|
59
|
+
(rawPolicy === 4
|
|
60
|
+
? DEFAULT_MATHLIB_REV_V4
|
|
61
|
+
: rawPolicy === 2 || rawPolicy === 3
|
|
62
|
+
? DEFAULT_MATHLIB_REV_V2_V3
|
|
63
|
+
: undefined);
|
|
64
|
+
const zeta23Rev = nonEmptyString(env?.zeta23_rev) ?? (rawPolicy === 4 ? DEFAULT_ZETA23_REV_V4 : undefined);
|
|
65
|
+
const swarmProofsRev = nonEmptyString(env?.swarm_proofs_rev);
|
|
66
|
+
const allowsDisproof = typeof env?.allows_disproof === "boolean"
|
|
67
|
+
? env.allows_disproof
|
|
68
|
+
: hasBidirectionalDirective(statementLean);
|
|
69
|
+
const maxProofLines = positiveInt(env?.max_proof_lines) ?? parsePositiveIntDirective(statementLean, "max_proof_lines");
|
|
70
|
+
const maxProofBytes = positiveInt(env?.max_proof_bytes) ?? parsePositiveIntDirective(statementLean, "max_proof_bytes");
|
|
71
|
+
const explicitMaxLineWidth = positiveInt(env?.max_line_width) ?? parsePositiveIntDirective(statementLean, "max_line_width");
|
|
72
|
+
const maxLineWidth = explicitMaxLineWidth ?? (maxProofLines !== undefined ? 120 : undefined);
|
|
73
|
+
const limits = {
|
|
74
|
+
...(maxProofLines !== undefined ? { max_proof_lines: maxProofLines } : {}),
|
|
75
|
+
...(maxProofBytes !== undefined ? { max_proof_bytes: maxProofBytes } : {}),
|
|
76
|
+
...(maxLineWidth !== undefined ? { max_line_width: maxLineWidth } : {}),
|
|
77
|
+
};
|
|
78
|
+
return {
|
|
79
|
+
task_id: taskId,
|
|
80
|
+
statement_lean: statementLean,
|
|
81
|
+
lean_policy: rawPolicy,
|
|
82
|
+
lean_toolchain: leanToolchain,
|
|
83
|
+
mode,
|
|
84
|
+
...(mathlibRev ? { mathlib_rev: mathlibRev } : {}),
|
|
85
|
+
...(zeta23Rev ? { zeta23_rev: zeta23Rev } : {}),
|
|
86
|
+
...(swarmProofsRev ? { swarm_proofs_rev: swarmProofsRev } : {}),
|
|
87
|
+
import_allowlist: importAllowlist,
|
|
88
|
+
allows_disproof: allowsDisproof,
|
|
89
|
+
limits,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
function buildLakefileToml(spec) {
|
|
93
|
+
const lines = [
|
|
94
|
+
'name = "SwarmTask"',
|
|
95
|
+
'version = "0.1.0"',
|
|
96
|
+
'defaultTargets = ["TargetStatement", "Solution"]',
|
|
97
|
+
"",
|
|
98
|
+
"[leanOptions]",
|
|
99
|
+
"pp.unicode.fun = true",
|
|
100
|
+
"relaxedAutoImplicit = false",
|
|
101
|
+
"",
|
|
102
|
+
];
|
|
103
|
+
if (spec.lean_policy === 4) {
|
|
104
|
+
const mathlibRev = spec.mathlib_rev ?? DEFAULT_MATHLIB_REV_V4;
|
|
105
|
+
const zeta23Rev = spec.zeta23_rev ?? DEFAULT_ZETA23_REV_V4;
|
|
106
|
+
const swarmProofsRev = spec.swarm_proofs_rev ?? "main";
|
|
107
|
+
lines.push("[[require]]", 'name = "mathlib"', 'git = "https://github.com/leanprover-community/mathlib4.git"', `rev = ${JSON.stringify(mathlibRev)}`, "", "[[require]]", 'name = "Zeta23"', 'git = "https://github.com/anthropics/formal-math"', `rev = ${JSON.stringify(zeta23Rev)}`, 'subDir = "zeta23"', "", "[[require]]", 'name = "SwarmProofs"', 'git = "https://github.com/corsur/swarm-tips"', `rev = ${JSON.stringify(swarmProofsRev)}`, 'subDir = "proofs/SwarmProofs"', "");
|
|
108
|
+
}
|
|
109
|
+
else if (spec.lean_policy === 2 || spec.lean_policy === 3 || spec.mathlib_rev) {
|
|
110
|
+
const mathlibRev = spec.mathlib_rev ?? DEFAULT_MATHLIB_REV_V2_V3;
|
|
111
|
+
lines.push("[[require]]", 'name = "mathlib"', 'git = "https://github.com/leanprover-community/mathlib4.git"', `rev = ${JSON.stringify(mathlibRev)}`, "");
|
|
112
|
+
}
|
|
113
|
+
lines.push("[[lean_lib]]", 'name = "TargetStatement"', "", "[[lean_lib]]", 'name = "Solution"', "");
|
|
114
|
+
return lines.join("\n");
|
|
115
|
+
}
|
|
116
|
+
function formatLimitsSummary(limits) {
|
|
117
|
+
const parts = [];
|
|
118
|
+
if (limits.max_proof_lines !== undefined && limits.max_proof_lines !== null) {
|
|
119
|
+
parts.push(`max_proof_lines=${limits.max_proof_lines}`);
|
|
120
|
+
}
|
|
121
|
+
if (limits.max_proof_bytes !== undefined && limits.max_proof_bytes !== null) {
|
|
122
|
+
parts.push(`max_proof_bytes=${limits.max_proof_bytes}`);
|
|
123
|
+
}
|
|
124
|
+
if (limits.max_line_width !== undefined && limits.max_line_width !== null) {
|
|
125
|
+
parts.push(`max_line_width=${limits.max_line_width}`);
|
|
126
|
+
}
|
|
127
|
+
return parts.length > 0 ? parts.join(", ") : undefined;
|
|
128
|
+
}
|
|
129
|
+
function buildTargetStatementLean(spec) {
|
|
130
|
+
const body = spec.statement_lean.endsWith("\n")
|
|
131
|
+
? spec.statement_lean
|
|
132
|
+
: `${spec.statement_lean}\n`;
|
|
133
|
+
return (`${body}\n` +
|
|
134
|
+
"namespace SwarmVerifyTarget\n" +
|
|
135
|
+
"def statementProp : Prop := _root_.statementProp\n" +
|
|
136
|
+
"end SwarmVerifyTarget\n");
|
|
137
|
+
}
|
|
138
|
+
function buildSolutionLean(spec) {
|
|
139
|
+
const importsLabel = spec.import_allowlist.length > 0
|
|
140
|
+
? spec.import_allowlist.join(", ")
|
|
141
|
+
: "none (self-contained)";
|
|
142
|
+
const polarityLine = spec.allows_disproof
|
|
143
|
+
? "-- Polarity: bidirectional — prove `theorem proof : statementProp` OR refute with `theorem disproof : ¬ statementProp`"
|
|
144
|
+
: "-- Polarity: proof only — prove `theorem proof : statementProp`";
|
|
145
|
+
const limitsSummary = formatLimitsSummary(spec.limits);
|
|
146
|
+
const lines = [
|
|
147
|
+
`-- Swarm Tips Lean 4 Task: ${spec.task_id}`,
|
|
148
|
+
`-- Policy: v${spec.lean_policy} (${spec.mode}) | Toolchain: ${spec.lean_toolchain}`,
|
|
149
|
+
`-- Allowed imports: ${importsLabel}`,
|
|
150
|
+
polarityLine,
|
|
151
|
+
];
|
|
152
|
+
if (limitsSummary) {
|
|
153
|
+
lines.push(`-- Compression limits: ${limitsSummary}`);
|
|
154
|
+
}
|
|
155
|
+
lines.push("", "import TargetStatement", "", "theorem proof : statementProp := by", " sorry", "");
|
|
156
|
+
return lines.join("\n");
|
|
157
|
+
}
|
|
158
|
+
function buildReadmeMd(spec) {
|
|
159
|
+
const importsLabel = spec.import_allowlist.length > 0
|
|
160
|
+
? spec.import_allowlist.map((i) => `\`${i}\``).join(", ")
|
|
161
|
+
: "None (self-contained)";
|
|
162
|
+
const polarityLabel = spec.allows_disproof
|
|
163
|
+
? "Bidirectional (`theorem proof : statementProp` or `theorem disproof : ¬ statementProp`)"
|
|
164
|
+
: "Proof only (`theorem proof : statementProp`)";
|
|
165
|
+
const limitsSummary = formatLimitsSummary(spec.limits);
|
|
166
|
+
const lines = [
|
|
167
|
+
`# Swarm Tips Lean 4 Workspace — \`${spec.task_id}\``,
|
|
168
|
+
"",
|
|
169
|
+
`- **Policy:** \`v${spec.lean_policy}\` (\`${spec.mode}\`)`,
|
|
170
|
+
`- **Toolchain:** \`${spec.lean_toolchain}\``,
|
|
171
|
+
`- **Allowed Imports:** ${importsLabel}`,
|
|
172
|
+
`- **Polarity:** ${polarityLabel}`,
|
|
173
|
+
];
|
|
174
|
+
if (limitsSummary) {
|
|
175
|
+
lines.push(`- **Compression Limits:** \`${limitsSummary}\``);
|
|
176
|
+
}
|
|
177
|
+
lines.push("", "## 1. Local Build (Optional)", "", "```bash", "lake exe cache get && lake build", "```", "", "## 2. Remote Compile Dry-Run (Zero SOL)", "", "```bash", `npx @swarm-tips/client tasks check ${spec.task_id} Solution.lean --compile`, "```", "", "## 3. Submit Proof", "", "```bash", `npx @swarm-tips/client tasks submit ${spec.task_id} --proof-file Solution.lean`, "```", "");
|
|
178
|
+
return lines.join("\n");
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Pure helper generating the complete Lean 4 workspace file map for a
|
|
182
|
+
* `platform === 10` task without touching the filesystem.
|
|
183
|
+
*/
|
|
184
|
+
export function buildLeanWorkspaceFiles(task, taskId) {
|
|
185
|
+
const spec = extractLeanWorkspaceSpec(task, taskId);
|
|
186
|
+
return {
|
|
187
|
+
"lean-toolchain": `${spec.lean_toolchain}\n`,
|
|
188
|
+
"lakefile.toml": buildLakefileToml(spec),
|
|
189
|
+
"TargetStatement.lean": buildTargetStatementLean(spec),
|
|
190
|
+
"Solution.lean": buildSolutionLean(spec),
|
|
191
|
+
"README.md": buildReadmeMd(spec),
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Materialize a Lean 4 prover workspace on disk. Refuses to overwrite any
|
|
196
|
+
* existing non-empty target file unless `force === true`.
|
|
197
|
+
*/
|
|
198
|
+
export function scaffoldLeanWorkspace(task, taskId, targetDir, force = false) {
|
|
199
|
+
const spec = extractLeanWorkspaceSpec(task, taskId);
|
|
200
|
+
const files = buildLeanWorkspaceFiles(task, taskId);
|
|
201
|
+
const resolvedDir = resolve(targetDir);
|
|
202
|
+
const existingNonEmpty = [];
|
|
203
|
+
for (const relName of Object.keys(files)) {
|
|
204
|
+
const dest = join(resolvedDir, relName);
|
|
205
|
+
if (existsSync(dest)) {
|
|
206
|
+
const stat = statSync(dest);
|
|
207
|
+
if (stat.isDirectory() || stat.size > 0) {
|
|
208
|
+
existingNonEmpty.push(relName);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
if (existingNonEmpty.length > 0 && !force) {
|
|
213
|
+
throw new CliUsageError(`Refusing to overwrite non-empty target file(s) in ${resolvedDir}: ${existingNonEmpty.join(", ")}. Pass --force to overwrite.`);
|
|
214
|
+
}
|
|
215
|
+
mkdirSync(resolvedDir, { recursive: true });
|
|
216
|
+
const written = [];
|
|
217
|
+
for (const [relName, content] of Object.entries(files)) {
|
|
218
|
+
writeFileSync(join(resolvedDir, relName), content, "utf8");
|
|
219
|
+
written.push(relName);
|
|
220
|
+
}
|
|
221
|
+
return {
|
|
222
|
+
ok: true,
|
|
223
|
+
task_id: taskId,
|
|
224
|
+
dir: resolvedDir,
|
|
225
|
+
lean_policy: spec.lean_policy,
|
|
226
|
+
lean_toolchain: spec.lean_toolchain,
|
|
227
|
+
allows_disproof: spec.allows_disproof,
|
|
228
|
+
limits: spec.limits,
|
|
229
|
+
files: written,
|
|
230
|
+
};
|
|
231
|
+
}
|
package/dist/cli/swarm.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { readFileSync, realpathSync } from "node:fs";
|
|
3
|
+
import { resolve } from "node:path";
|
|
3
4
|
import { fileURLToPath } from "node:url";
|
|
4
5
|
import { SwarmAgent } from "../agent.js";
|
|
5
6
|
import { SwarmClientError } from "../errors.js";
|
|
@@ -8,6 +9,7 @@ import { validatePreparedTransaction, } from "../shillbot/api.js";
|
|
|
8
9
|
import { inspectTransaction } from "../shillbot/index.js";
|
|
9
10
|
import { CLIENT_VERSION } from "../version.js";
|
|
10
11
|
import { CliUsageError, assertNoSecretCliFlags, loadSignerFromCliOrEnv, } from "./keyfile.js";
|
|
12
|
+
import { scaffoldLeanWorkspace } from "./lean-init.js";
|
|
11
13
|
const BOOLEAN_FLAGS = new Set([
|
|
12
14
|
"--json",
|
|
13
15
|
"--yes",
|
|
@@ -19,6 +21,8 @@ const BOOLEAN_FLAGS = new Set([
|
|
|
19
21
|
"--unmute",
|
|
20
22
|
"--cursor",
|
|
21
23
|
"--allow-localhost-http",
|
|
24
|
+
"--compile",
|
|
25
|
+
"--force",
|
|
22
26
|
]);
|
|
23
27
|
export function parseCliArgs(argv) {
|
|
24
28
|
assertNoSecretCliFlags(argv);
|
|
@@ -99,14 +103,18 @@ Usage:
|
|
|
99
103
|
|
|
100
104
|
Commands:
|
|
101
105
|
auth whoami | token
|
|
106
|
+
onboard
|
|
102
107
|
listings [--limit N] [--category CAT] [--chain CHAIN]
|
|
103
108
|
reputation <wallet>
|
|
104
|
-
tasks list [--limit N] | get <id> | mine | earnings | next <task-json-or-id>
|
|
105
|
-
tasks
|
|
106
|
-
tasks
|
|
109
|
+
tasks list [--limit N] | get <id> | mine | earnings | onboard | next <task-json-or-id>
|
|
110
|
+
tasks init <id> [--dir <path>] [--force] (scaffold pinned Lean 4 workspace — zero auth / zero SOL)
|
|
111
|
+
tasks check <id> <proof-file> [--compile] (static or remote compile preflight with diagnostics & compression metrics)
|
|
112
|
+
tasks claim <id> [--payout-to <pubkey>] [--fee-payer <pubkey>] [--no-exec]
|
|
113
|
+
tasks submit <id> (--content <str> | --proof-file <path>) [--harness <name>] [--model <name>] [--payout-to <pubkey>] [--fee-payer <pubkey>] [--no-exec]
|
|
107
114
|
tasks verify <id> [--no-exec]
|
|
108
115
|
tasks finalize <id> [--no-exec]
|
|
109
116
|
tasks expire <id> [--no-exec] (client escrow refund past claim/submit deadline or past submitted_at + verification_timeout)
|
|
117
|
+
proofs list [--limit N] [--policy N] (public verified Lean 4 proofs & prover leaderboard)
|
|
110
118
|
inbox list [--status pending|all] [--limit N]
|
|
111
119
|
inbox open <msg_id...>
|
|
112
120
|
inbox send <to_wallet> <body> [--thread <id>] [--intent <intent>]
|
|
@@ -121,8 +129,10 @@ Commands:
|
|
|
121
129
|
Global Options:
|
|
122
130
|
--json Output structured JSON (auto-enabled when stdout is non-TTY)
|
|
123
131
|
--network <mainnet|devnet> Target network (default: mainnet)
|
|
124
|
-
--keypair-file <path> Solana keypair file (
|
|
132
|
+
--keypair-file <path> Solana keypair or 0x-prefixed 32-byte EVM hex key file (chmod 600 on POSIX)
|
|
125
133
|
--rpc-url <url> Override Solana RPC URL
|
|
134
|
+
--evm-rpc-url <url> Override EVM JSON-RPC URL
|
|
135
|
+
--evm-chain-id <id> Optional EVM chain ID or CAIP-2 identifier (e.g. 84532, eip155:8453)
|
|
126
136
|
--max-spend-lamports <n> Max SOL escrow spend cap (default: 0)
|
|
127
137
|
--max-spend-wei <n> Max EVM escrow spend cap (default: 0)
|
|
128
138
|
--help, -h Show this help text
|
|
@@ -131,7 +141,9 @@ Global Options:
|
|
|
131
141
|
Environment Variables:
|
|
132
142
|
SWARM_SOLANA_KEYPAIR_PATH Path to chmod 600 Solana keypair JSON/base58 file
|
|
133
143
|
SWARM_SOLANA_SECRET_KEY Base58 or JSON 64-byte Solana secret key
|
|
144
|
+
SWARM_EVM_KEYPAIR_PATH Path to chmod 600 0x-prefixed 32-byte EVM hex private key file
|
|
134
145
|
SWARM_EVM_PRIVATE_KEY 0x-prefixed 32-byte EVM private key
|
|
146
|
+
SWARM_EVM_RPC_URL EVM JSON-RPC URL for EVM transaction broadcast
|
|
135
147
|
SWARM_AUTH_TOKEN Pre-minted Bearer JWT
|
|
136
148
|
`;
|
|
137
149
|
const VALID_ACTIONS = new Set([
|
|
@@ -164,6 +176,50 @@ function writeError(stdout, stderr, jsonMode, message, code) {
|
|
|
164
176
|
stderr.write(`Error: ${message}\n`);
|
|
165
177
|
}
|
|
166
178
|
}
|
|
179
|
+
function defaultLeanWorkspaceDir(taskId) {
|
|
180
|
+
const safeSlug = taskId.replace(/[^A-Za-z0-9._-]+/g, "-").replace(/^-+|-+$/g, "") || "lean";
|
|
181
|
+
return resolve(`swarm-task-${safeSlug}`);
|
|
182
|
+
}
|
|
183
|
+
function formatPreflightHuman(res, compiled) {
|
|
184
|
+
const modeLabel = res.compiled ?? compiled ? "compile" : "static";
|
|
185
|
+
const statusLabel = res.ok ? "PASS" : "FAIL";
|
|
186
|
+
const verdictPart = res.verdict ? ` [${res.verdict}]` : "";
|
|
187
|
+
const lines = [
|
|
188
|
+
`${statusLabel} (${modeLabel})${verdictPart} — task ${res.task_id}`,
|
|
189
|
+
];
|
|
190
|
+
if (res.detail) {
|
|
191
|
+
lines.push(`Detail: ${res.detail}`);
|
|
192
|
+
}
|
|
193
|
+
if (res.metrics) {
|
|
194
|
+
const m = res.metrics;
|
|
195
|
+
lines.push(`Metrics: ${m.code_lines} code lines (${m.total_lines} total), ${m.code_bytes} code bytes (${m.raw_bytes} raw), max line width ${m.max_line_width}`);
|
|
196
|
+
}
|
|
197
|
+
if (res.limits) {
|
|
198
|
+
const parts = [];
|
|
199
|
+
if (res.limits.max_proof_lines !== undefined && res.limits.max_proof_lines !== null) {
|
|
200
|
+
parts.push(`max_proof_lines=${res.limits.max_proof_lines}`);
|
|
201
|
+
}
|
|
202
|
+
if (res.limits.max_proof_bytes !== undefined && res.limits.max_proof_bytes !== null) {
|
|
203
|
+
parts.push(`max_proof_bytes=${res.limits.max_proof_bytes}`);
|
|
204
|
+
}
|
|
205
|
+
if (res.limits.max_line_width !== undefined && res.limits.max_line_width !== null) {
|
|
206
|
+
parts.push(`max_line_width=${res.limits.max_line_width}`);
|
|
207
|
+
}
|
|
208
|
+
if (parts.length > 0) {
|
|
209
|
+
lines.push(`Limits: ${parts.join(", ")}`);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
if (Array.isArray(res.axioms) && res.axioms.length > 0) {
|
|
213
|
+
lines.push(`Axioms: ${res.axioms.join(", ")}`);
|
|
214
|
+
}
|
|
215
|
+
if (Array.isArray(res.diagnostics) && res.diagnostics.length > 0) {
|
|
216
|
+
lines.push("Diagnostics:");
|
|
217
|
+
for (const d of res.diagnostics) {
|
|
218
|
+
lines.push(` ${d.file}:${d.line}:${d.column}: ${d.severity}: ${d.message}`);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
return `${lines.join("\n")}\n`;
|
|
222
|
+
}
|
|
167
223
|
export async function runSwarmCli(argv, io = {}) {
|
|
168
224
|
const stdout = io.stdout ?? process.stdout;
|
|
169
225
|
const stderr = io.stderr ?? process.stderr;
|
|
@@ -235,6 +291,7 @@ export async function runSwarmCli(argv, io = {}) {
|
|
|
235
291
|
}
|
|
236
292
|
const taskPda = getStringFlag(flags, "--task-pda");
|
|
237
293
|
const feePayer = getStringFlag(flags, "--fee-payer");
|
|
294
|
+
const sponsor = getStringFlag(flags, "--sponsor");
|
|
238
295
|
const payoutTo = getStringFlag(flags, "--payout-to");
|
|
239
296
|
const rawExpireState = getStringFlag(flags, "--expire-task-state")?.trim().toLowerCase();
|
|
240
297
|
if (rawExpireState !== undefined &&
|
|
@@ -251,6 +308,7 @@ export async function runSwarmCli(argv, io = {}) {
|
|
|
251
308
|
wallet,
|
|
252
309
|
...(taskPda ? { taskPda } : {}),
|
|
253
310
|
...(feePayer ? { feePayer } : {}),
|
|
311
|
+
...(sponsor ? { sponsor } : {}),
|
|
254
312
|
...(payoutTo ? { payoutTo } : {}),
|
|
255
313
|
...(expireTaskState ? { expireTaskState } : {}),
|
|
256
314
|
...(oracleAuthority ? { oracleAuthority } : {}),
|
|
@@ -267,10 +325,13 @@ export async function runSwarmCli(argv, io = {}) {
|
|
|
267
325
|
}
|
|
268
326
|
throw new CliUsageError("Usage: swarm tx <inspect|verify> [options]");
|
|
269
327
|
}
|
|
328
|
+
const evmRpcUrl = getStringFlag(flags, "--evm-rpc-url") ?? env.SWARM_EVM_RPC_URL;
|
|
329
|
+
const evmChainId = getStringFlag(flags, "--evm-chain-id") ?? env.SWARM_EVM_CHAIN_ID;
|
|
270
330
|
const signer = loadSignerFromCliOrEnv({
|
|
271
331
|
keypairFile: getStringFlag(flags, "--keypair-file"),
|
|
272
332
|
env,
|
|
273
|
-
evmRpcUrl
|
|
333
|
+
evmRpcUrl,
|
|
334
|
+
evmChainId,
|
|
274
335
|
});
|
|
275
336
|
const agent = new SwarmAgent({
|
|
276
337
|
signer,
|
|
@@ -284,6 +345,14 @@ export async function runSwarmCli(argv, io = {}) {
|
|
|
284
345
|
maxEscrowSpendWei,
|
|
285
346
|
fetch: io.fetch,
|
|
286
347
|
});
|
|
348
|
+
if (cmd === "onboard") {
|
|
349
|
+
if (!signer && !agent.auth.walletAddress && !env.SWARM_AUTH_TOKEN) {
|
|
350
|
+
throw new CliUsageError("No wallet configured. Pass --keypair-file <path> (chmod 600) or set SWARM_SOLANA_KEYPAIR_PATH, SWARM_SOLANA_SECRET_KEY, or SWARM_EVM_PRIVATE_KEY.");
|
|
351
|
+
}
|
|
352
|
+
const res = await agent.onboard();
|
|
353
|
+
writeOutput(stdout, jsonMode, res);
|
|
354
|
+
return 0;
|
|
355
|
+
}
|
|
287
356
|
if (cmd === "auth") {
|
|
288
357
|
if (sub === "whoami") {
|
|
289
358
|
if (!signer && !agent.auth.walletAddress) {
|
|
@@ -341,6 +410,58 @@ export async function runSwarmCli(argv, io = {}) {
|
|
|
341
410
|
writeOutput(stdout, jsonMode, res);
|
|
342
411
|
return 0;
|
|
343
412
|
}
|
|
413
|
+
if (sub === "init") {
|
|
414
|
+
const id = rest[0]?.trim();
|
|
415
|
+
if (!id) {
|
|
416
|
+
throw new CliUsageError("Usage: swarm tasks init <task_id> [--dir <path>] [--force]");
|
|
417
|
+
}
|
|
418
|
+
const task = await agent.tasks.get(id);
|
|
419
|
+
const dirFlag = getStringFlag(flags, "--dir");
|
|
420
|
+
const targetDir = dirFlag ? resolve(dirFlag) : defaultLeanWorkspaceDir(id);
|
|
421
|
+
const result = scaffoldLeanWorkspace(task, id, targetDir, Boolean(flags.get("--force")));
|
|
422
|
+
if (jsonMode) {
|
|
423
|
+
writeOutput(stdout, true, result);
|
|
424
|
+
}
|
|
425
|
+
else {
|
|
426
|
+
stdout.write([
|
|
427
|
+
`Scaffolded Lean 4 workspace for ${result.task_id} in ${result.dir}`,
|
|
428
|
+
`Policy: v${result.lean_policy} | Toolchain: ${result.lean_toolchain}`,
|
|
429
|
+
`Files: ${result.files.join(", ")}`,
|
|
430
|
+
`Next: cd ${result.dir} && npx @swarm-tips/client tasks check ${result.task_id} Solution.lean --compile`,
|
|
431
|
+
].join("\n") + "\n");
|
|
432
|
+
}
|
|
433
|
+
return 0;
|
|
434
|
+
}
|
|
435
|
+
if (sub === "check") {
|
|
436
|
+
const id = rest[0]?.trim();
|
|
437
|
+
const proofPath = rest[1] ?? getStringFlag(flags, "--proof-file");
|
|
438
|
+
if (!id || !proofPath) {
|
|
439
|
+
throw new CliUsageError("Usage: swarm tasks check <task_id> <proof_file> [--compile]");
|
|
440
|
+
}
|
|
441
|
+
let proofLean;
|
|
442
|
+
try {
|
|
443
|
+
proofLean = readFileSync(proofPath, "utf8");
|
|
444
|
+
}
|
|
445
|
+
catch (err) {
|
|
446
|
+
throw new CliUsageError(`Failed to read proof file ${proofPath}: ${err instanceof Error ? err.message : String(err)}`);
|
|
447
|
+
}
|
|
448
|
+
if (!proofLean.trim()) {
|
|
449
|
+
throw new CliUsageError(`Proof file ${proofPath} is empty.`);
|
|
450
|
+
}
|
|
451
|
+
const compile = Boolean(flags.get("--compile"));
|
|
452
|
+
const res = await agent.tasks.preflight(id, {
|
|
453
|
+
proof_lean: proofLean,
|
|
454
|
+
...(compile ? { compile: true } : {}),
|
|
455
|
+
network,
|
|
456
|
+
});
|
|
457
|
+
if (jsonMode) {
|
|
458
|
+
writeOutput(stdout, true, res);
|
|
459
|
+
}
|
|
460
|
+
else {
|
|
461
|
+
stdout.write(formatPreflightHuman(res, compile));
|
|
462
|
+
}
|
|
463
|
+
return res.ok ? 0 : 1;
|
|
464
|
+
}
|
|
344
465
|
if (sub === "mine") {
|
|
345
466
|
const tasks = await agent.tasks.mine();
|
|
346
467
|
writeOutput(stdout, jsonMode, { tasks });
|
|
@@ -351,6 +472,11 @@ export async function runSwarmCli(argv, io = {}) {
|
|
|
351
472
|
writeOutput(stdout, jsonMode, res);
|
|
352
473
|
return 0;
|
|
353
474
|
}
|
|
475
|
+
if (sub === "onboard") {
|
|
476
|
+
const res = await agent.onboard();
|
|
477
|
+
writeOutput(stdout, jsonMode, res);
|
|
478
|
+
return 0;
|
|
479
|
+
}
|
|
354
480
|
if (sub === "next") {
|
|
355
481
|
const arg = rest[0] ?? (await readStdinText(io));
|
|
356
482
|
if (!arg) {
|
|
@@ -364,25 +490,69 @@ export async function runSwarmCli(argv, io = {}) {
|
|
|
364
490
|
}
|
|
365
491
|
if (sub === "claim") {
|
|
366
492
|
const id = rest[0];
|
|
367
|
-
if (!id)
|
|
368
|
-
throw new CliUsageError("Usage: swarm tasks claim <task_id> [--payout-to <pubkey>] [--no-exec]");
|
|
493
|
+
if (!id) {
|
|
494
|
+
throw new CliUsageError("Usage: swarm tasks claim <task_id> [--payout-to <pubkey>] [--fee-payer <pubkey>] [--no-exec]");
|
|
495
|
+
}
|
|
369
496
|
const payoutTo = getStringFlag(flags, "--payout-to");
|
|
497
|
+
const feePayer = getStringFlag(flags, "--fee-payer");
|
|
498
|
+
const execOpts = payoutTo || feePayer
|
|
499
|
+
? { ...(payoutTo ? { payoutTo } : {}), ...(feePayer ? { feePayer } : {}) }
|
|
500
|
+
: undefined;
|
|
370
501
|
const res = noExec
|
|
371
502
|
? await agent.tasks.claim(id)
|
|
372
|
-
: await agent.tasks.claimAndExecute(id,
|
|
503
|
+
: await agent.tasks.claimAndExecute(id, execOpts);
|
|
373
504
|
writeOutput(stdout, jsonMode, res);
|
|
374
505
|
return 0;
|
|
375
506
|
}
|
|
376
507
|
if (sub === "submit") {
|
|
377
|
-
const id = rest[0];
|
|
508
|
+
const id = rest[0]?.trim();
|
|
378
509
|
const content = getStringFlag(flags, "--content");
|
|
379
|
-
|
|
380
|
-
|
|
510
|
+
const proofFile = getStringFlag(flags, "--proof-file");
|
|
511
|
+
const harness = getStringFlag(flags, "--harness");
|
|
512
|
+
const model = getStringFlag(flags, "--model");
|
|
513
|
+
if (!id || (!content && !proofFile)) {
|
|
514
|
+
throw new CliUsageError("Usage: swarm tasks submit <task_id> (--content <str> | --proof-file <path>) [--harness <name>] [--model <name>] [--payout-to <pubkey>] [--fee-payer <pubkey>] [--no-exec]");
|
|
515
|
+
}
|
|
516
|
+
let proofLean;
|
|
517
|
+
if (proofFile) {
|
|
518
|
+
try {
|
|
519
|
+
proofLean = readFileSync(proofFile, "utf8");
|
|
520
|
+
}
|
|
521
|
+
catch (err) {
|
|
522
|
+
throw new CliUsageError(`Failed to read proof file ${proofFile}: ${err instanceof Error ? err.message : String(err)}`);
|
|
523
|
+
}
|
|
524
|
+
if (!proofLean.trim()) {
|
|
525
|
+
throw new CliUsageError(`Proof file ${proofFile} is empty.`);
|
|
526
|
+
}
|
|
381
527
|
}
|
|
382
528
|
const payoutTo = getStringFlag(flags, "--payout-to");
|
|
383
|
-
const
|
|
384
|
-
|
|
385
|
-
|
|
529
|
+
const feePayer = getStringFlag(flags, "--fee-payer");
|
|
530
|
+
if (noExec) {
|
|
531
|
+
const submitBody = {
|
|
532
|
+
content_id: content ?? "",
|
|
533
|
+
...(proofLean !== undefined ? { proof_lean: proofLean } : {}),
|
|
534
|
+
...(harness ? { agent_harness: harness } : {}),
|
|
535
|
+
...(model ? { model_family: model } : {}),
|
|
536
|
+
};
|
|
537
|
+
const res = await agent.tasks.submit(id, submitBody);
|
|
538
|
+
writeOutput(stdout, jsonMode, res);
|
|
539
|
+
return 0;
|
|
540
|
+
}
|
|
541
|
+
const extra = {
|
|
542
|
+
...(proofLean !== undefined ? { proof_lean: proofLean } : {}),
|
|
543
|
+
...(harness ? { agent_harness: harness } : {}),
|
|
544
|
+
...(model ? { model_family: model } : {}),
|
|
545
|
+
};
|
|
546
|
+
const execOpts = payoutTo || feePayer || harness || model || Object.keys(extra).length > 0
|
|
547
|
+
? {
|
|
548
|
+
...(payoutTo ? { payoutTo } : {}),
|
|
549
|
+
...(feePayer ? { feePayer } : {}),
|
|
550
|
+
...(harness ? { agentHarness: harness } : {}),
|
|
551
|
+
...(model ? { modelFamily: model } : {}),
|
|
552
|
+
...(Object.keys(extra).length > 0 ? { extra } : {}),
|
|
553
|
+
}
|
|
554
|
+
: undefined;
|
|
555
|
+
const res = await agent.tasks.submitAndExecute(id, content ?? "", execOpts);
|
|
386
556
|
writeOutput(stdout, jsonMode, res);
|
|
387
557
|
return 0;
|
|
388
558
|
}
|
|
@@ -416,7 +586,19 @@ export async function runSwarmCli(argv, io = {}) {
|
|
|
416
586
|
writeOutput(stdout, jsonMode, res);
|
|
417
587
|
return 0;
|
|
418
588
|
}
|
|
419
|
-
throw new CliUsageError("Usage: swarm tasks <list|get|mine|earnings|next|claim|submit|verify|finalize|expire> ...");
|
|
589
|
+
throw new CliUsageError("Usage: swarm tasks <list|get|init|check|mine|earnings|onboard|next|claim|submit|verify|finalize|expire> ...");
|
|
590
|
+
}
|
|
591
|
+
if (cmd === "proofs") {
|
|
592
|
+
if (sub === "list" || !sub) {
|
|
593
|
+
const res = await agent.proofs.list({
|
|
594
|
+
network,
|
|
595
|
+
limit: getIntFlag(flags, "--limit"),
|
|
596
|
+
policy: getIntFlag(flags, "--policy"),
|
|
597
|
+
});
|
|
598
|
+
writeOutput(stdout, jsonMode, res);
|
|
599
|
+
return 0;
|
|
600
|
+
}
|
|
601
|
+
throw new CliUsageError("Usage: swarm proofs list [--limit N] [--policy N]");
|
|
420
602
|
}
|
|
421
603
|
if (cmd === "inbox") {
|
|
422
604
|
if (sub === "list") {
|