<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Alex Panchenko</title>
    <description>The latest articles on DEV Community by Alex Panchenko (@twe4k).</description>
    <link>https://dev.to/twe4k</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4266982%2F17e0d516-78e3-4300-b92f-67941d97d079.png</url>
      <title>DEV Community: Alex Panchenko</title>
      <link>https://dev.to/twe4k</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/twe4k"/>
    <language>en</language>
    <item>
      <title>I tested Apple's on-device model in 54 languages</title>
      <dc:creator>Alex Panchenko</dc:creator>
      <pubDate>Sat, 28 Nov 2026 01:27:56 +0000</pubDate>
      <link>https://dev.to/twe4k/i-tested-apples-on-device-model-in-54-languages-3bd8</link>
      <guid>https://dev.to/twe4k/i-tested-apples-on-device-model-in-54-languages-3bd8</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR.&lt;/strong&gt; Apple's on-device model summarized 52 of 54 languages well enough to use, including dozens&lt;br&gt;
that aren't on Apple's list. Asked for a summary in Ukrainian, it wrote in the page's language instead on&lt;br&gt;
50 of 53 pages. Repeating the language once more after the page text brought that down to one. CJK text&lt;br&gt;
costs about twice as many tokens per character as English.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;My Safari extension, Epitome, summarizes web pages with the language model built into Apple devices (the&lt;br&gt;
Foundation Models framework). Officially Apple lists only 15 languages for that model. But after digging&lt;br&gt;
around and experimenting, I found it knows a lot more of them: it understands text in them and writes in&lt;br&gt;
them too. So I dug further and tested it properly, in September and again in November.&lt;/p&gt;
&lt;h2&gt;
  
  
  How I tested
&lt;/h2&gt;

&lt;p&gt;I took the Wikipedia article about the Moon in 54 languages. The topic is the same everywhere, so the&lt;br&gt;
articles are comparable, though not identical: each language has its own article, of different length and&lt;br&gt;
detail. That hardly matters for which language the answer comes out in, or for how many tokens a script&lt;br&gt;
costs. The quality of a summary, though, can depend on the article as much as on the language.&lt;/p&gt;

&lt;p&gt;Each article got the short-summary prompt the extension really uses, on a Mac with macOS 27, where the&lt;br&gt;
model has an 8,192-token window. I checked the language of each summary with &lt;code&gt;NLLanguageRecognizer&lt;/code&gt; and&lt;br&gt;
read every mismatch myself. The recognizer is often wrong on short texts (it took a Belarusian summary for&lt;br&gt;
Kazakh), so reading them mattered.&lt;/p&gt;
&lt;h2&gt;
  
  
  Summary in the page's own language
&lt;/h2&gt;

&lt;p&gt;52 of the 54 worked. Every language on Apple's list was fine, and so were 37 that aren't on it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjkj6d21lmjah3rrcus9t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fjkj6d21lmjah3rrcus9t.png" alt="Green: usable. Amber: usable, with mistakes. Red: mixed with another language. A dot marks the 15 languages on Apple's list."&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Green: usable. Amber: usable, with mistakes. Red: mixed with another language. A dot marks the 15 languages on Apple's list.&lt;/em&gt;&lt;br&gt;
Belarusian and Serbian failed. The Belarusian summary drifted into Polish, the Serbian one into Russian.&lt;/p&gt;

&lt;p&gt;A few smaller languages had mistakes that didn't stop me from reading the summary: Persian used the wrong&lt;br&gt;
word for "Moon", and Galician, Slovenian and Latvian had grammar errors.&lt;/p&gt;

&lt;p&gt;So I don't filter languages by &lt;code&gt;supportedLanguages&lt;/code&gt;. Ukrainian, my own language, isn't on that list, and&lt;br&gt;
the Ukrainian article came out fine.&lt;/p&gt;
&lt;h2&gt;
  
  
  Summary in a different language
&lt;/h2&gt;

&lt;p&gt;This is where it got interesting. Say the page is in Japanese and you want the summary in Ukrainian. In&lt;br&gt;
November, on macOS 27.2.1, I asked for a Ukrainian summary of each of the other 53 articles. On 50 of them&lt;br&gt;
the model ignored that and wrote in the page's language, Japanese, German and English included. Only the&lt;br&gt;
Kazakh, Tagalog and Urdu pages got a Ukrainian answer.&lt;/p&gt;

&lt;p&gt;The "write in Ukrainian" instruction was in the system instructions, which is where you'd normally put&lt;br&gt;
it. What helped was repeating it after the page text, so it's the last thing the model reads:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;page text&amp;gt;
---
Write the summary in Ukrainian (українська).
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With that line, 52 of the 53 summaries came out in Ukrainian. The Bulgarian page still got a Bulgarian&lt;br&gt;
answer.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3k7ft4kddttxg36g30lg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3k7ft4kddttxg36g30lg.png" alt="The same 53 pages, without and with the extra line."&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The same 53 pages, without and with the extra line.&lt;/em&gt;&lt;br&gt;
In September I ran the same test with a different target language on an earlier build, and only 18 of 53&lt;br&gt;
pages ignored it. So how often the model drifts changes from build to build (whether it's the OS or Apple&lt;br&gt;
updated the model itself, I can't tell), but the fix held both times.&lt;/p&gt;

&lt;p&gt;I write the language name twice, in English and in the language itself. It's a few extra tokens, and&lt;br&gt;
there's no doubt which language I mean.&lt;/p&gt;

&lt;p&gt;I think it's about distance. A few thousand tokens of Swedish pull the model toward Swedish, and an&lt;br&gt;
instruction from way back at the start can't compete with that. When the instruction comes last, it wins.&lt;br&gt;
Every prompt in Epitome ends with that line now, including questions about a page and video transcripts.&lt;/p&gt;

&lt;p&gt;Getting the language right doesn't make the Ukrainian good, though. Some summaries were clumsy, with&lt;br&gt;
made-up words, and a few got facts wrong: one said the Moon formed 45 million years ago instead of 4.5&lt;br&gt;
billion. That's the small model, not the prompt. Apple's cloud model (Private Cloud Compute) writes&lt;br&gt;
noticeably better, which is why Epitome hands the summary to it when the cloud switch is on.&lt;/p&gt;

&lt;h2&gt;
  
  
  How much text fits
&lt;/h2&gt;

&lt;p&gt;The window is counted in tokens, and scripts differ a lot in how many tokens they need.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1cfl12wcuky0mdok9guw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1cfl12wcuky0mdok9guw.png" alt="Measured on the same 54 articles."&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Measured on the same 54 articles.&lt;/em&gt;&lt;br&gt;
A Chinese or Japanese page gets about half as much text into the window as an English one. If I trimmed&lt;br&gt;
by characters, I'd either leave half the window empty on English pages or overflow it on Japanese ones, so&lt;br&gt;
Epitome trims by the model's own token count.&lt;/p&gt;

&lt;h2&gt;
  
  
  Smaller things
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Now and then the model put a label on the first paragraph, "Intro:" or the Hindi word for it. One line
in the instructions fixes that: no label such as "Intro".&lt;/li&gt;
&lt;li&gt;Later I noticed that the device model sometimes mangled short English pages when asked to summarize them
into Slavic languages. That's one reason Epitome sends every summary to Apple's cloud model (Private
Cloud Compute) when its cloud switch is on, not only the pages too long for the device. The switch is
off by default.&lt;/li&gt;
&lt;li&gt;It depends on the device. In the 27 releases, Macs and the iPhone Air and 17 Pro get a bigger version of
the model than other devices (my iPhone 16 Pro gets the smaller one, with a 4,096-token window), and OS
updates change how it behaves. I'll have to run this again after each major update.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you're building on Foundation Models for people who don't read English, test the "page in one&lt;br&gt;
language, summary in another" case on its own. The same-language results had me thinking everything was&lt;br&gt;
fine.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Epitome summarizes pages, videos and PDFs in Safari in about 50 languages, on Mac, iPhone and iPad:&lt;br&gt;
&lt;a href="https://opanchenko.com/epitome/" rel="noopener noreferrer"&gt;opanchenko.com/epitome&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
      <category>ai</category>
      <category>machinelearning</category>
    </item>
    <item>
      <title>Safari's native messaging can't stream, so I long-poll the on-device model</title>
      <dc:creator>Alex Panchenko</dc:creator>
      <pubDate>Sat, 28 Nov 2026 01:27:50 +0000</pubDate>
      <link>https://dev.to/twe4k/safaris-native-messaging-cant-stream-so-i-long-poll-the-on-device-model-20d</link>
      <guid>https://dev.to/twe4k/safaris-native-messaging-cant-stream-so-i-long-poll-the-on-device-model-20d</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR.&lt;/strong&gt; &lt;code&gt;sendNativeMessage&lt;/code&gt; in Safari returns exactly one reply. To stream a model's output through it,&lt;br&gt;
the background script asks "anything after character N?" in a loop, and the Swift side holds each&lt;br&gt;
request for up to a second until the text grows. Every reply carries the whole text so far, counted in&lt;br&gt;
UTF-16. Then keep the background script alive yourself, and don't write to &lt;code&gt;storage.session&lt;/code&gt; per poll.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I'm working on Epitome, a Safari extension for Mac, iPhone and iPad. It summarizes the current page with&lt;br&gt;
the model Apple ships on the device (the Foundation Models framework).&lt;/p&gt;

&lt;p&gt;On my MacBook Pro with an M5 Pro the first words come back after about two seconds, and a summary of a&lt;br&gt;
long article takes 8 to 15. These are my numbers, not a benchmark: on other Macs and on iPhones they'll&lt;br&gt;
be different.&lt;br&gt;
When I tried showing nothing for 15 seconds and then the whole text at once, it felt like the extension&lt;br&gt;
had hung. So the text had to appear while the model writes it.&lt;/p&gt;

&lt;p&gt;The problem: a Safari Web Extension reaches its native code through &lt;code&gt;runtime.sendNativeMessage&lt;/code&gt;, and each&lt;br&gt;
message gets exactly one reply. As far as I could find, that's all there is, on the Mac and on iOS.&lt;/p&gt;
&lt;h2&gt;
  
  
  Start a job, then keep asking
&lt;/h2&gt;

&lt;p&gt;The model runs in the native half, an app extension written in Swift. The background script sends&lt;br&gt;
&lt;code&gt;start&lt;/code&gt; and gets a job id back right away.&lt;/p&gt;

&lt;p&gt;From then on it keeps asking: "anything after character N?" The Swift side doesn't answer at once. It&lt;br&gt;
holds the request for up to a second, until there's more text than N or the job has finished.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxzetjns642d9kkx4d9hh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxzetjns642d9kkx4d9hh.png" alt="Swift answers a poll only when there's new text or a second has passed. The JavaScript loop just sends the next poll as soon as a reply arrives." width="800" height="480"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Swift answers a poll only when there's new text or a second has passed. The JavaScript loop just sends the next poll as soon as a reply arrives.&lt;/em&gt;&lt;br&gt;
Two early decisions paid off later.&lt;/p&gt;

&lt;p&gt;The first: a reply always holds the full text, not a delta. Foundation Models' &lt;code&gt;streamResponse&lt;/code&gt; already&lt;br&gt;
hands you snapshots of the whole response, so Swift doesn't diff anything. On the JavaScript side, a&lt;br&gt;
dropped or duplicated reply doesn't matter, because the next one overwrites the text.&lt;/p&gt;

&lt;p&gt;The second: &lt;code&gt;since&lt;/code&gt; is measured in UTF-16 code units, because that's what &lt;code&gt;string.length&lt;/code&gt; returns in JavaScript.&lt;br&gt;
Swift's &lt;code&gt;String.count&lt;/code&gt; counts grapheme clusters, a different number as soon as the text has an emoji or a&lt;br&gt;
Devanagari conjunct. So Swift compares with &lt;code&gt;text.utf16.count&lt;/code&gt;, and the background just passes&lt;br&gt;
&lt;code&gt;response.text.length&lt;/code&gt; back.&lt;/p&gt;

&lt;p&gt;On the native side the jobs belong to an actor. Here's &lt;code&gt;poll&lt;/code&gt; with the error handling cut out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;actor&lt;/span&gt; &lt;span class="nc"&gt;SummaryJobStore&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;poll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;since&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;JobState&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;job&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;jobs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;   &lt;span class="c1"&gt;// → "jobLost"&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;job&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;done&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;job&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;utf16&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;since&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;waitForChange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;of&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                &lt;span class="c1"&gt;// new text, or 1 s&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;jobs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;                         &lt;span class="c1"&gt;// the whole text so far&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;waitForChange&lt;/code&gt; stores a continuation. Two things can resume it: the generating task writing new text,&lt;br&gt;
or a one-second timer, whichever fires first. When new text wins, the timer gets cancelled. My first&lt;br&gt;
version forgot that step. With a fast model, sleeping timer tasks piled up one after another.&lt;/p&gt;

&lt;p&gt;The JavaScript loop has no timer at all, since every call already blocks on the Swift side:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;runPoll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;jobId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sink&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;PollSink&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;poll&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;PollFn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;since&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sink&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stopRequested&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;sink&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onStopped&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;poll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;jobId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;since&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;sink&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;since&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;          &lt;span class="c1"&gt;// UTF-16, matches the Swift side&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;sink&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;done&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;done&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The job belongs to the background script, and the popup only displays what the background has. You can&lt;br&gt;
close the popup in the middle of a summary, open it a minute later, and the summary will have kept going.&lt;/p&gt;
&lt;h2&gt;
  
  
  Bug 1: Safari unloads the background while it waits
&lt;/h2&gt;

&lt;p&gt;The background is a non-persistent script. Safari unloads it 30 seconds after the last runtime message,&lt;br&gt;
and a &lt;code&gt;sendNativeMessage&lt;/code&gt; call still waiting for its reply doesn't count as one.&lt;/p&gt;

&lt;p&gt;I ran into this in October on an iPhone. A long job kept running after the popup was closed (it builds a&lt;br&gt;
condensed version of a long video), and it stopped 30 seconds later. Every time, with nothing in the logs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9r04qqrlkcw47c2h1s87.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9r04qqrlkcw47c2h1s87.png" alt="The same long job with the popup closed, before and after the fix." width="800" height="336"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;The same long job with the popup closed, before and after the fix.&lt;/em&gt;&lt;br&gt;
The fix isn't pretty, but I haven't found a better one. While any native call is pending, the background&lt;br&gt;
sends a message nobody listens to, every 8 seconds:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;inFlight&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;keepAlive&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ReturnType&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nb"&gt;setInterval&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;holdOpen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;keepAlive&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;keepAlive&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inFlight&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;browser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;runtime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sendMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"keepAlive"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nf"&gt;clearInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;keepAlive&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;keepAlive&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;sendNative&lt;/code&gt; bumps &lt;code&gt;inFlight&lt;/code&gt;, calls &lt;code&gt;holdOpen()&lt;/code&gt;, and decrements the counter in &lt;code&gt;finally&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bug 2: storage.session runs out after a few dozen writes
&lt;/h2&gt;

&lt;p&gt;Originally I wrote the job to &lt;code&gt;storage.session&lt;/code&gt; after every poll, so a popup opened later could pick it&lt;br&gt;
up. The first summary worked fine. Every one after it failed silently. Safari had hit its&lt;br&gt;
&lt;code&gt;storage.session&lt;/code&gt; quota ("Exceeded storage quota") after a few dozen writes.&lt;/p&gt;

&lt;p&gt;Now the running job lives only in the background's memory, and the popup gets updates as runtime&lt;br&gt;
messages. A finished job (done, stopped or failed) is written to storage once.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bug 3: the native process can go away
&lt;/h2&gt;

&lt;p&gt;The system kills the app extension whenever it likes: low memory, an update, a crash. The next poll then&lt;br&gt;
reaches a fresh process that knows nothing about the job.&lt;/p&gt;

&lt;p&gt;So &lt;code&gt;poll&lt;/code&gt; returns &lt;code&gt;nil&lt;/code&gt; for an unknown id, and JavaScript gets it as a &lt;code&gt;jobLost&lt;/code&gt; error. The popup keeps&lt;br&gt;
the text it has already received and says the summary was interrupted. Without this it would show a&lt;br&gt;
spinner that never stops.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bug 4: token counting can take 30 seconds
&lt;/h2&gt;

&lt;p&gt;The page text has to fit in the model's context window, so I trim it before the job starts. For the&lt;br&gt;
length check I ask the model for its own token count. Estimating tokens from the number of characters is&lt;br&gt;
off too often to be useful.&lt;/p&gt;

&lt;p&gt;On my iPhone I saw that call take 10 to 33 seconds while another job was generating: the model does the&lt;br&gt;
counting too, so one request waits for the other. Now, when the answer is known without the model&lt;br&gt;
("nothing to summarize", "too short", "busy"), the request replies first and counts nothing. Otherwise you&lt;br&gt;
get an empty popup and no idea what it's doing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where it ended up
&lt;/h2&gt;

&lt;p&gt;On my M5 Pro the first words show up after about two seconds, and then the text keeps coming at roughly&lt;br&gt;
reading speed. The polls arrive in uneven bursts, so the popup smooths that out and reveals the words at a&lt;br&gt;
steady pace. That part is plain CSS and JavaScript, though, and has nothing to do with native messaging.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Epitome is on the App Store for Mac, iPhone and iPad: &lt;a href="https://opanchenko.com/epitome/" rel="noopener noreferrer"&gt;opanchenko.com/epitome&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>safari</category>
      <category>swift</category>
      <category>webdev</category>
      <category>ios</category>
    </item>
  </channel>
</rss>
