MCPcopy
hub / github.com/openai/plugins / bounded_json_value

Function bounded_json_value

plugins/codex-security/scripts/finding_preview.py:197–236  ·  view source on GitHub ↗
(value: Any, budget: list[int], *, depth: int = 0)

Source from the content-addressed store, hash-verified

195
196
197def bounded_json_value(value: Any, budget: list[int], *, depth: int = 0) -> Any:
198 if budget[0] <= 0:
199 return None
200 if depth >= 4:
201 consume_json_budget(budget, 4)
202 return None
203 if isinstance(value, str):
204 bounded, size = bounded_json_text(value, budget[0])
205 consume_json_budget(budget, size)
206 return bounded
207 if value is None or isinstance(value, (bool, int, float)):
208 consume_json_budget(budget, len(json.dumps(value, separators=(",", ":")).encode("utf-8")))
209 return value
210 if isinstance(value, list):
211 if not consume_json_budget(budget, 2):
212 return []
213 result = []
214 for item in value[:20]:
215 separator = 0 if not result else 1
216 if not consume_json_budget(budget, separator):
217 break
218 result.append(bounded_json_value(item, budget, depth=depth + 1))
219 return result
220 if isinstance(value, dict):
221 if not consume_json_budget(budget, 2):
222 return {}
223 result = {}
224 for key, item in list(value.items())[:20]:
225 if budget[0] <= 0 or not isinstance(key, str):
226 break
227 separator = 0 if not result else 1
228 if not consume_json_budget(budget, separator):
229 break
230 bounded_key, key_size = bounded_json_text(key, min(budget[0], 512))
231 if not consume_json_budget(budget, key_size + 1):
232 break
233 result[bounded_key] = bounded_json_value(item, budget, depth=depth + 1)
234 return result
235 consume_json_budget(budget, 4)
236 return None
237
238
239def consume_json_budget(budget: list[int], size: int) -> bool:

Callers 2

bounded_finding_detailsFunction · 0.85
bounded_finding_sectionFunction · 0.85

Calls 2

consume_json_budgetFunction · 0.85
bounded_json_textFunction · 0.85

Tested by

no test coverage detected