 b3c00d7cd9
			
		
	
	b3c00d7cd9
	
	
	
		
			
			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>
		
			
				
	
	
		
			265 lines
		
	
	
		
			7.6 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			265 lines
		
	
	
		
			7.6 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| 'use strict';
 | |
| 
 | |
| const color = require('kleur');
 | |
| const Prompt = require('./prompt');
 | |
| const { erase, cursor } = require('sisteransi');
 | |
| const { style, clear, figures, wrap, entriesToDisplay } = require('../util');
 | |
| 
 | |
| const getVal = (arr, i) => arr[i] && (arr[i].value || arr[i].title || arr[i]);
 | |
| const getTitle = (arr, i) => arr[i] && (arr[i].title || arr[i].value || arr[i]);
 | |
| const getIndex = (arr, valOrTitle) => {
 | |
|   const index = arr.findIndex(el => el.value === valOrTitle || el.title === valOrTitle);
 | |
|   return index > -1 ? index : undefined;
 | |
| };
 | |
| 
 | |
| /**
 | |
|  * TextPrompt Base Element
 | |
|  * @param {Object} opts Options
 | |
|  * @param {String} opts.message Message
 | |
|  * @param {Array} opts.choices Array of auto-complete choices objects
 | |
|  * @param {Function} [opts.suggest] Filter function. Defaults to sort by title
 | |
|  * @param {Number} [opts.limit=10] Max number of results to show
 | |
|  * @param {Number} [opts.cursor=0] Cursor start position
 | |
|  * @param {String} [opts.style='default'] Render style
 | |
|  * @param {String} [opts.fallback] Fallback message - initial to default value
 | |
|  * @param {String} [opts.initial] Index of the default value
 | |
|  * @param {Boolean} [opts.clearFirst] The first ESCAPE keypress will clear the input
 | |
|  * @param {Stream} [opts.stdin] The Readable stream to listen to
 | |
|  * @param {Stream} [opts.stdout] The Writable stream to write readline data to
 | |
|  * @param {String} [opts.noMatches] The no matches found label
 | |
|  */
 | |
| class AutocompletePrompt extends Prompt {
 | |
|   constructor(opts={}) {
 | |
|     super(opts);
 | |
|     this.msg = opts.message;
 | |
|     this.suggest = opts.suggest;
 | |
|     this.choices = opts.choices;
 | |
|     this.initial = typeof opts.initial === 'number'
 | |
|       ? opts.initial
 | |
|       : getIndex(opts.choices, opts.initial);
 | |
|     this.select = this.initial || opts.cursor || 0;
 | |
|     this.i18n = { noMatches: opts.noMatches || 'no matches found' };
 | |
|     this.fallback = opts.fallback || this.initial;
 | |
|     this.clearFirst = opts.clearFirst || false;
 | |
|     this.suggestions = [];
 | |
|     this.input = '';
 | |
|     this.limit = opts.limit || 10;
 | |
|     this.cursor = 0;
 | |
|     this.transform = style.render(opts.style);
 | |
|     this.scale = this.transform.scale;
 | |
|     this.render = this.render.bind(this);
 | |
|     this.complete = this.complete.bind(this);
 | |
|     this.clear = clear('', this.out.columns);
 | |
|     this.complete(this.render);
 | |
|     this.render();
 | |
|   }
 | |
| 
 | |
|   set fallback(fb) {
 | |
|     this._fb = Number.isSafeInteger(parseInt(fb)) ? parseInt(fb) : fb;
 | |
|   }
 | |
| 
 | |
|   get fallback() {
 | |
|     let choice;
 | |
|     if (typeof this._fb === 'number')
 | |
|       choice = this.choices[this._fb];
 | |
|     else if (typeof this._fb === 'string')
 | |
|       choice = { title: this._fb };
 | |
|     return choice || this._fb || { title: this.i18n.noMatches };
 | |
|   }
 | |
| 
 | |
|   moveSelect(i) {
 | |
|     this.select = i;
 | |
|     if (this.suggestions.length > 0)
 | |
|       this.value = getVal(this.suggestions, i);
 | |
|     else this.value = this.fallback.value;
 | |
|     this.fire();
 | |
|   }
 | |
| 
 | |
|   async complete(cb) {
 | |
|     const p = (this.completing = this.suggest(this.input, this.choices));
 | |
|     const suggestions = await p;
 | |
| 
 | |
|     if (this.completing !== p) return;
 | |
|     this.suggestions = suggestions
 | |
|       .map((s, i, arr) => ({ title: getTitle(arr, i), value: getVal(arr, i), description: s.description }));
 | |
|     this.completing = false;
 | |
|     const l = Math.max(suggestions.length - 1, 0);
 | |
|     this.moveSelect(Math.min(l, this.select));
 | |
| 
 | |
|     cb && cb();
 | |
|   }
 | |
| 
 | |
|   reset() {
 | |
|     this.input = '';
 | |
|     this.complete(() => {
 | |
|       this.moveSelect(this.initial !== void 0 ? this.initial : 0);
 | |
|       this.render();
 | |
|     });
 | |
|     this.render();
 | |
|   }
 | |
| 
 | |
|   exit() {
 | |
|     if (this.clearFirst && this.input.length > 0) {
 | |
|       this.reset();
 | |
|     } else {
 | |
|       this.done = this.exited = true; 
 | |
|       this.aborted = false;
 | |
|       this.fire();
 | |
|       this.render();
 | |
|       this.out.write('\n');
 | |
|       this.close();
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   abort() {
 | |
|     this.done = this.aborted = true;
 | |
|     this.exited = false;
 | |
|     this.fire();
 | |
|     this.render();
 | |
|     this.out.write('\n');
 | |
|     this.close();
 | |
|   }
 | |
| 
 | |
|   submit() {
 | |
|     this.done = true;
 | |
|     this.aborted = this.exited = false;
 | |
|     this.fire();
 | |
|     this.render();
 | |
|     this.out.write('\n');
 | |
|     this.close();
 | |
|   }
 | |
| 
 | |
|   _(c, key) {
 | |
|     let s1 = this.input.slice(0, this.cursor);
 | |
|     let s2 = this.input.slice(this.cursor);
 | |
|     this.input = `${s1}${c}${s2}`;
 | |
|     this.cursor = s1.length+1;
 | |
|     this.complete(this.render);
 | |
|     this.render();
 | |
|   }
 | |
| 
 | |
|   delete() {
 | |
|     if (this.cursor === 0) return this.bell();
 | |
|     let s1 = this.input.slice(0, this.cursor-1);
 | |
|     let s2 = this.input.slice(this.cursor);
 | |
|     this.input = `${s1}${s2}`;
 | |
|     this.complete(this.render);
 | |
|     this.cursor = this.cursor-1;
 | |
|     this.render();
 | |
|   }
 | |
| 
 | |
|   deleteForward() {
 | |
|     if(this.cursor*this.scale >= this.rendered.length) return this.bell();
 | |
|     let s1 = this.input.slice(0, this.cursor);
 | |
|     let s2 = this.input.slice(this.cursor+1);
 | |
|     this.input = `${s1}${s2}`;
 | |
|     this.complete(this.render);
 | |
|     this.render();
 | |
|   }
 | |
| 
 | |
|   first() {
 | |
|     this.moveSelect(0);
 | |
|     this.render();
 | |
|   }
 | |
| 
 | |
|   last() {
 | |
|     this.moveSelect(this.suggestions.length - 1);
 | |
|     this.render();
 | |
|   }
 | |
| 
 | |
|   up() {
 | |
|     if (this.select === 0) {
 | |
|       this.moveSelect(this.suggestions.length - 1);
 | |
|     } else {
 | |
|       this.moveSelect(this.select - 1);
 | |
|     }
 | |
|     this.render();
 | |
|   }
 | |
| 
 | |
|   down() {
 | |
|     if (this.select === this.suggestions.length - 1) {
 | |
|       this.moveSelect(0);
 | |
|     } else {
 | |
|       this.moveSelect(this.select + 1);
 | |
|     }
 | |
|     this.render();
 | |
|   }
 | |
| 
 | |
|   next() {
 | |
|     if (this.select === this.suggestions.length - 1) {
 | |
|       this.moveSelect(0);
 | |
|     } else this.moveSelect(this.select + 1);
 | |
|     this.render();
 | |
|   }
 | |
| 
 | |
|   nextPage() {
 | |
|     this.moveSelect(Math.min(this.select + this.limit, this.suggestions.length - 1));
 | |
|     this.render();
 | |
|   }
 | |
| 
 | |
|   prevPage() {
 | |
|     this.moveSelect(Math.max(this.select - this.limit, 0));
 | |
|     this.render();
 | |
|   }
 | |
| 
 | |
|   left() {
 | |
|     if (this.cursor <= 0) return this.bell();
 | |
|     this.cursor = this.cursor-1;
 | |
|     this.render();
 | |
|   }
 | |
| 
 | |
|   right() {
 | |
|     if (this.cursor*this.scale >= this.rendered.length) return this.bell();
 | |
|     this.cursor = this.cursor+1;
 | |
|     this.render();
 | |
|   }
 | |
| 
 | |
|   renderOption(v, hovered, isStart, isEnd) {
 | |
|     let desc;
 | |
|     let prefix = isStart ? figures.arrowUp : isEnd ? figures.arrowDown : ' ';
 | |
|     let title = hovered ? color.cyan().underline(v.title) : v.title;
 | |
|     prefix = (hovered ? color.cyan(figures.pointer) + ' ' : '  ') + prefix;
 | |
|     if (v.description) {
 | |
|       desc = ` - ${v.description}`;
 | |
|       if (prefix.length + title.length + desc.length >= this.out.columns
 | |
|         || v.description.split(/\r?\n/).length > 1) {
 | |
|         desc = '\n' + wrap(v.description, { margin: 3, width: this.out.columns })
 | |
|       }
 | |
|     }
 | |
|     return prefix + ' ' + title + color.gray(desc || '');
 | |
|   }
 | |
| 
 | |
|   render() {
 | |
|     if (this.closed) return;
 | |
|     if (this.firstRender) this.out.write(cursor.hide);
 | |
|     else this.out.write(clear(this.outputText, this.out.columns));
 | |
|     super.render();
 | |
| 
 | |
|     let { startIndex, endIndex } = entriesToDisplay(this.select, this.choices.length, this.limit);
 | |
| 
 | |
|     this.outputText = [
 | |
|       style.symbol(this.done, this.aborted, this.exited),
 | |
|       color.bold(this.msg),
 | |
|       style.delimiter(this.completing),
 | |
|       this.done && this.suggestions[this.select]
 | |
|         ? this.suggestions[this.select].title
 | |
|         : this.rendered = this.transform.render(this.input)
 | |
|     ].join(' ');
 | |
| 
 | |
|     if (!this.done) {
 | |
|       const suggestions = this.suggestions
 | |
|         .slice(startIndex, endIndex)
 | |
|         .map((item, i) =>  this.renderOption(item,
 | |
|           this.select === i + startIndex,
 | |
|           i === 0 && startIndex > 0,
 | |
|           i + startIndex === endIndex - 1 && endIndex < this.choices.length))
 | |
|         .join('\n');
 | |
|       this.outputText += `\n` + (suggestions || color.gray(this.fallback.title));
 | |
|     }
 | |
| 
 | |
|     this.out.write(erase.line + cursor.to(0) + this.outputText);
 | |
|   }
 | |
| }
 | |
| 
 | |
| module.exports = AutocompletePrompt;
 |