Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions integrations/lifecycle/flush-progress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2026 OceanBase.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { createHash } from 'node:crypto'

// The flush acknowledgement cases in tests/integration_conformance/cases.json exercise the flush response check
// through each plugin.

export interface FlushProgress {
currentCursor: number
remainingWork: boolean
}

export function nonNegativeInteger(value: unknown): value is number {
return typeof value === 'number' && Number.isInteger(value) && value >= 0
}

/** Return the progress a memory flush response reports, or undefined when the response is not a flush result. */
export function readFlushProgress(value: unknown): FlushProgress | undefined {
if (!value || typeof value !== 'object') return undefined
const result = value as Record<string, unknown>
if (
!['idle', 'processed'].includes(String(result.status))
|| !nonNegativeInteger(result.previous_cursor)
|| !nonNegativeInteger(result.current_cursor)
|| !nonNegativeInteger(result.high_watermark)
|| !nonNegativeInteger(result.processed_source_count)
|| (result.remaining_work !== undefined && typeof result.remaining_work !== 'boolean')
) {
return undefined
}
return {
currentCursor: result.current_cursor,
remainingWork: result.remaining_work === true,
}
}

/** Return a short, stable label that correlates one Scope's diagnostics without naming the Scope. */
export function scopeCorrelation(scopeId: string): string {
return createHash('sha256').update(scopeId).digest('hex').slice(0, 16)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2026 OceanBase.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// Generated from integrations/lifecycle/flush-progress.ts; do not edit.
// Edit the source and run `make integration-lifecycle` instead.

import { createHash } from 'node:crypto'

// The flush acknowledgement cases in tests/integration_conformance/cases.json exercise the flush response check
// through each plugin.

export interface FlushProgress {
currentCursor: number
remainingWork: boolean
}

export function nonNegativeInteger(value: unknown): value is number {
return typeof value === 'number' && Number.isInteger(value) && value >= 0
}

/** Return the progress a memory flush response reports, or undefined when the response is not a flush result. */
export function readFlushProgress(value: unknown): FlushProgress | undefined {
if (!value || typeof value !== 'object') return undefined
const result = value as Record<string, unknown>
if (
!['idle', 'processed'].includes(String(result.status))
|| !nonNegativeInteger(result.previous_cursor)
|| !nonNegativeInteger(result.current_cursor)
|| !nonNegativeInteger(result.high_watermark)
|| !nonNegativeInteger(result.processed_source_count)
|| (result.remaining_work !== undefined && typeof result.remaining_work !== 'boolean')
) {
return undefined
}
return {
currentCursor: result.current_cursor,
remainingWork: result.remaining_work === true,
}
}

/** Return a short, stable label that correlates one Scope's diagnostics without naming the Scope. */
export function scopeCorrelation(scopeId: string): string {
return createHash('sha256').update(scopeId).digest('hex').slice(0, 16)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/


import { createHash } from "node:crypto";
import type { OpenClawPluginApi } from "openclaw/plugin-sdk/plugin-entry";
import { normalizeAgentId, parseAgentSessionKey } from "openclaw/plugin-sdk/routing";
import type { PowerContextConfig } from "./config.js";
Expand All @@ -30,6 +29,7 @@ import {
} from "./content.js";
import type { PowerContextClient } from "./http.js";
import { createDiagnosticEmitter, failureEvent } from "./diagnostics.js";
import { type FlushProgress, nonNegativeInteger, readFlushProgress, scopeCorrelation } from "./flush-progress.js";
import { resolvePowerContextScope } from "./scope.js";
import { isPowerContextCapabilities, isPreparedContext } from "./types.js";

Expand All @@ -44,11 +44,6 @@ const MAX_SESSION_SCOPES = 32;
const MAX_TRACKED_RUNS = 64;
const CLOSING_BUDGET_MS = 1000;

type FlushProgress = {
currentCursor: number;
remainingWork: boolean;
};

type CheckpointResult = {
acknowledged: boolean;
observedCursor?: number;
Expand All @@ -66,10 +61,6 @@ function agentIdFromSessionKey(sessionKey: string | undefined): string | undefin
return agentId ? normalizeAgentId(agentId) : undefined;
}

function nonNegativeInteger(value: unknown): value is number {
return typeof value === "number" && Number.isInteger(value) && value >= 0;
}

function sourcePosition(value: unknown): number {
if (!value || typeof value !== "object") {
throw new Error("PowerContext returned an invalid Source capture payload");
Expand All @@ -82,28 +73,11 @@ function sourcePosition(value: unknown): number {
}

function flushProgress(value: unknown): FlushProgress {
if (!value || typeof value !== "object") {
const progress = readFlushProgress(value);
if (!progress) {
throw new Error("PowerContext returned an invalid flush payload");
}
const result = value as Record<string, unknown>;
if (
!["idle", "processed"].includes(String(result.status))
|| !nonNegativeInteger(result.previous_cursor)
|| !nonNegativeInteger(result.current_cursor)
|| !nonNegativeInteger(result.high_watermark)
|| !nonNegativeInteger(result.processed_source_count)
|| (result.remaining_work !== undefined && typeof result.remaining_work !== "boolean")
) {
throw new Error("PowerContext returned an invalid flush payload");
}
return {
currentCursor: result.current_cursor,
remainingWork: result.remaining_work === true,
};
}

function scopeCorrelation(scopeId: string): string {
return createHash("sha256").update(scopeId).digest("hex").slice(0, 16);
return progress;
}

export function registerPowerContextLifecycle(api: OpenClawPluginApi, deps: LifecycleDependencies) {
Expand Down
57 changes: 57 additions & 0 deletions integrations/pi/plugins/powercontext/src/flush-progress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2026 OceanBase.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// Generated from integrations/lifecycle/flush-progress.ts; do not edit.
// Edit the source and run `make integration-lifecycle` instead.

import { createHash } from 'node:crypto'

// The flush acknowledgement cases in tests/integration_conformance/cases.json exercise the flush response check
// through each plugin.

export interface FlushProgress {
currentCursor: number
remainingWork: boolean
}

export function nonNegativeInteger(value: unknown): value is number {
return typeof value === 'number' && Number.isInteger(value) && value >= 0
}

/** Return the progress a memory flush response reports, or undefined when the response is not a flush result. */
export function readFlushProgress(value: unknown): FlushProgress | undefined {
if (!value || typeof value !== 'object') return undefined
const result = value as Record<string, unknown>
if (
!['idle', 'processed'].includes(String(result.status))
|| !nonNegativeInteger(result.previous_cursor)
|| !nonNegativeInteger(result.current_cursor)
|| !nonNegativeInteger(result.high_watermark)
|| !nonNegativeInteger(result.processed_source_count)
|| (result.remaining_work !== undefined && typeof result.remaining_work !== 'boolean')
) {
return undefined
}
return {
currentCursor: result.current_cursor,
remainingWork: result.remaining_work === true,
}
}

/** Return a short, stable label that correlates one Scope's diagnostics without naming the Scope. */
export function scopeCorrelation(scopeId: string): string {
return createHash('sha256').update(scopeId).digest('hex').slice(0, 16)
}
34 changes: 4 additions & 30 deletions integrations/pi/plugins/powercontext/src/flush.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,48 +14,22 @@
* limitations under the License.
*/

import { createHash } from 'node:crypto'
import { combineSignals, createTimeoutSignal, type PowerContextClient } from './client.ts'
import type { ResolvedConfig } from './config.ts'
import { InvalidResponseError } from './errors.ts'
import { type FlushProgress, readFlushProgress, scopeCorrelation } from './flush-progress.ts'

const CLOSING_BUDGET_MS = 1000

export interface FlushProgress {
currentCursor: number
remainingWork: boolean
}

export interface PendingFlushResult {
acknowledged: boolean
pendingScopes: number
}

function nonNegativeInteger(value: unknown): value is number {
return typeof value === 'number' && Number.isInteger(value) && value >= 0
}

export function flushProgress(value: unknown): FlushProgress {
if (!value || typeof value !== 'object') throw new InvalidResponseError('/v1/memory/flush')
const result = value as Record<string, unknown>
if (
!['idle', 'processed'].includes(String(result.status))
|| !nonNegativeInteger(result.previous_cursor)
|| !nonNegativeInteger(result.current_cursor)
|| !nonNegativeInteger(result.high_watermark)
|| !nonNegativeInteger(result.processed_source_count)
|| (result.remaining_work !== undefined && typeof result.remaining_work !== 'boolean')
) {
throw new InvalidResponseError('/v1/memory/flush')
}
return {
currentCursor: result.current_cursor,
remainingWork: result.remaining_work === true,
}
}

function scopeCorrelation(scopeId: string): string {
return createHash('sha256').update(scopeId).digest('hex').slice(0, 16)
const progress = readFlushProgress(value)
if (!progress) throw new InvalidResponseError('/v1/memory/flush')
return progress
}

export interface PendingSourceFlusher {
Expand Down
6 changes: 5 additions & 1 deletion scripts/generate_integration_lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@
f"{_CODEX}/hooks/bootstrap_context.py",
),
}
# A copy keeps the path its plugin imports, so bundled output still names the same module.
# Each copy sits at the path its plugin imports.
TYPESCRIPT_COPIES = {
"flush-progress.ts": (
f"{_OPENCLAW}/src/flush-progress.ts",
f"{_PI}/src/flush-progress.ts",
),
"transport.ts": (
f"{_DSH}/src/transport.ts",
f"{_OPENCLAW}/src/transport.ts",
Expand Down
Loading