@@ -539,8 +539,8 @@ static void build_def_props(char *buf, size_t bufsize, const CBMDefinition *def)
539539 }
540540 size_t pos = (size_t )n ;
541541 append_json_string (buf , bufsize , & pos , "docstring" , def -> docstring );
542- /* Right after the docstring: the buffer is sized for exactly these three
543- * uncapped fields (pp_props_buf), so none can be squeezed out. */
542+ /* The buffer is sized for every field below (pp_props_buf), so none can be
543+ * squeezed out. */
544544 append_json_raw (buf , bufsize , & pos , "variants" , def -> variants );
545545 append_json_string (buf , bufsize , & pos , "variant_key" , def -> variant_key );
546546 append_json_string (buf , bufsize , & pos , "signature" , def -> signature );
@@ -731,24 +731,71 @@ typedef struct {
731731 * to the response/logfile — this only throttles the stderr noise). */
732732enum { PP_OVERSIZED_WARN_MAX = 32 };
733733
734- /* A def's properties buffer: CBM_SZ_2K for every other field plus the whole
735- * serialized docstring, variants and variant_key fields, which have no length
736- * cap (a field that does not fit is dropped whole). Returns `stack` for a def
737- * with none of them, or when the larger buffer cannot be allocated. Twin of
738- * pass_definitions.c -- keep both in sync. */
739- static char * pp_props_buf (const CBMDefinition * def , char * stack , size_t * size ) {
740- size_t need = * size ;
741- if (def -> docstring && def -> docstring [0 ]) {
742- need += strlen ("docstring" ) + pp_json_escaped_len (def -> docstring ) + PP_JSON_FIELD_OVERHEAD ;
734+ /* Upper bound of build_def_props' fixed prefix: the Function/Method metrics
735+ * literal (about 330 B) with nine ints and seven true/false/test_role strings at
736+ * their widest stays under 600 B. */
737+ enum { PP_PROPS_PREFIX_MAX = CBM_SZ_1K };
738+
739+ /* Serialized size of one optional field as append_json_string emits it
740+ * (0 when the field is absent). */
741+ static size_t pp_str_field_len (const char * key , const char * val ) {
742+ return val && val [0 ] ? strlen (key ) + pp_json_escaped_len (val ) + PP_JSON_FIELD_OVERHEAD : 0 ;
743+ }
744+
745+ /* Serialized size of a string array as append_json_str_array emits it. */
746+ static size_t pp_arr_field_len (const char * key , const char * * arr ) {
747+ if (!arr || !arr [0 ]) {
748+ return 0 ;
749+ }
750+ size_t n = strlen (key ) + PP_JSON_FIELD_OVERHEAD ;
751+ for (int i = 0 ; arr [i ]; i ++ ) {
752+ n += pp_json_escaped_len (arr [i ]) + PP_ESC_SPACE + (i > 0 ? SKIP_ONE : 0 );
743753 }
754+ return n ;
755+ }
756+
757+ /* A def's properties buffer, sized so EVERY field build_def_props appends fits:
758+ * the fixed metrics prefix (PP_PROPS_PREFIX_MAX) plus each optional field's
759+ * whole serialized form. A field that does not fit is dropped whole, so a buffer
760+ * sized for only some of them let a long signature, decorator list or parameter
761+ * list squeeze out the MinHash fingerprint ("fp"), structural profile ("sp") and
762+ * body tokens ("bt") behind them: no SIMILAR_TO edge and fewer
763+ * SEMANTICALLY_RELATED edges for exactly the widest definitions (jellyfin's
764+ * ImageController actions, 2026-10-18). fp/sp/bt are appended behind their own
765+ * room checks, so both the check's reserve and the field are counted. Returns
766+ * `stack` when everything fits it, or when the larger buffer cannot be
767+ * allocated. Twin of pass_definitions.c -- keep both in sync. */
768+ static char * pp_props_buf (const CBMDefinition * def , char * stack , size_t * size ) {
769+ size_t need = PP_PROPS_PREFIX_MAX + pp_str_field_len ("docstring" , def -> docstring );
744770 if (def -> variants && def -> variants [0 ]) {
745771 need += strlen ("variants" ) + strlen (def -> variants ) + PP_JSON_FIELD_OVERHEAD ;
746772 }
747- if (def -> variant_key && def -> variant_key [0 ]) {
748- need +=
749- strlen ("variant_key" ) + pp_json_escaped_len (def -> variant_key ) + PP_JSON_FIELD_OVERHEAD ;
773+ need += pp_str_field_len ("variant_key" , def -> variant_key ) +
774+ pp_str_field_len ("signature" , def -> signature ) +
775+ pp_str_field_len ("return_type" , def -> return_type ) +
776+ pp_str_field_len ("parent_class" , def -> parent_class ) +
777+ pp_arr_field_len ("decorators" , def -> decorators ) +
778+ pp_arr_field_len ("base_classes" , def -> base_classes ) +
779+ pp_arr_field_len ("param_names" , def -> param_names ) +
780+ pp_arr_field_len ("param_types" , def -> param_types ) +
781+ pp_str_field_len ("route_path" , def -> route_path ) +
782+ pp_str_field_len ("route_method" , def -> route_method ) +
783+ pp_str_field_len ("http_client" , def -> http_client ) +
784+ pp_str_field_len ("http_base_url" , def -> http_base_url );
785+ for (const char * * kv = def -> extra_props ; kv && kv [0 ] && kv [SKIP_ONE ]; kv += PAIR_LEN ) {
786+ need += pp_str_field_len (kv [0 ], kv [SKIP_ONE ]);
787+ }
788+ if (def -> fingerprint && def -> fingerprint_k > 0 ) {
789+ need += CBM_MINHASH_HEX_LEN + CBM_MINHASH_JSON_OVERHEAD + SKIP_ONE ;
790+ }
791+ if (def -> structural_profile ) {
792+ need += CBM_AST_PROFILE_BUF + pp_str_field_len ("sp" , def -> structural_profile ) + SKIP_ONE ;
793+ }
794+ if (def -> body_tokens ) {
795+ need += CBM_SZ_512 + pp_str_field_len ("bt" , def -> body_tokens ) + SKIP_ONE ;
750796 }
751- if (need == * size ) {
797+ need += PP_ESC_SPACE ; /* the closing '}' + NUL */
798+ if (need <= * size ) {
752799 return stack ;
753800 }
754801 char * buf = cbm_alloc (CBM_MEM_CLASS_GBUF_STRING , need );
0 commit comments