@hackerrank/astra-cli 0.1.7 → 0.1.9
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/package.json +1 -1
- package/src/ledger.js +62 -19
- package/src/model.js +5 -2
package/package.json
CHANGED
package/src/ledger.js
CHANGED
|
@@ -15,6 +15,40 @@ function atomicWrite(file, value) {
|
|
|
15
15
|
fs.renameSync(temp, file);
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
+
function sleepSync(milliseconds) {
|
|
19
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, milliseconds);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function withLedgerLock(file, callback) {
|
|
23
|
+
const lock = `${file}.lock`;
|
|
24
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
25
|
+
const deadline = Date.now() + 30000;
|
|
26
|
+
let descriptor;
|
|
27
|
+
while (Date.now() < deadline) {
|
|
28
|
+
try {
|
|
29
|
+
descriptor = fs.openSync(lock, "wx");
|
|
30
|
+
break;
|
|
31
|
+
} catch (error) {
|
|
32
|
+
if (error?.code !== "EEXIST") throw error;
|
|
33
|
+
sleepSync(20);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (descriptor === undefined) throw new Error(`timed out waiting for benchmark ledger lock: ${file}`);
|
|
37
|
+
try {
|
|
38
|
+
return callback();
|
|
39
|
+
} finally {
|
|
40
|
+
try { fs.closeSync(descriptor); } catch {}
|
|
41
|
+
try { fs.rmSync(lock, { force: true }); } catch {}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function readLatest(ledger) {
|
|
46
|
+
if (!fs.existsSync(ledger.path)) return { version: LEDGER_VERSION, cells: {} };
|
|
47
|
+
const value = JSON.parse(fs.readFileSync(ledger.path, "utf8"));
|
|
48
|
+
if (value.version !== LEDGER_VERSION || !value.cells || typeof value.cells !== "object") throw new Error("invalid benchmark ledger");
|
|
49
|
+
return value;
|
|
50
|
+
}
|
|
51
|
+
|
|
18
52
|
export function loadLedger(file) {
|
|
19
53
|
if (!fs.existsSync(file)) return { version: LEDGER_VERSION, cells: {}, path: file };
|
|
20
54
|
const value = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
@@ -30,28 +64,37 @@ function nextRunPath(ledger, cell) {
|
|
|
30
64
|
}
|
|
31
65
|
|
|
32
66
|
export function claimCell(ledger, cell) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
67
|
+
return withLedgerLock(ledger.path, () => {
|
|
68
|
+
const latest = readLatest(ledger);
|
|
69
|
+
ledger.cells = latest.cells;
|
|
70
|
+
const existing = ledger.cells[cell.cellKey];
|
|
71
|
+
if (existing) return existing;
|
|
72
|
+
const record = {
|
|
73
|
+
...cell,
|
|
74
|
+
status: "pending",
|
|
75
|
+
runPath: nextRunPath(ledger, cell),
|
|
76
|
+
createdAt: new Date().toISOString(),
|
|
77
|
+
leaseAt: null,
|
|
78
|
+
};
|
|
79
|
+
ledger.cells[cell.cellKey] = record;
|
|
80
|
+
atomicWrite(ledger.path, { version: LEDGER_VERSION, cells: ledger.cells });
|
|
81
|
+
return record;
|
|
82
|
+
});
|
|
45
83
|
}
|
|
46
84
|
|
|
47
85
|
export function transitionCell(ledger, cellKey, status, patch = {}) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
86
|
+
return withLedgerLock(ledger.path, () => {
|
|
87
|
+
const localCell = ledger.cells[cellKey];
|
|
88
|
+
const latest = readLatest(ledger);
|
|
89
|
+
ledger.cells = latest.cells;
|
|
90
|
+
const cell = ledger.cells[cellKey] || localCell;
|
|
91
|
+
if (!cell) throw new Error(`unknown benchmark cell: ${cellKey}`);
|
|
92
|
+
const next = { ...cell, ...patch, status, updatedAt: new Date().toISOString() };
|
|
93
|
+
if (["generating", "verifying"].includes(status) && patch.leaseAt === undefined) next.leaseAt = next.updatedAt;
|
|
94
|
+
ledger.cells[cellKey] = next;
|
|
95
|
+
atomicWrite(ledger.path, { version: LEDGER_VERSION, cells: ledger.cells });
|
|
96
|
+
return next;
|
|
97
|
+
});
|
|
55
98
|
}
|
|
56
99
|
|
|
57
100
|
export function selectWork(ledger, desiredCells, now = Date.now(), { force = false, sessionId = null } = {}) {
|
package/src/model.js
CHANGED
|
@@ -42,7 +42,7 @@ export class GatewayModel {
|
|
|
42
42
|
* @param {number} [opts.maxRetries]
|
|
43
43
|
* @param {(info:object)=>void} [opts.onRetry] called before each retry sleep
|
|
44
44
|
*/
|
|
45
|
-
constructor({ model, baseUrl, apiKey, modelKwargs = {}, maxRetries = 5, maxTokens = 8192, onRetry } = {}) {
|
|
45
|
+
constructor({ model, baseUrl, apiKey, modelKwargs = {}, maxRetries = 5, maxTokens = 8192, requestTimeoutMs = 300000, onRetry } = {}) {
|
|
46
46
|
if (!model) throw new Error("GatewayModel: `model` is required");
|
|
47
47
|
this.model = model;
|
|
48
48
|
this.maxTokens = maxTokens;
|
|
@@ -50,6 +50,7 @@ export class GatewayModel {
|
|
|
50
50
|
this.apiKey = apiKey || process.env.ASTRA_GATEWAY_API_KEY || "";
|
|
51
51
|
this.modelKwargs = modelKwargs;
|
|
52
52
|
this.maxRetries = maxRetries;
|
|
53
|
+
this.requestTimeoutMs = requestTimeoutMs;
|
|
53
54
|
this.onRetry = onRetry || (() => {});
|
|
54
55
|
this.nCalls = 0;
|
|
55
56
|
// Cumulative token usage across all calls (exact, from the API).
|
|
@@ -96,6 +97,8 @@ export class GatewayModel {
|
|
|
96
97
|
let lastErr;
|
|
97
98
|
for (let attempt = 0; attempt <= this.maxRetries; attempt++) {
|
|
98
99
|
try {
|
|
100
|
+
const timeoutSignal = AbortSignal.timeout(this.requestTimeoutMs);
|
|
101
|
+
const signal = this.signal ? AbortSignal.any([this.signal, timeoutSignal]) : timeoutSignal;
|
|
99
102
|
const res = await fetch(`${this.baseUrl}/chat/completions`, {
|
|
100
103
|
method: "POST",
|
|
101
104
|
headers: {
|
|
@@ -103,7 +106,7 @@ export class GatewayModel {
|
|
|
103
106
|
Authorization: `Bearer ${this.apiKey}`,
|
|
104
107
|
},
|
|
105
108
|
body: JSON.stringify(body),
|
|
106
|
-
signal
|
|
109
|
+
signal,
|
|
107
110
|
});
|
|
108
111
|
|
|
109
112
|
if (!res.ok) {
|