@context-os/core 1.0.0
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/dist/index.d.ts +24 -0
- package/dist/index.js +88 -0
- package/dist/tests/security.test.d.ts +1 -0
- package/dist/tests/security.test.js +33 -0
- package/package.json +31 -0
- package/schemas/context.schema.json +25 -0
- package/schemas/decision.schema.json +24 -0
- package/schemas/memory.schema.json +24 -0
- package/schemas/soul.schema.json +27 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Discovers the workspace root by looking for root/soul.md in parent directories.
|
|
3
|
+
*/
|
|
4
|
+
export declare function findWorkspaceRoot(): string;
|
|
5
|
+
export declare const workspaceRoot: string;
|
|
6
|
+
/**
|
|
7
|
+
* Standard ContextOS "Buckets" for security isolation.
|
|
8
|
+
*/
|
|
9
|
+
export declare const ALLOWED_BUCKETS: string[];
|
|
10
|
+
/**
|
|
11
|
+
* Validates that a path is within the workspace root and inside an allowed bucket.
|
|
12
|
+
*/
|
|
13
|
+
export declare function validatePath(requestedPath: string): {
|
|
14
|
+
fullPath: string;
|
|
15
|
+
relativePath: string;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Checks if a path is in a read-only bucket for agents.
|
|
19
|
+
*/
|
|
20
|
+
export declare function isReadOnly(filePath: string): boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Executes an atomic git transaction (Add + Commit).
|
|
23
|
+
*/
|
|
24
|
+
export declare function gitCommit(filePath: string, message: string): Promise<void>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import { spawn } from 'node:child_process';
|
|
4
|
+
/**
|
|
5
|
+
* Discovers the workspace root by looking for root/soul.md in parent directories.
|
|
6
|
+
*/
|
|
7
|
+
export function findWorkspaceRoot() {
|
|
8
|
+
let current = process.cwd();
|
|
9
|
+
const root = path.parse(current).root;
|
|
10
|
+
while (current !== root) {
|
|
11
|
+
if (fs.existsSync(path.join(current, "root", "soul.md"))) {
|
|
12
|
+
return fs.realpathSync(current);
|
|
13
|
+
}
|
|
14
|
+
current = path.dirname(current);
|
|
15
|
+
}
|
|
16
|
+
return fs.realpathSync(process.cwd()); // Fallback to CWD
|
|
17
|
+
}
|
|
18
|
+
export const workspaceRoot = findWorkspaceRoot();
|
|
19
|
+
/**
|
|
20
|
+
* Standard ContextOS "Buckets" for security isolation.
|
|
21
|
+
*/
|
|
22
|
+
export const ALLOWED_BUCKETS = [
|
|
23
|
+
"projects",
|
|
24
|
+
"knowledge",
|
|
25
|
+
"schemas",
|
|
26
|
+
"archive",
|
|
27
|
+
"log",
|
|
28
|
+
"orgs",
|
|
29
|
+
"root"
|
|
30
|
+
];
|
|
31
|
+
/**
|
|
32
|
+
* Validates that a path is within the workspace root and inside an allowed bucket.
|
|
33
|
+
*/
|
|
34
|
+
export function validatePath(requestedPath) {
|
|
35
|
+
const resolvedPath = path.resolve(workspaceRoot, requestedPath);
|
|
36
|
+
let fullPath;
|
|
37
|
+
try {
|
|
38
|
+
fullPath = fs.realpathSync(resolvedPath);
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
fullPath = resolvedPath;
|
|
42
|
+
}
|
|
43
|
+
const relativePath = path.relative(workspaceRoot, fullPath);
|
|
44
|
+
// Security check: must be within the workspace root
|
|
45
|
+
if (relativePath.startsWith("..") || path.isAbsolute(relativePath)) {
|
|
46
|
+
throw new Error(`Security violation: Path ${requestedPath} is outside the allowed ContextOS workspace root.`);
|
|
47
|
+
}
|
|
48
|
+
// Enterprise check: must be within an allowed bucket
|
|
49
|
+
const isAllowed = ALLOWED_BUCKETS.some(bucket => {
|
|
50
|
+
const bucketRoot = path.join(workspaceRoot, bucket);
|
|
51
|
+
const bucketRelative = path.relative(bucketRoot, fullPath);
|
|
52
|
+
return !bucketRelative.startsWith("..") && !path.isAbsolute(bucketRelative);
|
|
53
|
+
});
|
|
54
|
+
if (!isAllowed) {
|
|
55
|
+
throw new Error(`Security violation: Path ${requestedPath} is outside the allowed bucket (projects, orgs, knowledge, schemas, etc).`);
|
|
56
|
+
}
|
|
57
|
+
return { fullPath, relativePath };
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Checks if a path is in a read-only bucket for agents.
|
|
61
|
+
*/
|
|
62
|
+
export function isReadOnly(filePath) {
|
|
63
|
+
const { fullPath } = validatePath(filePath);
|
|
64
|
+
const readOnlyBuckets = ["knowledge", "schemas", "root"];
|
|
65
|
+
return readOnlyBuckets.some(bucket => {
|
|
66
|
+
const bucketRoot = path.join(workspaceRoot, bucket);
|
|
67
|
+
const bucketRelative = path.relative(bucketRoot, fullPath);
|
|
68
|
+
return !bucketRelative.startsWith("..") && !path.isAbsolute(bucketRelative);
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Executes an atomic git transaction (Add + Commit).
|
|
73
|
+
*/
|
|
74
|
+
export async function gitCommit(filePath, message) {
|
|
75
|
+
return new Promise((resolve, reject) => {
|
|
76
|
+
const add = spawn("git", ["add", filePath], { cwd: workspaceRoot });
|
|
77
|
+
add.on("close", (code) => {
|
|
78
|
+
if (code !== 0 && code !== null) {
|
|
79
|
+
return reject(new Error(`Git add failed with code ${code}`));
|
|
80
|
+
}
|
|
81
|
+
const commit = spawn("git", ["commit", "-m", message], { cwd: workspaceRoot });
|
|
82
|
+
commit.on("close", (code) => {
|
|
83
|
+
// If code is not 0, it might be "nothing to commit" which is fine for our tools
|
|
84
|
+
resolve();
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import assert from "node:assert";
|
|
2
|
+
import { validatePath, findWorkspaceRoot, ALLOWED_BUCKETS, isReadOnly } from "../index.js";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
describe("Core Security Engine", () => {
|
|
5
|
+
const workspaceRoot = findWorkspaceRoot();
|
|
6
|
+
it("should discover the workspace root (contains root/soul.md)", () => {
|
|
7
|
+
assert.strictEqual(typeof workspaceRoot, "string");
|
|
8
|
+
assert.notStrictEqual(workspaceRoot, "/");
|
|
9
|
+
});
|
|
10
|
+
it("should allow paths within projects directory", () => {
|
|
11
|
+
const validPath = path.join(workspaceRoot, "projects", "TestProject", "memory.md");
|
|
12
|
+
assert.doesNotThrow(() => validatePath(validPath));
|
|
13
|
+
});
|
|
14
|
+
it("should block directory traversal attacks (..)", () => {
|
|
15
|
+
const maliciousPath = path.join(workspaceRoot, "projects", "..", "..", "package.json");
|
|
16
|
+
assert.throws(() => validatePath(maliciousPath), /Security violation/);
|
|
17
|
+
});
|
|
18
|
+
it("should block access to root config files (package.json)", () => {
|
|
19
|
+
const rootConfig = path.join(workspaceRoot, "package.json");
|
|
20
|
+
assert.throws(() => validatePath(rootConfig), /Security violation/);
|
|
21
|
+
});
|
|
22
|
+
it("should report knowledge, schemas, and root as read-only", () => {
|
|
23
|
+
assert.strictEqual(isReadOnly("knowledge/domains/ai.md"), true);
|
|
24
|
+
assert.strictEqual(isReadOnly("schemas/project.json"), true);
|
|
25
|
+
assert.strictEqual(isReadOnly("root/soul.md"), true);
|
|
26
|
+
assert.strictEqual(isReadOnly("projects/ContextOS/memory.md"), false);
|
|
27
|
+
});
|
|
28
|
+
it("should have a fixed list of allowed security buckets", () => {
|
|
29
|
+
assert.ok(ALLOWED_BUCKETS.includes("projects"));
|
|
30
|
+
assert.ok(ALLOWED_BUCKETS.includes("knowledge"));
|
|
31
|
+
assert.ok(ALLOWED_BUCKETS.includes("schemas"));
|
|
32
|
+
});
|
|
33
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@context-os/core",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Shared intelligence layer, schemas, and security buckets for ContextOS",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist",
|
|
12
|
+
"schemas",
|
|
13
|
+
"README.md"
|
|
14
|
+
],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "tsc",
|
|
17
|
+
"test": "npm run build && mocha dist/tests/**/*.test.js"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"fs-extra": "^11.2.0"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@types/chai": "^5.2.3",
|
|
24
|
+
"@types/fs-extra": "^11.0.4",
|
|
25
|
+
"@types/mocha": "^10.0.10",
|
|
26
|
+
"@types/node": "^22.13.9",
|
|
27
|
+
"chai": "^5.2.0",
|
|
28
|
+
"mocha": "^11.1.0",
|
|
29
|
+
"typescript": "^5.8.2"
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
3
|
+
"title": "ContextOS Context Schema",
|
|
4
|
+
"description": "Schema for project CONTEXT.md files.",
|
|
5
|
+
"type": "object",
|
|
6
|
+
"required": ["Overview", "Goals"],
|
|
7
|
+
"properties": {
|
|
8
|
+
"Overview": { "type": "string" },
|
|
9
|
+
"Goals": {
|
|
10
|
+
"type": "array",
|
|
11
|
+
"items": { "type": "string" },
|
|
12
|
+
"minItems": 1
|
|
13
|
+
},
|
|
14
|
+
"Stack": {
|
|
15
|
+
"type": "array",
|
|
16
|
+
"items": { "type": "string" }
|
|
17
|
+
},
|
|
18
|
+
"Metadata": {
|
|
19
|
+
"type": "object",
|
|
20
|
+
"properties": {
|
|
21
|
+
"Tags": { "type": "array", "items": { "type": "string" } }
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
3
|
+
"title": "ContextOS Decision Schema",
|
|
4
|
+
"description": "Schema for project decisions.md files.",
|
|
5
|
+
"type": "object",
|
|
6
|
+
"properties": {
|
|
7
|
+
"Decisions": {
|
|
8
|
+
"type": "array",
|
|
9
|
+
"items": {
|
|
10
|
+
"type": "object",
|
|
11
|
+
"required": ["Id", "Title", "Date", "Status", "Context", "Decision", "Rationale"],
|
|
12
|
+
"properties": {
|
|
13
|
+
"Id": { "type": "string", "pattern": "^ADR-\\d{4}$" },
|
|
14
|
+
"Title": { "type": "string" },
|
|
15
|
+
"Date": { "type": "string", "format": "date" },
|
|
16
|
+
"Status": { "type": "string", "enum": ["Proposed", "Accepted", "Superseded", "Deprecated"] },
|
|
17
|
+
"Context": { "type": "string" },
|
|
18
|
+
"Decision": { "type": "string" },
|
|
19
|
+
"Rationale": { "type": "string" }
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
3
|
+
"title": "ContextOS Memory Schema",
|
|
4
|
+
"description": "Schema for project memory.md files.",
|
|
5
|
+
"type": "object",
|
|
6
|
+
"required": ["Overview"],
|
|
7
|
+
"properties": {
|
|
8
|
+
"Overview": { "type": "string" },
|
|
9
|
+
"Learnings": {
|
|
10
|
+
"type": "array",
|
|
11
|
+
"items": { "type": "string" }
|
|
12
|
+
},
|
|
13
|
+
"Patterns": {
|
|
14
|
+
"type": "array",
|
|
15
|
+
"items": { "type": "string" }
|
|
16
|
+
},
|
|
17
|
+
"Metadata": {
|
|
18
|
+
"type": "object",
|
|
19
|
+
"properties": {
|
|
20
|
+
"Tags": { "type": "array", "items": { "type": "string" } }
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
3
|
+
"title": "ContextOS Soul Schema",
|
|
4
|
+
"description": "Schema for project SOUL.md files defining identity and values.",
|
|
5
|
+
"type": "object",
|
|
6
|
+
"required": ["Identity", "Core Principles", "Behavioral Rules"],
|
|
7
|
+
"properties": {
|
|
8
|
+
"Identity": {
|
|
9
|
+
"type": "string",
|
|
10
|
+
"description": "High-level description of the project's purpose."
|
|
11
|
+
},
|
|
12
|
+
"Core Principles": {
|
|
13
|
+
"type": "array",
|
|
14
|
+
"items": { "type": "string" },
|
|
15
|
+
"minItems": 1
|
|
16
|
+
},
|
|
17
|
+
"Behavioral Rules": {
|
|
18
|
+
"type": "array",
|
|
19
|
+
"items": { "type": "string" },
|
|
20
|
+
"minItems": 1
|
|
21
|
+
},
|
|
22
|
+
"Constraints": {
|
|
23
|
+
"type": "array",
|
|
24
|
+
"items": { "type": "string" }
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|