create-principles-disciple 1.97.0 → 1.98.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/console/dist/server/models/ActivationsConsoleModel.js +29 -3
  2. package/console/dist/server/models/ApprovalsConsoleModel.d.ts +13 -0
  3. package/console/dist/server/models/ApprovalsConsoleModel.js +94 -3
  4. package/console/dist/web/assets/app.js +9 -5
  5. package/console/package.json +3 -3
  6. package/core/dist/runtime-v2/__tests__/architecture-regression.test.js +5 -4
  7. package/core/dist/runtime-v2/__tests__/architecture-regression.test.js.map +1 -1
  8. package/core/dist/runtime-v2/__tests__/dreamer-runner.test.js +15 -2
  9. package/core/dist/runtime-v2/__tests__/dreamer-runner.test.js.map +1 -1
  10. package/core/dist/runtime-v2/__tests__/evaluator-runner-vslice-v2.test.js +44 -0
  11. package/core/dist/runtime-v2/__tests__/evaluator-runner-vslice-v2.test.js.map +1 -1
  12. package/core/dist/runtime-v2/activation/__tests__/activation-re-dispatch.test.d.ts +2 -0
  13. package/core/dist/runtime-v2/activation/__tests__/activation-re-dispatch.test.d.ts.map +1 -0
  14. package/core/dist/runtime-v2/activation/__tests__/activation-re-dispatch.test.js +149 -0
  15. package/core/dist/runtime-v2/activation/__tests__/activation-re-dispatch.test.js.map +1 -0
  16. package/core/dist/runtime-v2/activation/__tests__/sqlite-activation-state-store.test.js +72 -2
  17. package/core/dist/runtime-v2/activation/__tests__/sqlite-activation-state-store.test.js.map +1 -1
  18. package/core/dist/runtime-v2/activation/activation-dispatcher.d.ts.map +1 -1
  19. package/core/dist/runtime-v2/activation/activation-dispatcher.js +5 -0
  20. package/core/dist/runtime-v2/activation/activation-dispatcher.js.map +1 -1
  21. package/core/dist/runtime-v2/activation/activation-types.d.ts +1 -0
  22. package/core/dist/runtime-v2/activation/activation-types.d.ts.map +1 -1
  23. package/core/dist/runtime-v2/activation/activation-types.js.map +1 -1
  24. package/core/dist/runtime-v2/activation/memory-activation-state-store.d.ts.map +1 -1
  25. package/core/dist/runtime-v2/activation/memory-activation-state-store.js +6 -1
  26. package/core/dist/runtime-v2/activation/memory-activation-state-store.js.map +1 -1
  27. package/core/dist/runtime-v2/activation/sqlite-activation-state-store.d.ts +1 -0
  28. package/core/dist/runtime-v2/activation/sqlite-activation-state-store.d.ts.map +1 -1
  29. package/core/dist/runtime-v2/activation/sqlite-activation-state-store.js +64 -10
  30. package/core/dist/runtime-v2/activation/sqlite-activation-state-store.js.map +1 -1
  31. package/core/dist/runtime-v2/activation/writers/__tests__/rule-host-writer.test.js +11 -8
  32. package/core/dist/runtime-v2/activation/writers/__tests__/rule-host-writer.test.js.map +1 -1
  33. package/core/dist/runtime-v2/activation/writers/rule-host-writer.d.ts.map +1 -1
  34. package/core/dist/runtime-v2/activation/writers/rule-host-writer.js +8 -1
  35. package/core/dist/runtime-v2/activation/writers/rule-host-writer.js.map +1 -1
  36. package/core/dist/runtime-v2/adapter/__tests__/principle-tree-ledger-adapter.test.d.ts +2 -0
  37. package/core/dist/runtime-v2/adapter/__tests__/principle-tree-ledger-adapter.test.d.ts.map +1 -0
  38. package/core/dist/runtime-v2/adapter/__tests__/principle-tree-ledger-adapter.test.js +134 -0
  39. package/core/dist/runtime-v2/adapter/__tests__/principle-tree-ledger-adapter.test.js.map +1 -0
  40. package/core/dist/runtime-v2/adapter/principle-tree-ledger-adapter.d.ts +15 -0
  41. package/core/dist/runtime-v2/adapter/principle-tree-ledger-adapter.d.ts.map +1 -1
  42. package/core/dist/runtime-v2/adapter/principle-tree-ledger-adapter.js +23 -1
  43. package/core/dist/runtime-v2/adapter/principle-tree-ledger-adapter.js.map +1 -1
  44. package/core/dist/runtime-v2/index.d.ts +1 -1
  45. package/core/dist/runtime-v2/index.d.ts.map +1 -1
  46. package/core/dist/runtime-v2/index.js +1 -1
  47. package/core/dist/runtime-v2/index.js.map +1 -1
  48. package/core/dist/runtime-v2/internalization/__tests__/split-diagnostician-runner-retry.test.js +113 -0
  49. package/core/dist/runtime-v2/internalization/__tests__/split-diagnostician-runner-retry.test.js.map +1 -1
  50. package/core/dist/runtime-v2/internalization/dreamer-runner.d.ts.map +1 -1
  51. package/core/dist/runtime-v2/internalization/dreamer-runner.js +4 -0
  52. package/core/dist/runtime-v2/internalization/dreamer-runner.js.map +1 -1
  53. package/core/dist/runtime-v2/internalization/evaluator-runner.d.ts.map +1 -1
  54. package/core/dist/runtime-v2/internalization/evaluator-runner.js +3 -1
  55. package/core/dist/runtime-v2/internalization/evaluator-runner.js.map +1 -1
  56. package/core/dist/runtime-v2/internalization/split-diagnostician-runner.d.ts.map +1 -1
  57. package/core/dist/runtime-v2/internalization/split-diagnostician-runner.js +27 -0
  58. package/core/dist/runtime-v2/internalization/split-diagnostician-runner.js.map +1 -1
  59. package/core/dist/runtime-v2/internalization-chain-integrity-read-model.d.ts.map +1 -1
  60. package/core/dist/runtime-v2/internalization-chain-integrity-read-model.js +4 -1
  61. package/core/dist/runtime-v2/internalization-chain-integrity-read-model.js.map +1 -1
  62. package/core/dist/runtime-v2/proven-channel-baseline.js +1 -1
  63. package/core/dist/runtime-v2/proven-channel-baseline.js.map +1 -1
  64. package/core/dist/runtime-v2/store/sqlite-connection.d.ts.map +1 -1
  65. package/core/dist/runtime-v2/store/sqlite-connection.js +4 -0
  66. package/core/dist/runtime-v2/store/sqlite-connection.js.map +1 -1
  67. package/core/dist/runtime-v2/types/event-types.d.ts +4 -0
  68. package/core/dist/runtime-v2/types/event-types.d.ts.map +1 -1
  69. package/core/dist/runtime-v2/types/event-types.js +5 -0
  70. package/core/dist/runtime-v2/types/event-types.js.map +1 -1
  71. package/core/package.json +1 -1
  72. package/dist/mvp-config.js +1 -1
  73. package/dist/mvp-config.js.map +1 -1
  74. package/package.json +3 -3
  75. package/pd-cli/dist/commands/diagnose.d.ts.map +1 -1
  76. package/pd-cli/dist/commands/diagnose.js +71 -4
  77. package/pd-cli/dist/commands/diagnose.js.map +1 -1
  78. package/pd-cli/dist/commands/pain-record.d.ts.map +1 -1
  79. package/pd-cli/dist/commands/pain-record.js +21 -0
  80. package/pd-cli/dist/commands/pain-record.js.map +1 -1
  81. package/pd-cli/dist/commands/runtime-activation.d.ts +26 -0
  82. package/pd-cli/dist/commands/runtime-activation.d.ts.map +1 -1
  83. package/pd-cli/dist/commands/runtime-activation.js +307 -3
  84. package/pd-cli/dist/commands/runtime-activation.js.map +1 -1
  85. package/pd-cli/dist/config-reader.js +1 -1
  86. package/pd-cli/dist/config-reader.js.map +1 -1
  87. package/pd-cli/dist/index.js +22 -1
  88. package/pd-cli/dist/index.js.map +1 -1
  89. package/pd-cli/dist/services/pd-config-loader.js +1 -1
  90. package/pd-cli/dist/services/pd-config-loader.js.map +1 -1
  91. package/pd-cli/package.json +1 -1
  92. package/plugin/dist/core/pd-config-loader.js +1 -1
  93. package/plugin/dist/core/rule-host.d.ts +22 -1
  94. package/plugin/dist/core/rule-host.js +107 -18
  95. package/plugin/dist/core/workflow-funnel-loader.js +2 -2
  96. package/plugin/dist/core/workspace-context.d.ts +4 -0
  97. package/plugin/dist/core/workspace-context.js +13 -0
  98. package/plugin/dist/hooks/gate.js +36 -50
  99. package/plugin/package.json +2 -2
@@ -1,4 +1,4 @@
1
- import { SqliteConnection, SqliteActivationStateStore, SqlitePIArtifactStore, } from '@principles/core/runtime-v2';
1
+ import { SqliteConnection, SqliteActivationStateStore, SqlitePIArtifactStore, extractPrincipleId, } from '@principles/core/runtime-v2';
2
2
  import * as fs from 'node:fs';
3
3
  import * as path from 'node:path';
4
4
  function isMissingTableError(err) {
@@ -6,6 +6,26 @@ function isMissingTableError(err) {
6
6
  return false;
7
7
  return err.message.includes('no such table');
8
8
  }
9
+ /**
10
+ * Bug-O L2 fix: adapt PIArtifactRecord (DB row) to PIArtifactSnapshot (activation
11
+ * contract type). The two interfaces have identical fields but different names —
12
+ * we use explicit field copy instead of `as` to comply with rc-2-no-as-bypass
13
+ * and to surface future field drift as a compile error.
14
+ */
15
+ function toSnapshot(record) {
16
+ return {
17
+ artifactId: record.artifactId,
18
+ artifactKind: record.artifactKind,
19
+ sourceTaskId: record.sourceTaskId,
20
+ sourcePrincipleId: record.sourcePrincipleId,
21
+ sourceRuleId: record.sourceRuleId,
22
+ lineageArtifactIds: record.lineageArtifactIds,
23
+ validationStatus: record.validationStatus,
24
+ contentJson: record.contentJson,
25
+ createdAt: record.createdAt,
26
+ updatedAt: record.updatedAt,
27
+ };
28
+ }
9
29
  export class ActivationsConsoleModel {
10
30
  workspaceDir;
11
31
  constructor(workspaceDir) {
@@ -30,13 +50,19 @@ export class ActivationsConsoleModel {
30
50
  }
31
51
  throw err;
32
52
  }
33
- // Build artifactId → sourcePrincipleId map from PIArtifactSnapshot
53
+ // Build artifactId → principleId map. Bug-O L2 fix: use extractPrincipleId
54
+ // (4-step fallback: column → parsed.principleId → parsed.sourcePrincipleId
55
+ // → parsed.principleDraft.title) instead of only reading the column.
56
+ // Without this, dreamer artifacts whose sourcePrincipleId was stripped
57
+ // (non-core-principle case) would show 'unlinked' even when contentJson
58
+ // carries a resolvable principleId.
34
59
  const artifactPrincipleMap = new Map();
35
60
  for (const activation of allActivations) {
36
61
  if (!artifactPrincipleMap.has(activation.artifactId)) {
37
62
  try {
38
63
  const artifact = await artifactStore.getArtifactById(activation.artifactId);
39
- artifactPrincipleMap.set(activation.artifactId, artifact?.sourcePrincipleId ?? null);
64
+ const principleId = artifact ? extractPrincipleId(toSnapshot(artifact)) : null;
65
+ artifactPrincipleMap.set(activation.artifactId, principleId);
40
66
  }
41
67
  catch (err) {
42
68
  if (isMissingTableError(err)) {
@@ -38,6 +38,19 @@ export declare class ApprovalsConsoleModel {
38
38
  isMvpProven: boolean;
39
39
  }) | null>;
40
40
  approve(approvalId: string, decidedBy: string, note?: string): Promise<ApproveWithActivationResult>;
41
+ /**
42
+ * Bug-O L3b fix: upgrade the ledger principle linked to `artifactId` from
43
+ * 'candidate' to 'active'. Called by {@link approve} after a successful
44
+ * activation. Non-fatal on failure — the activation is already committed;
45
+ * ledger failure is surfaced as a warning string (rc-9).
46
+ *
47
+ * The principleId is resolved via {@link extractPrincipleId}'s 4-step
48
+ * fallback (column → parsed.principleId → parsed.sourcePrincipleId →
49
+ * parsed.principleDraft.title) so dreamer artifacts whose
50
+ * sourcePrincipleId column was stripped by stripFabricatedCorePrincipleIds
51
+ * can still be linked via contentJson.
52
+ */
53
+ private upgradeLedgerPrinciple;
41
54
  /**
42
55
  * Edit a pending approval's artifact to a new version (P1 #2 fix).
43
56
  *
@@ -1,6 +1,6 @@
1
1
  import * as fs from 'node:fs';
2
2
  import * as path from 'node:path';
3
- import { SqliteConnection, SqliteApprovalQueueStore, SqliteActivationStateStore, SqlitePIArtifactStore, ActivationDispatcher, PromptWriter, DeferArchiveWriter, RuleHostWriter, createProductionGateDeps, ApprovalCompletionService, ApprovalQueue, mapConfidenceToLabel, isArtifactRevisionOf, MVP_CHANNELS, } from '@principles/core/runtime-v2';
3
+ import { SqliteConnection, SqliteApprovalQueueStore, SqliteActivationStateStore, SqlitePIArtifactStore, ActivationDispatcher, PromptWriter, DeferArchiveWriter, RuleHostWriter, createProductionGateDeps, ApprovalCompletionService, ApprovalQueue, mapConfidenceToLabel, isArtifactRevisionOf, extractPrincipleId, PrincipleTreeLedgerAdapter, MVP_CHANNELS, } from '@principles/core/runtime-v2';
4
4
  import { loadPdConfig, computeFlagsFromLoadResult } from '../config/pd-config-store.js';
5
5
  const MVP_PROVEN_CHANNELS = new Set(MVP_CHANNELS);
6
6
  const EMPTY_STATS = { pending: 0, approved: 0, rejected: 0, cancelled: 0 };
@@ -12,6 +12,26 @@ function isMissingTableError(err) {
12
12
  function isActivationSuccess(activation) {
13
13
  return activation.decision === 'activated' || activation.decision === 'already_activated';
14
14
  }
15
+ /**
16
+ * Bug-O L3b fix: adapt a PIArtifactRecord (DB row) to a PIArtifactSnapshot
17
+ * (activation contract type). The two interfaces have identical fields but
18
+ * different names — explicit field copy surfaces future field drift as a
19
+ * compile error and complies with rc-2-no-as-bypass.
20
+ */
21
+ function toArtifactSnapshot(record) {
22
+ return {
23
+ artifactId: record.artifactId,
24
+ artifactKind: record.artifactKind,
25
+ sourceTaskId: record.sourceTaskId,
26
+ sourcePrincipleId: record.sourcePrincipleId,
27
+ sourceRuleId: record.sourceRuleId,
28
+ lineageArtifactIds: record.lineageArtifactIds,
29
+ validationStatus: record.validationStatus,
30
+ contentJson: record.contentJson,
31
+ createdAt: record.createdAt,
32
+ updatedAt: record.updatedAt,
33
+ };
34
+ }
15
35
  function stateDbExists(workspaceDir) {
16
36
  return fs.existsSync(path.join(workspaceDir, '.pd', 'state.db'));
17
37
  }
@@ -133,7 +153,75 @@ export class ApprovalsConsoleModel {
133
153
  warning: 'activation_skipped_feature_flag_disabled: approval is recorded but activation was not dispatched. Enable story_a_approval_completion flag or manually run "pd runtime activation dispatch" to activate.',
134
154
  };
135
155
  }
136
- return { ok: true, record: approvalResult.record, activation };
156
+ // Bug-O L3b fix: after a successful activation, upgrade the corresponding
157
+ // ledger principle from 'candidate' to 'active'. The activation is already
158
+ // committed to SQLite; a ledger failure here is non-fatal and surfaced as
159
+ // a warning (rc-9-no-silent-fallback) — the owner is informed that the
160
+ // ledger state may be out of sync and can be repaired separately.
161
+ //
162
+ // CodeRabbit review fix: use approvalResult.record.artifactId (post-approve
163
+ // source of truth) instead of the pre-read `existing.artifactId`. If
164
+ // editApproval() changed the artifact pointer between read and write, the
165
+ // pre-read value would bind the ledger upgrade to the wrong artifact while
166
+ // the returned record points to the new one.
167
+ let ledgerWarning;
168
+ if (isActivationSuccess(activation)) {
169
+ ledgerWarning = await this.upgradeLedgerPrinciple(approvalResult.record.artifactId);
170
+ }
171
+ return { ok: true, record: approvalResult.record, activation, warning: ledgerWarning };
172
+ }
173
+ /**
174
+ * Bug-O L3b fix: upgrade the ledger principle linked to `artifactId` from
175
+ * 'candidate' to 'active'. Called by {@link approve} after a successful
176
+ * activation. Non-fatal on failure — the activation is already committed;
177
+ * ledger failure is surfaced as a warning string (rc-9).
178
+ *
179
+ * The principleId is resolved via {@link extractPrincipleId}'s 4-step
180
+ * fallback (column → parsed.principleId → parsed.sourcePrincipleId →
181
+ * parsed.principleDraft.title) so dreamer artifacts whose
182
+ * sourcePrincipleId column was stripped by stripFabricatedCorePrincipleIds
183
+ * can still be linked via contentJson.
184
+ */
185
+ async upgradeLedgerPrinciple(artifactId) {
186
+ const stateDir = path.join(this.workspaceDir, '.state');
187
+ const { connection } = this.createReadContext();
188
+ try {
189
+ const piArtifactStore = new SqlitePIArtifactStore(connection);
190
+ const artifact = await piArtifactStore.getArtifactById(artifactId);
191
+ if (!artifact) {
192
+ // rc-9: surface the reason instead of silently returning.
193
+ return `ledger_activate_skipped: artifact ${artifactId} not found in artifact store`;
194
+ }
195
+ const principleId = extractPrincipleId(toArtifactSnapshot(artifact));
196
+ if (!principleId) {
197
+ // No principleId resolvable from any source — this is a legitimate
198
+ // skip (e.g. rule-only artifact), not an error. Surface it so the
199
+ // owner can verify whether the link was supposed to exist.
200
+ return `ledger_activate_skipped: artifact ${artifactId} has no resolvable principleId`;
201
+ }
202
+ const ledger = new PrincipleTreeLedgerAdapter({ stateDir });
203
+ const result = ledger.activatePrinciple(principleId);
204
+ if (!result.ok) {
205
+ // Reason is already prefixed with `ledger_activate_failed:`.
206
+ return result.reason;
207
+ }
208
+ return undefined;
209
+ }
210
+ catch (err) {
211
+ // CodeRabbit review fix: read-side failures (getArtifactById, missing
212
+ // table, connection errors) must NOT propagate upward and fail the
213
+ // approval flow after activation is already committed. The activation
214
+ // succeeded; ledger upgrade is a non-fatal post-step. Surface the error
215
+ // as a warning so the owner can repair the ledger separately (rc-9).
216
+ const message = err instanceof Error ? err.message : String(err);
217
+ return `ledger_activate_failed: ${message}`;
218
+ }
219
+ finally {
220
+ try {
221
+ connection.close();
222
+ }
223
+ catch { /* best-effort */ }
224
+ }
137
225
  }
138
226
  /**
139
227
  * Edit a pending approval's artifact to a new version (P1 #2 fix).
@@ -254,7 +342,10 @@ export class ApprovalsConsoleModel {
254
342
  const dispatcher = new ActivationDispatcher(artifactReadModel, activationStateStore, {
255
343
  writers: [
256
344
  new PromptWriter(),
257
- new RuleHostWriter({ gateDeps: createProductionGateDeps() }),
345
+ new RuleHostWriter({
346
+ gateDeps: createProductionGateDeps(),
347
+ featureFlagProbe: (flagId) => pdFlags.flags[flagId]?.enabled === true,
348
+ }),
258
349
  new DeferArchiveWriter(),
259
350
  ],
260
351
  approvalQueueStore,