@juspay/neurolink 9.52.0 → 9.53.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/CHANGELOG.md +6 -0
- package/README.md +19 -0
- package/dist/agent/directTools.d.ts +2 -2
- package/dist/auth/errors.d.ts +1 -1
- package/dist/auth/middleware/AuthMiddleware.d.ts +1 -1
- package/dist/auth/providers/BaseAuthProvider.d.ts +1 -1
- package/dist/autoresearch/config.d.ts +11 -0
- package/dist/autoresearch/config.js +108 -0
- package/dist/autoresearch/errors.d.ts +40 -0
- package/dist/autoresearch/errors.js +20 -0
- package/dist/autoresearch/index.d.ts +23 -0
- package/dist/autoresearch/index.js +34 -0
- package/dist/autoresearch/phasePolicy.d.ts +9 -0
- package/dist/autoresearch/phasePolicy.js +69 -0
- package/dist/autoresearch/promptCompiler.d.ts +15 -0
- package/dist/autoresearch/promptCompiler.js +120 -0
- package/dist/autoresearch/repoPolicy.d.ts +32 -0
- package/dist/autoresearch/repoPolicy.js +128 -0
- package/dist/autoresearch/resultRecorder.d.ts +20 -0
- package/dist/autoresearch/resultRecorder.js +130 -0
- package/dist/autoresearch/runner.d.ts +10 -0
- package/dist/autoresearch/runner.js +102 -0
- package/dist/autoresearch/stateStore.d.ts +12 -0
- package/dist/autoresearch/stateStore.js +163 -0
- package/dist/autoresearch/summaryParser.d.ts +16 -0
- package/dist/autoresearch/summaryParser.js +94 -0
- package/dist/autoresearch/tools.d.ts +257 -0
- package/dist/autoresearch/tools.js +617 -0
- package/dist/autoresearch/worker.d.ts +71 -0
- package/dist/autoresearch/worker.js +417 -0
- package/dist/browser/neurolink.min.js +340 -326
- package/dist/cli/commands/autoresearch.d.ts +41 -0
- package/dist/cli/commands/autoresearch.js +487 -0
- package/dist/cli/commands/config.d.ts +1 -1
- package/dist/cli/commands/task.d.ts +2 -0
- package/dist/cli/commands/task.js +32 -3
- package/dist/cli/parser.js +4 -1
- package/dist/core/baseProvider.js +18 -0
- package/dist/evaluation/errors/EvaluationError.d.ts +1 -1
- package/dist/lib/agent/directTools.d.ts +2 -2
- package/dist/lib/auth/errors.d.ts +1 -1
- package/dist/lib/auth/middleware/AuthMiddleware.d.ts +1 -1
- package/dist/lib/auth/providers/BaseAuthProvider.d.ts +1 -1
- package/dist/lib/autoresearch/config.d.ts +11 -0
- package/dist/lib/autoresearch/config.js +109 -0
- package/dist/lib/autoresearch/errors.d.ts +40 -0
- package/dist/lib/autoresearch/errors.js +21 -0
- package/dist/lib/autoresearch/index.d.ts +23 -0
- package/dist/lib/autoresearch/index.js +35 -0
- package/dist/lib/autoresearch/phasePolicy.d.ts +9 -0
- package/dist/lib/autoresearch/phasePolicy.js +70 -0
- package/dist/lib/autoresearch/promptCompiler.d.ts +15 -0
- package/dist/lib/autoresearch/promptCompiler.js +121 -0
- package/dist/lib/autoresearch/repoPolicy.d.ts +32 -0
- package/dist/lib/autoresearch/repoPolicy.js +129 -0
- package/dist/lib/autoresearch/resultRecorder.d.ts +20 -0
- package/dist/lib/autoresearch/resultRecorder.js +131 -0
- package/dist/lib/autoresearch/runner.d.ts +10 -0
- package/dist/lib/autoresearch/runner.js +103 -0
- package/dist/lib/autoresearch/stateStore.d.ts +12 -0
- package/dist/lib/autoresearch/stateStore.js +164 -0
- package/dist/lib/autoresearch/summaryParser.d.ts +16 -0
- package/dist/lib/autoresearch/summaryParser.js +95 -0
- package/dist/lib/autoresearch/tools.d.ts +257 -0
- package/dist/lib/autoresearch/tools.js +618 -0
- package/dist/lib/autoresearch/worker.d.ts +71 -0
- package/dist/lib/autoresearch/worker.js +418 -0
- package/dist/lib/core/baseProvider.js +18 -0
- package/dist/lib/evaluation/errors/EvaluationError.d.ts +1 -1
- package/dist/lib/files/fileTools.d.ts +1 -1
- package/dist/lib/neurolink.js +22 -2
- package/dist/lib/providers/litellm.js +2 -2
- package/dist/lib/providers/openRouter.js +2 -2
- package/dist/lib/providers/openaiCompatible.js +3 -1
- package/dist/lib/tasks/autoresearchTaskExecutor.d.ts +32 -0
- package/dist/lib/tasks/autoresearchTaskExecutor.js +303 -0
- package/dist/lib/tasks/errors.d.ts +3 -1
- package/dist/lib/tasks/errors.js +1 -0
- package/dist/lib/tasks/taskExecutor.d.ts +4 -2
- package/dist/lib/tasks/taskExecutor.js +8 -1
- package/dist/lib/tasks/taskManager.js +27 -3
- package/dist/lib/tasks/tools/taskTools.d.ts +1 -1
- package/dist/lib/telemetry/attributes.d.ts +15 -0
- package/dist/lib/telemetry/attributes.js +16 -0
- package/dist/lib/telemetry/tracers.d.ts +1 -0
- package/dist/lib/telemetry/tracers.js +1 -0
- package/dist/lib/types/autoresearchTypes.d.ts +194 -0
- package/dist/lib/types/autoresearchTypes.js +18 -0
- package/dist/lib/types/common.d.ts +11 -0
- package/dist/lib/types/index.d.ts +16 -14
- package/dist/lib/types/index.js +21 -17
- package/dist/lib/types/taskTypes.d.ts +38 -0
- package/dist/lib/workflow/config.d.ts +3 -3
- package/dist/neurolink.js +22 -2
- package/dist/providers/litellm.js +2 -2
- package/dist/providers/openRouter.js +2 -2
- package/dist/providers/openaiCompatible.js +3 -1
- package/dist/rag/errors/RAGError.d.ts +1 -1
- package/dist/tasks/autoresearchTaskExecutor.d.ts +32 -0
- package/dist/tasks/autoresearchTaskExecutor.js +302 -0
- package/dist/tasks/errors.d.ts +3 -1
- package/dist/tasks/errors.js +1 -0
- package/dist/tasks/taskExecutor.d.ts +4 -2
- package/dist/tasks/taskExecutor.js +8 -1
- package/dist/tasks/taskManager.js +27 -3
- package/dist/tasks/tools/taskTools.d.ts +1 -1
- package/dist/telemetry/attributes.d.ts +15 -0
- package/dist/telemetry/attributes.js +16 -0
- package/dist/telemetry/tracers.d.ts +1 -0
- package/dist/telemetry/tracers.js +1 -0
- package/dist/types/autoresearchTypes.d.ts +194 -0
- package/dist/types/autoresearchTypes.js +17 -0
- package/dist/types/common.d.ts +11 -0
- package/dist/types/index.d.ts +16 -14
- package/dist/types/index.js +21 -17
- package/dist/types/taskTypes.d.ts +38 -0
- package/package.json +1 -1
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Repository policy enforcement for autoresearch.
|
|
3
|
+
*
|
|
4
|
+
* Controls which files can be read/written and validates
|
|
5
|
+
* git operations against the research branch.
|
|
6
|
+
*/
|
|
7
|
+
import { execFileSync } from "node:child_process";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import { logger } from "../utils/logger.js";
|
|
10
|
+
export class RepoPolicy {
|
|
11
|
+
config;
|
|
12
|
+
resolvedMutablePaths;
|
|
13
|
+
resolvedImmutablePaths;
|
|
14
|
+
constructor(config) {
|
|
15
|
+
this.config = config;
|
|
16
|
+
this.resolvedMutablePaths = config.mutablePaths.map((p) => path.resolve(config.repoPath, p));
|
|
17
|
+
this.resolvedImmutablePaths = config.immutablePaths.map((p) => path.resolve(config.repoPath, p));
|
|
18
|
+
}
|
|
19
|
+
/** Returns true if resolved path is inside repoPath (handles prefix collision) */
|
|
20
|
+
isInsideRepo(resolved) {
|
|
21
|
+
const rel = path.relative(this.config.repoPath, resolved);
|
|
22
|
+
return !rel.startsWith("..") && !path.isAbsolute(rel);
|
|
23
|
+
}
|
|
24
|
+
/** Returns true if path is within mutablePaths and NOT in immutablePaths */
|
|
25
|
+
isWriteAllowed(filePath) {
|
|
26
|
+
const resolved = path.resolve(this.config.repoPath, filePath);
|
|
27
|
+
// Must be inside repoPath
|
|
28
|
+
if (!this.isInsideRepo(resolved)) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
// Immutable paths always deny, even if they're children of a mutable parent
|
|
32
|
+
if (this.isProtected(filePath)) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
return this.resolvedMutablePaths.some((mp) => resolved === mp || resolved.startsWith(mp + path.sep));
|
|
36
|
+
}
|
|
37
|
+
/** Returns true if path is in immutablePaths */
|
|
38
|
+
isProtected(filePath) {
|
|
39
|
+
const resolved = path.resolve(this.config.repoPath, filePath);
|
|
40
|
+
return this.resolvedImmutablePaths.some((ip) => resolved === ip || resolved.startsWith(ip + path.sep));
|
|
41
|
+
}
|
|
42
|
+
/** Returns true if path is readable (mutable, immutable, or program path) */
|
|
43
|
+
isReadAllowed(filePath) {
|
|
44
|
+
const resolved = path.resolve(this.config.repoPath, filePath);
|
|
45
|
+
if (!this.isInsideRepo(resolved)) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
const programResolved = path.resolve(this.config.repoPath, this.config.programPath);
|
|
49
|
+
return (this.isWriteAllowed(filePath) ||
|
|
50
|
+
this.isProtected(filePath) ||
|
|
51
|
+
resolved === programResolved);
|
|
52
|
+
}
|
|
53
|
+
/** Validates staged files are all in mutablePaths and on the right branch */
|
|
54
|
+
async validateCommit(expectedBranch) {
|
|
55
|
+
const violations = [];
|
|
56
|
+
const staged = await this.getStagedFiles();
|
|
57
|
+
for (const file of staged) {
|
|
58
|
+
const resolved = path.resolve(this.config.repoPath, file);
|
|
59
|
+
// Block results files
|
|
60
|
+
if (file === this.config.resultsPath) {
|
|
61
|
+
violations.push(`Results file staged: ${file}`);
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
// Block state files
|
|
65
|
+
const stateDir = path.dirname(this.config.statePath);
|
|
66
|
+
if (file === this.config.statePath ||
|
|
67
|
+
(stateDir !== "." && file.startsWith(stateDir + path.sep))) {
|
|
68
|
+
violations.push(`State file staged: ${file}`);
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
// Block immutable files (even if under a mutable parent)
|
|
72
|
+
if (this.resolvedImmutablePaths.some((ip) => resolved === ip || resolved.startsWith(ip + path.sep))) {
|
|
73
|
+
violations.push(`Immutable file staged: ${file}`);
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
// Block non-mutable files
|
|
77
|
+
if (!this.resolvedMutablePaths.some((mp) => resolved === mp || resolved.startsWith(mp + path.sep))) {
|
|
78
|
+
violations.push(`Non-mutable file staged: ${file}`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
// Verify branch
|
|
82
|
+
const currentBranch = this.getCurrentBranch();
|
|
83
|
+
if (currentBranch !== expectedBranch) {
|
|
84
|
+
violations.push(`Wrong branch: expected ${expectedBranch}, got ${currentBranch}`);
|
|
85
|
+
}
|
|
86
|
+
return { valid: violations.length === 0, violations };
|
|
87
|
+
}
|
|
88
|
+
/** Returns list of staged file paths. Throws on git failure. */
|
|
89
|
+
async getStagedFiles() {
|
|
90
|
+
const output = execFileSync("git", ["diff", "--cached", "--name-only"], {
|
|
91
|
+
cwd: this.config.repoPath,
|
|
92
|
+
encoding: "utf-8",
|
|
93
|
+
});
|
|
94
|
+
return output.trim().split("\n").filter(Boolean);
|
|
95
|
+
}
|
|
96
|
+
/** Returns current git branch */
|
|
97
|
+
getCurrentBranch() {
|
|
98
|
+
try {
|
|
99
|
+
return execFileSync("git", ["rev-parse", "--abbrev-ref", "HEAD"], {
|
|
100
|
+
cwd: this.config.repoPath,
|
|
101
|
+
encoding: "utf-8",
|
|
102
|
+
}).trim();
|
|
103
|
+
}
|
|
104
|
+
catch (err) {
|
|
105
|
+
logger.warn("[Autoresearch] getCurrentBranch failed", {
|
|
106
|
+
repoPath: this.config.repoPath,
|
|
107
|
+
error: err instanceof Error ? err.message : String(err),
|
|
108
|
+
});
|
|
109
|
+
return "";
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/** Returns short commit hash */
|
|
113
|
+
getHeadCommit() {
|
|
114
|
+
try {
|
|
115
|
+
return execFileSync("git", ["rev-parse", "--short=7", "HEAD"], {
|
|
116
|
+
cwd: this.config.repoPath,
|
|
117
|
+
encoding: "utf-8",
|
|
118
|
+
}).trim();
|
|
119
|
+
}
|
|
120
|
+
catch (err) {
|
|
121
|
+
logger.warn("[Autoresearch] getHeadCommit failed", {
|
|
122
|
+
repoPath: this.config.repoPath,
|
|
123
|
+
error: err instanceof Error ? err.message : String(err),
|
|
124
|
+
});
|
|
125
|
+
return "";
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
//# sourceMappingURL=repoPolicy.js.map
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Experiment result recording — TSV + optional JSONL.
|
|
3
|
+
*/
|
|
4
|
+
import type { ExperimentRecord, ExperimentStats, ResearchConfig } from "../types/autoresearchTypes.js";
|
|
5
|
+
export declare class ResultRecorder {
|
|
6
|
+
private config;
|
|
7
|
+
private tsvPath;
|
|
8
|
+
private jsonlPath;
|
|
9
|
+
constructor(config: ResearchConfig);
|
|
10
|
+
/** Creates results.tsv with header if it doesn't exist */
|
|
11
|
+
ensureResultsFile(): Promise<void>;
|
|
12
|
+
/** Appends one TSV row to results.tsv */
|
|
13
|
+
appendTsv(record: ExperimentRecord): Promise<void>;
|
|
14
|
+
/** Appends one JSON line to runs.jsonl */
|
|
15
|
+
appendJsonl(record: ExperimentRecord): Promise<void>;
|
|
16
|
+
/** Reads all records from results.tsv */
|
|
17
|
+
readAll(): Promise<ExperimentRecord[]>;
|
|
18
|
+
/** Returns summary stats */
|
|
19
|
+
getStats(): Promise<ExperimentStats>;
|
|
20
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Experiment result recording — TSV + optional JSONL.
|
|
3
|
+
*/
|
|
4
|
+
import { appendFileSync, existsSync, mkdirSync, readFileSync, writeFileSync, } from "node:fs";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { logger } from "../utils/logger.js";
|
|
7
|
+
import { AutoresearchError } from "./errors.js";
|
|
8
|
+
export class ResultRecorder {
|
|
9
|
+
config;
|
|
10
|
+
tsvPath;
|
|
11
|
+
jsonlPath;
|
|
12
|
+
constructor(config) {
|
|
13
|
+
this.config = config;
|
|
14
|
+
this.tsvPath = path.join(config.repoPath, config.resultsPath);
|
|
15
|
+
this.jsonlPath = path.join(config.repoPath, ".autoresearch", "runs.jsonl");
|
|
16
|
+
}
|
|
17
|
+
/** Creates results.tsv with header if it doesn't exist */
|
|
18
|
+
async ensureResultsFile() {
|
|
19
|
+
if (!existsSync(this.tsvPath)) {
|
|
20
|
+
try {
|
|
21
|
+
// Ensure parent directory exists (handles custom resultsPath like "artifacts/results.tsv")
|
|
22
|
+
const dir = path.dirname(this.tsvPath);
|
|
23
|
+
if (!existsSync(dir)) {
|
|
24
|
+
mkdirSync(dir, { recursive: true });
|
|
25
|
+
}
|
|
26
|
+
// Use the actual metric name in header
|
|
27
|
+
const header = `commit\t${this.config.metric.name}\tmemory_gb\tstatus\tdescription`;
|
|
28
|
+
writeFileSync(this.tsvPath, header + "\n", "utf-8");
|
|
29
|
+
logger.info("[Autoresearch] Created results file", {
|
|
30
|
+
path: this.tsvPath,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
throw AutoresearchError.create("RESULTS_WRITE_FAILED", `Failed to create results file: ${this.tsvPath}`, {
|
|
35
|
+
cause: error instanceof Error ? error : undefined,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/** Appends one TSV row to results.tsv */
|
|
41
|
+
async appendTsv(record) {
|
|
42
|
+
await this.ensureResultsFile();
|
|
43
|
+
const metricStr = record.metric !== null ? record.metric.toFixed(6) : "N/A";
|
|
44
|
+
const memoryStr = record.memoryGb !== null ? record.memoryGb.toFixed(1) : "N/A";
|
|
45
|
+
const safeDescription = record.description.replace(/[\t\n\r]/g, " ").trim();
|
|
46
|
+
const line = `${record.commit}\t${metricStr}\t${memoryStr}\t${record.status}\t${safeDescription}`;
|
|
47
|
+
try {
|
|
48
|
+
appendFileSync(this.tsvPath, line + "\n", "utf-8");
|
|
49
|
+
logger.debug("[Autoresearch] Appended TSV record", {
|
|
50
|
+
commit: record.commit,
|
|
51
|
+
status: record.status,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
throw AutoresearchError.create("RESULTS_WRITE_FAILED", `Failed to append to results file`, {
|
|
56
|
+
cause: error instanceof Error ? error : undefined,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/** Appends one JSON line to runs.jsonl */
|
|
61
|
+
async appendJsonl(record) {
|
|
62
|
+
try {
|
|
63
|
+
const dir = path.dirname(this.jsonlPath);
|
|
64
|
+
if (!existsSync(dir)) {
|
|
65
|
+
mkdirSync(dir, { recursive: true });
|
|
66
|
+
}
|
|
67
|
+
appendFileSync(this.jsonlPath, JSON.stringify(record) + "\n", "utf-8");
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
// JSONL is optional — log warning but don't throw
|
|
71
|
+
logger.warn("[Autoresearch] Failed to append JSONL audit entry", {
|
|
72
|
+
error: error instanceof Error ? error.message : String(error),
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/** Reads all records from results.tsv */
|
|
77
|
+
async readAll() {
|
|
78
|
+
if (!existsSync(this.tsvPath)) {
|
|
79
|
+
return [];
|
|
80
|
+
}
|
|
81
|
+
try {
|
|
82
|
+
const content = readFileSync(this.tsvPath, "utf-8");
|
|
83
|
+
const lines = content.trim().split("\n");
|
|
84
|
+
if (lines.length <= 1) {
|
|
85
|
+
return [];
|
|
86
|
+
} // Header only
|
|
87
|
+
return lines.slice(1).map((line) => {
|
|
88
|
+
const [commit, metricStr, memoryStr, status, ...descParts] = line.split("\t");
|
|
89
|
+
return {
|
|
90
|
+
commit: commit || "",
|
|
91
|
+
metric: metricStr && metricStr !== "N/A" ? parseFloat(metricStr) : null,
|
|
92
|
+
memoryGb: memoryStr && memoryStr !== "N/A" ? parseFloat(memoryStr) : null,
|
|
93
|
+
status: (status || "crash"),
|
|
94
|
+
description: descParts.join("\t"),
|
|
95
|
+
timestamp: new Date().toISOString(), // Not stored in TSV, use current
|
|
96
|
+
};
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
return [];
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/** Returns summary stats */
|
|
104
|
+
async getStats() {
|
|
105
|
+
const records = await this.readAll();
|
|
106
|
+
const keeps = records.filter((r) => r.status === "keep");
|
|
107
|
+
const bestKeep = keeps.reduce((best, r) => {
|
|
108
|
+
if (r.metric === null) {
|
|
109
|
+
return best;
|
|
110
|
+
}
|
|
111
|
+
if (best === null || best.metric === null) {
|
|
112
|
+
return r;
|
|
113
|
+
}
|
|
114
|
+
if (this.config.metric.direction === "lower") {
|
|
115
|
+
return r.metric < best.metric ? r : best;
|
|
116
|
+
}
|
|
117
|
+
return r.metric > best.metric ? r : best;
|
|
118
|
+
}, null);
|
|
119
|
+
return {
|
|
120
|
+
total: records.length,
|
|
121
|
+
keepCount: keeps.length,
|
|
122
|
+
discardCount: records.filter((r) => r.status === "discard").length,
|
|
123
|
+
crashCount: records.filter((r) => r.status === "crash").length,
|
|
124
|
+
timeoutCount: records.filter((r) => r.status === "timeout").length,
|
|
125
|
+
keepRate: records.length > 0 ? keeps.length / records.length : 0,
|
|
126
|
+
bestMetric: bestKeep?.metric ?? null,
|
|
127
|
+
bestCommit: bestKeep?.commit ?? null,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
//# sourceMappingURL=resultRecorder.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Experiment runner — spawn, timeout, capture.
|
|
3
|
+
*/
|
|
4
|
+
import type { ExperimentSummary, ResearchConfig } from "../types/autoresearchTypes.js";
|
|
5
|
+
export declare class ExperimentRunner {
|
|
6
|
+
private config;
|
|
7
|
+
constructor(config: ResearchConfig);
|
|
8
|
+
/** Runs the experiment with hard timeout, returns summary */
|
|
9
|
+
run(): Promise<ExperimentSummary>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Experiment runner — spawn, timeout, capture.
|
|
3
|
+
*/
|
|
4
|
+
import { spawn } from "node:child_process";
|
|
5
|
+
import { writeFileSync } from "node:fs";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { logger } from "../utils/logger.js";
|
|
8
|
+
import { parseExperimentSummary } from "./summaryParser.js";
|
|
9
|
+
export class ExperimentRunner {
|
|
10
|
+
config;
|
|
11
|
+
constructor(config) {
|
|
12
|
+
this.config = config;
|
|
13
|
+
}
|
|
14
|
+
/** Runs the experiment with hard timeout, returns summary */
|
|
15
|
+
async run() {
|
|
16
|
+
const logPath = path.join(this.config.repoPath, this.config.logPath);
|
|
17
|
+
// Redact potential inline env vars or tokens before logging
|
|
18
|
+
const redactedCmd = this.config.runCommand
|
|
19
|
+
.replace(/[A-Z_]+=\S+\s/g, (m) => m.split("=")[0] + "=*** ")
|
|
20
|
+
.replace(/--(?:token|key|secret|password)\s+\S+/gi, (m) => m.split(/\s+/)[0] + " ***");
|
|
21
|
+
logger.info("[Autoresearch] Starting experiment", {
|
|
22
|
+
command: redactedCmd,
|
|
23
|
+
timeoutMs: this.config.timeoutMs,
|
|
24
|
+
});
|
|
25
|
+
// eslint-disable-next-line no-useless-assignment -- catch block assigns on spawn failure
|
|
26
|
+
let logContent = "";
|
|
27
|
+
let timedOut = false;
|
|
28
|
+
let exitCode = 0;
|
|
29
|
+
try {
|
|
30
|
+
logContent = await new Promise((resolve, reject) => {
|
|
31
|
+
let output = "";
|
|
32
|
+
const proc = spawn(this.config.runCommand, {
|
|
33
|
+
shell: true,
|
|
34
|
+
cwd: this.config.repoPath,
|
|
35
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
36
|
+
});
|
|
37
|
+
// Capture stdout and stderr
|
|
38
|
+
proc.stdout?.on("data", (chunk) => {
|
|
39
|
+
output += chunk.toString();
|
|
40
|
+
});
|
|
41
|
+
proc.stderr?.on("data", (chunk) => {
|
|
42
|
+
output += chunk.toString();
|
|
43
|
+
});
|
|
44
|
+
// Hard timeout
|
|
45
|
+
const timer = setTimeout(() => {
|
|
46
|
+
timedOut = true;
|
|
47
|
+
try {
|
|
48
|
+
proc.kill("SIGKILL");
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
// Process may have already exited
|
|
52
|
+
}
|
|
53
|
+
}, this.config.timeoutMs);
|
|
54
|
+
proc.on("close", (code, signal) => {
|
|
55
|
+
clearTimeout(timer);
|
|
56
|
+
// Signal-terminated (SIGKILL, SIGSEGV, etc.) = crash unless we timed it out
|
|
57
|
+
if (signal && !timedOut) {
|
|
58
|
+
exitCode = 1;
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
exitCode = code ?? 0;
|
|
62
|
+
}
|
|
63
|
+
logger.debug("[Autoresearch] Experiment process exited", {
|
|
64
|
+
code,
|
|
65
|
+
signal,
|
|
66
|
+
exitCode,
|
|
67
|
+
timedOut,
|
|
68
|
+
});
|
|
69
|
+
resolve(output);
|
|
70
|
+
});
|
|
71
|
+
proc.on("error", (error) => {
|
|
72
|
+
clearTimeout(timer);
|
|
73
|
+
reject(error);
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
// Spawn failure — treat as crash with non-zero exit code
|
|
79
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
80
|
+
logContent = `SPAWN ERROR: ${errorMsg}\nFAIL`;
|
|
81
|
+
exitCode = 1;
|
|
82
|
+
logger.error("[Autoresearch] Experiment spawn failed", {
|
|
83
|
+
error: errorMsg,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
// Write log to file
|
|
87
|
+
try {
|
|
88
|
+
writeFileSync(logPath, logContent, "utf-8");
|
|
89
|
+
}
|
|
90
|
+
catch (writeError) {
|
|
91
|
+
logger.warn("[Autoresearch] Failed to write run.log", {
|
|
92
|
+
error: writeError instanceof Error ? writeError.message : String(writeError),
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
// Parse summary with exit code and timeout info
|
|
96
|
+
const summary = parseExperimentSummary(logContent, this.config.metric, this.config.memoryMetric, {
|
|
97
|
+
timedOut,
|
|
98
|
+
exitCode,
|
|
99
|
+
});
|
|
100
|
+
return summary;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
//# sourceMappingURL=runner.js.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Research state persistence — file-backed JSON store.
|
|
3
|
+
*/
|
|
4
|
+
import type { ResearchState } from "../types/autoresearchTypes.js";
|
|
5
|
+
export declare class ResearchStateStore {
|
|
6
|
+
private filePath;
|
|
7
|
+
constructor(repoPath: string, statePath: string);
|
|
8
|
+
load(): Promise<ResearchState | null>;
|
|
9
|
+
save(state: ResearchState): Promise<void>;
|
|
10
|
+
initialize(tag: string, branch: string): Promise<ResearchState>;
|
|
11
|
+
update(patch: Partial<ResearchState>): Promise<ResearchState>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Research state persistence — file-backed JSON store.
|
|
3
|
+
*/
|
|
4
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync, } from "node:fs";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { logger } from "../utils/logger.js";
|
|
7
|
+
import { AutoresearchError } from "./errors.js";
|
|
8
|
+
export class ResearchStateStore {
|
|
9
|
+
filePath;
|
|
10
|
+
constructor(repoPath, statePath) {
|
|
11
|
+
this.filePath = path.join(repoPath, statePath);
|
|
12
|
+
}
|
|
13
|
+
async load() {
|
|
14
|
+
if (!existsSync(this.filePath)) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
try {
|
|
18
|
+
const raw = readFileSync(this.filePath, "utf-8");
|
|
19
|
+
const parsed = JSON.parse(raw);
|
|
20
|
+
// Validate required fields
|
|
21
|
+
const requiredFields = [
|
|
22
|
+
"branch",
|
|
23
|
+
"currentPhase",
|
|
24
|
+
"runCount",
|
|
25
|
+
"keepCount",
|
|
26
|
+
"tag",
|
|
27
|
+
"startedAt",
|
|
28
|
+
"updatedAt",
|
|
29
|
+
];
|
|
30
|
+
for (const field of requiredFields) {
|
|
31
|
+
if (!(field in parsed)) {
|
|
32
|
+
throw AutoresearchError.create("STATE_CORRUPT", `State file missing required field: ${field}`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (!Number.isInteger(parsed.runCount) ||
|
|
36
|
+
parsed.runCount < 0 ||
|
|
37
|
+
!Number.isInteger(parsed.keepCount) ||
|
|
38
|
+
parsed.keepCount < 0) {
|
|
39
|
+
throw AutoresearchError.create("STATE_CORRUPT", `State file has invalid numeric fields: runCount=${parsed.runCount}, keepCount=${parsed.keepCount}`);
|
|
40
|
+
}
|
|
41
|
+
const validPhases = [
|
|
42
|
+
"bootstrap",
|
|
43
|
+
"baseline",
|
|
44
|
+
"propose",
|
|
45
|
+
"edit",
|
|
46
|
+
"commit",
|
|
47
|
+
"run",
|
|
48
|
+
"evaluate",
|
|
49
|
+
"record",
|
|
50
|
+
"accept_or_revert",
|
|
51
|
+
];
|
|
52
|
+
if (!validPhases.includes(parsed.currentPhase)) {
|
|
53
|
+
throw AutoresearchError.create("STATE_CORRUPT", `State file has invalid currentPhase: ${parsed.currentPhase}`);
|
|
54
|
+
}
|
|
55
|
+
if (isNaN(Date.parse(parsed.startedAt)) ||
|
|
56
|
+
isNaN(Date.parse(parsed.updatedAt))) {
|
|
57
|
+
throw AutoresearchError.create("STATE_CORRUPT", "State file has invalid timestamp fields");
|
|
58
|
+
}
|
|
59
|
+
return parsed;
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
// Don't double-wrap our own validation errors
|
|
63
|
+
if (error instanceof Error && error.message.includes("AUTORESEARCH")) {
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
throw AutoresearchError.create("STATE_CORRUPT", `Failed to parse state file: ${this.filePath}`, {
|
|
67
|
+
cause: error instanceof Error ? error : undefined,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
async save(state) {
|
|
72
|
+
const dir = path.dirname(this.filePath);
|
|
73
|
+
if (!existsSync(dir)) {
|
|
74
|
+
mkdirSync(dir, { recursive: true });
|
|
75
|
+
}
|
|
76
|
+
// Strip rawTail from lastSummary before persisting (it's arbitrary stdout/stderr,
|
|
77
|
+
// kept in run.log; storing it in state.json risks leaking sensitive output)
|
|
78
|
+
const stateToPersist = { ...state };
|
|
79
|
+
if (stateToPersist.lastSummary?.rawTail) {
|
|
80
|
+
stateToPersist.lastSummary = {
|
|
81
|
+
...stateToPersist.lastSummary,
|
|
82
|
+
rawTail: "[see run.log]",
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
// Atomic write: write to temp file, then rename
|
|
86
|
+
const tmpPath = `${this.filePath}.tmp`;
|
|
87
|
+
try {
|
|
88
|
+
writeFileSync(tmpPath, JSON.stringify(stateToPersist, null, 2), "utf-8");
|
|
89
|
+
renameSync(tmpPath, this.filePath);
|
|
90
|
+
logger.debug("[Autoresearch] State saved", {
|
|
91
|
+
phase: state.currentPhase,
|
|
92
|
+
runCount: state.runCount,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
throw AutoresearchError.create("STATE_CORRUPT", `Failed to write state file: ${this.filePath}`, {
|
|
97
|
+
cause: error instanceof Error ? error : undefined,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
async initialize(tag, branch) {
|
|
102
|
+
const now = new Date().toISOString();
|
|
103
|
+
const state = {
|
|
104
|
+
branch,
|
|
105
|
+
acceptedCommit: null,
|
|
106
|
+
baselineMetric: null,
|
|
107
|
+
bestMetric: null,
|
|
108
|
+
candidateCommit: null,
|
|
109
|
+
runCount: 0,
|
|
110
|
+
keepCount: 0,
|
|
111
|
+
lastStatus: null,
|
|
112
|
+
currentPhase: "bootstrap",
|
|
113
|
+
tag,
|
|
114
|
+
startedAt: now,
|
|
115
|
+
updatedAt: now,
|
|
116
|
+
};
|
|
117
|
+
await this.save(state);
|
|
118
|
+
logger.info("[Autoresearch] State initialized", { tag, branch });
|
|
119
|
+
return state;
|
|
120
|
+
}
|
|
121
|
+
async update(patch) {
|
|
122
|
+
const current = await this.load();
|
|
123
|
+
if (!current) {
|
|
124
|
+
throw AutoresearchError.create("STATE_NOT_FOUND", "Cannot update: no state file found");
|
|
125
|
+
}
|
|
126
|
+
// Validate patch fields before merging
|
|
127
|
+
const VALID_PHASES = [
|
|
128
|
+
"bootstrap",
|
|
129
|
+
"baseline",
|
|
130
|
+
"propose",
|
|
131
|
+
"edit",
|
|
132
|
+
"commit",
|
|
133
|
+
"run",
|
|
134
|
+
"evaluate",
|
|
135
|
+
"record",
|
|
136
|
+
"accept_or_revert",
|
|
137
|
+
];
|
|
138
|
+
if (patch.runCount !== undefined &&
|
|
139
|
+
(!Number.isInteger(patch.runCount) || patch.runCount < 0)) {
|
|
140
|
+
throw AutoresearchError.create("STATE_CORRUPT", `Invalid runCount: ${patch.runCount}`);
|
|
141
|
+
}
|
|
142
|
+
if (patch.keepCount !== undefined &&
|
|
143
|
+
(!Number.isInteger(patch.keepCount) || patch.keepCount < 0)) {
|
|
144
|
+
throw AutoresearchError.create("STATE_CORRUPT", `Invalid keepCount: ${patch.keepCount}`);
|
|
145
|
+
}
|
|
146
|
+
if (patch.currentPhase !== undefined &&
|
|
147
|
+
!VALID_PHASES.includes(patch.currentPhase)) {
|
|
148
|
+
throw AutoresearchError.create("STATE_CORRUPT", `Invalid currentPhase: ${patch.currentPhase}`);
|
|
149
|
+
}
|
|
150
|
+
if (patch.bestMetric !== undefined &&
|
|
151
|
+
patch.bestMetric !== null &&
|
|
152
|
+
!Number.isFinite(patch.bestMetric)) {
|
|
153
|
+
throw AutoresearchError.create("STATE_CORRUPT", `Invalid bestMetric: ${patch.bestMetric}`);
|
|
154
|
+
}
|
|
155
|
+
const updated = {
|
|
156
|
+
...current,
|
|
157
|
+
...patch,
|
|
158
|
+
updatedAt: new Date().toISOString(),
|
|
159
|
+
};
|
|
160
|
+
await this.save(updated);
|
|
161
|
+
return updated;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
//# sourceMappingURL=stateStore.js.map
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deterministic experiment log parser.
|
|
3
|
+
*
|
|
4
|
+
* Extracts metrics from run.log output. Never throws — returns
|
|
5
|
+
* crashed/timedOut summary on parse failure.
|
|
6
|
+
*/
|
|
7
|
+
import type { ExperimentSummary, MetricConfig, MemoryMetricConfig } from "../types/autoresearchTypes.js";
|
|
8
|
+
/**
|
|
9
|
+
* Parse experiment output into a structured summary.
|
|
10
|
+
*
|
|
11
|
+
* This function is deterministic and pure — no side effects, no throws.
|
|
12
|
+
*/
|
|
13
|
+
export declare function parseExperimentSummary(logContent: string, metricConfig: MetricConfig, memoryConfig?: MemoryMetricConfig, options?: {
|
|
14
|
+
timedOut?: boolean;
|
|
15
|
+
exitCode?: number;
|
|
16
|
+
}): ExperimentSummary;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deterministic experiment log parser.
|
|
3
|
+
*
|
|
4
|
+
* Extracts metrics from run.log output. Never throws — returns
|
|
5
|
+
* crashed/timedOut summary on parse failure.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Parse experiment output into a structured summary.
|
|
9
|
+
*
|
|
10
|
+
* This function is deterministic and pure — no side effects, no throws.
|
|
11
|
+
*/
|
|
12
|
+
export function parseExperimentSummary(logContent, metricConfig, memoryConfig, options) {
|
|
13
|
+
const lines = logContent.split("\n");
|
|
14
|
+
const tailLines = lines.slice(-50);
|
|
15
|
+
const rawTail = tailLines.join("\n");
|
|
16
|
+
// Check for crash indicators (only if not already determined by exit code)
|
|
17
|
+
let crashed = options?.exitCode !== undefined &&
|
|
18
|
+
options.exitCode !== 0 &&
|
|
19
|
+
!options?.timedOut;
|
|
20
|
+
if (!crashed) {
|
|
21
|
+
const lastMeaningfulLine = [...lines]
|
|
22
|
+
.reverse()
|
|
23
|
+
.find((l) => l.trim().length > 0)
|
|
24
|
+
?.trim() ?? "";
|
|
25
|
+
crashed =
|
|
26
|
+
lastMeaningfulLine === "FAIL" ||
|
|
27
|
+
lastMeaningfulLine.toLowerCase().includes("traceback") ||
|
|
28
|
+
lastMeaningfulLine.toLowerCase().includes("error:");
|
|
29
|
+
}
|
|
30
|
+
// Parse primary metric
|
|
31
|
+
let metric = null;
|
|
32
|
+
try {
|
|
33
|
+
const regex = new RegExp(metricConfig.pattern, "m");
|
|
34
|
+
const match = regex.exec(logContent);
|
|
35
|
+
if (match && match[1]) {
|
|
36
|
+
const parsed = parseFloat(match[1]);
|
|
37
|
+
if (!isNaN(parsed) && isFinite(parsed)) {
|
|
38
|
+
metric = parsed;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
// Invalid regex — treat as parse failure
|
|
44
|
+
}
|
|
45
|
+
// Parse memory metric
|
|
46
|
+
let memoryValue = null;
|
|
47
|
+
if (memoryConfig) {
|
|
48
|
+
try {
|
|
49
|
+
const regex = new RegExp(memoryConfig.pattern, "m");
|
|
50
|
+
const match = regex.exec(logContent);
|
|
51
|
+
if (match && match[1]) {
|
|
52
|
+
const parsed = parseFloat(match[1]);
|
|
53
|
+
if (!isNaN(parsed) && isFinite(parsed)) {
|
|
54
|
+
memoryValue = parsed;
|
|
55
|
+
// Convert MB to GB if the name suggests MB.
|
|
56
|
+
// Uses binary divisor (1024 = MiB→GiB). For decimal MB→GB use 1000.
|
|
57
|
+
// The 1024 convention matches GPU monitoring tools (nvidia-smi, etc.).
|
|
58
|
+
const MIB_TO_GIB_DIVISOR = 1024;
|
|
59
|
+
if (memoryConfig.name.toLowerCase().includes("mb")) {
|
|
60
|
+
memoryValue =
|
|
61
|
+
Math.round((memoryValue / MIB_TO_GIB_DIVISOR) * 100) / 100;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
// Invalid regex — treat as parse failure
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
// Parse training time
|
|
71
|
+
let trainingSeconds = null;
|
|
72
|
+
try {
|
|
73
|
+
const timeRegex = /^training_seconds:\s+([\d.]+)/m;
|
|
74
|
+
const match = timeRegex.exec(logContent);
|
|
75
|
+
if (match && match[1]) {
|
|
76
|
+
trainingSeconds = parseFloat(match[1]);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
// Ignore
|
|
81
|
+
}
|
|
82
|
+
// Only treat as timeout if explicitly signaled by the runner — don't infer from missing metric
|
|
83
|
+
const timedOut = options?.timedOut ?? false;
|
|
84
|
+
// Missing metric without explicit timeout is a crash (regex failure, spawn error, etc.)
|
|
85
|
+
const finalCrashed = crashed || (metric === null && !timedOut);
|
|
86
|
+
return {
|
|
87
|
+
crashed: finalCrashed,
|
|
88
|
+
timedOut,
|
|
89
|
+
metric,
|
|
90
|
+
memoryValue,
|
|
91
|
+
trainingSeconds,
|
|
92
|
+
rawTail,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
//# sourceMappingURL=summaryParser.js.map
|