@cjhyy/code-shell-arena 0.8.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 +46 -0
- package/dist/arena.d.ts +45 -0
- package/dist/arena.js +362 -0
- package/dist/capability.d.ts +43 -0
- package/dist/capability.js +453 -0
- package/dist/context/context-tools.d.ts +16 -0
- package/dist/context/context-tools.js +272 -0
- package/dist/context/within-root.d.ts +7 -0
- package/dist/context/within-root.js +15 -0
- package/dist/detect-mode.d.ts +20 -0
- package/dist/detect-mode.js +78 -0
- package/dist/digest-builder.d.ts +25 -0
- package/dist/digest-builder.js +120 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.js +23 -0
- package/dist/index.runtime.d.ts +13 -0
- package/dist/index.runtime.js +11 -0
- package/dist/iterate/convergence.d.ts +25 -0
- package/dist/iterate/convergence.js +103 -0
- package/dist/iterate/formats/index.d.ts +22 -0
- package/dist/iterate/formats/index.js +283 -0
- package/dist/iterate/index.d.ts +11 -0
- package/dist/iterate/index.js +9 -0
- package/dist/iterate/iterative-arena.d.ts +31 -0
- package/dist/iterate/iterative-arena.js +252 -0
- package/dist/iterate/parse.d.ts +42 -0
- package/dist/iterate/parse.js +123 -0
- package/dist/iterate/phases/argue.d.ts +22 -0
- package/dist/iterate/phases/argue.js +165 -0
- package/dist/iterate/phases/revise.d.ts +16 -0
- package/dist/iterate/phases/revise.js +62 -0
- package/dist/iterate/phases/tournament.d.ts +34 -0
- package/dist/iterate/phases/tournament.js +113 -0
- package/dist/iterate/tools/web-tools.d.ts +13 -0
- package/dist/iterate/tools/web-tools.js +54 -0
- package/dist/iterate/types.d.ts +152 -0
- package/dist/iterate/types.js +8 -0
- package/dist/ledger.d.ts +47 -0
- package/dist/ledger.js +159 -0
- package/dist/lenses/architecture.d.ts +5 -0
- package/dist/lenses/architecture.js +22 -0
- package/dist/lenses/engineering.d.ts +5 -0
- package/dist/lenses/engineering.js +22 -0
- package/dist/lenses/general.d.ts +5 -0
- package/dist/lenses/general.js +20 -0
- package/dist/lenses/index.d.ts +16 -0
- package/dist/lenses/index.js +47 -0
- package/dist/lenses/product.d.ts +5 -0
- package/dist/lenses/product.js +22 -0
- package/dist/model-presets.d.ts +23 -0
- package/dist/model-presets.js +44 -0
- package/dist/phases/adjudication.d.ts +25 -0
- package/dist/phases/adjudication.js +142 -0
- package/dist/phases/build-consensus.d.ts +30 -0
- package/dist/phases/build-consensus.js +85 -0
- package/dist/phases/claim-registry.d.ts +26 -0
- package/dist/phases/claim-registry.js +60 -0
- package/dist/phases/cross-review.d.ts +47 -0
- package/dist/phases/cross-review.js +224 -0
- package/dist/phases/debate-rounds.d.ts +28 -0
- package/dist/phases/debate-rounds.js +161 -0
- package/dist/phases/participant-research.d.ts +39 -0
- package/dist/phases/participant-research.js +322 -0
- package/dist/phases/planning-detail-expansion.d.ts +39 -0
- package/dist/phases/planning-detail-expansion.js +120 -0
- package/dist/planner.d.ts +28 -0
- package/dist/planner.js +312 -0
- package/dist/providers/docs.d.ts +7 -0
- package/dist/providers/docs.js +111 -0
- package/dist/providers/git.d.ts +8 -0
- package/dist/providers/git.js +174 -0
- package/dist/providers/index.d.ts +32 -0
- package/dist/providers/index.js +132 -0
- package/dist/providers/none.d.ts +7 -0
- package/dist/providers/none.js +11 -0
- package/dist/providers/repo.d.ts +7 -0
- package/dist/providers/repo.js +258 -0
- package/dist/render/session.d.ts +17 -0
- package/dist/render/session.js +190 -0
- package/dist/render/terminal.d.ts +34 -0
- package/dist/render/terminal.js +286 -0
- package/dist/strategies/discussion.d.ts +25 -0
- package/dist/strategies/discussion.js +143 -0
- package/dist/strategies/index.d.ts +15 -0
- package/dist/strategies/index.js +28 -0
- package/dist/strategies/language-wrapper.d.ts +17 -0
- package/dist/strategies/language-wrapper.js +102 -0
- package/dist/strategies/lens-wrapper.d.ts +16 -0
- package/dist/strategies/lens-wrapper.js +236 -0
- package/dist/strategies/planning.d.ts +30 -0
- package/dist/strategies/planning.js +225 -0
- package/dist/strategies/review.d.ts +26 -0
- package/dist/strategies/review.js +168 -0
- package/dist/strategies/utils.d.ts +36 -0
- package/dist/strategies/utils.js +603 -0
- package/dist/tools/selector.d.ts +17 -0
- package/dist/tools/selector.js +61 -0
- package/dist/transitions.d.ts +53 -0
- package/dist/transitions.js +97 -0
- package/dist/types.d.ts +518 -0
- package/dist/types.js +27 -0
- package/package.json +45 -0
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Context tools — read-only tools that arena participants can use
|
|
3
|
+
* to fetch additional source context during research.
|
|
4
|
+
*
|
|
5
|
+
* Security: All shell commands use execFileSync with argument arrays
|
|
6
|
+
* to prevent command injection. File paths are validated against the
|
|
7
|
+
* repository boundary to prevent path traversal.
|
|
8
|
+
*/
|
|
9
|
+
import { readFileSync, existsSync, readdirSync, statSync, realpathSync } from "node:fs";
|
|
10
|
+
import { execFileSync } from "node:child_process";
|
|
11
|
+
import { resolve, join, dirname, basename } from "node:path";
|
|
12
|
+
import { isWithinRoot } from "./within-root.js";
|
|
13
|
+
export const MAX_TOOL_RESULT = 15_000; // chars per tool call
|
|
14
|
+
// 5 was too generous for thinking-mode models — they'd spend the
|
|
15
|
+
// entire budget on read_file with textLen=0, then need an
|
|
16
|
+
// expensive force_conclude round to actually emit findings (logs
|
|
17
|
+
// consistently showed 5/5 rounds + 70-150s force_conclude). Capping
|
|
18
|
+
// at 3 makes the participant commit to a conclusion much sooner.
|
|
19
|
+
export const MAX_TOOL_ROUNDS = 3;
|
|
20
|
+
/** Repository root boundary — all file access is restricted to this directory */
|
|
21
|
+
const REPO_ROOT = resolve(".");
|
|
22
|
+
export const CONTEXT_TOOLS = [
|
|
23
|
+
{
|
|
24
|
+
name: "read_file",
|
|
25
|
+
description: "Read a file from the repository. Use this when you need to see the full source of a file " +
|
|
26
|
+
"referenced in the context, or to check surrounding code (callers, type definitions, etc.).",
|
|
27
|
+
inputSchema: {
|
|
28
|
+
type: "object",
|
|
29
|
+
properties: {
|
|
30
|
+
path: { type: "string", description: "Relative file path from repo root" },
|
|
31
|
+
offset: { type: "number", description: "Start line (1-based, optional)" },
|
|
32
|
+
limit: { type: "number", description: "Max lines to read (optional, default 200)" },
|
|
33
|
+
},
|
|
34
|
+
required: ["path"],
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
name: "grep_code",
|
|
39
|
+
description: "Search for a pattern across the codebase. Use this to find callers, references, " +
|
|
40
|
+
"type definitions, or usages of symbols.",
|
|
41
|
+
inputSchema: {
|
|
42
|
+
type: "object",
|
|
43
|
+
properties: {
|
|
44
|
+
pattern: { type: "string", description: "Regex pattern to search for" },
|
|
45
|
+
glob: { type: "string", description: "File glob filter, e.g. '*.ts' (optional)" },
|
|
46
|
+
},
|
|
47
|
+
required: ["pattern"],
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
name: "list_files",
|
|
52
|
+
description: "List files in a directory. Use this to understand project structure around relevant files.",
|
|
53
|
+
inputSchema: {
|
|
54
|
+
type: "object",
|
|
55
|
+
properties: {
|
|
56
|
+
path: { type: "string", description: "Directory path relative to repo root (default: '.')" },
|
|
57
|
+
},
|
|
58
|
+
required: [],
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
name: "git_show",
|
|
63
|
+
description: "Show a git object (commit, file at a ref, etc.). Use for inspecting specific commits " +
|
|
64
|
+
"or viewing a file at a different branch/commit.",
|
|
65
|
+
inputSchema: {
|
|
66
|
+
type: "object",
|
|
67
|
+
properties: {
|
|
68
|
+
ref: { type: "string", description: "Git ref, e.g. 'HEAD~3', 'main:src/foo.ts', commit hash" },
|
|
69
|
+
},
|
|
70
|
+
required: ["ref"],
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
name: "git_blame",
|
|
75
|
+
description: "Show git blame for a file. Use to understand who changed what and when.",
|
|
76
|
+
inputSchema: {
|
|
77
|
+
type: "object",
|
|
78
|
+
properties: {
|
|
79
|
+
path: { type: "string", description: "File path" },
|
|
80
|
+
lines: { type: "string", description: "Line range, e.g. '10,20' (optional)" },
|
|
81
|
+
},
|
|
82
|
+
required: ["path"],
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
];
|
|
86
|
+
/**
|
|
87
|
+
* Execute a context tool call and return the result string.
|
|
88
|
+
*/
|
|
89
|
+
export function executeContextTool(tc) {
|
|
90
|
+
try {
|
|
91
|
+
switch (tc.toolName) {
|
|
92
|
+
case "read_file":
|
|
93
|
+
return executeReadFile(tc.args);
|
|
94
|
+
case "grep_code":
|
|
95
|
+
return executeGrepCode(tc.args);
|
|
96
|
+
case "list_files":
|
|
97
|
+
return executeListFiles(tc.args);
|
|
98
|
+
case "git_show":
|
|
99
|
+
return executeGitShow(tc.args);
|
|
100
|
+
case "git_blame":
|
|
101
|
+
return executeGitBlame(tc.args);
|
|
102
|
+
default:
|
|
103
|
+
return `Unknown tool: ${tc.toolName}`;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
catch (err) {
|
|
107
|
+
return `Tool error: ${err.message}`;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Validate that a file path resolves within the repository root.
|
|
112
|
+
* Returns the resolved absolute path, or null if outside the boundary.
|
|
113
|
+
*/
|
|
114
|
+
function validatePath(filePath) {
|
|
115
|
+
const resolved = resolve(filePath);
|
|
116
|
+
// Realpath both sides so a symlink planted inside the repo that points OUTSIDE
|
|
117
|
+
// can't pass a lexical containment check (isWithinRoot assumes resolved input
|
|
118
|
+
// and won't catch a link escape on its own). Matches fs-service's discipline.
|
|
119
|
+
// Reads are read-only, but a contained-looking symlink would still leak an
|
|
120
|
+
// out-of-repo file into arena context. Fall back to the lexical check when the
|
|
121
|
+
// target doesn't exist yet (a read of a missing file fails downstream anyway).
|
|
122
|
+
let realRoot;
|
|
123
|
+
try {
|
|
124
|
+
realRoot = realpathSync(REPO_ROOT);
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
realRoot = REPO_ROOT;
|
|
128
|
+
}
|
|
129
|
+
let realTarget;
|
|
130
|
+
try {
|
|
131
|
+
realTarget = realpathSync(resolved);
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
// Target doesn't exist yet. Compare REAL-vs-REAL by resolving the existing
|
|
135
|
+
// parent dir's realpath and rejoining the missing leaf — comparing realRoot
|
|
136
|
+
// (a real path) against a lexical `resolved` would false-reject valid files
|
|
137
|
+
// whenever REPO_ROOT (or any ancestor) is reached through a symlink.
|
|
138
|
+
let realParent;
|
|
139
|
+
try {
|
|
140
|
+
realParent = realpathSync(dirname(resolved));
|
|
141
|
+
}
|
|
142
|
+
catch {
|
|
143
|
+
// Parent missing too — no realpath available; fall back to lexical-vs-lexical
|
|
144
|
+
// so both operands share the same (unresolved) mode.
|
|
145
|
+
return isWithinRoot(resolve(REPO_ROOT), resolved) ? resolved : null;
|
|
146
|
+
}
|
|
147
|
+
const candidate = join(realParent, basename(resolved));
|
|
148
|
+
return isWithinRoot(realRoot, candidate) ? candidate : null;
|
|
149
|
+
}
|
|
150
|
+
return isWithinRoot(realRoot, realTarget) ? realTarget : null;
|
|
151
|
+
}
|
|
152
|
+
function executeReadFile(args) {
|
|
153
|
+
const filePath = args.path;
|
|
154
|
+
if (!validatePath(filePath))
|
|
155
|
+
return `Error: path outside repository: ${filePath}`;
|
|
156
|
+
if (!existsSync(filePath))
|
|
157
|
+
return `Error: file not found: ${filePath}`;
|
|
158
|
+
const raw = readFileSync(filePath, "utf-8");
|
|
159
|
+
const lines = raw.split("\n");
|
|
160
|
+
const offset = Math.max(0, (args.offset ?? 1) - 1);
|
|
161
|
+
// Guard `limit` before slicing: a negative limit makes lines.slice count
|
|
162
|
+
// "from the end" and silently drop the file's tail (reader thinks the file is
|
|
163
|
+
// shorter). A missing/NaN limit falls back to the default; a non-positive
|
|
164
|
+
// limit reads nothing.
|
|
165
|
+
const rawLimit = args.limit;
|
|
166
|
+
const limit = rawLimit === undefined || !Number.isFinite(rawLimit)
|
|
167
|
+
? 200
|
|
168
|
+
: rawLimit > 0
|
|
169
|
+
? Math.floor(rawLimit)
|
|
170
|
+
: 0;
|
|
171
|
+
const slice = lines.slice(offset, offset + limit);
|
|
172
|
+
const numbered = slice.map((l, i) => `${offset + i + 1}\t${l}`).join("\n");
|
|
173
|
+
return truncateResult(numbered);
|
|
174
|
+
}
|
|
175
|
+
function executeGrepCode(args) {
|
|
176
|
+
const pattern = args.pattern;
|
|
177
|
+
const glob = args.glob;
|
|
178
|
+
const grepArgs = ["-rn", "-E", "--"];
|
|
179
|
+
if (glob) {
|
|
180
|
+
grepArgs.splice(1, 0, `--include=${glob}`);
|
|
181
|
+
}
|
|
182
|
+
grepArgs.push(pattern, ".");
|
|
183
|
+
const result = execFileSafe("grep", grepArgs, 80);
|
|
184
|
+
return result || "No matches found.";
|
|
185
|
+
}
|
|
186
|
+
function executeListFiles(args) {
|
|
187
|
+
const dir = args.path || ".";
|
|
188
|
+
if (!validatePath(dir))
|
|
189
|
+
return `Error: path outside repository: ${dir}`;
|
|
190
|
+
try {
|
|
191
|
+
const entries = readdirSync(dir);
|
|
192
|
+
const lines = [];
|
|
193
|
+
for (const entry of entries.slice(0, 50)) {
|
|
194
|
+
const fullPath = join(dir, entry);
|
|
195
|
+
try {
|
|
196
|
+
const st = statSync(fullPath);
|
|
197
|
+
const type = st.isDirectory() ? "d" : "-";
|
|
198
|
+
const size = st.isDirectory() ? "" : ` ${st.size}`;
|
|
199
|
+
lines.push(`${type} ${entry}${size}`);
|
|
200
|
+
}
|
|
201
|
+
catch {
|
|
202
|
+
lines.push(`? ${entry}`);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return lines.join("\n") || `Empty directory: ${dir}`;
|
|
206
|
+
}
|
|
207
|
+
catch {
|
|
208
|
+
return `Directory not found: ${dir}`;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
function executeGitShow(args) {
|
|
212
|
+
const ref = sanitizeGitRef(args.ref);
|
|
213
|
+
if (!ref)
|
|
214
|
+
return "Error: invalid git ref";
|
|
215
|
+
const result = execFileSafe("git", ["show", "--", ref], 200);
|
|
216
|
+
return truncateResult(result || `Could not resolve: ${ref}`);
|
|
217
|
+
}
|
|
218
|
+
function executeGitBlame(args) {
|
|
219
|
+
const filePath = args.path;
|
|
220
|
+
if (!validatePath(filePath))
|
|
221
|
+
return `Error: path outside repository: ${filePath}`;
|
|
222
|
+
const lines = args.lines;
|
|
223
|
+
const gitArgs = ["blame"];
|
|
224
|
+
if (lines && /^\d+,\d+$/.test(lines)) {
|
|
225
|
+
gitArgs.push(`-L`, lines);
|
|
226
|
+
}
|
|
227
|
+
gitArgs.push("--", filePath);
|
|
228
|
+
const result = execFileSafe("git", gitArgs, 50);
|
|
229
|
+
return result || `Could not blame: ${filePath}`;
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Sanitize a git ref string from LLM output.
|
|
233
|
+
* Only allows safe git ref characters.
|
|
234
|
+
*/
|
|
235
|
+
function sanitizeGitRef(ref) {
|
|
236
|
+
if (!ref || ref.length > 200)
|
|
237
|
+
return null;
|
|
238
|
+
// Only allow alphanumeric, /, -, _, ~, ^, ., :
|
|
239
|
+
const cleaned = ref.replace(/[^a-zA-Z0-9/_\-~^.:]/g, "");
|
|
240
|
+
// Block shell metacharacters and path traversal attempts
|
|
241
|
+
if (/\.\.\/|;\s|&&|\|\||`|\$\(/.test(cleaned))
|
|
242
|
+
return null;
|
|
243
|
+
return cleaned || null;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Execute a command safely using execFileSync (no shell interpretation).
|
|
247
|
+
* Optionally limits output to maxLines.
|
|
248
|
+
*/
|
|
249
|
+
function execFileSafe(cmd, args, maxLines) {
|
|
250
|
+
try {
|
|
251
|
+
const raw = execFileSync(cmd, args, {
|
|
252
|
+
encoding: "utf-8",
|
|
253
|
+
maxBuffer: 512 * 1024,
|
|
254
|
+
timeout: 10_000,
|
|
255
|
+
}).trim();
|
|
256
|
+
if (maxLines) {
|
|
257
|
+
const lines = raw.split("\n");
|
|
258
|
+
if (lines.length > maxLines) {
|
|
259
|
+
return lines.slice(0, maxLines).join("\n") + `\n... (${lines.length - maxLines} more lines)`;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return raw;
|
|
263
|
+
}
|
|
264
|
+
catch {
|
|
265
|
+
return "";
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
function truncateResult(text) {
|
|
269
|
+
if (text.length <= MAX_TOOL_RESULT)
|
|
270
|
+
return text;
|
|
271
|
+
return text.slice(0, MAX_TOOL_RESULT) + "\n... (truncated)";
|
|
272
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* True when `target` is the root itself or a descendant of it. Uses the
|
|
3
|
+
* platform separator (the old check hardcoded "/", which broke on Windows)
|
|
4
|
+
* and avoids the sibling-prefix trap (/repo vs /repo-evil). Both inputs are
|
|
5
|
+
* assumed already resolved to absolute paths.
|
|
6
|
+
*/
|
|
7
|
+
export declare function isWithinRoot(root: string, target: string): boolean;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as path from "node:path";
|
|
2
|
+
/**
|
|
3
|
+
* True when `target` is the root itself or a descendant of it. Uses the
|
|
4
|
+
* platform separator (the old check hardcoded "/", which broke on Windows)
|
|
5
|
+
* and avoids the sibling-prefix trap (/repo vs /repo-evil). Both inputs are
|
|
6
|
+
* assumed already resolved to absolute paths.
|
|
7
|
+
*/
|
|
8
|
+
export function isWithinRoot(root, target) {
|
|
9
|
+
if (target === root)
|
|
10
|
+
return true;
|
|
11
|
+
const rel = path.relative(root, target);
|
|
12
|
+
// Outside the root iff the relative path climbs out ("..") or is absolute
|
|
13
|
+
// (different drive on Windows).
|
|
14
|
+
return rel !== "" && !rel.startsWith("..") && !path.isAbsolute(rel);
|
|
15
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Arena mode auto-detection — infer review/discussion/planning from topic text.
|
|
3
|
+
*
|
|
4
|
+
* Heuristic-based: uses keyword matching with weighted scoring.
|
|
5
|
+
* Falls back to "review" (safest default) when confidence is low.
|
|
6
|
+
*/
|
|
7
|
+
import type { ArenaMode } from "./types.js";
|
|
8
|
+
/** Result of heuristic mode detection */
|
|
9
|
+
export interface ArenaModeDetection {
|
|
10
|
+
mode: ArenaMode;
|
|
11
|
+
confidence: "high" | "low";
|
|
12
|
+
reason: string;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Auto-detect arena mode from the topic string.
|
|
16
|
+
*
|
|
17
|
+
* Returns the detected mode, confidence level, and a human-readable reason.
|
|
18
|
+
* When confidence is low, falls back to "review" (the safest default).
|
|
19
|
+
*/
|
|
20
|
+
export declare function detectArenaMode(topic: string): ArenaModeDetection;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Arena mode auto-detection — infer review/discussion/planning from topic text.
|
|
3
|
+
*
|
|
4
|
+
* Heuristic-based: uses keyword matching with weighted scoring.
|
|
5
|
+
* Falls back to "review" (safest default) when confidence is low.
|
|
6
|
+
*/
|
|
7
|
+
const MODE_SIGNALS = {
|
|
8
|
+
review: [
|
|
9
|
+
{ patterns: [/\breview\b/i, /\baudit\b/i, /\binspect\b/i, /\bcheck\b/i], weight: 2 },
|
|
10
|
+
{ patterns: [/\bcode\s*quality\b/i, /\bbug\b/i, /\bvulnerab/i, /\bsecur/i] },
|
|
11
|
+
{ patterns: [/\bpr\b/i, /\bpull\s*request\b/i, /\bdiff\b/i, /\bchanges?\b/i] },
|
|
12
|
+
{ patterns: [/\brefactor/i, /\bclean\s*up\b/i, /\bimprove\b/i] },
|
|
13
|
+
],
|
|
14
|
+
discussion: [
|
|
15
|
+
{ patterns: [/\bdiscuss\b/i, /\bdebate\b/i, /\bcompare\b/i], weight: 2 },
|
|
16
|
+
{ patterns: [/\bpros?\s*(and|&|\/)\s*cons?\b/i, /\btrade\s*-?\s*offs?\b/i] },
|
|
17
|
+
{ patterns: [/\bshould\s+we\b/i, /\bwhich\s+(is|approach|way)\b/i, /\bvs\.?\b/i] },
|
|
18
|
+
{ patterns: [/\bopinion\b/i, /\bthoughts?\b/i, /\badvice\b/i] },
|
|
19
|
+
],
|
|
20
|
+
planning: [
|
|
21
|
+
{ patterns: [/\bplan\b/i, /\bplanning\b/i, /\broadmap\b/i, /\bstrategy\b/i], weight: 2 },
|
|
22
|
+
{ patterns: [/\barchitect/i, /\bdesign\b/i, /\bproposal\b/i] },
|
|
23
|
+
{ patterns: [/\bimplement(ation)?\s+(plan|strategy|approach)\b/i], weight: 2 },
|
|
24
|
+
{ patterns: [/\bphase\b/i, /\bmilestone\b/i, /\btimeline\b/i, /\bprioritiz/i] },
|
|
25
|
+
{ patterns: [/\bbuild\b/i, /\bcreate\b/i, /\bsetup\b/i, /\bbootstrap\b/i] },
|
|
26
|
+
],
|
|
27
|
+
};
|
|
28
|
+
/** Confidence threshold: below this → low confidence */
|
|
29
|
+
const HIGH_CONFIDENCE_THRESHOLD = 3;
|
|
30
|
+
/** Minimum lead over runner-up to be "high" confidence */
|
|
31
|
+
const MIN_LEAD = 2;
|
|
32
|
+
// ─── Detection ──────────────────────────────────────────────────
|
|
33
|
+
/**
|
|
34
|
+
* Auto-detect arena mode from the topic string.
|
|
35
|
+
*
|
|
36
|
+
* Returns the detected mode, confidence level, and a human-readable reason.
|
|
37
|
+
* When confidence is low, falls back to "review" (the safest default).
|
|
38
|
+
*/
|
|
39
|
+
export function detectArenaMode(topic) {
|
|
40
|
+
const scores = { review: 0, discussion: 0, planning: 0 };
|
|
41
|
+
for (const [mode, signals] of Object.entries(MODE_SIGNALS)) {
|
|
42
|
+
for (const signal of signals) {
|
|
43
|
+
const weight = signal.weight ?? 1;
|
|
44
|
+
for (const pattern of signal.patterns) {
|
|
45
|
+
if (pattern.test(topic)) {
|
|
46
|
+
scores[mode] += weight;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
// Sort modes by score descending
|
|
52
|
+
const ranked = Object.entries(scores)
|
|
53
|
+
.sort((a, b) => b[1] - a[1]);
|
|
54
|
+
const [bestMode, bestScore] = ranked[0];
|
|
55
|
+
const [, runnerUpScore] = ranked[1];
|
|
56
|
+
const lead = bestScore - runnerUpScore;
|
|
57
|
+
// Determine confidence
|
|
58
|
+
if (bestScore >= HIGH_CONFIDENCE_THRESHOLD && lead >= MIN_LEAD) {
|
|
59
|
+
return {
|
|
60
|
+
mode: bestMode,
|
|
61
|
+
confidence: "high",
|
|
62
|
+
reason: `Topic strongly matches "${bestMode}" (score: ${bestScore}, lead: +${lead})`,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
if (bestScore > 0 && lead > 0) {
|
|
66
|
+
return {
|
|
67
|
+
mode: bestMode,
|
|
68
|
+
confidence: "low",
|
|
69
|
+
reason: `Topic weakly matches "${bestMode}" (score: ${bestScore}, lead: +${lead})`,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
// No signal at all → default to review
|
|
73
|
+
return {
|
|
74
|
+
mode: "review",
|
|
75
|
+
confidence: "low",
|
|
76
|
+
reason: "No strong mode signal detected, defaulting to review",
|
|
77
|
+
};
|
|
78
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DigestBuilder — constructs RoundResearchDigest from the shared ledger.
|
|
3
|
+
*
|
|
4
|
+
* The digest is a filtered view injected into prompts to avoid
|
|
5
|
+
* token explosion from the full ledger. Program-built, not model-built.
|
|
6
|
+
*/
|
|
7
|
+
import type { ArenaLedger } from "./ledger.js";
|
|
8
|
+
import type { RoundResearchDigest } from "./types.js";
|
|
9
|
+
interface DigestOptions {
|
|
10
|
+
/** Current round number */
|
|
11
|
+
round: number;
|
|
12
|
+
/** Claim IDs relevant to this round (e.g. contested claims in debate) */
|
|
13
|
+
relevantClaimIds: string[];
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Build a digest from the shared ledger for a specific round.
|
|
17
|
+
* Only includes data relevant to the specified claims.
|
|
18
|
+
*/
|
|
19
|
+
export declare function buildDigest(ledger: ArenaLedger, options: DigestOptions): RoundResearchDigest;
|
|
20
|
+
/**
|
|
21
|
+
* Format a digest into a text block suitable for prompt injection.
|
|
22
|
+
* All LLM-originated text is sanitized before inclusion.
|
|
23
|
+
*/
|
|
24
|
+
export declare function formatDigest(digest: RoundResearchDigest): string;
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DigestBuilder — constructs RoundResearchDigest from the shared ledger.
|
|
3
|
+
*
|
|
4
|
+
* The digest is a filtered view injected into prompts to avoid
|
|
5
|
+
* token explosion from the full ledger. Program-built, not model-built.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Build a digest from the shared ledger for a specific round.
|
|
9
|
+
* Only includes data relevant to the specified claims.
|
|
10
|
+
*/
|
|
11
|
+
export function buildDigest(ledger, options) {
|
|
12
|
+
const { round, relevantClaimIds } = options;
|
|
13
|
+
const claimIdSet = new Set(relevantClaimIds);
|
|
14
|
+
const snapshot = ledger.getSnapshot();
|
|
15
|
+
// Collect evidence packets referenced by relevant claims
|
|
16
|
+
const packetIds = new Set();
|
|
17
|
+
for (const claimId of relevantClaimIds) {
|
|
18
|
+
const claim = ledger.getClaimById(claimId);
|
|
19
|
+
if (claim) {
|
|
20
|
+
for (const pid of claim.evidencePacketIds) {
|
|
21
|
+
packetIds.add(pid);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const evidencePackets = [];
|
|
26
|
+
for (const pid of packetIds) {
|
|
27
|
+
const packet = ledger.getPacketById(pid);
|
|
28
|
+
if (packet)
|
|
29
|
+
evidencePackets.push(packet);
|
|
30
|
+
}
|
|
31
|
+
// Tool trace summary — built directly from dossiers to preserve participant ownership
|
|
32
|
+
const toolTraceSummary = [];
|
|
33
|
+
for (const dossier of snapshot.dossiers) {
|
|
34
|
+
for (const trace of dossier.toolTrace) {
|
|
35
|
+
if (trace.keptAsEvidence) {
|
|
36
|
+
toolTraceSummary.push({
|
|
37
|
+
participant: dossier.participant,
|
|
38
|
+
toolName: trace.toolName,
|
|
39
|
+
ref: trace.resultRef,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
// Recent challenges for relevant claims
|
|
45
|
+
const recentChallenges = snapshot.challenges
|
|
46
|
+
.filter((c) => claimIdSet.has(c.claimId));
|
|
47
|
+
// Pending requested checks for relevant claims
|
|
48
|
+
const requestedChecks = snapshot.requestedChecks
|
|
49
|
+
.filter((c) => claimIdSet.has(c.claimId));
|
|
50
|
+
// Prior adjudications for relevant claims
|
|
51
|
+
const priorAdjudications = snapshot.adjudications
|
|
52
|
+
.filter((a) => claimIdSet.has(a.claimId));
|
|
53
|
+
return {
|
|
54
|
+
round,
|
|
55
|
+
relevantClaimIds,
|
|
56
|
+
evidencePackets,
|
|
57
|
+
toolTraceSummary,
|
|
58
|
+
recentChallenges,
|
|
59
|
+
requestedChecks,
|
|
60
|
+
priorAdjudications,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Sanitize text from LLM output before re-injecting into a prompt.
|
|
65
|
+
* Strips patterns that could be interpreted as prompt-level instructions.
|
|
66
|
+
*/
|
|
67
|
+
function sanitize(text) {
|
|
68
|
+
if (!text)
|
|
69
|
+
return "";
|
|
70
|
+
return text
|
|
71
|
+
// Strip fake system/assistant role tags
|
|
72
|
+
.replace(/<\/?(?:system|assistant|user|system-reminder)[^>]*>/gi, "")
|
|
73
|
+
// Strip instruction-like prefixes (case-insensitive)
|
|
74
|
+
.replace(/^(?:IGNORE|DISREGARD|FORGET|OVERRIDE|SYSTEM|INSTRUCTION)[:\s].*/gim, "")
|
|
75
|
+
// Collapse excessive whitespace
|
|
76
|
+
.replace(/\n{3,}/g, "\n\n")
|
|
77
|
+
// Limit length per field
|
|
78
|
+
.slice(0, 2000);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Format a digest into a text block suitable for prompt injection.
|
|
82
|
+
* All LLM-originated text is sanitized before inclusion.
|
|
83
|
+
*/
|
|
84
|
+
export function formatDigest(digest) {
|
|
85
|
+
const sections = [];
|
|
86
|
+
sections.push(`## Round ${digest.round} Research Digest`);
|
|
87
|
+
sections.push(`Claims under consideration: ${digest.relevantClaimIds.join(", ")}`);
|
|
88
|
+
if (digest.evidencePackets.length > 0) {
|
|
89
|
+
sections.push("\n### Evidence");
|
|
90
|
+
for (const packet of digest.evidencePackets) {
|
|
91
|
+
sections.push(`- [${packet.packetId}] ${sanitize(packet.title)} (${packet.source})`);
|
|
92
|
+
sections.push(` ${sanitize(packet.summary)}`);
|
|
93
|
+
for (const excerpt of packet.excerpts.slice(0, 3)) {
|
|
94
|
+
sections.push(` > ${excerpt.ref}: ${sanitize(excerpt.snippet)}`);
|
|
95
|
+
if (excerpt.note)
|
|
96
|
+
sections.push(` Note: ${sanitize(excerpt.note)}`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
if (digest.recentChallenges.length > 0) {
|
|
101
|
+
sections.push("\n### Challenges");
|
|
102
|
+
for (const c of digest.recentChallenges) {
|
|
103
|
+
sections.push(`- [${c.reviewer}] on ${c.claimId}: ${c.verdict} — ${sanitize(c.reason)}`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (digest.requestedChecks.length > 0) {
|
|
107
|
+
sections.push("\n### Requested Checks");
|
|
108
|
+
for (const check of digest.requestedChecks) {
|
|
109
|
+
const pri = check.priority ? ` (${check.priority})` : "";
|
|
110
|
+
sections.push(`- [${check.requestId}]${pri}: ${sanitize(check.description)}`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (digest.priorAdjudications.length > 0) {
|
|
114
|
+
sections.push("\n### Prior Adjudications");
|
|
115
|
+
for (const adj of digest.priorAdjudications) {
|
|
116
|
+
sections.push(`- ${adj.claimId}: ${adj.outcome} — ${sanitize(adj.rationale)}`);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return sections.join("\n");
|
|
120
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Arena compatibility root.
|
|
3
|
+
*
|
|
4
|
+
* Product hosts should prefer /runtime. The remaining exports expose advanced
|
|
5
|
+
* algorithms and Iterate mode for existing SDK consumers.
|
|
6
|
+
*/
|
|
7
|
+
export * from "./index.runtime.js";
|
|
8
|
+
export { getStrategy, getStrategyForPlan, ReviewStrategy, DiscussionStrategy, PlanningStrategy, } from "./strategies/index.js";
|
|
9
|
+
export { planArena } from "./planner.js";
|
|
10
|
+
export { collectEvidence } from "./providers/index.js";
|
|
11
|
+
export { selectTools, hasTools } from "./tools/selector.js";
|
|
12
|
+
export { getLens, resolveLenses, buildLensPrompt, LENS_NAMES } from "./lenses/index.js";
|
|
13
|
+
export { runParticipantResearch, runParticipantResearchWithDossiers, } from "./phases/participant-research.js";
|
|
14
|
+
export type { ResearchResult } from "./phases/participant-research.js";
|
|
15
|
+
export { runCrossReview, runVerificationReview } from "./phases/cross-review.js";
|
|
16
|
+
export { runDebateRounds } from "./phases/debate-rounds.js";
|
|
17
|
+
export { runAdjudication } from "./phases/adjudication.js";
|
|
18
|
+
export { buildConsensus } from "./phases/build-consensus.js";
|
|
19
|
+
export { registerClaims, selectClaimsForReview } from "./phases/claim-registry.js";
|
|
20
|
+
export { ArenaLedger } from "./ledger.js";
|
|
21
|
+
export { buildDigest, formatDigest } from "./digest-builder.js";
|
|
22
|
+
export { transitionClaim, resolveClaimStatus, markUnderReview, applyReviewResult, markUnresolved, isTerminal, validTransitions, } from "./transitions.js";
|
|
23
|
+
export { IterativeArena } from "./iterate/index.js";
|
|
24
|
+
export { defaultConvergence as defaultIterateConvergence, diffRatio as iterateDiffRatio, codeFormat as iterateCodeFormat, documentFormat as iterateDocumentFormat, getFormat as getIterateFormat, } from "./iterate/index.js";
|
|
25
|
+
export type { AuthorRotation, CheckpointAction, CheckpointContext, CheckpointFn, ConvergenceSignal, Critique, CritiqueCategory, CritiqueEvidence, CritiqueSeverity, Draft, DraftCandidate, IterateConfig, IterateFormat, IterateProgressEvent, IterateResult, IterateSubject, Round, StoppedReason, FormatPack as IterateFormatPack, } from "./iterate/index.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Arena compatibility root.
|
|
3
|
+
*
|
|
4
|
+
* Product hosts should prefer /runtime. The remaining exports expose advanced
|
|
5
|
+
* algorithms and Iterate mode for existing SDK consumers.
|
|
6
|
+
*/
|
|
7
|
+
export * from "./index.runtime.js";
|
|
8
|
+
export { getStrategy, getStrategyForPlan, ReviewStrategy, DiscussionStrategy, PlanningStrategy, } from "./strategies/index.js";
|
|
9
|
+
export { planArena } from "./planner.js";
|
|
10
|
+
export { collectEvidence } from "./providers/index.js";
|
|
11
|
+
export { selectTools, hasTools } from "./tools/selector.js";
|
|
12
|
+
export { getLens, resolveLenses, buildLensPrompt, LENS_NAMES } from "./lenses/index.js";
|
|
13
|
+
export { runParticipantResearch, runParticipantResearchWithDossiers, } from "./phases/participant-research.js";
|
|
14
|
+
export { runCrossReview, runVerificationReview } from "./phases/cross-review.js";
|
|
15
|
+
export { runDebateRounds } from "./phases/debate-rounds.js";
|
|
16
|
+
export { runAdjudication } from "./phases/adjudication.js";
|
|
17
|
+
export { buildConsensus } from "./phases/build-consensus.js";
|
|
18
|
+
export { registerClaims, selectClaimsForReview } from "./phases/claim-registry.js";
|
|
19
|
+
export { ArenaLedger } from "./ledger.js";
|
|
20
|
+
export { buildDigest, formatDigest } from "./digest-builder.js";
|
|
21
|
+
export { transitionClaim, resolveClaimStatus, markUnderReview, applyReviewResult, markUnresolved, isTerminal, validTransitions, } from "./transitions.js";
|
|
22
|
+
export { IterativeArena } from "./iterate/index.js";
|
|
23
|
+
export { defaultConvergence as defaultIterateConvergence, diffRatio as iterateDiffRatio, codeFormat as iterateCodeFormat, documentFormat as iterateDocumentFormat, getFormat as getIterateFormat, } from "./iterate/index.js";
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stable host runtime for Arena.
|
|
3
|
+
*
|
|
4
|
+
* This entry intentionally excludes phase/strategy internals and Iterate mode.
|
|
5
|
+
*/
|
|
6
|
+
export { Arena } from "./arena.js";
|
|
7
|
+
export { arenaTool, arenaToolDef, ArenaCapabilitySettingsSchema, createArenaCapability, getArenaStatus, saveArenaSettingsByKeys, type ArenaStatus, } from "./capability.js";
|
|
8
|
+
export { MODEL_PRESETS, getMaxOutputTokens } from "./model-presets.js";
|
|
9
|
+
export type { ModelPreset } from "./model-presets.js";
|
|
10
|
+
export { formatArenaResult, printArenaResult, renderProgress, createProgressRenderer, type OutputSink, } from "./render/terminal.js";
|
|
11
|
+
export { formatArenaResultForSession } from "./render/session.js";
|
|
12
|
+
export type { ArenaConfig, ArenaResultV2, ArenaStrategy, ArenaStrategyV2, ClaimStatusSummary, ArenaParticipant, ArenaBaseContext, ArenaFinding, FindingReview, ParticipantReport, ArenaConsensus, ArenaConsensusItem, ArenaRoadmapPhase, ArenaProgressEvent, ArenaMode, FindingKind, PeerVerdict, ArenaPlan, ArenaLens, ArenaLensName, ArenaLensRef, ArenaSourceKind, ArenaSourceSpec, ArenaSubject, ArenaOutputShape, ArenaArtifact, ArenaToolPack, ArenaContextProvider, ArenaQuickFact, ToolTrace, EvidencePacket, FindingEvidenceLink, ResearchDossier, ClaimStatus, ClaimRecord, ClaimChallenge, ClaimAdjudication, RequestedCheck, DebateRound, DebateTurn, TargetedCheckTask, SharedResearchLedger, RoundResearchDigest, ArenaExecutionLimits, } from "./types.js";
|
|
13
|
+
export { isStrategyV2 } from "./types.js";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stable host runtime for Arena.
|
|
3
|
+
*
|
|
4
|
+
* This entry intentionally excludes phase/strategy internals and Iterate mode.
|
|
5
|
+
*/
|
|
6
|
+
export { Arena } from "./arena.js";
|
|
7
|
+
export { arenaTool, arenaToolDef, ArenaCapabilitySettingsSchema, createArenaCapability, getArenaStatus, saveArenaSettingsByKeys, } from "./capability.js";
|
|
8
|
+
export { MODEL_PRESETS, getMaxOutputTokens } from "./model-presets.js";
|
|
9
|
+
export { formatArenaResult, printArenaResult, renderProgress, createProgressRenderer, } from "./render/terminal.js";
|
|
10
|
+
export { formatArenaResultForSession } from "./render/session.js";
|
|
11
|
+
export { isStrategyV2 } from "./types.js";
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convergence detection — when has the draft stabilized enough to stop?
|
|
3
|
+
*/
|
|
4
|
+
import type { ConvergenceSignal, Critique, Draft } from "./types.js";
|
|
5
|
+
/**
|
|
6
|
+
* Cheap O(n) approximation of "how different is new vs old".
|
|
7
|
+
* Returns 0..1 where 0 = identical, 1 = completely different.
|
|
8
|
+
*
|
|
9
|
+
* Uses character-trigram Jaccard distance — fast, good enough for "is the
|
|
10
|
+
* author actually changing anything substantial?".
|
|
11
|
+
*/
|
|
12
|
+
export declare function diffRatio(a: string, b: string): number;
|
|
13
|
+
/**
|
|
14
|
+
* Default convergence rule:
|
|
15
|
+
* stop if blockers == 0 AND
|
|
16
|
+
* (all critiques are minor/nit/praise OR diff < 5% from last version)
|
|
17
|
+
*
|
|
18
|
+
* Round 1 never converges (need at least one revision pass).
|
|
19
|
+
*/
|
|
20
|
+
export declare function defaultConvergence(args: {
|
|
21
|
+
round: number;
|
|
22
|
+
critiques: Critique[];
|
|
23
|
+
draft: Draft;
|
|
24
|
+
previousDraft?: Draft;
|
|
25
|
+
}): ConvergenceSignal;
|