4 Commits

Author SHA1 Message Date
33fcd17746 feat(hooks): fabric-chat-injector — suggest chat workflow on channel turns
New before_prompt_build hook that appends a "next action: workflow_start
chat" hint to the system prompt whenever the agent's turn was triggered
by a message in a fabric channel.

If Meridian (`globalThis.__meridian.getChatJournalForChannel`) reports
an existing chat journal for this (agentId, channelId), the hint
includes `from="<journal-id>"` so the agent resumes the conversation
file instead of starting a fresh one each turn.

Activation:
  - ctx.agentId AND ctx.channelId present
  - ctx.messageProvider in {fabric, '' (empty/omitted by gateway)}

TODO(phase-2): once Fabric exposes per-channel type info (DM / group /
triage) via a cross-plugin API, narrow this to xType === 'dm' only.
Today we fire on any fabric channel — chat workflow is a no-op outside
DMs, so the false positives are just prompt-text noise.

Dedup via WeakSet keyed on the event object (same pattern as the
existing before-prompt-build hook) so each turn injects at most once
even when multiple harness call sites trigger the hook.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-25 09:46:34 +01:00
f3e2e74d36 feat(prism-facet): add 'always' router + Hangman-Lab pcexec convention prompt
The 'always' router resolves to the constant key "always" for every
agent — pair with a rule like 'always:always → some-prompt.md' to
inject a prompt fragment unconditionally (no ego/role/position
lookup needed).

Bundle a site-specific prompt 'pcexec-convention.md' that tells every
agent: Hangman-Lab keeps site binaries at ~/.openclaw/bin (hf,
secret-mgr, ego-mgr, fabric-register, pcguard, lock-mgr, tea) — not
symlinked to /usr/local/bin — so they MUST be invoked via the pcexec
tool, not the codex built-in shell. Without this, agents would call
those CLIs directly and get 'command not found' (observed during the
2026-05-23 hf-wakeup runs on prod t2).

Register the binding in rules.json so it loads at gateway startup.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-23 08:48:05 +01:00
h z
be5b5f1922 Merge pull request 'fix: declare prompt_rules tool in contracts.tools' (#2) from fix/declare-contracts-tools into main 2026-05-20 14:48:08 +00:00
4d7f7dc6c8 fix: declare prompt_rules tool in contracts.tools
Same class of bug as Meridian (zhi/Meridian#2), HarborForge.OpenclawPlugin
(zhi/HarborForge.OpenclawPlugin#6), and PaddedCell (already fixed in
787d88c). OpenClaw's plugin host requires that any tool registered via
`api.registerTool()` is also declared in `contracts.tools` in the
plugin manifest, or the tool is silently dropped from the agent's
available tool list. plugin doctor was warning:

  prism-facet: plugin must declare contracts.tools before registering
  agent tools

PrismFacet registers exactly one tool, `prompt_rules` (in
tools/prompt-rules.js). Declaring it in the manifest. Verified in sim
that with this change the warning disappears and the tool becomes
visible to agents.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-20 14:49:42 +01:00
6 changed files with 138 additions and 1 deletions

View File

@@ -0,0 +1,85 @@
/**
* Inject a "start the chat workflow" hint into the agent's system prompt
* when this turn was triggered by a message in a fabric channel.
*
* Two cases:
* - no prior chat journal for this (agent, channel) → suggest
* `workflow_start chat` (fresh).
* - mapping exists → suggest `workflow_start chat from <journalId>`
* so the conversation continues in the same linear journal.
*
* The (channel → journal) mapping is owned by Meridian and exposed via
* globalThis.__meridian.getChatJournalForChannel. Meridian writes the
* entry the first time the agent starts a chat workflow on a channel
* with no `from` argument.
*
* TODO(phase-2): once Fabric.OpenclawPlugin exposes per-channel type
* info (DM / group / triage) via a cross-plugin API, narrow this hook
* to xType === 'dm' only. Today we inject for any fabric channel — chat
* workflow itself is a no-op outside DMs, but the suggestion is noise.
*/
const _G = globalThis as Record<string, unknown>;
const DEDUP_KEY = '_prismFacetFabricChatDedup';
interface MeridianBridge {
getChatJournalForChannel?: (agentId: string, channelId: string) => Promise<string | null>;
}
interface PromptCtx {
agentId?: string;
channelId?: string;
messageProvider?: string;
}
export function registerFabricChatInjector(api: {
on(hook: string, handler: (...args: any[]) => any): void;
logger: { info(msg: string): void; warn(msg: string): void };
}): void {
if (!(_G[DEDUP_KEY] instanceof WeakSet)) _G[DEDUP_KEY] = new WeakSet<object>();
const dedup = _G[DEDUP_KEY] as WeakSet<object>;
api.on('before_prompt_build', async (event: unknown, ctx: PromptCtx) => {
if (dedup.has(event as object)) return;
dedup.add(event as object);
const agentId = ctx.agentId || '';
const channelId = (ctx.channelId || '').trim();
const provider = (ctx.messageProvider || '').toLowerCase();
if (!agentId || !channelId) return;
if (provider && provider !== 'fabric') return;
// Empty provider also accepted — gateway sometimes omits the field
// even when the trigger was a fabric channel; channelId presence is
// the load-bearing signal.
let journalId: string | null = null;
const meridian = _G['__meridian'] as MeridianBridge | undefined;
if (typeof meridian?.getChatJournalForChannel === 'function') {
try {
journalId = await meridian.getChatJournalForChannel(agentId, channelId);
} catch (err) {
api.logger.warn(
`[prism-facet] fabric-chat-injector: meridian lookup failed for ` +
`agent=${agentId} channel=${channelId}: ${String(err)}`,
);
}
}
const cmd = journalId
? `\`workflow_start\` with \`workflow="chat"\` and \`from="${journalId}"\``
: `\`workflow_start\` with \`workflow="chat"\``;
const continuationLine = journalId
? `This channel already has an open chat journal (\`${journalId}\`). Resume it with the \`from\` argument so the conversation history stays in one file.`
: `No prior chat journal exists for this channel yet — Meridian will create a fresh one and remember the channel→journal mapping for future turns.`;
const segment =
`# Chat channel context\n` +
`\n` +
`This turn was triggered by a message in a fabric channel (\`${channelId}\`).\n` +
`${continuationLine}\n` +
`\n` +
`**Next action:** call ${cmd} to enter the chat workflow before doing anything else.\n`;
return { appendSystemContext: segment };
});
}

View File

@@ -2,6 +2,7 @@ import path from "node:path";
import { loadRouters } from "./core/router-loader.js"; import { loadRouters } from "./core/router-loader.js";
import { initRuleStore } from "./core/rule-store.js"; import { initRuleStore } from "./core/rule-store.js";
import { registerBeforePromptBuild } from "./hooks/before-prompt-build.js"; import { registerBeforePromptBuild } from "./hooks/before-prompt-build.js";
import { registerFabricChatInjector } from "./hooks/fabric-chat-injector.js";
import { registerPromptRulesTool } from "./tools/prompt-rules.js"; import { registerPromptRulesTool } from "./tools/prompt-rules.js";
interface PluginConfig { interface PluginConfig {
@@ -57,6 +58,7 @@ export default {
// Agent session hooks: register every time (dedup inside handler) // Agent session hooks: register every time (dedup inside handler)
registerBeforePromptBuild(api); registerBeforePromptBuild(api);
registerFabricChatInjector(api);
// Tools // Tools
registerPromptRulesTool(api, routersDir); registerPromptRulesTool(api, routersDir);

View File

@@ -17,5 +17,10 @@
"description": "Path to rules.json (default: {pluginDir}/rules.json)" "description": "Path to rules.json (default: {pluginDir}/rules.json)"
} }
} }
},
"contracts": {
"tools": [
"prompt_rules"
]
} }
} }

View File

@@ -0,0 +1,31 @@
# Hangman-Lab Site Convention — Shell Execution
This claw (sim or prod) keeps Hangman-Lab site binaries at `~/.openclaw/bin/`
and **does not** symlink them into `/usr/local/bin`. Your shell tool's PATH
does not include them by default, so calling them with the codex built-in
shell yields `command not found`.
**Rule:** any command that invokes one of these binaries MUST be run through
the `pcexec` tool, not the codex built-in shell:
- `hf` (HarborForge CLI)
- `secret-mgr` (per-agent secret store)
- `ego-mgr` (per-agent identity store; reads `role`, `position`, `default-username`, etc.)
- `fabric-register` (Fabric account provisioning)
- `pcguard` (PaddedCell guard)
- `lock-mgr`
- `tea`
`pcexec` injects `~/.openclaw/bin` into PATH and also wires the
`AGENT_ID`, `AGENT_WORKSPACE`, and `AGENT_VERIFY` env vars that
`secret-mgr` / `ego-mgr` need to authenticate as the calling agent.
## Examples
- ✅ Call the `pcexec` tool with `command: "hf calendar show --json"`
- ✅ Call the `pcexec` tool with `command: "HFT=$(secret-mgr get-secret --key hf-token); hf task list --token \"$HFT\" --json"` (the whole pipeline goes in one `pcexec` call)
- ❌ Sending `hf calendar show` to the codex built-in shell → `command not found`
If a workflow's `Procedure` shows a raw shell snippet involving these CLIs,
pass the **whole snippet** as a single `command:` argument to `pcexec`
don't split into multiple non-pcexec calls.

11
plugin/routers/always.ts Normal file
View File

@@ -0,0 +1,11 @@
/**
* `always` router — resolves to the constant key `"always"` for every
* agent. Pair with a rule like `always:always → <some-prompt.md>` to
* inject a prompt fragment into every agent's system prompt
* unconditionally (no ego / role / position lookup needed).
*/
import type { RouterContext } from "../core/router-loader.js";
export function resolve(_ctx: RouterContext): string {
return "always";
}

3
plugin/rules.json Normal file
View File

@@ -0,0 +1,3 @@
{
"always:always": "/root/.openclaw/plugins/prism-facet/prompts/pcexec-convention.md"
}