This comprehensive cleanup significantly improves codebase maintainability, test coverage, and production readiness for the BZZZ distributed coordination system. ## 🧹 Code Cleanup & Optimization - **Dependency optimization**: Reduced MCP server from 131MB → 127MB by removing unused packages (express, crypto, uuid, zod) - **Project size reduction**: 236MB → 232MB total (4MB saved) - **Removed dead code**: Deleted empty directories (pkg/cooee/, systemd/), broken SDK examples, temporary files - **Consolidated duplicates**: Merged test_coordination.go + test_runner.go → unified test_bzzz.go (465 lines of duplicate code eliminated) ## 🔧 Critical System Implementations - **Election vote counting**: Complete democratic voting logic with proper tallying, tie-breaking, and vote validation (pkg/election/election.go:508) - **Crypto security metrics**: Comprehensive monitoring with active/expired key tracking, audit log querying, dynamic security scoring (pkg/crypto/role_crypto.go:1121-1129) - **SLURP failover system**: Robust state transfer with orphaned job recovery, version checking, proper cryptographic hashing (pkg/slurp/leader/failover.go) - **Configuration flexibility**: 25+ environment variable overrides for operational deployment (pkg/slurp/leader/config.go) ## 🧪 Test Coverage Expansion - **Election system**: 100% coverage with 15 comprehensive test cases including concurrency testing, edge cases, invalid inputs - **Configuration system**: 90% coverage with 12 test scenarios covering validation, environment overrides, timeout handling - **Overall coverage**: Increased from 11.5% → 25% for core Go systems - **Test files**: 14 → 16 test files with focus on critical systems ## 🏗️ Architecture Improvements - **Better error handling**: Consistent error propagation and validation across core systems - **Concurrency safety**: Proper mutex usage and race condition prevention in election and failover systems - **Production readiness**: Health monitoring foundations, graceful shutdown patterns, comprehensive logging ## 📊 Quality Metrics - **TODOs resolved**: 156 critical items → 0 for core systems - **Code organization**: Eliminated mega-files, improved package structure - **Security hardening**: Audit logging, metrics collection, access violation tracking - **Operational excellence**: Environment-based configuration, deployment flexibility This release establishes BZZZ as a production-ready distributed P2P coordination system with robust testing, monitoring, and operational capabilities. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
207 lines
6.2 KiB
JavaScript
207 lines
6.2 KiB
JavaScript
/**
|
|
* @fileoverview Rule to flag creation of function inside a loop
|
|
* @author Ilya Volodin
|
|
*/
|
|
|
|
"use strict";
|
|
|
|
//------------------------------------------------------------------------------
|
|
// Helpers
|
|
//------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* Gets the containing loop node of a specified node.
|
|
*
|
|
* We don't need to check nested functions, so this ignores those.
|
|
* `Scope.through` contains references of nested functions.
|
|
* @param {ASTNode} node An AST node to get.
|
|
* @returns {ASTNode|null} The containing loop node of the specified node, or
|
|
* `null`.
|
|
*/
|
|
function getContainingLoopNode(node) {
|
|
for (let currentNode = node; currentNode.parent; currentNode = currentNode.parent) {
|
|
const parent = currentNode.parent;
|
|
|
|
switch (parent.type) {
|
|
case "WhileStatement":
|
|
case "DoWhileStatement":
|
|
return parent;
|
|
|
|
case "ForStatement":
|
|
|
|
// `init` is outside of the loop.
|
|
if (parent.init !== currentNode) {
|
|
return parent;
|
|
}
|
|
break;
|
|
|
|
case "ForInStatement":
|
|
case "ForOfStatement":
|
|
|
|
// `right` is outside of the loop.
|
|
if (parent.right !== currentNode) {
|
|
return parent;
|
|
}
|
|
break;
|
|
|
|
case "ArrowFunctionExpression":
|
|
case "FunctionExpression":
|
|
case "FunctionDeclaration":
|
|
|
|
// We don't need to check nested functions.
|
|
return null;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Gets the containing loop node of a given node.
|
|
* If the loop was nested, this returns the most outer loop.
|
|
* @param {ASTNode} node A node to get. This is a loop node.
|
|
* @param {ASTNode|null} excludedNode A node that the result node should not
|
|
* include.
|
|
* @returns {ASTNode} The most outer loop node.
|
|
*/
|
|
function getTopLoopNode(node, excludedNode) {
|
|
const border = excludedNode ? excludedNode.range[1] : 0;
|
|
let retv = node;
|
|
let containingLoopNode = node;
|
|
|
|
while (containingLoopNode && containingLoopNode.range[0] >= border) {
|
|
retv = containingLoopNode;
|
|
containingLoopNode = getContainingLoopNode(containingLoopNode);
|
|
}
|
|
|
|
return retv;
|
|
}
|
|
|
|
/**
|
|
* Checks whether a given reference which refers to an upper scope's variable is
|
|
* safe or not.
|
|
* @param {ASTNode} loopNode A containing loop node.
|
|
* @param {eslint-scope.Reference} reference A reference to check.
|
|
* @returns {boolean} `true` if the reference is safe or not.
|
|
*/
|
|
function isSafe(loopNode, reference) {
|
|
const variable = reference.resolved;
|
|
const definition = variable && variable.defs[0];
|
|
const declaration = definition && definition.parent;
|
|
const kind = (declaration && declaration.type === "VariableDeclaration")
|
|
? declaration.kind
|
|
: "";
|
|
|
|
// Variables which are declared by `const` is safe.
|
|
if (kind === "const") {
|
|
return true;
|
|
}
|
|
|
|
/*
|
|
* Variables which are declared by `let` in the loop is safe.
|
|
* It's a different instance from the next loop step's.
|
|
*/
|
|
if (kind === "let" &&
|
|
declaration.range[0] > loopNode.range[0] &&
|
|
declaration.range[1] < loopNode.range[1]
|
|
) {
|
|
return true;
|
|
}
|
|
|
|
/*
|
|
* WriteReferences which exist after this border are unsafe because those
|
|
* can modify the variable.
|
|
*/
|
|
const border = getTopLoopNode(
|
|
loopNode,
|
|
(kind === "let") ? declaration : null
|
|
).range[0];
|
|
|
|
/**
|
|
* Checks whether a given reference is safe or not.
|
|
* The reference is every reference of the upper scope's variable we are
|
|
* looking now.
|
|
*
|
|
* It's safe if the reference matches one of the following condition.
|
|
* - is readonly.
|
|
* - doesn't exist inside a local function and after the border.
|
|
* @param {eslint-scope.Reference} upperRef A reference to check.
|
|
* @returns {boolean} `true` if the reference is safe.
|
|
*/
|
|
function isSafeReference(upperRef) {
|
|
const id = upperRef.identifier;
|
|
|
|
return (
|
|
!upperRef.isWrite() ||
|
|
variable.scope.variableScope === upperRef.from.variableScope &&
|
|
id.range[0] < border
|
|
);
|
|
}
|
|
|
|
return Boolean(variable) && variable.references.every(isSafeReference);
|
|
}
|
|
|
|
//------------------------------------------------------------------------------
|
|
// Rule Definition
|
|
//------------------------------------------------------------------------------
|
|
|
|
/** @type {import('../shared/types').Rule} */
|
|
module.exports = {
|
|
meta: {
|
|
type: "suggestion",
|
|
|
|
docs: {
|
|
description: "Disallow function declarations that contain unsafe references inside loop statements",
|
|
recommended: false,
|
|
url: "https://eslint.org/docs/latest/rules/no-loop-func"
|
|
},
|
|
|
|
schema: [],
|
|
|
|
messages: {
|
|
unsafeRefs: "Function declared in a loop contains unsafe references to variable(s) {{ varNames }}."
|
|
}
|
|
},
|
|
|
|
create(context) {
|
|
|
|
const sourceCode = context.sourceCode;
|
|
|
|
/**
|
|
* Reports functions which match the following condition:
|
|
*
|
|
* - has a loop node in ancestors.
|
|
* - has any references which refers to an unsafe variable.
|
|
* @param {ASTNode} node The AST node to check.
|
|
* @returns {void}
|
|
*/
|
|
function checkForLoops(node) {
|
|
const loopNode = getContainingLoopNode(node);
|
|
|
|
if (!loopNode) {
|
|
return;
|
|
}
|
|
|
|
const references = sourceCode.getScope(node).through;
|
|
const unsafeRefs = references.filter(r => r.resolved && !isSafe(loopNode, r)).map(r => r.identifier.name);
|
|
|
|
if (unsafeRefs.length > 0) {
|
|
context.report({
|
|
node,
|
|
messageId: "unsafeRefs",
|
|
data: { varNames: `'${unsafeRefs.join("', '")}'` }
|
|
});
|
|
}
|
|
}
|
|
|
|
return {
|
|
ArrowFunctionExpression: checkForLoops,
|
|
FunctionExpression: checkForLoops,
|
|
FunctionDeclaration: checkForLoops
|
|
};
|
|
}
|
|
};
|