@tera-system/core 0.1.4 → 0.1.5

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/MANIFEST.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tera-system/core",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "Build manifest for the Tera System OpenCode Plugin component.",
5
5
  "compatibleEnvironments": [
6
6
  "opencode"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tera-system/core",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "Tera System OpenCode Plugin — governance core, 20 agents, 10 commands, distribution tools",
5
5
  "license": "SEE LICENSE IN LICENSE.md",
6
6
  "type": "module",
@@ -213,23 +213,135 @@ fs.mkdirSync(path.dirname(manifestPath), { recursive: true });
213
213
  fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2));
214
214
 
215
215
  /* ------------------- license state (non-blocking, never breaks install) ------------------- */
216
- try {
217
- const stateFile = path.join(target, ".tera", "license.state.json");
218
- let licenseKey = (readLicenseState(stateFile) || {}).licenseKey;
219
- if (!licenseKey) licenseKey = process.env.TERA_LICENSE_KEY || undefined;
216
+
217
+ /**
218
+ * License verification with API-first strategy + offline fallback.
219
+ *
220
+ * Flow:
221
+ * 1. Try online API verification (if API URL is configured)
222
+ * 2. On API failure → fallback to offline Ed25519 verification
223
+ * 3. Never blocks install — always continues
224
+ *
225
+ * Environment variables:
226
+ * TERA_LICENSE_API — API base URL (e.g. https://teranoo.com/api/license)
227
+ * TERA_LICENSE_KEY — Manual license key override
228
+ */
229
+ const LICENSE_API_URL = process.env.TERA_LICENSE_API || "https://teranoo.com/api/license";
230
+ const LICENSE_TIMEOUT_MS = 5000; // 5 seconds max for API call
231
+
232
+ /**
233
+ * Verify license via remote API (online).
234
+ * Returns: { ok: boolean, state: string, reason: string, expiresAt?: string, source: "api" }
235
+ */
236
+ async function verifyViaAPI(licenseKey) {
237
+ if (!licenseKey) return { ok: false, state: "Unlicensed", reason: "no-key", source: "api" };
238
+
239
+ try {
240
+ const controller = new AbortController();
241
+ const timeout = setTimeout(() => controller.abort(), LICENSE_TIMEOUT_MS);
242
+
243
+ const res = await fetch(`${LICENSE_API_URL}/verify`, {
244
+ method: "POST",
245
+ headers: { "Content-Type": "application/json" },
246
+ body: JSON.stringify({ licenseKey }),
247
+ signal: controller.signal,
248
+ });
249
+ clearTimeout(timeout);
250
+
251
+ if (!res.ok) {
252
+ return { ok: false, state: "Invalid", reason: `api-http-${res.status}`, source: "api" };
253
+ }
254
+
255
+ const data = await res.json();
256
+ return {
257
+ ok: data.valid === true,
258
+ state: data.state || "Invalid",
259
+ reason: data.reason || "api-response",
260
+ expiresAt: data.expiresAt || null,
261
+ source: "api",
262
+ };
263
+ } catch (err) {
264
+ // Network error, timeout, abort — fall through to offline
265
+ return { ok: false, state: "Unknown", reason: `api-error: ${err.message}`, source: "api-fallback" };
266
+ }
267
+ }
268
+
269
+ /**
270
+ * Verify license offline (Ed25519 signature check).
271
+ * Returns: { ok: boolean, state: string, reason: string, expiresAt?: string, source: "offline" }
272
+ */
273
+ function verifyOffline(licenseKey) {
220
274
  const state = assessLicenseState(licenseKey);
221
- writeLicenseState(stateFile, {
275
+ return {
276
+ ok: state.state === "Active" || state.state === "Grace",
222
277
  state: state.state,
223
278
  reason: state.reason,
224
- licenseKey: licenseKey || null,
225
279
  expiresAt: state.expiresAt || null,
226
- checkedAt: new Date().toISOString(),
227
- });
228
- console.log(` [license ] state=${state.state} (${state.reason})`);
229
- } catch (err) {
230
- console.log(` [license ] skipped (${err.message}) — install continues`);
280
+ graceUntil: state.graceUntil || null,
281
+ source: "offline",
282
+ };
231
283
  }
232
284
 
285
+ /**
286
+ * Combined verification: API-first, offline fallback.
287
+ * Always returns a result — never throws.
288
+ */
289
+ async function verifyLicense(licenseKey) {
290
+ // Step 1: Try API (unless explicitly disabled)
291
+ if (process.env.TERA_OFFLINE_ONLY === "1") {
292
+ return verifyOffline(licenseKey);
293
+ }
294
+
295
+ const apiResult = await verifyViaAPI(licenseKey);
296
+
297
+ // If API succeeded and gave a definitive answer, use it
298
+ if (apiResult.source === "api" && apiResult.ok !== undefined) {
299
+ return apiResult;
300
+ }
301
+
302
+ // Step 2: Fallback to offline
303
+ const offlineResult = verifyOffline(licenseKey);
304
+
305
+ // If offline confirms API's failure, use offline (more trusted)
306
+ if (!apiResult.ok && !offlineResult.ok) {
307
+ return offlineResult;
308
+ }
309
+
310
+ // If API failed but offline says active, trust offline (API might be down)
311
+ if (!apiResult.ok && offlineResult.ok) {
312
+ return { ...offlineResult, reason: `offline-verified (api-unavailable: ${apiResult.reason})` };
313
+ }
314
+
315
+ // Default: trust API
316
+ return apiResult;
317
+ }
318
+
319
+ // Run license check (async wrapper, non-blocking)
320
+ (async () => {
321
+ try {
322
+ const stateFile = path.join(target, ".tera", "license.state.json");
323
+ let licenseKey = (readLicenseState(stateFile) || {}).licenseKey;
324
+ if (!licenseKey) licenseKey = process.env.TERA_LICENSE_KEY || undefined;
325
+
326
+ const result = await verifyLicense(licenseKey);
327
+
328
+ writeLicenseState(stateFile, {
329
+ state: result.state,
330
+ reason: result.reason,
331
+ licenseKey: licenseKey || null,
332
+ expiresAt: result.expiresAt || null,
333
+ graceUntil: result.graceUntil || null,
334
+ source: result.source,
335
+ checkedAt: new Date().toISOString(),
336
+ });
337
+
338
+ const sourceTag = result.source === "api" ? "☁️" : result.source === "offline" ? "🔒" : "🔄";
339
+ console.log(` [license ] ${sourceTag} state=${result.state} (${result.reason})`);
340
+ } catch (err) {
341
+ console.log(` [license ] skipped (${err.message}) — install continues`);
342
+ }
343
+ })();
344
+
233
345
  console.log("\n summary:");
234
346
  console.log(` installed : ${stats.installed}`);
235
347
  console.log(` updated : ${stats.updated}`);