@haaaiawd/second-nature 0.2.5 → 0.2.7

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 (27) hide show
  1. package/openclaw.plugin.json +1 -1
  2. package/package.json +1 -1
  3. package/runtime/cli/commands/index.d.ts +4 -0
  4. package/runtime/cli/commands/index.js +188 -5
  5. package/runtime/cli/index.js +2 -0
  6. package/runtime/cli/ops/ops-router.js +23 -17
  7. package/runtime/connectors/base/normalized-evidence-content.d.ts +71 -0
  8. package/runtime/connectors/base/normalized-evidence-content.js +273 -0
  9. package/runtime/connectors/evidence-normalizer.d.ts +4 -3
  10. package/runtime/connectors/evidence-normalizer.js +128 -23
  11. package/runtime/core/second-nature/heartbeat/heartbeat-loop.js +15 -0
  12. package/runtime/core/second-nature/perception/perception-builder.d.ts +3 -1
  13. package/runtime/core/second-nature/perception/perception-builder.js +98 -44
  14. package/runtime/core/second-nature/quiet-dream/daily-rhythm-scheduler.d.ts +6 -3
  15. package/runtime/core/second-nature/quiet-dream/daily-rhythm-scheduler.js +198 -22
  16. package/runtime/core/second-nature/quiet-dream/quiet-daily-review-builder.d.ts +19 -3
  17. package/runtime/core/second-nature/quiet-dream/quiet-daily-review-builder.js +189 -12
  18. package/runtime/observability/db/index.d.ts +2 -0
  19. package/runtime/observability/db/index.js +6 -0
  20. package/runtime/shared/types/v8-contracts.d.ts +1 -1
  21. package/runtime/storage/db/index.d.ts +2 -0
  22. package/runtime/storage/db/index.js +16 -8
  23. package/runtime/storage/db/migrations/v8-003-quiet-closure-refs.js +2 -1
  24. package/runtime/storage/services/write-validation-gate.d.ts +2 -0
  25. package/runtime/storage/services/write-validation-gate.js +69 -17
  26. package/runtime/storage/v8-state-stores.d.ts +25 -0
  27. package/runtime/storage/v8-state-stores.js +87 -1
@@ -73,7 +73,16 @@ export async function writeEvidenceItem(db, row) {
73
73
  ...row,
74
74
  sourceRefsJson: serializeSourceRefs(validated.record),
75
75
  };
76
- await db.db.insert(evidenceItem).values(record);
76
+ await db.db
77
+ .insert(evidenceItem)
78
+ .values(record)
79
+ .onConflictDoUpdate({
80
+ target: [evidenceItem.platformId, evidenceItem.contentHash],
81
+ set: {
82
+ payloadJson: record.payloadJson,
83
+ observedAt: record.observedAt,
84
+ },
85
+ });
77
86
  return { id: row.id };
78
87
  }
79
88
  catch {
@@ -96,6 +105,47 @@ export async function readEvidenceItemsByStatus(db, lifecycleStatus) {
96
105
  };
97
106
  }
98
107
  }
108
+ export async function readEvidenceItemsByDay(db, day) {
109
+ try {
110
+ const rows = await db.db
111
+ .select()
112
+ .from(evidenceItem)
113
+ .where(like(evidenceItem.observedAt, `${day}%`))
114
+ .orderBy(desc(evidenceItem.observedAt));
115
+ return { rows };
116
+ }
117
+ catch {
118
+ return {
119
+ rows: [],
120
+ degraded: makeDegraded("state_unreadable", "ingestion", `Check state database connectivity for evidence day=${day}`),
121
+ };
122
+ }
123
+ }
124
+ export async function updateEvidenceItemLifecycleStatus(db, id, lifecycleStatus) {
125
+ try {
126
+ await db.db
127
+ .update(evidenceItem)
128
+ .set({ lifecycleStatus })
129
+ .where(eq(evidenceItem.id, id));
130
+ return { id };
131
+ }
132
+ catch {
133
+ return makeDegraded("state_unreadable", "ingestion", `Retry evidence lifecycle update for ${id} after DB recovery`);
134
+ }
135
+ }
136
+ export async function readEvidenceItemById(db, id) {
137
+ try {
138
+ const rows = await db.db
139
+ .select()
140
+ .from(evidenceItem)
141
+ .where(eq(evidenceItem.id, id))
142
+ .limit(1);
143
+ return { row: rows[0] };
144
+ }
145
+ catch {
146
+ return makeDegraded("state_unreadable", "ingestion", "Check state database connectivity");
147
+ }
148
+ }
99
149
  // ───────────────────────────────────────────────────────────────
100
150
  // PerceptionCard store
101
151
  // ───────────────────────────────────────────────────────────────
@@ -183,6 +233,22 @@ export async function readPerceptionCardsByCycle(db, cycleId) {
183
233
  };
184
234
  }
185
235
  }
236
+ export async function readPerceptionCardsByDay(db, day) {
237
+ try {
238
+ const rows = await db.db
239
+ .select()
240
+ .from(perceptionCard)
241
+ .where(like(perceptionCard.createdAt, `${day}%`))
242
+ .orderBy(desc(perceptionCard.createdAt));
243
+ return { rows };
244
+ }
245
+ catch {
246
+ return {
247
+ rows: [],
248
+ degraded: makeDegraded("state_unreadable", "perception", `Check state database connectivity for perception day=${day}`),
249
+ };
250
+ }
251
+ }
186
252
  export async function readPerceptionCardById(db, id) {
187
253
  try {
188
254
  const rows = await db.db
@@ -369,6 +435,26 @@ export async function writeDreamConsolidationRun(db, row) {
369
435
  return makeDegraded("state_unreadable", "dream", "Retry Dream write after DB recovery", validated.record);
370
436
  }
371
437
  }
438
+ /**
439
+ * Update an existing DreamConsolidationRun status and payload without
440
+ * primary-key conflict. Used by the scheduler after consolidation completes.
441
+ */
442
+ export async function updateDreamConsolidationRunStatus(db, id, status, options) {
443
+ try {
444
+ const updateData = { status };
445
+ if (options?.reason !== undefined)
446
+ updateData.reason = options.reason;
447
+ if (options?.lifecycleStatus !== undefined)
448
+ updateData.lifecycleStatus = options.lifecycleStatus;
449
+ if (options?.payloadJson !== undefined)
450
+ updateData.payloadJson = options.payloadJson;
451
+ await db.db.update(dreamConsolidationRun).set(updateData).where(eq(dreamConsolidationRun.id, id));
452
+ return { id };
453
+ }
454
+ catch {
455
+ return makeDegraded("state_unreadable", "dream", `Retry Dream status update for ${id} after DB recovery`);
456
+ }
457
+ }
372
458
  export async function readDreamConsolidationRunById(db, id) {
373
459
  try {
374
460
  const rows = await db.db