@haaaiawd/second-nature 0.2.12 → 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 (59) hide show
  1. package/index.js +96 -6
  2. package/openclaw.plugin.json +1 -1
  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/ops/heartbeat-surface.d.ts +3 -3
  8. package/runtime/cli/ops/heartbeat-surface.js +6 -5
  9. package/runtime/cli/ops/ops-router.d.ts +6 -2
  10. package/runtime/cli/ops/ops-router.js +1273 -1145
  11. package/runtime/connectors/base/normalized-evidence-content.d.ts +4 -0
  12. package/runtime/connectors/base/normalized-evidence-content.js +21 -2
  13. package/runtime/connectors/evidence-normalizer.js +32 -1
  14. package/runtime/core/second-nature/action/action-closure-recorder.d.ts +2 -0
  15. package/runtime/core/second-nature/action/action-closure-recorder.js +49 -34
  16. package/runtime/core/second-nature/action/action-proposal-builder.js +3 -2
  17. package/runtime/core/second-nature/action/policy-bound-dispatch.d.ts +2 -0
  18. package/runtime/core/second-nature/action/policy-bound-dispatch.js +7 -3
  19. package/runtime/core/second-nature/control-plane/cycle-finalizer.d.ts +82 -0
  20. package/runtime/core/second-nature/control-plane/cycle-finalizer.js +187 -0
  21. package/runtime/core/second-nature/control-plane/heartbeat-orchestrator.js +13 -9
  22. package/runtime/core/second-nature/control-plane/real-runtime-spine.js +1 -1
  23. package/runtime/core/second-nature/guidance/guidance-proposal-consumer.d.ts +2 -1
  24. package/runtime/core/second-nature/guidance/guidance-proposal-consumer.js +4 -2
  25. package/runtime/core/second-nature/perception/judgment-engine.js +8 -4
  26. package/runtime/core/second-nature/perception/perception-builder.js +14 -2
  27. package/runtime/core/second-nature/quiet-dream/daily-rhythm-scheduler.js +30 -3
  28. package/runtime/core/second-nature/quiet-dream/dream-consolidation-runner.d.ts +5 -1
  29. package/runtime/core/second-nature/quiet-dream/dream-consolidation-runner.js +68 -29
  30. package/runtime/core/second-nature/quiet-dream/dream-scheduler.js +2 -1
  31. package/runtime/core/second-nature/quiet-dream/memory-projection-lifecycle.js +2 -1
  32. package/runtime/core/second-nature/quiet-dream/quiet-daily-review-builder.d.ts +1 -0
  33. package/runtime/core/second-nature/quiet-dream/quiet-daily-review-builder.js +33 -0
  34. package/runtime/observability/causal-loop-health.d.ts +2 -1
  35. package/runtime/observability/causal-loop-health.js +7 -0
  36. package/runtime/observability/loop-stage-event-sink.js +6 -1
  37. package/runtime/observability/loop-status.d.ts +2 -0
  38. package/runtime/observability/loop-status.js +14 -1
  39. package/runtime/observability/services/heartbeat-digest-assembler.d.ts +3 -0
  40. package/runtime/observability/services/heartbeat-digest-assembler.js +9 -0
  41. package/runtime/shared/degraded-status-classifier.d.ts +16 -0
  42. package/runtime/shared/degraded-status-classifier.js +68 -0
  43. package/runtime/shared/evidence-level-classifier.d.ts +61 -0
  44. package/runtime/shared/evidence-level-classifier.js +116 -0
  45. package/runtime/shared/provenance-tier.d.ts +37 -0
  46. package/runtime/shared/provenance-tier.js +97 -0
  47. package/runtime/shared/setup-ack.d.ts +54 -0
  48. package/runtime/shared/setup-ack.js +108 -0
  49. package/runtime/shared/source-ref-compat.js +5 -2
  50. package/runtime/shared/types/v8-contracts.d.ts +13 -2
  51. package/runtime/storage/db/index.js +71 -28
  52. package/runtime/storage/db/migrations/v8-005-single-status-schema.js +2 -2
  53. package/runtime/storage/db/migrations/v8-006-loop-stage-event-proof-trace-columns.d.ts +9 -0
  54. package/runtime/storage/db/migrations/v8-006-loop-stage-event-proof-trace-columns.js +15 -0
  55. package/runtime/storage/db/schema/v8-entities.d.ts +76 -0
  56. package/runtime/storage/db/schema/v8-entities.js +4 -0
  57. package/runtime/storage/services/write-validation-gate.js +1 -1
  58. package/runtime/storage/v8-state-stores.d.ts +7 -2
  59. package/runtime/storage/v8-state-stores.js +37 -19
@@ -24,12 +24,13 @@
24
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
26
  import { serializeSourceRefs, parseSourceRefs, } from "../shared/serialization.js";
27
+ import { classifyDegradedStatus } from "../shared/degraded-status-classifier.js";
27
28
  // ───────────────────────────────────────────────────────────────
28
29
  // Shared helpers
29
30
  // ───────────────────────────────────────────────────────────────
30
31
  function makeDegraded(reason, ownerStage, operatorNextAction, sourceRefs = []) {
31
32
  return {
32
- status: "degraded",
33
+ status: classifyDegradedStatus(reason),
33
34
  reason,
34
35
  ownerStage,
35
36
  sourceRefs,
@@ -54,8 +55,9 @@ export async function writeEvidenceItem(db, row) {
54
55
  if (!validated.ok)
55
56
  return validated.degraded;
56
57
  try {
58
+ const { sourceRefs: _, ...rest } = row;
57
59
  const record = {
58
- ...row,
60
+ ...rest,
59
61
  sourceRefsJson: serializeSourceRefs(validated.record),
60
62
  };
61
63
  await db.db
@@ -142,7 +144,7 @@ function validatePerceptionCardCanonical(row) {
142
144
  return {
143
145
  ok: false,
144
146
  degraded: {
145
- status: "degraded",
147
+ status: classifyDegradedStatus("perception_contract_drift"),
146
148
  reason: "perception_contract_drift",
147
149
  ownerStage: "perception",
148
150
  sourceRefs: row.sourceRefs,
@@ -157,7 +159,7 @@ function validatePerceptionCardCanonical(row) {
157
159
  return {
158
160
  ok: false,
159
161
  degraded: {
160
- status: "degraded",
162
+ status: classifyDegradedStatus("perception_contract_drift"),
161
163
  reason: "perception_contract_drift",
162
164
  ownerStage: "perception",
163
165
  sourceRefs: row.sourceRefs,
@@ -172,7 +174,7 @@ function validatePerceptionCardCanonical(row) {
172
174
  return {
173
175
  ok: false,
174
176
  degraded: {
175
- status: "degraded",
177
+ status: classifyDegradedStatus("perception_contract_drift"),
176
178
  reason: "perception_contract_drift",
177
179
  ownerStage: "perception",
178
180
  sourceRefs: row.sourceRefs,
@@ -191,8 +193,9 @@ export async function writePerceptionCard(db, row) {
191
193
  if (!canonicalCheck.ok)
192
194
  return canonicalCheck.degraded;
193
195
  try {
196
+ const { sourceRefs: _, ...rest } = row;
194
197
  const record = {
195
- ...row,
198
+ ...rest,
196
199
  sourceRefsJson: serializeSourceRefs(validated.record),
197
200
  };
198
201
  await db.db.insert(perceptionCard).values(record);
@@ -257,8 +260,9 @@ export async function writeJudgmentVerdict(db, row) {
257
260
  if (!validated.ok)
258
261
  return validated.degraded;
259
262
  try {
263
+ const { sourceRefs: _, ...rest } = row;
260
264
  const record = {
261
- ...row,
265
+ ...rest,
262
266
  sourceRefsJson: serializeSourceRefs(validated.record),
263
267
  };
264
268
  await db.db.insert(judgmentVerdict).values(record);
@@ -307,9 +311,13 @@ export async function writeActionClosureRecord(db, row) {
307
311
  if (!validated.ok)
308
312
  return validated.degraded;
309
313
  try {
314
+ const { sourceRefs: _, proofRefs, traceRefs, payload, ...rest } = row;
310
315
  const record = {
311
- ...row,
316
+ ...rest,
312
317
  sourceRefsJson: serializeSourceRefs(validated.record),
318
+ proofRefsJson: serializeSourceRefs(proofRefs ?? []),
319
+ traceRefsJson: serializeSourceRefs(traceRefs ?? []),
320
+ payloadJson: JSON.stringify(payload ?? {}),
313
321
  };
314
322
  await db.db.insert(actionClosureRecord).values(record);
315
323
  return { id: row.id };
@@ -358,10 +366,11 @@ export async function writeQuietDailyReview(db, row) {
358
366
  if (!validated.ok)
359
367
  return validated.degraded;
360
368
  try {
369
+ const { sourceRefs: _, closureRefs, ...rest } = row;
361
370
  const record = {
362
- ...row,
371
+ ...rest,
363
372
  sourceRefsJson: serializeSourceRefs(validated.record),
364
- closureRefsJson: row.closureRefs ? serializeSourceRefs(row.closureRefs) : null,
373
+ closureRefsJson: closureRefs ? serializeSourceRefs(closureRefs) : null,
365
374
  };
366
375
  await db.db.insert(quietDailyReview).values(record);
367
376
  return { id: row.id };
@@ -409,8 +418,9 @@ export async function writeDreamConsolidationRun(db, row) {
409
418
  if (!validated.ok)
410
419
  return validated.degraded;
411
420
  try {
421
+ const { sourceRefs: _, ...rest } = row;
412
422
  const record = {
413
- ...row,
423
+ ...rest,
414
424
  sourceRefsJson: serializeSourceRefs(validated.record),
415
425
  };
416
426
  await db.db.insert(dreamConsolidationRun).values(record);
@@ -497,8 +507,9 @@ export async function writeLongTermMemoryProjection(db, row) {
497
507
  if (!validated.ok)
498
508
  return validated.degraded;
499
509
  try {
510
+ const { sourceRefs: _, ...rest } = row;
500
511
  const record = {
501
- ...row,
512
+ ...rest,
502
513
  sourceRefsJson: serializeSourceRefs(validated.record),
503
514
  };
504
515
  await db.db.insert(longTermMemoryProjection).values(record);
@@ -577,8 +588,9 @@ export async function readLongTermMemoryProjectionById(db, id) {
577
588
  // ───────────────────────────────────────────────────────────────
578
589
  export async function writeHeartbeatCycleTrace(db, row) {
579
590
  try {
591
+ const { sourceRefs: _, ...rest } = row;
580
592
  const record = {
581
- ...row,
593
+ ...rest,
582
594
  sourceRefsJson: row.sourceRefs ? serializeSourceRefs(row.sourceRefs) : "[]",
583
595
  };
584
596
  await db.db.insert(heartbeatCycleTrace).values(record);
@@ -613,15 +625,18 @@ export async function writeLoopStageEvent(db, row) {
613
625
  if (!validated.ok)
614
626
  return validated.degraded;
615
627
  try {
628
+ const { sourceRefs: _s, proofRefs: _p, traceRefs: _t, ...rest } = row;
616
629
  const record = {
617
- ...row,
630
+ ...rest,
618
631
  sourceRefsJson: serializeSourceRefs(validated.record),
632
+ proofRefsJson: serializeSourceRefs(row.proofRefs ?? []),
633
+ traceRefsJson: serializeSourceRefs(row.traceRefs ?? []),
619
634
  };
620
635
  await db.db.insert(loopStageEvent).values(record);
621
636
  return { id: row.id };
622
637
  }
623
- catch {
624
- 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);
625
640
  }
626
641
  }
627
642
  export async function readLoopStageEventsByCycle(db, cycleId) {
@@ -665,8 +680,9 @@ export async function writeImpulseContextArtifact(db, row) {
665
680
  if (!validated.ok)
666
681
  return validated.degraded;
667
682
  try {
683
+ const { sourceRefs: _, ...rest } = row;
668
684
  const record = {
669
- ...row,
685
+ ...rest,
670
686
  sourceRefsJson: serializeSourceRefs(validated.record),
671
687
  };
672
688
  // Upsert: delete existing then insert (SQLite primary-key conflict)
@@ -715,8 +731,9 @@ export async function writeDailyRhythmState(db, row) {
715
731
  if (!validated.ok)
716
732
  return validated.degraded;
717
733
  try {
734
+ const { sourceRefs: _, ...rest } = row;
718
735
  const record = {
719
- ...row,
736
+ ...rest,
720
737
  sourceRefsJson: serializeSourceRefs(validated.record),
721
738
  };
722
739
  await db.db.delete(dailyRhythmState).where(eq(dailyRhythmState.id, row.id));
@@ -764,8 +781,9 @@ export async function writeConnectorCooldownState(db, row) {
764
781
  if (!validated.ok)
765
782
  return validated.degraded;
766
783
  try {
784
+ const { sourceRefs: _, ...rest } = row;
767
785
  const record = {
768
- ...row,
786
+ ...rest,
769
787
  sourceRefsJson: serializeSourceRefs(validated.record),
770
788
  };
771
789
  await db.db.delete(connectorCooldownState).where(eq(connectorCooldownState.id, row.id));