somora 2026.930.1
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/LICENSE +21 -0
- package/README.md +364 -0
- package/bin/node-version.d.mts +3 -0
- package/bin/node-version.mjs +45 -0
- package/bin/somora.mjs +77 -0
- package/config.example.yaml +896 -0
- package/docs/agents.md +677 -0
- package/docs/api.md +3194 -0
- package/docs/browser.md +317 -0
- package/docs/builder.md +331 -0
- package/docs/cache-strategy.md +373 -0
- package/docs/compaction.md +253 -0
- package/docs/display.md +154 -0
- package/docs/dream-phases.md +891 -0
- package/docs/files.md +387 -0
- package/docs/imagegen.md +366 -0
- package/docs/images/somora-hero.png +0 -0
- package/docs/images/somora-mobile-2026-05-23.png +0 -0
- package/docs/images/somora-tui-2026-05-23.png +0 -0
- package/docs/images/somora-web-2026-05-23.png +0 -0
- package/docs/index.md +178 -0
- package/docs/lsp.md +93 -0
- package/docs/mcp.md +332 -0
- package/docs/memory.md +407 -0
- package/docs/mobile.md +310 -0
- package/docs/models.md +379 -0
- package/docs/projects.md +425 -0
- package/docs/realtime-voice.md +293 -0
- package/docs/resources.md +230 -0
- package/docs/sampling.md +128 -0
- package/docs/security.md +189 -0
- package/docs/sentinel.md +295 -0
- package/docs/setup.md +1217 -0
- package/docs/skills.md +501 -0
- package/docs/team.md +189 -0
- package/docs/thinking.md +463 -0
- package/docs/tmux.md +399 -0
- package/docs/tools.md +117 -0
- package/docs/videogen.md +191 -0
- package/docs/voice.md +288 -0
- package/docs/web.md +893 -0
- package/docs/wiki.md +583 -0
- package/npm-shrinkwrap.json +5670 -0
- package/package.json +116 -0
- package/src/attachments/store.ts +196 -0
- package/src/browser/change-stream.ts +31 -0
- package/src/browser/display.ts +108 -0
- package/src/browser/policy.ts +121 -0
- package/src/browser/screencast.ts +374 -0
- package/src/browser/service.ts +1337 -0
- package/src/browser/session.ts +15 -0
- package/src/browser/snapshot.ts +40 -0
- package/src/cli/auth.ts +117 -0
- package/src/cli/codex.ts +41 -0
- package/src/cli/lsp.ts +71 -0
- package/src/cli/resolvers/clawhub.ts +400 -0
- package/src/cli/setup.ts +835 -0
- package/src/cli/skill.ts +619 -0
- package/src/cli/somora.ts +548 -0
- package/src/cli/systemd-unit.ts +72 -0
- package/src/cli/team.ts +148 -0
- package/src/cli/tui/activity-stream.ts +121 -0
- package/src/cli/tui/api.ts +479 -0
- package/src/cli/tui/app.tsx +1169 -0
- package/src/cli/tui/autocomplete.tsx +55 -0
- package/src/cli/tui/commands.ts +913 -0
- package/src/cli/tui/dream-glyphs.ts +22 -0
- package/src/cli/tui/footer.tsx +15 -0
- package/src/cli/tui/format.ts +28 -0
- package/src/cli/tui/header.tsx +279 -0
- package/src/cli/tui/index.tsx +120 -0
- package/src/cli/tui/markdown.tsx +63 -0
- package/src/cli/tui/sampling.ts +162 -0
- package/src/cli/tui/separator.tsx +13 -0
- package/src/cli/tui/state.ts +42 -0
- package/src/cli/tui/stream.ts +330 -0
- package/src/cli/tui/thinking-text.ts +54 -0
- package/src/cli/tui/turn-views.tsx +499 -0
- package/src/cli/tui/types.ts +410 -0
- package/src/cli/tui/work-queue.ts +161 -0
- package/src/cli/update-args.ts +55 -0
- package/src/cli/wiki.ts +406 -0
- package/src/compaction/index.ts +28 -0
- package/src/compaction/midturn.ts +210 -0
- package/src/compaction/policy.ts +154 -0
- package/src/compaction/summarize.ts +691 -0
- package/src/compaction/template.ts +90 -0
- package/src/compaction/types.ts +148 -0
- package/src/config/loader.ts +276 -0
- package/src/config/reload.ts +43 -0
- package/src/config/types.ts +2292 -0
- package/src/dream/deep-actions.ts +324 -0
- package/src/dream/deep-dispatcher.ts +237 -0
- package/src/dream/deep-llm.ts +388 -0
- package/src/dream/deep-prompts.ts +106 -0
- package/src/dream/deep-runner.ts +757 -0
- package/src/dream/deep-skip-cache.ts +199 -0
- package/src/dream/deep-worker.ts +229 -0
- package/src/dream/json-salvage.ts +34 -0
- package/src/dream/judge.ts +188 -0
- package/src/dream/loop-state.ts +286 -0
- package/src/dream/lucid-actions.ts +303 -0
- package/src/dream/lucid-batches.ts +94 -0
- package/src/dream/lucid-prompt.ts +82 -0
- package/src/dream/lucid-runner.ts +744 -0
- package/src/dream/lucid-storage.ts +277 -0
- package/src/dream/lucid-structure.ts +102 -0
- package/src/dream/lucid-types.ts +137 -0
- package/src/dream/lucid-worker.ts +224 -0
- package/src/dream/rem-dedup.ts +502 -0
- package/src/dream/rem-extract.ts +782 -0
- package/src/dream/rem-reset-run.ts +67 -0
- package/src/dream/rem-runner.ts +696 -0
- package/src/dream/rem-worker.ts +527 -0
- package/src/dream/review-window.ts +71 -0
- package/src/dream/scheduler-state.ts +129 -0
- package/src/dream/storage.ts +497 -0
- package/src/dream/types.ts +133 -0
- package/src/dream/wiki-context.ts +133 -0
- package/src/engine/a2a.ts +33 -0
- package/src/engine/availability.ts +63 -0
- package/src/engine/claude-cli.ts +969 -0
- package/src/engine/codex-app-server-client.ts +294 -0
- package/src/engine/codex-bin.ts +49 -0
- package/src/engine/codex-cli.ts +1014 -0
- package/src/engine/codex-dynamic-tools.ts +239 -0
- package/src/engine/codex-home.ts +63 -0
- package/src/engine/codex-thread-config.ts +74 -0
- package/src/engine/context-budget.ts +257 -0
- package/src/engine/engine-meta-labels.ts +211 -0
- package/src/engine/grok-cli.ts +941 -0
- package/src/engine/inline-think.ts +59 -0
- package/src/engine/model-availability.ts +105 -0
- package/src/engine/openai-compatible.ts +2464 -0
- package/src/engine/reasoning-retry.ts +83 -0
- package/src/engine/registry.ts +13 -0
- package/src/engine/replay.ts +195 -0
- package/src/engine/sampling.ts +90 -0
- package/src/engine/thinking-params.ts +264 -0
- package/src/engine/types.ts +269 -0
- package/src/engine/user-tag.ts +24 -0
- package/src/imagegen/aspect.ts +128 -0
- package/src/imagegen/generate.ts +642 -0
- package/src/imagegen/references.ts +75 -0
- package/src/imagegen/types.ts +154 -0
- package/src/lsp/client.ts +183 -0
- package/src/lsp/fake-server.helper.mjs +42 -0
- package/src/lsp/format.ts +29 -0
- package/src/lsp/index.ts +21 -0
- package/src/lsp/manager.ts +238 -0
- package/src/lsp/registry.ts +151 -0
- package/src/mcp/config.ts +117 -0
- package/src/mcp/hub/audit.ts +55 -0
- package/src/mcp/hub/bridge.ts +115 -0
- package/src/mcp/hub/catalog.ts +69 -0
- package/src/mcp/hub/credentials.ts +406 -0
- package/src/mcp/hub/env-expand.ts +14 -0
- package/src/mcp/hub/manager.ts +770 -0
- package/src/mcp/hub/normalize.ts +213 -0
- package/src/mcp/server.ts +368 -0
- package/src/mcp/tool-schema.ts +20 -0
- package/src/media/capabilities.ts +392 -0
- package/src/media/records.ts +175 -0
- package/src/media/store.ts +217 -0
- package/src/media/types.ts +69 -0
- package/src/memory/chunking.ts +168 -0
- package/src/memory/embeddings.ts +262 -0
- package/src/memory/index.ts +15 -0
- package/src/memory/inject.ts +197 -0
- package/src/memory/manager.ts +996 -0
- package/src/memory/registry.ts +176 -0
- package/src/memory/rescan.ts +101 -0
- package/src/memory/retrieval.ts +586 -0
- package/src/memory/shared-index.ts +372 -0
- package/src/memory/slug.ts +39 -0
- package/src/memory/storage.ts +385 -0
- package/src/memory/watcher.ts +134 -0
- package/src/multimodal/blocks.ts +101 -0
- package/src/multimodal/dimensions.ts +214 -0
- package/src/multimodal/load.ts +86 -0
- package/src/multimodal/mime.ts +135 -0
- package/src/multimodal/pdf-render.ts +80 -0
- package/src/multimodal/user-content.ts +266 -0
- package/src/persona/files.ts +144 -0
- package/src/persona/loader.ts +598 -0
- package/src/persona/skill-gating-store.ts +59 -0
- package/src/persona/tool-gating-store.ts +113 -0
- package/src/projects/focus.ts +186 -0
- package/src/projects/prompt-block.ts +106 -0
- package/src/projects/scheme.ts +107 -0
- package/src/projects/store.ts +144 -0
- package/src/projects/types.ts +73 -0
- package/src/sentinel/dispatcher.ts +97 -0
- package/src/sentinel/schedule.ts +271 -0
- package/src/sentinel/scheduler.ts +840 -0
- package/src/sentinel/store.ts +259 -0
- package/src/sentinel/types.ts +221 -0
- package/src/server/activity-stream.ts +160 -0
- package/src/server/ask-calls.ts +137 -0
- package/src/server/ask-wait-graph.ts +171 -0
- package/src/server/async-tasks.ts +199 -0
- package/src/server/builder-busy.ts +86 -0
- package/src/server/builder-prompt.ts +204 -0
- package/src/server/builder-questions.ts +117 -0
- package/src/server/builder-session.ts +249 -0
- package/src/server/chat-aborts.ts +83 -0
- package/src/server/claude-config-dir.ts +71 -0
- package/src/server/claude-credential-sync.ts +454 -0
- package/src/server/env-file.ts +100 -0
- package/src/server/env.ts +115 -0
- package/src/server/heal-session.ts +151 -0
- package/src/server/history-page.ts +46 -0
- package/src/server/index.ts +7244 -0
- package/src/server/lockfile.ts +89 -0
- package/src/server/logger.ts +67 -0
- package/src/server/logs.ts +187 -0
- package/src/server/loopback-fetch.ts +130 -0
- package/src/server/openai-client.ts +89 -0
- package/src/server/prompt-assembly.ts +334 -0
- package/src/server/restart-reconcile.ts +139 -0
- package/src/server/run-turn-fallback.ts +275 -0
- package/src/server/run-turn-types.ts +91 -0
- package/src/server/run-turn.ts +1474 -0
- package/src/server/sanitize-assistant-text.ts +112 -0
- package/src/server/session-export.ts +220 -0
- package/src/server/session-queue.ts +376 -0
- package/src/server/session-workdir.ts +49 -0
- package/src/server/sse-h2-safe.ts +61 -0
- package/src/server/sse-liveness.ts +162 -0
- package/src/server/sse-serializer.ts +161 -0
- package/src/server/start-turn.ts +249 -0
- package/src/server/steer-inbox.ts +107 -0
- package/src/server/tls-keeper.ts +153 -0
- package/src/server/tool-format.ts +167 -0
- package/src/server/turn-framing.ts +52 -0
- package/src/server/turn-origin-kind.ts +74 -0
- package/src/server/turn-origin.ts +40 -0
- package/src/server/work-ledger.ts +975 -0
- package/src/server/workspace.ts +114 -0
- package/src/setup/config-edit.ts +201 -0
- package/src/setup/presets.ts +71 -0
- package/src/setup/prompt.ts +163 -0
- package/src/setup/tailscale.ts +44 -0
- package/src/skills/bin-checks.ts +227 -0
- package/src/skills/bootstrap.ts +347 -0
- package/src/skills/env-scope.ts +175 -0
- package/src/skills/gating.ts +71 -0
- package/src/skills/lint.ts +202 -0
- package/src/skills/load.ts +368 -0
- package/src/skills/path-helpers.ts +102 -0
- package/src/skills/registry.ts +178 -0
- package/src/ssh/exec.ts +142 -0
- package/src/ssh/index.ts +12 -0
- package/src/ssh/known-hosts.ts +109 -0
- package/src/ssh/pool.ts +307 -0
- package/src/storage/sessions.ts +602 -0
- package/src/team/render.ts +149 -0
- package/src/team/resolve.ts +122 -0
- package/src/team/store.ts +124 -0
- package/src/team/types.ts +111 -0
- package/src/team/write.ts +95 -0
- package/src/testing/isolated-home.mts +46 -0
- package/src/tmux/attention-watcher.ts +272 -0
- package/src/tmux/attention.ts +336 -0
- package/src/tmux/list.ts +91 -0
- package/src/tmux/origin-store.ts +102 -0
- package/src/tmux/tui-markers.ts +267 -0
- package/src/tools/agents/ask-cancel.ts +94 -0
- package/src/tools/agents/ask-result.ts +235 -0
- package/src/tools/agents/ask.ts +704 -0
- package/src/tools/agents/builder-dispatch.ts +217 -0
- package/src/tools/agents/images.ts +104 -0
- package/src/tools/agents/index.ts +42 -0
- package/src/tools/agents/long-task-timeouts.ts +39 -0
- package/src/tools/agents/session-list.ts +136 -0
- package/src/tools/agents/session-model.ts +106 -0
- package/src/tools/agents/spawn.ts +845 -0
- package/src/tools/agents/status.ts +589 -0
- package/src/tools/browser/index.ts +6 -0
- package/src/tools/browser/ops.ts +145 -0
- package/src/tools/browser/tools.ts +184 -0
- package/src/tools/builder/short-descriptions.ts +80 -0
- package/src/tools/builder/tools.ts +222 -0
- package/src/tools/docs/index.ts +1 -0
- package/src/tools/docs/tools.ts +228 -0
- package/src/tools/dream/index.ts +11 -0
- package/src/tools/dream/tools.ts +1002 -0
- package/src/tools/exec/allowlist.ts +233 -0
- package/src/tools/exec/background-detach.helper.mts +27 -0
- package/src/tools/exec/blacklist.ts +336 -0
- package/src/tools/exec/concurrency.ts +150 -0
- package/src/tools/exec/fit-output.ts +82 -0
- package/src/tools/exec/index.ts +7 -0
- package/src/tools/exec/internal-env.ts +89 -0
- package/src/tools/exec/job-store.ts +403 -0
- package/src/tools/exec/local.ts +649 -0
- package/src/tools/exec/remote.ts +399 -0
- package/src/tools/exec/tools.ts +934 -0
- package/src/tools/file/analyze.ts +378 -0
- package/src/tools/file/index.ts +2 -0
- package/src/tools/file/local.ts +707 -0
- package/src/tools/file/lsp-hook.ts +72 -0
- package/src/tools/file/persona-guard.ts +61 -0
- package/src/tools/file/policy.ts +292 -0
- package/src/tools/file/read-format.ts +129 -0
- package/src/tools/file/remote.ts +680 -0
- package/src/tools/file/replace.ts +507 -0
- package/src/tools/file/search-window.ts +224 -0
- package/src/tools/file/tools.ts +658 -0
- package/src/tools/file/write-scope.ts +182 -0
- package/src/tools/gating.ts +106 -0
- package/src/tools/image/budget.ts +82 -0
- package/src/tools/image/index.ts +7 -0
- package/src/tools/image/tools.ts +601 -0
- package/src/tools/index.ts +99 -0
- package/src/tools/memory/index.ts +12 -0
- package/src/tools/memory/tools.ts +399 -0
- package/src/tools/projects/index.ts +1 -0
- package/src/tools/projects/tools.ts +643 -0
- package/src/tools/registry.ts +427 -0
- package/src/tools/resources/index.ts +8 -0
- package/src/tools/resources/tools.ts +176 -0
- package/src/tools/resources/visibility.ts +60 -0
- package/src/tools/sentinel/index.ts +1 -0
- package/src/tools/sentinel/tools.ts +592 -0
- package/src/tools/skill/index.ts +4 -0
- package/src/tools/skill/tools.ts +285 -0
- package/src/tools/time/index.ts +5 -0
- package/src/tools/time/tools.ts +123 -0
- package/src/tools/tmux/index.ts +1 -0
- package/src/tools/tmux/local.ts +277 -0
- package/src/tools/tmux/remote.ts +168 -0
- package/src/tools/tmux/tools.ts +1064 -0
- package/src/tools/tool-output.ts +97 -0
- package/src/tools/types.ts +227 -0
- package/src/tools/video/index.ts +1 -0
- package/src/tools/video/tools.ts +425 -0
- package/src/tools/web/fetch.ts +337 -0
- package/src/tools/web/index.ts +4 -0
- package/src/tools/web/tools.ts +186 -0
- package/src/tools/wiki/index.ts +1 -0
- package/src/tools/wiki/tools.ts +445 -0
- package/src/tts/cache-gc.ts +128 -0
- package/src/tts/prepare-for-tts.ts +114 -0
- package/src/tts/service.ts +332 -0
- package/src/types/events.ts +695 -0
- package/src/types/turn-origin.ts +53 -0
- package/src/version.ts +21 -0
- package/src/videogen/dialects.ts +222 -0
- package/src/videogen/generate.ts +484 -0
- package/src/videogen/jobs.ts +178 -0
- package/src/videogen/runner.ts +125 -0
- package/src/videogen/wake.ts +119 -0
- package/src/voice/realtime/call.ts +948 -0
- package/src/voice/realtime/consult.ts +293 -0
- package/src/voice/realtime/fake-provider.ts +134 -0
- package/src/voice/realtime/manager.ts +384 -0
- package/src/voice/realtime/openai-provider.ts +471 -0
- package/src/voice/realtime/persona.ts +227 -0
- package/src/voice/realtime/session-match.ts +116 -0
- package/src/voice/realtime/types.ts +182 -0
- package/src/wiki/conflict.ts +113 -0
- package/src/wiki/explorer.ts +578 -0
- package/src/wiki/index-builder.ts +172 -0
- package/src/wiki/language.ts +134 -0
- package/src/wiki/log-builder.ts +150 -0
- package/src/wiki/map.ts +258 -0
- package/src/wiki/migration/analyze.ts +304 -0
- package/src/wiki/migration/execute.ts +689 -0
- package/src/wiki/migration/refine.ts +402 -0
- package/src/wiki/migration/store.ts +100 -0
- package/src/wiki/move-page.ts +57 -0
- package/src/wiki/structure-file.ts +192 -0
- package/src/wiki/taxonomy.ts +140 -0
- package/src/wiki/templates.ts +163 -0
- package/src/wiki/types.ts +133 -0
- package/templates/skills/README.md +24 -0
- package/templates/skills/_templates/api-wrapper/SKILL.md +26 -0
- package/templates/skills/_templates/cli-wrapper/BOOTSTRAP.md +32 -0
- package/templates/skills/_templates/cli-wrapper/SKILL.md +24 -0
- package/templates/skills/_templates/default/SKILL.md +25 -0
- package/templates/skills/builder-handover/SKILL.md +96 -0
- package/templates/skills/builder-handover/assets/ORDER.md +12 -0
- package/templates/skills/skill-author/SKILL.md +110 -0
- package/tsconfig.json +22 -0
- package/web/dist/assets/index-BaVv6pB2.js +631 -0
- package/web/dist/assets/index-BaVv6pB2.js.map +1 -0
- package/web/dist/assets/index-V3FV3Z25.css +32 -0
- package/web/dist/favicon.svg +8 -0
- package/web/dist/index.html +20 -0
- package/web/package.json +36 -0
- package/web/tsconfig.json +25 -0
- package/web-mobile/dist/apple-touch-icon.png +0 -0
- package/web-mobile/dist/assets/index-BqKt7IMx.css +1 -0
- package/web-mobile/dist/assets/index-X_vRr6cd.js +88 -0
- package/web-mobile/dist/assets/index-X_vRr6cd.js.map +1 -0
- package/web-mobile/dist/favicon.svg +8 -0
- package/web-mobile/dist/icon-192.png +0 -0
- package/web-mobile/dist/icon-512.png +0 -0
- package/web-mobile/dist/index.html +27 -0
- package/web-mobile/dist/manifest.webmanifest +32 -0
- package/web-mobile/dist/service-worker.js +90 -0
- package/web-mobile/package.json +28 -0
- package/web-mobile/tsconfig.json +25 -0
|
@@ -0,0 +1,592 @@
|
|
|
1
|
+
// `sentinel` tool — single action-enum entry point for the trigger
|
|
2
|
+
// runtime. Lets an agent install, list, inspect, test, pause/resume,
|
|
3
|
+
// delete its own triggers, and look at fire history. All actions go
|
|
4
|
+
// through the in-process sentinel store + scheduler — no HTTP.
|
|
5
|
+
//
|
|
6
|
+
// Safeguards are enforced HERE (in the tool handler) AND in the
|
|
7
|
+
// scheduler (defense-in-depth):
|
|
8
|
+
// - Minimum interval 60s (sentinel global floor)
|
|
9
|
+
// - Per-agent active trigger cap (default 50)
|
|
10
|
+
// - Per-trigger daily-fire cap (default 500)
|
|
11
|
+
// Plus: the scheduler auto-pauses on error-streak ≥ 3 and on daily-cap
|
|
12
|
+
// breach.
|
|
13
|
+
|
|
14
|
+
import { z } from 'zod';
|
|
15
|
+
import { logger } from '../../server/logger.ts';
|
|
16
|
+
import type { ToolDefinition } from '../types.ts';
|
|
17
|
+
import {
|
|
18
|
+
deleteTrigger,
|
|
19
|
+
getHistory,
|
|
20
|
+
getTrigger,
|
|
21
|
+
listTriggers,
|
|
22
|
+
loadTriggers,
|
|
23
|
+
saveTrigger,
|
|
24
|
+
countTriggersByAgent,
|
|
25
|
+
} from '../../sentinel/store.ts';
|
|
26
|
+
import {
|
|
27
|
+
dispatchTestFire,
|
|
28
|
+
reschedule,
|
|
29
|
+
} from '../../sentinel/scheduler.ts';
|
|
30
|
+
import {
|
|
31
|
+
computeNextFire,
|
|
32
|
+
describeTimeSpec,
|
|
33
|
+
parseInterval,
|
|
34
|
+
validateTimeSpec,
|
|
35
|
+
} from '../../sentinel/schedule.ts';
|
|
36
|
+
import {
|
|
37
|
+
SENTINEL_LIMITS,
|
|
38
|
+
type Source,
|
|
39
|
+
type Trigger,
|
|
40
|
+
type TriggerStatus,
|
|
41
|
+
} from '../../sentinel/types.ts';
|
|
42
|
+
|
|
43
|
+
// ─────────────────────────────────────────────────────────────────────
|
|
44
|
+
// Input schema
|
|
45
|
+
// ─────────────────────────────────────────────────────────────────────
|
|
46
|
+
|
|
47
|
+
// Per-action input fields are unioned into one wide schema. We can't
|
|
48
|
+
// use Zod discriminatedUnion for the JSON-Schema export because the
|
|
49
|
+
// model sees only the JSON (lesson: [[feedback_json_schema_discriminated_ops]]),
|
|
50
|
+
// so we use the standard "all fields optional, validate in handler"
|
|
51
|
+
// shape and document per-action requirements in the description.
|
|
52
|
+
|
|
53
|
+
const TimeSpecSchema = z.union([
|
|
54
|
+
z.object({ type: z.literal('at'), iso: z.string() }),
|
|
55
|
+
z.object({ type: z.literal('every'), interval: z.string() }),
|
|
56
|
+
z.object({ type: z.literal('daily'), time: z.string() }),
|
|
57
|
+
z.object({
|
|
58
|
+
type: z.literal('weekly'),
|
|
59
|
+
day: z.enum(['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']),
|
|
60
|
+
time: z.string(),
|
|
61
|
+
}),
|
|
62
|
+
z.object({ type: z.literal('cron'), expression: z.string() }),
|
|
63
|
+
]);
|
|
64
|
+
|
|
65
|
+
const SourceSchema = z.object({
|
|
66
|
+
type: z.literal('time'),
|
|
67
|
+
spec: TimeSpecSchema,
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
const DispatchSchema = z.object({
|
|
71
|
+
agent: z.string().min(1),
|
|
72
|
+
// Optional here (not `.default('main')`): left out means "the
|
|
73
|
+
// session I am in" for a trigger on yourself — resolved in the handler.
|
|
74
|
+
session: z.string().min(1).optional(),
|
|
75
|
+
prompt: z.string().min(1),
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
const PolicySchema = z.object({
|
|
79
|
+
cooldownMs: z.number().int().min(0).optional(),
|
|
80
|
+
maxFiresPerDay: z.number().int().min(1).max(SENTINEL_LIMITS.MAX_FIRES_PER_DAY).optional(),
|
|
81
|
+
missedFiresPolicy: z.enum(['skip', 'catchUpOnce', 'catchUpAll']).optional(),
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
const SentinelInput = z
|
|
85
|
+
.object({
|
|
86
|
+
action: z.enum([
|
|
87
|
+
'create', 'list', 'get', 'pause', 'resume', 'delete', 'test', 'history', 'purge_completed',
|
|
88
|
+
]),
|
|
89
|
+
// create
|
|
90
|
+
name: z.string().min(1).max(100).optional(),
|
|
91
|
+
intent: z.string().max(500).optional(),
|
|
92
|
+
source: SourceSchema.optional(),
|
|
93
|
+
dispatch: DispatchSchema.optional(),
|
|
94
|
+
policy: PolicySchema.optional(),
|
|
95
|
+
// get / pause / resume / delete / test / history
|
|
96
|
+
id: z.string().min(1).optional(),
|
|
97
|
+
// list (owner also filters purge_completed)
|
|
98
|
+
owner: z.string().optional(),
|
|
99
|
+
status: z.enum(['active', 'paused', 'error', 'completed']).optional(),
|
|
100
|
+
include_completed: z.boolean().optional(),
|
|
101
|
+
// history
|
|
102
|
+
limit: z.number().int().min(1).max(200).default(50).optional(),
|
|
103
|
+
})
|
|
104
|
+
.strict();
|
|
105
|
+
|
|
106
|
+
type SentinelInputT = z.infer<typeof SentinelInput>;
|
|
107
|
+
|
|
108
|
+
// ─────────────────────────────────────────────────────────────────────
|
|
109
|
+
// Result types (loose union — handler narrows)
|
|
110
|
+
// ─────────────────────────────────────────────────────────────────────
|
|
111
|
+
|
|
112
|
+
type SentinelResult =
|
|
113
|
+
| { action: 'create'; ok: true; trigger: Trigger; hint: string; /** Set when the session was left out and the trigger was bound to the caller's non-main session. */ session_note?: string }
|
|
114
|
+
| { action: 'create'; ok: false; error: string }
|
|
115
|
+
| {
|
|
116
|
+
action: 'list';
|
|
117
|
+
ok: true;
|
|
118
|
+
count: number;
|
|
119
|
+
triggers: Array<TriggerSummary>;
|
|
120
|
+
/** Fired one-shots hidden from this listing (no status filter, no include_completed). */
|
|
121
|
+
hidden_completed?: number;
|
|
122
|
+
hint?: string;
|
|
123
|
+
}
|
|
124
|
+
| { action: 'purge_completed'; ok: true; count: number; ids: string[] }
|
|
125
|
+
| { action: 'get'; ok: true; trigger: Trigger; describe: string }
|
|
126
|
+
| { action: 'get'; ok: false; error: string }
|
|
127
|
+
| { action: 'pause' | 'resume' | 'delete'; ok: boolean; id: string; error?: string }
|
|
128
|
+
| { action: 'test'; ok: boolean; id: string; hint?: string; error?: string }
|
|
129
|
+
| { action: 'history'; ok: true; id: string; count: number; entries: Awaited<ReturnType<typeof getHistory>> }
|
|
130
|
+
| { action: 'history'; ok: false; id: string; error: string };
|
|
131
|
+
|
|
132
|
+
interface TriggerSummary {
|
|
133
|
+
id: string;
|
|
134
|
+
name: string;
|
|
135
|
+
ownerAgent: string;
|
|
136
|
+
source: string;
|
|
137
|
+
status: TriggerStatus;
|
|
138
|
+
nextFireAt?: string;
|
|
139
|
+
lastFireAt?: string;
|
|
140
|
+
fireCount: number;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function summarize(t: Trigger): TriggerSummary {
|
|
144
|
+
return {
|
|
145
|
+
id: t.id,
|
|
146
|
+
name: t.name,
|
|
147
|
+
ownerAgent: t.ownerAgent,
|
|
148
|
+
source: t.source.type === 'time' ? describeTimeSpec(t.source.spec) : t.source.type,
|
|
149
|
+
status: t.status,
|
|
150
|
+
...(t.nextFireAt ? { nextFireAt: t.nextFireAt } : {}),
|
|
151
|
+
...(t.lastFireAt ? { lastFireAt: t.lastFireAt } : {}),
|
|
152
|
+
fireCount: t.fireCount,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// ─────────────────────────────────────────────────────────────────────
|
|
157
|
+
// Helpers
|
|
158
|
+
// ─────────────────────────────────────────────────────────────────────
|
|
159
|
+
|
|
160
|
+
function slugify(s: string): string {
|
|
161
|
+
return s
|
|
162
|
+
.toLowerCase()
|
|
163
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
164
|
+
.replace(/^-+|-+$/g, '')
|
|
165
|
+
.slice(0, 40) || 'trigger';
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function newTriggerId(name: string): string {
|
|
169
|
+
const rand = Math.random().toString(36).slice(2, 6);
|
|
170
|
+
return `${slugify(name)}-${rand}`;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function validateCreateInput(input: SentinelInputT): { ok: true; data: Required<Pick<SentinelInputT, 'name' | 'source' | 'dispatch'>> } | { ok: false; error: string } {
|
|
174
|
+
if (!input.name) return { ok: false, error: 'create: name is required' };
|
|
175
|
+
if (!input.source) return { ok: false, error: 'create: source is required' };
|
|
176
|
+
if (!input.dispatch) return { ok: false, error: 'create: dispatch (agent + prompt) is required' };
|
|
177
|
+
return { ok: true, data: { name: input.name, source: input.source, dispatch: input.dispatch } };
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function enforceLimits(source: Source, policy?: SentinelInputT['policy']): string | null {
|
|
181
|
+
if (source.type !== 'time') return null;
|
|
182
|
+
const spec = source.spec;
|
|
183
|
+
// Minimum-interval enforcement: 'every' triggers can't be < 60s.
|
|
184
|
+
if (spec.type === 'every') {
|
|
185
|
+
try {
|
|
186
|
+
const ms = parseInterval(spec.interval);
|
|
187
|
+
if (ms < SENTINEL_LIMITS.MIN_INTERVAL_MS) {
|
|
188
|
+
return `interval ${spec.interval} below minimum ${SENTINEL_LIMITS.MIN_INTERVAL_MS / 1000}s`;
|
|
189
|
+
}
|
|
190
|
+
} catch (err) {
|
|
191
|
+
return `invalid interval: ${(err as Error).message}`;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
// Cooldown floor: respect global minimum even if user passed lower.
|
|
195
|
+
if (policy?.cooldownMs !== undefined && policy.cooldownMs < SENTINEL_LIMITS.MIN_INTERVAL_MS) {
|
|
196
|
+
// Don't reject — silently raise to minimum (this is a sanity floor,
|
|
197
|
+
// not user input that should fail loud). We log it.
|
|
198
|
+
}
|
|
199
|
+
return null;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// ─────────────────────────────────────────────────────────────────────
|
|
203
|
+
// Tool
|
|
204
|
+
// ─────────────────────────────────────────────────────────────────────
|
|
205
|
+
|
|
206
|
+
export const sentinel: ToolDefinition<SentinelInputT, SentinelResult> = {
|
|
207
|
+
name: 'sentinel',
|
|
208
|
+
toolset: 'sentinel',
|
|
209
|
+
description:
|
|
210
|
+
'Install and manage TIME-based proactive triggers that wake an agent on a schedule. Use this ' +
|
|
211
|
+
'when the user says things like "erinner mich morgen 10 Uhr", "check meine mails jeden morgen ' +
|
|
212
|
+
'8 Uhr", "scan einmal die woche…", "wenn etwas in X passiert, lass mich Y machen" (where Y ' +
|
|
213
|
+
'is recurring/scheduled — for HTTP/exec event sources see Phase 2). ' +
|
|
214
|
+
'\n\n' +
|
|
215
|
+
'Triggers fire by sending the dispatched agent a turn with a structured [Sentinel trigger ' +
|
|
216
|
+
'fired] evidence-block prefix so the agent knows it was woken (not user-asked) and has full ' +
|
|
217
|
+
'context: trigger name, source, fire-time, original user intent. ' +
|
|
218
|
+
'\n\n' +
|
|
219
|
+
'Actions: ' +
|
|
220
|
+
'"create" (name + source + dispatch + optional policy → registered trigger), ' +
|
|
221
|
+
'"list" (optional owner / status filter; fired one-shots are hidden unless status:"completed" ' +
|
|
222
|
+
'or include_completed:true — the count is reported as hidden_completed), ' +
|
|
223
|
+
'"get" (full detail by id), ' +
|
|
224
|
+
'"pause" / "resume" (toggle active/paused by id), ' +
|
|
225
|
+
'"delete" (remove + clean history), ' +
|
|
226
|
+
'"test" (fire NOW, bypass cooldown/cap, mark as testMode in history), ' +
|
|
227
|
+
'"history" (last N fire-events for a trigger), ' +
|
|
228
|
+
'"purge_completed" (delete ALL fired one-shots at once, optional owner filter — recurring, ' +
|
|
229
|
+
'active, paused and errored triggers are never touched). Fired one-shots are also ' +
|
|
230
|
+
'auto-deleted after sentinel.completedRetentionDays (default 7). ' +
|
|
231
|
+
'\n\n' +
|
|
232
|
+
'Source variants (all under type:"time"): ' +
|
|
233
|
+
'`at` (one-shot ISO timestamp), `every` (interval like "5m", min 60s), `daily` ("08:00"), ' +
|
|
234
|
+
'`weekly` (mon/tue/…/sun + "09:00"), `cron` (standard 5-field — escape hatch for things ' +
|
|
235
|
+
'like "1st of each month"). Cron examples: `"0 9 1 * *"` (monthly 1st 9:00), ' +
|
|
236
|
+
'`"0 8 * * *"` (daily 8:00), `"*/15 * * * *"` (every 15 min). Cron does NOT support ' +
|
|
237
|
+
'ranges (use `1,2,3,4,5` not `1-5`) or named months/weekdays. Prefer the named ' +
|
|
238
|
+
'primitives when they fit — they read better. ' +
|
|
239
|
+
'\n\n' +
|
|
240
|
+
'Safeguards: minimum interval 60s (no sub-minute polling), max 50 active triggers per agent, ' +
|
|
241
|
+
'max 500 fires per trigger per day (auto-pause when hit), 3-strikes auto-pause on consecutive ' +
|
|
242
|
+
'errors. Visible + manageable in the web-UI sentinel tab.',
|
|
243
|
+
inputSchema: SentinelInput,
|
|
244
|
+
jsonSchema: {
|
|
245
|
+
type: 'object',
|
|
246
|
+
properties: {
|
|
247
|
+
action: {
|
|
248
|
+
type: 'string',
|
|
249
|
+
enum: ['create', 'list', 'get', 'pause', 'resume', 'delete', 'test', 'history', 'purge_completed'],
|
|
250
|
+
},
|
|
251
|
+
// create
|
|
252
|
+
name: { type: 'string', maxLength: 100, description: 'create only — human label.' },
|
|
253
|
+
intent: { type: 'string', maxLength: 500, description: 'create only — short user-intent summary echoed to the agent at fire-time.' },
|
|
254
|
+
source: {
|
|
255
|
+
type: 'object',
|
|
256
|
+
description: 'create only — where events come from. Phase 1: type:"time" with one of the spec variants.',
|
|
257
|
+
properties: {
|
|
258
|
+
type: { type: 'string', enum: ['time'] },
|
|
259
|
+
spec: {
|
|
260
|
+
description: 'Discriminated by spec.type. See examples.',
|
|
261
|
+
oneOf: [
|
|
262
|
+
{ type: 'object', properties: { type: { const: 'at' }, iso: { type: 'string' } }, required: ['type', 'iso'] },
|
|
263
|
+
{ type: 'object', properties: { type: { const: 'every' }, interval: { type: 'string', description: '30s / 5m / 1h. Minimum 60s.' } }, required: ['type', 'interval'] },
|
|
264
|
+
{ type: 'object', properties: { type: { const: 'daily' }, time: { type: 'string', description: 'HH:MM or HH:MM:SS.' } }, required: ['type', 'time'] },
|
|
265
|
+
{ type: 'object', properties: { type: { const: 'weekly' }, day: { type: 'string', enum: ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'] }, time: { type: 'string' } }, required: ['type', 'day', 'time'] },
|
|
266
|
+
{ type: 'object', properties: { type: { const: 'cron' }, expression: { type: 'string', description: 'Standard 5-field cron: m h dom mon dow. Supports *, N, */N, a,b,c. Does NOT support ranges (use 1,2,3,4,5 instead of 1-5) or named months/weekdays (use 1=Mon..7=Sun). Examples: "0 8 * * *" daily at 8:00 · "0 9 1 * *" 1st of each month 9:00 · "*/15 * * * *" every 15 min · "0 9,17 * * *" 9:00 AND 17:00 daily · "0 0 * * 0" sundays midnight. For simple cases prefer `daily`/`weekly`/`every` — they read better and cover 90% of recurring use-cases.' } }, required: ['type', 'expression'] },
|
|
267
|
+
],
|
|
268
|
+
},
|
|
269
|
+
},
|
|
270
|
+
required: ['type', 'spec'],
|
|
271
|
+
},
|
|
272
|
+
dispatch: {
|
|
273
|
+
type: 'object',
|
|
274
|
+
description: 'create only — which agent wakes up in which session with what prompt.',
|
|
275
|
+
properties: {
|
|
276
|
+
agent: { type: 'string', description: 'Target agent. Must exist.' },
|
|
277
|
+
session: {
|
|
278
|
+
type: 'string',
|
|
279
|
+
description:
|
|
280
|
+
'Target session: a slug or id, "main", or "current" = the session you are in right now ' +
|
|
281
|
+
'(only when dispatch.agent is you). Left out: a trigger on yourself fires in the session you ' +
|
|
282
|
+
'create it from (as a sub-agent: in "main"); a trigger on another agent fires in their "main". Say "main" explicitly when a ' +
|
|
283
|
+
'recurring job belongs there rather than in the conversation you happen to be in. ' +
|
|
284
|
+
'Auto-created with timestamp prefix if not existing.',
|
|
285
|
+
},
|
|
286
|
+
prompt: { type: 'string', description: 'Agent receives this as a user-turn body, prefixed by a structured evidence block.' },
|
|
287
|
+
},
|
|
288
|
+
required: ['agent', 'prompt'],
|
|
289
|
+
},
|
|
290
|
+
policy: {
|
|
291
|
+
type: 'object',
|
|
292
|
+
properties: {
|
|
293
|
+
cooldownMs: { type: 'integer', minimum: 0, description: 'Minimum ms between fires.' },
|
|
294
|
+
maxFiresPerDay: { type: 'integer', minimum: 1, maximum: SENTINEL_LIMITS.MAX_FIRES_PER_DAY },
|
|
295
|
+
missedFiresPolicy: {
|
|
296
|
+
type: 'string',
|
|
297
|
+
enum: ['skip', 'catchUpOnce', 'catchUpAll'],
|
|
298
|
+
description:
|
|
299
|
+
'How to handle recurring fires that elapsed while somora was down. ' +
|
|
300
|
+
'`skip` (default): no backfill — best for "daily inbox check" where stacked ' +
|
|
301
|
+
'fires after an outage are spam. `catchUpOnce`: fire one historical instance ' +
|
|
302
|
+
'with catchUp:true in the evidence block — best for "monthly summary" where ' +
|
|
303
|
+
'you want to know it was missed but not get N separate fires. `catchUpAll`: ' +
|
|
304
|
+
'fire one per missed instant (capped at 24) — best for log-style triggers ' +
|
|
305
|
+
'where each instant carries unique context. One-shot `at`-triggers use a 6h ' +
|
|
306
|
+
'grace window separately, this flag only affects recurring sources.',
|
|
307
|
+
},
|
|
308
|
+
},
|
|
309
|
+
},
|
|
310
|
+
// get / pause / resume / delete / test / history
|
|
311
|
+
id: { type: 'string', description: 'Trigger id — required for get/pause/resume/delete/test/history.' },
|
|
312
|
+
// list
|
|
313
|
+
owner: { type: 'string', description: 'list / purge_completed — filter by owner-agent.' },
|
|
314
|
+
status: { type: 'string', enum: ['active', 'paused', 'error', 'completed'], description: 'list only — filter by status.' },
|
|
315
|
+
include_completed: { type: 'boolean', description: 'list only — also show fired one-shots (hidden by default).' },
|
|
316
|
+
limit: { type: 'integer', minimum: 1, maximum: 200, default: 50, description: 'history only — newest-first cap.' },
|
|
317
|
+
},
|
|
318
|
+
required: ['action'],
|
|
319
|
+
additionalProperties: false,
|
|
320
|
+
},
|
|
321
|
+
defaultTimeoutMs: 15_000,
|
|
322
|
+
async handler(input, ctx): Promise<SentinelResult> {
|
|
323
|
+
// Make sure cache is fresh — cheap on subsequent calls within
|
|
324
|
+
// the same process.
|
|
325
|
+
await loadTriggers();
|
|
326
|
+
|
|
327
|
+
switch (input.action) {
|
|
328
|
+
case 'create': {
|
|
329
|
+
const v = validateCreateInput(input);
|
|
330
|
+
if (!v.ok) return { action: 'create', ok: false, error: v.error };
|
|
331
|
+
const { name, source, dispatch } = v.data;
|
|
332
|
+
// Validate the time spec
|
|
333
|
+
if (source.type === 'time') {
|
|
334
|
+
try { validateTimeSpec(source.spec); } catch (err) {
|
|
335
|
+
return { action: 'create', ok: false, error: `invalid time spec: ${(err as Error).message}` };
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
// Enforce limits
|
|
339
|
+
const limitErr = enforceLimits(source, input.policy);
|
|
340
|
+
if (limitErr) return { action: 'create', ok: false, error: limitErr };
|
|
341
|
+
const ownerAgent = ctx.agent;
|
|
342
|
+
const activeCount = countTriggersByAgent(ownerAgent);
|
|
343
|
+
if (activeCount >= SENTINEL_LIMITS.MAX_TRIGGERS_PER_AGENT) {
|
|
344
|
+
return {
|
|
345
|
+
action: 'create',
|
|
346
|
+
ok: false,
|
|
347
|
+
error: `agent '${ownerAgent}' has ${activeCount} active/paused triggers (cap ${SENTINEL_LIMITS.MAX_TRIGGERS_PER_AGENT}). Delete triggers you no longer need before adding more (completed/errored ones don't count).`,
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
// Compute next-fire upfront so list/UI immediately shows it.
|
|
352
|
+
let nextFireAt: string | undefined;
|
|
353
|
+
try {
|
|
354
|
+
const next = computeNextFire(source.spec);
|
|
355
|
+
if (next) nextFireAt = next.toISOString();
|
|
356
|
+
} catch (err) {
|
|
357
|
+
return { action: 'create', ok: false, error: `unschedulable: ${(err as Error).message}` };
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// Where it fires. "current" is the caller's own session — the
|
|
361
|
+
// one thing an agent could not name before (2026-09-15: a
|
|
362
|
+
// resume trigger set from a working session fired in `main`
|
|
363
|
+
// and ran next to the work it was meant to resume).
|
|
364
|
+
let dispatchSession = dispatch.session ?? 'main';
|
|
365
|
+
let sessionNote: string | undefined;
|
|
366
|
+
// Left out + a trigger on yourself + created from inside a
|
|
367
|
+
// session → that session (Rene, 2026-09-21). "Wake me to go on
|
|
368
|
+
// with this" is what an agent means when it says nothing; `main`
|
|
369
|
+
// was a different conversation that then ran alongside the work.
|
|
370
|
+
// Another agent's trigger, or a call with no session context,
|
|
371
|
+
// keeps `main`.
|
|
372
|
+
//
|
|
373
|
+
// Not for a sub-agent turn: its session is a sealed work room
|
|
374
|
+
// nobody looks into once the task is handed in — a reminder
|
|
375
|
+
// fired there would be seen by no one. That keeps `main`, and
|
|
376
|
+
// says so.
|
|
377
|
+
const isSubTurn = (ctx.subagentDepth ?? 0) > 0;
|
|
378
|
+
if (dispatch.session === undefined && dispatch.agent === ownerAgent && ctx.session) {
|
|
379
|
+
if (isSubTurn) {
|
|
380
|
+
sessionNote = `dispatch.session was left out. You are a sub-agent in '${ctx.session}', which nobody watches after you finish, so this trigger fires in 'main'. Pass session: "current" if it really should come back to this sub-session.`;
|
|
381
|
+
} else {
|
|
382
|
+
dispatchSession = ctx.session;
|
|
383
|
+
if (ctx.session !== 'main') {
|
|
384
|
+
sessionNote = `dispatch.session was left out, so this trigger fires in the session you are in ('${ctx.session}'). Pass session: "main" if you meant your main session.`;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
if (dispatchSession === 'current') {
|
|
389
|
+
if (dispatch.agent !== ownerAgent) {
|
|
390
|
+
return {
|
|
391
|
+
action: 'create',
|
|
392
|
+
ok: false,
|
|
393
|
+
error: `dispatch.session "current" means YOUR current session, but dispatch.agent is '${dispatch.agent}'. Name a session of that agent instead.`,
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
if (!ctx.session) {
|
|
397
|
+
return {
|
|
398
|
+
action: 'create',
|
|
399
|
+
ok: false,
|
|
400
|
+
error: 'dispatch.session "current" is not available here — this call is not running inside a session. Name the session explicitly.',
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
dispatchSession = ctx.session;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
const trigger: Trigger = {
|
|
407
|
+
id: newTriggerId(name),
|
|
408
|
+
name,
|
|
409
|
+
...(input.intent ? { intent: input.intent } : {}),
|
|
410
|
+
ownerAgent,
|
|
411
|
+
source,
|
|
412
|
+
evaluator: { type: 'none' },
|
|
413
|
+
dispatch: {
|
|
414
|
+
agent: dispatch.agent,
|
|
415
|
+
session: dispatchSession,
|
|
416
|
+
prompt: dispatch.prompt,
|
|
417
|
+
},
|
|
418
|
+
...(input.policy ? { policy: input.policy } : {}),
|
|
419
|
+
createdAt: new Date().toISOString(),
|
|
420
|
+
status: 'active',
|
|
421
|
+
fireCount: 0,
|
|
422
|
+
errorStreak: 0,
|
|
423
|
+
...(nextFireAt ? { nextFireAt } : {}),
|
|
424
|
+
};
|
|
425
|
+
await saveTrigger(trigger);
|
|
426
|
+
reschedule();
|
|
427
|
+
logger.info({
|
|
428
|
+
msg: 'sentinel.tool.create',
|
|
429
|
+
triggerId: trigger.id,
|
|
430
|
+
ownerAgent,
|
|
431
|
+
dispatchAgent: dispatch.agent,
|
|
432
|
+
source: trigger.source.type === 'time' ? describeTimeSpec(trigger.source.spec) : trigger.source.type,
|
|
433
|
+
nextFireAt,
|
|
434
|
+
});
|
|
435
|
+
return {
|
|
436
|
+
action: 'create',
|
|
437
|
+
ok: true,
|
|
438
|
+
trigger,
|
|
439
|
+
...(sessionNote ? { session_note: sessionNote } : {}),
|
|
440
|
+
hint:
|
|
441
|
+
(sessionNote ? `${sessionNote} ` : '') +
|
|
442
|
+
(nextFireAt
|
|
443
|
+
? `Trigger '${trigger.id}' active. Next fire at ${nextFireAt}. ` +
|
|
444
|
+
`Use sentinel({action:"test", id:"${trigger.id}"}) to fire now (bypasses cooldown).`
|
|
445
|
+
: `Trigger '${trigger.id}' created — but no future fire computed (one-shot moment already past?).`),
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
case 'list': {
|
|
450
|
+
let all = listTriggers();
|
|
451
|
+
if (input.owner) all = all.filter((t) => t.ownerAgent === input.owner);
|
|
452
|
+
if (input.status) all = all.filter((t) => t.status === input.status);
|
|
453
|
+
// Fired one-shots stay for the audit trail (and history) but
|
|
454
|
+
// clutter the working view — 27 of them after one project
|
|
455
|
+
// evening (2026-09-07 report). Hide by default, say how many.
|
|
456
|
+
let hiddenCompleted = 0;
|
|
457
|
+
if (!input.status && !input.include_completed) {
|
|
458
|
+
hiddenCompleted = all.filter((t) => t.status === 'completed').length;
|
|
459
|
+
all = all.filter((t) => t.status !== 'completed');
|
|
460
|
+
}
|
|
461
|
+
// Newest first by createdAt
|
|
462
|
+
all.sort((a, b) => b.createdAt.localeCompare(a.createdAt));
|
|
463
|
+
return {
|
|
464
|
+
action: 'list',
|
|
465
|
+
ok: true,
|
|
466
|
+
count: all.length,
|
|
467
|
+
triggers: all.map(summarize),
|
|
468
|
+
...(hiddenCompleted > 0
|
|
469
|
+
? {
|
|
470
|
+
hidden_completed: hiddenCompleted,
|
|
471
|
+
hint:
|
|
472
|
+
`${hiddenCompleted} fired one-shot(s) hidden — list with include_completed:true, ` +
|
|
473
|
+
`remove them with action:"purge_completed" (auto-deleted after the retention window anyway).`,
|
|
474
|
+
}
|
|
475
|
+
: {}),
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
case 'purge_completed': {
|
|
480
|
+
let victims = listTriggers().filter((t) => t.status === 'completed');
|
|
481
|
+
if (input.owner) victims = victims.filter((t) => t.ownerAgent === input.owner);
|
|
482
|
+
const ids: string[] = [];
|
|
483
|
+
for (const t of victims) {
|
|
484
|
+
if (await deleteTrigger(t.id)) ids.push(t.id);
|
|
485
|
+
}
|
|
486
|
+
if (ids.length > 0) reschedule();
|
|
487
|
+
logger.info({ msg: 'sentinel.tool.purge_completed', by: ctx.agent, owner: input.owner ?? null, count: ids.length });
|
|
488
|
+
return { action: 'purge_completed', ok: true, count: ids.length, ids };
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
case 'get': {
|
|
492
|
+
if (!input.id) return { action: 'get', ok: false, error: 'get: id required' };
|
|
493
|
+
const t = getTrigger(input.id);
|
|
494
|
+
if (!t) return { action: 'get', ok: false, error: `trigger '${input.id}' not found` };
|
|
495
|
+
return {
|
|
496
|
+
action: 'get',
|
|
497
|
+
ok: true,
|
|
498
|
+
trigger: t,
|
|
499
|
+
describe: t.source.type === 'time' ? describeTimeSpec(t.source.spec) : t.source.type,
|
|
500
|
+
};
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
case 'pause': {
|
|
504
|
+
if (!input.id) return { action: 'pause', ok: false, id: '', error: 'pause: id required' };
|
|
505
|
+
const t = getTrigger(input.id);
|
|
506
|
+
if (!t) return { action: 'pause', ok: false, id: input.id, error: `trigger '${input.id}' not found` };
|
|
507
|
+
await saveTrigger({ ...t, status: 'paused', statusReason: `paused by ${ctx.agent}` });
|
|
508
|
+
reschedule();
|
|
509
|
+
return { action: 'pause', ok: true, id: input.id };
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
case 'resume': {
|
|
513
|
+
if (!input.id) return { action: 'resume', ok: false, id: '', error: 'resume: id required' };
|
|
514
|
+
const t = getTrigger(input.id);
|
|
515
|
+
if (!t) return { action: 'resume', ok: false, id: input.id, error: `trigger '${input.id}' not found` };
|
|
516
|
+
// Recompute nextFireAt; an old one may have drifted past.
|
|
517
|
+
let nextFireAt: string | undefined;
|
|
518
|
+
if (t.source.type === 'time') {
|
|
519
|
+
try {
|
|
520
|
+
const next = computeNextFire(t.source.spec);
|
|
521
|
+
if (next) nextFireAt = next.toISOString();
|
|
522
|
+
} catch (err) {
|
|
523
|
+
return { action: 'resume', ok: false, id: input.id, error: `cannot resume: ${(err as Error).message}` };
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
const updated: Trigger = {
|
|
527
|
+
...t,
|
|
528
|
+
status: 'active',
|
|
529
|
+
errorStreak: 0,
|
|
530
|
+
...(nextFireAt ? { nextFireAt } : {}),
|
|
531
|
+
};
|
|
532
|
+
delete updated.statusReason;
|
|
533
|
+
await saveTrigger(updated);
|
|
534
|
+
reschedule();
|
|
535
|
+
return { action: 'resume', ok: true, id: input.id };
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
case 'delete': {
|
|
539
|
+
if (!input.id) return { action: 'delete', ok: false, id: '', error: 'delete: id required' };
|
|
540
|
+
const removed = await deleteTrigger(input.id);
|
|
541
|
+
if (removed) reschedule();
|
|
542
|
+
return removed
|
|
543
|
+
? { action: 'delete', ok: true, id: input.id }
|
|
544
|
+
: { action: 'delete', ok: false, id: input.id, error: `trigger '${input.id}' not found` };
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
case 'test': {
|
|
548
|
+
if (!input.id) return { action: 'test', ok: false, id: '', error: 'test: id required' };
|
|
549
|
+
const t = getTrigger(input.id);
|
|
550
|
+
if (!t) return { action: 'test', ok: false, id: input.id, error: `trigger '${input.id}' not found` };
|
|
551
|
+
// Fire NOW, bypassing cooldown and daily-cap. dispatchTestFire
|
|
552
|
+
// routes via the main server's HTTP endpoint when this tool
|
|
553
|
+
// runs inside an MCP-child (claude-cli / codex-cli engines) —
|
|
554
|
+
// those children have no chatTurnDeps and would otherwise
|
|
555
|
+
// record `"sentinel not initialized"` in history. Same fallback
|
|
556
|
+
// pattern as spawn.ts:spawnAsyncViaHttp.
|
|
557
|
+
try {
|
|
558
|
+
await dispatchTestFire(t);
|
|
559
|
+
} catch (err) {
|
|
560
|
+
return {
|
|
561
|
+
action: 'test',
|
|
562
|
+
ok: false,
|
|
563
|
+
id: input.id,
|
|
564
|
+
error: `test dispatch failed: ${(err as Error).message}`,
|
|
565
|
+
};
|
|
566
|
+
}
|
|
567
|
+
return {
|
|
568
|
+
action: 'test',
|
|
569
|
+
ok: true,
|
|
570
|
+
id: input.id,
|
|
571
|
+
hint: `Test-fire dispatched. Check sentinel history or the target agent's session '${t.dispatch.session}' for the agent's response.`,
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
case 'history': {
|
|
576
|
+
if (!input.id) return { action: 'history', ok: false, id: '', error: 'history: id required' };
|
|
577
|
+
const entries = await getHistory(input.id, input.limit ?? 50);
|
|
578
|
+
return {
|
|
579
|
+
action: 'history',
|
|
580
|
+
ok: true,
|
|
581
|
+
id: input.id,
|
|
582
|
+
count: entries.length,
|
|
583
|
+
entries,
|
|
584
|
+
};
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
},
|
|
588
|
+
};
|
|
589
|
+
|
|
590
|
+
export function sentinelTools(): ToolDefinition[] {
|
|
591
|
+
return [sentinel] as ToolDefinition[];
|
|
592
|
+
}
|