@agentic-kit/db-tools 0.12.42 → 0.12.44
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/README.md +1 -1
- package/context.d.ts +6 -7
- package/context.js +26 -23
- package/db-probe.js +5 -2
- package/esm/context.d.ts +6 -7
- package/esm/context.js +27 -24
- package/esm/db-probe.js +6 -3
- package/esm/host.d.ts +7 -0
- package/esm/host.js +7 -0
- package/esm/index.d.ts +1 -1
- package/esm/index.js +1 -1
- package/esm/provision-database/credential.js +2 -3
- package/esm/provision-database/env-file.d.ts +4 -7
- package/esm/provision-database/env-file.js +3 -7
- package/esm/provision-database/provision.js +13 -18
- package/esm/tools/provision-database.js +1 -1
- package/esm/tools/run-codegen.js +7 -7
- package/host.d.ts +7 -0
- package/host.js +8 -0
- package/index.d.ts +1 -1
- package/index.js +2 -1
- package/package.json +4 -4
- package/provision-database/credential.js +2 -3
- package/provision-database/env-file.d.ts +4 -7
- package/provision-database/env-file.js +3 -7
- package/provision-database/provision.js +12 -17
- package/tools/provision-database.js +1 -1
- package/tools/run-codegen.js +6 -6
package/README.md
CHANGED
|
@@ -77,7 +77,7 @@ Tenant APIs answer `ACCESS_SUSPENDED` (a 403 with a JSON body) for a deleted or
|
|
|
77
77
|
|
|
78
78
|
## Provisioning
|
|
79
79
|
|
|
80
|
-
`provision_database` and the host-side `provisionDatabase(input, { cwd })` never create a second database by accident. Calls for one project run one at a time. A binding is `DATABASE_ID` alone: it is probed, then kept (healing
|
|
80
|
+
`provision_database` and the host-side `provisionDatabase(input, { cwd })` never create a second database by accident. Calls for one project run one at a time. A binding is `DATABASE_ID` alone: it is probed, then kept (healing endpoints and any missing binding keys) or refused. The account bearer stays with the host, not in `.env`. Replace mode (`mode: 'replace', replaces: <the id the caller saw>`) swaps the binding only if it is still that one, and never swaps a suspended database, or a live one on the host path. A provision that outlasts the poll is recorded in `.env` and resumed by the next call. A ticket that can't be read back, or a call that lost track of its ticket, finds an in-flight provision of the same name with `listOwn` before anything is requested; a completed one is only reported. After `MAX_PENDING_ATTEMPTS` waits the call fails `provision-stuck` until the host passes `abandonPending`.
|
|
81
81
|
|
|
82
82
|
Failures carry a `code`: `signed-out`, `backend-unreachable`, `db-missing`, `db-suspended`, `account-mismatch`, `binding-live`, `binding-changed`, `pending-other`, `ticket-lost`, `provision-stuck`, `name-taken` (with `heldBy` when a database of this account holds the name, which the host can bind with `adopt: <id>`), `binding-not-saved`, `env-write-failed`, `reprovision-not-allowed` or `failed`.
|
|
83
83
|
|
package/context.d.ts
CHANGED
|
@@ -11,11 +11,11 @@ export type ProjectContext = {
|
|
|
11
11
|
apiEndpoint: string;
|
|
12
12
|
modulesEndpoint: string;
|
|
13
13
|
databaseName: string;
|
|
14
|
-
accessToken
|
|
14
|
+
accessToken?: string;
|
|
15
15
|
dataEndpoint: string;
|
|
16
16
|
hash?: string;
|
|
17
17
|
};
|
|
18
|
-
export type ProjectContextFailureCode = 'no-env' | 'missing-credentials' | 'db-missing' | 'db-suspended' | 'account-mismatch' | 'backend-unreachable' | 'schema-unresolved';
|
|
18
|
+
export type ProjectContextFailureCode = 'no-env' | 'no-backend' | 'missing-credentials' | 'db-missing' | 'db-suspended' | 'account-mismatch' | 'backend-unreachable' | 'schema-unresolved';
|
|
19
19
|
export type ResolveResult = {
|
|
20
20
|
context: ProjectContext | null;
|
|
21
21
|
reason: string;
|
|
@@ -34,9 +34,8 @@ export declare const CONTEXT_ENV_PREFIX = "CONSTRUCTIVE_";
|
|
|
34
34
|
export type ContextSource = Record<string, string | undefined> | ((name: string) => string | undefined);
|
|
35
35
|
/** Use the process environment (or any injected record) as the source. */
|
|
36
36
|
export declare function fromEnvironment(environment?: Record<string, string | undefined>): ContextSource;
|
|
37
|
-
/** Read a project `.env`
|
|
38
|
-
*
|
|
39
|
-
* the ambient environment. Returns null when the file is absent. */
|
|
37
|
+
/** Read a project `.env` without merging ambient environment variables.
|
|
38
|
+
* Returns null when the file is absent. */
|
|
40
39
|
export declare function fromEnvFile(cwd: string): Promise<ContextSource | null>;
|
|
41
40
|
export type ResolveOptions = {
|
|
42
41
|
/** Which plane the context is for. Ownership gates the control plane only:
|
|
@@ -50,8 +49,8 @@ export type ResolveOptions = {
|
|
|
50
49
|
/**
|
|
51
50
|
* Resolve the project context from injected values, or from a project folder.
|
|
52
51
|
*
|
|
53
|
-
* Passing a `cwd` string
|
|
54
|
-
*
|
|
52
|
+
* Passing a `cwd` string reads the binding in `<cwd>/.env` and authenticates
|
|
53
|
+
* through the host.
|
|
55
54
|
* Headless hosts pass values instead: `fromEnvironment()` for a container or
|
|
56
55
|
* Job, an explicit record for anything else.
|
|
57
56
|
*/
|
package/context.js
CHANGED
|
@@ -16,13 +16,9 @@ const sdk_1 = require("@constructive-io/sdk");
|
|
|
16
16
|
const dotenv_1 = require("12factor-env/dotenv");
|
|
17
17
|
const db_probe_1 = require("./db-probe");
|
|
18
18
|
const host_1 = require("./host");
|
|
19
|
-
const DEFAULT_API_ENDPOINT = 'http://api.localhost:3000/graphql';
|
|
20
|
-
const DEFAULT_MODULES_ENDPOINT = 'http://modules.localhost:3000/graphql';
|
|
21
19
|
// The project values pi needs, and where they come from. A scaffolded project
|
|
22
|
-
// folder carries
|
|
23
|
-
// Job, CI — injects
|
|
24
|
-
// ever written into a git clone the agent could commit. Both lanes produce the
|
|
25
|
-
// same record, which is why the resolver takes VALUES, not a directory.
|
|
20
|
+
// folder carries binding metadata in `.env`; a headless host — a container,
|
|
21
|
+
// a Job, CI — injects values without writing credentials into a git clone.
|
|
26
22
|
exports.CONTEXT_ENV_KEYS = [
|
|
27
23
|
'ACCESS_TOKEN',
|
|
28
24
|
'DATABASE_ID',
|
|
@@ -46,9 +42,8 @@ const readContextValue = (source, key) => {
|
|
|
46
42
|
function fromEnvironment(environment = process.env) {
|
|
47
43
|
return environment;
|
|
48
44
|
}
|
|
49
|
-
/** Read a project `.env`
|
|
50
|
-
*
|
|
51
|
-
* the ambient environment. Returns null when the file is absent. */
|
|
45
|
+
/** Read a project `.env` without merging ambient environment variables.
|
|
46
|
+
* Returns null when the file is absent. */
|
|
52
47
|
async function fromEnvFile(cwd) {
|
|
53
48
|
try {
|
|
54
49
|
return (0, dotenv_1.parseDotenv)(await (0, promises_1.readFile)(node_path_1.default.join(cwd, '.env'), 'utf8'));
|
|
@@ -60,13 +55,14 @@ async function fromEnvFile(cwd) {
|
|
|
60
55
|
/**
|
|
61
56
|
* Resolve the project context from injected values, or from a project folder.
|
|
62
57
|
*
|
|
63
|
-
* Passing a `cwd` string
|
|
64
|
-
*
|
|
58
|
+
* Passing a `cwd` string reads the binding in `<cwd>/.env` and authenticates
|
|
59
|
+
* through the host.
|
|
65
60
|
* Headless hosts pass values instead: `fromEnvironment()` for a container or
|
|
66
61
|
* Job, an explicit record for anything else.
|
|
67
62
|
*/
|
|
68
63
|
async function resolveProjectContext(input, options = {}) {
|
|
69
|
-
const
|
|
64
|
+
const fileBacked = typeof input === 'string';
|
|
65
|
+
const source = fileBacked ? await fromEnvFile(input) : input;
|
|
70
66
|
if (!source) {
|
|
71
67
|
return {
|
|
72
68
|
context: null,
|
|
@@ -74,13 +70,15 @@ async function resolveProjectContext(input, options = {}) {
|
|
|
74
70
|
code: 'no-env',
|
|
75
71
|
};
|
|
76
72
|
}
|
|
77
|
-
const env = Object.fromEntries(exports.CONTEXT_ENV_KEYS.map((key) => [key, readContextValue(source, key)]));
|
|
73
|
+
const env = Object.fromEntries(exports.CONTEXT_ENV_KEYS.map((key) => [key, fileBacked && key === 'ACCESS_TOKEN' ? undefined : readContextValue(source, key)]));
|
|
78
74
|
const accessToken = env.ACCESS_TOKEN;
|
|
79
75
|
const databaseId = env.DATABASE_ID;
|
|
80
|
-
if (!
|
|
76
|
+
if (!databaseId || (!fileBacked && !accessToken)) {
|
|
81
77
|
return {
|
|
82
78
|
context: null,
|
|
83
|
-
reason:
|
|
79
|
+
reason: fileBacked
|
|
80
|
+
? 'Project is not connected to a Constructive database yet (missing DATABASE_ID in .env). Provision the database first, then retry.'
|
|
81
|
+
: `Missing ${exports.CONTEXT_ENV_PREFIX}ACCESS_TOKEN or ${exports.CONTEXT_ENV_PREFIX}DATABASE_ID from the injected project context.`,
|
|
84
82
|
code: 'missing-credentials',
|
|
85
83
|
};
|
|
86
84
|
}
|
|
@@ -89,16 +87,14 @@ async function resolveProjectContext(input, options = {}) {
|
|
|
89
87
|
// store (environment-aware) so app + harness share one endpoint source.
|
|
90
88
|
const host = (0, host_1.getHost)();
|
|
91
89
|
const backend = host.backendConfig();
|
|
92
|
-
const
|
|
93
|
-
const
|
|
90
|
+
const controlApiEndpoint = backend?.apiEndpoint;
|
|
91
|
+
const controlModulesEndpoint = backend?.modulesEndpoint;
|
|
94
92
|
// The whole control plane (binding probe, schema resolution, blueprint/schema
|
|
95
93
|
// tools) authenticates with the ACCOUNT bearer: the platform api rejects
|
|
96
94
|
// per-database keys, so the project ACCESS_TOKEN can never act on metaschema
|
|
97
|
-
// surfaces.
|
|
95
|
+
// surfaces. An explicitly injected key stays in the context for the data plane only.
|
|
98
96
|
// The bearer is only ever sent to the app-configured backend — never a
|
|
99
97
|
// source-pinned endpoint, which an untrusted cloned project controls.
|
|
100
|
-
const controlApiEndpoint = backend?.apiEndpoint || DEFAULT_API_ENDPOINT;
|
|
101
|
-
const controlModulesEndpoint = backend?.modulesEndpoint || DEFAULT_MODULES_ENDPOINT;
|
|
102
98
|
const account = host.account();
|
|
103
99
|
const accountBearer = account?.apiKey ?? account?.accessToken;
|
|
104
100
|
if (!accountBearer) {
|
|
@@ -108,6 +104,11 @@ async function resolveProjectContext(input, options = {}) {
|
|
|
108
104
|
code: 'missing-credentials',
|
|
109
105
|
};
|
|
110
106
|
}
|
|
107
|
+
if (!controlApiEndpoint || !controlModulesEndpoint) {
|
|
108
|
+
return { context: null, reason: (0, host_1.noBackendReason)(host.signInHint), code: 'no-backend' };
|
|
109
|
+
}
|
|
110
|
+
const apiEndpoint = env.API_ENDPOINT || controlApiEndpoint;
|
|
111
|
+
const modulesEndpoint = env.MODULES_ENDPOINT || controlModulesEndpoint;
|
|
111
112
|
const controlHeaders = { Authorization: `Bearer ${accountBearer}` };
|
|
112
113
|
const apiClient = sdk_1.api.createClient({ endpoint: controlApiEndpoint, headers: controlHeaders });
|
|
113
114
|
// Always probe the bound database — the DATABASE_ID stamp proves it WAS
|
|
@@ -181,7 +182,7 @@ async function resolveProjectContext(input, options = {}) {
|
|
|
181
182
|
apiEndpoint,
|
|
182
183
|
modulesEndpoint,
|
|
183
184
|
databaseName,
|
|
184
|
-
accessToken,
|
|
185
|
+
...(accessToken ? { accessToken } : {}),
|
|
185
186
|
dataEndpoint,
|
|
186
187
|
hash: probe.hash,
|
|
187
188
|
},
|
|
@@ -261,8 +262,10 @@ async function resolveOrgName(ownerId) {
|
|
|
261
262
|
const bearer = account?.apiKey ?? account?.accessToken;
|
|
262
263
|
if (!bearer)
|
|
263
264
|
return undefined;
|
|
264
|
-
const
|
|
265
|
-
|
|
265
|
+
const apiEndpoint = host.backendConfig()?.apiEndpoint;
|
|
266
|
+
if (!apiEndpoint)
|
|
267
|
+
return undefined;
|
|
268
|
+
const authEndpoint = deriveSubdomainEndpoint(apiEndpoint, 'auth');
|
|
266
269
|
if (!authEndpoint)
|
|
267
270
|
return undefined;
|
|
268
271
|
const client = sdk_1.auth.createClient({
|
package/db-probe.js
CHANGED
|
@@ -4,7 +4,6 @@ exports.probeDatabase = probeDatabase;
|
|
|
4
4
|
exports.probeBoundDatabase = probeBoundDatabase;
|
|
5
5
|
const graphql_http_1 = require("./graphql-http");
|
|
6
6
|
const host_1 = require("./host");
|
|
7
|
-
const DEFAULT_API_ENDPOINT = 'http://api.localhost:3000/graphql';
|
|
8
7
|
const FIELDS = 'id name ownerId hash suspendedAt suspendedReason';
|
|
9
8
|
// A backend from before `hash` and the suspension columns.
|
|
10
9
|
const BASIC_FIELDS = 'id name ownerId';
|
|
@@ -101,8 +100,12 @@ async function probeBoundDatabase(databaseId) {
|
|
|
101
100
|
detail: `No usable account credential. ${host.signInHint ?? 'Sign in to the app, then retry.'}`,
|
|
102
101
|
};
|
|
103
102
|
}
|
|
103
|
+
const endpoint = host.backendConfig()?.apiEndpoint;
|
|
104
|
+
if (!endpoint) {
|
|
105
|
+
return { status: 'unreachable', databaseId, cause: 'error', detail: (0, host_1.noBackendReason)(host.signInHint) };
|
|
106
|
+
}
|
|
104
107
|
return probeDatabase({
|
|
105
|
-
endpoint
|
|
108
|
+
endpoint,
|
|
106
109
|
bearer,
|
|
107
110
|
databaseId,
|
|
108
111
|
signInHint: host.signInHint,
|
package/esm/context.d.ts
CHANGED
|
@@ -11,11 +11,11 @@ export type ProjectContext = {
|
|
|
11
11
|
apiEndpoint: string;
|
|
12
12
|
modulesEndpoint: string;
|
|
13
13
|
databaseName: string;
|
|
14
|
-
accessToken
|
|
14
|
+
accessToken?: string;
|
|
15
15
|
dataEndpoint: string;
|
|
16
16
|
hash?: string;
|
|
17
17
|
};
|
|
18
|
-
export type ProjectContextFailureCode = 'no-env' | 'missing-credentials' | 'db-missing' | 'db-suspended' | 'account-mismatch' | 'backend-unreachable' | 'schema-unresolved';
|
|
18
|
+
export type ProjectContextFailureCode = 'no-env' | 'no-backend' | 'missing-credentials' | 'db-missing' | 'db-suspended' | 'account-mismatch' | 'backend-unreachable' | 'schema-unresolved';
|
|
19
19
|
export type ResolveResult = {
|
|
20
20
|
context: ProjectContext | null;
|
|
21
21
|
reason: string;
|
|
@@ -34,9 +34,8 @@ export declare const CONTEXT_ENV_PREFIX = "CONSTRUCTIVE_";
|
|
|
34
34
|
export type ContextSource = Record<string, string | undefined> | ((name: string) => string | undefined);
|
|
35
35
|
/** Use the process environment (or any injected record) as the source. */
|
|
36
36
|
export declare function fromEnvironment(environment?: Record<string, string | undefined>): ContextSource;
|
|
37
|
-
/** Read a project `.env`
|
|
38
|
-
*
|
|
39
|
-
* the ambient environment. Returns null when the file is absent. */
|
|
37
|
+
/** Read a project `.env` without merging ambient environment variables.
|
|
38
|
+
* Returns null when the file is absent. */
|
|
40
39
|
export declare function fromEnvFile(cwd: string): Promise<ContextSource | null>;
|
|
41
40
|
export type ResolveOptions = {
|
|
42
41
|
/** Which plane the context is for. Ownership gates the control plane only:
|
|
@@ -50,8 +49,8 @@ export type ResolveOptions = {
|
|
|
50
49
|
/**
|
|
51
50
|
* Resolve the project context from injected values, or from a project folder.
|
|
52
51
|
*
|
|
53
|
-
* Passing a `cwd` string
|
|
54
|
-
*
|
|
52
|
+
* Passing a `cwd` string reads the binding in `<cwd>/.env` and authenticates
|
|
53
|
+
* through the host.
|
|
55
54
|
* Headless hosts pass values instead: `fromEnvironment()` for a container or
|
|
56
55
|
* Job, an explicit record for anything else.
|
|
57
56
|
*/
|
package/esm/context.js
CHANGED
|
@@ -3,14 +3,10 @@ import path from 'node:path';
|
|
|
3
3
|
import { api, auth, modules } from '@constructive-io/sdk';
|
|
4
4
|
import { parseDotenv } from '12factor-env/dotenv';
|
|
5
5
|
import { probeDatabase } from './db-probe';
|
|
6
|
-
import { DEFAULT_DATA_TOKEN_SKEW_MS, getHost, reprovisionAdvice } from './host';
|
|
7
|
-
const DEFAULT_API_ENDPOINT = 'http://api.localhost:3000/graphql';
|
|
8
|
-
const DEFAULT_MODULES_ENDPOINT = 'http://modules.localhost:3000/graphql';
|
|
6
|
+
import { DEFAULT_DATA_TOKEN_SKEW_MS, getHost, noBackendReason, reprovisionAdvice } from './host';
|
|
9
7
|
// The project values pi needs, and where they come from. A scaffolded project
|
|
10
|
-
// folder carries
|
|
11
|
-
// Job, CI — injects
|
|
12
|
-
// ever written into a git clone the agent could commit. Both lanes produce the
|
|
13
|
-
// same record, which is why the resolver takes VALUES, not a directory.
|
|
8
|
+
// folder carries binding metadata in `.env`; a headless host — a container,
|
|
9
|
+
// a Job, CI — injects values without writing credentials into a git clone.
|
|
14
10
|
export const CONTEXT_ENV_KEYS = [
|
|
15
11
|
'ACCESS_TOKEN',
|
|
16
12
|
'DATABASE_ID',
|
|
@@ -34,9 +30,8 @@ const readContextValue = (source, key) => {
|
|
|
34
30
|
export function fromEnvironment(environment = process.env) {
|
|
35
31
|
return environment;
|
|
36
32
|
}
|
|
37
|
-
/** Read a project `.env`
|
|
38
|
-
*
|
|
39
|
-
* the ambient environment. Returns null when the file is absent. */
|
|
33
|
+
/** Read a project `.env` without merging ambient environment variables.
|
|
34
|
+
* Returns null when the file is absent. */
|
|
40
35
|
export async function fromEnvFile(cwd) {
|
|
41
36
|
try {
|
|
42
37
|
return parseDotenv(await readFile(path.join(cwd, '.env'), 'utf8'));
|
|
@@ -48,13 +43,14 @@ export async function fromEnvFile(cwd) {
|
|
|
48
43
|
/**
|
|
49
44
|
* Resolve the project context from injected values, or from a project folder.
|
|
50
45
|
*
|
|
51
|
-
* Passing a `cwd` string
|
|
52
|
-
*
|
|
46
|
+
* Passing a `cwd` string reads the binding in `<cwd>/.env` and authenticates
|
|
47
|
+
* through the host.
|
|
53
48
|
* Headless hosts pass values instead: `fromEnvironment()` for a container or
|
|
54
49
|
* Job, an explicit record for anything else.
|
|
55
50
|
*/
|
|
56
51
|
export async function resolveProjectContext(input, options = {}) {
|
|
57
|
-
const
|
|
52
|
+
const fileBacked = typeof input === 'string';
|
|
53
|
+
const source = fileBacked ? await fromEnvFile(input) : input;
|
|
58
54
|
if (!source) {
|
|
59
55
|
return {
|
|
60
56
|
context: null,
|
|
@@ -62,13 +58,15 @@ export async function resolveProjectContext(input, options = {}) {
|
|
|
62
58
|
code: 'no-env',
|
|
63
59
|
};
|
|
64
60
|
}
|
|
65
|
-
const env = Object.fromEntries(CONTEXT_ENV_KEYS.map((key) => [key, readContextValue(source, key)]));
|
|
61
|
+
const env = Object.fromEntries(CONTEXT_ENV_KEYS.map((key) => [key, fileBacked && key === 'ACCESS_TOKEN' ? undefined : readContextValue(source, key)]));
|
|
66
62
|
const accessToken = env.ACCESS_TOKEN;
|
|
67
63
|
const databaseId = env.DATABASE_ID;
|
|
68
|
-
if (!
|
|
64
|
+
if (!databaseId || (!fileBacked && !accessToken)) {
|
|
69
65
|
return {
|
|
70
66
|
context: null,
|
|
71
|
-
reason:
|
|
67
|
+
reason: fileBacked
|
|
68
|
+
? 'Project is not connected to a Constructive database yet (missing DATABASE_ID in .env). Provision the database first, then retry.'
|
|
69
|
+
: `Missing ${CONTEXT_ENV_PREFIX}ACCESS_TOKEN or ${CONTEXT_ENV_PREFIX}DATABASE_ID from the injected project context.`,
|
|
72
70
|
code: 'missing-credentials',
|
|
73
71
|
};
|
|
74
72
|
}
|
|
@@ -77,16 +75,14 @@ export async function resolveProjectContext(input, options = {}) {
|
|
|
77
75
|
// store (environment-aware) so app + harness share one endpoint source.
|
|
78
76
|
const host = getHost();
|
|
79
77
|
const backend = host.backendConfig();
|
|
80
|
-
const
|
|
81
|
-
const
|
|
78
|
+
const controlApiEndpoint = backend?.apiEndpoint;
|
|
79
|
+
const controlModulesEndpoint = backend?.modulesEndpoint;
|
|
82
80
|
// The whole control plane (binding probe, schema resolution, blueprint/schema
|
|
83
81
|
// tools) authenticates with the ACCOUNT bearer: the platform api rejects
|
|
84
82
|
// per-database keys, so the project ACCESS_TOKEN can never act on metaschema
|
|
85
|
-
// surfaces.
|
|
83
|
+
// surfaces. An explicitly injected key stays in the context for the data plane only.
|
|
86
84
|
// The bearer is only ever sent to the app-configured backend — never a
|
|
87
85
|
// source-pinned endpoint, which an untrusted cloned project controls.
|
|
88
|
-
const controlApiEndpoint = backend?.apiEndpoint || DEFAULT_API_ENDPOINT;
|
|
89
|
-
const controlModulesEndpoint = backend?.modulesEndpoint || DEFAULT_MODULES_ENDPOINT;
|
|
90
86
|
const account = host.account();
|
|
91
87
|
const accountBearer = account?.apiKey ?? account?.accessToken;
|
|
92
88
|
if (!accountBearer) {
|
|
@@ -96,6 +92,11 @@ export async function resolveProjectContext(input, options = {}) {
|
|
|
96
92
|
code: 'missing-credentials',
|
|
97
93
|
};
|
|
98
94
|
}
|
|
95
|
+
if (!controlApiEndpoint || !controlModulesEndpoint) {
|
|
96
|
+
return { context: null, reason: noBackendReason(host.signInHint), code: 'no-backend' };
|
|
97
|
+
}
|
|
98
|
+
const apiEndpoint = env.API_ENDPOINT || controlApiEndpoint;
|
|
99
|
+
const modulesEndpoint = env.MODULES_ENDPOINT || controlModulesEndpoint;
|
|
99
100
|
const controlHeaders = { Authorization: `Bearer ${accountBearer}` };
|
|
100
101
|
const apiClient = api.createClient({ endpoint: controlApiEndpoint, headers: controlHeaders });
|
|
101
102
|
// Always probe the bound database — the DATABASE_ID stamp proves it WAS
|
|
@@ -169,7 +170,7 @@ export async function resolveProjectContext(input, options = {}) {
|
|
|
169
170
|
apiEndpoint,
|
|
170
171
|
modulesEndpoint,
|
|
171
172
|
databaseName,
|
|
172
|
-
accessToken,
|
|
173
|
+
...(accessToken ? { accessToken } : {}),
|
|
173
174
|
dataEndpoint,
|
|
174
175
|
hash: probe.hash,
|
|
175
176
|
},
|
|
@@ -249,8 +250,10 @@ export async function resolveOrgName(ownerId) {
|
|
|
249
250
|
const bearer = account?.apiKey ?? account?.accessToken;
|
|
250
251
|
if (!bearer)
|
|
251
252
|
return undefined;
|
|
252
|
-
const
|
|
253
|
-
|
|
253
|
+
const apiEndpoint = host.backendConfig()?.apiEndpoint;
|
|
254
|
+
if (!apiEndpoint)
|
|
255
|
+
return undefined;
|
|
256
|
+
const authEndpoint = deriveSubdomainEndpoint(apiEndpoint, 'auth');
|
|
254
257
|
if (!authEndpoint)
|
|
255
258
|
return undefined;
|
|
256
259
|
const client = auth.createClient({
|
package/esm/db-probe.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { classifyBackendErrors, errorsText, postGraphQL } from './graphql-http';
|
|
2
|
-
import { getHost } from './host';
|
|
3
|
-
const DEFAULT_API_ENDPOINT = 'http://api.localhost:3000/graphql';
|
|
2
|
+
import { getHost, noBackendReason } from './host';
|
|
4
3
|
const FIELDS = 'id name ownerId hash suspendedAt suspendedReason';
|
|
5
4
|
// A backend from before `hash` and the suspension columns.
|
|
6
5
|
const BASIC_FIELDS = 'id name ownerId';
|
|
@@ -97,8 +96,12 @@ export async function probeBoundDatabase(databaseId) {
|
|
|
97
96
|
detail: `No usable account credential. ${host.signInHint ?? 'Sign in to the app, then retry.'}`,
|
|
98
97
|
};
|
|
99
98
|
}
|
|
99
|
+
const endpoint = host.backendConfig()?.apiEndpoint;
|
|
100
|
+
if (!endpoint) {
|
|
101
|
+
return { status: 'unreachable', databaseId, cause: 'error', detail: noBackendReason(host.signInHint) };
|
|
102
|
+
}
|
|
100
103
|
return probeDatabase({
|
|
101
|
-
endpoint
|
|
104
|
+
endpoint,
|
|
102
105
|
bearer,
|
|
103
106
|
databaseId,
|
|
104
107
|
signInHint: host.signInHint,
|
package/esm/host.d.ts
CHANGED
|
@@ -18,7 +18,14 @@ export type HostAccount = {
|
|
|
18
18
|
export type HostBackendConfig = {
|
|
19
19
|
apiEndpoint?: string;
|
|
20
20
|
modulesEndpoint?: string;
|
|
21
|
+
agentEndpoint?: string;
|
|
22
|
+
computeEndpoint?: string;
|
|
21
23
|
};
|
|
24
|
+
/**
|
|
25
|
+
* The tools never default to a backend: with no configured endpoint they fail
|
|
26
|
+
* and say so, rather than sending the account credential to a guessed host.
|
|
27
|
+
*/
|
|
28
|
+
export declare function noBackendReason(hint?: string): string;
|
|
22
29
|
export type ActiveDataToken = {
|
|
23
30
|
token: string;
|
|
24
31
|
userId?: string;
|
package/esm/host.js
CHANGED
|
@@ -10,6 +10,13 @@
|
|
|
10
10
|
* Nothing here is harness-specific: the host is the *application* a tool acts
|
|
11
11
|
* on behalf of, so the same contract serves whichever adapter runs the tools.
|
|
12
12
|
*/
|
|
13
|
+
/**
|
|
14
|
+
* The tools never default to a backend: with no configured endpoint they fail
|
|
15
|
+
* and say so, rather than sending the account credential to a guessed host.
|
|
16
|
+
*/
|
|
17
|
+
export function noBackendReason(hint) {
|
|
18
|
+
return `No Constructive backend is configured. ${hint ?? 'Choose a backend in the app, then retry.'}`;
|
|
19
|
+
}
|
|
13
20
|
export const DEFAULT_DATA_TOKEN_SKEW_MS = 30_000;
|
|
14
21
|
const DEFAULT_REPROVISION_ADVICE = 'Re-provision with provision_database (reprovision: true): the schema is rebuilt from the project, and records do not carry over.';
|
|
15
22
|
/** The advice for a database that is gone or under another account. */
|
package/esm/index.d.ts
CHANGED
|
@@ -15,7 +15,7 @@ export { CONTEXT_ENV_KEYS, CONTEXT_ENV_PREFIX, type ContextEnvKey, type ContextS
|
|
|
15
15
|
export { type DatabaseProbe, probeBoundDatabase, probeDatabase, type UnreachableCause, } from './db-probe';
|
|
16
16
|
export { constructiveGateDeps, type ConstructiveGateResolvers } from './gate';
|
|
17
17
|
export { type BackendErrorKind, classifyBackendErrors, type HttpGraphQLResponse, postGraphQL, statusAdapter, } from './graphql-http';
|
|
18
|
-
export { type ActiveDataToken, configureHost, type DataAuthBroker, DEFAULT_DATA_TOKEN_SKEW_MS, getHost, type HostAccount, type HostBackendConfig, type HostProvisionOverlay, type PreviewToken, type ProjectBinding, type SecretDelivery, type StepUpRequest, type ToolsHost, } from './host';
|
|
18
|
+
export { type ActiveDataToken, configureHost, type DataAuthBroker, DEFAULT_DATA_TOKEN_SKEW_MS, getHost, type HostAccount, type HostBackendConfig, type HostProvisionOverlay, noBackendReason, type PreviewToken, type ProjectBinding, type SecretDelivery, type StepUpRequest, type ToolsHost, } from './host';
|
|
19
19
|
export { archiveBindingKeys, ARCHIVED_BINDING_KEYS, type EnvVars, mergeEnv, provisionEnvVars, readEnv, } from './provision-database/env-file';
|
|
20
20
|
export { loadProvisionManifest, parseProvisionManifest, PROVISION_MANIFEST_FILE, type ProvisionManifest, } from './provision-database/manifest';
|
|
21
21
|
export { allModulePresets, DEFAULT_PROVISION_PRESET, getModulePreset, type ModulePreset, type ProvisionModule, } from './provision-database/presets';
|
package/esm/index.js
CHANGED
|
@@ -47,7 +47,7 @@ export { CONTEXT_ENV_KEYS, CONTEXT_ENV_PREFIX, deriveSubdomainEndpoint, fromEnvF
|
|
|
47
47
|
export { probeBoundDatabase, probeDatabase, } from './db-probe';
|
|
48
48
|
export { constructiveGateDeps } from './gate';
|
|
49
49
|
export { classifyBackendErrors, postGraphQL, statusAdapter, } from './graphql-http';
|
|
50
|
-
export { configureHost, DEFAULT_DATA_TOKEN_SKEW_MS, getHost, } from './host';
|
|
50
|
+
export { configureHost, DEFAULT_DATA_TOKEN_SKEW_MS, getHost, noBackendReason, } from './host';
|
|
51
51
|
export { archiveBindingKeys, ARCHIVED_BINDING_KEYS, mergeEnv, provisionEnvVars, readEnv, } from './provision-database/env-file';
|
|
52
52
|
export { loadProvisionManifest, parseProvisionManifest, PROVISION_MANIFEST_FILE, } from './provision-database/manifest';
|
|
53
53
|
export { allModulePresets, DEFAULT_PROVISION_PRESET, getModulePreset, } from './provision-database/presets';
|
|
@@ -2,9 +2,8 @@
|
|
|
2
2
|
// account gate a signed-in session is guaranteed, so every new project provisions
|
|
3
3
|
// UNDER the account (account-owned + enumerable): the database's owner_id is the
|
|
4
4
|
// account's own userId, and the account's API key (falling back to its session
|
|
5
|
-
// token) is the bearer that authenticates the provision mutation
|
|
6
|
-
//
|
|
7
|
-
// skip path). A signed-in session with no usable bearer (keychain locked) is an
|
|
5
|
+
// token) is the bearer that authenticates the provision mutation. A
|
|
6
|
+
// signed-in session with no usable bearer (keychain locked) is an
|
|
8
7
|
// explicit error, never a silent throwaway owner.
|
|
9
8
|
// Side-effect-free so the branch is unit-testable without runtime/network.
|
|
10
9
|
export function selectProvisionCredential(account, signInHint) {
|
|
@@ -26,17 +26,15 @@ export declare const PENDING_PROVISION_KEYS: {
|
|
|
26
26
|
readonly userId: "PROVISION_TICKET_USER_ID";
|
|
27
27
|
readonly attempts: "PROVISION_TICKET_ATTEMPTS";
|
|
28
28
|
};
|
|
29
|
-
/** The
|
|
30
|
-
export declare const PROVISION_ENV_KEYS: readonly ["DATABASE_ID", "DATABASE_NAME", "OWNER_ID", "
|
|
29
|
+
/** The binding keys provision_database owns in a project's .env. */
|
|
30
|
+
export declare const PROVISION_ENV_KEYS: readonly ["DATABASE_ID", "DATABASE_NAME", "OWNER_ID", "PGDATABASE", "API_ENDPOINT", "MODULES_ENDPOINT"];
|
|
31
31
|
/**
|
|
32
32
|
* Keys archived as comments on reprovision — the binding identity: which
|
|
33
|
-
* database, under whom, on which backend.
|
|
34
|
-
* token, identical across bindings, so it is overwritten in place rather than
|
|
35
|
-
* archived.
|
|
33
|
+
* database, under whom, on which backend.
|
|
36
34
|
*/
|
|
37
35
|
export declare const ARCHIVED_BINDING_KEYS: readonly ["DATABASE_ID", "DATABASE_NAME", "OWNER_ID", "PGDATABASE", "API_ENDPOINT", "MODULES_ENDPOINT"];
|
|
38
36
|
/**
|
|
39
|
-
* Build the
|
|
37
|
+
* Build the database binding written to a project's .env. The
|
|
40
38
|
* endpoint pins record which backend owns this binding — outside-app consumers
|
|
41
39
|
* (verify-feature.sh, skill scripts, dev-server shells) resolve them instead of
|
|
42
40
|
* falling back to localhost defaults when the app targets a hosted backend.
|
|
@@ -45,7 +43,6 @@ export declare function provisionEnvVars(args: {
|
|
|
45
43
|
databaseId: string;
|
|
46
44
|
databaseName: string;
|
|
47
45
|
ownerId: string;
|
|
48
|
-
accessToken: string;
|
|
49
46
|
apiEndpoint: string;
|
|
50
47
|
modulesEndpoint: string;
|
|
51
48
|
}): EnvVars;
|
|
@@ -87,21 +87,18 @@ export const PENDING_PROVISION_KEYS = {
|
|
|
87
87
|
userId: 'PROVISION_TICKET_USER_ID',
|
|
88
88
|
attempts: 'PROVISION_TICKET_ATTEMPTS',
|
|
89
89
|
};
|
|
90
|
-
/** The
|
|
90
|
+
/** The binding keys provision_database owns in a project's .env. */
|
|
91
91
|
export const PROVISION_ENV_KEYS = [
|
|
92
92
|
'DATABASE_ID',
|
|
93
93
|
'DATABASE_NAME',
|
|
94
94
|
'OWNER_ID',
|
|
95
|
-
'ACCESS_TOKEN',
|
|
96
95
|
'PGDATABASE',
|
|
97
96
|
'API_ENDPOINT',
|
|
98
97
|
'MODULES_ENDPOINT',
|
|
99
98
|
];
|
|
100
99
|
/**
|
|
101
100
|
* Keys archived as comments on reprovision — the binding identity: which
|
|
102
|
-
* database, under whom, on which backend.
|
|
103
|
-
* token, identical across bindings, so it is overwritten in place rather than
|
|
104
|
-
* archived.
|
|
101
|
+
* database, under whom, on which backend.
|
|
105
102
|
*/
|
|
106
103
|
export const ARCHIVED_BINDING_KEYS = [
|
|
107
104
|
'DATABASE_ID',
|
|
@@ -112,7 +109,7 @@ export const ARCHIVED_BINDING_KEYS = [
|
|
|
112
109
|
'MODULES_ENDPOINT',
|
|
113
110
|
];
|
|
114
111
|
/**
|
|
115
|
-
* Build the
|
|
112
|
+
* Build the database binding written to a project's .env. The
|
|
116
113
|
* endpoint pins record which backend owns this binding — outside-app consumers
|
|
117
114
|
* (verify-feature.sh, skill scripts, dev-server shells) resolve them instead of
|
|
118
115
|
* falling back to localhost defaults when the app targets a hosted backend.
|
|
@@ -122,7 +119,6 @@ export function provisionEnvVars(args) {
|
|
|
122
119
|
DATABASE_ID: args.databaseId,
|
|
123
120
|
DATABASE_NAME: args.databaseName,
|
|
124
121
|
OWNER_ID: args.ownerId,
|
|
125
|
-
ACCESS_TOKEN: args.accessToken,
|
|
126
122
|
PGDATABASE: args.databaseName,
|
|
127
123
|
API_ENDPOINT: args.apiEndpoint,
|
|
128
124
|
MODULES_ENDPOINT: args.modulesEndpoint,
|
|
@@ -12,7 +12,7 @@ import { readFile, realpath, writeFile } from 'node:fs/promises';
|
|
|
12
12
|
import path from 'node:path';
|
|
13
13
|
import { prewarmAppWorkspace, run } from '../app-workspace';
|
|
14
14
|
import { probeDatabase } from '../db-probe';
|
|
15
|
-
import { getHost, reprovisionAdvice } from '../host';
|
|
15
|
+
import { getHost, noBackendReason, reprovisionAdvice } from '../host';
|
|
16
16
|
import { selectProvisionCredential } from './credential';
|
|
17
17
|
import { archiveBindingKeys, ARCHIVED_BINDING_KEYS, mergeEnv, provisionEnvVars, readEnv } from './env-file';
|
|
18
18
|
import { loadProvisionManifest } from './manifest';
|
|
@@ -20,8 +20,6 @@ import { abandonedTickets, archivePendingTicket, clearPendingTicket, MAX_PENDING
|
|
|
20
20
|
import { selectProvisionRequest } from './preset-match';
|
|
21
21
|
import { createProvisionExecutor, isTicketInFlight, isTransientProvisionError, ProvisionPendingError, ProvisionTicketLostError, requestDatabaseProvision, } from './request-database';
|
|
22
22
|
import { resolveProvisionModules } from './resolve';
|
|
23
|
-
const DEFAULT_API_ENDPOINT = 'http://api.localhost:3000/graphql';
|
|
24
|
-
const DEFAULT_MODULES_ENDPOINT = 'http://modules.localhost:3000/graphql';
|
|
25
23
|
function failure(code, message, extra = {}) {
|
|
26
24
|
return { ...extra, success: false, message, code };
|
|
27
25
|
}
|
|
@@ -108,18 +106,19 @@ async function provision(input, cwd, exec, viaTool) {
|
|
|
108
106
|
const name = input.databaseName?.trim();
|
|
109
107
|
if (!name)
|
|
110
108
|
return failure('failed', 'database_name is required.');
|
|
111
|
-
//
|
|
112
|
-
//
|
|
113
|
-
//
|
|
114
|
-
// store isn't ready.
|
|
109
|
+
// The host's backend config is the only endpoint source: the account bearer
|
|
110
|
+
// provisions with it, so an ambient env var or a guessed default must never
|
|
111
|
+
// redirect it.
|
|
115
112
|
const host = getHost();
|
|
116
113
|
const backend = host.backendConfig();
|
|
117
|
-
const apiEndpoint =
|
|
118
|
-
const modulesEndpoint =
|
|
114
|
+
const apiEndpoint = backend?.apiEndpoint;
|
|
115
|
+
const modulesEndpoint = backend?.modulesEndpoint;
|
|
116
|
+
if (!apiEndpoint || !modulesEndpoint) {
|
|
117
|
+
return failure('failed', `Cannot provision a database: ${noBackendReason(host.signInHint)}`);
|
|
118
|
+
}
|
|
119
119
|
// Every project provisions UNDER the account (requestDatabase owns the
|
|
120
120
|
// database to the JWT user). The account bearer authenticates the mutation
|
|
121
|
-
// and the probes
|
|
122
|
-
// the login session, and any provision run (the skip path too) refreshes it.
|
|
121
|
+
// and the probes.
|
|
123
122
|
const credential = selectProvisionCredential(host.account(), host.signInHint);
|
|
124
123
|
if (credential.mode === 'error') {
|
|
125
124
|
return failure('signed-out', `Cannot provision a database: ${credential.reason}`);
|
|
@@ -177,13 +176,10 @@ async function keepBinding(r, env, bound, adopt) {
|
|
|
177
176
|
if (adopt && adopt !== bound) {
|
|
178
177
|
return failure('binding-live', `The project is bound to a live database (DATABASE_ID=${bound}); adopt binds only an unbound project. Use replace mode to swap it.`, { databaseId: bound });
|
|
179
178
|
}
|
|
180
|
-
// Heal
|
|
181
|
-
// endpoint pins, and the binding keys a partial .env lacks.
|
|
179
|
+
// Heal stale endpoint pins and missing binding keys on the skip path.
|
|
182
180
|
const databaseName = env.DATABASE_NAME || probe.name || '';
|
|
183
181
|
const ownerId = probe.ownerId ?? r.ownerId;
|
|
184
182
|
const refresh = {};
|
|
185
|
-
if (env.ACCESS_TOKEN !== r.bearer)
|
|
186
|
-
refresh.ACCESS_TOKEN = r.bearer;
|
|
187
183
|
if (env.API_ENDPOINT !== r.apiEndpoint)
|
|
188
184
|
refresh.API_ENDPOINT = r.apiEndpoint;
|
|
189
185
|
if (env.MODULES_ENDPOINT !== r.modulesEndpoint)
|
|
@@ -522,7 +518,6 @@ async function bindDatabase(r, bind, prewarm) {
|
|
|
522
518
|
databaseId,
|
|
523
519
|
databaseName: r.name,
|
|
524
520
|
ownerId: r.ownerId,
|
|
525
|
-
accessToken: r.bearer,
|
|
526
521
|
apiEndpoint: r.apiEndpoint,
|
|
527
522
|
modulesEndpoint: r.modulesEndpoint,
|
|
528
523
|
})));
|
|
@@ -542,8 +537,8 @@ async function bindDatabase(r, bind, prewarm) {
|
|
|
542
537
|
: '',
|
|
543
538
|
].join('');
|
|
544
539
|
const message = bind.adopted
|
|
545
|
-
? `Bound database "${r.name}" (ID: ${databaseId}), which this account provisioned earlier.
|
|
546
|
-
: `Provisioned database "${r.name}" (ID: ${databaseId}).
|
|
540
|
+
? `Bound database "${r.name}" (ID: ${databaseId}), which this account provisioned earlier. Binding written to .env.${notes}`
|
|
541
|
+
: `Provisioned database "${r.name}" (ID: ${databaseId}). Binding written to .env.${notes}`;
|
|
547
542
|
return {
|
|
548
543
|
success: true,
|
|
549
544
|
message,
|
|
@@ -15,7 +15,7 @@ export function createProvisionDatabaseTool(exec = run) {
|
|
|
15
15
|
return {
|
|
16
16
|
name: 'provision_database',
|
|
17
17
|
label: 'Provision database',
|
|
18
|
-
description: 'Bootstrap a new Constructive database for the project under your account: provision the standard module set and write
|
|
18
|
+
description: 'Bootstrap a new Constructive database for the project under your account: provision the standard module set and write the database binding (DATABASE_ID, DATABASE_NAME, etc.) to the project .env. Run this ONCE before any schema/record tools. If the project is already bound to a live database under the signed-in account it skips; if the bound database no longer exists on this backend (refreshed/deleted) or belongs to a different account, pass reprovision: true to mint a fresh one (old binding archived in .env, old database kept; rebuild the schema afterwards). If a call returns while the database is still provisioning, call it again with the same database_name to keep waiting; a different name starts a second database.',
|
|
19
19
|
promptSnippet: 'provision_database: one-time bootstrap of the project database (owner + modules + .env). Run before describe_schema/provision_blueprint. Skips when the existing binding is live under the signed-in account; reprovision: true replaces a dead or foreign-account binding (archives old keys, never deletes the old db). Gated.',
|
|
20
20
|
parameters: ProvisionDatabaseZod,
|
|
21
21
|
async execute(params, ctx) {
|
package/esm/tools/run-codegen.js
CHANGED
|
@@ -4,11 +4,10 @@ import path from 'node:path';
|
|
|
4
4
|
import { z } from 'zod';
|
|
5
5
|
import { detectAppKind, ensureAppScaffold, ensureComposedInstall, ensureWorkspaceConfig, IGNORED_BUILDS_RE, pinCodegenLatest, prewarmAppWorkspace, run, settlePrewarm, } from '../app-workspace';
|
|
6
6
|
import { classifyBackendErrors, postGraphQL } from '../graphql-http';
|
|
7
|
-
import { getHost } from '../host';
|
|
7
|
+
import { getHost, noBackendReason } from '../host';
|
|
8
8
|
import { readEnv } from '../provision-database/env-file';
|
|
9
9
|
import { normalizeSdkBarrels } from '../run-codegen/barrels';
|
|
10
10
|
import { codegenEndpointEnv, derivePlaneEndpoints, envLocalContent } from '../run-codegen/endpoints';
|
|
11
|
-
const DEFAULT_API_ENDPOINT = 'http://api.localhost:3000/graphql';
|
|
12
11
|
const RunCodegenZod = z.object({});
|
|
13
12
|
function fail(message, extra) {
|
|
14
13
|
return {
|
|
@@ -128,8 +127,10 @@ export function createRunCodegenTool(exec = run) {
|
|
|
128
127
|
if (!dbName) {
|
|
129
128
|
return fail('DATABASE_NAME missing from .env — provision the database first, then re-run.');
|
|
130
129
|
}
|
|
131
|
-
const
|
|
132
|
-
const apiEndpoint = envApiEndpoint ||
|
|
130
|
+
const host = getHost();
|
|
131
|
+
const apiEndpoint = envApiEndpoint || host.backendConfig()?.apiEndpoint;
|
|
132
|
+
if (!apiEndpoint)
|
|
133
|
+
return fail(noBackendReason(host.signInHint));
|
|
133
134
|
const planes = derivePlaneEndpoints(apiEndpoint, dbName);
|
|
134
135
|
// The dev server reads .env.local (NEXT_PUBLIC_DB_NAME + per-DB endpoint
|
|
135
136
|
// overrides so it doesn't fall back to localhost vhosts on a remote
|
|
@@ -184,9 +185,8 @@ export function createRunCodegenTool(exec = run) {
|
|
|
184
185
|
return fail('codegen failed: BACKEND OUTAGE — the Constructive backend reset the connection ' +
|
|
185
186
|
'during schema introspection, and it stayed down across every internal retry. ' +
|
|
186
187
|
'This is an EXTERNAL outage, not a code or install problem; re-running run_codegen ' +
|
|
187
|
-
'will NOT fix it.
|
|
188
|
-
'
|
|
189
|
-
'build-orchestrator. Do NOT loop on run_codegen (THRASH-001).\n\n' +
|
|
188
|
+
'will NOT fix it. Check backend reachability once, then report the blocker and wait ' +
|
|
189
|
+
'for the backend to recover. Do NOT loop on run_codegen (THRASH-001).\n\n' +
|
|
190
190
|
codegen.out, stepDetails());
|
|
191
191
|
}
|
|
192
192
|
return fail(`codegen failed:\n${codegen.out}`, stepDetails());
|
package/host.d.ts
CHANGED
|
@@ -18,7 +18,14 @@ export type HostAccount = {
|
|
|
18
18
|
export type HostBackendConfig = {
|
|
19
19
|
apiEndpoint?: string;
|
|
20
20
|
modulesEndpoint?: string;
|
|
21
|
+
agentEndpoint?: string;
|
|
22
|
+
computeEndpoint?: string;
|
|
21
23
|
};
|
|
24
|
+
/**
|
|
25
|
+
* The tools never default to a backend: with no configured endpoint they fail
|
|
26
|
+
* and say so, rather than sending the account credential to a guessed host.
|
|
27
|
+
*/
|
|
28
|
+
export declare function noBackendReason(hint?: string): string;
|
|
22
29
|
export type ActiveDataToken = {
|
|
23
30
|
token: string;
|
|
24
31
|
userId?: string;
|
package/host.js
CHANGED
|
@@ -13,9 +13,17 @@
|
|
|
13
13
|
*/
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.DEFAULT_DATA_TOKEN_SKEW_MS = void 0;
|
|
16
|
+
exports.noBackendReason = noBackendReason;
|
|
16
17
|
exports.reprovisionAdvice = reprovisionAdvice;
|
|
17
18
|
exports.configureHost = configureHost;
|
|
18
19
|
exports.getHost = getHost;
|
|
20
|
+
/**
|
|
21
|
+
* The tools never default to a backend: with no configured endpoint they fail
|
|
22
|
+
* and say so, rather than sending the account credential to a guessed host.
|
|
23
|
+
*/
|
|
24
|
+
function noBackendReason(hint) {
|
|
25
|
+
return `No Constructive backend is configured. ${hint ?? 'Choose a backend in the app, then retry.'}`;
|
|
26
|
+
}
|
|
19
27
|
exports.DEFAULT_DATA_TOKEN_SKEW_MS = 30_000;
|
|
20
28
|
const DEFAULT_REPROVISION_ADVICE = 'Re-provision with provision_database (reprovision: true): the schema is rebuilt from the project, and records do not carry over.';
|
|
21
29
|
/** The advice for a database that is gone or under another account. */
|
package/index.d.ts
CHANGED
|
@@ -15,7 +15,7 @@ export { CONTEXT_ENV_KEYS, CONTEXT_ENV_PREFIX, type ContextEnvKey, type ContextS
|
|
|
15
15
|
export { type DatabaseProbe, probeBoundDatabase, probeDatabase, type UnreachableCause, } from './db-probe';
|
|
16
16
|
export { constructiveGateDeps, type ConstructiveGateResolvers } from './gate';
|
|
17
17
|
export { type BackendErrorKind, classifyBackendErrors, type HttpGraphQLResponse, postGraphQL, statusAdapter, } from './graphql-http';
|
|
18
|
-
export { type ActiveDataToken, configureHost, type DataAuthBroker, DEFAULT_DATA_TOKEN_SKEW_MS, getHost, type HostAccount, type HostBackendConfig, type HostProvisionOverlay, type PreviewToken, type ProjectBinding, type SecretDelivery, type StepUpRequest, type ToolsHost, } from './host';
|
|
18
|
+
export { type ActiveDataToken, configureHost, type DataAuthBroker, DEFAULT_DATA_TOKEN_SKEW_MS, getHost, type HostAccount, type HostBackendConfig, type HostProvisionOverlay, noBackendReason, type PreviewToken, type ProjectBinding, type SecretDelivery, type StepUpRequest, type ToolsHost, } from './host';
|
|
19
19
|
export { archiveBindingKeys, ARCHIVED_BINDING_KEYS, type EnvVars, mergeEnv, provisionEnvVars, readEnv, } from './provision-database/env-file';
|
|
20
20
|
export { loadProvisionManifest, parseProvisionManifest, PROVISION_MANIFEST_FILE, type ProvisionManifest, } from './provision-database/manifest';
|
|
21
21
|
export { allModulePresets, DEFAULT_PROVISION_PRESET, getModulePreset, type ModulePreset, type ProvisionModule, } from './provision-database/presets';
|
package/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createTemplatePreviewTables = exports.toolSchema = exports.SchemaReadError = exports.readSchemaModel = exports.accessOf = exports.resolveProvisionModules = exports.moduleKey = exports.provisionDatabase = exports.getModulePreset = exports.DEFAULT_PROVISION_PRESET = exports.allModulePresets = exports.PROVISION_MANIFEST_FILE = exports.parseProvisionManifest = exports.loadProvisionManifest = exports.readEnv = exports.provisionEnvVars = exports.mergeEnv = exports.ARCHIVED_BINDING_KEYS = exports.archiveBindingKeys = exports.getHost = exports.DEFAULT_DATA_TOKEN_SKEW_MS = exports.configureHost = exports.statusAdapter = exports.postGraphQL = exports.classifyBackendErrors = exports.constructiveGateDeps = exports.probeDatabase = exports.probeBoundDatabase = exports.resolveProjectContext = exports.resolveDataToken = exports.fromEnvironment = exports.fromEnvFile = exports.deriveSubdomainEndpoint = exports.CONTEXT_ENV_PREFIX = exports.CONTEXT_ENV_KEYS = exports.waitForConstruction = exports.readConstructionFailure = exports.CONSTRUCTION_DEADLINE_MS = exports.buildBlueprint = exports.BlueprintBuildError = exports.constructiveDbTools = void 0;
|
|
3
|
+
exports.createTemplatePreviewTables = exports.toolSchema = exports.SchemaReadError = exports.readSchemaModel = exports.accessOf = exports.resolveProvisionModules = exports.moduleKey = exports.provisionDatabase = exports.getModulePreset = exports.DEFAULT_PROVISION_PRESET = exports.allModulePresets = exports.PROVISION_MANIFEST_FILE = exports.parseProvisionManifest = exports.loadProvisionManifest = exports.readEnv = exports.provisionEnvVars = exports.mergeEnv = exports.ARCHIVED_BINDING_KEYS = exports.archiveBindingKeys = exports.noBackendReason = exports.getHost = exports.DEFAULT_DATA_TOKEN_SKEW_MS = exports.configureHost = exports.statusAdapter = exports.postGraphQL = exports.classifyBackendErrors = exports.constructiveGateDeps = exports.probeDatabase = exports.probeBoundDatabase = exports.resolveProjectContext = exports.resolveDataToken = exports.fromEnvironment = exports.fromEnvFile = exports.deriveSubdomainEndpoint = exports.CONTEXT_ENV_PREFIX = exports.CONTEXT_ENV_KEYS = exports.waitForConstruction = exports.readConstructionFailure = exports.CONSTRUCTION_DEADLINE_MS = exports.buildBlueprint = exports.BlueprintBuildError = exports.constructiveDbTools = void 0;
|
|
4
4
|
exports.createConstructiveDbTools = createConstructiveDbTools;
|
|
5
5
|
const host_1 = require("./host");
|
|
6
6
|
const add_policies_1 = require("./tools/add-policies");
|
|
@@ -73,6 +73,7 @@ var host_2 = require("./host");
|
|
|
73
73
|
Object.defineProperty(exports, "configureHost", { enumerable: true, get: function () { return host_2.configureHost; } });
|
|
74
74
|
Object.defineProperty(exports, "DEFAULT_DATA_TOKEN_SKEW_MS", { enumerable: true, get: function () { return host_2.DEFAULT_DATA_TOKEN_SKEW_MS; } });
|
|
75
75
|
Object.defineProperty(exports, "getHost", { enumerable: true, get: function () { return host_2.getHost; } });
|
|
76
|
+
Object.defineProperty(exports, "noBackendReason", { enumerable: true, get: function () { return host_2.noBackendReason; } });
|
|
76
77
|
var env_file_1 = require("./provision-database/env-file");
|
|
77
78
|
Object.defineProperty(exports, "archiveBindingKeys", { enumerable: true, get: function () { return env_file_1.archiveBindingKeys; } });
|
|
78
79
|
Object.defineProperty(exports, "ARCHIVED_BINDING_KEYS", { enumerable: true, get: function () { return env_file_1.ARCHIVED_BINDING_KEYS; } });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agentic-kit/db-tools",
|
|
3
|
-
"version": "0.12.
|
|
3
|
+
"version": "0.12.44",
|
|
4
4
|
"author": "Constructive <developers@constructive.io>",
|
|
5
5
|
"description": "harness-neutral Constructive database tools — provisioning, schema, policies, records, templates and codegen as HarnessTools any adapter can bind",
|
|
6
6
|
"main": "index.js",
|
|
@@ -22,10 +22,10 @@
|
|
|
22
22
|
"dependencies": {
|
|
23
23
|
"zod": "^4.4.3",
|
|
24
24
|
"12factor-env": "^1.34.1",
|
|
25
|
-
"@constructive-io/sdk": "^1.21.48",
|
|
26
25
|
"@agentic-kit/harness": "^0.18.2",
|
|
27
|
-
"@constructive-io/graphql-query": "^4.24.
|
|
28
|
-
"
|
|
26
|
+
"@constructive-io/graphql-query": "^4.24.24",
|
|
27
|
+
"@constructive-io/sdk": "^1.21.50",
|
|
28
|
+
"node-type-registry": "^1.22.10"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"typebox": "^1.0.0"
|
|
@@ -3,9 +3,8 @@
|
|
|
3
3
|
// account gate a signed-in session is guaranteed, so every new project provisions
|
|
4
4
|
// UNDER the account (account-owned + enumerable): the database's owner_id is the
|
|
5
5
|
// account's own userId, and the account's API key (falling back to its session
|
|
6
|
-
// token) is the bearer that authenticates the provision mutation
|
|
7
|
-
//
|
|
8
|
-
// skip path). A signed-in session with no usable bearer (keychain locked) is an
|
|
6
|
+
// token) is the bearer that authenticates the provision mutation. A
|
|
7
|
+
// signed-in session with no usable bearer (keychain locked) is an
|
|
9
8
|
// explicit error, never a silent throwaway owner.
|
|
10
9
|
// Side-effect-free so the branch is unit-testable without runtime/network.
|
|
11
10
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
@@ -26,17 +26,15 @@ export declare const PENDING_PROVISION_KEYS: {
|
|
|
26
26
|
readonly userId: "PROVISION_TICKET_USER_ID";
|
|
27
27
|
readonly attempts: "PROVISION_TICKET_ATTEMPTS";
|
|
28
28
|
};
|
|
29
|
-
/** The
|
|
30
|
-
export declare const PROVISION_ENV_KEYS: readonly ["DATABASE_ID", "DATABASE_NAME", "OWNER_ID", "
|
|
29
|
+
/** The binding keys provision_database owns in a project's .env. */
|
|
30
|
+
export declare const PROVISION_ENV_KEYS: readonly ["DATABASE_ID", "DATABASE_NAME", "OWNER_ID", "PGDATABASE", "API_ENDPOINT", "MODULES_ENDPOINT"];
|
|
31
31
|
/**
|
|
32
32
|
* Keys archived as comments on reprovision — the binding identity: which
|
|
33
|
-
* database, under whom, on which backend.
|
|
34
|
-
* token, identical across bindings, so it is overwritten in place rather than
|
|
35
|
-
* archived.
|
|
33
|
+
* database, under whom, on which backend.
|
|
36
34
|
*/
|
|
37
35
|
export declare const ARCHIVED_BINDING_KEYS: readonly ["DATABASE_ID", "DATABASE_NAME", "OWNER_ID", "PGDATABASE", "API_ENDPOINT", "MODULES_ENDPOINT"];
|
|
38
36
|
/**
|
|
39
|
-
* Build the
|
|
37
|
+
* Build the database binding written to a project's .env. The
|
|
40
38
|
* endpoint pins record which backend owns this binding — outside-app consumers
|
|
41
39
|
* (verify-feature.sh, skill scripts, dev-server shells) resolve them instead of
|
|
42
40
|
* falling back to localhost defaults when the app targets a hosted backend.
|
|
@@ -45,7 +43,6 @@ export declare function provisionEnvVars(args: {
|
|
|
45
43
|
databaseId: string;
|
|
46
44
|
databaseName: string;
|
|
47
45
|
ownerId: string;
|
|
48
|
-
accessToken: string;
|
|
49
46
|
apiEndpoint: string;
|
|
50
47
|
modulesEndpoint: string;
|
|
51
48
|
}): EnvVars;
|
|
@@ -95,21 +95,18 @@ exports.PENDING_PROVISION_KEYS = {
|
|
|
95
95
|
userId: 'PROVISION_TICKET_USER_ID',
|
|
96
96
|
attempts: 'PROVISION_TICKET_ATTEMPTS',
|
|
97
97
|
};
|
|
98
|
-
/** The
|
|
98
|
+
/** The binding keys provision_database owns in a project's .env. */
|
|
99
99
|
exports.PROVISION_ENV_KEYS = [
|
|
100
100
|
'DATABASE_ID',
|
|
101
101
|
'DATABASE_NAME',
|
|
102
102
|
'OWNER_ID',
|
|
103
|
-
'ACCESS_TOKEN',
|
|
104
103
|
'PGDATABASE',
|
|
105
104
|
'API_ENDPOINT',
|
|
106
105
|
'MODULES_ENDPOINT',
|
|
107
106
|
];
|
|
108
107
|
/**
|
|
109
108
|
* Keys archived as comments on reprovision — the binding identity: which
|
|
110
|
-
* database, under whom, on which backend.
|
|
111
|
-
* token, identical across bindings, so it is overwritten in place rather than
|
|
112
|
-
* archived.
|
|
109
|
+
* database, under whom, on which backend.
|
|
113
110
|
*/
|
|
114
111
|
exports.ARCHIVED_BINDING_KEYS = [
|
|
115
112
|
'DATABASE_ID',
|
|
@@ -120,7 +117,7 @@ exports.ARCHIVED_BINDING_KEYS = [
|
|
|
120
117
|
'MODULES_ENDPOINT',
|
|
121
118
|
];
|
|
122
119
|
/**
|
|
123
|
-
* Build the
|
|
120
|
+
* Build the database binding written to a project's .env. The
|
|
124
121
|
* endpoint pins record which backend owns this binding — outside-app consumers
|
|
125
122
|
* (verify-feature.sh, skill scripts, dev-server shells) resolve them instead of
|
|
126
123
|
* falling back to localhost defaults when the app targets a hosted backend.
|
|
@@ -130,7 +127,6 @@ function provisionEnvVars(args) {
|
|
|
130
127
|
DATABASE_ID: args.databaseId,
|
|
131
128
|
DATABASE_NAME: args.databaseName,
|
|
132
129
|
OWNER_ID: args.ownerId,
|
|
133
|
-
ACCESS_TOKEN: args.accessToken,
|
|
134
130
|
PGDATABASE: args.databaseName,
|
|
135
131
|
API_ENDPOINT: args.apiEndpoint,
|
|
136
132
|
MODULES_ENDPOINT: args.modulesEndpoint,
|
|
@@ -28,8 +28,6 @@ const pending_ticket_1 = require("./pending-ticket");
|
|
|
28
28
|
const preset_match_1 = require("./preset-match");
|
|
29
29
|
const request_database_1 = require("./request-database");
|
|
30
30
|
const resolve_1 = require("./resolve");
|
|
31
|
-
const DEFAULT_API_ENDPOINT = 'http://api.localhost:3000/graphql';
|
|
32
|
-
const DEFAULT_MODULES_ENDPOINT = 'http://modules.localhost:3000/graphql';
|
|
33
31
|
function failure(code, message, extra = {}) {
|
|
34
32
|
return { ...extra, success: false, message, code };
|
|
35
33
|
}
|
|
@@ -116,18 +114,19 @@ async function provision(input, cwd, exec, viaTool) {
|
|
|
116
114
|
const name = input.databaseName?.trim();
|
|
117
115
|
if (!name)
|
|
118
116
|
return failure('failed', 'database_name is required.');
|
|
119
|
-
//
|
|
120
|
-
//
|
|
121
|
-
//
|
|
122
|
-
// store isn't ready.
|
|
117
|
+
// The host's backend config is the only endpoint source: the account bearer
|
|
118
|
+
// provisions with it, so an ambient env var or a guessed default must never
|
|
119
|
+
// redirect it.
|
|
123
120
|
const host = (0, host_1.getHost)();
|
|
124
121
|
const backend = host.backendConfig();
|
|
125
|
-
const apiEndpoint =
|
|
126
|
-
const modulesEndpoint =
|
|
122
|
+
const apiEndpoint = backend?.apiEndpoint;
|
|
123
|
+
const modulesEndpoint = backend?.modulesEndpoint;
|
|
124
|
+
if (!apiEndpoint || !modulesEndpoint) {
|
|
125
|
+
return failure('failed', `Cannot provision a database: ${(0, host_1.noBackendReason)(host.signInHint)}`);
|
|
126
|
+
}
|
|
127
127
|
// Every project provisions UNDER the account (requestDatabase owns the
|
|
128
128
|
// database to the JWT user). The account bearer authenticates the mutation
|
|
129
|
-
// and the probes
|
|
130
|
-
// the login session, and any provision run (the skip path too) refreshes it.
|
|
129
|
+
// and the probes.
|
|
131
130
|
const credential = (0, credential_1.selectProvisionCredential)(host.account(), host.signInHint);
|
|
132
131
|
if (credential.mode === 'error') {
|
|
133
132
|
return failure('signed-out', `Cannot provision a database: ${credential.reason}`);
|
|
@@ -185,13 +184,10 @@ async function keepBinding(r, env, bound, adopt) {
|
|
|
185
184
|
if (adopt && adopt !== bound) {
|
|
186
185
|
return failure('binding-live', `The project is bound to a live database (DATABASE_ID=${bound}); adopt binds only an unbound project. Use replace mode to swap it.`, { databaseId: bound });
|
|
187
186
|
}
|
|
188
|
-
// Heal
|
|
189
|
-
// endpoint pins, and the binding keys a partial .env lacks.
|
|
187
|
+
// Heal stale endpoint pins and missing binding keys on the skip path.
|
|
190
188
|
const databaseName = env.DATABASE_NAME || probe.name || '';
|
|
191
189
|
const ownerId = probe.ownerId ?? r.ownerId;
|
|
192
190
|
const refresh = {};
|
|
193
|
-
if (env.ACCESS_TOKEN !== r.bearer)
|
|
194
|
-
refresh.ACCESS_TOKEN = r.bearer;
|
|
195
191
|
if (env.API_ENDPOINT !== r.apiEndpoint)
|
|
196
192
|
refresh.API_ENDPOINT = r.apiEndpoint;
|
|
197
193
|
if (env.MODULES_ENDPOINT !== r.modulesEndpoint)
|
|
@@ -530,7 +526,6 @@ async function bindDatabase(r, bind, prewarm) {
|
|
|
530
526
|
databaseId,
|
|
531
527
|
databaseName: r.name,
|
|
532
528
|
ownerId: r.ownerId,
|
|
533
|
-
accessToken: r.bearer,
|
|
534
529
|
apiEndpoint: r.apiEndpoint,
|
|
535
530
|
modulesEndpoint: r.modulesEndpoint,
|
|
536
531
|
})));
|
|
@@ -550,8 +545,8 @@ async function bindDatabase(r, bind, prewarm) {
|
|
|
550
545
|
: '',
|
|
551
546
|
].join('');
|
|
552
547
|
const message = bind.adopted
|
|
553
|
-
? `Bound database "${r.name}" (ID: ${databaseId}), which this account provisioned earlier.
|
|
554
|
-
: `Provisioned database "${r.name}" (ID: ${databaseId}).
|
|
548
|
+
? `Bound database "${r.name}" (ID: ${databaseId}), which this account provisioned earlier. Binding written to .env.${notes}`
|
|
549
|
+
: `Provisioned database "${r.name}" (ID: ${databaseId}). Binding written to .env.${notes}`;
|
|
555
550
|
return {
|
|
556
551
|
success: true,
|
|
557
552
|
message,
|
|
@@ -19,7 +19,7 @@ function createProvisionDatabaseTool(exec = app_workspace_1.run) {
|
|
|
19
19
|
return {
|
|
20
20
|
name: 'provision_database',
|
|
21
21
|
label: 'Provision database',
|
|
22
|
-
description: 'Bootstrap a new Constructive database for the project under your account: provision the standard module set and write
|
|
22
|
+
description: 'Bootstrap a new Constructive database for the project under your account: provision the standard module set and write the database binding (DATABASE_ID, DATABASE_NAME, etc.) to the project .env. Run this ONCE before any schema/record tools. If the project is already bound to a live database under the signed-in account it skips; if the bound database no longer exists on this backend (refreshed/deleted) or belongs to a different account, pass reprovision: true to mint a fresh one (old binding archived in .env, old database kept; rebuild the schema afterwards). If a call returns while the database is still provisioning, call it again with the same database_name to keep waiting; a different name starts a second database.',
|
|
23
23
|
promptSnippet: 'provision_database: one-time bootstrap of the project database (owner + modules + .env). Run before describe_schema/provision_blueprint. Skips when the existing binding is live under the signed-in account; reprovision: true replaces a dead or foreign-account binding (archives old keys, never deletes the old db). Gated.',
|
|
24
24
|
parameters: ProvisionDatabaseZod,
|
|
25
25
|
async execute(params, ctx) {
|
package/tools/run-codegen.js
CHANGED
|
@@ -16,7 +16,6 @@ const host_1 = require("../host");
|
|
|
16
16
|
const env_file_1 = require("../provision-database/env-file");
|
|
17
17
|
const barrels_1 = require("../run-codegen/barrels");
|
|
18
18
|
const endpoints_1 = require("../run-codegen/endpoints");
|
|
19
|
-
const DEFAULT_API_ENDPOINT = 'http://api.localhost:3000/graphql';
|
|
20
19
|
const RunCodegenZod = zod_1.z.object({});
|
|
21
20
|
function fail(message, extra) {
|
|
22
21
|
return {
|
|
@@ -136,8 +135,10 @@ function createRunCodegenTool(exec = app_workspace_1.run) {
|
|
|
136
135
|
if (!dbName) {
|
|
137
136
|
return fail('DATABASE_NAME missing from .env — provision the database first, then re-run.');
|
|
138
137
|
}
|
|
139
|
-
const
|
|
140
|
-
const apiEndpoint = envApiEndpoint ||
|
|
138
|
+
const host = (0, host_1.getHost)();
|
|
139
|
+
const apiEndpoint = envApiEndpoint || host.backendConfig()?.apiEndpoint;
|
|
140
|
+
if (!apiEndpoint)
|
|
141
|
+
return fail((0, host_1.noBackendReason)(host.signInHint));
|
|
141
142
|
const planes = (0, endpoints_1.derivePlaneEndpoints)(apiEndpoint, dbName);
|
|
142
143
|
// The dev server reads .env.local (NEXT_PUBLIC_DB_NAME + per-DB endpoint
|
|
143
144
|
// overrides so it doesn't fall back to localhost vhosts on a remote
|
|
@@ -192,9 +193,8 @@ function createRunCodegenTool(exec = app_workspace_1.run) {
|
|
|
192
193
|
return fail('codegen failed: BACKEND OUTAGE — the Constructive backend reset the connection ' +
|
|
193
194
|
'during schema introspection, and it stayed down across every internal retry. ' +
|
|
194
195
|
'This is an EXTERNAL outage, not a code or install problem; re-running run_codegen ' +
|
|
195
|
-
'will NOT fix it.
|
|
196
|
-
'
|
|
197
|
-
'build-orchestrator. Do NOT loop on run_codegen (THRASH-001).\n\n' +
|
|
196
|
+
'will NOT fix it. Check backend reachability once, then report the blocker and wait ' +
|
|
197
|
+
'for the backend to recover. Do NOT loop on run_codegen (THRASH-001).\n\n' +
|
|
198
198
|
codegen.out, stepDetails());
|
|
199
199
|
}
|
|
200
200
|
return fail(`codegen failed:\n${codegen.out}`, stepDetails());
|