-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_server.mjs
More file actions
1356 lines (1228 loc) · 49.9 KB
/
Copy pathagent_server.mjs
File metadata and controls
1356 lines (1228 loc) · 49.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import express from "express";
import fs from "fs";
import path from "path";
import crypto from "crypto";
import { acknowledgeNotifications, connectRedis, enqueueJob, getJob, makeQueueKeys } from "./lib/queue.mjs";
import { buildCapabilityReceipt, getTrustZoneCapabilities, handleIncomingMessage } from "./lib/dispatch.mjs";
import { buildClientConversationKey, conversationPathForKey, deleteClientContinuity, executionHistoryPath, exportClientContinuity, pruneExpiredClientContinuity } from "./lib/client_continuity.mjs";
import { getCachedChatSystemPrompt } from "./lib/prompt_bundle.mjs";
import { getMemoryGraph, getRelevantMemoryGraphContext } from "./lib/memory_graph.mjs";
import { assertRuntimeSafetyConfig, getRuntimeSafetyConfig, isLoopbackHost } from "./lib/runtime_config.mjs";
import { durableAppendJsonl } from "./lib/durable_write_policy.mjs";
import { securityHeaders } from "./lib/security_headers.mjs";
import { getChosenModelString } from "./lib/model_router.mjs";
function isMainModule() {
try {
const mainPath = process.argv?.[1] || "";
return new URL(import.meta.url).pathname.endsWith(mainPath.replace(/\\/g, "/"));
} catch {
return false;
}
}
function isLoopbackRemoteAddress(address) {
const value = String(address ?? "").trim().toLowerCase();
if (!value) return false;
return value === "127.0.0.1"
|| value === "::1"
|| value === "::ffff:127.0.0.1";
}
function normalizeIp(ip) {
if (!ip) return "";
let s = String(ip).trim().toLowerCase();
if (s.startsWith("::ffff:")) {
s = s.substring(7);
}
if (s === "::1") return "127.0.0.1";
if (s === "localhost") return "127.0.0.1";
return s;
}
function tokensEqual(candidate, expected) {
if (!candidate || !expected) return false;
const left = Buffer.from(String(candidate));
const right = Buffer.from(String(expected));
return left.length === right.length && crypto.timingSafeEqual(left, right);
}
const DASHBOARD_SESSION_COOKIE = "dizzy_dashboard_session";
function readCookie(req, name) {
const raw = String(req.headers?.cookie || "");
for (const pair of raw.split(";")) {
const separator = pair.indexOf("=");
if (separator < 0) continue;
if (pair.slice(0, separator).trim() === name) return pair.slice(separator + 1).trim();
}
return "";
}
function isDashboardRoute(pathname) {
return pathname === "/dashboard"
|| pathname === "/dashboard/login"
|| pathname === "/dashboard/session"
|| pathname === "/dashboard/logout"
|| pathname === "/assets/dashboard.js"
|| pathname === "/assets/dashboard-login.js"
|| pathname === "/api/dashboard-data"
|| pathname === "/api/dashboard-query"
|| pathname === "/api/operator-continuity"
|| pathname === "/api/operator-continuity/export"
|| pathname === "/api/operator-continuity/audit"
|| pathname === "/api/operator-continuity/delete"
|| pathname === "/api/operator/hardware-status"
|| pathname === "/api/operator/consensus-map"
|| pathname === "/api/operator/sandbox-preflight"
|| pathname === "/api/operator/signoff"
|| pathname === "/api/operator/veto"
|| pathname === "/api/operator/run-simulation"
|| pathname === "/api/operator-execute"
|| pathname === "/api/operator/resolve-containment"
|| pathname === "/api/operator/friction-telemetry"
|| pathname === "/api/operator/quarantined-bridges"
|| pathname === "/api/operator/quarantined-bridges/accept"
|| pathname === "/api/operator/quarantined-bridges/reject"
|| pathname === "/api/operator/prune-continuity"
|| pathname === "/api/operator/prune-receipts"
|| pathname === "/api/operator/run-scenario-simulation";
}
function parseBool(value, fallback = false) {
const raw = String(value ?? (fallback ? "1" : "0")).trim().toLowerCase();
return raw === "1" || raw === "true" || raw === "yes" || raw === "on";
}
function parsePositiveInt(value, fallback) {
const n = Number(value);
return Number.isFinite(n) && n > 0 ? Math.floor(n) : fallback;
}
function registerDashboardFallbackRoutes(app, { enabled } = {}) {
for (const route of ["/dashboard", "/assets/dashboard.js", "/assets/dashboard-login.js", "/api/dashboard-data", "/api/dashboard-query", "/api/operator-continuity", "/api/operator-continuity/export", "/api/operator-continuity/audit", "/api/operator/hardware-status", "/api/operator/consensus-map", "/api/operator/sandbox-preflight"]) {
app.get(route, (req, res) => {
if (!enabled) return res.status(404).json({ ok: false, error: "Dashboard disabled" });
return res.status(503).json({
ok: false,
error: "Dashboard unavailable",
});
});
}
app.get("/dashboard/login", (req, res) => {
if (!enabled) return res.status(404).json({ ok: false, error: "Dashboard disabled" });
return res.status(503).json({ ok: false, error: "Dashboard unavailable" });
});
for (const route of ["/dashboard/session", "/dashboard/logout"]) {
app.post(route, (req, res) => {
if (!enabled) return res.status(404).json({ ok: false, error: "Dashboard disabled" });
return res.status(503).json({ ok: false, error: "Dashboard unavailable" });
});
}
for (const route of ["/api/operator-execute", "/api/operator-continuity/delete", "/api/operator/signoff", "/api/operator/veto", "/api/operator/run-simulation"]) {
app.post(route, (req, res) => {
if (!enabled) return res.status(404).json({ ok: false, error: "Dashboard disabled" });
return res.status(503).json({ ok: false, error: "Dashboard unavailable" });
});
}
}
function getRateLimitConfig(opts = {}) {
const enabled = opts.rateLimitEnabled !== undefined
? Boolean(opts.rateLimitEnabled)
: parseBool(process.env.DIZZY_RATE_LIMIT_ENABLED, false);
return {
enabled,
windowMs: parsePositiveInt(opts.rateLimitWindowMs ?? process.env.DIZZY_RATE_LIMIT_WINDOW_MS, 60000),
max: parsePositiveInt(opts.rateLimitMax ?? process.env.DIZZY_RATE_LIMIT_MAX, 120),
};
}
export function pruneExpiredRateLimitBuckets(buckets, now = Date.now()) {
let removed = 0;
for (const [key, bucket] of buckets.entries()) {
if (!bucket || Number(bucket.resetAt) <= now) {
buckets.delete(key);
removed += 1;
}
}
return removed;
}
function forwardedClientIp(req, trustedProxies = []) {
const forwardedFor = String(req.headers?.["x-forwarded-for"] || "")
.split(",")
.map(normalizeIp)
.filter(Boolean);
for (let i = forwardedFor.length - 1; i >= 0; i -= 1) {
if (!trustedProxies.includes(forwardedFor[i])) return forwardedFor[i];
}
if (forwardedFor.length > 0) return forwardedFor[0];
return normalizeIp(req.headers?.["x-real-ip"]);
}
function rateLimitClientKey(req, { deploymentMode, trustedProxies = [] } = {}) {
const remote = normalizeIp(req.socket?.remoteAddress || req.ip);
if (["proxied", "hosted"].includes(deploymentMode) && trustedProxies.includes(remote)) {
return forwardedClientIp(req, trustedProxies) || remote || "unknown";
}
return remote || normalizeIp(req.ip) || "unknown";
}
function createRateLimitMiddleware(config, trust = {}) {
const buckets = new Map();
let nextPruneAt = 0;
return function rateLimit(req, res, next) {
if (!config.enabled || req.path === "/health") return next();
const now = Date.now();
if (now >= nextPruneAt) {
pruneExpiredRateLimitBuckets(buckets, now);
nextPruneAt = now + config.windowMs;
}
const key = rateLimitClientKey(req, trust);
const current = buckets.get(key);
const bucket = current && current.resetAt > now
? current
: { count: 0, resetAt: now + config.windowMs };
bucket.count += 1;
buckets.set(key, bucket);
const remaining = Math.max(0, config.max - bucket.count);
const resetSeconds = Math.ceil(bucket.resetAt / 1000);
res.setHeader("RateLimit-Limit", String(config.max));
res.setHeader("RateLimit-Remaining", String(remaining));
res.setHeader("RateLimit-Reset", String(resetSeconds));
if (bucket.count > config.max) {
res.setHeader("Retry-After", String(Math.ceil((bucket.resetAt - now) / 1000)));
return res.status(429).json({
ok: false,
error: "Rate limit exceeded",
retry_after_ms: Math.max(0, bucket.resetAt - now),
});
}
return next();
};
}
function normalizeAllowedOrigins(value) {
const values = Array.isArray(value) ? value : String(value || "").split(",");
const origins = new Set();
for (const raw of values) {
const candidate = String(raw || "").trim();
if (!candidate) continue;
try {
origins.add(new URL(candidate).origin.toLowerCase());
} catch {
// Invalid allowlist entries never widen browser access.
}
}
return origins;
}
function createProxyExposureGuard({ authToken, deploymentMode, trustedProxies = [] }) {
return function proxyExposureGuard(req, res, next) {
const proxyHeaders = ["forwarded", "x-forwarded-for", "x-forwarded-host", "x-forwarded-proto", "x-real-ip"];
const forwarded = proxyHeaders.some((name) => String(req.headers?.[name] ?? "").trim() !== "");
if (forwarded && deploymentMode === "direct_local") {
return res.status(403).json({
ok: false,
error: "Forwarded requests are disabled in direct_local mode",
});
}
if (forwarded) {
const remote = normalizeIp(req.socket?.remoteAddress || req.ip);
if (trustedProxies.length === 0 || !trustedProxies.includes(remote)) {
return res.status(403).json({ ok: false, error: "Forwarded request from untrusted proxy" });
}
}
if (forwarded && !authToken) return res.status(403).json({ ok: false, error: "Forwarded requests require DIZZY_AUTH_TOKEN" });
return next();
};
}
function createBrowserOriginGuard({ bindHost, allowedOrigins }) {
const allowlist = normalizeAllowedOrigins(allowedOrigins);
const loopbackBinding = isLoopbackHost(bindHost);
return function browserOriginGuard(req, res, next) {
const rawOrigin = String(req.headers?.origin || "").trim();
if (!rawOrigin) return next();
let origin;
try {
origin = new URL(rawOrigin);
} catch {
return res.status(403).json({ ok: false, error: "Browser origin rejected" });
}
const normalizedOrigin = origin.origin.toLowerCase();
const allowed = allowlist.has(normalizedOrigin)
|| (loopbackBinding && isLoopbackHost(origin.hostname));
if (!allowed) {
return res.status(403).json({ ok: false, error: "Browser origin rejected" });
}
return next();
};
}
export function redactTextPayload(text) {
if (!text) return "";
let t = String(text);
t = t.replace(/\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/g, "[REDACTED_EMAIL]");
t = t.replace(/\b\d{3}[-.]?\d{3}[-.]?\d{4}\b/g, "[REDACTED_PHONE]");
t = t.replace(/\bsk-[A-Za-z0-9_-]{16,}\b/g, "[REDACTED_API_KEY]");
t = t.replace(/\b(\d{6,}):([A-Za-z0-9_-]{20,})\b/g, "[REDACTED_TELEGRAM_TOKEN]");
t = t.replace(/\bAIza[0-9A-Za-z_-]{20,}\b/g, "[REDACTED_API_KEY]");
t = t.replace(/\b([A-Z0-9_]{2,64}(?:API|TOKEN|SECRET|KEY)[A-Z0-9_]{0,64})=([^\s]{6,})\b/g, "$1=[REDACTED]");
return t;
}
function redactAuditValue(value, depth = 0) {
if (value == null) return value;
if (depth > 4) return "[REDACTED_DEPTH]";
if (typeof value === "string") return redactTextPayload(value).slice(0, 1000);
if (typeof value === "number" || typeof value === "boolean") return value;
if (Array.isArray(value)) return value.slice(0, 20).map((item) => redactAuditValue(item, depth + 1));
if (typeof value !== "object") return String(value);
const out = {};
for (const [key, raw] of Object.entries(value)) {
const k = String(key);
const lower = k.toLowerCase();
if (
lower.includes("authorization")
|| lower.includes("cookie")
|| lower.includes("token")
|| lower.includes("secret")
|| lower.includes("password")
|| lower.includes("api_key")
|| lower.includes("apikey")
|| lower.includes("#private")
|| lower.includes("#private_self")
) {
out[k] = "[REDACTED]";
continue;
}
out[k] = redactAuditValue(raw, depth + 1);
}
return out;
}
function buildBoundaryViolationReceipt({ reason, req, zone }) {
return {
t: new Date().toISOString(),
type: "boundary_violation",
reason,
zone,
ip: req.socket?.remoteAddress,
method: req.method,
path: req.path,
headers: redactAuditValue(req.headers),
body: redactAuditValue(req.body),
};
}
function cloneJsonValue(value) {
return value === undefined ? undefined : JSON.parse(JSON.stringify(value));
}
export function projectPublicState(state) {
const governance = state?.governance ?? {};
const transparency = governance.transparency ?? {};
const principles = governance.principles ?? {};
const productKernel = state?.product_kernel ?? {};
const constitutionalKernel = state?.constitutional_kernel ?? {};
return {
schema_version: state?.schema_version,
updated_at: state?.updated_at,
canonical_source: state?.canonical_source,
docs: {
primary: state?.docs?.primary,
constitutional_kernel: state?.docs?.constitutional_kernel,
constitutional_expansion: state?.docs?.constitutional_expansion,
},
governance: {
anchors: cloneJsonValue(governance.anchors ?? []),
transparency: {
structural_transparency: Boolean(transparency.structural_transparency),
operational_confidentiality: Boolean(transparency.operational_confidentiality),
public_docs: cloneJsonValue(transparency.public_docs ?? []),
},
principles: {
benkler: cloneJsonValue(principles.benkler ?? []),
waldron: cloneJsonValue(principles.waldron ?? []),
},
},
product_kernel: {
value: productKernel.value,
positive_primitives: cloneJsonValue(productKernel.positive_primitives ?? []),
day_1: productKernel.day_1,
week_2: productKernel.week_2,
month_3: productKernel.month_3,
acceptance_checks: cloneJsonValue(productKernel.acceptance_checks ?? []),
},
constitutional_kernel: {
file: constitutionalKernel.file,
expansion_file: constitutionalKernel.expansion_file,
non_negotiables: cloneJsonValue(constitutionalKernel.non_negotiables ?? []),
},
};
}
export function loadStateConfig(zone) {
const statePath = path.resolve(process.cwd(), "state.json");
if (!fs.existsSync(statePath)) return {};
const raw = fs.readFileSync(statePath, "utf8");
const parsed = JSON.parse(raw);
if (zone === "public") {
return projectPublicState(parsed);
}
return parsed;
}
function requestBoundaryAuditGuard(req, res, next) {
const text = String(req.body?.brief || req.body?.text || "").toLowerCase();
const rawZone = req.headers?.["x-dizzy-zone"] || req.body?.zone || "public";
const zone = rawZone === "private" ? "private" : "public";
const isLocal = isLoopbackRemoteAddress(req.socket?.remoteAddress);
let violation = false;
let reason = "";
if (zone === "private" && !isLocal) {
violation = true;
reason = "untrusted_host_claimed_private_zone";
}
// Phrase detection is defense-in-depth telemetry, not semantic authorization.
// Trust-zone capability checks remain the boundary for retrieval and mutation.
if (zone === "public" || !isLocal) {
if (
text.includes("override trust_zone") ||
text.includes("system prompt override") ||
text.includes("ignore boundaries") ||
text.includes("trust_zone=private_self") ||
text.includes("trust_zone: private_self") ||
text.includes("#private")
) {
violation = true;
reason = "adversarial_prompt_injection_trust_zone_bypass";
}
}
if (violation) {
const receiptPath = path.resolve(process.cwd(), "runtime", "audit", "boundary_violations.jsonl");
const receipt = buildBoundaryViolationReceipt({ reason, req, zone });
durableAppendJsonl(receiptPath, receipt);
return res.status(403).json({
ok: false,
error: "Boundary violation detected",
receipt,
});
}
if (zone === "public") {
if (req.body?.brief) {
req.body.brief = redactTextPayload(req.body.brief);
}
if (req.body?.text) {
req.body.text = redactTextPayload(req.body.text);
}
}
next();
}
function buildRuntimeContext(req) {
return {
trusted_local: isLoopbackRemoteAddress(req.socket?.remoteAddress),
request_path: String(req.path || ""),
};
}
function normalizeIdentifier(value, fallback) {
const normalized = String(value ?? "")
.trim()
.toLowerCase()
.replace(/[^a-z0-9_-]+/g, "_")
.replace(/^_+|_+$/g, "")
.slice(0, 80);
return normalized || fallback;
}
function normalizeFreeText(value, maxChars = 20_000) {
return String(value ?? "").trim().slice(0, Math.max(1, Number(maxChars) || 20_000));
}
function countJsonlRows(filePath) {
try {
const raw = fs.readFileSync(filePath, "utf8").trim();
return raw ? raw.split(/\r?\n/).length : 0;
} catch {
return 0;
}
}
function persistMissingExecuteTranscript({ conversationKey, beforeRows, brief, out }) {
const conversationPath = conversationPathForKey(conversationKey);
const rowsWritten = countJsonlRows(conversationPath) - beforeRows;
const nowIso = new Date().toISOString();
if (rowsWritten <= 0) {
durableAppendJsonl(conversationPath, {
t: nowIso,
role: "user",
text: redactTextPayload(normalizeFreeText(brief)),
source: "agent_execute",
});
}
if (rowsWritten < 2) {
const replyText = normalizeFreeText(out?.text || (out?.kind ? `Result kind: ${out.kind}` : "No textual response."));
durableAppendJsonl(conversationPath, {
t: new Date().toISOString(),
role: "assistant",
text: redactTextPayload(replyText),
source: "agent_execute",
});
}
}
function randomSuffix() {
return Math.random().toString(36).slice(2, 8);
}
function normalizeMeta(value) {
return value && typeof value === "object" && !Array.isArray(value) ? value : {};
}
function buildExecuteConversationKey(body = {}) {
const continuityMode = String(body?.continuity_mode ?? "").trim().toLowerCase();
if (continuityMode === "client") {
const key = buildClientConversationKey({ client_id: body?.client_id, service_id: body?.service_id });
if (key) return key;
}
return `execute_req_${Date.now()}_${randomSuffix()}`;
}
function buildIncomingMessage(body, req, defaults = {}) {
// HTTP normalization is an operator-safety boundary for machine-facing surfaces.
// It exists to keep queue keys, logs, and transport payloads sane, not to shape voice.
return {
channel: normalizeIdentifier(body?.channel ?? defaults.channel ?? "local", defaults.channel ?? "local"),
from: body?.from == null ? (defaults.from ?? null) : normalizeIdentifier(body?.from, "anon"),
text: normalizeFreeText(
body?.text ?? defaults.text ?? "",
Math.max(1_000, Number(process.env.DIZZY_HTTP_MESSAGE_MAX_CHARS || 20_000) || 20_000),
),
meta: normalizeMeta(body?.meta ?? defaults.meta ?? {}),
runtime_context: {
...buildRuntimeContext(req),
...(defaults.runtime_context && typeof defaults.runtime_context === "object" ? defaults.runtime_context : {}),
},
};
}
function shapeJobForResponse(job) {
const result = job.result_json ? JSON.parse(job.result_json) : null;
const maxErrorChars = Math.max(200, Number(process.env.DIZZY_HTTP_JOB_ERROR_MAX_CHARS || 1000) || 1000);
return {
id: job.id,
status: job.status,
type: job.type,
tool: job.tool,
effect: job.effect,
attempts: Number(job.attempts || "0"),
max_attempts: Number(job.max_attempts || "0"),
retry_count: Number(job.retry_count || "0"),
max_retries: Number(job.max_retries || "0"),
created_at_ms: job.created_at_ms,
updated_at_ms: job.updated_at_ms,
started_at_ms: job.started_at_ms,
finished_at_ms: job.finished_at_ms,
next_retry_at_ms: job.next_retry_at_ms,
last_retry_reason: job.last_retry_reason,
// Bound machine-surface payloads without altering assistant reply style.
last_error: job.last_error ? String(job.last_error).slice(0, maxErrorChars) : "",
dead_letter_path: job.dead_letter_path,
result,
};
}
export async function createRuntime(opts = {}) {
const port = Number(opts.port ?? process.env.PORT ?? 3000);
const bindHost = String(opts.bindHost ?? process.env.DIZZY_BIND_HOST ?? "127.0.0.1");
const authToken = String(opts.authToken ?? process.env.DIZZY_AUTH_TOKEN ?? "").trim();
const redisUrl = String(opts.redisUrl ?? process.env.REDIS_URL ?? "");
const queuePrefix = String(opts.queuePrefix ?? process.env.DIZZY_QUEUE_PREFIX ?? "dizzy");
const rateLimit = getRateLimitConfig(opts);
const allowedOrigins = opts.allowedOrigins ?? process.env.DIZZY_ALLOWED_ORIGINS ?? "";
const deploymentMode = String(opts.deploymentMode ?? process.env.DIZZY_DEPLOYMENT_MODE ?? "direct_local").trim().toLowerCase();
const publicSurfaceMode = String(opts.publicSurfaceMode ?? process.env.DIZZY_PUBLIC_SURFACES ?? "closed").trim().toLowerCase();
const dashboardEnabled = opts.dashboardEnabled !== undefined
? Boolean(opts.dashboardEnabled)
: parseBool(process.env.DIZZY_DASHBOARD_ENABLED, false);
const memoryGraphEnabled = opts.memoryGraphEnabled !== undefined
? Boolean(opts.memoryGraphEnabled)
: parseBool(process.env.DIZZY_MEMORY_GRAPH_ENABLED, false);
const enforceIdentityHeaders = opts.enforceIdentityHeaders !== undefined
? Boolean(opts.enforceIdentityHeaders)
: parseBool(process.env.DIZZY_ENFORCE_IDENTITY_HEADERS, false);
const executeToken = String(opts.executeToken ?? process.env.DIZZY_EXECUTE_TOKEN ?? "").trim();
const notifyToken = String(opts.notifyToken ?? process.env.DIZZY_NOTIFY_TOKEN ?? "").trim();
const trustedProxiesInput = opts.trustedProxies ?? process.env.DIZZY_TRUSTED_PROXIES ?? "";
const trustedProxies = String(trustedProxiesInput)
.split(",")
.map(normalizeIp)
.filter(Boolean);
const verifiedHttps = opts.verifiedHttps ?? parseBool(process.env.DIZZY_VERIFIED_HTTPS, false);
const runtimeSafety = getRuntimeSafetyConfig();
const safetyDiagnostics = assertRuntimeSafetyConfig({
...runtimeSafety,
bindHost,
authTokenConfigured: Boolean(authToken),
authTokenLength: authToken.length,
deploymentMode,
publicSurfaceMode,
verifiedHttps,
});
if (authToken && authToken.length < 32) {
console.warn(`[WARNING] DIZZY_AUTH_TOKEN is only ${authToken.length} characters long. A minimum length of 32 characters is highly recommended for security.`);
}
if (executeToken && executeToken.length < 16) {
console.warn(`[WARNING] DIZZY_EXECUTE_TOKEN is only ${executeToken.length} characters long. A minimum length of 16 characters is highly recommended for security.`);
}
if (notifyToken && notifyToken.length < 16) {
console.warn(`[WARNING] DIZZY_NOTIFY_TOKEN is only ${notifyToken.length} characters long. A minimum length of 16 characters is highly recommended for security.`);
}
const app = express();
app.use(securityHeaders({ verifiedHttps }));
app.use(express.json({ limit: "5mb" }));
app.use(express.urlencoded({ extended: false, limit: "16kb" }));
app.use(createProxyExposureGuard({ authToken, deploymentMode, trustedProxies }));
app.use(createBrowserOriginGuard({ bindHost, allowedOrigins }));
app.use(createRateLimitMiddleware(rateLimit, { deploymentMode, trustedProxies }));
if (enforceIdentityHeaders && deploymentMode !== "proxied") {
throw new Error("DIZZY_ENFORCE_IDENTITY_HEADERS=1 requires DIZZY_DEPLOYMENT_MODE=proxied.");
}
if (enforceIdentityHeaders && trustedProxies.length === 0) {
throw new Error("DIZZY_ENFORCE_IDENTITY_HEADERS=1 requires DIZZY_TRUSTED_PROXIES.");
}
if ((executeToken || notifyToken) && !authToken) {
throw new Error("DIZZY_AUTH_TOKEN is required when scoped API tokens are configured.");
}
const pruneClientContinuity = opts.pruneClientContinuity || pruneExpiredClientContinuity;
const clientContinuityPruneIntervalMs = parsePositiveInt(
opts.clientContinuityPruneIntervalMs ?? process.env.DIZZY_CLIENT_CONTINUITY_PRUNE_INTERVAL_MS,
60 * 1000,
);
let nextClientContinuityPruneAt = 0;
let clientContinuityPruneScheduled = false;
function scheduleClientContinuityPrune(nowMs = Date.now()) {
if (clientContinuityPruneScheduled || nowMs < nextClientContinuityPruneAt) return false;
nextClientContinuityPruneAt = nowMs + clientContinuityPruneIntervalMs;
clientContinuityPruneScheduled = true;
const timer = setTimeout(async () => {
try {
await pruneClientContinuity();
} catch (error) {
const message = redactTextPayload(String(error?.message ?? error)).slice(0, 300);
console.warn(`[client_continuity] prune_failed=${message}`);
} finally {
clientContinuityPruneScheduled = false;
}
}, 0);
if (typeof timer.unref === "function") timer.unref();
return true;
}
const dashboardSessionTtlMs = parsePositiveInt(
opts.dashboardSessionTtlMs ?? process.env.DIZZY_DASHBOARD_SESSION_TTL_MS,
60 * 60 * 1000,
);
let dashboardSessionToken = "";
let dashboardSessionExpiresAt = 0;
const hasDashboardSession = (req) => {
if (!dashboardSessionToken || Date.now() >= dashboardSessionExpiresAt) return false;
return tokensEqual(readCookie(req, DASHBOARD_SESSION_COOKIE), dashboardSessionToken);
};
const createDashboardSession = (candidate) => {
if (!tokensEqual(String(candidate || ""), authToken)) return null;
dashboardSessionToken = crypto.randomBytes(32).toString("base64url");
dashboardSessionExpiresAt = Date.now() + dashboardSessionTtlMs;
return {
token: dashboardSessionToken,
maxAgeSeconds: Math.max(1, Math.floor(dashboardSessionTtlMs / 1000)),
};
};
const clearDashboardSession = () => {
dashboardSessionToken = "";
dashboardSessionExpiresAt = 0;
};
const hasAnyToken = Boolean(authToken || executeToken || notifyToken);
if (hasAnyToken) {
const anonymousDiscoveryRoutes = new Set([
"/agent/profile",
"/agent/services",
"/agent/portfolio",
"/assets/logo",
"/governance",
]);
app.use((req, res, next) => {
// Health can remain open only on loopback bindings.
if (req.path === "/health" && isLoopbackHost(bindHost)) return next();
if (publicSurfaceMode === "discovery" && anonymousDiscoveryRoutes.has(req.path)) return next();
if (dashboardEnabled && ["/dashboard/login", "/dashboard/session", "/assets/dashboard-login.js"].includes(req.path)) return next();
if (dashboardEnabled && isDashboardRoute(req.path) && hasDashboardSession(req)) return next();
const auth = String(req.headers?.authorization ?? "");
const bearer = auth.toLowerCase().startsWith("bearer ") ? auth.slice("bearer ".length).trim() : "";
const headerToken = bearer || String(req.headers?.["x-dizzy-token"] ?? "").trim();
if (!headerToken) {
return res.status(401).json({ ok: false, error: "Unauthorized" });
}
// Check master authToken first (gives full access to everything)
if (tokensEqual(headerToken, authToken)) return next();
// Check scoped /agent/execute token
if (req.path === "/agent/execute" && tokensEqual(headerToken, executeToken)) {
return next();
}
// Check scoped /notify routes token (accepts /notify/... or /notify/.../ack)
const isNotifyRoute = req.path === "/notify" || req.path.startsWith("/notify/");
if (isNotifyRoute && tokensEqual(headerToken, notifyToken)) {
return next();
}
res.status(401).json({ ok: false, error: "Unauthorized" });
});
}
let redis = null;
let queueKeys = makeQueueKeys(queuePrefix);
let redisReady = false;
async function initRedis() {
if (!redisUrl) return;
try {
redis = await connectRedis(redisUrl);
queueKeys = makeQueueKeys(queuePrefix);
redisReady = true;
} catch {
redis = null;
redisReady = false;
}
}
await initRedis();
app.get("/health", async (req, res) => {
const out = {
ok: true,
service: "dizzy-agent-server",
port,
bind_host: bindHost,
auth: {
configured: Boolean(authToken),
scheme: authToken ? "bearer" : "none",
health_exempted: Boolean(authToken) ? isLoopbackHost(bindHost) : true,
},
deployment: {
mode: deploymentMode,
public_surfaces: publicSurfaceMode,
},
browser_origin_guard: {
enabled: true,
external_allowlist_configured: normalizeAllowedOrigins(allowedOrigins).size > 0,
},
redis: {
configured: Boolean(redisUrl),
ready: redisReady,
prefix: queuePrefix,
},
safety: {
warnings: safetyDiagnostics.warnings,
remote_mutations_enabled: runtimeSafety.allowRemoteMutations,
self_modify_enabled: runtimeSafety.allowSelfModify,
},
rate_limit: {
enabled: rateLimit.enabled,
window_ms: rateLimit.windowMs,
max: rateLimit.max,
health_exempted: true,
},
};
if (redisReady) {
try {
await redis.ping();
out.redis.ping = "ok";
} catch {
out.redis.ready = false;
out.redis.ping = "failed";
out.ok = false;
}
}
res.json(out);
});
app.get("/state", (req, res, next) => {
const rawZone = req.headers?.["x-dizzy-zone"] || req.query?.zone || "public";
const zone = rawZone === "private" ? "private" : "public";
const isLocal = isLoopbackRemoteAddress(req.socket?.remoteAddress);
const directLocalPrivateAccess = deploymentMode === "direct_local" && isLocal;
const authenticatedDeployment = ["proxied", "hosted"].includes(deploymentMode) && Boolean(authToken);
if (zone === "private" && !directLocalPrivateAccess && !authenticatedDeployment) {
return res.status(403).json({ ok: false, error: "Access denied to private state" });
}
try {
const stateConfig = loadStateConfig(zone);
res.json({ ok: true, state: stateConfig });
} catch (e) {
next(e);
}
});
app.get("/governance", async (req, res, next) => {
try {
const docPath = path.resolve(process.cwd(), "INTERACTION_NORMS.md");
if (!fs.existsSync(docPath)) {
return res.status(404).type("text/plain").send("Missing INTERACTION_NORMS.md");
}
const text = fs.readFileSync(docPath, "utf8");
res.setHeader("Cache-Control", "no-store");
res.type("text/markdown").send(text);
} catch (e) {
next(e);
}
});
if (!dashboardEnabled) {
registerDashboardFallbackRoutes(app, { enabled: false });
} else {
const dashboardLoader = opts.dashboardModuleLoader || (() => import("./lib/dashboard.mjs"));
try {
const dashboard = await dashboardLoader();
dashboard.registerDashboardRoutes(app, {
authToken,
normalizeIp,
isLoopbackHost,
assetPath: opts.dashboardAssetPath,
scriptAssetPath: opts.dashboardScriptAssetPath,
loginScriptAssetPath: opts.dashboardLoginScriptAssetPath,
verifiedHttps,
createDashboardSession,
clearDashboardSession,
requestBoundaryAuditGuard,
operatorExecute: runAgentExecute,
});
} catch (error) {
console.warn(`[dashboard] initialization_failed=${String(error?.message ?? error)}`);
registerDashboardFallbackRoutes(app, {
enabled: true,
});
}
}
app.get("/prompt", async (req, res, next) => {
try {
const trustZone = String(req.query?.trust_zone ?? "").trim().toLowerCase();
const { sources } = getCachedChatSystemPrompt({ trustZone });
const totalBytes = sources.reduce((sum, s) => sum + Number(s.bytes || 0), 0);
const constitutionalCount = sources.filter((s) => s.role === "constitutional").length;
const out = {
ok: true,
chat_backend: String(process.env.DIZZY_CHAT_BACKEND ?? "").trim() || "",
gemini_model: String(process.env.GEMINI_MODEL ?? "").trim() || "",
prompt_pack: String(process.env.DIZZY_PROMPT_PACK ?? "").trim() || "",
effective_trust_zone: trustZone || "",
prompt_modes: {
brevity: String(process.env.DIZZY_BREVITY_MODE ?? "lite").trim() || "lite",
affect: String(process.env.DIZZY_AFFECT_MODE ?? "attuned").trim() || "attuned",
reinforcement: String(process.env.DIZZY_REINFORCEMENT_MODE ?? "gold_star").trim() || "gold_star",
},
rag: {
enabled: String(process.env.DIZZY_RAG_ENABLED ?? "1") === "1",
top_k: Number(process.env.DIZZY_RAG_TOP_K ?? 4) || 4,
},
prompt_budget: {
files: sources.length,
constitutional_files: constitutionalCount,
supplemental_files: Math.max(0, sources.length - constitutionalCount),
total_bytes: totalBytes,
truncated_files: sources.filter((s) => s.truncated).length,
},
prompt_files: sources.map((s) => ({
path: s.path,
role: s.role,
exists: s.exists,
bytes: s.bytes,
sha256: s.sha256,
truncated: s.truncated,
})),
};
res.setHeader("Cache-Control", "no-store");
res.json(out);
} catch (e) {
next(e);
}
});
function memoryGraphAccessGuard(req, res, next) {
if (!memoryGraphEnabled) return res.status(404).json({ ok: false, error: "Memory graph disabled" });
if (!isLoopbackRemoteAddress(req.socket?.remoteAddress) && !authToken) {
return res.status(403).json({ ok: false, error: "Memory graph requires local access or authentication" });
}
return next();
}
app.get("/memory/graph", memoryGraphAccessGuard, async (req, res, next) => {
try {
const query = String(req.query.q ?? "").trim();
if (query) {
const graph = getRelevantMemoryGraphContext(query, {
k: Math.max(1, Math.min(10, Number(req.query.k ?? 3) || 3)),
});
return res.json({
ok: true,
query,
mode: "query",
graph: {
...graph,
docs: graph.docs.map(({ excerpt: _excerpt, ...doc }) => doc),
},
});
}
const graph = getMemoryGraph();
return res.json({
ok: true,
mode: "summary",
built_at: graph.built_at,
counts: graph.counts,
docs: graph.docs.slice(0, 20).map((d) => ({
path: d.path,
title: d.title,
kind: d.kind,
keywords: d.keywords.slice(0, 6),
entities: d.entities.slice(0, 6),
})),
entities: graph.entities.slice(0, 20),
});
} catch (e) {
next(e);
}
});
app.get("/assets/logo", (req, res) => {
const logoPath = path.resolve(process.cwd(), "dizzylogofull.png");
if (!fs.existsSync(logoPath)) {
return res.status(404).json({ ok: false, error: "Missing dizzylogofull.png" });
}
return res.sendFile(logoPath);
});
function absoluteUrl(req, routePath) {
return `${req.protocol}://${req.get("host")}${routePath}`;
}
// GET /agent/profile
app.get("/agent/profile", (req, res) => {
res.json({
name: "Dizzy",
description: "Operator-mediated public surface for bounded visuals and analysis",
avatar_url: absoluteUrl(req, "/assets/logo"),
capabilities: ["image_gen", "bounded_analysis"],
governance: { doc_path: "/governance" },
delivery_mode: "operator_mediated",
});
});
// GET /agent/services
app.get("/agent/services", (req, res) => {
res.json({
services: [
{ id: "dizzy_image_gen", title: "Narrative Visual", pricing: "informal_quote", category: "image_gen" },
{ id: "dizzy_visual_pack", title: "Visual Pack", pricing: "informal_quote", category: "image_gen" },
],
});
});
// GET /agent/portfolio
app.get("/agent/portfolio", (req, res) => {
res.json({
works: [],
});
});
async function enqueueTool({ tool, payload, effect, notify, idempotencyKey }) {
if (!redisReady) {
throw new Error("Redis not ready. Set REDIS_URL and run Redis.");
}
const maxRetries = Number(process.env.DIZZY_MAX_RETRIES || 3);
const result = await enqueueJob(redis, queueKeys, payload, {
type: "tool",
tool,
effect,
maxRetries,
notify,
idempotencyKey,
});
if (Array.isArray(result)) {
return { jobId: result[0], deduplicated: result[1] === 0 };
}
return { jobId: result, deduplicated: false };
}