Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Algorithmic_Reparation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"outcome_type": "ACCEPTED_PLAN",
"target_module": "app/public/app.js",
"initial_cognitive_complexity_score": 8,
"hypothesis_summary": "By centralizing the redundant `JSON.parse` and error handling blocks across multiple event listeners into a single `parseMcpResponse` helper function, we will significantly reduce cognitive complexity, lower the defect remediation deficit, and enforce the DRY (Don't Repeat Yourself) constraint, ultimately strengthening the codebase's resilience against silent failures.",
"ACU_robustness_score": 0.95,
"tension_metric": {
"novelty_score": 0.45,
"grounding_score": 0.98
},
"justification_or_plan": "1. Extracted repeated `JSON.parse` parsing and error exception handling block in `app/public/app.js` into the `parseMcpResponse` function. 2. Verified all 53 unit tests pass without regressions."
}
91 changes: 21 additions & 70 deletions app/public/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@

function parseMcpResponse(result) {
if (result.isError) {
const errorContent = result.content[0].text;
let errorObj;
try {
errorObj = JSON.parse(errorContent);
} catch (e) {
throw new Error(errorContent || "Request failed");
}
throw new Error(errorObj.structured_detail?.error || errorObj.error_code || "Request failed");
}
return JSON.parse(result.content[0].text);
}

/**
* Populates an HTML list element with a series of text items.
* Clears the list before adding items. If the items array is empty or undefined,
Expand Down Expand Up @@ -84,19 +99,7 @@ const isBrowser = typeof window !== 'undefined' && typeof document !== 'undefine
name: "synthesize_symbiosis",
arguments: { human_lens: humanLens, ai_spec: aiSpec }
});

if (result.isError) {
const errorContent = result.content[0].text;
let errorObj;
try {
errorObj = JSON.parse(errorContent);
} catch (e) {
throw new Error(errorContent || "Request failed");
}
throw new Error(errorObj.structured_detail?.error || errorObj.error_code || "Request failed");
}

const data = JSON.parse(result.content[0].text);
const data = parseMcpResponse(result);

integratedFrameworkEl.textContent = data.integrated_framework;
emergentValueEl.textContent = data.emergent_value;
Expand Down Expand Up @@ -205,19 +208,7 @@ if (isBrowser) {
name: "map_semantic_relations",
arguments: { words }
});

if (result.isError) {
const errorContent = result.content[0].text;
let errorObj;
try {
errorObj = JSON.parse(errorContent);
} catch (e) {
throw new Error(errorContent || "Request failed");
}
throw new Error(errorObj.structured_detail?.error || errorObj.error_code || "Request failed");
}

const data = JSON.parse(result.content[0].text);
const data = parseMcpResponse(result);

primaryWordEl.textContent = `Primary focus: "${data.primary}" (inputs: ${data.words.join(", ")})`;

Expand Down Expand Up @@ -306,19 +297,7 @@ if (isBrowser) {
name: "mine_lexical_topology",
arguments: { domains }
});

if (result.isError) {
const errorContent = result.content[0].text;
let errorObj;
try {
errorObj = JSON.parse(errorContent);
} catch (e) {
throw new Error(errorContent || "Request failed");
}
throw new Error(errorObj.structured_detail?.error || errorObj.error_code || "Request failed");
}

const data = JSON.parse(result.content[0].text);
const data = parseMcpResponse(result);

semanticDriftEl.textContent = data.analysis_zones.semantic_drift;
connotationVectorsEl.textContent = data.analysis_zones.connotation_vectors;
Expand Down Expand Up @@ -435,15 +414,7 @@ if (isBrowser) {
name: "agentic_inversion_engine",
arguments: { human_hypothesis: humanHypothesis, ai_constraint: aiConstraint }
});

if (result.isError) {
const errorContent = result.content[0].text;
let errorObj;
try { errorObj = JSON.parse(errorContent); } catch (e) { throw new Error(errorContent || "Request failed"); }
throw new Error(errorObj.structured_detail?.error || errorObj.error_code || "Request failed");
}

const data = JSON.parse(result.content[0].text);
const data = parseMcpResponse(result);

epistemicDriftEl.textContent = String(data.epistemic_drift);
latentLeapEl.textContent = data.latent_leap;
Expand Down Expand Up @@ -484,19 +455,7 @@ if (isBrowser) {
name: "paraconsistent_synthesis",
arguments: { human_input: humanInput, ai_input: aiInput }
});

if (result.isError) {
const errorContent = result.content[0].text;
let errorObj;
try {
errorObj = JSON.parse(errorContent);
} catch (e) {
throw new Error(errorContent || "Request failed");
}
throw new Error(errorObj.structured_detail?.error || errorObj.error_code || "Request failed");
}

const data = JSON.parse(result.content[0].text);
const data = parseMcpResponse(result);

goldenScarEl.textContent = String(data.golden_scar);
superpositionPayloadEl.textContent = data.superposition_payload;
Expand Down Expand Up @@ -631,15 +590,7 @@ if (isBrowser) {
name: agent,
arguments: args
});

if (result.isError) {
const errorContent = result.content[0].text;
let errorObj;
try { errorObj = JSON.parse(errorContent); } catch (e) { throw new Error(errorContent || "Request failed"); }
throw new Error(errorObj.structured_detail?.error || errorObj.error_code || "Request failed");
}

const data = JSON.parse(result.content[0].text);
const data = parseMcpResponse(result);

const renderCard = (title, content, valueColor) => {
const card = document.createElement('div');
Expand Down
1 change: 1 addition & 0 deletions coverage/tmp/coverage-37766-1779505255025-0.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions coverage/tmp/coverage-37778-1779505260143-0.json

Large diffs are not rendered by default.

38 changes: 38 additions & 0 deletions parse_refactor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import re

with open('app/public/app.js', 'r') as f:
content = f.read()

helper = """
function parseMcpResponse(result) {
if (result.isError) {
const errorContent = result.content[0].text;
let errorObj;
try {
errorObj = JSON.parse(errorContent);
} catch (e) {
throw new Error(errorContent || "Request failed");
}
throw new Error(errorObj.structured_detail?.error || errorObj.error_code || "Request failed");
}
return JSON.parse(result.content[0].text);
}
"""

# Insert helper function at the top after imports (if any) or right before the first DOM load logic
content = helper + "\n" + content

pattern = re.compile(
r'\s*if\s*\(result\.isError\)\s*\{\s*const\s*errorContent\s*=\s*result\.content\[0\]\.text;\s*let\s*errorObj;\s*try\s*\{\s*errorObj\s*=\s*JSON\.parse\(errorContent\);\s*\}\s*catch\s*\(e\)\s*\{\s*throw\s*new\s*Error\(errorContent\s*\|\|\s*"Request\s*failed"\);\s*\}\s*throw\s*new\s*Error\(errorObj\.structured_detail\?\.error\s*\|\|\s*errorObj\.error_code\s*\|\|\s*"Request\s*failed"\);\s*\}\s*const\s*data\s*=\s*JSON\.parse\(result\.content\[0\]\.text\);',
re.MULTILINE
)

new_content = pattern.sub(
'\n const data = parseMcpResponse(result);',
content
)

with open('app/public/app.js', 'w') as f:
f.write(new_content)

print("Done")
Loading