Skip to content

Commit 1b34dda

Browse files
CopilotlpcoxCopilot
authored
Extract shared positive-integer parsing for api-proxy guards (#3565)
* Initial plan * Refactor duplicate guard integer parsing * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Landon Cox <landon.cox@microsoft.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent d2cf0a4 commit 1b34dda

5 files changed

Lines changed: 48 additions & 24 deletions

File tree

containers/api-proxy/guards/effective-token-guard.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
const { parsePositiveInteger } = require('./guard-utils');
4+
35
const ET_WARNING_THRESHOLDS = [80, 90, 95, 99];
46

57
const ET_DEFAULT_WEIGHTS = Object.freeze({
@@ -33,13 +35,6 @@ const effectiveTokenConfigCache = {
3335
parsed: { max: null, multipliers: {} },
3436
};
3537

36-
function parseMaxEffectiveTokens(raw) {
37-
if (raw === undefined || raw === null || String(raw).trim() === '') return null;
38-
const parsed = Number(raw);
39-
if (!Number.isInteger(parsed) || parsed <= 0) return null;
40-
return parsed;
41-
}
42-
4338
function parseModelMultipliers(raw) {
4439
if (!raw || String(raw).trim() === '') return {};
4540
try {
@@ -69,7 +64,7 @@ function getEffectiveTokenConfig() {
6964
effectiveTokenConfigCache.rawMultipliers = rawMultipliers;
7065
const parsedMultipliers = Object.freeze(parseModelMultipliers(rawMultipliers));
7166
effectiveTokenConfigCache.parsed = {
72-
max: parseMaxEffectiveTokens(rawMax),
67+
max: parsePositiveInteger(rawMax),
7368
multipliers: parsedMultipliers,
7469
};
7570
return effectiveTokenConfigCache.parsed;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
function parsePositiveInteger(raw) {
4+
if (raw === undefined || raw === null || String(raw).trim() === '') return null;
5+
const parsed = Number(raw);
6+
if (!Number.isInteger(parsed) || parsed <= 0) return null;
7+
return parsed;
8+
}
9+
10+
module.exports = {
11+
parsePositiveInteger,
12+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const { parsePositiveInteger } = require('./guard-utils');
2+
3+
describe('guard-utils', () => {
4+
describe('parsePositiveInteger', () => {
5+
it.each([
6+
undefined,
7+
null,
8+
'',
9+
' ',
10+
0,
11+
'0',
12+
-1,
13+
'-1',
14+
'1.5',
15+
'abc',
16+
])('returns null for %p', (raw) => {
17+
expect(parsePositiveInteger(raw)).toBeNull();
18+
});
19+
20+
it.each([
21+
[1, 1],
22+
['1', 1],
23+
[' 42 ', 42],
24+
])('for raw value %p returns %p', (raw, expected) => {
25+
expect(parsePositiveInteger(raw)).toBe(expected);
26+
});
27+
});
28+
});

containers/api-proxy/guards/max-runs-guard.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
const { parsePositiveInteger } = require('./guard-utils');
4+
35
let maxRunsGuardState = {
46
configKey: null,
57
invocationCount: 0,
@@ -10,20 +12,13 @@ const maxRunsConfigCache = {
1012
parsed: null,
1113
};
1214

13-
function parseMaxRuns(raw) {
14-
if (raw === undefined || raw === null || String(raw).trim() === '') return null;
15-
const parsed = Number(raw);
16-
if (!Number.isInteger(parsed) || parsed <= 0) return null;
17-
return parsed;
18-
}
19-
2015
function getMaxRunsConfig() {
2116
const rawMax = process.env.AWF_MAX_RUNS;
2217
if (maxRunsConfigCache.rawMax === rawMax) {
2318
return maxRunsConfigCache.parsed;
2419
}
2520
maxRunsConfigCache.rawMax = rawMax;
26-
maxRunsConfigCache.parsed = parseMaxRuns(rawMax);
21+
maxRunsConfigCache.parsed = parsePositiveInteger(rawMax);
2722
return maxRunsConfigCache.parsed;
2823
}
2924

containers/api-proxy/guards/timeout-steering.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const { ET_WARNING_THRESHOLDS } = require('./effective-token-guard');
4+
const { parsePositiveInteger } = require('./guard-utils');
45

56
const TIMEOUT_STEERING_MESSAGES = {
67
80: 'You have used 80% of your allotted run time. Begin planning to wrap up your current work.',
@@ -25,20 +26,13 @@ const timeoutSteeringConfigCache = {
2526
parsedMinutes: null,
2627
};
2728

28-
function parseAgentTimeoutMinutes(raw) {
29-
if (raw === undefined || raw === null || String(raw).trim() === '') return null;
30-
const parsed = Number(raw);
31-
if (!Number.isInteger(parsed) || parsed <= 0) return null;
32-
return parsed;
33-
}
34-
3529
function getTimeoutSteeringConfig() {
3630
const rawMinutes = process.env.AWF_AGENT_TIMEOUT_MINUTES;
3731
if (timeoutSteeringConfigCache.rawMinutes === rawMinutes) {
3832
return timeoutSteeringConfigCache.parsedMinutes;
3933
}
4034
timeoutSteeringConfigCache.rawMinutes = rawMinutes;
41-
timeoutSteeringConfigCache.parsedMinutes = parseAgentTimeoutMinutes(rawMinutes);
35+
timeoutSteeringConfigCache.parsedMinutes = parsePositiveInteger(rawMinutes);
4236
return timeoutSteeringConfigCache.parsedMinutes;
4337
}
4438

0 commit comments

Comments
 (0)