contextos-agents 1.6.1 → 2.0.0-beta.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/.agents/AGENTS.md +6 -1
- package/.agents/adapters/aider/export.js +117 -97
- package/.agents/adapters/claude/export.js +68 -26
- package/.agents/adapters/copilot/export.js +90 -51
- package/.agents/adapters/cursor/export.js +83 -68
- package/.agents/adapters/drift-detector.js +196 -0
- package/.agents/adapters/gemini/export.js +76 -45
- package/.agents/adapters/pure-compiler.js +443 -0
- package/.agents/adapters/zed/export.js +109 -62
- package/.agents/compiled/registry.v2.json +504 -0
- package/.agents/compiled/registry.v2.sha256 +1 -0
- package/.agents/compiler/manifest-compiler.js +963 -0
- package/.agents/compiler/vendor/yaml.LICENSE.txt +13 -0
- package/.agents/compiler/vendor/yaml.SBOM.json +6 -0
- package/.agents/compiler/vendor/yaml.js +139 -0
- package/.agents/core/profiles/init.yaml +25 -0
- package/.agents/core/skills/context-manager/references/context-rules.md +59 -0
- package/.agents/core/skills/context-manager/skill.yaml +10 -5
- package/.agents/core/skills/context-os/SKILL.md +3 -6
- package/.agents/core/skills/context-os/skill.yaml +14 -8
- package/.agents/core/skills/engineering-workflow/SKILL.md +1 -1
- package/.agents/core/skills/engineering-workflow/skill.yaml +7 -7
- package/.agents/core/skills/gemini-precision/SKILL.md +4 -0
- package/.agents/core/skills/gemini-precision/skill.yaml +5 -6
- package/.agents/core/skills/gstack-roles/SKILL.md +3 -1
- package/.agents/core/skills/gstack-roles/skill.yaml +9 -6
- package/.agents/core/skills/ponytail-mindset/skill.yaml +7 -7
- package/.agents/core/skills/security/skill.yaml +21 -2
- package/.agents/ctx.js +587 -111
- package/.agents/customization-dx.js +282 -0
- package/.agents/doctor.js +877 -33
- package/.agents/filesystem/index.js +71 -0
- package/.agents/filesystem/journaled-transaction.js +451 -0
- package/.agents/filesystem/lockfile-v2.js +275 -0
- package/.agents/filesystem/platform-hardening.js +222 -0
- package/.agents/filesystem/project-lock.js +218 -0
- package/.agents/filesystem/safe-path.js +256 -0
- package/.agents/generated/claude/skills/context-os/SKILL.md +1 -1
- package/.agents/generated/claude/skills/engineering-workflow/SKILL.md +1 -1
- package/.agents/generated/claude/skills/gemini-precision/SKILL.md +4 -0
- package/.agents/generated/claude/skills/gstack-roles/SKILL.md +3 -1
- package/.agents/generated/gemini/skills/context-os/SKILL.md +2 -2
- package/.agents/generated/gemini/skills/engineering-workflow/SKILL.md +1 -1
- package/.agents/generated/gemini/skills/gemini-precision/SKILL.md +4 -0
- package/.agents/generated/gemini/skills/gstack-roles/SKILL.md +3 -1
- package/.agents/plugins/contextos/hooks.json +25 -0
- package/.agents/plugins/contextos/plugin.json +19 -0
- package/.agents/plugins.js +432 -73
- package/.agents/profiles.js +507 -46
- package/.agents/resolver.js +50 -414
- package/.agents/schemas/attestation.review.v1.json +111 -0
- package/.agents/schemas/attestation.verification.v1.json +85 -0
- package/.agents/schemas/lockfile.v2.schema.json +134 -0
- package/.agents/schemas/profile.v2.schema.json +114 -0
- package/.agents/schemas/runtime.thread.v1.json +192 -0
- package/.agents/schemas/skill.manifest.v2.json +177 -0
- package/.agents/schemas/verification.spec.v1.json +39 -0
- package/.agents/schemas/workspace.graph.schema.json +106 -0
- package/.agents/stats.js +22 -9
- package/.agents/transaction-core/event-store.js +288 -0
- package/.agents/transaction-core/idempotency.js +129 -0
- package/.agents/transaction-core/ipc-lock.js +311 -0
- package/.agents/transaction-core/plugin-supply-chain-bundle.js +436 -0
- package/.agents/validate.js +143 -14
- package/.agents/watch.js +354 -102
- package/.agents/workspace/workspace-graph.js +778 -0
- package/README.md +59 -387
- package/benchmarks/v2/analysis/statistics.js +140 -0
- package/benchmarks/v2/analysis/stats.js +69 -0
- package/benchmarks/v2/arms/arm-definitions.js +79 -0
- package/benchmarks/v2/dataset.schema.json +34 -0
- package/benchmarks/v2/evaluators/index.js +25 -0
- package/benchmarks/v2/evaluators/verified-success.js +116 -0
- package/benchmarks/v2/harness/runner.js +88 -0
- package/benchmarks/v2/pilot-tasks.json +392 -0
- package/bin/commands/recover.js +88 -0
- package/bin/commands/uninstall.js +207 -0
- package/bin/commands/update.js +325 -0
- package/bin/commands.js +342 -0
- package/bin/index.js +326 -149
- package/bin/lib/detector.js +106 -0
- package/bin/lib/lockfile.js +253 -0
- package/bin/lib/safe-writer.js +290 -0
- package/package.json +85 -73
- package/registry.json +15 -7
- package/registry.schema.json +3 -1
- package/registry.v2.schema.json +86 -0
- package/.agents/core/profiles/backend.yaml +0 -47
- package/.agents/core/profiles/enterprise.yaml +0 -46
- package/.agents/core/profiles/frontend.yaml +0 -46
- package/.agents/core/profiles/hackathon.yaml +0 -45
- package/.agents/core/profiles/mvp.yaml +0 -44
- package/.agents/core/profiles/startup.yaml +0 -48
- package/.agents/core/skills/adapters/EXAMPLES.md +0 -19
- package/.agents/core/skills/adapters/SKILL.md +0 -105
- package/.agents/core/skills/adapters/TROUBLESHOOTING.md +0 -7
- package/.agents/core/skills/adapters/VALIDATION.json +0 -12
- package/.agents/core/skills/adapters/skill.yaml +0 -10
- package/.agents/core/skills/architecture-diagrams/SKILL.md +0 -108
- package/.agents/core/skills/architecture-diagrams/VALIDATION.json +0 -12
- package/.agents/core/skills/architecture-diagrams/skill.yaml +0 -8
- package/.agents/core/skills/brutalist-design/SKILL.md +0 -150
- package/.agents/core/skills/brutalist-design/VALIDATION.json +0 -12
- package/.agents/core/skills/brutalist-design/skill.yaml +0 -8
- package/.agents/core/skills/database/EXAMPLES.md +0 -74
- package/.agents/core/skills/database/SKILL.md +0 -101
- package/.agents/core/skills/database/TROUBLESHOOTING.md +0 -18
- package/.agents/core/skills/database/VALIDATION.json +0 -11
- package/.agents/core/skills/database/skill.yaml +0 -25
- package/.agents/core/skills/ddd/EXAMPLES.md +0 -42
- package/.agents/core/skills/ddd/SKILL.md +0 -247
- package/.agents/core/skills/ddd/TROUBLESHOOTING.md +0 -19
- package/.agents/core/skills/ddd/VALIDATION.json +0 -12
- package/.agents/core/skills/ddd/ddd.md +0 -178
- package/.agents/core/skills/ddd/skill.yaml +0 -10
- package/.agents/core/skills/decisions/EXAMPLES.md +0 -35
- package/.agents/core/skills/decisions/SKILL.md +0 -90
- package/.agents/core/skills/decisions/TROUBLESHOOTING.md +0 -13
- package/.agents/core/skills/decisions/VALIDATION.json +0 -12
- package/.agents/core/skills/decisions/skill.yaml +0 -10
- package/.agents/core/skills/docker/EXAMPLES.md +0 -56
- package/.agents/core/skills/docker/SKILL.md +0 -63
- package/.agents/core/skills/docker/TROUBLESHOOTING.md +0 -18
- package/.agents/core/skills/docker/VALIDATION.json +0 -11
- package/.agents/core/skills/docker/skill.yaml +0 -23
- package/.agents/core/skills/fastapi/EXAMPLES.md +0 -36
- package/.agents/core/skills/fastapi/SKILL.md +0 -148
- package/.agents/core/skills/fastapi/TROUBLESHOOTING.md +0 -19
- package/.agents/core/skills/fastapi/VALIDATION.json +0 -12
- package/.agents/core/skills/fastapi/fastapi.md +0 -112
- package/.agents/core/skills/fastapi/skill.yaml +0 -10
- package/.agents/core/skills/generators/EXAMPLES.md +0 -19
- package/.agents/core/skills/generators/SKILL.md +0 -112
- package/.agents/core/skills/generators/TROUBLESHOOTING.md +0 -7
- package/.agents/core/skills/generators/VALIDATION.json +0 -12
- package/.agents/core/skills/generators/skill.yaml +0 -10
- package/.agents/core/skills/generators/templates/API.md +0 -77
- package/.agents/core/skills/generators/templates/ARCHITECTURE.md +0 -70
- package/.agents/core/skills/generators/templates/DATABASE.md +0 -42
- package/.agents/core/skills/generators/templates/DECISION.md +0 -46
- package/.agents/core/skills/generators/templates/PRD.md +0 -67
- package/.agents/core/skills/generators/templates/PROJECT_GRAPH.md +0 -56
- package/.agents/core/skills/generators/templates/ROADMAP.md +0 -51
- package/.agents/core/skills/generators/templates/TASKS.md +0 -43
- package/.agents/core/skills/generators/templates/UI.md +0 -73
- package/.agents/core/skills/graphify/EXAMPLES.md +0 -73
- package/.agents/core/skills/graphify/SKILL.md +0 -130
- package/.agents/core/skills/graphify/VALIDATION.json +0 -12
- package/.agents/core/skills/graphify/skill.yaml +0 -13
- package/.agents/core/skills/impeccable-design/EXAMPLES.md +0 -26
- package/.agents/core/skills/impeccable-design/SKILL.md +0 -201
- package/.agents/core/skills/impeccable-design/TROUBLESHOOTING.md +0 -19
- package/.agents/core/skills/impeccable-design/VALIDATION.json +0 -12
- package/.agents/core/skills/impeccable-design/skill.yaml +0 -14
- package/.agents/core/skills/interview-me/SKILL.md +0 -97
- package/.agents/core/skills/interview-me/VALIDATION.json +0 -12
- package/.agents/core/skills/interview-me/skill.yaml +0 -8
- package/.agents/core/skills/microservices/EXAMPLES.md +0 -38
- package/.agents/core/skills/microservices/SKILL.md +0 -164
- package/.agents/core/skills/microservices/TROUBLESHOOTING.md +0 -19
- package/.agents/core/skills/microservices/VALIDATION.json +0 -12
- package/.agents/core/skills/microservices/microservices.md +0 -119
- package/.agents/core/skills/microservices/skill.yaml +0 -10
- package/.agents/core/skills/minimalist-design/SKILL.md +0 -113
- package/.agents/core/skills/minimalist-design/VALIDATION.json +0 -12
- package/.agents/core/skills/minimalist-design/skill.yaml +0 -8
- package/.agents/core/skills/nestjs/EXAMPLES.md +0 -40
- package/.agents/core/skills/nestjs/SKILL.md +0 -139
- package/.agents/core/skills/nestjs/TROUBLESHOOTING.md +0 -19
- package/.agents/core/skills/nestjs/VALIDATION.json +0 -12
- package/.agents/core/skills/nestjs/nestjs.md +0 -103
- package/.agents/core/skills/nestjs/skill.yaml +0 -10
- package/.agents/core/skills/nextjs/EXAMPLES.md +0 -40
- package/.agents/core/skills/nextjs/SKILL.md +0 -163
- package/.agents/core/skills/nextjs/TROUBLESHOOTING.md +0 -19
- package/.agents/core/skills/nextjs/VALIDATION.json +0 -12
- package/.agents/core/skills/nextjs/nextjs.md +0 -67
- package/.agents/core/skills/nextjs/skill.yaml +0 -10
- package/.agents/core/skills/node/EXAMPLES.md +0 -80
- package/.agents/core/skills/node/SKILL.md +0 -128
- package/.agents/core/skills/node/TROUBLESHOOTING.md +0 -19
- package/.agents/core/skills/node/VALIDATION.json +0 -12
- package/.agents/core/skills/node/node.md +0 -87
- package/.agents/core/skills/node/skill.yaml +0 -10
- package/.agents/core/skills/performance/EXAMPLES.md +0 -30
- package/.agents/core/skills/performance/SKILL.md +0 -75
- package/.agents/core/skills/performance/TROUBLESHOOTING.md +0 -19
- package/.agents/core/skills/performance/VALIDATION.json +0 -12
- package/.agents/core/skills/performance/performance.md +0 -52
- package/.agents/core/skills/performance/skill.yaml +0 -10
- package/.agents/core/skills/react/EXAMPLES.md +0 -79
- package/.agents/core/skills/react/SKILL.md +0 -132
- package/.agents/core/skills/react/TROUBLESHOOTING.md +0 -19
- package/.agents/core/skills/react/VALIDATION.json +0 -12
- package/.agents/core/skills/react/react.md +0 -93
- package/.agents/core/skills/react/skill.yaml +0 -10
- package/.agents/core/skills/react-best-practices/SKILL.md +0 -155
- package/.agents/core/skills/react-best-practices/VALIDATION.json +0 -12
- package/.agents/core/skills/react-best-practices/skill.yaml +0 -10
- package/.agents/core/skills/redesign-audit/SKILL.md +0 -117
- package/.agents/core/skills/redesign-audit/VALIDATION.json +0 -12
- package/.agents/core/skills/redesign-audit/skill.yaml +0 -8
- package/.agents/core/skills/soft-design/SKILL.md +0 -108
- package/.agents/core/skills/soft-design/VALIDATION.json +0 -12
- package/.agents/core/skills/soft-design/skill.yaml +0 -8
- package/.agents/core/skills/state-management/EXAMPLES.md +0 -56
- package/.agents/core/skills/state-management/SKILL.md +0 -48
- package/.agents/core/skills/state-management/TROUBLESHOOTING.md +0 -18
- package/.agents/core/skills/state-management/VALIDATION.json +0 -11
- package/.agents/core/skills/state-management/skill.yaml +0 -22
- package/.agents/core/skills/subagent-orchestrator/SKILL.md +0 -100
- package/.agents/core/skills/subagent-orchestrator/VALIDATION.json +0 -12
- package/.agents/core/skills/subagent-orchestrator/skill.yaml +0 -8
- package/.agents/core/skills/system-design/EXAMPLES.md +0 -75
- package/.agents/core/skills/system-design/SKILL.md +0 -419
- package/.agents/core/skills/system-design/TROUBLESHOOTING.md +0 -19
- package/.agents/core/skills/system-design/VALIDATION.json +0 -12
- package/.agents/core/skills/system-design/skill.yaml +0 -13
- package/.agents/core/skills/system-design/system-design.md +0 -112
- package/.agents/core/skills/testing/EXAMPLES.md +0 -71
- package/.agents/core/skills/testing/SKILL.md +0 -70
- package/.agents/core/skills/testing/TROUBLESHOOTING.md +0 -18
- package/.agents/core/skills/testing/VALIDATION.json +0 -11
- package/.agents/core/skills/testing/skill.yaml +0 -26
- package/.agents/core/skills/typescript/EXAMPLES.md +0 -64
- package/.agents/core/skills/typescript/SKILL.md +0 -112
- package/.agents/core/skills/typescript/TROUBLESHOOTING.md +0 -19
- package/.agents/core/skills/typescript/VALIDATION.json +0 -12
- package/.agents/core/skills/typescript/skill.yaml +0 -10
- package/.agents/core/skills/typescript/typescript.md +0 -71
- package/.agents/core/skills/ui-design/EXAMPLES.md +0 -21
- package/.agents/core/skills/ui-design/SKILL.md +0 -124
- package/.agents/core/skills/ui-design/TROUBLESHOOTING.md +0 -19
- package/.agents/core/skills/ui-design/VALIDATION.json +0 -12
- package/.agents/core/skills/ui-design/skill.yaml +0 -10
- package/.agents/core/skills/ui-design/ui.md +0 -88
- package/.agents/core/skills/ui-ux-pro/EXAMPLES.md +0 -62
- package/.agents/core/skills/ui-ux-pro/SKILL.md +0 -375
- package/.agents/core/skills/ui-ux-pro/TROUBLESHOOTING.md +0 -19
- package/.agents/core/skills/ui-ux-pro/VALIDATION.json +0 -12
- package/.agents/core/skills/ui-ux-pro/skill.yaml +0 -13
- package/.agents/core/skills/ux-design/EXAMPLES.md +0 -36
- package/.agents/core/skills/ux-design/SKILL.md +0 -116
- package/.agents/core/skills/ux-design/TROUBLESHOOTING.md +0 -19
- package/.agents/core/skills/ux-design/VALIDATION.json +0 -12
- package/.agents/core/skills/ux-design/skill.yaml +0 -10
- package/.agents/core/skills/ux-design/ux.md +0 -80
- package/.agents/core/skills/vercel-optimize/SKILL.md +0 -83
- package/.agents/core/skills/vercel-optimize/VALIDATION.json +0 -12
- package/.agents/core/skills/vercel-optimize/skill.yaml +0 -10
- package/.agents/core/skills/web-accessibility/EXAMPLES.md +0 -39
- package/.agents/core/skills/web-accessibility/SKILL.md +0 -170
- package/.agents/core/skills/web-accessibility/TROUBLESHOOTING.md +0 -19
- package/.agents/core/skills/web-accessibility/VALIDATION.json +0 -12
- package/.agents/core/skills/web-accessibility/accessibility.md +0 -63
- package/.agents/core/skills/web-accessibility/skill.yaml +0 -10
- package/.agents/generated/claude/skills/adapters/SKILL.md +0 -126
- package/.agents/generated/claude/skills/architecture-diagrams/SKILL.md +0 -101
- package/.agents/generated/claude/skills/brutalist-design/SKILL.md +0 -145
- package/.agents/generated/claude/skills/database/SKILL.md +0 -191
- package/.agents/generated/claude/skills/ddd/SKILL.md +0 -305
- package/.agents/generated/claude/skills/decisions/SKILL.md +0 -134
- package/.agents/generated/claude/skills/docker/SKILL.md +0 -135
- package/.agents/generated/claude/skills/fastapi/SKILL.md +0 -200
- package/.agents/generated/claude/skills/generators/SKILL.md +0 -133
- package/.agents/generated/claude/skills/graphify/SKILL.md +0 -198
- package/.agents/generated/claude/skills/impeccable-design/SKILL.md +0 -241
- package/.agents/generated/claude/skills/interview-me/SKILL.md +0 -90
- package/.agents/generated/claude/skills/microservices/SKILL.md +0 -218
- package/.agents/generated/claude/skills/minimalist-design/SKILL.md +0 -108
- package/.agents/generated/claude/skills/nestjs/SKILL.md +0 -195
- package/.agents/generated/claude/skills/nextjs/SKILL.md +0 -219
- package/.agents/generated/claude/skills/node/SKILL.md +0 -224
- package/.agents/generated/claude/skills/performance/SKILL.md +0 -121
- package/.agents/generated/claude/skills/react/SKILL.md +0 -227
- package/.agents/generated/claude/skills/react-best-practices/SKILL.md +0 -146
- package/.agents/generated/claude/skills/redesign-audit/SKILL.md +0 -112
- package/.agents/generated/claude/skills/soft-design/SKILL.md +0 -103
- package/.agents/generated/claude/skills/state-management/SKILL.md +0 -120
- package/.agents/generated/claude/skills/subagent-orchestrator/SKILL.md +0 -93
- package/.agents/generated/claude/skills/system-design/SKILL.md +0 -507
- package/.agents/generated/claude/skills/testing/SKILL.md +0 -157
- package/.agents/generated/claude/skills/typescript/SKILL.md +0 -192
- package/.agents/generated/claude/skills/ui-design/SKILL.md +0 -161
- package/.agents/generated/claude/skills/ui-ux-pro/SKILL.md +0 -451
- package/.agents/generated/claude/skills/ux-design/SKILL.md +0 -168
- package/.agents/generated/claude/skills/vercel-optimize/SKILL.md +0 -76
- package/.agents/generated/claude/skills/web-accessibility/SKILL.md +0 -225
- package/.agents/generated/gemini/skills/adapters/SKILL.md +0 -135
- package/.agents/generated/gemini/skills/architecture-diagrams/SKILL.md +0 -107
- package/.agents/generated/gemini/skills/brutalist-design/SKILL.md +0 -151
- package/.agents/generated/gemini/skills/database/SKILL.md +0 -200
- package/.agents/generated/gemini/skills/ddd/SKILL.md +0 -314
- package/.agents/generated/gemini/skills/decisions/SKILL.md +0 -143
- package/.agents/generated/gemini/skills/docker/SKILL.md +0 -144
- package/.agents/generated/gemini/skills/fastapi/SKILL.md +0 -209
- package/.agents/generated/gemini/skills/generators/SKILL.md +0 -142
- package/.agents/generated/gemini/skills/graphify/SKILL.md +0 -205
- package/.agents/generated/gemini/skills/impeccable-design/SKILL.md +0 -250
- package/.agents/generated/gemini/skills/interview-me/SKILL.md +0 -96
- package/.agents/generated/gemini/skills/microservices/SKILL.md +0 -227
- package/.agents/generated/gemini/skills/minimalist-design/SKILL.md +0 -114
- package/.agents/generated/gemini/skills/nestjs/SKILL.md +0 -204
- package/.agents/generated/gemini/skills/nextjs/SKILL.md +0 -298
- package/.agents/generated/gemini/skills/node/SKILL.md +0 -323
- package/.agents/generated/gemini/skills/performance/SKILL.md +0 -185
- package/.agents/generated/gemini/skills/react/SKILL.md +0 -332
- package/.agents/generated/gemini/skills/react-best-practices/SKILL.md +0 -152
- package/.agents/generated/gemini/skills/redesign-audit/SKILL.md +0 -118
- package/.agents/generated/gemini/skills/soft-design/SKILL.md +0 -109
- package/.agents/generated/gemini/skills/state-management/SKILL.md +0 -129
- package/.agents/generated/gemini/skills/subagent-orchestrator/SKILL.md +0 -99
- package/.agents/generated/gemini/skills/system-design/SKILL.md +0 -631
- package/.agents/generated/gemini/skills/testing/SKILL.md +0 -166
- package/.agents/generated/gemini/skills/typescript/SKILL.md +0 -275
- package/.agents/generated/gemini/skills/ui-design/SKILL.md +0 -170
- package/.agents/generated/gemini/skills/ui-ux-pro/SKILL.md +0 -460
- package/.agents/generated/gemini/skills/ux-design/SKILL.md +0 -177
- package/.agents/generated/gemini/skills/vercel-optimize/SKILL.md +0 -82
- package/.agents/generated/gemini/skills/web-accessibility/SKILL.md +0 -300
- package/.agents/mcp/runtime.py +0 -470
- package/.agents/mcp/server.mjs +0 -189271
- package/benchmarks/gemini-issues.js +0 -533
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
3
|
+
"title": "SkillManifestV2",
|
|
4
|
+
"description": "ContextOS Skill Manifest Schema v2",
|
|
5
|
+
"type": "object",
|
|
6
|
+
"required": [
|
|
7
|
+
"schemaVersion",
|
|
8
|
+
"id",
|
|
9
|
+
"displayName",
|
|
10
|
+
"version",
|
|
11
|
+
"description",
|
|
12
|
+
"category",
|
|
13
|
+
"entrypoint"
|
|
14
|
+
],
|
|
15
|
+
"additionalProperties": false,
|
|
16
|
+
"properties": {
|
|
17
|
+
"schemaVersion": {
|
|
18
|
+
"type": "integer",
|
|
19
|
+
"const": 2
|
|
20
|
+
},
|
|
21
|
+
"id": {
|
|
22
|
+
"type": "string",
|
|
23
|
+
"pattern": "^[a-z0-9-]+$"
|
|
24
|
+
},
|
|
25
|
+
"displayName": {
|
|
26
|
+
"type": "string",
|
|
27
|
+
"minLength": 1
|
|
28
|
+
},
|
|
29
|
+
"version": {
|
|
30
|
+
"type": "string",
|
|
31
|
+
"pattern": "^\\d+\\.\\d+\\.\\d+.*$"
|
|
32
|
+
},
|
|
33
|
+
"description": {
|
|
34
|
+
"type": "string",
|
|
35
|
+
"minLength": 1
|
|
36
|
+
},
|
|
37
|
+
"type": {
|
|
38
|
+
"type": "string",
|
|
39
|
+
"enum": ["instruction-only", "compiler", "runtime", "experimental"]
|
|
40
|
+
},
|
|
41
|
+
"category": {
|
|
42
|
+
"type": "string",
|
|
43
|
+
"enum": [
|
|
44
|
+
"frontend",
|
|
45
|
+
"backend",
|
|
46
|
+
"architecture",
|
|
47
|
+
"devops",
|
|
48
|
+
"testing",
|
|
49
|
+
"security",
|
|
50
|
+
"ui",
|
|
51
|
+
"cross-cutting",
|
|
52
|
+
"workflow",
|
|
53
|
+
"general"
|
|
54
|
+
]
|
|
55
|
+
},
|
|
56
|
+
"entrypoint": {
|
|
57
|
+
"type": "string",
|
|
58
|
+
"pattern": "^[^/\\\\].*\\.md$"
|
|
59
|
+
},
|
|
60
|
+
"signals": {
|
|
61
|
+
"type": "object",
|
|
62
|
+
"additionalProperties": false,
|
|
63
|
+
"properties": {
|
|
64
|
+
"aliases": {
|
|
65
|
+
"type": "array",
|
|
66
|
+
"items": { "type": "string" }
|
|
67
|
+
},
|
|
68
|
+
"keywords": {
|
|
69
|
+
"type": "array",
|
|
70
|
+
"items": {
|
|
71
|
+
"type": "object",
|
|
72
|
+
"required": ["value", "weight"],
|
|
73
|
+
"additionalProperties": false,
|
|
74
|
+
"properties": {
|
|
75
|
+
"value": { "type": "string" },
|
|
76
|
+
"locale": { "type": "string", "enum": ["en", "ru", "any"] },
|
|
77
|
+
"weight": { "type": "number", "minimum": 1, "maximum": 100 }
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
"fileGlobs": {
|
|
82
|
+
"type": "array",
|
|
83
|
+
"items": {
|
|
84
|
+
"type": "object",
|
|
85
|
+
"required": ["value", "weight"],
|
|
86
|
+
"additionalProperties": false,
|
|
87
|
+
"properties": {
|
|
88
|
+
"value": { "type": "string" },
|
|
89
|
+
"weight": { "type": "number", "minimum": 1, "maximum": 100 }
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
"packages": {
|
|
94
|
+
"type": "array",
|
|
95
|
+
"items": {
|
|
96
|
+
"type": "object",
|
|
97
|
+
"required": ["ecosystem", "name", "weight"],
|
|
98
|
+
"additionalProperties": false,
|
|
99
|
+
"properties": {
|
|
100
|
+
"ecosystem": { "type": "string", "enum": ["npm", "pypi", "cargo", "go", "unknown"] },
|
|
101
|
+
"name": { "type": "string" },
|
|
102
|
+
"weight": { "type": "number", "minimum": 1, "maximum": 100 }
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
"dependencies": {
|
|
109
|
+
"type": "object",
|
|
110
|
+
"additionalProperties": false,
|
|
111
|
+
"properties": {
|
|
112
|
+
"requires": {
|
|
113
|
+
"type": "array",
|
|
114
|
+
"items": { "type": "string" }
|
|
115
|
+
},
|
|
116
|
+
"optional": {
|
|
117
|
+
"type": "array",
|
|
118
|
+
"items": { "type": "string" }
|
|
119
|
+
},
|
|
120
|
+
"conflicts": {
|
|
121
|
+
"type": "array",
|
|
122
|
+
"items": { "type": "string" }
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
"context": {
|
|
127
|
+
"type": "object",
|
|
128
|
+
"additionalProperties": false,
|
|
129
|
+
"properties": {
|
|
130
|
+
"priority": {
|
|
131
|
+
"type": "integer",
|
|
132
|
+
"minimum": 0,
|
|
133
|
+
"maximum": 100
|
|
134
|
+
},
|
|
135
|
+
"estimatedTokens": {
|
|
136
|
+
"type": "integer",
|
|
137
|
+
"minimum": 0
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
"resources": {
|
|
142
|
+
"type": "array",
|
|
143
|
+
"items": {
|
|
144
|
+
"type": "object",
|
|
145
|
+
"required": ["path"],
|
|
146
|
+
"additionalProperties": false,
|
|
147
|
+
"properties": {
|
|
148
|
+
"path": { "type": "string" },
|
|
149
|
+
"mode": { "type": "string", "enum": ["eager", "on-demand"] }
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
"deprecated": {
|
|
154
|
+
"type": "boolean"
|
|
155
|
+
},
|
|
156
|
+
"canonical": {
|
|
157
|
+
"type": "string",
|
|
158
|
+
"pattern": "^[a-z0-9-]+$"
|
|
159
|
+
},
|
|
160
|
+
"rules": {
|
|
161
|
+
"type": "array",
|
|
162
|
+
"items": {
|
|
163
|
+
"type": "object",
|
|
164
|
+
"required": ["id", "level", "enforcement", "summary"],
|
|
165
|
+
"additionalProperties": false,
|
|
166
|
+
"properties": {
|
|
167
|
+
"id": { "type": "string", "pattern": "^[A-Z0-9_-]+$" },
|
|
168
|
+
"level": { "type": "string", "enum": ["must", "should", "may"] },
|
|
169
|
+
"enforcement": { "type": "string", "enum": ["runtime", "linter", "prompt-guidance", "reference", "example"] },
|
|
170
|
+
"summary": { "type": "string" },
|
|
171
|
+
"checker": { "type": "string" },
|
|
172
|
+
"tokenCost": { "type": "integer", "minimum": 0 }
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
3
|
+
"title": "ContextOS Verification Spec v1",
|
|
4
|
+
"type": "object",
|
|
5
|
+
"required": [
|
|
6
|
+
"executable",
|
|
7
|
+
"args",
|
|
8
|
+
"timeoutMs",
|
|
9
|
+
"required",
|
|
10
|
+
"network"
|
|
11
|
+
],
|
|
12
|
+
"additionalProperties": false,
|
|
13
|
+
"properties": {
|
|
14
|
+
"executable": {
|
|
15
|
+
"type": "string",
|
|
16
|
+
"enum": ["npm", "pnpm", "yarn", "pytest", "cargo", "go"]
|
|
17
|
+
},
|
|
18
|
+
"args": {
|
|
19
|
+
"type": "array",
|
|
20
|
+
"items": { "type": "string" }
|
|
21
|
+
},
|
|
22
|
+
"timeoutMs": {
|
|
23
|
+
"type": "integer",
|
|
24
|
+
"minimum": 1000,
|
|
25
|
+
"maximum": 600000
|
|
26
|
+
},
|
|
27
|
+
"required": {
|
|
28
|
+
"type": "boolean"
|
|
29
|
+
},
|
|
30
|
+
"network": {
|
|
31
|
+
"type": "string",
|
|
32
|
+
"enum": ["deny", "allow"]
|
|
33
|
+
},
|
|
34
|
+
"allowedOutputPaths": {
|
|
35
|
+
"type": "array",
|
|
36
|
+
"items": { "type": "string" }
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
3
|
+
"title": "WorkspaceGraph",
|
|
4
|
+
"description": "ContextOS Workspace Evidence Graph Schema v1",
|
|
5
|
+
"type": "object",
|
|
6
|
+
"required": [
|
|
7
|
+
"schemaVersion",
|
|
8
|
+
"repositoryRoot",
|
|
9
|
+
"packages",
|
|
10
|
+
"evidence",
|
|
11
|
+
"fingerprint",
|
|
12
|
+
"partial"
|
|
13
|
+
],
|
|
14
|
+
"additionalProperties": false,
|
|
15
|
+
"properties": {
|
|
16
|
+
"schemaVersion": {
|
|
17
|
+
"type": "integer",
|
|
18
|
+
"const": 1
|
|
19
|
+
},
|
|
20
|
+
"repositoryRoot": {
|
|
21
|
+
"type": "string",
|
|
22
|
+
"description": "Normalized root directory of the workspace/repository"
|
|
23
|
+
},
|
|
24
|
+
"packages": {
|
|
25
|
+
"type": "array",
|
|
26
|
+
"description": "Discovered packages in the monorepo or workspace",
|
|
27
|
+
"items": {
|
|
28
|
+
"type": "object",
|
|
29
|
+
"required": [
|
|
30
|
+
"id",
|
|
31
|
+
"root",
|
|
32
|
+
"ecosystem",
|
|
33
|
+
"manifests",
|
|
34
|
+
"dependencies",
|
|
35
|
+
"configs",
|
|
36
|
+
"languages",
|
|
37
|
+
"internalDependencies"
|
|
38
|
+
],
|
|
39
|
+
"additionalProperties": false,
|
|
40
|
+
"properties": {
|
|
41
|
+
"id": {
|
|
42
|
+
"type": "string",
|
|
43
|
+
"description": "Package identifier (name from manifest or normalized directory path)"
|
|
44
|
+
},
|
|
45
|
+
"root": {
|
|
46
|
+
"type": "string",
|
|
47
|
+
"description": "POSIX relative directory path from repositoryRoot"
|
|
48
|
+
},
|
|
49
|
+
"ecosystem": {
|
|
50
|
+
"type": "string",
|
|
51
|
+
"enum": ["npm", "python", "rust", "go", "unknown"],
|
|
52
|
+
"description": "Primary package ecosystem"
|
|
53
|
+
},
|
|
54
|
+
"manifests": {
|
|
55
|
+
"type": "array",
|
|
56
|
+
"items": { "type": "string" },
|
|
57
|
+
"description": "Relative paths to package manifests (e.g. package.json, pyproject.toml)"
|
|
58
|
+
},
|
|
59
|
+
"dependencies": {
|
|
60
|
+
"type": "array",
|
|
61
|
+
"items": { "type": "string" },
|
|
62
|
+
"description": "Normalized dependency package names"
|
|
63
|
+
},
|
|
64
|
+
"configs": {
|
|
65
|
+
"type": "array",
|
|
66
|
+
"items": { "type": "string" },
|
|
67
|
+
"description": "Detected configuration files (e.g. tsconfig.json, next.config.js)"
|
|
68
|
+
},
|
|
69
|
+
"languages": {
|
|
70
|
+
"type": "array",
|
|
71
|
+
"items": { "type": "string" },
|
|
72
|
+
"description": "Programming languages identified in the package"
|
|
73
|
+
},
|
|
74
|
+
"internalDependencies": {
|
|
75
|
+
"type": "array",
|
|
76
|
+
"items": { "type": "string" },
|
|
77
|
+
"description": "Package IDs within this workspace that this package depends on"
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
"evidence": {
|
|
83
|
+
"type": "array",
|
|
84
|
+
"description": "Accumulated evidence items derived from the workspace structure",
|
|
85
|
+
"items": {
|
|
86
|
+
"type": "object",
|
|
87
|
+
"required": ["target", "source", "weight", "description"],
|
|
88
|
+
"additionalProperties": false,
|
|
89
|
+
"properties": {
|
|
90
|
+
"target": { "type": "string" },
|
|
91
|
+
"source": { "type": "string" },
|
|
92
|
+
"weight": { "type": "number" },
|
|
93
|
+
"description": { "type": "string" }
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
"fingerprint": {
|
|
98
|
+
"type": "string",
|
|
99
|
+
"description": "Cryptographic SHA-256 fingerprint of the workspace manifest state"
|
|
100
|
+
},
|
|
101
|
+
"partial": {
|
|
102
|
+
"type": "boolean",
|
|
103
|
+
"description": "True if workspace discovery hit a depth, package count, or traversal limit"
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
package/.agents/stats.js
CHANGED
|
@@ -64,8 +64,18 @@ function calculateContextStats(projectDir = process.cwd()) {
|
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
// Resolved context:
|
|
68
|
-
|
|
67
|
+
// Resolved context: dynamically resolved skills for this project's stack
|
|
68
|
+
let typicalResolvedSkills = ['engineering-workflow', 'ponytail-mindset'];
|
|
69
|
+
try {
|
|
70
|
+
const resolver = require('./resolver.js');
|
|
71
|
+
const resolved = resolver.resolveSkills({ prompt: 'Implement standard feature', projectDir });
|
|
72
|
+
if (resolved && Array.isArray(resolved.skills)) {
|
|
73
|
+
typicalResolvedSkills = resolved.skills;
|
|
74
|
+
}
|
|
75
|
+
} catch (_) {
|
|
76
|
+
typicalResolvedSkills = ['engineering-workflow', 'ponytail-mindset', 'react', 'ui-ux-pro'];
|
|
77
|
+
}
|
|
78
|
+
|
|
69
79
|
let resolvedChars = agentsMdChars;
|
|
70
80
|
let resolvedSkillCount = 0;
|
|
71
81
|
for (const s of typicalResolvedSkills) {
|
|
@@ -86,6 +96,7 @@ function calculateContextStats(projectDir = process.cwd()) {
|
|
|
86
96
|
totalSkillCount,
|
|
87
97
|
profiledSkillCount,
|
|
88
98
|
resolvedSkillCount,
|
|
99
|
+
resolvedSkillList: typicalResolvedSkills,
|
|
89
100
|
profileName,
|
|
90
101
|
full: { chars: fullChars, tokens: fullTokens },
|
|
91
102
|
profile: { chars: profileChars, tokens: profileTokens, savings: profileSavings },
|
|
@@ -96,28 +107,30 @@ function calculateContextStats(projectDir = process.cwd()) {
|
|
|
96
107
|
function runStats(projectDir = process.cwd()) {
|
|
97
108
|
const stats = calculateContextStats(projectDir);
|
|
98
109
|
|
|
99
|
-
console.log('\nContextOS — Context Savings Report\n');
|
|
110
|
+
console.log('\nContextOS — Context Savings Report & Payload Reduction\n');
|
|
100
111
|
console.log('┌──────────────────────────────────────┬─────────────┬──────────┐');
|
|
101
|
-
console.log('│ Mode
|
|
112
|
+
console.log('│ Context Mode │ Est. Tokens │ Savings │');
|
|
102
113
|
console.log('├──────────────────────────────────────┼─────────────┼──────────┤');
|
|
103
114
|
|
|
104
|
-
const fullLabel = `Full (${stats.totalSkillCount} skills)`;
|
|
115
|
+
const fullLabel = `Full (${stats.totalSkillCount} skills catalog)`;
|
|
105
116
|
const fullTok = stats.full.tokens.toLocaleString().padStart(9);
|
|
106
|
-
console.log(`│ ${fullLabel.padEnd(36)} │ ${fullTok} │
|
|
117
|
+
console.log(`│ ${fullLabel.slice(0, 36).padEnd(36)} │ ${fullTok} │ baseline │`);
|
|
107
118
|
|
|
108
119
|
const profLabel = `Profile: ${stats.profileName} (${stats.profiledSkillCount} skills)`;
|
|
109
120
|
const profTok = stats.profile.tokens.toLocaleString().padStart(9);
|
|
110
121
|
const profSav = `${stats.profile.savings}%`.padStart(7);
|
|
111
122
|
console.log(`│ ${profLabel.slice(0, 36).padEnd(36)} │ ${profTok} │ ${profSav} │`);
|
|
112
123
|
|
|
113
|
-
const resLabel = `
|
|
124
|
+
const resLabel = `Dynamic: resolved task (${stats.resolvedSkillCount} skills)`;
|
|
114
125
|
const resTok = stats.resolved.tokens.toLocaleString().padStart(9);
|
|
115
126
|
const resSav = `${stats.resolved.savings}%`.padStart(7);
|
|
116
127
|
console.log(`│ ${resLabel.slice(0, 36).padEnd(36)} │ ${resTok} │ ${resSav} │`);
|
|
117
128
|
|
|
118
129
|
console.log('└──────────────────────────────────────┴─────────────┴──────────┘');
|
|
119
|
-
console.log(
|
|
120
|
-
console.log('
|
|
130
|
+
console.log(`Resolved skills for project: ${stats.resolvedSkillList.join(', ')}`);
|
|
131
|
+
console.log('\nNote: Character-based token estimation (~4 chars/token heuristic).');
|
|
132
|
+
console.log('Measures static rule payload reduction against an uncurated full-catalog dump.');
|
|
133
|
+
console.log('Real workflow cost savings depend on prompt turns, model tokenizer, and task complexity.\n');
|
|
121
134
|
|
|
122
135
|
return stats;
|
|
123
136
|
}
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* .agents/runtime/event-store.js
|
|
3
|
+
* ContextOS — Event-Sourced Durable Event Store
|
|
4
|
+
*
|
|
5
|
+
* Implements Section 17.3 of CONTEXTOS_IMPLEMENTATION_PLAN.md:
|
|
6
|
+
* - Monotonically incrementing revision log with SHA-256 event checksums
|
|
7
|
+
* - Periodic snapshots with replay recovery
|
|
8
|
+
* - Torn tail detection and automatic quarantine (zero lost valid snapshots)
|
|
9
|
+
* - Atomic event append with collision prevention
|
|
10
|
+
* - Zero install-time dependencies (pure Node.js crypto & fs)
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
'use strict';
|
|
14
|
+
|
|
15
|
+
const fs = require('fs');
|
|
16
|
+
const path = require('path');
|
|
17
|
+
const crypto = require('crypto');
|
|
18
|
+
|
|
19
|
+
function sha256(str) {
|
|
20
|
+
return crypto.createHash('sha256').update(str || '').digest('hex');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
class DurableEventStore {
|
|
24
|
+
/**
|
|
25
|
+
* @param {Object} options
|
|
26
|
+
* @param {string} options.baseDir - Base state directory (.agents/.contextos/state)
|
|
27
|
+
* @param {number} [options.snapshotInterval=50] - Events between snapshots
|
|
28
|
+
*/
|
|
29
|
+
constructor(options = {}) {
|
|
30
|
+
if (!options.baseDir) {
|
|
31
|
+
throw new Error('DurableEventStore requires baseDir');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
this.baseDir = path.resolve(options.baseDir);
|
|
35
|
+
this.eventsDir = path.join(this.baseDir, 'events');
|
|
36
|
+
this.snapshotsDir = path.join(this.baseDir, 'snapshots');
|
|
37
|
+
this.quarantineDir = path.join(this.baseDir, 'quarantine');
|
|
38
|
+
this.snapshotInterval = Math.max(5, options.snapshotInterval || 50);
|
|
39
|
+
|
|
40
|
+
fs.mkdirSync(this.eventsDir, { recursive: true });
|
|
41
|
+
fs.mkdirSync(this.snapshotsDir, { recursive: true });
|
|
42
|
+
fs.mkdirSync(this.quarantineDir, { recursive: true });
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Appends an immutable event to the log.
|
|
47
|
+
*
|
|
48
|
+
* @param {string} streamId - Identifier of the entity/thread
|
|
49
|
+
* @param {Object} eventData - Payload data
|
|
50
|
+
* @param {number} expectedRevision - Optimistic concurrency revision check
|
|
51
|
+
* @returns {Object} Stored event record
|
|
52
|
+
*/
|
|
53
|
+
append(streamId, eventData = {}, expectedRevision = null) {
|
|
54
|
+
const streamDir = path.join(this.eventsDir, streamId);
|
|
55
|
+
fs.mkdirSync(streamDir, { recursive: true });
|
|
56
|
+
|
|
57
|
+
// Determine next monotonic revision
|
|
58
|
+
const existingEvents = this._listEventFiles(streamId);
|
|
59
|
+
const lastRevision = existingEvents.length > 0 ? existingEvents[existingEvents.length - 1].revision : 0;
|
|
60
|
+
|
|
61
|
+
if (typeof expectedRevision === 'number' && expectedRevision !== lastRevision) {
|
|
62
|
+
const err = new Error(
|
|
63
|
+
`Optimistic concurrency conflict in stream "${streamId}": expected revision ${expectedRevision}, last revision is ${lastRevision}`
|
|
64
|
+
);
|
|
65
|
+
err.code = 'CTX_EVENT_CONCURRENCY_CONFLICT';
|
|
66
|
+
throw err;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const nextRevision = lastRevision + 1;
|
|
70
|
+
const eventId = crypto.randomUUID();
|
|
71
|
+
const timestamp = Date.now();
|
|
72
|
+
|
|
73
|
+
const record = {
|
|
74
|
+
streamId,
|
|
75
|
+
eventId,
|
|
76
|
+
revision: nextRevision,
|
|
77
|
+
timestamp,
|
|
78
|
+
data: eventData,
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const checksum = sha256(JSON.stringify(record));
|
|
82
|
+
const fullEnvelope = {
|
|
83
|
+
...record,
|
|
84
|
+
checksum,
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const revPadded = String(nextRevision).padStart(6, '0');
|
|
88
|
+
const filename = `${revPadded}-${eventId}.json`;
|
|
89
|
+
const targetFile = path.join(streamDir, filename);
|
|
90
|
+
const tempFile = `${targetFile}.${crypto.randomBytes(4).toString('hex')}.tmp`;
|
|
91
|
+
|
|
92
|
+
try {
|
|
93
|
+
fs.writeFileSync(tempFile, JSON.stringify(fullEnvelope, null, 2), 'utf8');
|
|
94
|
+
fs.renameSync(tempFile, targetFile);
|
|
95
|
+
} catch (err) {
|
|
96
|
+
try { if (fs.existsSync(tempFile)) fs.unlinkSync(tempFile); } catch {}
|
|
97
|
+
throw err;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return Object.freeze(fullEnvelope);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Creates a snapshot of the reduced state up to a given revision.
|
|
105
|
+
*
|
|
106
|
+
* @param {string} streamId
|
|
107
|
+
* @param {Object} state
|
|
108
|
+
* @param {number} revision
|
|
109
|
+
* @returns {Object} Snapshot record
|
|
110
|
+
*/
|
|
111
|
+
createSnapshot(streamId, state, revision) {
|
|
112
|
+
const streamSnapDir = path.join(this.snapshotsDir, streamId);
|
|
113
|
+
fs.mkdirSync(streamSnapDir, { recursive: true });
|
|
114
|
+
|
|
115
|
+
const revPadded = String(revision).padStart(6, '0');
|
|
116
|
+
const filename = `snapshot-${revPadded}.json`;
|
|
117
|
+
const targetFile = path.join(streamSnapDir, filename);
|
|
118
|
+
const tempFile = `${targetFile}.${crypto.randomBytes(4).toString('hex')}.tmp`;
|
|
119
|
+
|
|
120
|
+
const snapshot = {
|
|
121
|
+
streamId,
|
|
122
|
+
revision,
|
|
123
|
+
timestamp: Date.now(),
|
|
124
|
+
state,
|
|
125
|
+
checksum: sha256(JSON.stringify(state)),
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
try {
|
|
129
|
+
fs.writeFileSync(tempFile, JSON.stringify(snapshot, null, 2), 'utf8');
|
|
130
|
+
fs.renameSync(tempFile, targetFile);
|
|
131
|
+
} catch (err) {
|
|
132
|
+
try { if (fs.existsSync(tempFile)) fs.unlinkSync(tempFile); } catch {}
|
|
133
|
+
throw err;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return Object.freeze(snapshot);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Reads latest valid snapshot for stream if available.
|
|
141
|
+
*
|
|
142
|
+
* @param {string} streamId
|
|
143
|
+
* @returns {Object|null}
|
|
144
|
+
*/
|
|
145
|
+
getLatestSnapshot(streamId) {
|
|
146
|
+
const streamSnapDir = path.join(this.snapshotsDir, streamId);
|
|
147
|
+
if (!fs.existsSync(streamSnapDir)) return null;
|
|
148
|
+
|
|
149
|
+
const files = fs.readdirSync(streamSnapDir)
|
|
150
|
+
.filter(f => f.startsWith('snapshot-') && f.endsWith('.json'))
|
|
151
|
+
.sort();
|
|
152
|
+
|
|
153
|
+
if (files.length === 0) return null;
|
|
154
|
+
|
|
155
|
+
const latestFile = files[files.length - 1];
|
|
156
|
+
try {
|
|
157
|
+
const content = fs.readFileSync(path.join(streamSnapDir, latestFile), 'utf8');
|
|
158
|
+
const snap = JSON.parse(content);
|
|
159
|
+
// Check integrity
|
|
160
|
+
if (snap.checksum && snap.checksum !== sha256(JSON.stringify(snap.state))) {
|
|
161
|
+
// Corrupted snapshot -> quarantine
|
|
162
|
+
this._quarantineFile(path.join(streamSnapDir, latestFile), 'corrupted_snapshot_checksum');
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
return snap;
|
|
166
|
+
} catch {
|
|
167
|
+
this._quarantineFile(path.join(streamSnapDir, latestFile), 'unparseable_snapshot');
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Reads all valid events in stream.
|
|
174
|
+
*
|
|
175
|
+
* @param {string} streamId
|
|
176
|
+
* @returns {Array<Object>}
|
|
177
|
+
*/
|
|
178
|
+
readStream(streamId) {
|
|
179
|
+
const eventFiles = this._listEventFiles(streamId);
|
|
180
|
+
const events = [];
|
|
181
|
+
for (const item of eventFiles) {
|
|
182
|
+
const evt = this._readAndVerifyEvent(item.filePath);
|
|
183
|
+
if (evt) {
|
|
184
|
+
events.push(evt);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return events;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Replays events from snapshot or beginning to restore current state.
|
|
192
|
+
* Tolerates and quarantines torn tail events from unexpected crashes.
|
|
193
|
+
*
|
|
194
|
+
* @param {string} streamId
|
|
195
|
+
* @param {Function} reducer - (state, event) => newState
|
|
196
|
+
* @param {Object} [initialState={}]
|
|
197
|
+
* @returns {{ state: Object, revision: number, replayedEvents: number, quarantinedCount: number }}
|
|
198
|
+
*/
|
|
199
|
+
replay(streamId, reducer, initialState = {}) {
|
|
200
|
+
if (typeof reducer !== 'function') {
|
|
201
|
+
throw new Error('replay requires a reducer function');
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const snapshot = this.getLatestSnapshot(streamId);
|
|
205
|
+
let state = snapshot ? JSON.parse(JSON.stringify(snapshot.state)) : JSON.parse(JSON.stringify(initialState));
|
|
206
|
+
let lastRevision = snapshot ? snapshot.revision : 0;
|
|
207
|
+
|
|
208
|
+
const eventFiles = this._listEventFiles(streamId);
|
|
209
|
+
let replayedEvents = 0;
|
|
210
|
+
let quarantinedCount = 0;
|
|
211
|
+
|
|
212
|
+
for (const item of eventFiles) {
|
|
213
|
+
if (item.revision <= lastRevision) continue;
|
|
214
|
+
|
|
215
|
+
const eventData = this._readAndVerifyEvent(item.filePath);
|
|
216
|
+
if (!eventData) {
|
|
217
|
+
// Torn tail detected at end of stream! Quarantine it to recover.
|
|
218
|
+
this._quarantineFile(item.filePath, 'torn_tail_corrupted_event');
|
|
219
|
+
quarantinedCount++;
|
|
220
|
+
break; // Stop replaying past torn tail
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
state = reducer(state, eventData);
|
|
224
|
+
lastRevision = eventData.revision;
|
|
225
|
+
replayedEvents++;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return {
|
|
229
|
+
state: Object.freeze(state),
|
|
230
|
+
revision: lastRevision,
|
|
231
|
+
replayedEvents,
|
|
232
|
+
quarantinedCount,
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
_listEventFiles(streamId) {
|
|
237
|
+
const streamDir = path.join(this.eventsDir, streamId);
|
|
238
|
+
if (!fs.existsSync(streamDir)) return [];
|
|
239
|
+
|
|
240
|
+
const files = fs.readdirSync(streamDir)
|
|
241
|
+
.filter(f => /^\d{6}-[a-zA-Z0-9-]+\.json$/.test(f))
|
|
242
|
+
.sort();
|
|
243
|
+
|
|
244
|
+
return files.map(f => {
|
|
245
|
+
const rev = parseInt(f.slice(0, 6), 10);
|
|
246
|
+
return {
|
|
247
|
+
filename: f,
|
|
248
|
+
revision: rev,
|
|
249
|
+
filePath: path.join(streamDir, f),
|
|
250
|
+
};
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
_readAndVerifyEvent(filePath) {
|
|
255
|
+
try {
|
|
256
|
+
const content = fs.readFileSync(filePath, 'utf8');
|
|
257
|
+
const envelope = JSON.parse(content);
|
|
258
|
+
|
|
259
|
+
if (!envelope.revision || !envelope.checksum) {
|
|
260
|
+
return null;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const { checksum, ...rest } = envelope;
|
|
264
|
+
const expectedChecksum = sha256(JSON.stringify(rest));
|
|
265
|
+
if (checksum !== expectedChecksum) {
|
|
266
|
+
return null; // Checksum mismatch
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
return envelope;
|
|
270
|
+
} catch {
|
|
271
|
+
return null;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
_quarantineFile(filePath, reason) {
|
|
276
|
+
try {
|
|
277
|
+
const filename = path.basename(filePath);
|
|
278
|
+
const target = path.join(this.quarantineDir, `${Date.now()}-${reason}-${filename}`);
|
|
279
|
+
fs.renameSync(filePath, target);
|
|
280
|
+
} catch {
|
|
281
|
+
// ignore
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
module.exports = {
|
|
287
|
+
DurableEventStore,
|
|
288
|
+
};
|