my-durable 0.0.0-stage → 0.0.2
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.
- package/.env.example +7 -0
- package/LICENSE +21 -0
- package/README.md +328 -2
- package/ai_docs/durable_pi/announcement.md +860 -0
- package/ai_docs/durable_pi/changelog.md +51 -0
- package/ai_docs/durable_pi/chord-delta-findings.md +362 -0
- package/ai_docs/durable_pi/coding-agent-integration.md +70 -0
- package/ai_docs/durable_pi/pico-v5-chord-usage.md +391 -0
- package/ai_docs/durable_pi/pico-v5-handoff.md +921 -0
- package/ai_docs/durable_pi/readme.md +617 -0
- package/ai_docs/durable_pi/sources.md +22 -0
- package/ai_docs/durable_pi/spec.md +4696 -0
- package/ai_docs/durable_pi/vacation-planner.md +57 -0
- package/ai_docs/pi/chord.md +268 -0
- package/ai_docs/pi/cli-integration.md +110 -0
- package/ai_docs/pi/cli.md +325 -0
- package/ai_docs/pi/codemode.md +211 -0
- package/ai_docs/pi/compaction.md +467 -0
- package/ai_docs/pi/configuration.md +51 -0
- package/ai_docs/pi/containerization.md +187 -0
- package/ai_docs/pi/custom-provider.md +177 -0
- package/ai_docs/pi/environment-variables.md +102 -0
- package/ai_docs/pi/extensions.md +278 -0
- package/ai_docs/pi/how-pi-works.md +53 -0
- package/ai_docs/pi/index.md +43 -0
- package/ai_docs/pi/json.md +230 -0
- package/ai_docs/pi/keybindings.md +198 -0
- package/ai_docs/pi/llama-cpp.md +118 -0
- package/ai_docs/pi/mcp.md +274 -0
- package/ai_docs/pi/message-types.md +265 -0
- package/ai_docs/pi/models.md +209 -0
- package/ai_docs/pi/overview.md +166 -0
- package/ai_docs/pi/packages.md +133 -0
- package/ai_docs/pi/pi-agent-core.md +568 -0
- package/ai_docs/pi/pi-ai.md +1942 -0
- package/ai_docs/pi/prompt-templates.md +63 -0
- package/ai_docs/pi/providers.md +204 -0
- package/ai_docs/pi/quickstart.md +140 -0
- package/ai_docs/pi/readme.md +99 -0
- package/ai_docs/pi/rpc-commands.md +864 -0
- package/ai_docs/pi/rpc-extension-ui.md +204 -0
- package/ai_docs/pi/rpc.md +197 -0
- package/ai_docs/pi/sdk.md +151 -0
- package/ai_docs/pi/security.md +103 -0
- package/ai_docs/pi/session-format.md +297 -0
- package/ai_docs/pi/sessions.md +70 -0
- package/ai_docs/pi/settings.md +173 -0
- package/ai_docs/pi/shell-aliases.md +97 -0
- package/ai_docs/pi/skills.md +97 -0
- package/ai_docs/pi/slash-commands.md +64 -0
- package/ai_docs/pi/sources.md +57 -0
- package/ai_docs/pi/terminal-setup.md +223 -0
- package/ai_docs/pi/termux.md +121 -0
- package/ai_docs/pi/themes.md +140 -0
- package/ai_docs/pi/tmux.md +59 -0
- package/ai_docs/pi/toolkit.md +168 -0
- package/ai_docs/pi/tui.md +129 -0
- package/ai_docs/pi/usage.md +98 -0
- package/ai_docs/pi/virtual-models.md +118 -0
- package/ai_docs/pi/windows.md +71 -0
- package/apps/dpi-backend/.nvmrc +1 -0
- package/apps/dpi-backend/API.md +99 -0
- package/apps/dpi-backend/README.md +119 -0
- package/apps/dpi-backend/deploy/backend.env.example +9 -0
- package/apps/dpi-backend/deploy/dpi-backend.plist +26 -0
- package/apps/dpi-backend/deploy/dpi-backend.service +22 -0
- package/apps/dpi-backend/package.json +33 -0
- package/apps/dpi-backend/prompts/one_sentence.md +3 -0
- package/apps/dpi-backend/prompts/smart_ass_opus_fix.md +169 -0
- package/apps/dpi-backend/scripts/check-contract.mjs +151 -0
- package/apps/dpi-backend/scripts/migrate-playground.ts +66 -0
- package/apps/dpi-backend/scripts/record-nested-fixture.mts +154 -0
- package/apps/dpi-backend/scripts/token.ts +57 -0
- package/apps/dpi-backend/src/addons.ts +139 -0
- package/apps/dpi-backend/src/auth.ts +20 -0
- package/apps/dpi-backend/src/config.ts +54 -0
- package/apps/dpi-backend/src/contract-check.ts +118 -0
- package/apps/dpi-backend/src/errors.ts +50 -0
- package/apps/dpi-backend/src/extensions.ts +510 -0
- package/apps/dpi-backend/src/faux.ts +80 -0
- package/apps/dpi-backend/src/history.ts +165 -0
- package/apps/dpi-backend/src/idempotency.ts +20 -0
- package/apps/dpi-backend/src/jev.ts +43 -0
- package/apps/dpi-backend/src/jobs.ts +790 -0
- package/apps/dpi-backend/src/main.ts +76 -0
- package/apps/dpi-backend/src/model-catalog.ts +180 -0
- package/apps/dpi-backend/src/pairing-files.ts +142 -0
- package/apps/dpi-backend/src/pairing.ts +459 -0
- package/apps/dpi-backend/src/routes.ts +217 -0
- package/apps/dpi-backend/src/shared-state.ts +85 -0
- package/apps/dpi-backend/src/store.ts +1240 -0
- package/apps/dpi-backend/src/streams.ts +256 -0
- package/apps/dpi-backend/src/tunnel.ts +955 -0
- package/apps/dpi-backend/tsconfig.json +13 -0
- package/apps/dpi-playground/README.md +83 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/assets/index-CFGc7lHT.js +77 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/assets/index-DFdKpwm_.css +1 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/icons/anthropic.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/icons/deepseek.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/icons/durable-pi.svg +14 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/icons/gemini.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/icons/grok.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/icons/kimi.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/icons/lab.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/icons/meta.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/icons/minimax.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/icons/openai.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/icons/qwen.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/icons/xiaomi.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/icons/zai.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/index.html +16 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/index.html +15 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/package.json +5 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/public/icons/anthropic.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/public/icons/deepseek.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/public/icons/durable-pi.svg +14 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/public/icons/gemini.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/public/icons/grok.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/public/icons/kimi.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/public/icons/lab.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/public/icons/meta.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/public/icons/minimax.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/public/icons/openai.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/public/icons/qwen.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/public/icons/xiaomi.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/public/icons/zai.png +0 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/App.vue +180 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/commands.ts +119 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/composables.ts +129 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/hub.ts +40 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/icon-path.ts +7 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/ids.ts +2 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/jobs.ts +80 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/link-policy.ts +16 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/markdown-policy.ts +115 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/markdown.ts +60 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/pair-close.ts +23 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/pair-handoff.ts +56 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/pair.ts +166 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/qrcode.ts +323 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/signature.ts +146 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/store.ts +373 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/client/stream.ts +183 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/AgentView.vue +342 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/Composer.vue +198 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/ContextBar.vue +254 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/DeviceCredentialsDialog.vue +40 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/DeviceIcon.vue +56 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/DirBrowser.vue +95 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/ExtensionsPanel.vue +96 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/HelpDialog.vue +69 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/Inspector.vue +425 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/JobCard.vue +221 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/MobileNav.vue +39 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/ModelPicker.vue +71 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/NetworkSidebar.vue +236 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/NewSessionDialog.vue +114 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/OfflinePanel.vue +128 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/PairDeviceWizard.vue +433 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/PromptDialog.vue +89 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/StateEditor.vue +179 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/TasksPanel.vue +280 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/Toast.vue +11 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/TokenManager.vue +175 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/TopBar.vue +114 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/TranscriptItem.vue +152 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/TreeRow.vue +195 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/components/TunnelStatusBar.vue +107 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/extensions.css +105 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/jobs.css +655 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/main.ts +22 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/network.css +675 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/src/style.css +3203 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/tsconfig.json +16 -0
- package/apps/dpi-playground/apps/dpi-playground-fe/vite.config.ts +14 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/package.json +5 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/audit.ts +406 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/client-ip.ts +72 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/credentials.ts +265 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/guard.ts +43 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/http-errors.ts +156 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/identity.ts +60 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/limiter.ts +165 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/main.ts +460 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/monitor.ts +320 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/mux.ts +277 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/pairing.ts +816 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/proxy.ts +128 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/rotation.ts +583 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/seed.ts +11 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/transport.ts +108 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/tunnel-token.ts +11 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/tunnel-transport.ts +560 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/src/tunnel.ts +873 -0
- package/apps/dpi-playground/apps/dpi-playground-hub/tsconfig.json +13 -0
- package/apps/dpi-playground/package.json +37 -0
- package/apps/dpi-sdk/README.md +74 -0
- package/apps/dpi-sdk/package.json +20 -0
- package/apps/dpi-sdk/scripts/net.ts +73 -0
- package/apps/dpi-sdk/src/api.ts +631 -0
- package/apps/dpi-sdk/src/app-state.ts +111 -0
- package/apps/dpi-sdk/src/client.ts +330 -0
- package/apps/dpi-sdk/src/errors.ts +16 -0
- package/apps/dpi-sdk/src/jobs.ts +99 -0
- package/apps/dpi-sdk/src/models.ts +472 -0
- package/apps/dpi-sdk/src/node/chunk.ts +488 -0
- package/apps/dpi-sdk/src/node/config.ts +175 -0
- package/apps/dpi-sdk/src/node/drivers.ts +402 -0
- package/apps/dpi-sdk/src/node/ops.ts +114 -0
- package/apps/dpi-sdk/src/node/private-dir.ts +48 -0
- package/apps/dpi-sdk/src/node/registry-migration.ts +549 -0
- package/apps/dpi-sdk/src/node/registry.ts +390 -0
- package/apps/dpi-sdk/src/node/sse-writer.ts +151 -0
- package/apps/dpi-sdk/src/protocol/backoff.ts +31 -0
- package/apps/dpi-sdk/src/protocol/limits.ts +199 -0
- package/apps/dpi-sdk/src/protocol/pairing.ts +311 -0
- package/apps/dpi-sdk/src/protocol/rest-routes.ts +105 -0
- package/apps/dpi-sdk/src/protocol/tunnel.ts +331 -0
- package/apps/dpi-sdk/src/sse.ts +117 -0
- package/apps/dpi-sdk/src/transcript.ts +291 -0
- package/apps/dpi-sdk/tsconfig.json +13 -0
- package/apps/dpi-sdk/w +20 -0
- package/apps/dpi-tui/README.md +54 -0
- package/apps/dpi-tui/bin/dpi-tui.mjs +5 -0
- package/apps/dpi-tui/package.json +20 -0
- package/apps/dpi-tui/src/app.ts +433 -0
- package/apps/dpi-tui/src/commands.ts +151 -0
- package/apps/dpi-tui/src/main.ts +122 -0
- package/apps/dpi-tui/src/pairing.ts +287 -0
- package/apps/dpi-tui/src/pickers.ts +238 -0
- package/apps/dpi-tui/src/render.ts +164 -0
- package/apps/dpi-tui/src/theme.ts +57 -0
- package/apps/dpi-tui/tsconfig.json +14 -0
- package/bin/device.mjs +5 -0
- package/bin/hub.mjs +5 -0
- package/bin/tui.mjs +5 -0
- package/deploy/Caddyfile +120 -0
- package/docs/verification.md +102 -0
- package/dpi-network.example.yaml +62 -0
- package/images/01_durable_agent_network.svg +185 -0
- package/images/02_pi_made_durable.svg +204 -0
- package/images/03_restart_mid_tool.svg +168 -0
- package/images/07_one_api_every_client.svg +123 -0
- package/images/08_device_session_agent.svg +123 -0
- package/images/09_compact_keep_history.svg +143 -0
- package/images/10_shared_app_state.svg +177 -0
- package/images/11_durable_jobs_wait.svg +141 -0
- package/images/12_tools_change_mid_run.svg +231 -0
- package/images/13_hook_blocks_tool.svg +122 -0
- package/images/14_fork_at_any_entry.svg +119 -0
- package/images/15_web_client.png +0 -0
- package/images/16_terminal_client.svg +43 -0
- package/images/17_devices_are_data.svg +182 -0
- package/images/18_should_every_agent_be_durable.svg +118 -0
- package/images/19_sqlite_on_every_device.svg +155 -0
- package/images/20_admission_and_effects.svg +134 -0
- package/justfile +33 -0
- package/package.json +41 -4
- package/scripts/verify.mjs +210 -0
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
// Custom harness extensions an agent can turn on and off. Each is a plain pi-durable extension in the registry; the
|
|
2
|
+
// agent's stored selection (`configure({ extensions: { add | remove } })`) decides whether it runs, from the next
|
|
3
|
+
// request on. Neither is selected by default.
|
|
4
|
+
//
|
|
5
|
+
// system-prompts a checklist of built-in system prompts, rendered as one section
|
|
6
|
+
// jev-damage-control Jev judges every bash command before it runs and blocks destructive ones
|
|
7
|
+
import { readFileSync } from "node:fs";
|
|
8
|
+
import { join } from "node:path";
|
|
9
|
+
import { defineDoc, defineExtension, hook, section, ToolTask, type Extension } from "@earendil-works/pi-durable";
|
|
10
|
+
import type { HarnessExtensionInfo } from "../../dpi-sdk/src/api.js";
|
|
11
|
+
import { askJev, type JevAnswer, type JevQuestion } from "./jev.js";
|
|
12
|
+
|
|
13
|
+
const PROMPTS_DIR = join(import.meta.dirname, "../prompts");
|
|
14
|
+
|
|
15
|
+
export const PROMPTS = [
|
|
16
|
+
{
|
|
17
|
+
id: "smart_ass_opus_fix.md",
|
|
18
|
+
title: "Smart Ass Opus fix",
|
|
19
|
+
description: "Clear, concise, actionable communication: the most important point last, no filler, reference codes.",
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
id: "one_sentence.md",
|
|
23
|
+
title: "One sentence",
|
|
24
|
+
description: "Replies in one sentence unless you ask for more. Easy to see turn on and off.",
|
|
25
|
+
},
|
|
26
|
+
].map((p) => ({ ...p, text: readFileSync(join(PROMPTS_DIR, p.id), "utf8").trim() }));
|
|
27
|
+
|
|
28
|
+
/** Which built-in prompts this agent's system-prompts extension renders. */
|
|
29
|
+
export const PromptChecklist = defineDoc<{ enabled: string[] }>({
|
|
30
|
+
kind: "ext.system-prompts",
|
|
31
|
+
version: 1,
|
|
32
|
+
scope: "conversation",
|
|
33
|
+
history: "latest",
|
|
34
|
+
fork: "current",
|
|
35
|
+
initial: () => ({ enabled: [PROMPTS[0].id] }),
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// ------------------------------------------------------------------ jev-damage-control
|
|
39
|
+
|
|
40
|
+
/** The jev-bash gate's questions and thresholds (harness/extensions/jev-bash/bash-gate.ts). */
|
|
41
|
+
const BASH_QUESTIONS: Record<string, JevQuestion> = {
|
|
42
|
+
effect: {
|
|
43
|
+
type: "choice",
|
|
44
|
+
instructions: "What does running `command` do to the machine?",
|
|
45
|
+
criteria: {
|
|
46
|
+
read_only: "Lists, reads, searches, tests, builds into a scratch directory; nothing durable changes",
|
|
47
|
+
reversible: "Changes files or state that git or a reinstall can restore: edits, installs, generated output",
|
|
48
|
+
irreversible:
|
|
49
|
+
"Deletes or overwrites things with no way back: removing directories, force pushing, dropping data, rewriting history",
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
destructive_intent: {
|
|
53
|
+
type: "noul",
|
|
54
|
+
instructions: "Does `command` aim to remove or wipe something rather than build or inspect?",
|
|
55
|
+
criteria: {
|
|
56
|
+
true: "rm -rf, drop, purge, force, reset --hard, truncate, overwriting real data",
|
|
57
|
+
false: "Reading, listing, testing, installing, generating, or editing in place",
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
type Verdict = { block: boolean; reason: string };
|
|
63
|
+
|
|
64
|
+
function gate(a: Record<string, JevAnswer>): Verdict {
|
|
65
|
+
const effect = a.effect as Extract<JevAnswer, { type: "choice" }>;
|
|
66
|
+
const destructive = (a.destructive_intent as Extract<JevAnswer, { type: "noul" }>).noul;
|
|
67
|
+
if (effect.choice === "irreversible" && effect.confidence >= 0.6)
|
|
68
|
+
return { block: true, reason: `irreversible (${effect.confidence.toFixed(2)}): nothing would restore what this removes or overwrites` };
|
|
69
|
+
if (destructive >= 0.7)
|
|
70
|
+
return { block: true, reason: `destructive intent (${destructive.toFixed(2)}): this command aims to wipe something` };
|
|
71
|
+
return { block: false, reason: `${effect.choice} (${effect.confidence.toFixed(2)}), destructive ${destructive.toFixed(2)}` };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** The faux provider's stand-in for Jev, so tests and offline demos never call the network. */
|
|
75
|
+
const OFFLINE_DANGER = /\brm\s+-[a-z]*[rf]|\bgit\s+push\b.*--force|\bgit\s+reset\s+--hard|\bdropdb\b|\bmkfs|\bdd\s+.*of=|\btruncate\b/;
|
|
76
|
+
async function judgeOffline(command: string): Promise<Verdict> {
|
|
77
|
+
return OFFLINE_DANGER.test(command)
|
|
78
|
+
? { block: true, reason: "destructive intent (offline rules): this command aims to wipe something" }
|
|
79
|
+
: { block: false, reason: "allowed (offline rules)" };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// ------------------------------------------------------------------ the extensions
|
|
83
|
+
|
|
84
|
+
export const ADDON_INFO: Omit<HarnessExtensionInfo, "enabled" | "prompts">[] = [
|
|
85
|
+
{
|
|
86
|
+
name: "system-prompts",
|
|
87
|
+
title: "System prompts",
|
|
88
|
+
description: "Adds the checked prompts to this agent's system prompt. A change applies from the next request.",
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
name: "jev-damage-control",
|
|
92
|
+
title: "Jev damage control",
|
|
93
|
+
description:
|
|
94
|
+
"Before every bash call, Jev judges the command in about 300 ms and blocks irreversible or destructive ones. The decision is a durable memo, so a restart never re-asks. If Jev is down, commands run.",
|
|
95
|
+
},
|
|
96
|
+
];
|
|
97
|
+
|
|
98
|
+
export function createAddons(opts: { offline: boolean }) {
|
|
99
|
+
const prompts = defineExtension({
|
|
100
|
+
name: "system-prompts",
|
|
101
|
+
sections: [
|
|
102
|
+
section(
|
|
103
|
+
"system-prompts",
|
|
104
|
+
async (input, ctx) => {
|
|
105
|
+
const checklist = await input.read.snapshot(PromptChecklist, input.conversationId, ctx);
|
|
106
|
+
const enabled = checklist?.enabled ?? [PROMPTS[0].id];
|
|
107
|
+
const text = PROMPTS.filter((p) => enabled.includes(p.id)).map((p) => p.text);
|
|
108
|
+
return text.length ? text.join("\n\n") : undefined;
|
|
109
|
+
},
|
|
110
|
+
{ tag: false },
|
|
111
|
+
),
|
|
112
|
+
],
|
|
113
|
+
});
|
|
114
|
+
const judge = opts.offline ? judgeOffline : async (command: string) => gate(await askJev({ command }, BASH_QUESTIONS));
|
|
115
|
+
const jev = defineExtension({
|
|
116
|
+
name: "jev-damage-control",
|
|
117
|
+
hooks: [
|
|
118
|
+
hook(ToolTask, {
|
|
119
|
+
beforeTool: async (call, api, ctx) => {
|
|
120
|
+
if (call.name !== "bash") return;
|
|
121
|
+
const command = String((call.arguments as { command?: unknown }).command ?? "");
|
|
122
|
+
let verdict = await api.memo<Verdict>("jev-damage-control", ctx);
|
|
123
|
+
if (!verdict) {
|
|
124
|
+
try {
|
|
125
|
+
verdict = await api.memo<Verdict>("jev-damage-control", await judge(command), ctx);
|
|
126
|
+
} catch {
|
|
127
|
+
return; // Fails open: a Jev outage must not stop bash.
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (verdict.block) return { block: `jev-damage-control blocked this command: ${verdict.reason}` };
|
|
131
|
+
},
|
|
132
|
+
}),
|
|
133
|
+
],
|
|
134
|
+
});
|
|
135
|
+
const all: Record<string, Extension> = { "system-prompts": prompts, "jev-damage-control": jev };
|
|
136
|
+
return { all, list: [prompts, jev] };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export type Addons = ReturnType<typeof createAddons>;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { RequestHandler } from "express";
|
|
2
|
+
import { createHash, timingSafeEqual } from "node:crypto";
|
|
3
|
+
|
|
4
|
+
/** Constant-time check of `Authorization: Bearer <token>`, or `X-DPI-Token` for proxies that strip Authorization. */
|
|
5
|
+
export function bearerAuth(token: string): RequestHandler {
|
|
6
|
+
const expected = createHash("sha256").update(token).digest();
|
|
7
|
+
return (req, res, next) => {
|
|
8
|
+
const header = req.get("authorization") ?? "";
|
|
9
|
+
const presented = header.startsWith("Bearer ") ? header.slice(7).trim() : (req.get("x-dpi-token") ?? "").trim();
|
|
10
|
+
const actual = createHash("sha256").update(presented).digest();
|
|
11
|
+
if (!presented || !timingSafeEqual(actual, expected)) {
|
|
12
|
+
res
|
|
13
|
+
.status(401)
|
|
14
|
+
.set("WWW-Authenticate", 'Bearer realm="dpi"')
|
|
15
|
+
.json({ error: { code: "unauthorized", message: "Missing or invalid device token" } });
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
next();
|
|
19
|
+
};
|
|
20
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// Device configuration: ~/.dpi/backend.env (or $DPI_ENV_FILE) plus CLI flags.
|
|
2
|
+
// Nothing here knows about specific machines; dpi-network.yaml on the operator's laptop
|
|
3
|
+
// writes this env file through `just net up <device>`.
|
|
4
|
+
import { existsSync } from "node:fs";
|
|
5
|
+
import { homedir, hostname } from "node:os";
|
|
6
|
+
import { join, resolve } from "node:path";
|
|
7
|
+
|
|
8
|
+
export interface BackendConfig {
|
|
9
|
+
name: string;
|
|
10
|
+
host: string;
|
|
11
|
+
port: number;
|
|
12
|
+
home: string;
|
|
13
|
+
envFile: string;
|
|
14
|
+
dataDir: string;
|
|
15
|
+
startDirs: string[];
|
|
16
|
+
provider: "openrouter" | "faux";
|
|
17
|
+
token: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const expand = (p: string) => (p === "~" ? homedir() : p.startsWith("~/") ? join(homedir(), p.slice(2)) : p);
|
|
21
|
+
|
|
22
|
+
export function loadConfig(argv = process.argv.slice(2), env = process.env): BackendConfig {
|
|
23
|
+
const flag = (key: string, fallback: string) => {
|
|
24
|
+
const i = argv.indexOf(key);
|
|
25
|
+
if (i < 0) return fallback;
|
|
26
|
+
const value = argv[i + 1];
|
|
27
|
+
if (!value || value.startsWith("--")) throw Error(`${key} needs a value`);
|
|
28
|
+
return value;
|
|
29
|
+
};
|
|
30
|
+
const home = expand(env.DPI_HOME ?? join(homedir(), ".dpi"));
|
|
31
|
+
const envFile = expand(flag("--env-file", env.DPI_ENV_FILE ?? join(home, "backend.env")));
|
|
32
|
+
if (existsSync(envFile)) process.loadEnvFile(envFile);
|
|
33
|
+
const config: BackendConfig = {
|
|
34
|
+
name: flag("--name", env.DPI_DEVICE_NAME ?? hostname().replace(/\.local$/, "")),
|
|
35
|
+
host: flag("--host", env.DPI_HOST ?? "127.0.0.1"),
|
|
36
|
+
port: Number(flag("--port", env.DPI_PORT ?? "4780")),
|
|
37
|
+
home,
|
|
38
|
+
envFile,
|
|
39
|
+
dataDir: resolve(expand(flag("--data-dir", env.DPI_DATA_DIR ?? join(home, "backend")))),
|
|
40
|
+
startDirs: flag("--start-dirs", env.DPI_START_DIRS ?? "~/dpi-work")
|
|
41
|
+
.split(":")
|
|
42
|
+
.filter(Boolean)
|
|
43
|
+
.map((p) => resolve(expand(p))),
|
|
44
|
+
provider: (env.DPI_PROVIDER ?? "openrouter") === "faux" ? "faux" : "openrouter",
|
|
45
|
+
token: env.DPI_TOKEN ?? "",
|
|
46
|
+
};
|
|
47
|
+
if (!Number.isInteger(config.port) || config.port < 1 || config.port > 65535)
|
|
48
|
+
throw Error(`Invalid port: ${config.port}`);
|
|
49
|
+
if (config.token.length < 32)
|
|
50
|
+
throw Error(
|
|
51
|
+
`DPI_TOKEN is missing or shorter than 32 characters (looked in ${envFile}). Create one with: npm run token`,
|
|
52
|
+
);
|
|
53
|
+
return config;
|
|
54
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
// Compile-time proof that pi-durable's agent events still fit the SDK's wire contract, and that the wire
|
|
2
|
+
// contract still carries every field and tag each library variant provides.
|
|
3
|
+
//
|
|
4
|
+
// The positive checks run inside `npm run check` (this file is in tsconfig `include`). The compile-negative
|
|
5
|
+
// runner (`npm run check:contract` → scripts/check-contract.mjs) proves they actually fail when a field, a
|
|
6
|
+
// tag or a nested subshape is removed from a temporary copy of the wire types.
|
|
7
|
+
import type { AgentEvent, SnapshotEvent } from "@earendil-works/pi-durable";
|
|
8
|
+
import type {
|
|
9
|
+
WireAgentEvent,
|
|
10
|
+
WireCompactionFailure,
|
|
11
|
+
WireNestedToolResult,
|
|
12
|
+
WireNestedToolSlot,
|
|
13
|
+
WireSnapshotEvent,
|
|
14
|
+
} from "../../dpi-sdk/src/api.js";
|
|
15
|
+
|
|
16
|
+
/** Forward fit: the library's shape must stay assignable to ours. */
|
|
17
|
+
export const agentEventFits = (e: AgentEvent): WireAgentEvent => e;
|
|
18
|
+
export const snapshotFits = (e: SnapshotEvent): WireSnapshotEvent => e;
|
|
19
|
+
|
|
20
|
+
// ---------------------------------------------------------------- completeness helpers
|
|
21
|
+
|
|
22
|
+
type LibTags = AgentEvent["type"];
|
|
23
|
+
|
|
24
|
+
/** Members of a union whose `type` includes `Tag` — grouped tags like `run_start | run_end` included. */
|
|
25
|
+
type MemberWithTag<U, Tag extends PropertyKey> = U extends { type: infer T }
|
|
26
|
+
? Tag extends T
|
|
27
|
+
? U
|
|
28
|
+
: never
|
|
29
|
+
: never;
|
|
30
|
+
|
|
31
|
+
type LibVariant<Tag extends PropertyKey> = MemberWithTag<AgentEvent, Tag>;
|
|
32
|
+
type WireVariant<Tag extends PropertyKey> = MemberWithTag<WireAgentEvent, Tag>;
|
|
33
|
+
|
|
34
|
+
/** `true` only when the wire variant exists and declares at least every field the library variant has. */
|
|
35
|
+
type WireCovers<Tag extends LibTags> = [WireVariant<Tag>] extends [never]
|
|
36
|
+
? { wireTagMissing: Tag }
|
|
37
|
+
: Exclude<keyof LibVariant<Tag>, keyof WireVariant<Tag>> extends never
|
|
38
|
+
? true
|
|
39
|
+
: { wireFieldsMissing: Exclude<keyof LibVariant<Tag>, keyof WireVariant<Tag>> };
|
|
40
|
+
|
|
41
|
+
/** `true` when every key of `Keys` is a key of `Shape` (`Keys` must be a subset of `keyof Shape`). */
|
|
42
|
+
// The tuple wrapper stops the conditional from distributing over the union, which would mask a missing key.
|
|
43
|
+
type HasKeys<Shape, Keys extends PropertyKey> = [Keys] extends [Extract<Keys, keyof Shape>]
|
|
44
|
+
? true
|
|
45
|
+
: { missingKeys: Exclude<Keys, keyof Shape> };
|
|
46
|
+
|
|
47
|
+
type MustBeRequired<T> = undefined extends T ? { mustBeRequired: true } : true;
|
|
48
|
+
type MustBeOptional<T> = undefined extends T ? true : { mustBeOptional: true };
|
|
49
|
+
|
|
50
|
+
// ---------------------------------------------------------------- every event tag and field
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* One entry per library tag. A new tag in pi-durable breaks this object until the wire type covers it;
|
|
54
|
+
* a removed or renamed wire variant (or a field dropped from one) breaks the corresponding entry.
|
|
55
|
+
*/
|
|
56
|
+
export const eventFieldCoverage: { [K in LibTags]: WireCovers<K> } = {
|
|
57
|
+
snapshot: true,
|
|
58
|
+
run_start: true,
|
|
59
|
+
run_end: true,
|
|
60
|
+
turn_start: true,
|
|
61
|
+
turn_end: true,
|
|
62
|
+
message_start: true,
|
|
63
|
+
message_update: true,
|
|
64
|
+
message_end: true,
|
|
65
|
+
tool_execution_start: true,
|
|
66
|
+
tool_execution_update: true,
|
|
67
|
+
tool_execution_end: true,
|
|
68
|
+
inbox_update: true,
|
|
69
|
+
submission: true,
|
|
70
|
+
auto_retry_start: true,
|
|
71
|
+
auto_retry_end: true,
|
|
72
|
+
deferred_poll: true,
|
|
73
|
+
entry_appended: true,
|
|
74
|
+
agent_changed: true,
|
|
75
|
+
usage_changed: true,
|
|
76
|
+
task_failed: true,
|
|
77
|
+
compaction_start: true,
|
|
78
|
+
compaction_end: true,
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/** The snapshot keeps every field of the library's snapshot, including `nestedTools` and `compactionFailure`. */
|
|
82
|
+
export const snapshotFieldCoverage: Exclude<keyof SnapshotEvent, keyof WireSnapshotEvent> extends never
|
|
83
|
+
? true
|
|
84
|
+
: { snapshotFieldsMissing: Exclude<keyof SnapshotEvent, keyof WireSnapshotEvent> } = true;
|
|
85
|
+
|
|
86
|
+
// ---------------------------------------------------------------- required/optional shape
|
|
87
|
+
|
|
88
|
+
/** `nestedTools` is a required array, so a serializer that drops it cannot compile. */
|
|
89
|
+
export const nestedToolsRequired: MustBeRequired<WireSnapshotEvent["nestedTools"]> = true;
|
|
90
|
+
export const nestedToolsArray: WireSnapshotEvent["nestedTools"] extends readonly WireNestedToolSlot[] ? true : { notArray: true } = true;
|
|
91
|
+
|
|
92
|
+
/** `compactionFailure` and the tool-end `entry`/`result`/`error` fields stay optional. */
|
|
93
|
+
export const compactionFailureOptional: MustBeOptional<WireSnapshotEvent["compactionFailure"]> = true;
|
|
94
|
+
export const toolEndEntryOptional: MustBeOptional<WireVariant<"tool_execution_end">["entry"]> = true;
|
|
95
|
+
export const toolEndResultOptional: MustBeOptional<WireVariant<"tool_execution_end">["result"]> = true;
|
|
96
|
+
export const compactionEndErrorOptional: MustBeOptional<WireVariant<"compaction_end">["error"]> = true;
|
|
97
|
+
|
|
98
|
+
/** The neither-entry-nor-result case stays representable (faulted, orphaned, or an unfinished slot). */
|
|
99
|
+
export const toolEndWithoutResult: WireVariant<"tool_execution_end"> = {
|
|
100
|
+
type: "tool_execution_end",
|
|
101
|
+
toolCallId: "call",
|
|
102
|
+
toolName: "tool",
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/** Tool events carry the call's task and, for a nested call, its parent call and task. */
|
|
106
|
+
export const toolStartMeta: HasKeys<WireVariant<"tool_execution_start">, "taskId" | "parentToolCallId" | "parentTaskId"> = true;
|
|
107
|
+
export const toolUpdateMeta: HasKeys<WireVariant<"tool_execution_update">, "taskId" | "parentToolCallId" | "parentTaskId"> = true;
|
|
108
|
+
export const toolEndMeta: HasKeys<WireVariant<"tool_execution_end">, "taskId" | "parentToolCallId" | "parentTaskId"> = true;
|
|
109
|
+
|
|
110
|
+
// ---------------------------------------------------------------- nested subshapes
|
|
111
|
+
|
|
112
|
+
type LibNestedSlot = SnapshotEvent["nestedTools"][number];
|
|
113
|
+
type LibNestedResult = NonNullable<MemberWithTag<AgentEvent, "tool_execution_end">["result"]>;
|
|
114
|
+
type LibCompactionFailure = NonNullable<SnapshotEvent["compactionFailure"]>;
|
|
115
|
+
|
|
116
|
+
export const nestedSlotShape: HasKeys<WireNestedToolSlot, "callId" | "name" | "status" | "parentCallId" | "parentTaskId" | "taskId" | "arguments" | keyof LibNestedSlot> = true;
|
|
117
|
+
export const nestedResultShape: HasKeys<WireNestedToolResult, "taskId" | "isError" | "diagnostics" | keyof LibNestedResult> = true;
|
|
118
|
+
export const compactionFailureShape: HasKeys<WireCompactionFailure, "taskId" | "reason" | "error" | keyof LibCompactionFailure> = true;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { ErrorRequestHandler } from "express";
|
|
2
|
+
import { ConversationBusy, SessionFailed } from "@earendil-works/pi-durable";
|
|
3
|
+
import type { ErrorCode } from "../../dpi-sdk/src/api.js";
|
|
4
|
+
|
|
5
|
+
const STATUS: Record<ErrorCode, number> = {
|
|
6
|
+
unauthorized: 401,
|
|
7
|
+
not_found: 404,
|
|
8
|
+
invalid: 400,
|
|
9
|
+
busy: 409,
|
|
10
|
+
conflict: 409,
|
|
11
|
+
ambiguous: 409,
|
|
12
|
+
unavailable: 503,
|
|
13
|
+
internal: 500,
|
|
14
|
+
offline: 503,
|
|
15
|
+
rate_limited: 429,
|
|
16
|
+
timeout: 504,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export class ApiFault extends Error {
|
|
20
|
+
readonly status: number;
|
|
21
|
+
constructor(
|
|
22
|
+
readonly code: ErrorCode,
|
|
23
|
+
message: string,
|
|
24
|
+
) {
|
|
25
|
+
super(message);
|
|
26
|
+
this.status = STATUS[code];
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const invalid = (message: string) => new ApiFault("invalid", message);
|
|
31
|
+
export const notFound = (message: string) => new ApiFault("not_found", message);
|
|
32
|
+
|
|
33
|
+
export const errorMiddleware: ErrorRequestHandler = (err, req, res, _next) => {
|
|
34
|
+
let fault: ApiFault;
|
|
35
|
+
if (err instanceof ApiFault) fault = err;
|
|
36
|
+
else if (err instanceof SessionFailed)
|
|
37
|
+
fault = new ApiFault("unavailable", "The session failed and is closed; it is unavailable until reopened");
|
|
38
|
+
else if (err instanceof ConversationBusy) fault = new ApiFault("busy", "The agent is busy and the input asked to be rejected while busy");
|
|
39
|
+
else if (err?.type === "entity.parse.failed") fault = invalid("Body must be valid JSON");
|
|
40
|
+
else if (err?.type === "entity.too.large") fault = invalid("Body too large");
|
|
41
|
+
else if (/App State changed while you were editing/.test(String(err?.message))) fault = new ApiFault("conflict", err.message);
|
|
42
|
+
else if (err instanceof Error && /^(Field|Invalid|Use a JSON Pointer|Increment|The state root|State path|Parent must|Value must|Reserved field|App State must|State is too)/.test(err.message))
|
|
43
|
+
fault = invalid(err.message);
|
|
44
|
+
else {
|
|
45
|
+
console.error(`[dpi-backend] ${req.method} ${req.path}:`, err instanceof Error ? err.stack : err);
|
|
46
|
+
fault = new ApiFault("internal", err instanceof Error ? err.message : "Internal error");
|
|
47
|
+
}
|
|
48
|
+
if (res.headersSent) return void res.end();
|
|
49
|
+
res.status(fault.status).json({ error: { code: fault.code, message: fault.message } });
|
|
50
|
+
};
|