from __future__ import annotations import json from pathlib import Path from awkweave.execution import run_awk FLOWS_1 = """2026-12-01T10:00:00Z|10.0.0.1|10.0.0.2|TCP|12|1400 2026-12-01T10:00:01Z|10.0.0.3|10.0.0.2|UDP|4|240 2026-12-01T10:00:02Z|10.0.0.1|10.0.0.4|TCP|8|900 2026-12-01T10:00:03Z|10.0.0.2|10.0.0.5|ICMP|1|64 2026-12-01T10:00:04Z|10.0.0.3|10.0.0.1|UDP|7|630 """ FLOWS_2 = """2026-12-01T11:00:00Z|10.1.0.1|10.1.0.2|TCP|5|500 2026-12-01T11:00:01Z|10.1.0.4|10.1.0.1|TCP|9|1100 2026-12-01T11:00:02Z|10.1.0.2|10.1.0.4|UDP|3|180 2026-12-01T11:00:03Z|10.1.0.4|10.1.0.5|TCP|2|250 """ INVENTORY_1 = """sku,category,price,stock A1,book,12.50,3 B2,tool,20.00,0 C3,book,7.50,8 D4,tool,15.00,2 E5,game,30.00,1 """ INVENTORY_2 = """sku,category,price,stock F6,game,10.00,4 G7,book,5.00,0 H8,tool,25.00,3 I9,game,8.00,2 """ SYSLOG_1 = """Dec 01 10:00:00 web01 sshd[101]: Failed password for invalid user admin Dec 01 10:00:01 db01 cron[102]: (root) CMD (backup) Dec 01 10:00:02 web01 sshd[103]: Accepted publickey for alice Dec 01 10:00:03 cache01 redis[104]: Error allocating buffer Dec 01 10:00:04 db01 cron[105]: (root) CMD (cleanup) """ SYSLOG_2 = """Dec 02 11:00:00 api01 sshd[201]: Accepted password for bruno Dec 02 11:00:01 api01 sshd[202]: Failed password for root Dec 02 11:00:02 job01 cron[203]: (daemon) CMD (index) Dec 02 11:00:03 api01 nginx[204]: Error connecting upstream """ GROUPS_1 = """wheel:x:10:alice,bruno developers:x:1000:alice,chen,dina audio:x:29: users:x:100:bruno,eli """ GROUPS_2 = """admins:x:4:farah developers:x:1000:farah,gus video:x:44:gus,hana,ivan """ SENSORS_1 = """s1 temperature 20.5 C s2 humidity 45.0 percent s1 temperature 22.0 C s3 pressure 101.2 kPa s2 humidity 47.5 percent """ SENSORS_2 = """s4 temperature 18.0 C s4 temperature 19.5 C s5 humidity 52.0 percent s6 pressure 100.8 kPa """ def main() -> None: cases = [] def add(family, request, program, input_text, validation_input, difficulty=3): expected = run_awk(program, input_text) validation_expected = run_awk(program, validation_input) if not expected.strip(): raise ValueError(family) cases.append({ "id": f"awkweave-frozen-{len(cases) + 1:04d}", "request": request, "program": program, "input_text": input_text, "expected_output": expected, "validation_input": validation_input, "validation_output": validation_expected, "family": family, "difficulty": difficulty, "split": "frozen_test", "source": "original-frozen-hand-authored", "license": "CC-BY-4.0", }) add("flow_tcp_count", "Using pipe delimiters, count TCP flows and print 0 when none exist.", 'BEGIN{FS="|"}$4=="TCP"{n++}END{print n+0}', FLOWS_1, FLOWS_2) add("flow_bytes_total", "Sum bytes in field 6 of these pipe-delimited flows.", 'BEGIN{FS="|"}{s+=$6}END{print s+0}', FLOWS_1, FLOWS_2) add("flow_protocol_bytes", "For pipe-delimited flows, total bytes per protocol in first-appearance order.", 'BEGIN{FS="|"}{if(!($4 in sums))order[++n]=$4;sums[$4]+=$6}END{for(i=1;i<=n;i++)print order[i],sums[order[i]]}', FLOWS_1, FLOWS_2) add("flow_udp_average", "For UDP flows, print average packets to two decimals. Fields are pipe-delimited.", 'BEGIN{FS="|"}$4=="UDP"{s+=$5;n++}END{printf "%.2f\\n",s/n}', FLOWS_1, FLOWS_2) add("flow_destination_count", "Count distinct destination IPs in field 3 of this pipe-delimited data.", 'BEGIN{FS="|"}{if(!seen[$3]++)n++}END{print n+0}', FLOWS_1, FLOWS_2) add("flow_top_sources", "Print the two complete flows with the most bytes, highest first and stable on ties.", 'BEGIN{FS="|"}{v[NR]=$6+0;line[NR]=$0}END{for(i=2;i<=NR;i++){x=v[i];s=line[i];j=i-1;while(j>=1&&v[j]best{best=$h["price"];record=$0}END{print record}', INVENTORY_1, INVENTORY_2) add("syslog_failed_count", "Count syslog lines containing the exact word Failed.", '$0 ~ /Failed/{n++}END{print n+0}', SYSLOG_1, SYSLOG_2) add("syslog_sshd_hosts", "Print hostnames (field 4) for records whose process field begins with sshd.", '$5 ~ /^sshd/{print $4}', SYSLOG_1, SYSLOG_2) add("syslog_error_casefold", "Print complete syslog records containing error in any letter case.", 'tolower($0) ~ /error/', SYSLOG_1, SYSLOG_2) add("syslog_process_counts", "Extract the process name before [ in field 5 and count records per process in first-appearance order.", '{split($5,p,"[");if(!(p[1] in counts))order[++n]=p[1];counts[p[1]]++}END{for(i=1;i<=n;i++)print order[i],counts[order[i]]}', SYSLOG_1, SYSLOG_2) add("syslog_cron_timestamp", "For cron records, print month, day, and time (the first three fields).", '$5 ~ /^cron/{print $1,$2,$3}', SYSLOG_1, SYSLOG_2) add("syslog_unique_hosts", "Count distinct hostnames in field 4 of the syslog input.", '{if(!seen[$4]++)n++}END{print n+0}', SYSLOG_1, SYSLOG_2) add("group_membership", "In colon-delimited group data, print group names containing alice as a comma-separated member of field 4.", 'BEGIN{FS=":"}{split($4,members,",");for(i in members)if(members[i]=="alice")print $1}', GROUPS_1, GROUPS_2) add("group_member_counts", "For each colon-delimited group, print its name and number of members; an empty fourth field means zero.", 'BEGIN{FS=":"}{n=split($4,members,",");if($4=="")n=0;print $1,n}', GROUPS_1, GROUPS_2) add("group_gid_filter", "Print names of colon-delimited groups with numeric GID at least 1000.", 'BEGIN{FS=":"}$3>=1000{print $1}', GROUPS_1, GROUPS_2) add("sensor_device_average", "For each device in column 1, print its average reading from column 3 to two decimals in first-appearance order.", '{if(!($1 in sums))order[++n]=$1;sums[$1]+=$3;counts[$1]++}END{for(i=1;i<=n;i++)printf "%s %.2f\\n",order[i],sums[order[i]]/counts[order[i]]}', SENSORS_1, SENSORS_2) add("sensor_threshold", "Print complete readings with value in column 3 greater than 45.", '$3>45', SENSORS_1, SENSORS_2) add("sensor_metric_max", "For each metric in column 2, print its maximum value from column 3 in first-appearance order.", '{if(!($2 in seen)){order[++n]=$2;seen[$2]=1;max[$2]=$3}if($3>max[$2])max[$2]=$3}END{for(i=1;i<=n;i++)print order[i],max[order[i]]}', SENSORS_1, SENSORS_2) add("sensor_device_count", "Count distinct device identifiers in column 1.", '{if(!seen[$1]++)n++}END{print n+0}', SENSORS_1, SENSORS_2) add("sensor_unit_filter", "Print device and value for readings whose unit in column 4 is C.", '$4=="C"{print $1,$3}', SENSORS_1, SENSORS_2) add("tsv_to_csv", "Convert tab-separated rows to comma-separated rows while preserving all fields.", 'BEGIN{FS="\\t";OFS=","}{$1=$1;print}', "a\t1\tx\nb\t2\ty\n", "c\t3\tz\nd\t4\tw\n") add("running_average", "For each record, print column 1 and the running mean of column 3 to two decimals.", '{s+=$3;n++;printf "%s %.2f\\n",$1,s/n}', SENSORS_1, SENSORS_2) add("median_value", "Print the median of column 3; for an even number of records use the lower middle value.", '{v[NR]=$3+0}END{for(i=2;i<=NR;i++){x=v[i];j=i-1;while(j>=1&&v[j]>x){v[j+1]=v[j];j--}v[j+1]=x}print v[int((NR+1)/2)]}', SENSORS_1, SENSORS_2) add("comment_filter", "Remove blank lines and lines whose first non-whitespace character is #.", '$0 ~ /[^[:space:]]/ && $0 !~ /^[[:space:]]*#/', "# comment\na 1\n\n # another\nb 2\n", "c 3\n# note\n\nd 4\n") add("comment_count", "Count lines whose first non-whitespace character is #, excluding blank lines.", '$0 ~ /^[[:space:]]*#/{n++}END{print n+0}', "# comment\na 1\n\n # another\nb 2\n", "c 3\n# note\n\nd 4\n") add("trim_lines", "Trim leading and trailing whitespace from each line without altering internal spacing.", '{gsub(/^[[:space:]]+|[[:space:]]+$/,"",$0);print}', " a 1 \n\tb 2\t\n", " c 3 \n d 4 \n") add("iso_date_parts", "For ISO timestamps in field 1, print year, month, and day separated by hyphens.", 'BEGIN{OFS="-"}{split($1,date,"-");split(date[3],parts,"T");print date[1],date[2],parts[1]}', FLOWS_1, FLOWS_2) add("sort_numeric_ascending", "Print complete records sorted by column 3 ascending, preserving input order on ties.", '{v[NR]=$3+0;line[NR]=$0}END{for(i=2;i<=NR;i++){x=v[i];s=line[i];j=i-1;while(j>=1&&v[j]>x){v[j+1]=v[j];line[j+1]=line[j];j--}v[j+1]=x;line[j+1]=s}for(i=1;i<=NR;i++)print line[i]}', SENSORS_1, SENSORS_2) add("sum_if_metric", "Sum column 3 only for temperature records, printing 0 when no such records exist.", '$2=="temperature"{s+=$3}END{print s+0}', SENSORS_1, SENSORS_2) add("first_record_per_device", "Print only the first complete record for each device in column 1.", '!seen[$1]++', SENSORS_1, SENSORS_2) add("count_units", "Count records per unit in column 4 in first-appearance order.", '{if(!($4 in counts))order[++n]=$4;counts[$4]++}END{for(i=1;i<=n;i++)print order[i],counts[order[i]]}', SENSORS_1, SENSORS_2) output = Path("frozen-eval.jsonl") with output.open("w", encoding="utf-8") as handle: for case in cases: handle.write(json.dumps(case, ensure_ascii=False, separators=(",", ":")) + "\n") print(f"wrote {len(cases)} frozen cases to {output}") if __name__ == "__main__": main()