wide-cli 0.0.0-stage → 0.3.101126

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (172) hide show
  1. package/.github/workflows/ci.yml +29 -0
  2. package/LICENSE +674 -0
  3. package/README.md +80 -2
  4. package/assets/logo.png +0 -0
  5. package/assets/wide-logo.png +0 -0
  6. package/bin/wide.js +17 -0
  7. package/dist/agent/context.d.ts +7 -0
  8. package/dist/agent/context.d.ts.map +1 -0
  9. package/dist/agent/context.js +38 -0
  10. package/dist/agent/context.js.map +1 -0
  11. package/dist/agent/index.d.ts +16 -0
  12. package/dist/agent/index.d.ts.map +1 -0
  13. package/dist/agent/index.js +138 -0
  14. package/dist/agent/index.js.map +1 -0
  15. package/dist/config/index.d.ts +29 -0
  16. package/dist/config/index.d.ts.map +1 -0
  17. package/dist/config/index.js +66 -0
  18. package/dist/config/index.js.map +1 -0
  19. package/dist/config/workspace.d.ts +7 -0
  20. package/dist/config/workspace.d.ts.map +1 -0
  21. package/dist/config/workspace.js +31 -0
  22. package/dist/config/workspace.js.map +1 -0
  23. package/dist/decision/index.d.ts +113 -0
  24. package/dist/decision/index.d.ts.map +1 -0
  25. package/dist/decision/index.js +485 -0
  26. package/dist/decision/index.js.map +1 -0
  27. package/dist/index.d.ts +2 -0
  28. package/dist/index.d.ts.map +1 -0
  29. package/dist/index.js +167 -0
  30. package/dist/index.js.map +1 -0
  31. package/dist/providers/claude.d.ts +10 -0
  32. package/dist/providers/claude.d.ts.map +1 -0
  33. package/dist/providers/claude.js +147 -0
  34. package/dist/providers/claude.js.map +1 -0
  35. package/dist/providers/gemini.d.ts +10 -0
  36. package/dist/providers/gemini.d.ts.map +1 -0
  37. package/dist/providers/gemini.js +129 -0
  38. package/dist/providers/gemini.js.map +1 -0
  39. package/dist/providers/index.d.ts +4 -0
  40. package/dist/providers/index.d.ts.map +1 -0
  41. package/dist/providers/index.js +44 -0
  42. package/dist/providers/index.js.map +1 -0
  43. package/dist/providers/ollama.d.ts +10 -0
  44. package/dist/providers/ollama.d.ts.map +1 -0
  45. package/dist/providers/ollama.js +94 -0
  46. package/dist/providers/ollama.js.map +1 -0
  47. package/dist/providers/openai.d.ts +11 -0
  48. package/dist/providers/openai.d.ts.map +1 -0
  49. package/dist/providers/openai.js +142 -0
  50. package/dist/providers/openai.js.map +1 -0
  51. package/dist/providers/types.d.ts +22 -0
  52. package/dist/providers/types.d.ts.map +1 -0
  53. package/dist/providers/types.js +2 -0
  54. package/dist/providers/types.js.map +1 -0
  55. package/dist/repl/index.d.ts +2 -0
  56. package/dist/repl/index.d.ts.map +1 -0
  57. package/dist/repl/index.js +94 -0
  58. package/dist/repl/index.js.map +1 -0
  59. package/dist/security/fuzzer.d.ts +20 -0
  60. package/dist/security/fuzzer.d.ts.map +1 -0
  61. package/dist/security/fuzzer.js +109 -0
  62. package/dist/security/fuzzer.js.map +1 -0
  63. package/dist/security/loader.d.ts +17 -0
  64. package/dist/security/loader.d.ts.map +1 -0
  65. package/dist/security/loader.js +126 -0
  66. package/dist/security/loader.js.map +1 -0
  67. package/dist/security/payloads.d.ts +9 -0
  68. package/dist/security/payloads.d.ts.map +1 -0
  69. package/dist/security/payloads.js +88 -0
  70. package/dist/security/payloads.js.map +1 -0
  71. package/dist/security/rules.d.ts +14 -0
  72. package/dist/security/rules.d.ts.map +1 -0
  73. package/dist/security/rules.js +288 -0
  74. package/dist/security/rules.js.map +1 -0
  75. package/dist/security/scanner.d.ts +29 -0
  76. package/dist/security/scanner.d.ts.map +1 -0
  77. package/dist/security/scanner.js +136 -0
  78. package/dist/security/scanner.js.map +1 -0
  79. package/dist/tools/browser.d.ts +47 -0
  80. package/dist/tools/browser.d.ts.map +1 -0
  81. package/dist/tools/browser.js +441 -0
  82. package/dist/tools/browser.js.map +1 -0
  83. package/dist/tools/fs.d.ts +5 -0
  84. package/dist/tools/fs.d.ts.map +1 -0
  85. package/dist/tools/fs.js +131 -0
  86. package/dist/tools/fs.js.map +1 -0
  87. package/dist/tools/git.d.ts +3 -0
  88. package/dist/tools/git.d.ts.map +1 -0
  89. package/dist/tools/git.js +50 -0
  90. package/dist/tools/git.js.map +1 -0
  91. package/dist/tools/http.d.ts +3 -0
  92. package/dist/tools/http.d.ts.map +1 -0
  93. package/dist/tools/http.js +69 -0
  94. package/dist/tools/http.js.map +1 -0
  95. package/dist/tools/index.d.ts +5 -0
  96. package/dist/tools/index.d.ts.map +1 -0
  97. package/dist/tools/index.js +50 -0
  98. package/dist/tools/index.js.map +1 -0
  99. package/dist/tools/search.d.ts +4 -0
  100. package/dist/tools/search.d.ts.map +1 -0
  101. package/dist/tools/search.js +109 -0
  102. package/dist/tools/search.js.map +1 -0
  103. package/dist/tools/security.d.ts +5 -0
  104. package/dist/tools/security.d.ts.map +1 -0
  105. package/dist/tools/security.js +129 -0
  106. package/dist/tools/security.js.map +1 -0
  107. package/dist/tools/shell.d.ts +3 -0
  108. package/dist/tools/shell.d.ts.map +1 -0
  109. package/dist/tools/shell.js +50 -0
  110. package/dist/tools/shell.js.map +1 -0
  111. package/dist/tools/types.d.ts +33 -0
  112. package/dist/tools/types.d.ts.map +1 -0
  113. package/dist/tools/types.js +2 -0
  114. package/dist/tools/types.js.map +1 -0
  115. package/dist/ui/banner.d.ts +2 -0
  116. package/dist/ui/banner.d.ts.map +1 -0
  117. package/dist/ui/banner.js +14 -0
  118. package/dist/ui/banner.js.map +1 -0
  119. package/dist/ui/colors.d.ts +18 -0
  120. package/dist/ui/colors.d.ts.map +1 -0
  121. package/dist/ui/colors.js +19 -0
  122. package/dist/ui/colors.js.map +1 -0
  123. package/dist/ui/diff.d.ts +2 -0
  124. package/dist/ui/diff.d.ts.map +1 -0
  125. package/dist/ui/diff.js +26 -0
  126. package/dist/ui/diff.js.map +1 -0
  127. package/dist/ui/markdown.d.ts +2 -0
  128. package/dist/ui/markdown.d.ts.map +1 -0
  129. package/dist/ui/markdown.js +32 -0
  130. package/dist/ui/markdown.js.map +1 -0
  131. package/dist/ui/spinner.d.ts +12 -0
  132. package/dist/ui/spinner.d.ts.map +1 -0
  133. package/dist/ui/spinner.js +46 -0
  134. package/dist/ui/spinner.js.map +1 -0
  135. package/package.json +50 -4
  136. package/src/agent/context.ts +44 -0
  137. package/src/agent/index.ts +166 -0
  138. package/src/config/index.ts +88 -0
  139. package/src/config/workspace.ts +39 -0
  140. package/src/decision/index.ts +528 -0
  141. package/src/index.ts +180 -0
  142. package/src/providers/claude.ts +163 -0
  143. package/src/providers/gemini.ts +147 -0
  144. package/src/providers/index.ts +52 -0
  145. package/src/providers/ollama.ts +108 -0
  146. package/src/providers/openai.ts +159 -0
  147. package/src/providers/types.ts +29 -0
  148. package/src/repl/index.ts +100 -0
  149. package/src/security/fuzzer.ts +140 -0
  150. package/src/security/loader.ts +129 -0
  151. package/src/security/payloads.ts +95 -0
  152. package/src/security/rules.ts +300 -0
  153. package/src/security/scanner.ts +180 -0
  154. package/src/tools/browser.ts +528 -0
  155. package/src/tools/fs.ts +143 -0
  156. package/src/tools/git.ts +51 -0
  157. package/src/tools/http.ts +77 -0
  158. package/src/tools/index.ts +64 -0
  159. package/src/tools/search.ts +109 -0
  160. package/src/tools/security.ts +140 -0
  161. package/src/tools/shell.ts +56 -0
  162. package/src/tools/types.ts +33 -0
  163. package/src/ui/banner.ts +17 -0
  164. package/src/ui/colors.ts +19 -0
  165. package/src/ui/diff.ts +24 -0
  166. package/src/ui/markdown.ts +34 -0
  167. package/src/ui/spinner.ts +53 -0
  168. package/tests/browser.test.mjs +71 -0
  169. package/tests/decision.test.mjs +68 -0
  170. package/tests/loader.test.mjs +24 -0
  171. package/tests/suite.test.mjs +61 -0
  172. package/tsconfig.json +20 -0
@@ -0,0 +1,528 @@
1
+ export class CommitmentGate {
2
+ private readonly statesCount: number;
3
+ private readonly minDwellTime: number;
4
+ private readonly cooldowns: Float64Array;
5
+ private readonly lastEntries: Float64Array;
6
+ private readonly exemptions: Uint8Array;
7
+ private currentState: number;
8
+ private enteredAtTime: number;
9
+ private acceptedTransitions: number;
10
+ private rejectedTransitions: number;
11
+
12
+ constructor(statesCount: number, minDwellTime: number, defaultCooldown = 0.0) {
13
+ if (statesCount < 1) throw new RangeError("statesCount must be at least 1");
14
+ if (minDwellTime < 0.0) throw new RangeError("minDwellTime cannot be negative");
15
+
16
+ this.statesCount = statesCount;
17
+ this.minDwellTime = minDwellTime;
18
+ this.cooldowns = new Float64Array(statesCount);
19
+ this.lastEntries = new Float64Array(statesCount);
20
+ this.exemptions = new Uint8Array(statesCount);
21
+
22
+ for (let i = 0; i < statesCount; i++) {
23
+ this.cooldowns[i] = defaultCooldown;
24
+ this.lastEntries[i] = -Infinity;
25
+ }
26
+
27
+ this.currentState = 0;
28
+ this.enteredAtTime = 0.0;
29
+ this.acceptedTransitions = 0;
30
+ this.rejectedTransitions = 0;
31
+ }
32
+
33
+ public get state(): number {
34
+ return this.currentState;
35
+ }
36
+
37
+ public get minDwell(): number {
38
+ return this.minDwellTime;
39
+ }
40
+
41
+ public get accepted(): number {
42
+ return this.acceptedTransitions;
43
+ }
44
+
45
+ public get rejected(): number {
46
+ return this.rejectedTransitions;
47
+ }
48
+
49
+ public setCooldown(state: number, seconds: number): void {
50
+ if (state < 0 || state >= this.statesCount) return;
51
+ this.cooldowns[state] = seconds;
52
+ }
53
+
54
+ public setExempt(state: number, exempt: boolean): void {
55
+ if (state < 0 || state >= this.statesCount) return;
56
+ this.exemptions[state] = exempt ? 1 : 0;
57
+ }
58
+
59
+ public allowed(next: number, now: number): boolean {
60
+ if (next < 0 || next >= this.statesCount) return false;
61
+ if (next === this.currentState) return false;
62
+ if (this.exemptions[next] !== 0) return true;
63
+ if (this.cooldowns[next] > 0.0 && now - this.lastEntries[next] < this.cooldowns[next]) return false;
64
+ return now - this.enteredAtTime >= this.minDwellTime;
65
+ }
66
+
67
+ public tryTransition(next: number, now: number): boolean {
68
+ if (!this.allowed(next, now)) {
69
+ this.rejectedTransitions++;
70
+ return false;
71
+ }
72
+
73
+ this.currentState = next;
74
+ this.enteredAtTime = now;
75
+ this.lastEntries[next] = now;
76
+ this.acceptedTransitions++;
77
+ return true;
78
+ }
79
+
80
+ public forceTransition(next: number, now: number): void {
81
+ if (next < 0 || next >= this.statesCount) return;
82
+ this.currentState = next;
83
+ this.enteredAtTime = now;
84
+ this.lastEntries[next] = now;
85
+ this.acceptedTransitions++;
86
+ }
87
+ }
88
+
89
+ export class Commitment {
90
+ private readonly lifetime: number;
91
+ private readonly lockWindow: number;
92
+ private adoptedAt: number;
93
+ private currentScore: number;
94
+ private hasCommitment: boolean;
95
+
96
+ constructor(lifetime: number, lockWindow: number) {
97
+ if (lifetime <= 0.0) throw new RangeError("lifetime must be positive");
98
+ if (lockWindow < 0.0) throw new RangeError("lockWindow cannot be negative");
99
+
100
+ this.lifetime = lifetime;
101
+ this.lockWindow = lockWindow;
102
+ this.adoptedAt = -Infinity;
103
+ this.currentScore = 0.0;
104
+ this.hasCommitment = false;
105
+ }
106
+
107
+ public get score(): number {
108
+ return this.currentScore;
109
+ }
110
+
111
+ public get committed(): boolean {
112
+ return this.hasCommitment;
113
+ }
114
+
115
+ public isExpired(now: number): boolean {
116
+ return !this.hasCommitment || now - this.adoptedAt >= this.lifetime;
117
+ }
118
+
119
+ public isLocked(now: number): boolean {
120
+ return this.hasCommitment && now - this.adoptedAt < this.lockWindow;
121
+ }
122
+
123
+ public shouldReplace(challengerScore: number, margin: number, now: number): boolean {
124
+ if (this.isExpired(now)) return true;
125
+ if (this.isLocked(now)) return false;
126
+ return challengerScore >= this.currentScore + margin;
127
+ }
128
+
129
+ public adopt(score: number, now: number): void {
130
+ this.currentScore = score;
131
+ this.adoptedAt = now;
132
+ this.hasCommitment = true;
133
+ }
134
+
135
+ public clear(): void {
136
+ this.hasCommitment = false;
137
+ this.currentScore = 0.0;
138
+ this.adoptedAt = -Infinity;
139
+ }
140
+ }
141
+
142
+ export class PluralityConsensus {
143
+ private readonly capacity: number;
144
+ private readonly classesCount: number;
145
+ private readonly freshnessWindow: number;
146
+ private readonly minVotesCount: number;
147
+ private readonly voterIds: Int32Array;
148
+ private readonly voterClasses: Int32Array;
149
+ private readonly voterTimes: Float64Array;
150
+ private readonly tallies: Int32Array;
151
+ private agreedValue: number;
152
+ private isSettled: boolean;
153
+
154
+ constructor(classesCount: number, capacity = 16, freshnessWindow = 6.0, minVotesCount = 1) {
155
+ if (classesCount < 1) throw new RangeError("classesCount must be at least 1");
156
+ if (capacity < 1) throw new RangeError("capacity must be at least 1");
157
+ if (freshnessWindow <= 0.0) throw new RangeError("freshnessWindow must be positive");
158
+ if (minVotesCount < 1) throw new RangeError("minVotesCount must be at least 1");
159
+
160
+ this.classesCount = classesCount;
161
+ this.capacity = capacity;
162
+ this.freshnessWindow = freshnessWindow;
163
+ this.minVotesCount = minVotesCount;
164
+ this.voterIds = new Int32Array(capacity);
165
+ this.voterClasses = new Int32Array(capacity);
166
+ this.voterTimes = new Float64Array(capacity);
167
+ this.tallies = new Int32Array(classesCount);
168
+ this.agreedValue = 0;
169
+ this.isSettled = false;
170
+ }
171
+
172
+ public get value(): number {
173
+ return this.agreedValue;
174
+ }
175
+
176
+ public get settled(): boolean {
177
+ return this.isSettled;
178
+ }
179
+
180
+ public vote(voterId: number, classIndex: number, now: number): boolean {
181
+ if (voterId === 0) return false;
182
+ if (classIndex < 0 || classIndex >= this.classesCount) return false;
183
+
184
+ for (let i = 0; i < this.capacity; i++) {
185
+ if (this.voterIds[i] === voterId) {
186
+ this.voterClasses[i] = classIndex;
187
+ this.voterTimes[i] = now;
188
+ return true;
189
+ }
190
+ }
191
+
192
+ for (let i = 0; i < this.capacity; i++) {
193
+ const live = this.voterIds[i] !== 0 && now - this.voterTimes[i] < this.freshnessWindow;
194
+ if (!live) {
195
+ this.voterIds[i] = voterId;
196
+ this.voterClasses[i] = classIndex;
197
+ this.voterTimes[i] = now;
198
+ return true;
199
+ }
200
+ }
201
+
202
+ return false;
203
+ }
204
+
205
+ public resolve(now: number): boolean {
206
+ this.tallies.fill(0);
207
+ let totalLive = 0;
208
+
209
+ for (let i = 0; i < this.capacity; i++) {
210
+ if (this.voterIds[i] !== 0 && now - this.voterTimes[i] < this.freshnessWindow) {
211
+ const c = this.voterClasses[i];
212
+ if (c >= 0 && c < this.classesCount) {
213
+ this.tallies[c]++;
214
+ totalLive++;
215
+ }
216
+ }
217
+ }
218
+
219
+ if (totalLive < this.minVotesCount) {
220
+ this.isSettled = false;
221
+ return false;
222
+ }
223
+
224
+ let topVotes = 0;
225
+ let topClass = 0;
226
+ let tie = false;
227
+
228
+ for (let c = 0; c < this.classesCount; c++) {
229
+ const v = this.tallies[c];
230
+ if (v > topVotes) {
231
+ topVotes = v;
232
+ topClass = c;
233
+ tie = false;
234
+ } else if (v === topVotes && topVotes > 0) {
235
+ tie = true;
236
+ }
237
+ }
238
+
239
+ if (topVotes >= this.minVotesCount && !tie) {
240
+ this.agreedValue = topClass;
241
+ this.isSettled = true;
242
+ return true;
243
+ }
244
+
245
+ this.isSettled = false;
246
+ return false;
247
+ }
248
+ }
249
+
250
+ export class ClaimTable {
251
+ private readonly slotsCount: number;
252
+ private readonly holders: Int32Array;
253
+ private readonly expirationTimes: Float64Array;
254
+ private readonly bids: Float64Array;
255
+
256
+ constructor(slotsCount: number) {
257
+ if (slotsCount < 1) throw new RangeError("slotsCount must be at least 1");
258
+ this.slotsCount = slotsCount;
259
+ this.holders = new Int32Array(slotsCount);
260
+ this.expirationTimes = new Float64Array(slotsCount);
261
+ this.bids = new Float64Array(slotsCount);
262
+ }
263
+
264
+ public get slots(): number {
265
+ return this.slotsCount;
266
+ }
267
+
268
+ public occupancy(now: number): number {
269
+ let n = 0;
270
+ for (let i = 0; i < this.slotsCount; i++) {
271
+ if (this.holders[i] !== 0 && now < this.expirationTimes[i]) n++;
272
+ }
273
+ return n;
274
+ }
275
+
276
+ public tryClaim(holderId: number, bid: number, lifetime: number, now: number, capacity: number): boolean {
277
+ if (holderId === 0 || capacity < 1 || lifetime <= 0.0) return false;
278
+
279
+ for (let i = 0; i < this.slotsCount; i++) {
280
+ if (this.holders[i] === holderId) {
281
+ this.expirationTimes[i] = now + lifetime;
282
+ this.bids[i] = bid;
283
+ return true;
284
+ }
285
+ }
286
+
287
+ let used = 0;
288
+ let freeIndex = -1;
289
+ let weakestIndex = -1;
290
+ let weakestBid = Infinity;
291
+
292
+ for (let i = 0; i < this.slotsCount; i++) {
293
+ const live = this.holders[i] !== 0 && now < this.expirationTimes[i];
294
+ if (!live) {
295
+ if (freeIndex < 0) freeIndex = i;
296
+ continue;
297
+ }
298
+ used++;
299
+ if (this.bids[i] < weakestBid) {
300
+ weakestBid = this.bids[i];
301
+ weakestIndex = i;
302
+ }
303
+ }
304
+
305
+ if (used < capacity && freeIndex >= 0) {
306
+ this.holders[freeIndex] = holderId;
307
+ this.expirationTimes[freeIndex] = now + lifetime;
308
+ this.bids[freeIndex] = bid;
309
+ return true;
310
+ }
311
+
312
+ if (weakestIndex >= 0 && bid > weakestBid) {
313
+ this.holders[weakestIndex] = holderId;
314
+ this.expirationTimes[weakestIndex] = now + lifetime;
315
+ this.bids[weakestIndex] = bid;
316
+ return true;
317
+ }
318
+
319
+ return false;
320
+ }
321
+
322
+ public release(holderId: number): void {
323
+ for (let i = 0; i < this.slotsCount; i++) {
324
+ if (this.holders[i] === holderId) {
325
+ this.holders[i] = 0;
326
+ this.expirationTimes[i] = 0.0;
327
+ this.bids[i] = 0.0;
328
+ }
329
+ }
330
+ }
331
+ }
332
+
333
+ export class PartitionAssignment {
334
+ private readonly partitionsCount: number;
335
+ private readonly assignments: Int32Array;
336
+ private readonly expirationTimes: Float64Array;
337
+
338
+ constructor(partitionsCount: number) {
339
+ if (partitionsCount < 1) throw new RangeError("partitionsCount must be at least 1");
340
+ this.partitionsCount = partitionsCount;
341
+ this.assignments = new Int32Array(partitionsCount);
342
+ this.expirationTimes = new Float64Array(partitionsCount);
343
+ }
344
+
345
+ public tryAssign(agentId: number, preferred: number, lifetime: number, now: number): { success: boolean; partition: number } {
346
+ if (agentId === 0 || lifetime <= 0.0) return { success: false, partition: -1 };
347
+
348
+ for (let i = 0; i < this.partitionsCount; i++) {
349
+ if (this.assignments[i] === agentId && now < this.expirationTimes[i]) {
350
+ this.expirationTimes[i] = now + lifetime;
351
+ return { success: true, partition: i };
352
+ }
353
+ }
354
+
355
+ const normPref = ((preferred % this.partitionsCount) + this.partitionsCount) % this.partitionsCount;
356
+
357
+ for (let step = 0; step <= Math.floor(this.partitionsCount / 2); step++) {
358
+ const candidates = step === 0 ? [normPref] : [
359
+ (normPref + step) % this.partitionsCount,
360
+ ((normPref - step) % this.partitionsCount + this.partitionsCount) % this.partitionsCount
361
+ ];
362
+
363
+ for (const c of candidates) {
364
+ const live = this.assignments[c] !== 0 && now < this.expirationTimes[c];
365
+ if (!live) {
366
+ this.assignments[c] = agentId;
367
+ this.expirationTimes[c] = now + lifetime;
368
+ return { success: true, partition: c };
369
+ }
370
+ }
371
+ }
372
+
373
+ return { success: false, partition: -1 };
374
+ }
375
+
376
+ public release(agentId: number): void {
377
+ for (let i = 0; i < this.partitionsCount; i++) {
378
+ if (this.assignments[i] === agentId) {
379
+ this.assignments[i] = 0;
380
+ this.expirationTimes[i] = 0.0;
381
+ }
382
+ }
383
+ }
384
+ }
385
+
386
+ export class Criterion {
387
+ public readonly weight: number;
388
+ public readonly term: (candidate: number) => number;
389
+
390
+ constructor(weight: number, term: (candidate: number) => number) {
391
+ if (weight < 0.0) throw new RangeError("weight cannot be negative");
392
+ if (!term) throw new TypeError("term cannot be null");
393
+ this.weight = weight;
394
+ this.term = term;
395
+ }
396
+ }
397
+
398
+ export class WeightedScorer {
399
+ private readonly criteria: Criterion[];
400
+ private readonly totalWeight: number;
401
+
402
+ constructor(...criteria: Criterion[]) {
403
+ if (!criteria || criteria.length === 0) throw new TypeError("criteria cannot be null or empty");
404
+ this.criteria = [...criteria];
405
+ let m = 0.0;
406
+ for (const c of this.criteria) {
407
+ if (!c || !c.term) throw new TypeError("criterion or term cannot be null");
408
+ m += c.weight;
409
+ }
410
+ if (m <= 0.0) throw new RangeError("total weight must be positive");
411
+ this.totalWeight = m;
412
+ }
413
+
414
+ public score(candidate: number): number {
415
+ let s = 0.0;
416
+ for (const c of this.criteria) {
417
+ let f = c.term(candidate);
418
+ if (f < 0.0) f = 0.0;
419
+ else if (f > 1.0) f = 1.0;
420
+ s += c.weight * f;
421
+ }
422
+ return s / this.totalWeight;
423
+ }
424
+
425
+ public trySelect(candidateCount: number, threshold: number, bias = 1.0): { success: boolean; best: number; score: number } {
426
+ if (candidateCount < 1 || bias <= 0.0) return { success: false, best: -1, score: 0.0 };
427
+
428
+ const effectiveThreshold = threshold / bias;
429
+ let bestIndex = -1;
430
+ let bestScore = -1.0;
431
+
432
+ for (let i = 0; i < candidateCount; i++) {
433
+ const s = this.score(i);
434
+ if (s > bestScore) {
435
+ bestScore = s;
436
+ bestIndex = i;
437
+ }
438
+ }
439
+
440
+ if (bestScore >= effectiveThreshold && bestIndex >= 0) {
441
+ return { success: true, best: bestIndex, score: bestScore };
442
+ }
443
+
444
+ return { success: false, best: bestIndex, score: bestScore };
445
+ }
446
+ }
447
+
448
+ export enum BarrierBudget {
449
+ Patient = 0,
450
+ Measured = 1,
451
+ Hasty = 2,
452
+ Immediate = 3
453
+ }
454
+
455
+ export class SynchronizationBarrier {
456
+ private readonly capacity: number;
457
+ private readonly agentIds: Int32Array;
458
+ private readonly readyStates: Uint8Array;
459
+ private readonly reportedEtas: Float64Array;
460
+ private readonly reportTimes: Float64Array;
461
+ private quorumReached: boolean;
462
+ private currentPhase: number;
463
+ private stageStartTime: number;
464
+
465
+ constructor(capacity = 16) {
466
+ if (capacity < 1) throw new RangeError("capacity must be at least 1");
467
+ this.capacity = capacity;
468
+ this.agentIds = new Int32Array(capacity);
469
+ this.readyStates = new Uint8Array(capacity);
470
+ this.reportedEtas = new Float64Array(capacity);
471
+ this.reportTimes = new Float64Array(capacity);
472
+ this.quorumReached = false;
473
+ this.currentPhase = 0;
474
+ this.stageStartTime = 0.0;
475
+ }
476
+
477
+ public get reachedQuorum(): boolean {
478
+ return this.quorumReached;
479
+ }
480
+
481
+ public reportReady(agentId: number, now: number): void {
482
+ if (agentId === 0) return;
483
+ for (let i = 0; i < this.capacity; i++) {
484
+ if (this.agentIds[i] === agentId || this.agentIds[i] === 0) {
485
+ this.agentIds[i] = agentId;
486
+ this.readyStates[i] = 1;
487
+ this.reportTimes[i] = now;
488
+ return;
489
+ }
490
+ }
491
+ }
492
+
493
+ public tick(population: number, quorumThreshold = 0.6, timeoutSeconds = 10.0, now: number): boolean {
494
+ if (population < 1) return false;
495
+ const required = Math.ceil(population * quorumThreshold);
496
+
497
+ let readyCount = 0;
498
+ for (let i = 0; i < this.capacity; i++) {
499
+ if (this.agentIds[i] !== 0 && this.readyStates[i] === 1) {
500
+ readyCount++;
501
+ }
502
+ }
503
+
504
+ if (this.stageStartTime === 0.0) this.stageStartTime = now;
505
+
506
+ if (readyCount >= required) {
507
+ this.quorumReached = true;
508
+ return true;
509
+ }
510
+
511
+ if (now - this.stageStartTime >= timeoutSeconds) {
512
+ this.quorumReached = false;
513
+ return true;
514
+ }
515
+
516
+ return false;
517
+ }
518
+
519
+ public reset(): void {
520
+ this.agentIds.fill(0);
521
+ this.readyStates.fill(0);
522
+ this.reportedEtas.fill(0.0);
523
+ this.reportTimes.fill(0.0);
524
+ this.quorumReached = false;
525
+ this.currentPhase = 0;
526
+ this.stageStartTime = 0.0;
527
+ }
528
+ }
package/src/index.ts ADDED
@@ -0,0 +1,180 @@
1
+ import { Command } from "commander";
2
+ import pc from "picocolors";
3
+ import { Agent } from "./agent/index.js";
4
+ import { configManager } from "./config/index.js";
5
+ import { loadWorkspaceConfig } from "./config/workspace.js";
6
+ import { createProvider } from "./providers/index.js";
7
+ import { startRepl } from "./repl/index.js";
8
+ import { executeTool } from "./tools/index.js";
9
+ import { getAllPayloadTypes } from "./security/payloads.js";
10
+ import { RuleLoader } from "./security/loader.js";
11
+
12
+ async function readStdin(): Promise<string> {
13
+ if (process.stdin.isTTY) {
14
+ return "";
15
+ }
16
+ return new Promise((resolvePromise) => {
17
+ let data = "";
18
+ process.stdin.setEncoding("utf8");
19
+ process.stdin.on("data", chunk => {
20
+ data += chunk;
21
+ });
22
+ process.stdin.on("end", () => {
23
+ resolvePromise(data.trim());
24
+ });
25
+ process.stdin.on("error", () => {
26
+ resolvePromise("");
27
+ });
28
+ });
29
+ }
30
+
31
+ const program = new Command();
32
+
33
+ program
34
+ .name("wide")
35
+ .description("Terminal-first autonomous AI coding and security agent for the Wide ecosystem")
36
+ .version("0.3101126")
37
+ .argument("[prompt...]", "Optional one-shot prompt for the agent to execute")
38
+ .option("-p, --provider <provider>", "LLM provider to use (gemini, ollama, claude, deepseek, openai)")
39
+ .option("-m, --model <model>", "Model name to use")
40
+ .option("-y, --yes", "Auto-approve all file edits and command executions")
41
+ .action(async (promptParts, options) => {
42
+ const pipedData = await readStdin();
43
+ const argPrompt = Array.isArray(promptParts) ? promptParts.join(" ").trim() : "";
44
+ let userPrompt = argPrompt;
45
+
46
+ if (pipedData) {
47
+ userPrompt = userPrompt ? `${userPrompt}\n\n[Piped Input]:\n${pipedData}` : pipedData;
48
+ }
49
+
50
+ if (options.yes) {
51
+ configManager.set("autoApproveWrite", true);
52
+ configManager.set("autoApproveRun", true);
53
+ }
54
+
55
+ const ws = loadWorkspaceConfig();
56
+ if (ws.rules.length > 0) {
57
+ console.log(pc.dim(`Loaded ${ws.rules.length} workspace rule(s) from .wide/rules`));
58
+ }
59
+
60
+ if (userPrompt) {
61
+ try {
62
+ const provider = createProvider(options.provider, options.model);
63
+ const agent = new Agent(process.cwd(), provider);
64
+ await agent.runPrompt(userPrompt);
65
+ } catch (err: any) {
66
+ console.error(`${pc.red("Error:")} ${err.message}`);
67
+ process.exit(1);
68
+ }
69
+ } else {
70
+ await startRepl();
71
+ }
72
+ });
73
+
74
+ program
75
+ .command("scan [path]")
76
+ .description("Run Wide native SAST security engine across 20+ vulnerability categories")
77
+ .option("-s, --severity <level>", "Minimum severity (CRITICAL, HIGH, MEDIUM, LOW, ALL)", "ALL")
78
+ .option("-r, --rules <path>", "Path to custom rules.json file")
79
+ .action(async (targetPath = ".", opts) => {
80
+ const res = await executeTool("run_security_scan", { path: targetPath, severityFilter: opts.severity, rulesPath: opts.rules }, { workingDirectory: process.cwd() });
81
+ console.log(res.output);
82
+ });
83
+
84
+ const rulesCmd = program
85
+ .command("rules")
86
+ .description("Manage and synchronize Wide security rules");
87
+
88
+ rulesCmd
89
+ .command("update")
90
+ .description("Download latest security rules from wide-rules database")
91
+ .action(async () => {
92
+ try {
93
+ console.log(pc.cyan("Downloading latest rules from wide-rules repository..."));
94
+ const result = await RuleLoader.updateRules();
95
+ console.log(pc.green(`Successfully synchronized ${result.count} rules to ${result.path}`));
96
+ } catch (err: any) {
97
+ console.error(pc.red(`Failed to update rules: ${err.message}`));
98
+ process.exit(1);
99
+ }
100
+ });
101
+
102
+ rulesCmd
103
+ .command("path")
104
+ .description("Show local rules cache path")
105
+ .action(() => {
106
+ console.log(RuleLoader.getCachePath());
107
+ });
108
+
109
+ rulesCmd
110
+ .command("agent")
111
+ .description("List active AI agent security rules")
112
+ .option("-r, --rules <path>", "Path to custom agent rules JSON file")
113
+ .action((opts) => {
114
+ const rules = RuleLoader.loadAgentRules(opts.rules);
115
+ const info = RuleLoader.getAgentRulesPath();
116
+ console.log(pc.bold(`Active AI Agent Security Rules (${rules.length}) [source: ${info.active}]:`));
117
+ for (const r of rules) {
118
+ console.log(` ${pc.cyan(r.id)} [${pc.yellow(r.severity)}] (${r.cwe}) - ${r.message}`);
119
+ }
120
+ });
121
+
122
+ program
123
+ .command("payloads <type>")
124
+ .description(`Output targeted security injection payloads (${getAllPayloadTypes().join(", ")})`)
125
+ .action(async (type) => {
126
+ const res = await executeTool("generate_payloads", { type }, { workingDirectory: process.cwd() });
127
+ console.log(res.output);
128
+ });
129
+
130
+ program
131
+ .command("fuzz <url> <param> <type>")
132
+ .description("Actively probe an endpoint parameter with injection vectors")
133
+ .option("-X, --method <method>", "HTTP method (GET, POST)", "GET")
134
+ .action(async (url, param, type, opts) => {
135
+ const res = await executeTool("fuzz_endpoint", { url, param, payloadType: type, method: opts.method }, { workingDirectory: process.cwd() });
136
+ console.log(res.output);
137
+ });
138
+
139
+ const configCmd = program
140
+ .command("config")
141
+ .description("Manage Wide CLI configuration and API keys");
142
+
143
+ configCmd
144
+ .command("list")
145
+ .description("List current configuration")
146
+ .action(() => {
147
+ const cfg = configManager.get();
148
+ console.log(pc.bold("\nWide CLI Configuration:"));
149
+ console.log(` Provider: ${pc.cyan(cfg.provider)}`);
150
+ console.log(` Model: ${pc.cyan(cfg.model)}`);
151
+ console.log(` Ollama URL: ${pc.dim(cfg.ollamaBaseUrl)}`);
152
+ console.log(` Auto-Approve: Read=${cfg.autoApproveRead}, Write=${cfg.autoApproveWrite}, Run=${cfg.autoApproveRun}`);
153
+ console.log(` Config Path: ${pc.dim("~/.wide/config.json")}\n`);
154
+ });
155
+
156
+ configCmd
157
+ .command("set <key> <value>")
158
+ .description("Set configuration property")
159
+ .action((key, value) => {
160
+ try {
161
+ configManager.set(key as any, value);
162
+ console.log(pc.green(`[OK] Updated '${key}' to '${value}'`));
163
+ } catch (err: any) {
164
+ console.error(pc.red(`Failed to set config: ${err.message}`));
165
+ }
166
+ });
167
+
168
+ configCmd
169
+ .command("set-key <provider> <apiKey>")
170
+ .description("Store API key for a provider")
171
+ .action((provider, apiKey) => {
172
+ try {
173
+ configManager.setApiKey(provider as any, apiKey);
174
+ console.log(pc.green(`[OK] Stored API key for provider '${provider}' in ~/.wide/config.json`));
175
+ } catch (err: any) {
176
+ console.error(pc.red(`Failed to set key: ${err.message}`));
177
+ }
178
+ });
179
+
180
+ await program.parseAsync(process.argv);