Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/test/simulation/workbench/stores/amlSimulations.ts
13399 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import * as fs from 'fs';
7
import * as mobx from 'mobx';
8
import * as path from 'path';
9
import { RunOnceScheduler } from '../../../../src/util/vs/base/common/async';
10
import { Disposable, toDisposable } from '../../../../src/util/vs/base/common/lifecycle';
11
import { AML_OUTPUT_PATH, STDOUT_FILENAME } from '../../shared/sharedTypes';
12
import { REPO_ROOT } from '../utils/utils';
13
import { SimulationStorage, SimulationStorageValue } from './simulationStorage';
14
15
const AML_OUTPUT_FOLDER_PATH = path.join(REPO_ROOT, AML_OUTPUT_PATH);
16
17
export enum AMLRunKind {
18
Fix = 'fix',
19
Doc = 'doc',
20
WorkspaceE2E = 'workspace-e2e',
21
TestGen = 'test_gen',
22
MethodGen = 'method_gen',
23
EditFix = 'edit_fix',
24
Unknown = 'unknown',
25
Swebench = 'swebench',
26
FixTestFailure = 'fix_test_failure',
27
HumanEval = 'human_eval',
28
SafetyPrompt = 'safety_prompt',
29
}
30
31
function pathIfExists(filePath: string): string | undefined {
32
return fs.existsSync(filePath) ? filePath : undefined;
33
}
34
35
export class AMLRun {
36
37
public readonly scoreCardCsvPath: string | undefined;
38
public readonly scoreCardByLanguageJsonPath: string | undefined;
39
public readonly scoredPredictionsJSONL: string | undefined;
40
public readonly simulationInputPath: string;
41
42
constructor(
43
public readonly kind: AMLRunKind,
44
public readonly name: string,
45
public readonly runPath: string,
46
public readonly stat: fs.Stats,
47
public readonly stdoutPath: string,
48
) {
49
this.scoreCardCsvPath = kind !== 'unknown' ? pathIfExists(path.join(runPath, `eval/${kind}_scorecard.csv`)) : undefined;
50
this.scoreCardByLanguageJsonPath = pathIfExists(path.join(runPath, `eval/metric_scorecard_by_language.json`));
51
52
this.scoredPredictionsJSONL = kind !== 'unknown' ? pathIfExists(path.join(runPath, `eval/${kind}_scored_predictions.jsonl`)) : undefined;
53
this.simulationInputPath = path.join(runPath, 'simulate', 'simulator_input');
54
}
55
}
56
57
/**
58
* Detects possible AML runs
59
*/
60
export class AMLProvider extends Disposable {
61
62
private readonly _updateSoon = this._register(new RunOnceScheduler(() => this._update(), 50));
63
64
@mobx.observable
65
public runs: AMLRun[] = [];
66
67
@mobx.observable
68
public selectedName: SimulationStorageValue<string>;
69
70
@mobx.observable
71
public compareAgainstRunName: SimulationStorageValue<string>;
72
73
@mobx.computed
74
public get selected(): AMLRun | undefined {
75
return this.runs.find(r => r.name === this.selectedName.value);
76
}
77
78
@mobx.computed
79
public get compareAgainstRun(): AMLRun | undefined {
80
return this.runs.find(r => r.name === this.compareAgainstRunName.value);
81
}
82
83
constructor(storage: SimulationStorage) {
84
super();
85
86
this.selectedName = new SimulationStorageValue(storage, 'selectedAML', '');
87
this.compareAgainstRunName = new SimulationStorageValue(storage, 'compareAgainstAML', '');
88
89
mobx.makeObservable(this);
90
91
const listener = () => {
92
if (!this._updateSoon.isScheduled()) {
93
this._updateSoon.schedule();
94
}
95
};
96
97
fs.promises.mkdir(AML_OUTPUT_FOLDER_PATH, { recursive: true }).then(() => {
98
fs.watch(AML_OUTPUT_FOLDER_PATH, { recursive: false }, listener);
99
this._register(toDisposable(() => fs.unwatchFile(AML_OUTPUT_FOLDER_PATH, listener)));
100
this._update();
101
});
102
}
103
104
private async _update(): Promise<void> {
105
const amlOutputFolders = await fs.promises.readdir(AML_OUTPUT_FOLDER_PATH);
106
const rawEntries = await Promise.all(
107
amlOutputFolders.map(async (entry) => {
108
const entryPath = path.join(AML_OUTPUT_FOLDER_PATH, entry);
109
const stat = await fs.promises.stat(entryPath);
110
const stdoutPath = path.join(entryPath, 'simulate', 'simulator_output_dir', 'simulator_output', STDOUT_FILENAME);
111
try {
112
const stdoutStat = await fs.promises.stat(stdoutPath);
113
if (!stdoutStat.isFile()) {
114
return undefined;
115
}
116
} catch (err) {
117
// stdout does not exist
118
return undefined;
119
}
120
const kind = await AMLProvider.determineKind(entryPath);
121
return new AMLRun(kind, entry, entryPath, stat, stdoutPath);
122
})
123
);
124
125
let entries = rawEntries.filter((entry): entry is AMLRun => !!entry);
126
entries = entries.filter(({ stat }) => stat.isDirectory());
127
entries.sort((a, b) => b.stat.ctimeMs - a.stat.ctimeMs);
128
129
mobx.runInAction(() => {
130
this.runs = entries;
131
132
// if no run is selected, pre-select first output folder
133
if (this.runs.length > 0 && !this.selected) {
134
this.selectedName.value = this.runs[0].name;
135
}
136
});
137
}
138
139
/**
140
* Determines the kind (fix/doc/workspace-e2e/etc) of an AML run based on job_parameters.json.
141
* @param amlRunPath - The path to the AML run directory.
142
* @returns The kind of AML run.
143
*/
144
private static async determineKind(amlRunPath: string): Promise<AMLRunKind> {
145
146
const defaultKind = AMLRunKind.Unknown;
147
const jobParametersPath = path.join(amlRunPath, 'simulate', 'simulator_output_dir', 'simulator_output', 'job_parameters.json');
148
try {
149
const file = await fs.promises.readFile(jobParametersPath);
150
const jobParameters = JSON.parse(file.toString());
151
if (Object.values(AMLRunKind).includes(jobParameters.dataset)) {
152
return jobParameters.dataset as AMLRunKind;
153
}
154
155
console.error(`Unknown AML run kind: ${jobParameters.dataset}; considering it as 'unknown'`);
156
} catch (err) {
157
console.error(`Error determining AML run kind: Unable to read ${jobParametersPath}`);
158
}
159
return defaultKind;
160
}
161
}
162
163