@haaaiawd/second-nature 0.2.9 → 0.2.13

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 (115) hide show
  1. package/index.js +102 -6
  2. package/openclaw.plugin.json +2 -5
  3. package/package.json +1 -1
  4. package/runtime/cli/commands/index.js +85 -11
  5. package/runtime/cli/host-capability/host-discovery-port.d.ts +85 -0
  6. package/runtime/cli/host-capability/host-discovery-port.js +137 -0
  7. package/runtime/cli/host-capability/probe-host-capability.js +1 -1
  8. package/runtime/cli/host-capability/types.d.ts +2 -7
  9. package/runtime/cli/host-capability/types.js +0 -5
  10. package/runtime/cli/ops/heartbeat-surface.d.ts +5 -3
  11. package/runtime/cli/ops/heartbeat-surface.js +38 -8
  12. package/runtime/cli/ops/ops-router.d.ts +6 -2
  13. package/runtime/cli/ops/ops-router.js +1275 -1147
  14. package/runtime/cli/ops/workspace-heartbeat-runner.js +2 -5
  15. package/runtime/connectors/base/normalized-evidence-content.d.ts +4 -0
  16. package/runtime/connectors/base/normalized-evidence-content.js +21 -2
  17. package/runtime/connectors/evidence-normalizer.js +32 -1
  18. package/runtime/connectors/manifest/manifest-schema.d.ts +2 -2
  19. package/runtime/connectors/registry/dynamic-connector-registry.js +16 -1
  20. package/runtime/connectors/services/connector-executor-adapter.js +54 -35
  21. package/runtime/core/second-nature/action/action-closure-recorder.d.ts +4 -0
  22. package/runtime/core/second-nature/action/action-closure-recorder.js +51 -38
  23. package/runtime/core/second-nature/action/action-proposal-builder.js +8 -34
  24. package/runtime/core/second-nature/action/policy-bound-dispatch.d.ts +2 -0
  25. package/runtime/core/second-nature/action/policy-bound-dispatch.js +10 -5
  26. package/runtime/core/second-nature/control-plane/accepted-projection-loader.js +1 -11
  27. package/runtime/core/second-nature/control-plane/cycle-finalizer.d.ts +82 -0
  28. package/runtime/core/second-nature/control-plane/cycle-finalizer.js +187 -0
  29. package/runtime/core/second-nature/control-plane/heartbeat-orchestrator.d.ts +1 -0
  30. package/runtime/core/second-nature/control-plane/heartbeat-orchestrator.js +23 -15
  31. package/runtime/core/second-nature/control-plane/real-runtime-spine.d.ts +2 -0
  32. package/runtime/core/second-nature/control-plane/real-runtime-spine.js +2 -1
  33. package/runtime/core/second-nature/guidance/guidance-proposal-consumer.d.ts +2 -1
  34. package/runtime/core/second-nature/guidance/guidance-proposal-consumer.js +4 -2
  35. package/runtime/core/second-nature/heartbeat/heartbeat-loop.js +4 -3
  36. package/runtime/core/second-nature/heartbeat/runtime-snapshot.d.ts +3 -2
  37. package/runtime/core/second-nature/heartbeat/snapshot-builder.d.ts +3 -2
  38. package/runtime/core/second-nature/orchestrator/intent-planner.js +4 -2
  39. package/runtime/core/second-nature/orchestrator/narrative-update.js +1 -2
  40. package/runtime/core/second-nature/orchestrator/platform-capability-router.d.ts +2 -2
  41. package/runtime/core/second-nature/orchestrator/platform-capability-router.js +1 -1
  42. package/runtime/core/second-nature/outreach/build-outreach-draft-request.js +2 -3
  43. package/runtime/core/second-nature/outreach/dispatch-user-outreach.d.ts +2 -2
  44. package/runtime/core/second-nature/outreach/dispatch-user-outreach.js +6 -1
  45. package/runtime/core/second-nature/outreach/judge-input-from-snapshot.js +3 -14
  46. package/runtime/core/second-nature/outreach/judge-outreach.d.ts +6 -5
  47. package/runtime/core/second-nature/perception/judgment-engine.js +10 -16
  48. package/runtime/core/second-nature/perception/perception-builder.js +15 -11
  49. package/runtime/core/second-nature/quiet/run-source-backed-quiet.js +13 -15
  50. package/runtime/core/second-nature/quiet-dream/daily-rhythm-scheduler.js +40 -16
  51. package/runtime/core/second-nature/quiet-dream/dream-consolidation-runner.d.ts +5 -1
  52. package/runtime/core/second-nature/quiet-dream/dream-consolidation-runner.js +68 -29
  53. package/runtime/core/second-nature/quiet-dream/dream-scheduler.js +2 -3
  54. package/runtime/core/second-nature/quiet-dream/memory-projection-lifecycle.js +2 -13
  55. package/runtime/core/second-nature/quiet-dream/quiet-daily-review-builder.d.ts +1 -0
  56. package/runtime/core/second-nature/quiet-dream/quiet-daily-review-builder.js +34 -11
  57. package/runtime/core/second-nature/types.d.ts +2 -9
  58. package/runtime/dream/dream-engine.js +11 -4
  59. package/runtime/guidance/outreach-draft-schema.d.ts +12 -12
  60. package/runtime/guidance/persona-selection.js +5 -0
  61. package/runtime/guidance/template-registry.js +6 -1
  62. package/runtime/guidance/types.d.ts +2 -2
  63. package/runtime/observability/causal-loop-health.d.ts +2 -1
  64. package/runtime/observability/causal-loop-health.js +7 -0
  65. package/runtime/observability/living-loop-health-gate.js +2 -8
  66. package/runtime/observability/loop-stage-event-sink.js +6 -2
  67. package/runtime/observability/loop-status.d.ts +2 -0
  68. package/runtime/observability/loop-status.js +14 -1
  69. package/runtime/observability/services/heartbeat-digest-assembler.d.ts +3 -0
  70. package/runtime/observability/services/heartbeat-digest-assembler.js +9 -0
  71. package/runtime/observability/services/lived-experience-audit.d.ts +7 -7
  72. package/runtime/shared/degraded-status-classifier.d.ts +16 -0
  73. package/runtime/shared/degraded-status-classifier.js +68 -0
  74. package/runtime/shared/evidence-level-classifier.d.ts +61 -0
  75. package/runtime/shared/evidence-level-classifier.js +116 -0
  76. package/runtime/shared/provenance-tier.d.ts +37 -0
  77. package/runtime/shared/provenance-tier.js +97 -0
  78. package/runtime/shared/serialization.d.ts +17 -0
  79. package/runtime/shared/serialization.js +27 -0
  80. package/runtime/shared/setup-ack.d.ts +54 -0
  81. package/runtime/shared/setup-ack.js +108 -0
  82. package/runtime/shared/source-ref-compat.d.ts +26 -0
  83. package/runtime/shared/source-ref-compat.js +64 -0
  84. package/runtime/shared/types/goal.d.ts +4 -4
  85. package/runtime/shared/types/goal.js +1 -1
  86. package/runtime/shared/types/index.d.ts +1 -0
  87. package/runtime/shared/types/index.js +1 -3
  88. package/runtime/shared/types/source-ref.d.ts +2 -2
  89. package/runtime/shared/types/v7-entities.d.ts +5 -5
  90. package/runtime/shared/types/v7-entities.js +1 -1
  91. package/runtime/shared/types/v8-contracts.d.ts +13 -2
  92. package/runtime/storage/db/index.js +60 -12
  93. package/runtime/storage/db/migrations/index.js +4 -0
  94. package/runtime/storage/db/migrations/v8-004-schema-closure.d.ts +19 -0
  95. package/runtime/storage/db/migrations/v8-004-schema-closure.js +74 -0
  96. package/runtime/storage/db/migrations/v8-005-single-status-schema.d.ts +11 -0
  97. package/runtime/storage/db/migrations/v8-005-single-status-schema.js +16 -0
  98. package/runtime/storage/db/migrations/v8-006-loop-stage-event-proof-trace-columns.d.ts +9 -0
  99. package/runtime/storage/db/migrations/v8-006-loop-stage-event-proof-trace-columns.js +15 -0
  100. package/runtime/storage/db/schema/v8-entities.d.ts +65 -84
  101. package/runtime/storage/db/schema/v8-entities.js +8 -7
  102. package/runtime/storage/delivery/types.d.ts +2 -2
  103. package/runtime/storage/fallback/load-operator-fallback.d.ts +2 -2
  104. package/runtime/storage/fallback/operator-fallback-types.d.ts +2 -2
  105. package/runtime/storage/fallback/operator-fallback-view.d.ts +2 -2
  106. package/runtime/storage/index.d.ts +1 -1
  107. package/runtime/storage/life-evidence/types.d.ts +5 -5
  108. package/runtime/storage/quiet/quiet-artifact-types.d.ts +4 -4
  109. package/runtime/storage/quiet/quiet-artifact-writer.d.ts +2 -2
  110. package/runtime/storage/services/write-validation-gate.d.ts +1 -1
  111. package/runtime/storage/services/write-validation-gate.js +15 -3
  112. package/runtime/storage/snapshots/types.d.ts +8 -8
  113. package/runtime/storage/user-interest/types.d.ts +3 -3
  114. package/runtime/storage/v8-state-stores.d.ts +15 -3
  115. package/runtime/storage/v8-state-stores.js +60 -39
@@ -21,30 +21,16 @@
21
21
  *
22
22
  * Test coverage: tests/unit/storage/v8-state-stores.test.ts
23
23
  */
24
- import { eq, and, desc, like, isNull } from "drizzle-orm";
24
+ import { eq, and, desc, like, isNull, inArray } from "drizzle-orm";
25
25
  import { evidenceItem, perceptionCard, judgmentVerdict, actionClosureRecord, quietDailyReview, dreamConsolidationRun, longTermMemoryProjection, heartbeatCycleTrace, loopStageEvent, impulseContextArtifact, dailyRhythmState, connectorCooldownState, } from "./db/schema/v8-entities.js";
26
+ import { serializeSourceRefs, parseSourceRefs, } from "../shared/serialization.js";
27
+ import { classifyDegradedStatus } from "../shared/degraded-status-classifier.js";
26
28
  // ───────────────────────────────────────────────────────────────
27
29
  // Shared helpers
28
30
  // ───────────────────────────────────────────────────────────────
29
- function serializeSourceRefs(refs) {
30
- return JSON.stringify(refs);
31
- }
32
- function parseSourceRefs(json) {
33
- if (!json)
34
- return [];
35
- try {
36
- const parsed = JSON.parse(json);
37
- if (Array.isArray(parsed))
38
- return parsed;
39
- return [];
40
- }
41
- catch {
42
- return [];
43
- }
44
- }
45
31
  function makeDegraded(reason, ownerStage, operatorNextAction, sourceRefs = []) {
46
32
  return {
47
- status: "degraded",
33
+ status: classifyDegradedStatus(reason),
48
34
  reason,
49
35
  ownerStage,
50
36
  sourceRefs,
@@ -69,8 +55,9 @@ export async function writeEvidenceItem(db, row) {
69
55
  if (!validated.ok)
70
56
  return validated.degraded;
71
57
  try {
58
+ const { sourceRefs: _, ...rest } = row;
72
59
  const record = {
73
- ...row,
60
+ ...rest,
74
61
  sourceRefsJson: serializeSourceRefs(validated.record),
75
62
  };
76
63
  await db.db
@@ -157,7 +144,7 @@ function validatePerceptionCardCanonical(row) {
157
144
  return {
158
145
  ok: false,
159
146
  degraded: {
160
- status: "degraded",
147
+ status: classifyDegradedStatus("perception_contract_drift"),
161
148
  reason: "perception_contract_drift",
162
149
  ownerStage: "perception",
163
150
  sourceRefs: row.sourceRefs,
@@ -172,7 +159,7 @@ function validatePerceptionCardCanonical(row) {
172
159
  return {
173
160
  ok: false,
174
161
  degraded: {
175
- status: "degraded",
162
+ status: classifyDegradedStatus("perception_contract_drift"),
176
163
  reason: "perception_contract_drift",
177
164
  ownerStage: "perception",
178
165
  sourceRefs: row.sourceRefs,
@@ -187,7 +174,7 @@ function validatePerceptionCardCanonical(row) {
187
174
  return {
188
175
  ok: false,
189
176
  degraded: {
190
- status: "degraded",
177
+ status: classifyDegradedStatus("perception_contract_drift"),
191
178
  reason: "perception_contract_drift",
192
179
  ownerStage: "perception",
193
180
  sourceRefs: row.sourceRefs,
@@ -206,8 +193,9 @@ export async function writePerceptionCard(db, row) {
206
193
  if (!canonicalCheck.ok)
207
194
  return canonicalCheck.degraded;
208
195
  try {
196
+ const { sourceRefs: _, ...rest } = row;
209
197
  const record = {
210
- ...row,
198
+ ...rest,
211
199
  sourceRefsJson: serializeSourceRefs(validated.record),
212
200
  };
213
201
  await db.db.insert(perceptionCard).values(record);
@@ -272,8 +260,9 @@ export async function writeJudgmentVerdict(db, row) {
272
260
  if (!validated.ok)
273
261
  return validated.degraded;
274
262
  try {
263
+ const { sourceRefs: _, ...rest } = row;
275
264
  const record = {
276
- ...row,
265
+ ...rest,
277
266
  sourceRefsJson: serializeSourceRefs(validated.record),
278
267
  };
279
268
  await db.db.insert(judgmentVerdict).values(record);
@@ -322,9 +311,13 @@ export async function writeActionClosureRecord(db, row) {
322
311
  if (!validated.ok)
323
312
  return validated.degraded;
324
313
  try {
314
+ const { sourceRefs: _, proofRefs, traceRefs, payload, ...rest } = row;
325
315
  const record = {
326
- ...row,
316
+ ...rest,
327
317
  sourceRefsJson: serializeSourceRefs(validated.record),
318
+ proofRefsJson: serializeSourceRefs(proofRefs ?? []),
319
+ traceRefsJson: serializeSourceRefs(traceRefs ?? []),
320
+ payloadJson: JSON.stringify(payload ?? {}),
328
321
  };
329
322
  await db.db.insert(actionClosureRecord).values(record);
330
323
  return { id: row.id };
@@ -373,10 +366,11 @@ export async function writeQuietDailyReview(db, row) {
373
366
  if (!validated.ok)
374
367
  return validated.degraded;
375
368
  try {
369
+ const { sourceRefs: _, closureRefs, ...rest } = row;
376
370
  const record = {
377
- ...row,
371
+ ...rest,
378
372
  sourceRefsJson: serializeSourceRefs(validated.record),
379
- closureRefsJson: row.closureRefs ? serializeSourceRefs(row.closureRefs) : null,
373
+ closureRefsJson: closureRefs ? serializeSourceRefs(closureRefs) : null,
380
374
  };
381
375
  await db.db.insert(quietDailyReview).values(record);
382
376
  return { id: row.id };
@@ -424,8 +418,9 @@ export async function writeDreamConsolidationRun(db, row) {
424
418
  if (!validated.ok)
425
419
  return validated.degraded;
426
420
  try {
421
+ const { sourceRefs: _, ...rest } = row;
427
422
  const record = {
428
- ...row,
423
+ ...rest,
429
424
  sourceRefsJson: serializeSourceRefs(validated.record),
430
425
  };
431
426
  await db.db.insert(dreamConsolidationRun).values(record);
@@ -444,8 +439,6 @@ export async function updateDreamConsolidationRunStatus(db, id, status, options)
444
439
  const updateData = { status };
445
440
  if (options?.reason !== undefined)
446
441
  updateData.reason = options.reason;
447
- if (options?.lifecycleStatus !== undefined)
448
- updateData.lifecycleStatus = options.lifecycleStatus;
449
442
  if (options?.payloadJson !== undefined)
450
443
  updateData.payloadJson = options.payloadJson;
451
444
  await db.db.update(dreamConsolidationRun).set(updateData).where(eq(dreamConsolidationRun.id, id));
@@ -486,6 +479,26 @@ export async function readDreamConsolidationRunsByQuietId(db, quietReviewId) {
486
479
  };
487
480
  }
488
481
  }
482
+ /**
483
+ * Read the most recent DreamConsolidationRun globally, filtered by status.
484
+ * Used to enforce the 7-day Dream interval across Quiet review IDs.
485
+ */
486
+ export async function readLatestDreamConsolidationRunByStatus(db, statuses) {
487
+ try {
488
+ const rows = await db.db
489
+ .select()
490
+ .from(dreamConsolidationRun)
491
+ .where(inArray(dreamConsolidationRun.status, statuses))
492
+ .orderBy(desc(dreamConsolidationRun.createdAt))
493
+ .limit(1);
494
+ return { row: rows[0] };
495
+ }
496
+ catch {
497
+ return {
498
+ degraded: makeDegraded("state_unreadable", "dream", "Check state database connectivity"),
499
+ };
500
+ }
501
+ }
489
502
  // ───────────────────────────────────────────────────────────────
490
503
  // LongTermMemoryProjection store
491
504
  // ───────────────────────────────────────────────────────────────
@@ -494,8 +507,9 @@ export async function writeLongTermMemoryProjection(db, row) {
494
507
  if (!validated.ok)
495
508
  return validated.degraded;
496
509
  try {
510
+ const { sourceRefs: _, ...rest } = row;
497
511
  const record = {
498
- ...row,
512
+ ...rest,
499
513
  sourceRefsJson: serializeSourceRefs(validated.record),
500
514
  };
501
515
  await db.db.insert(longTermMemoryProjection).values(record);
@@ -511,7 +525,7 @@ export async function writeLongTermMemoryProjection(db, row) {
511
525
  */
512
526
  export async function updateLongTermMemoryProjectionStatus(db, id, status, payloadJson) {
513
527
  try {
514
- const updateData = { status, lifecycleStatus: status };
528
+ const updateData = { status };
515
529
  if (payloadJson !== undefined) {
516
530
  updateData.payloadJson = payloadJson;
517
531
  }
@@ -574,8 +588,9 @@ export async function readLongTermMemoryProjectionById(db, id) {
574
588
  // ───────────────────────────────────────────────────────────────
575
589
  export async function writeHeartbeatCycleTrace(db, row) {
576
590
  try {
591
+ const { sourceRefs: _, ...rest } = row;
577
592
  const record = {
578
- ...row,
593
+ ...rest,
579
594
  sourceRefsJson: row.sourceRefs ? serializeSourceRefs(row.sourceRefs) : "[]",
580
595
  };
581
596
  await db.db.insert(heartbeatCycleTrace).values(record);
@@ -610,15 +625,18 @@ export async function writeLoopStageEvent(db, row) {
610
625
  if (!validated.ok)
611
626
  return validated.degraded;
612
627
  try {
628
+ const { sourceRefs: _s, proofRefs: _p, traceRefs: _t, ...rest } = row;
613
629
  const record = {
614
- ...row,
630
+ ...rest,
615
631
  sourceRefsJson: serializeSourceRefs(validated.record),
632
+ proofRefsJson: serializeSourceRefs(row.proofRefs ?? []),
633
+ traceRefsJson: serializeSourceRefs(row.traceRefs ?? []),
616
634
  };
617
635
  await db.db.insert(loopStageEvent).values(record);
618
636
  return { id: row.id };
619
637
  }
620
- catch {
621
- return makeDegraded("state_unreadable", stage, "Retry stage event write after DB recovery", validated.record);
638
+ catch (err) {
639
+ return makeDegraded("state_unreadable", stage, `Retry stage event write after DB recovery: ${err instanceof Error ? err.message : String(err)}`, validated.record);
622
640
  }
623
641
  }
624
642
  export async function readLoopStageEventsByCycle(db, cycleId) {
@@ -662,8 +680,9 @@ export async function writeImpulseContextArtifact(db, row) {
662
680
  if (!validated.ok)
663
681
  return validated.degraded;
664
682
  try {
683
+ const { sourceRefs: _, ...rest } = row;
665
684
  const record = {
666
- ...row,
685
+ ...rest,
667
686
  sourceRefsJson: serializeSourceRefs(validated.record),
668
687
  };
669
688
  // Upsert: delete existing then insert (SQLite primary-key conflict)
@@ -712,8 +731,9 @@ export async function writeDailyRhythmState(db, row) {
712
731
  if (!validated.ok)
713
732
  return validated.degraded;
714
733
  try {
734
+ const { sourceRefs: _, ...rest } = row;
715
735
  const record = {
716
- ...row,
736
+ ...rest,
717
737
  sourceRefsJson: serializeSourceRefs(validated.record),
718
738
  };
719
739
  await db.db.delete(dailyRhythmState).where(eq(dailyRhythmState.id, row.id));
@@ -761,8 +781,9 @@ export async function writeConnectorCooldownState(db, row) {
761
781
  if (!validated.ok)
762
782
  return validated.degraded;
763
783
  try {
784
+ const { sourceRefs: _, ...rest } = row;
764
785
  const record = {
765
- ...row,
786
+ ...rest,
766
787
  sourceRefsJson: serializeSourceRefs(validated.record),
767
788
  };
768
789
  await db.db.delete(connectorCooldownState).where(eq(connectorCooldownState.id, row.id));