osuite 2.15.0 → 2.17.0

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.
package/runtime/hook.mjs CHANGED
@@ -137,12 +137,15 @@ export async function runHook(data, localConfig, dependencies = {}) {
137
137
  const checkedNote = message => fenceUnchanged() ? note(message) : pre ? deny(accessChanged) : note(accessChanged);
138
138
  const config = { ...applyPosture(localConfig, remote.posture), ruleRollout: remote.ruleRollout,
139
139
  orgId: remote.orgId || localConfig.orgId };
140
+ const postureEvidence = { local_mode: localConfig.mode, effective_mode: config.mode,
141
+ source: remote.source, scope: remote.scope || null, valid_until: remote.validUntil || null,
142
+ revision: remote.revision, fallback: remote.source === 'unavailable' ? config.guardFailureMode : null };
140
143
  api = trackPostureRejections(dependencies.api || apiFactory(config), localConfig, store, now);
141
144
  if (event === 'Stop') {
142
145
  const stopped = await stopHook(data, config, api, store, fenceUnchanged);
143
146
  return fenceUnchanged() ? stopped : note(accessChanged);
144
147
  }
145
- if (remote.blocked) return pre ? deny('Verified runtime posture is unavailable or expired. Execution is blocked; reconnect or restore access.')
148
+ if (remote.blocked) return pre ? deny('POSTURE_UNAVAILABLE. Verified runtime posture is unavailable or expired. Execution is blocked. Reconnect or restore access.')
146
149
  : note('Verified runtime posture is unavailable. Enforce hooks will block execution until it can be verified.');
147
150
  if (!pre && event !== 'SessionStart' && event !== 'SessionEnd') return note('Unsupported hook event; no execution decision was made.');
148
151
  const session = await openSession(api, config, data, store, now());
@@ -156,7 +159,8 @@ export async function runHook(data, localConfig, dependencies = {}) {
156
159
  rememberGap(store, 'posture_unavailable', now());
157
160
  return checkedNote('Not governed: remote posture is unavailable; explicit observe mode does not enforce OSuite policy.');
158
161
  }
159
- await emitRuntimeEvent(api, config, context, 'tool_preflight', null, { tool_input_digest: context.tool_input_digest, install_check: config.installCheck === true });
162
+ await emitRuntimeEvent(api, config, context, 'tool_preflight', null, { tool_input_digest: context.tool_input_digest,
163
+ install_check: config.installCheck === true, runtime_posture: postureEvidence });
160
164
  if (!fenceUnchanged()) return deny(accessChanged);
161
165
  const operationKey = `operation:${session}:${context.tool_use_id}`;
162
166
  const rememberOperation = ['claude', 'codex'].includes(config.host) && config.privateKeyBase64 && !config.installCheck
@@ -164,6 +168,10 @@ export async function runHook(data, localConfig, dependencies = {}) {
164
168
  const current = store.update(operationKey, prior => prior || { action_id: actionId, input_digest: context.tool_input_digest });
165
169
  return current.action_id === actionId && current.input_digest === context.tool_input_digest;
166
170
  } : undefined;
171
+ if (rememberOperation && config.mode === 'observe' && remote.source === 'remote' && !remote.ruleRollout?.enabled) {
172
+ context.hook_observation = { runtime_id: config.runtimeId, provider: config.provider ?? null,
173
+ local_mode: localConfig.mode, valid_until: remote.validUntil, revision: remote.revision };
174
+ }
167
175
  const result = await admitAction(context, { api, config, ...dependencies.admission, rememberOperation,
168
176
  beforeRelease: response => validateGitPublication(context, response) });
169
177
  const dispatchBody = result.hookOperationId ? { hook_action_id: result.hookOperationId, tool_input_digest: context.tool_input_digest } : null;
@@ -176,7 +184,9 @@ export async function runHook(data, localConfig, dependencies = {}) {
176
184
  };
177
185
  if (result.degraded) {
178
186
  rememberGap(store, result.reason, now());
179
- await emitRuntimeEvent(api, config, context, 'governance_unavailable', null, { decision: result.decision === 'allow' ? 'guard_unavailable' : 'guard_unavailable_blocked' });
187
+ await emitRuntimeEvent(api, config, context, 'governance_unavailable', null, { decision: result.decision === 'allow' ? 'guard_unavailable' : 'guard_unavailable_blocked',
188
+ phase: result.phase || 'guard', retry_same_operation: result.retrySameOperation === true,
189
+ retry_after_seconds: result.retryAfterSeconds ?? null });
180
190
  return result.decision === 'allow' ? checkedNote('Not governed: the guard is unavailable and this verified posture explicitly allows availability fallback. No authorization or completion is claimed.')
181
191
  : deny('Guard unavailable. Verified policy requires blocking this call.');
182
192
  }
@@ -185,7 +195,7 @@ export async function runHook(data, localConfig, dependencies = {}) {
185
195
  await emitRuntimeEvent(api, config, context, 'install_check_policy_result', null, { decision: result.decision, approval_state: 'not_requested', install_check: true });
186
196
  return deny('Install check: policy would require human approval. No request was sent to reviewers and no tool was executed.');
187
197
  }
188
- if (result.authorized) {
198
+ if (result.authorized || result.observeRecorded) {
189
199
  try {
190
200
  store.write(`action:${context.tool_use_id}`, { schema: 'osuite.node_action.v1', action_id: result.actionId,
191
201
  runtime_session_id: session, tool_name: data.tool_name, tool_use_id: context.tool_use_id,
@@ -203,7 +213,9 @@ export async function runHook(data, localConfig, dependencies = {}) {
203
213
  }
204
214
  }
205
215
  updatePending(store, context);
206
- await emitRuntimeEvent(api, config, context, 'tool_allowed', result.actionId, { decision: result.decision, install_check: config.installCheck === true });
216
+ await emitRuntimeEvent(api, config, context, 'tool_allowed', result.actionId, { decision: result.observeRecorded ? 'observed_dispatch' : result.decision,
217
+ ...(result.observeRecorded ? { observe_only: true, policy_recommendation: result.decision } : {}),
218
+ approval_required: false, install_check: config.installCheck === true });
207
219
  return checkedNote(config.mode === 'observe' && !result.ruleRolloutBinding ? 'Observe mode recorded a policy result; it does not enforce OSuite policy.' : result.decision === 'warn' ? 'OSuite authorized this call with a policy warning.' : '');
208
220
  }
209
221
  if (result.reason === 'action_evidence_changed') await cancelUnreleased();
@@ -214,7 +226,8 @@ export async function runHook(data, localConfig, dependencies = {}) {
214
226
  return result.observeOnly === true ? checkedNote('Observe mode: this call would require human approval; it is not authorized by a reviewer.')
215
227
  : deny(`Human approval is still required for action ${result.actionId}. Do not run or automatically retry it. Review it in OSuite Studio.`);
216
228
  }
217
- await emitRuntimeEvent(api, config, context, 'tool_blocked', result.actionId, { decision: result.observeOnly === true ? 'observe_would_block' : 'block', reason: result.reason });
229
+ await emitRuntimeEvent(api, config, context, 'tool_blocked', result.actionId, { decision: result.observeOnly === true ? 'observe_would_block' : 'block', reason: result.reason,
230
+ phase: result.phase || null, retry_same_operation: result.retrySameOperation === true, retry_after_seconds: result.retryAfterSeconds ?? null });
218
231
  return result.observeOnly === true ? checkedNote('Observe mode: OSuite policy would block this call. It is not authorized by OSuite.')
219
232
  : deny(`OSuite policy did not authorize this exact action (${result.reason}). Do not retry automatically.`);
220
233
  } catch {
@@ -232,7 +232,7 @@ export async function resolveRemotePosture(config, { api, store, now = Date.now(
232
232
  entry = cacheEntry(saved, config, now); cached = entry?.document;
233
233
  if (rejectionFenceUnchanged(config, store, rejectionFence) && cached && canonicalJson(cached) === canonicalJson(document)) {
234
234
  return { available: true, blocked: false, source: 'remote', posture: document.posture, revision: document.revision,
235
- ruleRollout: document.rule_rollout, orgId: document.org_id, rejectionFence };
235
+ validUntil: document.valid_until, scope: response.body.scope, ruleRollout: document.rule_rollout, orgId: document.org_id, rejectionFence };
236
236
  }
237
237
  } catch {
238
238
  const posture = offlinePosture(offlinePosture(localPosture(config), readEntry(store, config, now)?.document?.posture), document.posture);
@@ -246,7 +246,7 @@ export async function resolveRemotePosture(config, { api, store, now = Date.now(
246
246
  && validNegotiation(cached, config, now)) {
247
247
  if (capable && !matchesAcknowledgement(cached, acknowledged)) return { available: false, blocked: true, source: 'unavailable', posture, revision: cached.revision };
248
248
  return { available: true, blocked: false, source: 'cache', posture, revision: cached.revision,
249
- ruleRollout: cached.rule_rollout, orgId: cached.org_id, rejectionFence };
249
+ validUntil: cached.valid_until, ruleRollout: cached.rule_rollout, orgId: cached.org_id, rejectionFence };
250
250
  }
251
251
  return { available: false, blocked: rejected || fenced || entry?.rejected === true || capable || posture.mode !== 'observe',
252
252
  source: 'unavailable', posture, revision: cached?.revision || null, rejectionFence };
@@ -101,9 +101,12 @@ export function createApiClient(config, { fetchImpl = globalThis.fetch, timeoutM
101
101
  clearTimeout(timer);
102
102
  timer = setTimeout(() => controller.abort(), Math.max(1, totalMs - elapsed));
103
103
  }
104
- if (status < 200 || status >= 300) {
104
+ const ok = status >= 200 && status < 300;
105
+ if (!ok && !(status === 403 && options.readForbiddenBody === true)) {
106
+ const retryAfter = response.headers?.get('retry-after');
107
+ const retryAfterSeconds = /^\d{1,4}$/.test(retryAfter || '') && Number(retryAfter) <= 3600 ? Number(retryAfter) : null;
105
108
  await response.body?.cancel();
106
- return { ok: false, status, error: 'http_error' };
109
+ return { ok: false, status, error: 'http_error', ...(retryAfterSeconds !== null ? { retryAfterSeconds } : {}) };
107
110
  }
108
111
  const reader = response.body?.getReader();
109
112
  if (!reader) return { ok: false, status, error: 'invalid_response' };
@@ -127,7 +130,7 @@ export function createApiClient(config, { fetchImpl = globalThis.fetch, timeoutM
127
130
  return { ok: false, status, error: 'timeout' };
128
131
  }
129
132
  if (!isObject(parsed)) return { ok: false, status, error: 'invalid_response' };
130
- return { ok: true, status, body: parsed };
133
+ return ok ? { ok, status, body: parsed } : { ok, status, body: parsed, error: 'http_error' };
131
134
  } catch (error) {
132
135
  return { ok: false, status, error: status ? 'invalid_response' : controller.signal.aborted ? 'timeout'
133
136
  : transientTransport(error) ? 'transport_failure' : 'request_failed' };