@promptowl/contextnest-community 0.1.0-alpha.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.
@@ -0,0 +1,118 @@
1
+ # Configuration
2
+
3
+ All server configuration is driven by environment variables. Set them in your shell, in a `.env` file, in a Docker image, or via your process manager — however you deploy the rest of your stack.
4
+
5
+ ## Auth modes
6
+
7
+ The server supports two authentication modes. You pick one per instance with `AUTH_MODE`.
8
+
9
+ ### `AUTH_MODE=key` (default)
10
+
11
+ Every request requires an `Authorization: Bearer cnst_...` header. Use this for any multi-user or internet-facing deployment.
12
+
13
+ - Users register via `POST /auth/register` (email + password) or log in with PromptOwl via `POST /auth/device` → `POST /auth/promptowl`.
14
+ - API keys are per-user (`cnst_<64-hex>`) and can be scoped to a single nest for service-account use.
15
+ - Rate-limited login / register / device-auth endpoints (sliding window, per IP + per email).
16
+ - First PromptOwl-authenticated user becomes the server admin (atomic claim). Admin can invite teammates at `POST /auth/invite`.
17
+
18
+ Clients include the token on every request:
19
+
20
+ ```bash
21
+ curl -H 'Authorization: Bearer cnst_<your-token>' http://your-server/nests
22
+ ```
23
+
24
+ ### `AUTH_MODE=open`
25
+
26
+ No authentication required. Every request is attributed to the anonymous admin user (`00000000-...`). Everyone effectively owns every anon-created nest.
27
+
28
+ **Use this only when:**
29
+ - You're running locally for yourself (`bind 127.0.0.1`), or
30
+ - You're on a trusted LAN and don't care about access controls, or
31
+ - You're behind an upstream reverse proxy that already authenticates.
32
+
33
+ **Don't use this when:**
34
+ - The port is reachable from the public internet
35
+ - Multiple people share the deployment and need isolated data
36
+ - You need audit trails (every write shows up under "anonymous admin")
37
+
38
+ The server prints a loud warning at startup when `AUTH_MODE=open` is active.
39
+
40
+ ---
41
+
42
+ ## Full env var reference
43
+
44
+ | Var | Default | Purpose |
45
+ |---|---|---|
46
+ | `PORT` | `3838` | HTTP port the server listens on. |
47
+ | `DATA_ROOT` | `./data` (relative to cwd) | Root directory for SQLite DB + nest filesystem vaults. Set to an absolute path in production so it's independent of where you `cd`'d from. |
48
+ | `DATABASE_PATH` | `$DATA_ROOT/community.db` | Override the SQLite file location explicitly. |
49
+ | `AUTH_MODE` | `key` | `key` or `open`. See above. |
50
+ | `PROMPTOWL_API_URL` | `https://app.promptowl.ai` | PromptOwl's API origin — used for device auth, license validation, telemetry. Override for air-gapped or test setups. |
51
+ | `PROMPTOWL_KEY` | `""` | Your PromptOwl Community Server license key (`pk_...`). Unlicensed instances still run but some features may be limited. |
52
+ | `TELEMETRY_ENABLED` | `"true"` (set to `"false"` to disable) | Batched, anonymized usage events sent to PromptOwl. Off disables the loop entirely. |
53
+ | `TELEMETRY_INTERVAL_MS` | `3600000` (1 hour) | How often buffered telemetry is flushed to PromptOwl. |
54
+ | `CORS_ORIGINS` | `*` in open mode; `http://localhost:5173,http://localhost:3838` in key mode | Comma-separated allowlist. Set to `*` to allow any origin (**only** safe in open mode — in key mode with Bearer tokens this enables CSRF). |
55
+ | `MAX_BODY_BYTES` | `10485760` (10 MB) | Reject requests whose `Content-Length` exceeds this. Prevents giant-payload DoS. |
56
+
57
+ ---
58
+
59
+ ## Typical deployments
60
+
61
+ ### Local dev / single user
62
+
63
+ ```bash
64
+ AUTH_MODE=open DATA_ROOT=./my-data pnpm dev
65
+ ```
66
+
67
+ Or the default dev mode if `pnpm dev` already sets `AUTH_MODE=open` in your scripts.
68
+
69
+ ### Team / multi-user behind a reverse proxy
70
+
71
+ ```bash
72
+ AUTH_MODE=key \
73
+ CORS_ORIGINS="https://team.example.com,https://admin.example.com" \
74
+ DATA_ROOT=/var/lib/contextnest \
75
+ PROMPTOWL_KEY=pk_... \
76
+ pnpm start
77
+ ```
78
+
79
+ Terminate TLS at the proxy, forward `X-Forwarded-For` and `X-Real-IP` headers (the rate limiter reads them), and bind the server to `127.0.0.1` so only the proxy can reach it.
80
+
81
+ ### Hosted / commercial SaaS
82
+
83
+ Same as team, plus:
84
+ - Run N instances behind a load balancer sharing a network-attached `DATA_ROOT` (note: SQLite + shared filesystem is not a great long-term story — migrate to the MongoDB storage adapter when scaling beyond one box).
85
+ - Set `TELEMETRY_ENABLED=true` and a valid `PROMPTOWL_KEY` so usage rolls up to PromptOwl.
86
+ - Rotate keys regularly via `DELETE /auth/keys/:id` + `POST /auth/keys`.
87
+
88
+ ---
89
+
90
+ ## access.yaml (optional, open + key mode)
91
+
92
+ Drop an `access.yaml` at `$DATA_ROOT/access.yaml` to add a server-level ABAC layer — useful even in key mode for super-admins and group-based defaults.
93
+
94
+ ```yaml
95
+ mode: restricted
96
+ allowed_users:
97
+ - "*.acme.com" # email wildcard — anyone @acme.com
98
+ - "partner@vendor.com" # exact match
99
+ super_admins:
100
+ - "ceo@acme.com" # always allowed on every nest
101
+ groups:
102
+ engineering:
103
+ default_permission: write
104
+ members:
105
+ - "*.eng.acme.com"
106
+ viewers:
107
+ default_permission: read
108
+ members:
109
+ - "*.contractor.acme.com"
110
+ ```
111
+
112
+ See `STEWARDSHIP.md` for how super-admins and groups interact with per-nest stewardship.
113
+
114
+ ---
115
+
116
+ ## Reload
117
+
118
+ Changing env vars requires a restart — the server reads them at boot. `tsx watch` in dev will pick up code changes automatically but not env changes.
package/LICENSE.md ADDED
@@ -0,0 +1,142 @@
1
+ # ContextNest Community Edition — Commercial Software License
2
+
3
+ **Version 1.0 — Effective Date: April 24, 2026**
4
+
5
+ Copyright © 2026 Promptowl LLC. All rights reserved.
6
+
7
+ This ContextNest Community Edition Commercial Software License (the **"License"**) is a legal agreement between you (either an individual or a single legal entity, **"You"**) and **Promptowl LLC**, a Georgia limited liability company with its principal place of business at 3060 Mercer University Dr Ste 110, Atlanta, GA 30341, USA (**"Licensor,"** "we," "us," or "our"), governing Your use of the ContextNest Community Edition software, including its command-line interface, server, user interface, source or compiled code, and any related documentation (collectively, the **"Software"**).
8
+
9
+ **BY INSTALLING, COPYING, EXECUTING, OR OTHERWISE USING THE SOFTWARE, YOU AGREE TO BE BOUND BY THIS LICENSE. IF YOU DO NOT AGREE, DO NOT INSTALL OR USE THE SOFTWARE.**
10
+
11
+ ---
12
+
13
+ ## 1. Software and Service Relationship
14
+
15
+ The Software is a client that interoperates with the hosted **PromptOwl** platform operated by Licensor (the **"Platform"**). Operation of the Software requires a valid PromptOwl account. Your use of the Platform, and any acceptance of Platform-level terms presented upon login or account creation, is governed separately by:
16
+
17
+ - **End User License Agreement** — <https://promptowl.ai/eula/>
18
+ - **Terms of Service** — <https://promptowl.ai/terms-of-service/>
19
+ - **Privacy Policy** — <https://promptowl.ai/privacy-policy/>
20
+ - **Acceptable Use Policy** — <https://promptowl.ai/acceptable-use/>
21
+ - **Disclaimer** — <https://promptowl.ai/disclaimer/>
22
+ - **Cookie Policy** — <https://promptowl.ai/cookies/>
23
+
24
+ (collectively, the **"Platform Terms"**). The Platform Terms are incorporated into this License by reference with respect to any use of the Platform. In the event of a conflict between this License and the Platform Terms regarding the Software itself (as distinct from the Platform), this License controls.
25
+
26
+ ## 2. License Grant
27
+
28
+ Subject to Your continued compliance with this License and the Platform Terms, Licensor grants You a limited, non-exclusive, non-transferable, non-sublicensable, revocable license to:
29
+
30
+ **(a)** install and execute the Software on computing devices that You own or control;
31
+
32
+ **(b)** use the Software solely for Your own internal business purposes or personal non-commercial purposes, in connection with a valid PromptOwl account; and
33
+
34
+ **(c)** make a reasonable number of copies of the Software solely for backup, archival, or internal deployment purposes, provided that all copyright, trademark, and other proprietary notices are preserved intact.
35
+
36
+ ## 3. Restrictions
37
+
38
+ You shall not, and shall not permit any third party to:
39
+
40
+ **(a) Redistribution.** Sell, rent, lease, lend, sublicense, assign, distribute, publish, or otherwise make the Software available to any third party, whether standalone, bundled, or as part of a derivative work.
41
+
42
+ **(b) Hosting-as-a-Service.** Offer, operate, or make available the Software, or any substantial portion of its functionality, as a hosted, managed, software-as-a-service, or similar service to any third party. Hosting the Software for Your own internal use is permitted; hosting it for use by any person or entity outside Your legal entity is not.
43
+
44
+ **(c) Competitive Use.** Use the Software, in whole or in part, to develop, train, operate, or support any product or service that competes with ContextNest, PromptOwl, or any Licensor product.
45
+
46
+ **(d) Reverse Engineering.** Reverse engineer, decompile, disassemble, or otherwise attempt to derive the source code, underlying ideas, algorithms, file formats, or non-public APIs of the Software, except and only to the extent this restriction is expressly prohibited by applicable law.
47
+
48
+ **(e) Modification.** Modify, adapt, translate, or create derivative works of the Software, except for configuration files and integration code that You author and that are clearly separable from the Software itself.
49
+
50
+ **(f) Circumvention.** Circumvent, disable, or interfere with any license, authentication, metering, telemetry, or security mechanism of the Software or the Platform, including but not limited to the PromptOwl login requirement.
51
+
52
+ **(g) Notice Removal.** Remove, obscure, or alter any copyright, trademark, license, or other proprietary notice contained in or displayed by the Software.
53
+
54
+ **(h) Trademark Use.** Use the names, logos, or trademarks of Licensor, including "PromptOwl," "ContextNest," or any confusingly similar mark, in any forked, modified, or derivative distribution, or in a manner suggesting endorsement, affiliation, or sponsorship that does not exist.
55
+
56
+ **(i) Unlawful Use.** Use the Software in violation of any applicable law or regulation, or in any manner that violates the Acceptable Use Policy.
57
+
58
+ **(j) Regulated Industry Use.** Use the Software in environments subject to HIPAA, FISMA, GLBA, PCI-DSS, or similar industry-specific regulations, unless You have obtained express written authorization from Licensor. The Software is not certified for such environments.
59
+
60
+ ## 4. Ownership and Intellectual Property
61
+
62
+ The Software is licensed, not sold. Licensor and its licensors retain all right, title, and interest in and to the Software, including all intellectual property rights therein. No rights are granted to You other than those expressly set forth in this License. All rights not expressly granted are reserved by Licensor. "PromptOwl" and "ContextNest" are trademarks of Promptowl LLC.
63
+
64
+ ## 5. Account Requirement and Suspension
65
+
66
+ The Software's functionality is conditioned on a valid, active PromptOwl account in good standing. Licensor may suspend, terminate, or restrict account access (and, consequently, Software functionality) in accordance with the Platform Terms. Termination or suspension of Your PromptOwl account does not, by itself, waive Licensor's rights under this License.
67
+
68
+ ## 6. Updates
69
+
70
+ Licensor may, but is not obligated to, provide updates, patches, or new versions of the Software. Any such update is deemed part of the Software and is governed by this License unless accompanied by a separate license.
71
+
72
+ ## 7. Third-Party Components
73
+
74
+ The Software may include or depend upon open-source or third-party components, each of which is governed by its own license. A listing of such components and their licenses is provided in the Software distribution or in accompanying documentation. Nothing in this License limits Your rights or obligations under those third-party licenses.
75
+
76
+ ## 8. Commercial Licensing for Restricted Uses
77
+
78
+ If You wish to use the Software in a manner not permitted by Section 3 — including redistribution, hosting-as-a-service, embedding in a commercial product, or use in regulated-industry environments — You must obtain a separate written commercial license from Licensor. Contact: **hoot@promptowl.ai**.
79
+
80
+ ## 9. Term and Termination
81
+
82
+ **9.1** This License is effective upon Your first installation or use of the Software and continues until terminated.
83
+
84
+ **9.2** This License terminates automatically and without notice if You breach any term of this License. Licensor may also terminate this License at any time for material breach upon notice.
85
+
86
+ **9.3** Upon termination, You shall immediately (i) cease all use of the Software, (ii) delete or destroy all copies of the Software in Your possession or control, and (iii) certify such destruction in writing upon Licensor's request.
87
+
88
+ **9.4** Sections 3, 4, 9.3, 10, 11, 12, 13, and 14 survive termination.
89
+
90
+ ## 10. Disclaimer of Warranties
91
+
92
+ THE SOFTWARE IS PROVIDED **"AS IS"** AND **"AS AVAILABLE,"** WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED, OR STATUTORY, INCLUDING WITHOUT LIMITATION ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE, NON-INFRINGEMENT, ACCURACY, RELIABILITY, OR UNINTERRUPTED OPERATION. LICENSOR DOES NOT WARRANT THAT THE SOFTWARE WILL MEET YOUR REQUIREMENTS, OPERATE WITHOUT INTERRUPTION, OR BE ERROR-FREE. SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF IMPLIED WARRANTIES, IN WHICH CASE THE FOREGOING EXCLUSIONS APPLY TO THE MAXIMUM EXTENT PERMITTED BY LAW.
93
+
94
+ ## 11. AI Output Disclaimer
95
+
96
+ The Software enables the creation, storage, retrieval, and injection of content into large language models and other AI systems. Licensor makes no representation or warranty regarding the accuracy, legality, completeness, appropriateness, or non-infringement of any output generated by such systems. You are solely responsible for reviewing, validating, and determining the fitness of AI-generated output for Your intended use. You agree not to deploy the Software in high-risk contexts (including medical, legal, financial advisory, safety-critical, or life-critical environments) without appropriate human oversight and independent safeguards.
97
+
98
+ ## 12. Limitation of Liability
99
+
100
+ TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, IN NO EVENT SHALL LICENSOR, ITS AFFILIATES, OR ITS LICENSORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL, SPECIAL, CONSEQUENTIAL, EXEMPLARY, OR PUNITIVE DAMAGES, OR FOR ANY LOSS OF PROFITS, REVENUE, DATA, GOODWILL, OR BUSINESS OPPORTUNITY, ARISING OUT OF OR RELATED TO THIS LICENSE OR THE SOFTWARE, WHETHER IN CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY, OR ANY OTHER LEGAL THEORY, EVEN IF LICENSOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
101
+
102
+ LICENSOR'S TOTAL CUMULATIVE LIABILITY ARISING OUT OF OR RELATED TO THIS LICENSE OR THE SOFTWARE SHALL NOT EXCEED ONE HUNDRED U.S. DOLLARS (US $100.00) OR THE AMOUNT PAID BY YOU TO LICENSOR FOR THE SOFTWARE IN THE TWELVE (12) MONTHS PRECEDING THE CLAIM, WHICHEVER IS GREATER.
103
+
104
+ THE LIMITATIONS IN THIS SECTION APPLY NOTWITHSTANDING THE FAILURE OF ESSENTIAL PURPOSE OF ANY LIMITED REMEDY. SOME JURISDICTIONS DO NOT ALLOW CERTAIN LIMITATIONS OF LIABILITY, IN WHICH CASE THE FOREGOING LIMITATIONS APPLY TO THE MAXIMUM EXTENT PERMITTED BY LAW.
105
+
106
+ ## 13. Indemnification
107
+
108
+ You agree to indemnify, defend, and hold harmless Licensor, its affiliates, and its officers, directors, employees, and agents from and against any and all claims, damages, liabilities, costs, and expenses (including reasonable attorneys' fees) arising out of or related to (a) Your use of the Software in breach of this License, (b) Your violation of any applicable law or third-party right, or (c) content or data You process through the Software.
109
+
110
+ ## 14. Governing Law and Venue
111
+
112
+ This License is governed by the laws of the State of Georgia, United States of America, without regard to its conflict-of-laws principles. The exclusive venue for any dispute arising under or related to this License shall be the state or federal courts located in Fulton County, Georgia, and the parties consent to the personal jurisdiction of such courts. The United Nations Convention on Contracts for the International Sale of Goods does not apply.
113
+
114
+ ## 15. Export Compliance
115
+
116
+ You represent and warrant that (a) You are not located in a country subject to a U.S. Government embargo or designated as a "terrorist supporting" country, and (b) You are not listed on any U.S. Government list of prohibited or restricted parties. You shall comply with all applicable export-control laws and regulations in Your use of the Software.
117
+
118
+ ## 16. Assignment
119
+
120
+ You may not assign or transfer this License, in whole or in part, without Licensor's prior written consent. Any attempted assignment in violation of this Section is void. Licensor may assign this License freely.
121
+
122
+ ## 17. Severability and Waiver
123
+
124
+ If any provision of this License is held unenforceable, the remaining provisions shall remain in full force and effect, and the unenforceable provision shall be reformed to the extent necessary to render it enforceable while preserving the parties' intent. No waiver of any term shall be deemed a waiver of any other term or of a subsequent breach.
125
+
126
+ ## 18. Entire Agreement
127
+
128
+ This License, together with the Platform Terms to the extent applicable to Your use of the Platform, constitutes the entire agreement between You and Licensor regarding the Software and supersedes all prior or contemporaneous understandings, whether written or oral. Any modification must be in writing and signed by an authorized representative of Licensor.
129
+
130
+ ## 19. Contact
131
+
132
+ **Promptowl LLC**
133
+ 3060 Mercer University Dr Ste 110
134
+ Atlanta, GA 30341, USA
135
+ Email: **hoot@promptowl.ai**
136
+ Web: <https://promptowl.ai>
137
+
138
+ For commercial licensing inquiries (redistribution, hosted-service offerings, OEM, or regulated-industry use), contact **hoot@promptowl.ai** with subject line "ContextNest Commercial License."
139
+
140
+ ---
141
+
142
+ *"ContextNest" and "PromptOwl" are trademarks of Promptowl LLC. All rights reserved.*
package/README.md ADDED
@@ -0,0 +1,105 @@
1
+ # ContextNest Community Edition
2
+
3
+ **Self-hosted context governance server for AI workflows.** Part of the [PromptOwl](https://promptowl.ai) platform.
4
+
5
+ > ⚠️ **Commercial Software.** ContextNest Community Edition is proprietary software licensed by Promptowl LLC. A free [PromptOwl account](https://app.promptowl.ai) is required to use it. Redistribution, hosting-as-a-service, and competitive use are prohibited. See [LICENSE.md](./LICENSE.md) for full terms.
6
+
7
+ ---
8
+
9
+ ## What it is
10
+
11
+ ContextNest Community Edition is a self-hosted server that lets you:
12
+
13
+ - Store, version, and govern markdown-based context documents ("nests")
14
+ - Apply stewardship workflows — draft, pending review, approved
15
+ - Serve approved context to AI agents via MCP, HTTP, or CLI
16
+ - Sync with the PromptOwl hosted platform for multi-user collaboration
17
+
18
+ The server runs locally or on your own infrastructure. Your PromptOwl account handles authentication, entitlement, and governance metadata.
19
+
20
+ ## Quickstart
21
+
22
+ ```bash
23
+ # 1. Get a license key — sign up free at https://app.promptowl.ai
24
+ # Settings → License Keys → Create a Community Server key
25
+
26
+ # 2. (Optional) Scaffold a local nest with the open-source CLI
27
+ npx @promptowl/contextnest-cli init
28
+
29
+ # 3. Run the community server
30
+ PROMPTOWL_KEY=pk_... npx @promptowl/contextnest-community
31
+ ```
32
+
33
+ The server listens on `http://localhost:3000` by default. Without a valid `PROMPTOWL_KEY` the server still runs but some features are limited. See [CONFIGURATION.md](./CONFIGURATION.md) for all environment variables (port, auth mode, storage, telemetry).
34
+
35
+ ## System requirements
36
+
37
+ - **Node.js** 20.x or later
38
+ - **PromptOwl account** — free signup at <https://app.promptowl.ai/signup>
39
+ - **OS:** Windows, macOS, or Linux
40
+ - **Disk:** ~200 MB for the server, plus storage for your nests
41
+
42
+ ## What you get
43
+
44
+ | Feature | Community Edition | Enterprise |
45
+ |---|:---:|:---:|
46
+ | Self-hosted context server | ✅ | ✅ |
47
+ | Markdown + YAML frontmatter vaults | ✅ | ✅ |
48
+ | Stewardship workflow (draft/review/approve) | ✅ | ✅ |
49
+ | MCP server for AI agents | ✅ | ✅ |
50
+ | Multi-user governance UI | — | ✅ |
51
+ | SSO / SAML / SCIM | — | ✅ |
52
+ | Audit log streaming | — | ✅ |
53
+ | Policy transforms (redaction, summarization) | — | ✅ |
54
+ | Priority support and SLA | — | ✅ |
55
+
56
+ For Enterprise pricing and features, contact **hoot@promptowl.ai** or visit <https://promptowl.ai/contextnest/>.
57
+
58
+ ## Licensing
59
+
60
+ ContextNest Community Edition is **commercial software**. It is **not open source**.
61
+
62
+ **You may:**
63
+ - Install and run the Software on devices You own or control
64
+ - Use the Software for internal business purposes, tied to a valid PromptOwl account
65
+ - Make backup and archival copies
66
+
67
+ **You may not:**
68
+ - Redistribute, resell, rent, lease, or sublicense the Software
69
+ - Offer the Software as a hosted, managed, or software-as-a-service product to third parties
70
+ - Reverse engineer, decompile, or create derivative works
71
+ - Use the Software to build a competing product or service
72
+ - Remove copyright, trademark, or license notices
73
+
74
+ Full license text: [LICENSE.md](./LICENSE.md)
75
+
76
+ **For redistribution, hosted-service, OEM, or regulated-industry use,** contact **hoot@promptowl.ai** for a commercial license agreement.
77
+
78
+ ## Platform terms
79
+
80
+ Because the Software requires a PromptOwl account, the following terms also apply to Your use:
81
+
82
+ - **End User License Agreement** — <https://promptowl.ai/eula/>
83
+ - **Terms of Service** — <https://promptowl.ai/terms-of-service/>
84
+ - **Privacy Policy** — <https://promptowl.ai/privacy-policy/>
85
+ - **Acceptable Use Policy** — <https://promptowl.ai/acceptable-use/>
86
+ - **Disclaimer** — <https://promptowl.ai/disclaimer/>
87
+ - **Cookie Policy** — <https://promptowl.ai/cookies/>
88
+
89
+ ## Support
90
+
91
+ - **Documentation:** <https://promptowl.ai/contextnest/>
92
+ - **Product questions:** <https://promptowl.ai/contact-us/>
93
+ - **Support & bugs:** `hoot@promptowl.ai`
94
+ - **Commercial licensing:** `hoot@promptowl.ai` (subject: *ContextNest Commercial License*)
95
+
96
+ ## AI output disclaimer
97
+
98
+ The Software injects content into large language models. AI output may be inaccurate, incomplete, or inappropriate for your use case. You are responsible for reviewing and validating any AI-generated content before relying on it, particularly in business-critical or regulated contexts. Do not deploy the Software in medical, legal, financial-advisory, or safety-critical environments without appropriate human oversight.
99
+
100
+ ---
101
+
102
+ **Copyright © 2026 Promptowl LLC.** All rights reserved.
103
+ "ContextNest" and "PromptOwl" are trademarks of Promptowl LLC.
104
+
105
+ Promptowl LLC · 3060 Mercer University Dr Ste 110 · Atlanta, GA 30341 · USA
@@ -0,0 +1,58 @@
1
+ // src/auth/keys.ts
2
+ import { randomBytes, createHash, timingSafeEqual } from "crypto";
3
+ import bcrypt from "bcryptjs";
4
+ var KEY_PREFIX = "cnst_";
5
+ var BCRYPT_ROUNDS = 12;
6
+ function generateApiKey() {
7
+ return KEY_PREFIX + randomBytes(32).toString("hex");
8
+ }
9
+ function hashApiKey(key) {
10
+ return createHash("sha256").update(key).digest("hex");
11
+ }
12
+ function getKeyPrefix(key) {
13
+ return key.slice(0, 12) + "...";
14
+ }
15
+ function parseBearerToken(header) {
16
+ if (!header) return null;
17
+ const BEARER = "Bearer ";
18
+ if (!header.startsWith(BEARER)) return null;
19
+ const token = header.slice(BEARER.length);
20
+ if (!token.startsWith(KEY_PREFIX)) return null;
21
+ if (token.length < KEY_PREFIX.length + 32) return null;
22
+ return token;
23
+ }
24
+ async function hashPassword(password) {
25
+ return bcrypt.hash(password, BCRYPT_ROUNDS);
26
+ }
27
+ async function verifyPassword(password, stored) {
28
+ if (isBcrypt(stored)) {
29
+ return { ok: await bcrypt.compare(password, stored), needsRehash: false };
30
+ }
31
+ const [salt, hash] = stored.split(":");
32
+ if (!salt || !hash) return { ok: false, needsRehash: false };
33
+ const attempt = createHash("sha256").update(salt + password).digest("hex");
34
+ return {
35
+ ok: timingSafeEqualHex(hash, attempt),
36
+ needsRehash: true
37
+ };
38
+ }
39
+ function isBcrypt(stored) {
40
+ return stored.startsWith("$2a$") || stored.startsWith("$2b$") || stored.startsWith("$2y$");
41
+ }
42
+ function timingSafeEqualHex(a, b) {
43
+ if (a.length !== b.length) return false;
44
+ try {
45
+ return timingSafeEqual(Buffer.from(a, "hex"), Buffer.from(b, "hex"));
46
+ } catch {
47
+ return false;
48
+ }
49
+ }
50
+
51
+ export {
52
+ generateApiKey,
53
+ hashApiKey,
54
+ getKeyPrefix,
55
+ parseBearerToken,
56
+ hashPassword,
57
+ verifyPassword
58
+ };
@@ -0,0 +1,199 @@
1
+ import {
2
+ canUserApprove,
3
+ resolveStewardsForNode
4
+ } from "./chunk-Q2DCOS7V.js";
5
+ import {
6
+ getDb
7
+ } from "./chunk-USIDOGVJ.js";
8
+
9
+ // src/governance/review-service.ts
10
+ import { v4 as uuid } from "uuid";
11
+ function submitForReview(params) {
12
+ const db = getDb();
13
+ const existing = db.prepare(
14
+ "SELECT id FROM review_requests WHERE nest_id = ? AND node_id = ? AND status = 'pending'"
15
+ ).get(params.nestId, params.nodeId);
16
+ if (existing) {
17
+ throw new Error("A review is already pending for this node");
18
+ }
19
+ const id = uuid();
20
+ db.prepare(
21
+ `INSERT INTO review_requests
22
+ (id, nest_id, node_id, version, requested_by, request_note, priority)
23
+ VALUES (?, ?, ?, ?, ?, ?, ?)`
24
+ ).run(
25
+ id,
26
+ params.nestId,
27
+ params.nodeId,
28
+ params.version,
29
+ params.requestedBy,
30
+ params.note || null,
31
+ params.priority || "normal"
32
+ );
33
+ db.prepare(
34
+ "UPDATE node_versions SET status = 'pending_review' WHERE nest_id = ? AND node_id = ? AND version = ?"
35
+ ).run(params.nestId, params.nodeId, params.version);
36
+ return getReviewRequest(id);
37
+ }
38
+ function approve(params) {
39
+ const db = getDb();
40
+ const pending = db.prepare(
41
+ "SELECT * FROM review_requests WHERE nest_id = ? AND node_id = ? AND status = 'pending' ORDER BY requested_at DESC LIMIT 1"
42
+ ).get(params.nestId, params.nodeId);
43
+ if (!pending) {
44
+ throw new Error("No pending review for this node");
45
+ }
46
+ if (!params.override) {
47
+ const check = canUserApprove(
48
+ params.nestId,
49
+ params.nodeId,
50
+ params.approvedBy
51
+ );
52
+ if (!check.allowed) {
53
+ throw new Error(check.reason);
54
+ }
55
+ }
56
+ db.prepare(
57
+ `UPDATE review_requests
58
+ SET status = 'approved', resolved_by = ?, resolved_at = datetime('now'),
59
+ resolution_note = ?, is_override = ?
60
+ WHERE id = ?`
61
+ ).run(
62
+ params.approvedBy,
63
+ params.note || null,
64
+ params.override ? 1 : 0,
65
+ pending.id
66
+ );
67
+ db.prepare(
68
+ "UPDATE node_versions SET status = 'approved' WHERE nest_id = ? AND node_id = ? AND version = ?"
69
+ ).run(params.nestId, params.nodeId, params.version);
70
+ db.prepare(
71
+ `INSERT OR REPLACE INTO approved_versions (nest_id, node_id, approved_version, approved_by)
72
+ VALUES (?, ?, ?, ?)`
73
+ ).run(params.nestId, params.nodeId, params.version, params.approvedBy);
74
+ return getReviewRequest(pending.id);
75
+ }
76
+ function reject(params) {
77
+ const db = getDb();
78
+ const pending = db.prepare(
79
+ "SELECT * FROM review_requests WHERE nest_id = ? AND node_id = ? AND status = 'pending' ORDER BY requested_at DESC LIMIT 1"
80
+ ).get(params.nestId, params.nodeId);
81
+ if (!pending) {
82
+ throw new Error("No pending review for this node");
83
+ }
84
+ const check = canUserApprove(
85
+ params.nestId,
86
+ params.nodeId,
87
+ params.rejectedBy
88
+ );
89
+ if (!check.allowed) {
90
+ throw new Error(check.reason);
91
+ }
92
+ db.prepare(
93
+ `UPDATE review_requests
94
+ SET status = 'rejected', resolved_by = ?, resolved_at = datetime('now'), resolution_note = ?
95
+ WHERE id = ?`
96
+ ).run(params.rejectedBy, params.note, pending.id);
97
+ db.prepare(
98
+ "UPDATE node_versions SET status = 'rejected' WHERE nest_id = ? AND node_id = ? AND version = ?"
99
+ ).run(params.nestId, params.nodeId, params.version);
100
+ return getReviewRequest(pending.id);
101
+ }
102
+ function cancelReview(params) {
103
+ const db = getDb();
104
+ const pending = db.prepare(
105
+ "SELECT * FROM review_requests WHERE nest_id = ? AND node_id = ? AND status = 'pending' ORDER BY requested_at DESC LIMIT 1"
106
+ ).get(params.nestId, params.nodeId);
107
+ if (!pending) return null;
108
+ db.prepare(
109
+ `UPDATE review_requests
110
+ SET status = 'cancelled', resolved_by = ?, resolved_at = datetime('now')
111
+ WHERE id = ?`
112
+ ).run(params.cancelledBy, pending.id);
113
+ db.prepare(
114
+ "UPDATE node_versions SET status = 'draft' WHERE nest_id = ? AND node_id = ? AND version = ?"
115
+ ).run(params.nestId, params.nodeId, pending.version);
116
+ return getReviewRequest(pending.id);
117
+ }
118
+ function getReviewQueue(params) {
119
+ const db = getDb();
120
+ let whereClauses = [];
121
+ const args = [];
122
+ if (params.nestId) {
123
+ whereClauses.push("nest_id = ?");
124
+ args.push(params.nestId);
125
+ }
126
+ if (params.status) {
127
+ const statuses = Array.isArray(params.status) ? params.status : [params.status];
128
+ whereClauses.push(
129
+ `status IN (${statuses.map(() => "?").join(",")})`
130
+ );
131
+ args.push(...statuses);
132
+ }
133
+ const where = whereClauses.length > 0 ? `WHERE ${whereClauses.join(" AND ")}` : "";
134
+ const total = db.prepare(`SELECT COUNT(*) as c FROM review_requests ${where}`).get(...args).c;
135
+ const limit = params.limit || 50;
136
+ const offset = params.offset || 0;
137
+ const rows = db.prepare(
138
+ `SELECT * FROM review_requests ${where}
139
+ ORDER BY
140
+ CASE priority WHEN 'urgent' THEN 0 WHEN 'high' THEN 1 WHEN 'normal' THEN 2 ELSE 3 END,
141
+ requested_at DESC
142
+ LIMIT ? OFFSET ?`
143
+ ).all(...args, limit, offset);
144
+ let requests = rows.map(rowToReviewRequest);
145
+ if (params.stewardEmail) {
146
+ requests = requests.filter((r) => {
147
+ const stewards = resolveStewardsForNode(r.nestId, r.nodeId);
148
+ return stewards.some(
149
+ (s) => s.steward.userEmail === params.stewardEmail
150
+ );
151
+ });
152
+ }
153
+ return { requests, total };
154
+ }
155
+ function getReviewHistory(nestId, nodeId) {
156
+ const db = getDb();
157
+ const rows = db.prepare(
158
+ "SELECT * FROM review_requests WHERE nest_id = ? AND node_id = ? ORDER BY requested_at DESC"
159
+ ).all(nestId, nodeId);
160
+ return rows.map(rowToReviewRequest);
161
+ }
162
+ function getPendingReview(nestId, nodeId) {
163
+ const db = getDb();
164
+ const row = db.prepare(
165
+ "SELECT * FROM review_requests WHERE nest_id = ? AND node_id = ? AND status = 'pending' ORDER BY requested_at DESC LIMIT 1"
166
+ ).get(nestId, nodeId);
167
+ return row ? rowToReviewRequest(row) : null;
168
+ }
169
+ function getReviewRequest(id) {
170
+ const db = getDb();
171
+ const row = db.prepare("SELECT * FROM review_requests WHERE id = ?").get(id);
172
+ return row ? rowToReviewRequest(row) : null;
173
+ }
174
+ function rowToReviewRequest(row) {
175
+ return {
176
+ id: row.id,
177
+ nestId: row.nest_id,
178
+ nodeId: row.node_id,
179
+ version: row.version,
180
+ requestedBy: row.requested_by,
181
+ requestedAt: row.requested_at,
182
+ requestNote: row.request_note || void 0,
183
+ status: row.status,
184
+ resolvedBy: row.resolved_by || void 0,
185
+ resolvedAt: row.resolved_at || void 0,
186
+ resolutionNote: row.resolution_note || void 0,
187
+ priority: row.priority
188
+ };
189
+ }
190
+
191
+ export {
192
+ submitForReview,
193
+ approve,
194
+ reject,
195
+ cancelReview,
196
+ getReviewQueue,
197
+ getReviewHistory,
198
+ getPendingReview
199
+ };